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