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