]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
3c7d0945 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
09abbca1 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 RE |
8 | */ |
9 | ||
d02b48c6 | 10 | #include <stdio.h> |
58964a49 | 11 | #include <stdlib.h> |
d02b48c6 | 12 | #include <errno.h> |
706457b7 | 13 | #include "bio_local.h" |
9ba4cc00 | 14 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
15 | # define SOCKET_PROTOCOL IPPROTO_TCP |
16 | # ifdef SO_MAXCONN | |
17 | # define MAX_LISTEN SO_MAXCONN | |
18 | # elif defined(SOMAXCONN) | |
19 | # define MAX_LISTEN SOMAXCONN | |
20 | # else | |
21 | # define MAX_LISTEN 32 | |
22 | # endif | |
1fbab1dc | 23 | # if defined(OPENSSL_SYS_WINDOWS) |
0f113f3e MC |
24 | static int wsa_init_done = 0; |
25 | # endif | |
d02b48c6 | 26 | |
00db8c60 | 27 | # ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
6b691a5c | 28 | int BIO_get_host_ip(const char *str, unsigned char *ip) |
0f113f3e | 29 | { |
5bca70ca RL |
30 | BIO_ADDRINFO *res = NULL; |
31 | int ret = 0; | |
0f113f3e | 32 | |
0f113f3e MC |
33 | if (BIO_sock_init() != 1) |
34 | return 0; /* don't generate another error code here */ | |
35 | ||
5bca70ca RL |
36 | if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) { |
37 | size_t l; | |
0f113f3e | 38 | |
5bca70ca RL |
39 | if (BIO_ADDRINFO_family(res) != AF_INET) { |
40 | BIOerr(BIO_F_BIO_GET_HOST_IP, | |
41 | BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); | |
86f31dd9 MC |
42 | } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l)) { |
43 | /* | |
44 | * Because only AF_INET addresses will reach this far, we can assert | |
45 | * that l should be 4 | |
46 | */ | |
47 | if (ossl_assert(l == 4)) | |
48 | ret = BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l); | |
5bca70ca RL |
49 | } |
50 | BIO_ADDRINFO_free(res); | |
51 | } else { | |
52 | ERR_add_error_data(2, "host=", str); | |
0f113f3e | 53 | } |
ba9f2808 | 54 | |
5bca70ca | 55 | return ret; |
0f113f3e | 56 | } |
d02b48c6 | 57 | |
6b691a5c | 58 | int BIO_get_port(const char *str, unsigned short *port_ptr) |
0f113f3e | 59 | { |
5bca70ca RL |
60 | BIO_ADDRINFO *res = NULL; |
61 | int ret = 0; | |
0f113f3e MC |
62 | |
63 | if (str == NULL) { | |
64 | BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED); | |
26a7d938 | 65 | return 0; |
0f113f3e | 66 | } |
5bca70ca RL |
67 | |
68 | if (BIO_sock_init() != 1) | |
69 | return 0; /* don't generate another error code here */ | |
70 | ||
71 | if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) { | |
72 | if (BIO_ADDRINFO_family(res) != AF_INET) { | |
73 | BIOerr(BIO_F_BIO_GET_PORT, | |
74 | BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET); | |
75 | } else { | |
76 | *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res))); | |
77 | ret = 1; | |
0f113f3e | 78 | } |
5bca70ca RL |
79 | BIO_ADDRINFO_free(res); |
80 | } else { | |
81 | ERR_add_error_data(2, "host=", str); | |
0f113f3e | 82 | } |
5bca70ca RL |
83 | |
84 | return ret; | |
0f113f3e | 85 | } |
d33b215b | 86 | # endif |
d02b48c6 | 87 | |
6b691a5c | 88 | int BIO_sock_error(int sock) |
0f113f3e | 89 | { |
5bca70ca | 90 | int j = 0, i; |
2bd8c853 | 91 | socklen_t size = sizeof(j); |
5bca70ca | 92 | |
0f113f3e MC |
93 | /* |
94 | * Note: under Windows the third parameter is of type (char *) whereas | |
95 | * under other systems it is (void *) if you don't have a cast it will | |
96 | * choke the compiler: if you do have a cast then you can either go for | |
97 | * (char *) or (void *). | |
98 | */ | |
5bca70ca | 99 | i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size); |
0f113f3e | 100 | if (i < 0) |
26a7d938 | 101 | return get_last_socket_error(); |
0f113f3e | 102 | else |
26a7d938 | 103 | return j; |
0f113f3e MC |
104 | } |
105 | ||
00db8c60 | 106 | # ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
6b691a5c | 107 | struct hostent *BIO_gethostbyname(const char *name) |
0f113f3e | 108 | { |
0f113f3e MC |
109 | /* |
110 | * Caching gethostbyname() results forever is wrong, so we have to let | |
111 | * the true gethostbyname() worry about this | |
112 | */ | |
4d428cd2 | 113 | return gethostbyname(name); |
0f113f3e | 114 | } |
d33b215b | 115 | # endif |
c602e7f4 | 116 | |
6b691a5c | 117 | int BIO_sock_init(void) |
0f113f3e MC |
118 | { |
119 | # ifdef OPENSSL_SYS_WINDOWS | |
120 | static struct WSAData wsa_state; | |
121 | ||
122 | if (!wsa_init_done) { | |
0f113f3e MC |
123 | wsa_init_done = 1; |
124 | memset(&wsa_state, 0, sizeof(wsa_state)); | |
125 | /* | |
126 | * Not making wsa_state available to the rest of the code is formally | |
0d4fb843 | 127 | * wrong. But the structures we use are [believed to be] invariable |
0f113f3e MC |
128 | * among Winsock DLLs, while API availability is [expected to be] |
129 | * probed at run-time with DSO_global_lookup. | |
130 | */ | |
131 | if (WSAStartup(0x0202, &wsa_state) != 0) { | |
ff988500 RS |
132 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
133 | "calling wsastartup()"); | |
0f113f3e | 134 | BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP); |
26a7d938 | 135 | return -1; |
0f113f3e MC |
136 | } |
137 | } | |
138 | # endif /* OPENSSL_SYS_WINDOWS */ | |
139 | # ifdef WATT32 | |
140 | extern int _watt_do_exit; | |
141 | _watt_do_exit = 0; /* don't make sock_init() call exit() */ | |
142 | if (sock_init()) | |
26a7d938 | 143 | return -1; |
0f113f3e MC |
144 | # endif |
145 | ||
208fb891 | 146 | return 1; |
0f113f3e | 147 | } |
d02b48c6 | 148 | |
b3599dbb | 149 | void bio_sock_cleanup_int(void) |
0f113f3e MC |
150 | { |
151 | # ifdef OPENSSL_SYS_WINDOWS | |
152 | if (wsa_init_done) { | |
153 | wsa_init_done = 0; | |
4d8743f4 | 154 | WSACleanup(); |
0f113f3e | 155 | } |
0f113f3e MC |
156 | # endif |
157 | } | |
d02b48c6 | 158 | |
c029841e | 159 | int BIO_socket_ioctl(int fd, long type, void *arg) |
0f113f3e MC |
160 | { |
161 | int i; | |
162 | ||
163 | # ifdef __DJGPP__ | |
164 | i = ioctlsocket(fd, type, (char *)arg); | |
165 | # else | |
166 | # if defined(OPENSSL_SYS_VMS) | |
35a1cc90 MC |
167 | /*- |
168 | * 2011-02-18 SMS. | |
169 | * VMS ioctl() can't tolerate a 64-bit "void *arg", but we | |
170 | * observe that all the consumers pass in an "unsigned long *", | |
171 | * so we arrange a local copy with a short pointer, and use | |
172 | * that, instead. | |
173 | */ | |
0f113f3e MC |
174 | # if __INITIAL_POINTER_SIZE == 64 |
175 | # define ARG arg_32p | |
176 | # pragma pointer_size save | |
177 | # pragma pointer_size 32 | |
178 | unsigned long arg_32; | |
179 | unsigned long *arg_32p; | |
180 | # pragma pointer_size restore | |
181 | arg_32p = &arg_32; | |
182 | arg_32 = *((unsigned long *)arg); | |
183 | # else /* __INITIAL_POINTER_SIZE == 64 */ | |
184 | # define ARG arg | |
185 | # endif /* __INITIAL_POINTER_SIZE == 64 [else] */ | |
186 | # else /* defined(OPENSSL_SYS_VMS) */ | |
187 | # define ARG arg | |
188 | # endif /* defined(OPENSSL_SYS_VMS) [else] */ | |
189 | ||
190 | i = ioctlsocket(fd, type, ARG); | |
191 | # endif /* __DJGPP__ */ | |
192 | if (i < 0) | |
ff988500 RS |
193 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
194 | "calling ioctlsocket()"); | |
26a7d938 | 195 | return i; |
0f113f3e | 196 | } |
0f113f3e | 197 | |
00db8c60 | 198 | # ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
6b691a5c | 199 | int BIO_get_accept_socket(char *host, int bind_mode) |
0f113f3e | 200 | { |
5bca70ca RL |
201 | int s = INVALID_SOCKET; |
202 | char *h = NULL, *p = NULL; | |
203 | BIO_ADDRINFO *res = NULL; | |
0f113f3e | 204 | |
5bca70ca RL |
205 | if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV)) |
206 | return INVALID_SOCKET; | |
0f113f3e | 207 | |
5bca70ca RL |
208 | if (BIO_sock_init() != 1) |
209 | return INVALID_SOCKET; | |
0f113f3e | 210 | |
5bca70ca | 211 | if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0) |
0f113f3e MC |
212 | goto err; |
213 | ||
5bca70ca RL |
214 | if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res), |
215 | BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) { | |
216 | s = INVALID_SOCKET; | |
0f113f3e MC |
217 | goto err; |
218 | } | |
0f113f3e | 219 | |
5bca70ca RL |
220 | if (!BIO_listen(s, BIO_ADDRINFO_address(res), |
221 | bind_mode ? BIO_SOCK_REUSEADDR : 0)) { | |
222 | BIO_closesocket(s); | |
223 | s = INVALID_SOCKET; | |
0f113f3e | 224 | } |
5bca70ca | 225 | |
0f113f3e | 226 | err: |
5bca70ca RL |
227 | BIO_ADDRINFO_free(res); |
228 | OPENSSL_free(h); | |
229 | OPENSSL_free(p); | |
230 | ||
231 | return s; | |
0f113f3e | 232 | } |
d02b48c6 | 233 | |
5bca70ca | 234 | int BIO_accept(int sock, char **ip_port) |
0f113f3e | 235 | { |
d9d8e7a9 | 236 | BIO_ADDR res; |
5bca70ca | 237 | int ret = -1; |
0f113f3e | 238 | |
d9d8e7a9 | 239 | ret = BIO_accept_ex(sock, &res, 0); |
b13fdc48 | 240 | if (ret == (int)INVALID_SOCKET) { |
5bca70ca RL |
241 | if (BIO_sock_should_retry(ret)) { |
242 | ret = -2; | |
243 | goto end; | |
244 | } | |
ff988500 RS |
245 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
246 | "calling accept()"); | |
0f113f3e MC |
247 | BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR); |
248 | goto end; | |
249 | } | |
250 | ||
5bca70ca | 251 | if (ip_port != NULL) { |
d9d8e7a9 RS |
252 | char *host = BIO_ADDR_hostname_string(&res, 1); |
253 | char *port = BIO_ADDR_service_string(&res, 1); | |
ad9a0562 MC |
254 | if (host != NULL && port != NULL) |
255 | *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2); | |
256 | else | |
257 | *ip_port = NULL; | |
258 | ||
259 | if (*ip_port == NULL) { | |
260 | BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); | |
261 | BIO_closesocket(ret); | |
262 | ret = (int)INVALID_SOCKET; | |
263 | } else { | |
264 | strcpy(*ip_port, host); | |
265 | strcat(*ip_port, ":"); | |
266 | strcat(*ip_port, port); | |
267 | } | |
5bca70ca RL |
268 | OPENSSL_free(host); |
269 | OPENSSL_free(port); | |
0f113f3e | 270 | } |
5bca70ca | 271 | |
0f113f3e | 272 | end: |
5bca70ca | 273 | return ret; |
0f113f3e | 274 | } |
d33b215b | 275 | # endif |
d02b48c6 | 276 | |
6b691a5c | 277 | int BIO_set_tcp_ndelay(int s, int on) |
0f113f3e MC |
278 | { |
279 | int ret = 0; | |
280 | # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) | |
281 | int opt; | |
d02b48c6 | 282 | |
0f113f3e MC |
283 | # ifdef SOL_TCP |
284 | opt = SOL_TCP; | |
285 | # else | |
286 | # ifdef IPPROTO_TCP | |
287 | opt = IPPROTO_TCP; | |
288 | # endif | |
289 | # endif | |
dfeab068 | 290 | |
0f113f3e MC |
291 | ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on)); |
292 | # endif | |
293 | return (ret == 0); | |
294 | } | |
295 | ||
296 | int BIO_socket_nbio(int s, int mode) | |
297 | { | |
298 | int ret = -1; | |
299 | int l; | |
300 | ||
301 | l = mode; | |
302 | # ifdef FIONBIO | |
27f172d9 RS |
303 | l = mode; |
304 | ||
0f113f3e | 305 | ret = BIO_socket_ioctl(s, FIONBIO, &l); |
27f172d9 RS |
306 | # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY)) |
307 | /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */ | |
308 | ||
309 | l = fcntl(s, F_GETFL, 0); | |
310 | if (l == -1) { | |
ff988500 RS |
311 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
312 | "calling fcntl()"); | |
27f172d9 RS |
313 | ret = -1; |
314 | } else { | |
315 | # if defined(O_NONBLOCK) | |
316 | l &= ~O_NONBLOCK; | |
317 | # else | |
318 | l &= ~FNDELAY; /* BSD4.x */ | |
319 | # endif | |
320 | if (mode) { | |
321 | # if defined(O_NONBLOCK) | |
322 | l |= O_NONBLOCK; | |
323 | # else | |
324 | l |= FNDELAY; /* BSD4.x */ | |
325 | # endif | |
326 | } | |
327 | ret = fcntl(s, F_SETFL, l); | |
328 | ||
329 | if (ret < 0) { | |
ff988500 RS |
330 | ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(), |
331 | "calling fcntl()"); | |
27f172d9 RS |
332 | } |
333 | } | |
334 | # else | |
335 | /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */ | |
336 | BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT); | |
0f113f3e | 337 | # endif |
27f172d9 | 338 | |
0f113f3e MC |
339 | return (ret == 0); |
340 | } | |
d33b215b RL |
341 | |
342 | int BIO_sock_info(int sock, | |
343 | enum BIO_sock_info_type type, union BIO_sock_info_u *info) | |
344 | { | |
345 | switch (type) { | |
346 | case BIO_SOCK_INFO_ADDRESS: | |
347 | { | |
348 | socklen_t addr_len; | |
349 | int ret = 0; | |
350 | addr_len = sizeof(*info->addr); | |
351 | ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr), | |
352 | &addr_len); | |
353 | if (ret == -1) { | |
ff988500 RS |
354 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
355 | "calling getsockname()"); | |
d33b215b RL |
356 | BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR); |
357 | return 0; | |
358 | } | |
59d9bb59 | 359 | if ((size_t)addr_len > sizeof(*info->addr)) { |
d33b215b RL |
360 | BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS); |
361 | return 0; | |
362 | } | |
363 | } | |
364 | break; | |
365 | default: | |
366 | BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE); | |
367 | return 0; | |
368 | } | |
369 | return 1; | |
370 | } | |
371 | ||
4a1fbd13 | 372 | #endif |