b_addr.c 26 KB

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