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