-
Notifications
You must be signed in to change notification settings - Fork 4
/
assl_socket.c
179 lines (147 loc) · 3.15 KB
/
assl_socket.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Copyright (c) 2011 Conformal Systems LLC <info@conformal.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include "assl.h"
#include "assl_internal.h"
int
assl_initialize_sockets()
{
/* Nothing to do. */
return (0);
}
int
assl_shutdown_sockets()
{
/* Nothing to do. */
return (0);
}
int
assl_close_socket(int s)
{
return close(s);
}
int
assl_get_ssl_error(const SSL *ssl, int ret)
{
return SSL_get_error(ssl, ret);
}
void
assl_get_socket_error(int err, char *outstr, int len)
{
strlcpy(outstr, strerror(err), len);
}
BIO *
assl_bio_new_socket(int sock, int close_flag)
{
return BIO_new_socket(sock, close_flag);
}
int
assl_is_nonblock(int s)
{
int r, rv = -1;
r = fcntl(s, F_GETFL, 0);
if (r < 0)
ERROR_OUT(ERR_SOCKET, done);
rv = 0;
if (r & O_NONBLOCK)
rv = 1;
done:
return (rv);
}
int
assl_set_nonblock(int fd)
{
int val, rv = 1;
val = fcntl(fd, F_GETFL, 0);
if (val < 0)
goto done;
if (val & O_NONBLOCK)
return (0);
val |= O_NONBLOCK;
if (fcntl(fd, F_SETFL, val) == -1)
goto done;
rv = 0;
done:
return (rv);
}
int
assl_set_keepalive(int fd)
{
int val = 1;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)) == -1)
return (-1);
return (0);
}
void
assl_set_tos(int fd, int family, int flags)
{
int level, optname, tos;
if (family == AF_INET) {
level = IPPROTO_IP;
optname = IP_TOS;
#ifdef IPV6_TCLASS
} else if (family == AF_INET6) {
level = IPPROTO_IPV6;
optname = IPV6_TCLASS;
#endif
} else
return;
if (flags & ASSL_F_LOWDELAY) {
tos = IPTOS_LOWDELAY;
setsockopt(fd, level, optname, &tos, sizeof(tos));
}
if (flags & ASSL_F_THROUGHPUT) {
tos = IPTOS_THROUGHPUT;
setsockopt(fd, level, optname, &tos, sizeof(tos));
}
}
void
assl_set_recvbuf(int fd, int bufsize)
{
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
}
void
assl_set_sendbuf(int fd, int bufsize)
{
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
}
int
assl_get_recvtimeo(int fd, struct timeval *t)
{
socklen_t sz = sizeof *t;
if (getsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, t, &sz) == -1)
return (-1);
return (0);
}
int
assl_set_recvtimeo(int fd, struct timeval *t)
{
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, t, sizeof *t) == -1)
return (-1);
return (0);
}
int
assl_fd(struct assl_context *c)
{
return c->as_sock;
}