]>
Commit | Line | Data |
---|---|---|
58964a49 | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
d02b48c6 RE |
2 | * All rights reserved. |
3 | * | |
4 | * This package is an SSL implementation written | |
5 | * by Eric Young (eay@cryptsoft.com). | |
6 | * The implementation was written so as to conform with Netscapes SSL. | |
0f113f3e | 7 | * |
d02b48c6 RE |
8 | * This library is free for commercial and non-commercial use as long as |
9 | * the following conditions are aheared to. The following conditions | |
10 | * apply to all code found in this distribution, be it the RC4, RSA, | |
11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
12 | * included with this distribution is covered by the same copyright terms | |
13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
0f113f3e | 14 | * |
d02b48c6 RE |
15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | * the code are not to be removed. | |
17 | * If this package is used in a product, Eric Young should be given attribution | |
18 | * as the author of the parts of the library used. | |
19 | * This can be in the form of a textual message at program startup or | |
20 | * in documentation (online or textual) provided with the package. | |
0f113f3e | 21 | * |
d02b48c6 RE |
22 | * Redistribution and use in source and binary forms, with or without |
23 | * modification, are permitted provided that the following conditions | |
24 | * are met: | |
25 | * 1. Redistributions of source code must retain the copyright | |
26 | * notice, this list of conditions and the following disclaimer. | |
27 | * 2. Redistributions in binary form must reproduce the above copyright | |
28 | * notice, this list of conditions and the following disclaimer in the | |
29 | * documentation and/or other materials provided with the distribution. | |
30 | * 3. All advertising materials mentioning features or use of this software | |
31 | * must display the following acknowledgement: | |
32 | * "This product includes cryptographic software written by | |
33 | * Eric Young (eay@cryptsoft.com)" | |
34 | * The word 'cryptographic' can be left out if the rouines from the library | |
35 | * being used are not cryptographic related :-). | |
0f113f3e | 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
d02b48c6 RE |
37 | * the apps directory (application code) you must include an acknowledgement: |
38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
0f113f3e | 39 | * |
d02b48c6 RE |
40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
50 | * SUCH DAMAGE. | |
0f113f3e | 51 | * |
d02b48c6 RE |
52 | * The licence and distribution terms for any publically available version or |
53 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
54 | * copied and put under another distribution licence | |
55 | * [including the GNU Public Licence.] | |
56 | */ | |
57 | ||
d02b48c6 | 58 | #include <stdio.h> |
58964a49 | 59 | #include <stdlib.h> |
d02b48c6 | 60 | #include <errno.h> |
28a0841b RL |
61 | #include "bio_lcl.h" |
62 | #if defined(NETWARE_CLIB) | |
63 | # include <sys/ioctl.h> | |
eef0c1f3 DSH |
64 | NETDB_DEFINE_CONTEXT |
65 | #endif | |
9ba4cc00 | 66 | #ifndef OPENSSL_NO_SOCK |
0f113f3e MC |
67 | # define SOCKET_PROTOCOL IPPROTO_TCP |
68 | # ifdef SO_MAXCONN | |
69 | # define MAX_LISTEN SO_MAXCONN | |
70 | # elif defined(SOMAXCONN) | |
71 | # define MAX_LISTEN SOMAXCONN | |
72 | # else | |
73 | # define MAX_LISTEN 32 | |
74 | # endif | |
75 | # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)) | |
76 | static int wsa_init_done = 0; | |
77 | # endif | |
d02b48c6 | 78 | |
d33b215b | 79 | # if OPENSSL_API_COMPAT < 0x10100000L |
6b691a5c | 80 | int BIO_get_host_ip(const char *str, unsigned char *ip) |
0f113f3e | 81 | { |
5bca70ca RL |
82 | BIO_ADDRINFO *res = NULL; |
83 | int ret = 0; | |
0f113f3e | 84 | |
0f113f3e MC |
85 | if (BIO_sock_init() != 1) |
86 | return 0; /* don't generate another error code here */ | |
87 | ||
5bca70ca RL |
88 | if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) { |
89 | size_t l; | |
0f113f3e | 90 | |
5bca70ca RL |
91 | if (BIO_ADDRINFO_family(res) != AF_INET) { |
92 | BIOerr(BIO_F_BIO_GET_HOST_IP, | |
93 | BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET); | |
94 | } else { | |
95 | BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l); | |
96 | /* Because only AF_INET addresses will reach this far, | |
97 | we can assert that l should be 4 */ | |
98 | OPENSSL_assert(l == 4); | |
0f113f3e | 99 | |
5bca70ca RL |
100 | BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l); |
101 | ret = 1; | |
102 | } | |
103 | BIO_ADDRINFO_free(res); | |
104 | } else { | |
105 | ERR_add_error_data(2, "host=", str); | |
0f113f3e | 106 | } |
ba9f2808 | 107 | |
5bca70ca | 108 | return ret; |
0f113f3e | 109 | } |
d02b48c6 | 110 | |
6b691a5c | 111 | int BIO_get_port(const char *str, unsigned short *port_ptr) |
0f113f3e | 112 | { |
5bca70ca RL |
113 | BIO_ADDRINFO *res = NULL; |
114 | int ret = 0; | |
0f113f3e MC |
115 | |
116 | if (str == NULL) { | |
117 | BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED); | |
118 | return (0); | |
119 | } | |
5bca70ca RL |
120 | |
121 | if (BIO_sock_init() != 1) | |
122 | return 0; /* don't generate another error code here */ | |
123 | ||
124 | if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) { | |
125 | if (BIO_ADDRINFO_family(res) != AF_INET) { | |
126 | BIOerr(BIO_F_BIO_GET_PORT, | |
127 | BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET); | |
128 | } else { | |
129 | *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res))); | |
130 | ret = 1; | |
0f113f3e | 131 | } |
5bca70ca RL |
132 | BIO_ADDRINFO_free(res); |
133 | } else { | |
134 | ERR_add_error_data(2, "host=", str); | |
0f113f3e | 135 | } |
5bca70ca RL |
136 | |
137 | return ret; | |
0f113f3e | 138 | } |
d33b215b | 139 | # endif |
d02b48c6 | 140 | |
6b691a5c | 141 | int BIO_sock_error(int sock) |
0f113f3e | 142 | { |
5bca70ca RL |
143 | int j = 0, i; |
144 | socklen_t size = 0; | |
145 | ||
0f113f3e MC |
146 | /* |
147 | * Note: under Windows the third parameter is of type (char *) whereas | |
148 | * under other systems it is (void *) if you don't have a cast it will | |
149 | * choke the compiler: if you do have a cast then you can either go for | |
150 | * (char *) or (void *). | |
151 | */ | |
5bca70ca | 152 | i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size); |
0f113f3e MC |
153 | if (i < 0) |
154 | return (1); | |
155 | else | |
156 | return (j); | |
157 | } | |
158 | ||
d33b215b | 159 | # if OPENSSL_API_COMPAT < 0x10100000L |
6b691a5c | 160 | struct hostent *BIO_gethostbyname(const char *name) |
0f113f3e | 161 | { |
0f113f3e MC |
162 | /* |
163 | * Caching gethostbyname() results forever is wrong, so we have to let | |
164 | * the true gethostbyname() worry about this | |
165 | */ | |
5bca70ca | 166 | # if (defined(NETWARE_BSDSOCK) && !defined(__NOVELL_LIBC__)) |
0f113f3e | 167 | return gethostbyname((char *)name); |
5bca70ca | 168 | # else |
4d428cd2 | 169 | return gethostbyname(name); |
5bca70ca | 170 | # endif |
0f113f3e | 171 | } |
d33b215b | 172 | # endif |
c602e7f4 | 173 | |
6b691a5c | 174 | int BIO_sock_init(void) |
0f113f3e MC |
175 | { |
176 | # ifdef OPENSSL_SYS_WINDOWS | |
177 | static struct WSAData wsa_state; | |
178 | ||
179 | if (!wsa_init_done) { | |
180 | int err; | |
181 | ||
182 | wsa_init_done = 1; | |
183 | memset(&wsa_state, 0, sizeof(wsa_state)); | |
184 | /* | |
185 | * Not making wsa_state available to the rest of the code is formally | |
0d4fb843 | 186 | * wrong. But the structures we use are [believed to be] invariable |
0f113f3e MC |
187 | * among Winsock DLLs, while API availability is [expected to be] |
188 | * probed at run-time with DSO_global_lookup. | |
189 | */ | |
190 | if (WSAStartup(0x0202, &wsa_state) != 0) { | |
191 | err = WSAGetLastError(); | |
192 | SYSerr(SYS_F_WSASTARTUP, err); | |
193 | BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP); | |
194 | return (-1); | |
195 | } | |
196 | } | |
197 | # endif /* OPENSSL_SYS_WINDOWS */ | |
198 | # ifdef WATT32 | |
199 | extern int _watt_do_exit; | |
200 | _watt_do_exit = 0; /* don't make sock_init() call exit() */ | |
201 | if (sock_init()) | |
202 | return (-1); | |
203 | # endif | |
204 | ||
205 | # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK) | |
4d8743f4 RL |
206 | WORD wVerReq; |
207 | WSADATA wsaData; | |
208 | int err; | |
209 | ||
0f113f3e MC |
210 | if (!wsa_init_done) { |
211 | wsa_init_done = 1; | |
212 | wVerReq = MAKEWORD(2, 0); | |
213 | err = WSAStartup(wVerReq, &wsaData); | |
214 | if (err != 0) { | |
215 | SYSerr(SYS_F_WSASTARTUP, err); | |
216 | BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP); | |
217 | return (-1); | |
218 | } | |
219 | } | |
220 | # endif | |
221 | ||
222 | return (1); | |
223 | } | |
d02b48c6 | 224 | |
6b691a5c | 225 | void BIO_sock_cleanup(void) |
0f113f3e MC |
226 | { |
227 | # ifdef OPENSSL_SYS_WINDOWS | |
228 | if (wsa_init_done) { | |
229 | wsa_init_done = 0; | |
4d8743f4 | 230 | WSACleanup(); |
0f113f3e MC |
231 | } |
232 | # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK) | |
233 | if (wsa_init_done) { | |
234 | wsa_init_done = 0; | |
235 | WSACleanup(); | |
236 | } | |
237 | # endif | |
238 | } | |
d02b48c6 | 239 | |
0f113f3e | 240 | # if !defined(OPENSSL_SYS_VMS) || __VMS_VER >= 70000000 |
7d7d2cbc | 241 | |
c029841e | 242 | int BIO_socket_ioctl(int fd, long type, void *arg) |
0f113f3e MC |
243 | { |
244 | int i; | |
245 | ||
246 | # ifdef __DJGPP__ | |
247 | i = ioctlsocket(fd, type, (char *)arg); | |
248 | # else | |
249 | # if defined(OPENSSL_SYS_VMS) | |
35a1cc90 MC |
250 | /*- |
251 | * 2011-02-18 SMS. | |
252 | * VMS ioctl() can't tolerate a 64-bit "void *arg", but we | |
253 | * observe that all the consumers pass in an "unsigned long *", | |
254 | * so we arrange a local copy with a short pointer, and use | |
255 | * that, instead. | |
256 | */ | |
0f113f3e MC |
257 | # if __INITIAL_POINTER_SIZE == 64 |
258 | # define ARG arg_32p | |
259 | # pragma pointer_size save | |
260 | # pragma pointer_size 32 | |
261 | unsigned long arg_32; | |
262 | unsigned long *arg_32p; | |
263 | # pragma pointer_size restore | |
264 | arg_32p = &arg_32; | |
265 | arg_32 = *((unsigned long *)arg); | |
266 | # else /* __INITIAL_POINTER_SIZE == 64 */ | |
267 | # define ARG arg | |
268 | # endif /* __INITIAL_POINTER_SIZE == 64 [else] */ | |
269 | # else /* defined(OPENSSL_SYS_VMS) */ | |
270 | # define ARG arg | |
271 | # endif /* defined(OPENSSL_SYS_VMS) [else] */ | |
272 | ||
273 | i = ioctlsocket(fd, type, ARG); | |
274 | # endif /* __DJGPP__ */ | |
275 | if (i < 0) | |
276 | SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error()); | |
277 | return (i); | |
278 | } | |
279 | # endif /* __VMS_VER */ | |
280 | ||
d33b215b | 281 | # if OPENSSL_API_COMPAT < 0x10100000L |
6b691a5c | 282 | int BIO_get_accept_socket(char *host, int bind_mode) |
0f113f3e | 283 | { |
5bca70ca RL |
284 | int s = INVALID_SOCKET; |
285 | char *h = NULL, *p = NULL; | |
286 | BIO_ADDRINFO *res = NULL; | |
0f113f3e | 287 | |
5bca70ca RL |
288 | if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV)) |
289 | return INVALID_SOCKET; | |
0f113f3e | 290 | |
5bca70ca RL |
291 | if (BIO_sock_init() != 1) |
292 | return INVALID_SOCKET; | |
0f113f3e | 293 | |
5bca70ca | 294 | if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0) |
0f113f3e MC |
295 | goto err; |
296 | ||
5bca70ca RL |
297 | if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res), |
298 | BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) { | |
299 | s = INVALID_SOCKET; | |
0f113f3e MC |
300 | goto err; |
301 | } | |
0f113f3e | 302 | |
5bca70ca RL |
303 | if (!BIO_listen(s, BIO_ADDRINFO_address(res), |
304 | bind_mode ? BIO_SOCK_REUSEADDR : 0)) { | |
305 | BIO_closesocket(s); | |
306 | s = INVALID_SOCKET; | |
0f113f3e | 307 | } |
5bca70ca | 308 | |
0f113f3e | 309 | err: |
5bca70ca RL |
310 | BIO_ADDRINFO_free(res); |
311 | OPENSSL_free(h); | |
312 | OPENSSL_free(p); | |
313 | ||
314 | return s; | |
0f113f3e | 315 | } |
d02b48c6 | 316 | |
5bca70ca | 317 | int BIO_accept(int sock, char **ip_port) |
0f113f3e | 318 | { |
5bca70ca RL |
319 | BIO_ADDR *res = BIO_ADDR_new(); |
320 | int ret = -1; | |
0f113f3e | 321 | |
5bca70ca RL |
322 | if (res == NULL) { |
323 | BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE); | |
324 | return ret; | |
0f113f3e | 325 | } |
5bca70ca RL |
326 | |
327 | ret = BIO_accept_ex(sock, res, 0); | |
328 | ||
b13fdc48 | 329 | if (ret == (int)INVALID_SOCKET) { |
5bca70ca RL |
330 | if (BIO_sock_should_retry(ret)) { |
331 | ret = -2; | |
332 | goto end; | |
333 | } | |
0f113f3e MC |
334 | SYSerr(SYS_F_ACCEPT, get_last_socket_error()); |
335 | BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR); | |
336 | goto end; | |
337 | } | |
338 | ||
5bca70ca RL |
339 | if (ip_port != NULL) { |
340 | char *host = BIO_ADDR_hostname_string(res, 1); | |
341 | char *port = BIO_ADDR_service_string(res, 1); | |
342 | *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2); | |
343 | strcpy(*ip_port, host); | |
344 | strcat(*ip_port, ":"); | |
345 | strcat(*ip_port, port); | |
346 | OPENSSL_free(host); | |
347 | OPENSSL_free(port); | |
0f113f3e | 348 | } |
5bca70ca | 349 | |
0f113f3e | 350 | end: |
5bca70ca RL |
351 | BIO_ADDR_free(res); |
352 | return ret; | |
0f113f3e | 353 | } |
d33b215b | 354 | # endif |
d02b48c6 | 355 | |
6b691a5c | 356 | int BIO_set_tcp_ndelay(int s, int on) |
0f113f3e MC |
357 | { |
358 | int ret = 0; | |
359 | # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP)) | |
360 | int opt; | |
d02b48c6 | 361 | |
0f113f3e MC |
362 | # ifdef SOL_TCP |
363 | opt = SOL_TCP; | |
364 | # else | |
365 | # ifdef IPPROTO_TCP | |
366 | opt = IPPROTO_TCP; | |
367 | # endif | |
368 | # endif | |
dfeab068 | 369 | |
0f113f3e MC |
370 | ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on)); |
371 | # endif | |
372 | return (ret == 0); | |
373 | } | |
374 | ||
375 | int BIO_socket_nbio(int s, int mode) | |
376 | { | |
377 | int ret = -1; | |
378 | int l; | |
379 | ||
380 | l = mode; | |
381 | # ifdef FIONBIO | |
382 | ret = BIO_socket_ioctl(s, FIONBIO, &l); | |
383 | # endif | |
384 | return (ret == 0); | |
385 | } | |
d33b215b RL |
386 | |
387 | int BIO_sock_info(int sock, | |
388 | enum BIO_sock_info_type type, union BIO_sock_info_u *info) | |
389 | { | |
390 | switch (type) { | |
391 | case BIO_SOCK_INFO_ADDRESS: | |
392 | { | |
393 | socklen_t addr_len; | |
394 | int ret = 0; | |
395 | addr_len = sizeof(*info->addr); | |
396 | ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr), | |
397 | &addr_len); | |
398 | if (ret == -1) { | |
399 | SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error()); | |
400 | BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR); | |
401 | return 0; | |
402 | } | |
403 | if (addr_len > sizeof(*info->addr)) { | |
404 | BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS); | |
405 | return 0; | |
406 | } | |
407 | } | |
408 | break; | |
409 | default: | |
410 | BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE); | |
411 | return 0; | |
412 | } | |
413 | return 1; | |
414 | } | |
415 | ||
4a1fbd13 | 416 | #endif |