b_addr.c 26 KB

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