2
0

bio_addr.c 27 KB

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