bio_addr.c 27 KB

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