getaddrinfo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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. {
  153. /* Get the interface list via getifaddrs. */
  154. struct ifaddrs *ifa = NULL;
  155. struct ifaddrs *runp;
  156. if (getifaddrs(&ifa) != 0) {
  157. /* We cannot determine what interfaces are available.
  158. * Be optimistic. */
  159. #if defined __UCLIBC_HAS_IPV4__
  160. seen |= SEEN_IPV4;
  161. #endif /* __UCLIBC_HAS_IPV4__ */
  162. #if defined __UCLIBC_HAS_IPV6__
  163. seen |= SEEN_IPV6;
  164. #endif /* __UCLIBC_HAS_IPV6__ */
  165. return seen;
  166. }
  167. for (runp = ifa; runp != NULL; runp = runp->ifa_next)
  168. #if defined __UCLIBC_HAS_IPV4__
  169. if (runp->ifa_addr->sa_family == PF_INET)
  170. seen |= SEEN_IPV4;
  171. #endif /* __UCLIBC_HAS_IPV4__ */
  172. #if defined __UCLIBC_HAS_IPV6__
  173. if (runp->ifa_addr->sa_family == PF_INET6)
  174. seen |= SEEN_IPV6;
  175. #endif /* __UCLIBC_HAS_IPV6__ */
  176. freeifaddrs(ifa);
  177. }
  178. #else
  179. /* AI_ADDRCONFIG is disabled, assume both ipv4 and ipv6 available. */
  180. #if defined __UCLIBC_HAS_IPV4__
  181. seen |= SEEN_IPV4;
  182. #endif /* __UCLIBC_HAS_IPV4__ */
  183. #if defined __UCLIBC_HAS_IPV6__
  184. seen |= SEEN_IPV6;
  185. #endif /* __UCLIBC_HAS_IPV6__ */
  186. #endif /* __UCLIBC_SUPPORT_AI_ADDRCONFIG__ */
  187. return seen;
  188. }
  189. static int addrconfig(sa_family_t af)
  190. {
  191. int s;
  192. int ret;
  193. int saved_errno = errno;
  194. unsigned seen;
  195. seen = __check_pf();
  196. #if defined __UCLIBC_HAS_IPV4__
  197. if (af == AF_INET)
  198. ret = seen & SEEN_IPV4;
  199. else
  200. #endif
  201. #if defined __UCLIBC_HAS_IPV6__
  202. if (af == AF_INET6)
  203. ret = seen & SEEN_IPV6;
  204. else
  205. #endif
  206. {
  207. s = socket(af, SOCK_DGRAM, 0);
  208. ret = 1; /* Assume PF_UNIX. */
  209. if (s < 0) {
  210. if (errno != EMFILE)
  211. ret = 0;
  212. } else
  213. close(s);
  214. }
  215. __set_errno(saved_errno);
  216. return ret;
  217. }
  218. #if 0
  219. /* Using Unix sockets this way is a security risk. */
  220. static int
  221. gaih_local(const char *name, const struct gaih_service *service,
  222. const struct addrinfo *req, struct addrinfo **pai)
  223. {
  224. struct utsname utsname;
  225. struct addrinfo *ai = *pai;
  226. if ((name != NULL) && (req->ai_flags & AI_NUMERICHOST))
  227. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  228. if ((name != NULL) || (req->ai_flags & AI_CANONNAME))
  229. if (uname(&utsname) < 0)
  230. return -EAI_SYSTEM;
  231. if (name != NULL) {
  232. if (strcmp(name, "localhost") &&
  233. strcmp(name, "local") &&
  234. strcmp(name, "unix") &&
  235. strcmp(name, utsname.nodename))
  236. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  237. }
  238. if (req->ai_protocol || req->ai_socktype) {
  239. const struct gaih_typeproto *tp = gaih_inet_typeproto + 1;
  240. while (tp->name[0]
  241. && ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0
  242. || (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  243. || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol))
  244. ) {
  245. ++tp;
  246. }
  247. if (! tp->name[0]) {
  248. if (req->ai_socktype)
  249. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  250. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  251. }
  252. }
  253. *pai = ai = malloc(sizeof(struct addrinfo) + sizeof(struct sockaddr_un)
  254. + ((req->ai_flags & AI_CANONNAME)
  255. ? (strlen(utsname.nodename) + 1) : 0));
  256. if (ai == NULL)
  257. return -EAI_MEMORY;
  258. ai->ai_next = NULL;
  259. ai->ai_flags = req->ai_flags;
  260. ai->ai_family = AF_LOCAL;
  261. ai->ai_socktype = req->ai_socktype ? req->ai_socktype : SOCK_STREAM;
  262. ai->ai_protocol = req->ai_protocol;
  263. ai->ai_addrlen = sizeof(struct sockaddr_un);
  264. ai->ai_addr = (void *)ai + sizeof(struct addrinfo);
  265. #if SALEN
  266. ((struct sockaddr_un *)ai->ai_addr)->sun_len = sizeof(struct sockaddr_un);
  267. #endif /* SALEN */
  268. ((struct sockaddr_un *)ai->ai_addr)->sun_family = AF_LOCAL;
  269. memset(((struct sockaddr_un *)ai->ai_addr)->sun_path, 0, UNIX_PATH_MAX);
  270. if (service) {
  271. struct sockaddr_un *sunp = (struct sockaddr_un *)ai->ai_addr;
  272. if (strchr(service->name, '/') != NULL) {
  273. if (strlen(service->name) >= sizeof(sunp->sun_path))
  274. return GAIH_OKIFUNSPEC | -EAI_SERVICE;
  275. strcpy(sunp->sun_path, service->name);
  276. } else {
  277. if (strlen(P_tmpdir "/") + 1 + strlen(service->name) >= sizeof(sunp->sun_path))
  278. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  279. stpcpy(stpcpy(sunp->sun_path, P_tmpdir "/"), service->name);
  280. }
  281. } else {
  282. /* This is a dangerous use of the interface since there is a time
  283. window between the test for the file and the actual creation
  284. (done by the caller) in which a file with the same name could
  285. be created. */
  286. char *buf = ((struct sockaddr_un *)ai->ai_addr)->sun_path;
  287. if (__path_search(buf, L_tmpnam, NULL, NULL, 0) != 0
  288. || __gen_tempname(buf, __GT_NOCREATE) != 0
  289. ) {
  290. return -EAI_SYSTEM;
  291. }
  292. }
  293. ai->ai_canonname = NULL;
  294. if (req->ai_flags & AI_CANONNAME)
  295. ai->ai_canonname = strcpy((char *)(ai + 1) + sizeof(struct sockaddr_un),
  296. utsname.nodename);
  297. return 0;
  298. }
  299. #endif /* 0 */
  300. static int
  301. gaih_inet_serv(const char *servicename, const struct gaih_typeproto *tp,
  302. const struct addrinfo *req, struct gaih_servtuple *st)
  303. {
  304. struct servent *s;
  305. size_t tmpbuflen = 1024;
  306. struct servent ts;
  307. char *tmpbuf;
  308. int r;
  309. while (1) {
  310. tmpbuf = alloca(tmpbuflen);
  311. r = getservbyname_r(servicename, tp->name, &ts, tmpbuf, tmpbuflen, &s);
  312. if (r == 0 && s != NULL)
  313. break;
  314. if (r != ERANGE)
  315. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  316. tmpbuflen *= 2;
  317. }
  318. st->next = NULL;
  319. st->socktype = tp->socktype;
  320. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY) ? req->ai_protocol : tp->protocol);
  321. st->port = s->s_port;
  322. return 0;
  323. }
  324. /* NB: also uses h,pat,rc,no_data variables */
  325. #define gethosts(_family, _type) \
  326. { \
  327. int i, herrno; \
  328. size_t tmpbuflen; \
  329. struct hostent th; \
  330. char *tmpbuf; \
  331. \
  332. tmpbuflen = 512; \
  333. no_data = 0; \
  334. do { \
  335. tmpbuflen *= 2; \
  336. tmpbuf = alloca(tmpbuflen); \
  337. rc = gethostbyname2_r(name, _family, &th, tmpbuf, \
  338. tmpbuflen, &h, &herrno); \
  339. } while (rc == ERANGE && herrno == NETDB_INTERNAL); \
  340. if (rc != 0) { \
  341. if (herrno == NETDB_INTERNAL) { \
  342. __set_h_errno(herrno); \
  343. return -EAI_SYSTEM; \
  344. } \
  345. if (herrno == TRY_AGAIN) \
  346. no_data = EAI_AGAIN; \
  347. else \
  348. no_data = (herrno == NO_DATA); \
  349. } else if (h != NULL) { \
  350. for (i = 0; h->h_addr_list[i]; i++) { \
  351. if (*pat == NULL) { \
  352. *pat = alloca(sizeof(struct gaih_addrtuple)); \
  353. (*pat)->scopeid = 0; \
  354. } \
  355. (*pat)->next = NULL; \
  356. (*pat)->family = _family; \
  357. memcpy((*pat)->addr, h->h_addr_list[i], sizeof(_type)); \
  358. pat = &((*pat)->next); \
  359. } \
  360. } \
  361. }
  362. static int
  363. gaih_inet(const char *name, const struct gaih_service *service,
  364. const struct addrinfo *req, struct addrinfo **pai)
  365. {
  366. struct gaih_servtuple nullserv;
  367. const struct gaih_typeproto *tp = gaih_inet_typeproto;
  368. struct gaih_servtuple *st = &nullserv;
  369. struct gaih_addrtuple *at = NULL;
  370. int rc;
  371. int v4mapped = (req->ai_family == PF_UNSPEC || req->ai_family == PF_INET6)
  372. && (req->ai_flags & AI_V4MAPPED);
  373. unsigned seen = __check_pf();
  374. memset(&nullserv, 0, sizeof(nullserv));
  375. if (req->ai_protocol || req->ai_socktype) {
  376. ++tp;
  377. while (tp->name[0]
  378. && ((req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  379. || (req->ai_protocol != 0 && !(tp->protoflag & GAI_PROTO_PROTOANY) && req->ai_protocol != tp->protocol)
  380. )
  381. ) {
  382. ++tp;
  383. }
  384. if (! tp->name[0]) {
  385. if (req->ai_socktype)
  386. return (GAIH_OKIFUNSPEC | -EAI_SOCKTYPE);
  387. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  388. }
  389. }
  390. if (service != NULL) {
  391. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  392. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  393. if (service->num < 0) {
  394. if (tp->name[0]) {
  395. st = alloca(sizeof(struct gaih_servtuple));
  396. rc = gaih_inet_serv(service->name, tp, req, st);
  397. if (rc)
  398. return rc;
  399. } else {
  400. struct gaih_servtuple **pst = &st;
  401. for (tp++; tp->name[0]; tp++) {
  402. struct gaih_servtuple *newp;
  403. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  404. continue;
  405. if (req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  406. continue;
  407. if (req->ai_protocol != 0
  408. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  409. && req->ai_protocol != tp->protocol)
  410. continue;
  411. newp = alloca(sizeof(struct gaih_servtuple));
  412. rc = gaih_inet_serv(service->name, tp, req, newp);
  413. if (rc) {
  414. if (rc & GAIH_OKIFUNSPEC)
  415. continue;
  416. return rc;
  417. }
  418. *pst = newp;
  419. pst = &(newp->next);
  420. }
  421. if (st == &nullserv)
  422. return (GAIH_OKIFUNSPEC | -EAI_SERVICE);
  423. }
  424. } else {
  425. st = alloca(sizeof(struct gaih_servtuple));
  426. st->next = NULL;
  427. st->socktype = tp->socktype;
  428. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  429. ? req->ai_protocol : tp->protocol);
  430. st->port = htons(service->num);
  431. }
  432. } else if (req->ai_socktype || req->ai_protocol) {
  433. st = alloca(sizeof(struct gaih_servtuple));
  434. st->next = NULL;
  435. st->socktype = tp->socktype;
  436. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  437. ? req->ai_protocol : tp->protocol);
  438. st->port = 0;
  439. } else {
  440. /*
  441. * Neither socket type nor protocol is set. Return all socket types
  442. * we know about.
  443. */
  444. struct gaih_servtuple **lastp = &st;
  445. for (++tp; tp->name[0]; ++tp) {
  446. struct gaih_servtuple *newp;
  447. newp = alloca(sizeof(struct gaih_servtuple));
  448. newp->next = NULL;
  449. newp->socktype = tp->socktype;
  450. newp->protocol = tp->protocol;
  451. newp->port = 0;
  452. *lastp = newp;
  453. lastp = &newp->next;
  454. }
  455. }
  456. if (name != NULL) {
  457. at = alloca(sizeof(struct gaih_addrtuple));
  458. at->family = AF_UNSPEC;
  459. at->scopeid = 0;
  460. at->next = NULL;
  461. if (inet_pton(AF_INET, name, at->addr) > 0) {
  462. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET || v4mapped)
  463. at->family = AF_INET;
  464. else
  465. return -EAI_FAMILY;
  466. }
  467. #if defined __UCLIBC_HAS_IPV6__
  468. if (at->family == AF_UNSPEC) {
  469. char *namebuf = strdupa(name);
  470. char *scope_delim;
  471. scope_delim = strchr(namebuf, SCOPE_DELIMITER);
  472. if (scope_delim != NULL)
  473. *scope_delim = '\0';
  474. if (inet_pton(AF_INET6, namebuf, at->addr) > 0) {
  475. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  476. at->family = AF_INET6;
  477. else
  478. return -EAI_FAMILY;
  479. if (scope_delim != NULL) {
  480. int try_numericscope = 0;
  481. if (IN6_IS_ADDR_LINKLOCAL(at->addr) || IN6_IS_ADDR_MC_LINKLOCAL(at->addr)) {
  482. at->scopeid = if_nametoindex(scope_delim + 1);
  483. if (at->scopeid == 0)
  484. try_numericscope = 1;
  485. } else
  486. try_numericscope = 1;
  487. if (try_numericscope != 0) {
  488. char *end;
  489. assert(sizeof(uint32_t) <= sizeof(unsigned long));
  490. at->scopeid = (uint32_t)strtoul(scope_delim + 1, &end, 10);
  491. if (*end != '\0')
  492. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  493. }
  494. }
  495. }
  496. }
  497. #endif
  498. if (at->family == AF_UNSPEC && (req->ai_flags & AI_NUMERICHOST) == 0) {
  499. struct hostent *h;
  500. struct gaih_addrtuple **pat = &at;
  501. int no_data = 0;
  502. int no_inet6_data;
  503. /*
  504. * If we are looking for both IPv4 and IPv6 address we don't want
  505. * the lookup functions to automatically promote IPv4 addresses to
  506. * IPv6 addresses.
  507. */
  508. #if defined __UCLIBC_HAS_IPV6__
  509. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  510. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV6))
  511. gethosts(AF_INET6, struct in6_addr);
  512. #endif
  513. no_inet6_data = no_data;
  514. if (req->ai_family == AF_INET
  515. || (!v4mapped && req->ai_family == AF_UNSPEC)
  516. || (v4mapped && (no_inet6_data != 0 || (req->ai_flags & AI_ALL)))
  517. ) {
  518. if (!(req->ai_flags & AI_ADDRCONFIG) || (seen & SEEN_IPV4))
  519. gethosts(AF_INET, struct in_addr);
  520. }
  521. if (no_data != 0 && no_inet6_data != 0) {
  522. /* If both requests timed out report this. */
  523. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  524. return -EAI_AGAIN;
  525. /*
  526. * We made requests but they turned out no data.
  527. * The name is known, though.
  528. */
  529. return (GAIH_OKIFUNSPEC | -EAI_AGAIN);
  530. }
  531. }
  532. if (at->family == AF_UNSPEC)
  533. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  534. } else {
  535. struct gaih_addrtuple *atr;
  536. atr = at = alloca(sizeof(struct gaih_addrtuple));
  537. memset(at, '\0', sizeof(struct gaih_addrtuple));
  538. if (req->ai_family == 0) {
  539. at->next = alloca(sizeof(struct gaih_addrtuple));
  540. memset(at->next, '\0', sizeof(struct gaih_addrtuple));
  541. }
  542. #if defined __UCLIBC_HAS_IPV6__
  543. if (req->ai_family == 0 || req->ai_family == AF_INET6) {
  544. at->family = AF_INET6;
  545. if ((req->ai_flags & AI_PASSIVE) == 0)
  546. memcpy(at->addr, &in6addr_loopback, sizeof(struct in6_addr));
  547. atr = at->next;
  548. }
  549. #endif
  550. if (req->ai_family == 0 || req->ai_family == AF_INET) {
  551. atr->family = AF_INET;
  552. if ((req->ai_flags & AI_PASSIVE) == 0)
  553. *(uint32_t*)atr->addr = htonl(INADDR_LOOPBACK);
  554. }
  555. }
  556. if (pai == NULL)
  557. return 0;
  558. {
  559. const char *c = NULL;
  560. struct gaih_servtuple *st2;
  561. struct gaih_addrtuple *at2 = at;
  562. size_t socklen, namelen;
  563. sa_family_t family;
  564. /*
  565. * buffer is the size of an unformatted IPv6 address in
  566. * printable format.
  567. */
  568. char buffer[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  569. while (at2 != NULL) {
  570. if (req->ai_flags & AI_CANONNAME) {
  571. struct hostent *h = NULL;
  572. int herrno;
  573. struct hostent th;
  574. size_t tmpbuflen = 512;
  575. char *tmpbuf;
  576. do {
  577. tmpbuflen *= 2;
  578. tmpbuf = alloca(tmpbuflen);
  579. //if (tmpbuf == NULL)
  580. // return -EAI_MEMORY;
  581. rc = gethostbyaddr_r(at2->addr,
  582. ((at2->family == AF_INET6)
  583. ? sizeof(struct in6_addr)
  584. : sizeof(struct in_addr)),
  585. at2->family,
  586. &th, tmpbuf, tmpbuflen,
  587. &h, &herrno);
  588. } while (rc == errno && herrno == NETDB_INTERNAL);
  589. if (rc != 0 && herrno == NETDB_INTERNAL) {
  590. __set_h_errno(herrno);
  591. return -EAI_SYSTEM;
  592. }
  593. if (h == NULL)
  594. c = inet_ntop(at2->family, at2->addr, buffer, sizeof(buffer));
  595. else
  596. c = h->h_name;
  597. if (c == NULL)
  598. return (GAIH_OKIFUNSPEC | -EAI_NONAME);
  599. namelen = strlen(c) + 1;
  600. } else
  601. namelen = 0;
  602. #if defined __UCLIBC_HAS_IPV6__
  603. if (at2->family == AF_INET6 || v4mapped) {
  604. family = AF_INET6;
  605. socklen = sizeof(struct sockaddr_in6);
  606. }
  607. #endif
  608. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  609. else
  610. #endif
  611. #if defined __UCLIBC_HAS_IPV4__
  612. {
  613. family = AF_INET;
  614. socklen = sizeof(struct sockaddr_in);
  615. }
  616. #endif
  617. for (st2 = st; st2 != NULL; st2 = st2->next) {
  618. if (req->ai_flags & AI_ADDRCONFIG) {
  619. if (family == AF_INET && !(seen & SEEN_IPV4))
  620. break;
  621. #if defined __UCLIBC_HAS_IPV6__
  622. else if (family == AF_INET6 && !(seen & SEEN_IPV6))
  623. break;
  624. #endif
  625. }
  626. *pai = malloc(sizeof(struct addrinfo) + socklen + namelen);
  627. if (*pai == NULL)
  628. return -EAI_MEMORY;
  629. (*pai)->ai_flags = req->ai_flags;
  630. (*pai)->ai_family = family;
  631. (*pai)->ai_socktype = st2->socktype;
  632. (*pai)->ai_protocol = st2->protocol;
  633. (*pai)->ai_addrlen = socklen;
  634. (*pai)->ai_addr = (void *) (*pai) + sizeof(struct addrinfo);
  635. #if SALEN
  636. (*pai)->ai_addr->sa_len = socklen;
  637. #endif /* SALEN */
  638. (*pai)->ai_addr->sa_family = family;
  639. #if defined __UCLIBC_HAS_IPV6__
  640. if (family == AF_INET6) {
  641. struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *) (*pai)->ai_addr;
  642. sin6p->sin6_flowinfo = 0;
  643. if (at2->family == AF_INET6) {
  644. memcpy(&sin6p->sin6_addr,
  645. at2->addr, sizeof(struct in6_addr));
  646. } else {
  647. sin6p->sin6_addr.s6_addr32[0] = 0;
  648. sin6p->sin6_addr.s6_addr32[1] = 0;
  649. sin6p->sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
  650. memcpy(&sin6p->sin6_addr.s6_addr32[3],
  651. at2->addr, sizeof(sin6p->sin6_addr.s6_addr32[3]));
  652. }
  653. sin6p->sin6_port = st2->port;
  654. sin6p->sin6_scope_id = at2->scopeid;
  655. }
  656. #endif
  657. #if defined __UCLIBC_HAS_IPV4__ && defined __UCLIBC_HAS_IPV6__
  658. else
  659. #endif
  660. #if defined __UCLIBC_HAS_IPV4__
  661. {
  662. struct sockaddr_in *sinp = (struct sockaddr_in *) (*pai)->ai_addr;
  663. memcpy(&sinp->sin_addr, at2->addr, sizeof(struct in_addr));
  664. sinp->sin_port = st2->port;
  665. memset(sinp->sin_zero, '\0', sizeof(sinp->sin_zero));
  666. }
  667. #endif
  668. if (c) {
  669. (*pai)->ai_canonname = ((void *) (*pai) +
  670. sizeof(struct addrinfo) + socklen);
  671. strcpy((*pai)->ai_canonname, c);
  672. } else {
  673. (*pai)->ai_canonname = NULL;
  674. }
  675. (*pai)->ai_next = NULL;
  676. pai = &((*pai)->ai_next);
  677. }
  678. at2 = at2->next;
  679. }
  680. }
  681. return 0;
  682. }
  683. static const struct gaih gaih[] = {
  684. #if defined __UCLIBC_HAS_IPV6__
  685. { PF_INET6, gaih_inet },
  686. #endif
  687. { PF_INET, gaih_inet },
  688. #if 0
  689. { PF_LOCAL, gaih_local },
  690. #endif
  691. { PF_UNSPEC, NULL }
  692. };
  693. /* libc_hidden_proto(freeaddrinfo) */
  694. void
  695. freeaddrinfo(struct addrinfo *ai)
  696. {
  697. struct addrinfo *p;
  698. while (ai != NULL) {
  699. p = ai;
  700. ai = ai->ai_next;
  701. free(p);
  702. }
  703. }
  704. libc_hidden_def(freeaddrinfo)
  705. /* libc_hidden_proto(getaddrinfo) */
  706. int
  707. getaddrinfo(const char *name, const char *service,
  708. const struct addrinfo *hints, struct addrinfo **pai)
  709. {
  710. int i = 0, j, last_i = 0;
  711. struct addrinfo *p = NULL, **end;
  712. const struct gaih *g = gaih, *pg = NULL;
  713. struct gaih_service gaih_service, *pservice;
  714. struct addrinfo default_hints;
  715. if (name != NULL && name[0] == '*' && name[1] == 0)
  716. name = NULL;
  717. if (service != NULL && service[0] == '*' && service[1] == 0)
  718. service = NULL;
  719. if (name == NULL && service == NULL)
  720. return EAI_NONAME;
  721. if (hints == NULL) {
  722. memset(&default_hints, 0, sizeof(default_hints));
  723. if (AF_UNSPEC)
  724. default_hints.ai_family = AF_UNSPEC;
  725. hints = &default_hints;
  726. }
  727. if (hints->ai_flags & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST|
  728. AI_ADDRCONFIG|AI_V4MAPPED|AI_NUMERICSERV|AI_ALL))
  729. return EAI_BADFLAGS;
  730. if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
  731. return EAI_BADFLAGS;
  732. if (service && service[0]) {
  733. char *c;
  734. gaih_service.name = service;
  735. gaih_service.num = strtoul(gaih_service.name, &c, 10);
  736. if (*c != '\0') {
  737. if (hints->ai_flags & AI_NUMERICSERV)
  738. return EAI_NONAME;
  739. gaih_service.num = -1;
  740. } else {
  741. /*
  742. * Can't specify a numerical socket unless a protocol
  743. * family was given.
  744. */
  745. if (hints->ai_socktype == 0 && hints->ai_protocol == 0)
  746. return EAI_SERVICE;
  747. }
  748. pservice = &gaih_service;
  749. } else
  750. pservice = NULL;
  751. end = NULL;
  752. if (pai)
  753. end = &p;
  754. j = 0;
  755. while (g->gaih) {
  756. if (hints->ai_family == g->family || hints->ai_family == AF_UNSPEC) {
  757. if ((hints->ai_flags & AI_ADDRCONFIG) && !addrconfig(g->family)) {
  758. ++g;
  759. continue;
  760. }
  761. j++;
  762. if (pg == NULL || pg->gaih != g->gaih) {
  763. pg = g;
  764. i = g->gaih(name, pservice, hints, end);
  765. if (i != 0) {
  766. last_i = i;
  767. if (hints->ai_family == AF_UNSPEC && (i & GAIH_OKIFUNSPEC))
  768. continue;
  769. if (p)
  770. freeaddrinfo(p);
  771. return -(i & GAIH_EAI);
  772. }
  773. if (end)
  774. while (*end)
  775. end = &((*end)->ai_next);
  776. }
  777. }
  778. ++g;
  779. }
  780. if (j == 0)
  781. return EAI_FAMILY;
  782. if (p) {
  783. *pai = p;
  784. return 0;
  785. }
  786. if (pai == NULL && last_i == 0)
  787. return 0;
  788. /* if (p) - never happens, see above */
  789. /* freeaddrinfo(p); */
  790. return last_i ? -(last_i & GAIH_EAI) : EAI_NONAME;
  791. }
  792. libc_hidden_def(getaddrinfo)