getaddrinfo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /*
  2. * Copyright 1996 by Craig Metz
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. * Portions from the GNU C library,
  5. * Copyright (C) 2003, 2006 Free Software Foundation, Inc.
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. /* $USAGI: getaddrinfo.c,v 1.16 2001/10/04 09:52:03 sekiya Exp $ */
  10. /* The Inner Net License, Version 2.00
  11. The author(s) grant permission for redistribution and use in source and
  12. binary forms, with or without modification, of the software and documentation
  13. provided that the following conditions are met:
  14. 0. If you receive a version of the software that is specifically labelled
  15. as not being for redistribution (check the version message and/or README),
  16. you are not permitted to redistribute that version of the software in any
  17. way or form.
  18. 1. All terms of the all other applicable copyrights and licenses must be
  19. followed.
  20. 2. Redistributions of source code must retain the authors' copyright
  21. notice(s), this list of conditions, and the following disclaimer.
  22. 3. Redistributions in binary form must reproduce the authors' copyright
  23. notice(s), this list of conditions, and the following disclaimer in the
  24. documentation and/or other materials provided with the distribution.
  25. 4. All advertising materials mentioning features or use of this software
  26. must display the following acknowledgement with the name(s) of the
  27. authors as specified in the copyright notice(s) substituted where
  28. indicated:
  29. This product includes software developed by <name(s)>, The Inner
  30. Net, and other contributors.
  31. 5. Neither the name(s) of the author(s) nor the names of its contributors
  32. may be used to endorse or promote products derived from this software
  33. without specific prior written permission.
  34. THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND ANY
  35. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  36. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  37. DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
  38. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  39. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  41. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  42. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  43. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. If these license terms cause you a real problem, contact the author. */
  45. #define __FORCE_GLIBC
  46. #include <features.h>
  47. #include <assert.h>
  48. #include <errno.h>
  49. #include <netdb.h>
  50. #include <resolv.h>
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <unistd.h>
  55. #include <arpa/inet.h>
  56. #include <sys/socket.h>
  57. #include <netinet/in.h>
  58. #include <sys/types.h>
  59. #include <sys/un.h>
  60. #include <sys/utsname.h>
  61. #include <net/if.h>
  62. #include <ifaddrs.h>
  63. /* Experimentally off - libc_hidden_proto(memcpy) */
  64. /* Experimentally off - libc_hidden_proto(memset) */
  65. /* libc_hidden_proto(strcmp) */
  66. /* libc_hidden_proto(stpcpy) */
  67. /* Experimentally off - libc_hidden_proto(strchr) */
  68. /* Experimentally off - libc_hidden_proto(strcpy) */
  69. /* Experimentally off - libc_hidden_proto(strlen) */
  70. /* libc_hidden_proto(socket) */
  71. /* libc_hidden_proto(close) */
  72. /* libc_hidden_proto(getservbyname_r) */
  73. /* libc_hidden_proto(gethostbyname2_r) */
  74. /* libc_hidden_proto(gethostbyaddr_r) */
  75. /* libc_hidden_proto(inet_pton) */
  76. /* libc_hidden_proto(inet_ntop) */
  77. /* libc_hidden_proto(strtoul) */
  78. /* libc_hidden_proto(if_nametoindex) */
  79. /* libc_hidden_proto(__h_errno_location) */
  80. /* libc_hidden_proto(uname) */
  81. #ifdef __UCLIBC_HAS_IPV6__
  82. /* libc_hidden_proto(in6addr_loopback) */
  83. #endif
  84. #define GAIH_OKIFUNSPEC 0x0100
  85. #define GAIH_EAI ~(GAIH_OKIFUNSPEC)
  86. #ifndef UNIX_PATH_MAX
  87. #define UNIX_PATH_MAX 108
  88. #endif
  89. /* Useful for having small structure members/global variables */
  90. typedef int8_t socktype_t;
  91. typedef int8_t family_t;
  92. typedef int8_t protocol_t;
  93. struct BUG_too_small {
  94. char BUG_socktype_t_too_small[(0
  95. | SOCK_STREAM
  96. | SOCK_DGRAM
  97. | SOCK_RAW
  98. ) <= 127 ? 1 : -1];
  99. char BUG_family_t_too_small[(0
  100. | AF_UNSPEC
  101. | AF_INET
  102. | AF_INET6
  103. ) <= 127 ? 1 : -1];
  104. char BUG_protocol_t_too_small[(0
  105. | IPPROTO_TCP
  106. | IPPROTO_UDP
  107. ) <= 127 ? 1 : -1];
  108. };
  109. struct gaih_service {
  110. const char *name;
  111. int num;
  112. };
  113. struct gaih_servtuple {
  114. struct gaih_servtuple *next;
  115. int socktype;
  116. int protocol;
  117. int port;
  118. };
  119. struct gaih_addrtuple {
  120. struct gaih_addrtuple *next;
  121. int family;
  122. char addr[16];
  123. uint32_t scopeid;
  124. };
  125. struct gaih_typeproto {
  126. socktype_t socktype;
  127. protocol_t protocol;
  128. int8_t protoflag;
  129. char name[4];
  130. };
  131. /* Values for `protoflag'. */
  132. #define GAI_PROTO_NOSERVICE 1
  133. #define GAI_PROTO_PROTOANY 2
  134. static const struct gaih_typeproto gaih_inet_typeproto[] = {
  135. { 0 , 0 , 0, "" },
  136. { SOCK_STREAM, IPPROTO_TCP, 0, "tcp" },
  137. { SOCK_DGRAM , IPPROTO_UDP, 0, "udp" },
  138. { SOCK_RAW , 0 , GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE, "raw" },
  139. { 0 , 0 , 0, "" },
  140. };
  141. struct gaih {
  142. int family;
  143. int (*gaih)(const char *name, const struct gaih_service *service,
  144. const struct addrinfo *req, struct addrinfo **pai);
  145. };
  146. #define SEEN_IPV4 1
  147. #define SEEN_IPV6 2
  148. static unsigned __check_pf(void)
  149. {
  150. unsigned seen = 0;
  151. #if defined __UCLIBC_SUPPORT_AI_ADDRCONFIG__
  152. struct ifaddrs *ifa;
  153. struct ifaddrs *runp;
  154. /* Get the interface list via getifaddrs. */
  155. if (getifaddrs(&ifa) != 0) {
  156. /* We cannot determine what interfaces are available.
  157. * Be optimistic. */
  158. #if defined __UCLIBC_HAS_IPV4__
  159. seen |= SEEN_IPV4;
  160. #endif
  161. #if defined __UCLIBC_HAS_IPV6__
  162. seen |= SEEN_IPV6;
  163. #endif
  164. return seen;
  165. }
  166. for (runp = ifa; runp != NULL; runp = runp->ifa_next) {
  167. if (runp->ifa_addr == NULL)
  168. continue;
  169. #if defined __UCLIBC_HAS_IPV4__
  170. if (runp->ifa_addr->sa_family == PF_INET)
  171. seen |= SEEN_IPV4;
  172. #endif
  173. #if defined __UCLIBC_HAS_IPV6__
  174. if (runp->ifa_addr->sa_family == PF_INET6)
  175. seen |= SEEN_IPV6;
  176. #endif
  177. }
  178. freeifaddrs(ifa);
  179. #else
  180. /* AI_ADDRCONFIG is disabled, assume both ipv4 and ipv6 available. */
  181. #if defined __UCLIBC_HAS_IPV4__
  182. seen |= SEEN_IPV4;
  183. #endif
  184. #if defined __UCLIBC_HAS_IPV6__
  185. seen |= SEEN_IPV6;
  186. #endif
  187. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  188. return seen;
  189. }
  190. static int addrconfig(sa_family_t af)
  191. {
  192. int s;
  193. int ret;
  194. int saved_errno = errno;
  195. unsigned seen;
  196. seen = __check_pf();
  197. #if defined __UCLIBC_HAS_IPV4__
  198. if (af == AF_INET)
  199. ret = seen & SEEN_IPV4;
  200. else
  201. #endif
  202. #if defined __UCLIBC_HAS_IPV6__
  203. if (af == AF_INET6)
  204. ret = seen & SEEN_IPV6;
  205. else
  206. #endif
  207. {
  208. s = socket(af, SOCK_DGRAM, 0);
  209. ret = 1; /* Assume PF_UNIX. */
  210. if (s < 0) {
  211. if (errno != EMFILE)
  212. ret = 0;
  213. } else
  214. close(s);
  215. }
  216. __set_errno(saved_errno);
  217. return ret;
  218. }
  219. #if 0
  220. /* Using Unix sockets this way is a security risk. */
  221. static int
  222. gaih_local(const char *name, const struct gaih_service *service,
  223. const struct addrinfo *req, struct addrinfo **pai)
  224. {
  225. struct utsname utsname;
  226. struct addrinfo *ai = *pai;
  227. if ((name != NULL) && (req->ai_flags & AI_NUMERICHOST))
  228. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  229. if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
  230. if (uname(&utsname) < 0)
  231. return -EAI_SYSTEM;
  232. if (name != NULL) {
  233. if (strcmp(name, "localhost") &&
  234. strcmp(name, "local") &&
  235. strcmp(name, "unix") &&
  236. strcmp(name, utsname.nodename))
  237. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  238. }
  239. if (req->ai_protocol || req->ai_socktype) {
  240. const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
  241. while (tp->name[0]
  242. && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
  243. || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  244. || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
  245. ) {
  246. ++tp;
  247. }
  248. if (! tp->name[0]) {
  249. if (req->ai_socktype)
  250. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  251. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  252. }
  253. }
  254. *pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
  255. + ((req->ai_flags & AI_CANONNAME)
  256. ? (strlen(utsname.nodename) + 1) : 0));
  257. if (ai == NULL)
  258. return -EAI_MEMORY;
  259. ai->ai_next = NULL;
  260. ai->ai_flags = req->ai_flags;
  261. ai->ai_family = AF_LOCAL;
  262. ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
  263. ai->ai_protocol = req->ai_protocol;
  264. ai->ai_addrlen = sizeof(struct sockaddr_un);
  265. ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
  266. #if SALEN
  267. ((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
  268. #endif /* SALEN */
  269. ((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
  270. memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
  271. if (service) {
  272. struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;
  273. if (strchr(service->name, '/') != NULL) {
  274. if (strlen(service->name) >= sizeof(sunp->sun_path))
  275. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  276. strcpy(sunp->sun_path, service->name);
  277. } else {
  278. if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(sunp->sun_path))
  279. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  280. stpcpy(stpcpy(sunp->sun_path, P_tmpdir "/"), service->name);
  281. }
  282. } else {
  283. /* This is a dangerous use of the interface since there is a time
  284. window between the test for the file and the actual creation
  285. (done by the caller) in which a file with the same name could
  286. be created. */
  287. char *buf = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
  288. if (__path_search(buf, L_tmpnam, NULL, NULL, 0) != 0
  289. || __gen_tempname(buf, __GT_NOCREATE) != 0
  290. ) {
  291. return -EAI_SYSTEM;
  292. }
  293. }
  294. ai->ai_canonname = NULL;
  295. if (req->ai_flags & AI_CANONNAME)
  296. ai->ai_canonname = strcpy((char *)(ai + 1) + sizeof(struct sockaddr_un),
  297. utsname.nodename);
  298. return 0;
  299. }
  300. #endif /* 0 */
  301. static int
  302. gaih_inet_serv(const char *servicename, const struct gaih_typeproto *tp,
  303. const struct addrinfo *req, struct gaih_servtuple *st)
  304. {
  305. struct servent *s;
  306. size_t tmpbuflen = 1024;
  307. struct servent ts;
  308. char *tmpbuf;
  309. int r;
  310. while (1) {
  311. tmpbuf = alloca(tmpbuflen);
  312. r = getservbyname_r(servicename, tp->name, &ts, tmpbuf, tmpbuflen, &s);
  313. if (r == 0 && s != NULL)
  314. break;
  315. if (r != ERANGE)
  316. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  317. tmpbuflen *= 2;
  318. }
  319. st->next = NULL;
  320. st->socktype = tp->socktype;
  321. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY) ? req->ai_protocol : tp->protocol);
  322. st->port = s->s_port;
  323. return 0;
  324. }
  325. /* NB: also uses h,pat,rc,no_data variables */
  326. #define gethosts(_family, _type) \
  327. { \
  328. int i, herrno; \
  329. size_t tmpbuflen; \
  330. struct hostent th; \
  331. char *tmpbuf; \
  332. \
  333. tmpbuflen = 512; \
  334. no_data = 0; \
  335. do { \
  336. tmpbuflen *= 2; \
  337. tmpbuf = alloca(tmpbuflen); \
  338. rc = gethostbyname2_r(name, _family, &th, tmpbuf, \
  339. tmpbuflen, &h, &herrno); \
  340. } while (rc == ERANGE && herrno == NETDB_INTERNAL); \
  341. if (rc != 0) { \
  342. if (herrno == NETDB_INTERNAL) { \
  343. __set_h_errno(herrno); \
  344. return -EAI_SYSTEM; \
  345. } \
  346. if (herrno == TRY_AGAIN) \
  347. no_data = EAI_AGAIN; \
  348. else \
  349. no_data = (herrno == NO_DATA); \
  350. } else if (h != NULL) { \
  351. for (i = 0; h->h_addr_list[i]; i++) { \
  352. if (*pat == NULL) { \
  353. *pat = alloca(sizeof(struct gaih_addrtuple)); \
  354. (*pat)->scopeid = 0; \
  355. } \
  356. (*pat)->next = NULL; \
  357. (*pat)->family = _family; \
  358. memcpy((*pat)->addr, h->h_addr_list[i], sizeof(_type)); \
  359. pat = &((*pat)->next); \
  360. } \
  361. } \
  362. }
  363. static int
  364. gaih_inet(const char *name, const struct gaih_service *service,
  365. const struct addrinfo *req, struct addrinfo **pai)
  366. {
  367. struct gaih_servtuple nullserv;
  368. const struct gaih_typeproto *tp = gaih_inet_typeproto;
  369. struct gaih_servtuple *st = &nullserv;
  370. struct gaih_addrtuple *at = NULL;
  371. int rc;
  372. int v4mapped = (req->ai_family == PF_UNSPEC || req->ai_family == PF_INET6)
  373. && (req->ai_flags & AI_V4MAPPED);
  374. unsigned seen = __check_pf();
  375. memset(&nullserv, 0, sizeof(nullserv));
  376. if (req->ai_protocol || req->ai_socktype) {
  377. ++tp;
  378. while (tp->name[0]
  379. && ((req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  380. || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol)
  381. )
  382. ) {
  383. ++tp;
  384. }
  385. if (! tp->name[0]) {
  386. if (req->ai_socktype)
  387. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  388. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  389. }
  390. }
  391. if (service != NULL) {
  392. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  393. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  394. if (service->num < 0) {
  395. if (tp->name[0]) {
  396. st = alloca(sizeof(struct gaih_servtuple));
  397. rc = gaih_inet_serv(service->name, tp, req, st);
  398. if (rc)
  399. return rc;
  400. } else {
  401. struct gaih_servtuple **pst = &st;
  402. for (tp++; tp->name[0]; tp++) {
  403. struct gaih_servtuple *newp;
  404. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  405. continue;
  406. if (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  407. continue;
  408. if (req->ai_protocol != 0
  409. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  410. && req->ai_protocol != tp->protocol)
  411. continue;
  412. newp = alloca(sizeof(struct gaih_servtuple));
  413. rc = gaih_inet_serv(service->name, tp, req, newp);
  414. if (rc) {
  415. if (rc & GAIH_OKIFUNSPEC)
  416. continue;
  417. return rc;
  418. }
  419. *pst = newp;
  420. pst = &(newp->next);
  421. }
  422. if (st == &nullserv)
  423. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  424. }
  425. } else {
  426. st = alloca(sizeof(struct gaih_servtuple));
  427. st->next = NULL;
  428. st->socktype = tp->socktype;
  429. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  430. ? req->ai_protocol : tp->protocol);
  431. st->port = htons(service->num);
  432. }
  433. } else if (req->ai_socktype || req->ai_protocol) {
  434. st = alloca(sizeof(struct gaih_servtuple));
  435. st->next = NULL;
  436. st->socktype = tp->socktype;
  437. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  438. ? req->ai_protocol : tp->protocol);
  439. st->port = 0;
  440. } else {
  441. /*
  442. * Neither socket type nor protocol is set. Return all socket types
  443. * we know about.
  444. */
  445. struct gaih_servtuple **lastp = &st;
  446. for (++tp; tp->name[0]; ++tp) {
  447. struct gaih_servtuple *newp;
  448. newp = alloca(sizeof(struct gaih_servtuple));
  449. newp->next = NULL;
  450. newp->socktype = tp->socktype;
  451. newp->protocol = tp->protocol;
  452. newp->port = 0;
  453. *lastp = newp;
  454. lastp = &newp->next;
  455. }
  456. }
  457. if (name != NULL) {
  458. at = alloca(sizeof(struct gaih_addrtuple));
  459. at->family = AF_UNSPEC;
  460. at->scopeid = 0;
  461. at->next = NULL;
  462. if (inet_pton(AF_INET, name, at->addr) > 0) {
  463. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET || v4mapped)
  464. at->family = AF_INET;
  465. else
  466. return -EAI_FAMILY;
  467. }
  468. #if defined __UCLIBC_HAS_IPV6__
  469. if (at->family == AF_UNSPEC) {
  470. char *namebuf = strdupa(name);
  471. char *scope_delim;
  472. scope_delim = strchr(namebuf, SCOPE_DELIMITER);
  473. if (scope_delim != NULL)
  474. *scope_delim = '\0';
  475. if (inet_pton(AF_INET6, namebuf, at->addr) > 0) {
  476. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  477. at->family = AF_INET6;
  478. else
  479. return -EAI_FAMILY;
  480. if (scope_delim != NULL) {
  481. int try_numericscope = 0;
  482. if (IN6_IS_ADDR_LINKLOCAL(at->addr) || IN6_IS_ADDR_MC_LINKLOCAL(at->addr)) {
  483. at->scopeid = if_nametoindex(scope_delim + 1);
  484. if (at->scopeid == 0)
  485. try_numericscope = 1;
  486. } else
  487. try_numericscope = 1;
  488. if (try_numericscope != 0) {
  489. char *end;
  490. assert(sizeof(uint32_t) <= sizeof(unsigned long));
  491. at->scopeid = (uint32_t)strtoul(scope_delim + 1, &end, 10);
  492. if (*end != '\0')
  493. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  494. }
  495. }
  496. }
  497. }
  498. #endif
  499. if (at->family == AF_UNSPEC && (req->ai_flags & AI_NUMERICHOST) == 0) {
  500. struct hostent *h;
  501. struct gaih_addrtuple **pat = &at;
  502. int no_data = 0;
  503. int no_inet6_data;
  504. /*
  505. * If we are looking for both IPv4 and IPv6 address we don't want
  506. * the lookup functions to automatically promote IPv4 addresses to
  507. * IPv6 addresses.
  508. */
  509. #if defined __UCLIBC_HAS_IPV6__
  510. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  511. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV6))
  512. gethosts(AF_INET6, struct in6_addr);
  513. #endif
  514. no_inet6_data = no_data;
  515. if (req->ai_family == AF_INET
  516. || (!v4mapped && req->ai_family == AF_UNSPEC)
  517. || (v4mapped && (no_inet6_data != 0 || (req->ai_flags & AI_ALL)))
  518. ) {
  519. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV4))
  520. gethosts(AF_INET, struct in_addr);
  521. }
  522. if (no_data != 0 && no_inet6_data != 0) {
  523. /* If both requests timed out report this. */
  524. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  525. return -EAI_AGAIN;
  526. /*
  527. * We made requests but they turned out no data.
  528. * The name is known, though.
  529. */
  530. return (GAIH_OKIFUNSPEC | -EAI_AGAIN);
  531. }
  532. }
  533. if (at->family == AF_UNSPEC)
  534. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  535. } else {
  536. struct gaih_addrtuple *atr;
  537. atr = at = alloca(sizeof(struct gaih_addrtuple));
  538. memset(at, '\0', sizeof(struct gaih_addrtuple));
  539. if (req->ai_family == 0) {
  540. at->next = alloca(sizeof(struct gaih_addrtuple));
  541. memset(at->next, '\0', sizeof(struct gaih_addrtuple));
  542. }
  543. #if defined __UCLIBC_HAS_IPV6__
  544. if (req->ai_family == 0 || req->ai_family == AF_INET6) {
  545. at->family = AF_INET6;
  546. if ((req->ai_flags & AI_PASSIVE) == 0)
  547. memcpy(at->addr, &in6addr_loopback, sizeof(struct in6_addr));
  548. atr = at->next;
  549. }
  550. #endif
  551. if (req->ai_family == 0 || req->ai_family == AF_INET) {
  552. atr->family = AF_INET;
  553. if ((req->ai_flags & AI_PASSIVE) == 0)
  554. *(uint32_t*)atr->addr = htonl(INADDR_LOOPBACK);
  555. }
  556. }
  557. if (pai == NULL)
  558. return 0;
  559. {
  560. const char *c = NULL;
  561. struct gaih_servtuple *st2;
  562. struct gaih_addrtuple *at2 = at;
  563. size_t socklen, namelen;
  564. sa_family_t family;
  565. /*
  566. * buffer is the size of an unformatted IPv6 address in
  567. * printable format.
  568. */
  569. char buffer[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  570. while (at2 != NULL) {
  571. if (req->ai_flags & AI_CANONNAME) {
  572. struct hostent *h = NULL;
  573. int herrno;
  574. struct hostent th;
  575. size_t tmpbuflen = 512;
  576. char *tmpbuf;
  577. do {
  578. tmpbuflen *= 2;
  579. tmpbuf = alloca(tmpbuflen);
  580. //if (tmpbuf == NULL)
  581. // return -EAI_MEMORY;
  582. rc = gethostbyaddr_r(at2->addr,
  583. ((at2->family == AF_INET6)
  584. ? sizeof(struct in6_addr)
  585. : sizeof(struct in_addr)),
  586. at2->family,
  587. &th, tmpbuf, tmpbuflen,
  588. &h, &herrno);
  589. } while (rc == errno && herrno == NETDB_INTERNAL);
  590. if (rc != 0 && herrno == NETDB_INTERNAL) {
  591. __set_h_errno(herrno);
  592. return -EAI_SYSTEM;
  593. }
  594. if (h == NULL)
  595. c = inet_ntop(at2->family, at2->addr, buffer, sizeof(buffer));
  596. else
  597. c = h->h_name;
  598. if (c == NULL)
  599. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  600. namelen = strlen(c) + 1;
  601. } else
  602. namelen = 0;
  603. #if defined __UCLIBC_HAS_IPV6__
  604. if (at2->family == AF_INET6 || v4mapped) {
  605. family = AF_INET6;
  606. socklen = sizeof(struct sockaddr_in6);
  607. }
  608. #endif
  609. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  610. else
  611. #endif
  612. #if defined __UCLIBC_HAS_IPV4__
  613. {
  614. family = AF_INET;
  615. socklen = sizeof(struct sockaddr_in);
  616. }
  617. #endif
  618. for (st2 = st; st2 != NULL; st2 = st2->next) {
  619. if (req->ai_flags & AI_ADDRCONFIG) {
  620. if (family == AF_INET && !(seen & SEEN_IPV4))
  621. break;
  622. #if defined __UCLIBC_HAS_IPV6__
  623. else if (family == AF_INET6 && !(seen & SEEN_IPV6))
  624. break;
  625. #endif
  626. }
  627. *pai = malloc(sizeof(struct addrinfo) + socklen + namelen);
  628. if (*pai == NULL)
  629. return -EAI_MEMORY;
  630. (*pai)->ai_flags = req->ai_flags;
  631. (*pai)->ai_family = family;
  632. (*pai)->ai_socktype = st2->socktype;
  633. (*pai)->ai_protocol = st2->protocol;
  634. (*pai)->ai_addrlen = socklen;
  635. (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
  636. #if SALEN
  637. (*pai)->ai_addr->sa_len = socklen;
  638. #endif /* SALEN */
  639. (*pai)->ai_addr->sa_family = family;
  640. #if defined __UCLIBC_HAS_IPV6__
  641. if (family == AF_INET6) {
  642. struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *) (*pai)->ai_addr;
  643. sin6p->sin6_flowinfo = 0;
  644. if (at2->family == AF_INET6) {
  645. memcpy(&sin6p->sin6_addr,
  646. at2->addr, sizeof(struct in6_addr));
  647. } else {
  648. sin6p->sin6_addr.s6_addr32[0] = 0;
  649. sin6p->sin6_addr.s6_addr32[1] = 0;
  650. sin6p->sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
  651. memcpy(&sin6p->sin6_addr.s6_addr32[3],
  652. at2->addr, sizeof(sin6p->sin6_addr.s6_addr32[3]));
  653. }
  654. sin6p->sin6_port = st2->port;
  655. sin6p->sin6_scope_id = at2->scopeid;
  656. }
  657. #endif
  658. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  659. else
  660. #endif
  661. #if defined __UCLIBC_HAS_IPV4__
  662. {
  663. struct sockaddr_in *sinp = (struct sockaddr_in *) (*pai)->ai_addr;
  664. memcpy(&sinp->sin_addr, at2->addr, sizeof(struct in_addr));
  665. sinp->sin_port = st2->port;
  666. memset(sinp->sin_zero, '\0', sizeof(sinp->sin_zero));
  667. }
  668. #endif
  669. if (c) {
  670. (*pai)->ai_canonname = ((void *) (*pai) +
  671. sizeof(struct addrinfo) + socklen);
  672. strcpy((*pai)->ai_canonname, c);
  673. } else {
  674. (*pai)->ai_canonname = NULL;
  675. }
  676. (*pai)->ai_next = NULL;
  677. pai = &((*pai)->ai_next);
  678. }
  679. at2 = at2->next;
  680. }
  681. }
  682. return 0;
  683. }
  684. static const struct gaih gaih[] = {
  685. #if defined __UCLIBC_HAS_IPV6__
  686. { PF_INET6, gaih_inet },
  687. #endif
  688. { PF_INET, gaih_inet },
  689. #if 0
  690. { PF_LOCAL, gaih_local },
  691. #endif
  692. { PF_UNSPEC, NULL }
  693. };
  694. /* libc_hidden_proto(freeaddrinfo) */
  695. void
  696. freeaddrinfo(struct addrinfo *ai)
  697. {
  698. struct addrinfo *p;
  699. while (ai != NULL) {
  700. p = ai;
  701. ai = ai->ai_next;
  702. free(p);
  703. }
  704. }
  705. libc_hidden_def(freeaddrinfo)
  706. /* libc_hidden_proto(getaddrinfo) */
  707. int
  708. getaddrinfo(const char *name, const char *service,
  709. const struct addrinfo *hints, struct addrinfo **pai)
  710. {
  711. int i = 0, j, last_i = 0;
  712. struct addrinfo *p = NULL, **end;
  713. const struct gaih *g = gaih, *pg = NULL;
  714. struct gaih_service gaih_service, *pservice;
  715. struct addrinfo default_hints;
  716. if (name != NULL && name[0] == '*' && name[1] == 0)
  717. name = NULL;
  718. if (service != NULL && service[0] == '*' && service[1] == 0)
  719. service = NULL;
  720. if (name == NULL && service == NULL)
  721. return EAI_NONAME;
  722. if (hints == NULL) {
  723. memset(&default_hints, 0, sizeof(default_hints));
  724. if (AF_UNSPEC)
  725. default_hints.ai_family = AF_UNSPEC;
  726. hints = &default_hints;
  727. }
  728. if (hints->ai_flags & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST|
  729. AI_ADDRCONFIG|AI_V4MAPPED|AI_NUMERICSERV|AI_ALL))
  730. return EAI_BADFLAGS;
  731. if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
  732. return EAI_BADFLAGS;
  733. if (service && service[0]) {
  734. char *c;
  735. gaih_service.name = service;
  736. gaih_service.num = strtoul(gaih_service.name, &c, 10);
  737. if (*c != '\0') {
  738. if (hints->ai_flags & AI_NUMERICSERV)
  739. return EAI_NONAME;
  740. gaih_service.num = -1;
  741. } else {
  742. /*
  743. * Can't specify a numerical socket unless a protocol
  744. * family was given.
  745. */
  746. if (hints->ai_socktype == 0 && hints->ai_protocol == 0)
  747. return EAI_SERVICE;
  748. }
  749. pservice = &gaih_service;
  750. } else
  751. pservice = NULL;
  752. end = NULL;
  753. if (pai)
  754. end = &p;
  755. j = 0;
  756. while (g->gaih) {
  757. if (hints->ai_family == g->family || hints->ai_family == AF_UNSPEC) {
  758. if ((hints->ai_flags & AI_ADDRCONFIG) && !addrconfig(g->family)) {
  759. ++g;
  760. continue;
  761. }
  762. j++;
  763. if (pg == NULL || pg->gaih != g->gaih) {
  764. pg = g;
  765. i = g->gaih(name, pservice, hints, end);
  766. if (i != 0) {
  767. last_i = i;
  768. if (hints->ai_family == AF_UNSPEC && (i & GAIH_OKIFUNSPEC))
  769. continue;
  770. if (p)
  771. freeaddrinfo(p);
  772. return -(i & GAIH_EAI);
  773. }
  774. if (end)
  775. while (*end)
  776. end = &((*end)->ai_next);
  777. }
  778. }
  779. ++g;
  780. }
  781. if (j == 0)
  782. return EAI_FAMILY;
  783. if (p) {
  784. *pai = p;
  785. return 0;
  786. }
  787. if (pai == NULL && last_i == 0)
  788. return 0;
  789. /* if (p) - never happens, see above */
  790. /* freeaddrinfo(p); */
  791. return last_i ? -(last_i & GAIH_EAI) : EAI_NONAME;
  792. }
  793. libc_hidden_def(getaddrinfo)