]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
eec0ad10 | 2 | * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved. |
28a0841b | 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 | |
28a0841b RL |
8 | */ |
9 | ||
99501e81 | 10 | #ifndef _GNU_SOURCE |
11 | # define _GNU_SOURCE | |
12 | #endif | |
13 | ||
256d41d4 RL |
14 | /* |
15 | * VC configurations may define UNICODE, to indicate to the C RTL that | |
16 | * WCHAR functions are preferred. | |
17 | * This affects functions like gai_strerror(), which is implemented as | |
18 | * an alias macro for gai_strerrorA() (which returns a const char *) or | |
19 | * gai_strerrorW() (which returns a const WCHAR *). This source file | |
20 | * assumes POSIX declarations, so prefer the non-UNICODE definitions. | |
21 | */ | |
22 | #undef UNICODE | |
23 | ||
86f31dd9 | 24 | #include <assert.h> |
28a0841b RL |
25 | #include <string.h> |
26 | ||
706457b7 | 27 | #include "bio_local.h" |
5c4328f0 | 28 | #include <openssl/crypto.h> |
28a0841b | 29 | |
1288f26f | 30 | #ifndef OPENSSL_NO_SOCK |
28a0841b RL |
31 | #include <openssl/err.h> |
32 | #include <openssl/buffer.h> | |
176db6dc | 33 | #include "internal/thread_once.h" |
28a0841b | 34 | |
ff234405 | 35 | CRYPTO_RWLOCK *bio_lookup_lock; |
f989cd8c AG |
36 | static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT; |
37 | ||
28a0841b | 38 | /* |
706457b7 | 39 | * Throughout this file and bio_local.h, the existence of the macro |
28a0841b RL |
40 | * AI_PASSIVE is used to detect the availability of struct addrinfo, |
41 | * getnameinfo() and getaddrinfo(). If that macro doesn't exist, | |
42 | * we use our own implementation instead, using gethostbyname, | |
43 | * getservbyname and a few other. | |
44 | */ | |
45 | ||
46 | /********************************************************************** | |
47 | * | |
48 | * Address structure | |
49 | * | |
50 | */ | |
51 | ||
52 | BIO_ADDR *BIO_ADDR_new(void) | |
53 | { | |
43ecb9c3 RS |
54 | BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret)); |
55 | ||
cb1d435c | 56 | if (ret == NULL) { |
9311d0c4 | 57 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
138388fe | 58 | return NULL; |
cb1d435c | 59 | } |
138388fe | 60 | |
7d1d48a2 | 61 | ret->sa.sa_family = AF_UNSPEC; |
28a0841b RL |
62 | return ret; |
63 | } | |
64 | ||
65 | void BIO_ADDR_free(BIO_ADDR *ap) | |
66 | { | |
67 | OPENSSL_free(ap); | |
68 | } | |
69 | ||
7d1d48a2 MC |
70 | void BIO_ADDR_clear(BIO_ADDR *ap) |
71 | { | |
72 | memset(ap, 0, sizeof(*ap)); | |
73 | ap->sa.sa_family = AF_UNSPEC; | |
74 | } | |
75 | ||
28a0841b RL |
76 | /* |
77 | * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents | |
78 | * of a struct sockaddr. | |
79 | */ | |
80 | int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa) | |
81 | { | |
82 | if (sa->sa_family == AF_INET) { | |
55bd917b | 83 | memcpy(&(ap->s_in), sa, sizeof(struct sockaddr_in)); |
28a0841b RL |
84 | return 1; |
85 | } | |
86 | #ifdef AF_INET6 | |
87 | if (sa->sa_family == AF_INET6) { | |
55bd917b | 88 | memcpy(&(ap->s_in6), sa, sizeof(struct sockaddr_in6)); |
28a0841b RL |
89 | return 1; |
90 | } | |
91 | #endif | |
92 | #ifdef AF_UNIX | |
17750375 | 93 | if (sa->sa_family == AF_UNIX) { |
55bd917b | 94 | memcpy(&(ap->s_un), sa, sizeof(struct sockaddr_un)); |
28a0841b RL |
95 | return 1; |
96 | } | |
97 | #endif | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, | |
103 | const void *where, size_t wherelen, | |
104 | unsigned short port) | |
105 | { | |
106 | #ifdef AF_UNIX | |
107 | if (family == AF_UNIX) { | |
29620124 | 108 | if (wherelen + 1 > sizeof(ap->s_un.sun_path)) |
28a0841b | 109 | return 0; |
29620124 RL |
110 | memset(&ap->s_un, 0, sizeof(ap->s_un)); |
111 | ap->s_un.sun_family = family; | |
112 | strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1); | |
28a0841b RL |
113 | return 1; |
114 | } | |
115 | #endif | |
116 | if (family == AF_INET) { | |
117 | if (wherelen != sizeof(struct in_addr)) | |
118 | return 0; | |
29620124 RL |
119 | memset(&ap->s_in, 0, sizeof(ap->s_in)); |
120 | ap->s_in.sin_family = family; | |
121 | ap->s_in.sin_port = port; | |
122 | ap->s_in.sin_addr = *(struct in_addr *)where; | |
28a0841b RL |
123 | return 1; |
124 | } | |
125 | #ifdef AF_INET6 | |
126 | if (family == AF_INET6) { | |
127 | if (wherelen != sizeof(struct in6_addr)) | |
128 | return 0; | |
29620124 RL |
129 | memset(&ap->s_in6, 0, sizeof(ap->s_in6)); |
130 | ap->s_in6.sin6_family = family; | |
131 | ap->s_in6.sin6_port = port; | |
132 | ap->s_in6.sin6_addr = *(struct in6_addr *)where; | |
28a0841b RL |
133 | return 1; |
134 | } | |
135 | #endif | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
140 | int BIO_ADDR_family(const BIO_ADDR *ap) | |
141 | { | |
142 | return ap->sa.sa_family; | |
143 | } | |
144 | ||
145 | int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l) | |
146 | { | |
147 | size_t len = 0; | |
148 | const void *addrptr = NULL; | |
149 | ||
150 | if (ap->sa.sa_family == AF_INET) { | |
29620124 RL |
151 | len = sizeof(ap->s_in.sin_addr); |
152 | addrptr = &ap->s_in.sin_addr; | |
28a0841b RL |
153 | } |
154 | #ifdef AF_INET6 | |
155 | else if (ap->sa.sa_family == AF_INET6) { | |
29620124 RL |
156 | len = sizeof(ap->s_in6.sin6_addr); |
157 | addrptr = &ap->s_in6.sin6_addr; | |
28a0841b RL |
158 | } |
159 | #endif | |
160 | #ifdef AF_UNIX | |
161 | else if (ap->sa.sa_family == AF_UNIX) { | |
29620124 RL |
162 | len = strlen(ap->s_un.sun_path); |
163 | addrptr = &ap->s_un.sun_path; | |
28a0841b RL |
164 | } |
165 | #endif | |
166 | ||
167 | if (addrptr == NULL) | |
168 | return 0; | |
169 | ||
170 | if (p != NULL) { | |
171 | memcpy(p, addrptr, len); | |
172 | } | |
173 | if (l != NULL) | |
174 | *l = len; | |
175 | ||
176 | return 1; | |
177 | } | |
178 | ||
179 | unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap) | |
180 | { | |
181 | if (ap->sa.sa_family == AF_INET) | |
29620124 | 182 | return ap->s_in.sin_port; |
28a0841b RL |
183 | #ifdef AF_INET6 |
184 | if (ap->sa.sa_family == AF_INET6) | |
29620124 | 185 | return ap->s_in6.sin6_port; |
28a0841b RL |
186 | #endif |
187 | return 0; | |
188 | } | |
189 | ||
190 | /*- | |
191 | * addr_strings - helper function to get host and service names | |
192 | * @ap: the BIO_ADDR that has the input info | |
193 | * @numeric: 0 if actual names should be returned, 1 if the numeric | |
194 | * representation should be returned. | |
195 | * @hostname: a pointer to a pointer to a memory area to store the | |
196 | * host name or numeric representation. Unused if NULL. | |
197 | * @service: a pointer to a pointer to a memory area to store the | |
198 | * service name or numeric representation. Unused if NULL. | |
199 | * | |
200 | * The return value is 0 on failure, with the error code in the error | |
201 | * stack, and 1 on success. | |
202 | */ | |
203 | static int addr_strings(const BIO_ADDR *ap, int numeric, | |
204 | char **hostname, char **service) | |
205 | { | |
ed03c461 RL |
206 | if (BIO_sock_init() != 1) |
207 | return 0; | |
208 | ||
28a0841b RL |
209 | if (1) { |
210 | #ifdef AI_PASSIVE | |
211 | int ret = 0; | |
6faffd0a | 212 | char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = ""; |
28a0841b RL |
213 | int flags = 0; |
214 | ||
215 | if (numeric) | |
216 | flags |= NI_NUMERICHOST | NI_NUMERICSERV; | |
217 | ||
218 | if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap), | |
219 | BIO_ADDR_sockaddr_size(ap), | |
220 | host, sizeof(host), serv, sizeof(serv), | |
221 | flags)) != 0) { | |
222 | # ifdef EAI_SYSTEM | |
223 | if (ret == EAI_SYSTEM) { | |
ff988500 RS |
224 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
225 | "calling getnameinfo()"); | |
28a0841b RL |
226 | } else |
227 | # endif | |
228 | { | |
a150f8e1 | 229 | ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, gai_strerror(ret)); |
28a0841b RL |
230 | } |
231 | return 0; | |
232 | } | |
d40cf9bc | 233 | |
6faffd0a RL |
234 | /* VMS getnameinfo() has a bug, it doesn't fill in serv, which |
235 | * leaves it with whatever garbage that happens to be there. | |
236 | * However, we initialise serv with the empty string (serv[0] | |
237 | * is therefore NUL), so it gets real easy to detect when things | |
238 | * didn't go the way one might expect. | |
d40cf9bc | 239 | */ |
6faffd0a | 240 | if (serv[0] == '\0') { |
d40cf9bc RL |
241 | BIO_snprintf(serv, sizeof(serv), "%d", |
242 | ntohs(BIO_ADDR_rawport(ap))); | |
243 | } | |
244 | ||
24854e01 | 245 | if (hostname != NULL) |
28a0841b | 246 | *hostname = OPENSSL_strdup(host); |
24854e01 | 247 | if (service != NULL) |
28a0841b RL |
248 | *service = OPENSSL_strdup(serv); |
249 | } else { | |
250 | #endif | |
24854e01 | 251 | if (hostname != NULL) |
29620124 | 252 | *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr)); |
24854e01 | 253 | if (service != NULL) { |
28a0841b | 254 | char serv[6]; /* port is 16 bits => max 5 decimal digits */ |
29620124 | 255 | BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port)); |
28a0841b RL |
256 | *service = OPENSSL_strdup(serv); |
257 | } | |
258 | } | |
259 | ||
24854e01 MC |
260 | if ((hostname != NULL && *hostname == NULL) |
261 | || (service != NULL && *service == NULL)) { | |
262 | if (hostname != NULL) { | |
263 | OPENSSL_free(*hostname); | |
264 | *hostname = NULL; | |
265 | } | |
266 | if (service != NULL) { | |
267 | OPENSSL_free(*service); | |
268 | *service = NULL; | |
269 | } | |
9311d0c4 | 270 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
24854e01 MC |
271 | return 0; |
272 | } | |
273 | ||
28a0841b RL |
274 | return 1; |
275 | } | |
276 | ||
277 | char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric) | |
278 | { | |
279 | char *hostname = NULL; | |
280 | ||
281 | if (addr_strings(ap, numeric, &hostname, NULL)) | |
282 | return hostname; | |
283 | ||
284 | return NULL; | |
285 | } | |
286 | ||
287 | char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric) | |
288 | { | |
289 | char *service = NULL; | |
290 | ||
291 | if (addr_strings(ap, numeric, NULL, &service)) | |
292 | return service; | |
293 | ||
294 | return NULL; | |
295 | } | |
296 | ||
297 | char *BIO_ADDR_path_string(const BIO_ADDR *ap) | |
298 | { | |
299 | #ifdef AF_UNIX | |
300 | if (ap->sa.sa_family == AF_UNIX) | |
29620124 | 301 | return OPENSSL_strdup(ap->s_un.sun_path); |
28a0841b RL |
302 | #endif |
303 | return NULL; | |
304 | } | |
305 | ||
306 | /* | |
307 | * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr | |
308 | * for a given BIO_ADDR. In reality, this is simply a type safe cast. | |
309 | * The returned struct sockaddr is const, so it can't be tampered with. | |
310 | */ | |
311 | const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap) | |
312 | { | |
313 | return &(ap->sa); | |
314 | } | |
315 | ||
316 | /* | |
317 | * BIO_ADDR_sockaddr_noconst - non-public function that does the same | |
318 | * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as | |
319 | * it allows you to tamper with the data (and thereby the contents | |
320 | * of the input BIO_ADDR). | |
321 | */ | |
322 | struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap) | |
323 | { | |
324 | return &(ap->sa); | |
325 | } | |
326 | ||
327 | /* | |
328 | * BIO_ADDR_sockaddr_size - non-public function that returns the size | |
329 | * of the struct sockaddr the BIO_ADDR is using. If the protocol family | |
330 | * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX, | |
331 | * the size of the BIO_ADDR type is returned. | |
332 | */ | |
333 | socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap) | |
334 | { | |
335 | if (ap->sa.sa_family == AF_INET) | |
29620124 | 336 | return sizeof(ap->s_in); |
28a0841b RL |
337 | #ifdef AF_INET6 |
338 | if (ap->sa.sa_family == AF_INET6) | |
29620124 | 339 | return sizeof(ap->s_in6); |
28a0841b RL |
340 | #endif |
341 | #ifdef AF_UNIX | |
342 | if (ap->sa.sa_family == AF_UNIX) | |
29620124 | 343 | return sizeof(ap->s_un); |
28a0841b RL |
344 | #endif |
345 | return sizeof(*ap); | |
346 | } | |
347 | ||
348 | /********************************************************************** | |
349 | * | |
e5a82bfd | 350 | * Address info database |
28a0841b RL |
351 | * |
352 | */ | |
353 | ||
354 | const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai) | |
355 | { | |
356 | if (bai != NULL) | |
357 | return bai->bai_next; | |
358 | return NULL; | |
359 | } | |
360 | ||
361 | int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai) | |
362 | { | |
363 | if (bai != NULL) | |
364 | return bai->bai_family; | |
365 | return 0; | |
366 | } | |
367 | ||
368 | int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai) | |
369 | { | |
370 | if (bai != NULL) | |
371 | return bai->bai_socktype; | |
372 | return 0; | |
373 | } | |
374 | ||
375 | int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai) | |
376 | { | |
c72fb77f RL |
377 | if (bai != NULL) { |
378 | if (bai->bai_protocol != 0) | |
379 | return bai->bai_protocol; | |
380 | ||
381 | #ifdef AF_UNIX | |
382 | if (bai->bai_family == AF_UNIX) | |
383 | return 0; | |
384 | #endif | |
385 | ||
386 | switch (bai->bai_socktype) { | |
387 | case SOCK_STREAM: | |
388 | return IPPROTO_TCP; | |
389 | case SOCK_DGRAM: | |
390 | return IPPROTO_UDP; | |
391 | default: | |
392 | break; | |
393 | } | |
394 | } | |
28a0841b RL |
395 | return 0; |
396 | } | |
397 | ||
398 | /* | |
399 | * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size | |
400 | * of the struct sockaddr inside the BIO_ADDRINFO. | |
401 | */ | |
402 | socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai) | |
403 | { | |
404 | if (bai != NULL) | |
405 | return bai->bai_addrlen; | |
406 | return 0; | |
407 | } | |
408 | ||
409 | /* | |
410 | * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr | |
411 | * as the struct sockaddr it is. | |
412 | */ | |
413 | const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai) | |
414 | { | |
415 | if (bai != NULL) | |
416 | return bai->bai_addr; | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai) | |
421 | { | |
422 | if (bai != NULL) | |
423 | return (BIO_ADDR *)bai->bai_addr; | |
424 | return NULL; | |
425 | } | |
426 | ||
427 | void BIO_ADDRINFO_free(BIO_ADDRINFO *bai) | |
428 | { | |
429 | if (bai == NULL) | |
430 | return; | |
431 | ||
432 | #ifdef AI_PASSIVE | |
433 | # ifdef AF_UNIX | |
434 | # define _cond bai->bai_family != AF_UNIX | |
435 | # else | |
436 | # define _cond 1 | |
437 | # endif | |
438 | if (_cond) { | |
439 | freeaddrinfo(bai); | |
440 | return; | |
441 | } | |
442 | #endif | |
443 | ||
444 | /* Free manually when we know that addrinfo_wrap() was used. | |
445 | * See further comment above addrinfo_wrap() | |
446 | */ | |
447 | while (bai != NULL) { | |
448 | BIO_ADDRINFO *next = bai->bai_next; | |
449 | OPENSSL_free(bai->bai_addr); | |
450 | OPENSSL_free(bai); | |
451 | bai = next; | |
452 | } | |
453 | } | |
454 | ||
455 | /********************************************************************** | |
456 | * | |
457 | * Service functions | |
458 | * | |
459 | */ | |
460 | ||
461 | /*- | |
462 | * The specs in hostserv can take these forms: | |
463 | * | |
464 | * host:service => *host = "host", *service = "service" | |
465 | * host:* => *host = "host", *service = NULL | |
466 | * host: => *host = "host", *service = NULL | |
467 | * :service => *host = NULL, *service = "service" | |
468 | * *:service => *host = NULL, *service = "service" | |
469 | * | |
470 | * in case no : is present in the string, the result depends on | |
471 | * hostserv_prio, as follows: | |
472 | * | |
473 | * when hostserv_prio == BIO_PARSE_PRIO_HOST | |
474 | * host => *host = "host", *service untouched | |
475 | * | |
476 | * when hostserv_prio == BIO_PARSE_PRIO_SERV | |
477 | * service => *host untouched, *service = "service" | |
478 | * | |
479 | */ | |
480 | int BIO_parse_hostserv(const char *hostserv, char **host, char **service, | |
481 | enum BIO_hostserv_priorities hostserv_prio) | |
482 | { | |
483 | const char *h = NULL; size_t hl = 0; | |
484 | const char *p = NULL; size_t pl = 0; | |
485 | ||
486 | if (*hostserv == '[') { | |
487 | if ((p = strchr(hostserv, ']')) == NULL) | |
488 | goto spec_err; | |
489 | h = hostserv + 1; | |
490 | hl = p - h; | |
491 | p++; | |
492 | if (*p == '\0') | |
493 | p = NULL; | |
494 | else if (*p != ':') | |
495 | goto spec_err; | |
496 | else { | |
497 | p++; | |
498 | pl = strlen(p); | |
499 | } | |
500 | } else { | |
501 | const char *p2 = strrchr(hostserv, ':'); | |
502 | p = strchr(hostserv, ':'); | |
503 | ||
504 | /*- | |
505 | * Check for more than one colon. There are three possible | |
506 | * interpretations: | |
507 | * 1. IPv6 address with port number, last colon being separator. | |
508 | * 2. IPv6 address only. | |
509 | * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST, | |
510 | * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV | |
511 | * Because of this ambiguity, we currently choose to make it an | |
512 | * error. | |
513 | */ | |
514 | if (p != p2) | |
515 | goto amb_err; | |
516 | ||
517 | if (p != NULL) { | |
518 | h = hostserv; | |
519 | hl = p - h; | |
520 | p++; | |
521 | pl = strlen(p); | |
522 | } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) { | |
523 | h = hostserv; | |
524 | hl = strlen(h); | |
525 | } else { | |
526 | p = hostserv; | |
527 | pl = strlen(p); | |
528 | } | |
529 | } | |
530 | ||
80926502 | 531 | if (p != NULL && strchr(p, ':')) |
28a0841b RL |
532 | goto spec_err; |
533 | ||
534 | if (h != NULL && host != NULL) { | |
535 | if (hl == 0 | |
536 | || (hl == 1 && h[0] == '*')) { | |
537 | *host = NULL; | |
538 | } else { | |
539 | *host = OPENSSL_strndup(h, hl); | |
540 | if (*host == NULL) | |
541 | goto memerr; | |
542 | } | |
543 | } | |
544 | if (p != NULL && service != NULL) { | |
545 | if (pl == 0 | |
546 | || (pl == 1 && p[0] == '*')) { | |
547 | *service = NULL; | |
548 | } else { | |
549 | *service = OPENSSL_strndup(p, pl); | |
550 | if (*service == NULL) | |
551 | goto memerr; | |
552 | } | |
553 | } | |
554 | ||
555 | return 1; | |
556 | amb_err: | |
9311d0c4 | 557 | ERR_raise(ERR_LIB_BIO, BIO_R_AMBIGUOUS_HOST_OR_SERVICE); |
28a0841b RL |
558 | return 0; |
559 | spec_err: | |
9311d0c4 | 560 | ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); |
28a0841b RL |
561 | return 0; |
562 | memerr: | |
9311d0c4 | 563 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
28a0841b RL |
564 | return 0; |
565 | } | |
566 | ||
567 | /* addrinfo_wrap is used to build our own addrinfo "chain". | |
568 | * (it has only one entry, so calling it a chain may be a stretch) | |
569 | * It should ONLY be called when getaddrinfo() and friends | |
570 | * aren't available, OR when dealing with a non IP protocol | |
571 | * family, such as AF_UNIX | |
572 | * | |
573 | * the return value is 1 on success, or 0 on failure, which | |
8483a003 | 574 | * only happens if a memory allocation error occurred. |
28a0841b RL |
575 | */ |
576 | static int addrinfo_wrap(int family, int socktype, | |
577 | const void *where, size_t wherelen, | |
578 | unsigned short port, | |
579 | BIO_ADDRINFO **bai) | |
580 | { | |
cdb10bae | 581 | if ((*bai = OPENSSL_zalloc(sizeof(**bai))) == NULL) { |
9311d0c4 | 582 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
28a0841b | 583 | return 0; |
cdb10bae | 584 | } |
43ecb9c3 | 585 | |
28a0841b RL |
586 | (*bai)->bai_family = family; |
587 | (*bai)->bai_socktype = socktype; | |
588 | if (socktype == SOCK_STREAM) | |
589 | (*bai)->bai_protocol = IPPROTO_TCP; | |
590 | if (socktype == SOCK_DGRAM) | |
591 | (*bai)->bai_protocol = IPPROTO_UDP; | |
592 | #ifdef AF_UNIX | |
593 | if (family == AF_UNIX) | |
594 | (*bai)->bai_protocol = 0; | |
595 | #endif | |
596 | { | |
597 | /* Magic: We know that BIO_ADDR_sockaddr_noconst is really | |
598 | just an advanced cast of BIO_ADDR* to struct sockaddr * | |
599 | by the power of union, so while it may seem that we're | |
600 | creating a memory leak here, we are not. It will be | |
601 | all right. */ | |
602 | BIO_ADDR *addr = BIO_ADDR_new(); | |
603 | if (addr != NULL) { | |
604 | BIO_ADDR_rawmake(addr, family, where, wherelen, port); | |
605 | (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr); | |
606 | } | |
607 | } | |
608 | (*bai)->bai_next = NULL; | |
609 | if ((*bai)->bai_addr == NULL) { | |
610 | BIO_ADDRINFO_free(*bai); | |
611 | *bai = NULL; | |
612 | return 0; | |
613 | } | |
614 | return 1; | |
615 | } | |
616 | ||
c2e4e5d2 | 617 | DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init) |
f989cd8c | 618 | { |
63ab5ea1 | 619 | bio_lookup_lock = CRYPTO_THREAD_lock_new(); |
5a7ad1f0 | 620 | return bio_lookup_lock != NULL; |
f989cd8c AG |
621 | } |
622 | ||
5114d822 MC |
623 | int BIO_lookup(const char *host, const char *service, |
624 | enum BIO_lookup_type lookup_type, | |
625 | int family, int socktype, BIO_ADDRINFO **res) | |
626 | { | |
627 | return BIO_lookup_ex(host, service, lookup_type, family, socktype, 0, res); | |
628 | } | |
629 | ||
28a0841b | 630 | /*- |
561f6f1e | 631 | * BIO_lookup_ex - look up the node and service you want to connect to. |
28a0841b RL |
632 | * @node: the node you want to connect to. |
633 | * @service: the service you want to connect to. | |
634 | * @lookup_type: declare intent with the result, client or server. | |
635 | * @family: the address family you want to use. Use AF_UNSPEC for any, or | |
636 | * AF_INET, AF_INET6 or AF_UNIX. | |
637 | * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM | |
638 | * or 0 for all. | |
5114d822 MC |
639 | * @protocol: The protocol to use, e.g. IPPROTO_TCP or IPPROTO_UDP or 0 for all. |
640 | * Note that some platforms may not return IPPROTO_SCTP without | |
641 | * explicitly requesting it (i.e. IPPROTO_SCTP may not be returned | |
642 | * with 0 for the protocol) | |
28a0841b RL |
643 | * @res: Storage place for the resulting list of returned addresses |
644 | * | |
645 | * This will do a lookup of the node and service that you want to connect to. | |
646 | * It returns a linked list of different addresses you can try to connect to. | |
647 | * | |
648 | * When no longer needed you should call BIO_ADDRINFO_free() to free the result. | |
649 | * | |
650 | * The return value is 1 on success or 0 in case of error. | |
651 | */ | |
e8291428 | 652 | int BIO_lookup_ex(const char *host, const char *service, int lookup_type, |
5114d822 | 653 | int family, int socktype, int protocol, BIO_ADDRINFO **res) |
28a0841b RL |
654 | { |
655 | int ret = 0; /* Assume failure */ | |
656 | ||
657 | switch(family) { | |
658 | case AF_INET: | |
659 | #ifdef AF_INET6 | |
660 | case AF_INET6: | |
661 | #endif | |
662 | #ifdef AF_UNIX | |
663 | case AF_UNIX: | |
664 | #endif | |
665 | #ifdef AF_UNSPEC | |
666 | case AF_UNSPEC: | |
667 | #endif | |
668 | break; | |
669 | default: | |
9311d0c4 | 670 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY); |
28a0841b RL |
671 | return 0; |
672 | } | |
673 | ||
674 | #ifdef AF_UNIX | |
675 | if (family == AF_UNIX) { | |
676 | if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res)) | |
677 | return 1; | |
678 | else | |
9311d0c4 | 679 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
28a0841b RL |
680 | return 0; |
681 | } | |
682 | #endif | |
683 | ||
ed03c461 RL |
684 | if (BIO_sock_init() != 1) |
685 | return 0; | |
686 | ||
28a0841b RL |
687 | if (1) { |
688 | #ifdef AI_PASSIVE | |
91cb81d4 | 689 | int gai_ret = 0, old_ret = 0; |
28a0841b | 690 | struct addrinfo hints; |
3e49ee23 | 691 | |
cbe29648 | 692 | memset(&hints, 0, sizeof(hints)); |
28a0841b | 693 | |
28a0841b RL |
694 | hints.ai_family = family; |
695 | hints.ai_socktype = socktype; | |
5114d822 | 696 | hints.ai_protocol = protocol; |
91cb81d4 TM |
697 | # ifdef AI_ADDRCONFIG |
698 | # ifdef AF_UNSPEC | |
b8472b4e | 699 | if (family == AF_UNSPEC) |
91cb81d4 | 700 | # endif |
b8472b4e | 701 | hints.ai_flags |= AI_ADDRCONFIG; |
91cb81d4 | 702 | # endif |
28a0841b RL |
703 | |
704 | if (lookup_type == BIO_LOOKUP_SERVER) | |
705 | hints.ai_flags |= AI_PASSIVE; | |
706 | ||
707 | /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to | |
706457b7 | 708 | * macro magic in bio_local.h |
28a0841b | 709 | */ |
be66a15c | 710 | # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) |
3f91ede9 | 711 | retry: |
be66a15c | 712 | # endif |
ed03c461 | 713 | switch ((gai_ret = getaddrinfo(host, service, &hints, res))) { |
28a0841b RL |
714 | # ifdef EAI_SYSTEM |
715 | case EAI_SYSTEM: | |
ff988500 RS |
716 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
717 | "calling getaddrinfo()"); | |
9311d0c4 | 718 | ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
28a0841b | 719 | break; |
91cb81d4 TM |
720 | # endif |
721 | # ifdef EAI_MEMORY | |
722 | case EAI_MEMORY: | |
9311d0c4 | 723 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
91cb81d4 | 724 | break; |
28a0841b RL |
725 | # endif |
726 | case 0: | |
727 | ret = 1; /* Success */ | |
728 | break; | |
7f616a00 TM |
729 | default: |
730 | # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) | |
3f91ede9 TM |
731 | if (hints.ai_flags & AI_ADDRCONFIG) { |
732 | hints.ai_flags &= ~AI_ADDRCONFIG; | |
7f616a00 | 733 | hints.ai_flags |= AI_NUMERICHOST; |
91cb81d4 | 734 | old_ret = gai_ret; |
3f91ede9 TM |
735 | goto retry; |
736 | } | |
3f91ede9 | 737 | # endif |
a150f8e1 RL |
738 | ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, |
739 | gai_strerror(old_ret ? old_ret : gai_ret)); | |
28a0841b RL |
740 | break; |
741 | } | |
742 | } else { | |
743 | #endif | |
37e3daf4 | 744 | const struct hostent *he; |
fcd9c8c0 RL |
745 | /* |
746 | * Because struct hostent is defined for 32-bit pointers only with | |
747 | * VMS C, we need to make sure that '&he_fallback_address' and | |
748 | * '&he_fallback_addresses' are 32-bit pointers | |
749 | */ | |
750 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) | |
751 | # pragma pointer_size save | |
752 | # pragma pointer_size 32 | |
753 | #endif | |
28a0841b RL |
754 | /* Windows doesn't seem to have in_addr_t */ |
755 | #ifdef OPENSSL_SYS_WINDOWS | |
37e3daf4 | 756 | static uint32_t he_fallback_address; |
7fb4b92c MC |
757 | static const char *he_fallback_addresses[] = |
758 | { (char *)&he_fallback_address, NULL }; | |
28a0841b | 759 | #else |
37e3daf4 | 760 | static in_addr_t he_fallback_address; |
7fb4b92c MC |
761 | static const char *he_fallback_addresses[] = |
762 | { (char *)&he_fallback_address, NULL }; | |
28a0841b | 763 | #endif |
37e3daf4 KR |
764 | static const struct hostent he_fallback = |
765 | { NULL, NULL, AF_INET, sizeof(he_fallback_address), | |
766 | (char **)&he_fallback_addresses }; | |
fcd9c8c0 RL |
767 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
768 | # pragma pointer_size restore | |
769 | #endif | |
770 | ||
28a0841b | 771 | struct servent *se; |
8483a003 | 772 | /* Apparently, on WIN64, s_proto and s_port have traded places... */ |
28a0841b RL |
773 | #ifdef _WIN64 |
774 | struct servent se_fallback = { NULL, NULL, NULL, 0 }; | |
775 | #else | |
776 | struct servent se_fallback = { NULL, NULL, 0, NULL }; | |
777 | #endif | |
28a0841b | 778 | |
c2e4e5d2 | 779 | if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) { |
9311d0c4 | 780 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
c2e4e5d2 RL |
781 | ret = 0; |
782 | goto err; | |
783 | } | |
f989cd8c AG |
784 | |
785 | CRYPTO_THREAD_write_lock(bio_lookup_lock); | |
37e3daf4 | 786 | he_fallback_address = INADDR_ANY; |
28a0841b RL |
787 | if (host == NULL) { |
788 | he = &he_fallback; | |
789 | switch(lookup_type) { | |
790 | case BIO_LOOKUP_CLIENT: | |
791 | he_fallback_address = INADDR_LOOPBACK; | |
792 | break; | |
793 | case BIO_LOOKUP_SERVER: | |
794 | he_fallback_address = INADDR_ANY; | |
795 | break; | |
796 | default: | |
86f31dd9 MC |
797 | /* We forgot to handle a lookup type! */ |
798 | assert("We forgot to handle a lookup type!" == NULL); | |
9311d0c4 | 799 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
86f31dd9 MC |
800 | ret = 0; |
801 | goto err; | |
28a0841b RL |
802 | } |
803 | } else { | |
804 | he = gethostbyname(host); | |
805 | ||
806 | if (he == NULL) { | |
c86d1f19 | 807 | #ifndef OPENSSL_SYS_WINDOWS |
3e49ee23 AP |
808 | /* |
809 | * This might be misleading, because h_errno is used as if | |
810 | * it was errno. To minimize mixup add 1000. Underlying | |
811 | * reason for this is that hstrerror is declared obsolete, | |
812 | * not to mention that a) h_errno is not always guaranteed | |
69687aa8 | 813 | * to be meaningless; b) hstrerror can reside in yet another |
3e49ee23 AP |
814 | * library, linking for sake of hstrerror is an overkill; |
815 | * c) this path is not executed on contemporary systems | |
816 | * anyway [above getaddrinfo/gai_strerror is]. We just let | |
817 | * system administrator figure this out... | |
818 | */ | |
5c8b7b4c KT |
819 | # if defined(OPENSSL_SYS_VXWORKS) |
820 | /* h_errno doesn't exist on VxWorks */ | |
ff988500 RS |
821 | ERR_raise_data(ERR_LIB_SYS, 1000, |
822 | "calling gethostbyname()"); | |
5c8b7b4c | 823 | # else |
ff988500 RS |
824 | ERR_raise_data(ERR_LIB_SYS, 1000 + h_errno, |
825 | "calling gethostbyname()"); | |
5c8b7b4c | 826 | # endif |
c86d1f19 | 827 | #else |
ff988500 RS |
828 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
829 | "calling gethostbyname()"); | |
c86d1f19 | 830 | #endif |
28a0841b RL |
831 | ret = 0; |
832 | goto err; | |
833 | } | |
834 | } | |
835 | ||
836 | if (service == NULL) { | |
837 | se_fallback.s_port = 0; | |
622c7e99 | 838 | se_fallback.s_proto = NULL; |
28a0841b RL |
839 | se = &se_fallback; |
840 | } else { | |
841 | char *endp = NULL; | |
842 | long portnum = strtol(service, &endp, 10); | |
fcd9c8c0 RL |
843 | |
844 | /* | |
845 | * Because struct servent is defined for 32-bit pointers only with | |
846 | * VMS C, we need to make sure that 'proto' is a 32-bit pointer. | |
847 | */ | |
848 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) | |
849 | # pragma pointer_size save | |
850 | # pragma pointer_size 32 | |
851 | #endif | |
622c7e99 | 852 | char *proto = NULL; |
fcd9c8c0 RL |
853 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
854 | # pragma pointer_size restore | |
855 | #endif | |
622c7e99 RL |
856 | |
857 | switch (socktype) { | |
858 | case SOCK_STREAM: | |
859 | proto = "tcp"; | |
860 | break; | |
861 | case SOCK_DGRAM: | |
862 | proto = "udp"; | |
863 | break; | |
864 | } | |
28a0841b RL |
865 | |
866 | if (endp != service && *endp == '\0' | |
867 | && portnum > 0 && portnum < 65536) { | |
3a63c0ed | 868 | se_fallback.s_port = htons((unsigned short)portnum); |
28a0841b RL |
869 | se_fallback.s_proto = proto; |
870 | se = &se_fallback; | |
871 | } else if (endp == service) { | |
28a0841b RL |
872 | se = getservbyname(service, proto); |
873 | ||
874 | if (se == NULL) { | |
ff988500 RS |
875 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
876 | "calling getservbyname()"); | |
28a0841b RL |
877 | goto err; |
878 | } | |
879 | } else { | |
9311d0c4 | 880 | ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); |
28a0841b RL |
881 | goto err; |
882 | } | |
883 | } | |
884 | ||
885 | *res = NULL; | |
886 | ||
887 | { | |
fcd9c8c0 RL |
888 | /* |
889 | * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C, | |
890 | * we must make sure our iterator designates the same element type, hence | |
891 | * the pointer size dance. | |
892 | */ | |
893 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) | |
894 | # pragma pointer_size save | |
895 | # pragma pointer_size 32 | |
896 | #endif | |
28a0841b | 897 | char **addrlistp; |
fcd9c8c0 RL |
898 | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
899 | # pragma pointer_size restore | |
900 | #endif | |
28a0841b RL |
901 | size_t addresses; |
902 | BIO_ADDRINFO *tmp_bai = NULL; | |
903 | ||
904 | /* The easiest way to create a linked list from an | |
905 | array is to start from the back */ | |
906 | for(addrlistp = he->h_addr_list; *addrlistp != NULL; | |
907 | addrlistp++) | |
908 | ; | |
909 | ||
910 | for(addresses = addrlistp - he->h_addr_list; | |
911 | addrlistp--, addresses-- > 0; ) { | |
912 | if (!addrinfo_wrap(he->h_addrtype, socktype, | |
913 | *addrlistp, he->h_length, | |
914 | se->s_port, &tmp_bai)) | |
915 | goto addrinfo_malloc_err; | |
916 | tmp_bai->bai_next = *res; | |
917 | *res = tmp_bai; | |
918 | continue; | |
919 | addrinfo_malloc_err: | |
920 | BIO_ADDRINFO_free(*res); | |
921 | *res = NULL; | |
9311d0c4 | 922 | ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE); |
28a0841b RL |
923 | ret = 0; |
924 | goto err; | |
925 | } | |
926 | ||
927 | ret = 1; | |
928 | } | |
929 | err: | |
f989cd8c | 930 | CRYPTO_THREAD_unlock(bio_lookup_lock); |
28a0841b RL |
931 | } |
932 | ||
933 | return ret; | |
934 | } | |
ff234405 | 935 | |
1288f26f | 936 | #endif /* OPENSSL_NO_SOCK */ |