| 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 |
{ |
| 54 |
BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret)); |
| 55 |
|
| 56 |
if (ret == NULL) |
| 57 |
return NULL; |
| 58 |
|
| 59 |
ret->sa.sa_family = AF_UNSPEC; |
| 60 |
return ret; |
| 61 |
} |
| 62 |
|
| 63 |
void BIO_ADDR_free(BIO_ADDR *ap) |
| 64 |
{ |
| 65 |
OPENSSL_free(ap); |
| 66 |
} |
| 67 |
|
| 68 |
int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src) |
| 69 |
{ |
| 70 |
if (dst == NULL || src == NULL) |
| 71 |
return 0; |
| 72 |
|
| 73 |
if (src->sa.sa_family == AF_UNSPEC) { |
| 74 |
BIO_ADDR_clear(dst); |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
return BIO_ADDR_make(dst, &src->sa); |
| 79 |
} |
| 80 |
|
| 81 |
BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap) |
| 82 |
{ |
| 83 |
BIO_ADDR *ret = NULL; |
| 84 |
|
| 85 |
if (ap != NULL) { |
| 86 |
ret = BIO_ADDR_new(); |
| 87 |
if (ret != NULL && !BIO_ADDR_copy(ret, ap)) { |
| 88 |
BIO_ADDR_free(ret); |
| 89 |
ret = NULL; |
| 90 |
} |
| 91 |
} |
| 92 |
return ret; |
| 93 |
} |
| 94 |
|
| 95 |
void BIO_ADDR_clear(BIO_ADDR *ap) |
| 96 |
{ |
| 97 |
memset(ap, 0, sizeof(*ap)); |
| 98 |
ap->sa.sa_family = AF_UNSPEC; |
| 99 |
} |
| 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 |
{ |
| 107 |
memset(ap, 0, sizeof(BIO_ADDR)); |
| 108 |
if (sa->sa_family == AF_INET) { |
| 109 |
memcpy(&(ap->s_in), sa, sizeof(struct sockaddr_in)); |
| 110 |
return 1; |
| 111 |
} |
| 112 |
#if OPENSSL_USE_IPV6 |
| 113 |
if (sa->sa_family == AF_INET6) { |
| 114 |
memcpy(&(ap->s_in6), sa, sizeof(struct sockaddr_in6)); |
| 115 |
return 1; |
| 116 |
} |
| 117 |
#endif |
| 118 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 119 |
if (sa->sa_family == AF_UNIX) { |
| 120 |
memcpy(&(ap->s_un), sa, sizeof(struct sockaddr_un)); |
| 121 |
return 1; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, |
| 129 |
const void *where, size_t wherelen, |
| 130 |
unsigned short port) |
| 131 |
{ |
| 132 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 133 |
if (family == AF_UNIX) { |
| 134 |
if (wherelen + 1 > sizeof(ap->s_un.sun_path)) |
| 135 |
return 0; |
| 136 |
memset(&ap->s_un, 0, sizeof(ap->s_un)); |
| 137 |
ap->s_un.sun_family = family; |
| 138 |
strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1); |
| 139 |
return 1; |
| 140 |
} |
| 141 |
#endif |
| 142 |
if (family == AF_INET) { |
| 143 |
if (wherelen != sizeof(struct in_addr)) |
| 144 |
return 0; |
| 145 |
memset(&ap->s_in, 0, sizeof(ap->s_in)); |
| 146 |
ap->s_in.sin_family = family; |
| 147 |
ap->s_in.sin_port = port; |
| 148 |
ap->s_in.sin_addr = *(struct in_addr *)where; |
| 149 |
return 1; |
| 150 |
} |
| 151 |
#if OPENSSL_USE_IPV6 |
| 152 |
if (family == AF_INET6) { |
| 153 |
if (wherelen != sizeof(struct in6_addr)) |
| 154 |
return 0; |
| 155 |
memset(&ap->s_in6, 0, sizeof(ap->s_in6)); |
| 156 |
ap->s_in6.sin6_family = family; |
| 157 |
ap->s_in6.sin6_port = port; |
| 158 |
ap->s_in6.sin6_addr = *(struct in6_addr *)where; |
| 159 |
return 1; |
| 160 |
} |
| 161 |
#endif |
| 162 |
|
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
int BIO_ADDR_family(const BIO_ADDR *ap) |
| 167 |
{ |
| 168 |
return ap->sa.sa_family; |
| 169 |
} |
| 170 |
|
| 171 |
int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l) |
| 172 |
{ |
| 173 |
size_t len = 0; |
| 174 |
const void *addrptr = NULL; |
| 175 |
|
| 176 |
if (ap->sa.sa_family == AF_INET) { |
| 177 |
len = sizeof(ap->s_in.sin_addr); |
| 178 |
addrptr = &ap->s_in.sin_addr; |
| 179 |
} |
| 180 |
#if OPENSSL_USE_IPV6 |
| 181 |
else if (ap->sa.sa_family == AF_INET6) { |
| 182 |
len = sizeof(ap->s_in6.sin6_addr); |
| 183 |
addrptr = &ap->s_in6.sin6_addr; |
| 184 |
} |
| 185 |
#endif |
| 186 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 187 |
else if (ap->sa.sa_family == AF_UNIX) { |
| 188 |
len = strlen(ap->s_un.sun_path); |
| 189 |
addrptr = &ap->s_un.sun_path; |
| 190 |
} |
| 191 |
#endif |
| 192 |
|
| 193 |
if (addrptr == NULL) |
| 194 |
return 0; |
| 195 |
|
| 196 |
if (p != NULL) { |
| 197 |
memcpy(p, addrptr, len); |
| 198 |
} |
| 199 |
if (l != NULL) |
| 200 |
*l = len; |
| 201 |
|
| 202 |
return 1; |
| 203 |
} |
| 204 |
|
| 205 |
unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap) |
| 206 |
{ |
| 207 |
if (ap->sa.sa_family == AF_INET) |
| 208 |
return ap->s_in.sin_port; |
| 209 |
#if OPENSSL_USE_IPV6 |
| 210 |
if (ap->sa.sa_family == AF_INET6) |
| 211 |
return ap->s_in6.sin6_port; |
| 212 |
#endif |
| 213 |
return 0; |
| 214 |
} |
| 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 |
{ |
| 232 |
if (BIO_sock_init() != 1) |
| 233 |
return 0; |
| 234 |
|
| 235 |
if (1) { |
| 236 |
#ifdef AI_PASSIVE |
| 237 |
int ret = 0; |
| 238 |
char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = ""; |
| 239 |
int flags = 0; |
| 240 |
|
| 241 |
if (numeric) |
| 242 |
flags |= NI_NUMERICHOST | NI_NUMERICSERV; |
| 243 |
|
| 244 |
if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap), |
| 245 |
BIO_ADDR_sockaddr_size(ap), |
| 246 |
host, sizeof(host), serv, sizeof(serv), |
| 247 |
flags)) |
| 248 |
!= 0) { |
| 249 |
#ifdef EAI_SYSTEM |
| 250 |
if (ret == EAI_SYSTEM) { |
| 251 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 252 |
"calling getnameinfo()"); |
| 253 |
} else |
| 254 |
#endif |
| 255 |
{ |
| 256 |
ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, gai_strerror(ret)); |
| 257 |
} |
| 258 |
return 0; |
| 259 |
} |
| 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 |
if (serv[0] == '\0') { |
| 268 |
BIO_snprintf(serv, sizeof(serv), "%d", |
| 269 |
ntohs(BIO_ADDR_rawport(ap))); |
| 270 |
} |
| 271 |
|
| 272 |
if (hostname != NULL) |
| 273 |
*hostname = OPENSSL_strdup(host); |
| 274 |
if (service != NULL) |
| 275 |
*service = OPENSSL_strdup(serv); |
| 276 |
} else { |
| 277 |
#endif |
| 278 |
if (hostname != NULL) |
| 279 |
*hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr)); |
| 280 |
if (service != NULL) { |
| 281 |
char serv[6]; /* port is 16 bits => max 5 decimal digits */ |
| 282 |
BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port)); |
| 283 |
*service = OPENSSL_strdup(serv); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if ((hostname != NULL && *hostname == NULL) |
| 288 |
|| (service != NULL && *service == NULL)) { |
| 289 |
if (hostname != NULL) { |
| 290 |
OPENSSL_free(*hostname); |
| 291 |
*hostname = NULL; |
| 292 |
} |
| 293 |
if (service != NULL) { |
| 294 |
OPENSSL_free(*service); |
| 295 |
*service = NULL; |
| 296 |
} |
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
return 1; |
| 301 |
} |
| 302 |
|
| 303 |
char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric) |
| 304 |
{ |
| 305 |
char *hostname = NULL; |
| 306 |
|
| 307 |
if (addr_strings(ap, numeric, &hostname, NULL)) |
| 308 |
return hostname; |
| 309 |
|
| 310 |
return NULL; |
| 311 |
} |
| 312 |
|
| 313 |
char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric) |
| 314 |
{ |
| 315 |
char *service = NULL; |
| 316 |
|
| 317 |
if (addr_strings(ap, numeric, NULL, &service)) |
| 318 |
return service; |
| 319 |
|
| 320 |
return NULL; |
| 321 |
} |
| 322 |
|
| 323 |
char *BIO_ADDR_path_string(const BIO_ADDR *ap) |
| 324 |
{ |
| 325 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 326 |
if (ap->sa.sa_family == AF_UNIX) |
| 327 |
return OPENSSL_strdup(ap->s_un.sun_path); |
| 328 |
#endif |
| 329 |
return NULL; |
| 330 |
} |
| 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 |
{ |
| 339 |
return &(ap->sa); |
| 340 |
} |
| 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 |
{ |
| 350 |
return &(ap->sa); |
| 351 |
} |
| 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 |
{ |
| 361 |
if (ap->sa.sa_family == AF_INET) |
| 362 |
return sizeof(ap->s_in); |
| 363 |
#if OPENSSL_USE_IPV6 |
| 364 |
if (ap->sa.sa_family == AF_INET6) |
| 365 |
return sizeof(ap->s_in6); |
| 366 |
#endif |
| 367 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 368 |
if (ap->sa.sa_family == AF_UNIX) |
| 369 |
return sizeof(ap->s_un); |
| 370 |
#endif |
| 371 |
return sizeof(*ap); |
| 372 |
} |
| 373 |
|
| 374 |
/********************************************************************** |
| 375 |
* |
| 376 |
* Address info database |
| 377 |
* |
| 378 |
*/ |
| 379 |
|
| 380 |
const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai) |
| 381 |
{ |
| 382 |
if (bai != NULL) |
| 383 |
return bai->bai_next; |
| 384 |
return NULL; |
| 385 |
} |
| 386 |
|
| 387 |
int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai) |
| 388 |
{ |
| 389 |
if (bai != NULL) |
| 390 |
return bai->bai_family; |
| 391 |
return 0; |
| 392 |
} |
| 393 |
|
| 394 |
int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai) |
| 395 |
{ |
| 396 |
if (bai != NULL) |
| 397 |
return bai->bai_socktype; |
| 398 |
return 0; |
| 399 |
} |
| 400 |
|
| 401 |
int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai) |
| 402 |
{ |
| 403 |
if (bai != NULL) { |
| 404 |
if (bai->bai_protocol != 0) |
| 405 |
return bai->bai_protocol; |
| 406 |
|
| 407 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 408 |
if (bai->bai_family == AF_UNIX) |
| 409 |
return 0; |
| 410 |
#endif |
| 411 |
|
| 412 |
switch (bai->bai_socktype) { |
| 413 |
case SOCK_STREAM: |
| 414 |
return IPPROTO_TCP; |
| 415 |
case SOCK_DGRAM: |
| 416 |
return IPPROTO_UDP; |
| 417 |
default: |
| 418 |
break; |
| 419 |
} |
| 420 |
} |
| 421 |
return 0; |
| 422 |
} |
| 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 |
{ |
| 430 |
if (bai != NULL) |
| 431 |
return (socklen_t)bai->bai_addrlen; |
| 432 |
return 0; |
| 433 |
} |
| 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 |
{ |
| 441 |
if (bai != NULL) |
| 442 |
return bai->bai_addr; |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
|
| 446 |
const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai) |
| 447 |
{ |
| 448 |
if (bai != NULL) |
| 449 |
return (BIO_ADDR *)bai->bai_addr; |
| 450 |
return NULL; |
| 451 |
} |
| 452 |
|
| 453 |
void BIO_ADDRINFO_free(BIO_ADDRINFO *bai) |
| 454 |
{ |
| 455 |
if (bai == NULL) |
| 456 |
return; |
| 457 |
|
| 458 |
#ifdef AI_PASSIVE |
| 459 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 460 |
#define _cond bai->bai_family != AF_UNIX |
| 461 |
#else |
| 462 |
#define _cond 1 |
| 463 |
#endif |
| 464 |
if (_cond) { |
| 465 |
freeaddrinfo(bai); |
| 466 |
return; |
| 467 |
} |
| 468 |
#endif |
| 469 |
|
| 470 |
/* Free manually when we know that addrinfo_wrap() was used. |
| 471 |
* See further comment above addrinfo_wrap() |
| 472 |
*/ |
| 473 |
while (bai != NULL) { |
| 474 |
BIO_ADDRINFO *next = bai->bai_next; |
| 475 |
OPENSSL_free(bai->bai_addr); |
| 476 |
OPENSSL_free(bai); |
| 477 |
bai = next; |
| 478 |
} |
| 479 |
} |
| 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 |
{ |
| 509 |
const char *h = NULL; |
| 510 |
size_t hl = 0; |
| 511 |
const char *p = NULL; |
| 512 |
size_t pl = 0; |
| 513 |
|
| 514 |
if (*hostserv == '[') { |
| 515 |
if ((p = strchr(hostserv, ']')) == NULL) |
| 516 |
goto spec_err; |
| 517 |
h = hostserv + 1; |
| 518 |
hl = p - h; |
| 519 |
p++; |
| 520 |
if (*p == '\0') |
| 521 |
p = NULL; |
| 522 |
else if (*p != ':') |
| 523 |
goto spec_err; |
| 524 |
else { |
| 525 |
p++; |
| 526 |
pl = strlen(p); |
| 527 |
} |
| 528 |
} else { |
| 529 |
const char *p2 = strrchr(hostserv, ':'); |
| 530 |
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 |
if (p != p2) |
| 543 |
goto amb_err; |
| 544 |
|
| 545 |
if (p != NULL) { |
| 546 |
h = hostserv; |
| 547 |
hl = p - h; |
| 548 |
p++; |
| 549 |
pl = strlen(p); |
| 550 |
} else if (hostserv_prio == BIO_PARSE_PRIO_HOST) { |
| 551 |
h = hostserv; |
| 552 |
hl = strlen(h); |
| 553 |
} else { |
| 554 |
p = hostserv; |
| 555 |
pl = strlen(p); |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
if (p != NULL && strchr(p, ':')) |
| 560 |
goto spec_err; |
| 561 |
|
| 562 |
if (h != NULL && host != NULL) { |
| 563 |
if (hl == 0 |
| 564 |
|| (hl == 1 && h[0] == '*')) { |
| 565 |
*host = NULL; |
| 566 |
} else { |
| 567 |
*host = OPENSSL_strndup(h, hl); |
| 568 |
if (*host == NULL) |
| 569 |
return 0; |
| 570 |
} |
| 571 |
} |
| 572 |
if (p != NULL && service != NULL) { |
| 573 |
if (pl == 0 |
| 574 |
|| (pl == 1 && p[0] == '*')) { |
| 575 |
*service = NULL; |
| 576 |
} else { |
| 577 |
*service = OPENSSL_strndup(p, pl); |
| 578 |
if (*service == NULL) { |
| 579 |
if (h != NULL && host != NULL) { |
| 580 |
OPENSSL_free(*host); |
| 581 |
*host = NULL; |
| 582 |
} |
| 583 |
return 0; |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
return 1; |
| 589 |
amb_err: |
| 590 |
ERR_raise(ERR_LIB_BIO, BIO_R_AMBIGUOUS_HOST_OR_SERVICE); |
| 591 |
return 0; |
| 592 |
spec_err: |
| 593 |
ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); |
| 594 |
return 0; |
| 595 |
} |
| 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 |
{ |
| 611 |
if ((*bai = OPENSSL_zalloc(sizeof(**bai))) == NULL) |
| 612 |
return 0; |
| 613 |
|
| 614 |
(*bai)->bai_family = family; |
| 615 |
(*bai)->bai_socktype = socktype; |
| 616 |
if (socktype == SOCK_STREAM) |
| 617 |
(*bai)->bai_protocol = IPPROTO_TCP; |
| 618 |
if (socktype == SOCK_DGRAM) |
| 619 |
(*bai)->bai_protocol = IPPROTO_UDP; |
| 620 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 621 |
if (family == AF_UNIX) |
| 622 |
(*bai)->bai_protocol = 0; |
| 623 |
#endif |
| 624 |
{ |
| 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 |
BIO_ADDR *addr = BIO_ADDR_new(); |
| 631 |
if (addr != NULL) { |
| 632 |
BIO_ADDR_rawmake(addr, family, where, wherelen, port); |
| 633 |
(*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr); |
| 634 |
} |
| 635 |
} |
| 636 |
(*bai)->bai_next = NULL; |
| 637 |
if ((*bai)->bai_addr == NULL) { |
| 638 |
BIO_ADDRINFO_free(*bai); |
| 639 |
*bai = NULL; |
| 640 |
return 0; |
| 641 |
} |
| 642 |
return 1; |
| 643 |
} |
| 644 |
|
| 645 |
DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init) |
| 646 |
{ |
| 647 |
bio_lookup_lock = CRYPTO_THREAD_lock_new(); |
| 648 |
return bio_lookup_lock != NULL; |
| 649 |
} |
| 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 |
{ |
| 655 |
return BIO_lookup_ex(host, service, lookup_type, family, socktype, 0, res); |
| 656 |
} |
| 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 |
{ |
| 683 |
int ret = 0; /* Assume failure */ |
| 684 |
|
| 685 |
switch (family) { |
| 686 |
case AF_INET: |
| 687 |
#if OPENSSL_USE_IPV6 |
| 688 |
case AF_INET6: |
| 689 |
#endif |
| 690 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 691 |
case AF_UNIX: |
| 692 |
#endif |
| 693 |
#ifdef AF_UNSPEC |
| 694 |
case AF_UNSPEC: |
| 695 |
#endif |
| 696 |
break; |
| 697 |
default: |
| 698 |
ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY); |
| 699 |
return 0; |
| 700 |
} |
| 701 |
|
| 702 |
#ifndef OPENSSL_NO_UNIX_SOCK |
| 703 |
if (family == AF_UNIX) { |
| 704 |
if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res)) |
| 705 |
return 1; |
| 706 |
else |
| 707 |
ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB); |
| 708 |
return 0; |
| 709 |
} |
| 710 |
#endif |
| 711 |
|
| 712 |
if (BIO_sock_init() != 1) |
| 713 |
return 0; |
| 714 |
|
| 715 |
if (1) { |
| 716 |
#ifdef AI_PASSIVE |
| 717 |
int gai_ret = 0, old_ret = 0; |
| 718 |
struct addrinfo hints; |
| 719 |
|
| 720 |
memset(&hints, 0, sizeof(hints)); |
| 721 |
|
| 722 |
hints.ai_family = family; |
| 723 |
hints.ai_socktype = socktype; |
| 724 |
hints.ai_protocol = protocol; |
| 725 |
#ifdef AI_ADDRCONFIG |
| 726 |
#ifdef AF_UNSPEC |
| 727 |
if (host != NULL && family == AF_UNSPEC) |
| 728 |
#endif |
| 729 |
hints.ai_flags |= AI_ADDRCONFIG; |
| 730 |
#endif |
| 731 |
|
| 732 |
if (lookup_type == BIO_LOOKUP_SERVER) |
| 733 |
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 |
#if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) |
| 739 |
retry: |
| 740 |
#endif |
| 741 |
switch ((gai_ret = getaddrinfo(host, service, &hints, res))) { |
| 742 |
#ifdef EAI_SYSTEM |
| 743 |
case EAI_SYSTEM: |
| 744 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 745 |
"calling getaddrinfo()"); |
| 746 |
ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
| 747 |
break; |
| 748 |
#endif |
| 749 |
#ifdef EAI_MEMORY |
| 750 |
case EAI_MEMORY: |
| 751 |
ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, |
| 752 |
gai_strerror(old_ret ? old_ret : gai_ret)); |
| 753 |
break; |
| 754 |
#endif |
| 755 |
case 0: |
| 756 |
ret = 1; /* Success */ |
| 757 |
break; |
| 758 |
default: |
| 759 |
#if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) |
| 760 |
if (hints.ai_flags & AI_ADDRCONFIG) { |
| 761 |
hints.ai_flags &= ~AI_ADDRCONFIG; |
| 762 |
hints.ai_flags |= AI_NUMERICHOST; |
| 763 |
old_ret = gai_ret; |
| 764 |
goto retry; |
| 765 |
} |
| 766 |
#endif |
| 767 |
ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, |
| 768 |
gai_strerror(old_ret ? old_ret : gai_ret)); |
| 769 |
break; |
| 770 |
} |
| 771 |
} else { |
| 772 |
#endif |
| 773 |
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 |
static in_addr_t he_fallback_address; |
| 791 |
static const char *he_fallback_addresses[] = { |
| 792 |
(char *)&he_fallback_address, NULL |
| 793 |
}; |
| 794 |
#endif |
| 795 |
static const struct hostent he_fallback = { |
| 796 |
NULL, NULL, AF_INET, sizeof(he_fallback_address), |
| 797 |
(char **)&he_fallback_addresses |
| 798 |
}; |
| 799 |
#if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
| 800 |
#pragma pointer_size restore |
| 801 |
#endif |
| 802 |
|
| 803 |
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 |
struct servent se_fallback = { NULL, NULL, 0, NULL }; |
| 809 |
#endif |
| 810 |
|
| 811 |
if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) { |
| 812 |
/* Should this be raised inside do_bio_lookup_init()? */ |
| 813 |
ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB); |
| 814 |
return 0; |
| 815 |
} |
| 816 |
|
| 817 |
if (!CRYPTO_THREAD_write_lock(bio_lookup_lock)) |
| 818 |
return 0; |
| 819 |
|
| 820 |
he_fallback_address = INADDR_ANY; |
| 821 |
if (host == NULL) { |
| 822 |
he = &he_fallback; |
| 823 |
switch (lookup_type) { |
| 824 |
case BIO_LOOKUP_CLIENT: |
| 825 |
he_fallback_address = INADDR_LOOPBACK; |
| 826 |
break; |
| 827 |
case BIO_LOOKUP_SERVER: |
| 828 |
he_fallback_address = INADDR_ANY; |
| 829 |
break; |
| 830 |
default: |
| 831 |
/* We forgot to handle a lookup type! */ |
| 832 |
assert("We forgot to handle a lookup type!" == NULL); |
| 833 |
ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
| 834 |
ret = 0; |
| 835 |
goto err; |
| 836 |
} |
| 837 |
} else { |
| 838 |
he = gethostbyname(host); |
| 839 |
|
| 840 |
if (he == NULL) { |
| 841 |
#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 |
ERR_raise_data(ERR_LIB_SYS, 1000 + h_errno, |
| 859 |
"calling gethostbyname()"); |
| 860 |
#endif |
| 861 |
#else |
| 862 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 863 |
"calling gethostbyname()"); |
| 864 |
#endif |
| 865 |
ret = 0; |
| 866 |
goto err; |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
if (service == NULL) { |
| 871 |
se_fallback.s_port = 0; |
| 872 |
se_fallback.s_proto = NULL; |
| 873 |
se = &se_fallback; |
| 874 |
} else { |
| 875 |
char *endp = NULL; |
| 876 |
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 |
char *proto = NULL; |
| 887 |
#if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
| 888 |
#pragma pointer_size restore |
| 889 |
#endif |
| 890 |
|
| 891 |
switch (socktype) { |
| 892 |
case SOCK_STREAM: |
| 893 |
proto = "tcp"; |
| 894 |
break; |
| 895 |
case SOCK_DGRAM: |
| 896 |
proto = "udp"; |
| 897 |
break; |
| 898 |
} |
| 899 |
|
| 900 |
if (endp != service && *endp == '\0' |
| 901 |
&& portnum > 0 && portnum < 65536) { |
| 902 |
se_fallback.s_port = htons((unsigned short)portnum); |
| 903 |
se_fallback.s_proto = proto; |
| 904 |
se = &se_fallback; |
| 905 |
} else if (endp == service) { |
| 906 |
se = getservbyname(service, proto); |
| 907 |
|
| 908 |
if (se == NULL) { |
| 909 |
ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
| 910 |
"calling getservbyname()"); |
| 911 |
goto err; |
| 912 |
} |
| 913 |
} else { |
| 914 |
ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); |
| 915 |
goto err; |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
*res = NULL; |
| 920 |
|
| 921 |
{ |
| 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 |
char **addrlistp; |
| 932 |
#if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
| 933 |
#pragma pointer_size restore |
| 934 |
#endif |
| 935 |
size_t addresses; |
| 936 |
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 |
for (addrlistp = he->h_addr_list; *addrlistp != NULL; |
| 941 |
addrlistp++) |
| 942 |
; |
| 943 |
|
| 944 |
for (addresses = addrlistp - he->h_addr_list; |
| 945 |
addrlistp--, addresses-- > 0;) { |
| 946 |
if (!addrinfo_wrap(he->h_addrtype, socktype, |
| 947 |
*addrlistp, he->h_length, |
| 948 |
se->s_port, &tmp_bai)) |
| 949 |
goto addrinfo_wrap_err; |
| 950 |
tmp_bai->bai_next = *res; |
| 951 |
*res = tmp_bai; |
| 952 |
continue; |
| 953 |
addrinfo_wrap_err: |
| 954 |
BIO_ADDRINFO_free(*res); |
| 955 |
*res = NULL; |
| 956 |
ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB); |
| 957 |
ret = 0; |
| 958 |
goto err; |
| 959 |
} |
| 960 |
|
| 961 |
ret = 1; |
| 962 |
} |
| 963 |
err: |
| 964 |
CRYPTO_THREAD_unlock(bio_lookup_lock); |
| 965 |
} |
| 966 |
|
| 967 |
return ret; |
| 968 |
} |
| 969 |
|
| 970 |
#endif /* OPENSSL_NO_SOCK */ |