getaddrinfo.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  1. /* Host and service name lookups using Name Service Switch modules.
  2. Copyright (C) 1996-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* The Inner Net License, Version 2.00
  16. The author(s) grant permission for redistribution and use in source and
  17. binary forms, with or without modification, of the software and documentation
  18. provided that the following conditions are met:
  19. 0. If you receive a version of the software that is specifically labelled
  20. as not being for redistribution (check the version message and/or README),
  21. you are not permitted to redistribute that version of the software in any
  22. way or form.
  23. 1. All terms of the all other applicable copyrights and licenses must be
  24. followed.
  25. 2. Redistributions of source code must retain the authors' copyright
  26. notice(s), this list of conditions, and the following disclaimer.
  27. 3. Redistributions in binary form must reproduce the authors' copyright
  28. notice(s), this list of conditions, and the following disclaimer in the
  29. documentation and/or other materials provided with the distribution.
  30. 4. [The copyright holder has authorized the removal of this clause.]
  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. /* This software is Copyright 1996 by Craig Metz, All Rights Reserved. */
  46. #include <assert.h>
  47. #include <ctype.h>
  48. #include <errno.h>
  49. #include <ifaddrs.h>
  50. #include <netdb.h>
  51. #include <nss.h>
  52. #include <resolv/resolv-internal.h>
  53. #include <resolv/resolv_context.h>
  54. #include <stdbit.h>
  55. #include <stdbool.h>
  56. #include <stdio.h>
  57. #include <stdio_ext.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #include <stdint.h>
  61. #include <arpa/inet.h>
  62. #include <net/if.h>
  63. #include <netinet/in.h>
  64. #include <sys/socket.h>
  65. #include <sys/stat.h>
  66. #include <sys/types.h>
  67. #include <sys/un.h>
  68. #include <sys/utsname.h>
  69. #include <unistd.h>
  70. #include <nsswitch.h>
  71. #include <libc-lock.h>
  72. #include <not-cancel.h>
  73. #include <nscd/nscd-client.h>
  74. #include <nscd/nscd_proto.h>
  75. #include <scratch_buffer.h>
  76. #include <inet/net-internal.h>
  77. /* Former AI_IDN_ALLOW_UNASSIGNED and AI_IDN_USE_STD3_ASCII_RULES
  78. flags, now ignored. */
  79. #define DEPRECATED_AI_IDN 0x300
  80. #if IS_IN (libc)
  81. # define feof_unlocked(fp) __feof_unlocked (fp)
  82. #endif
  83. struct gaih_service
  84. {
  85. const char *name;
  86. int num;
  87. };
  88. struct gaih_servtuple
  89. {
  90. int socktype;
  91. int protocol;
  92. int port;
  93. bool set;
  94. };
  95. struct gaih_typeproto
  96. {
  97. int socktype;
  98. int protocol;
  99. uint8_t protoflag;
  100. bool defaultflag;
  101. char name[8];
  102. };
  103. struct gaih_result
  104. {
  105. struct gaih_addrtuple *at;
  106. char *canon;
  107. char *h_name;
  108. bool free_at;
  109. bool got_ipv6;
  110. };
  111. /* Values for `protoflag'. */
  112. #define GAI_PROTO_NOSERVICE 1
  113. #define GAI_PROTO_PROTOANY 2
  114. static const struct gaih_typeproto gaih_inet_typeproto[] =
  115. {
  116. { 0, 0, 0, false, "" },
  117. { SOCK_STREAM, IPPROTO_TCP, 0, true, "tcp" },
  118. { SOCK_DGRAM, IPPROTO_UDP, 0, true, "udp" },
  119. #if defined SOCK_DCCP && defined IPPROTO_DCCP
  120. { SOCK_DCCP, IPPROTO_DCCP, 0, false, "dccp" },
  121. #endif
  122. #ifdef IPPROTO_UDPLITE
  123. { SOCK_DGRAM, IPPROTO_UDPLITE, 0, false, "udplite" },
  124. #endif
  125. #ifdef IPPROTO_SCTP
  126. { SOCK_STREAM, IPPROTO_SCTP, 0, false, "sctp" },
  127. { SOCK_SEQPACKET, IPPROTO_SCTP, 0, false, "sctp" },
  128. #endif
  129. #ifdef IPPROTO_MPTCP
  130. { SOCK_STREAM, IPPROTO_MPTCP, 0, false, "mptcp" },
  131. #endif
  132. { SOCK_RAW, 0, GAI_PROTO_PROTOANY|GAI_PROTO_NOSERVICE, true, "raw" },
  133. { 0, 0, 0, false, "" }
  134. };
  135. static const struct addrinfo default_hints =
  136. {
  137. .ai_flags = AI_DEFAULT,
  138. .ai_family = PF_UNSPEC,
  139. .ai_socktype = 0,
  140. .ai_protocol = 0,
  141. .ai_addrlen = 0,
  142. .ai_addr = NULL,
  143. .ai_canonname = NULL,
  144. .ai_next = NULL
  145. };
  146. static void
  147. gaih_result_reset (struct gaih_result *res)
  148. {
  149. if (res->free_at)
  150. free (res->at);
  151. free (res->canon);
  152. free (res->h_name);
  153. memset (res, 0, sizeof (*res));
  154. }
  155. static int
  156. gaih_inet_serv (const char *servicename, const struct gaih_typeproto *tp,
  157. const struct addrinfo *req, struct gaih_servtuple *st,
  158. struct scratch_buffer *tmpbuf)
  159. {
  160. struct servent *s;
  161. struct servent ts;
  162. int r;
  163. do
  164. {
  165. r = __getservbyname_r (servicename, tp->name, &ts,
  166. tmpbuf->data, tmpbuf->length, &s);
  167. if (r != 0 || s == NULL)
  168. {
  169. if (r == ERANGE)
  170. {
  171. if (!scratch_buffer_grow (tmpbuf))
  172. return -EAI_MEMORY;
  173. }
  174. else
  175. return -EAI_SERVICE;
  176. }
  177. }
  178. while (r);
  179. st->socktype = tp->socktype;
  180. st->protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  181. ? req->ai_protocol : tp->protocol);
  182. st->port = s->s_port;
  183. st->set = true;
  184. return 0;
  185. }
  186. /* Convert struct hostent to a list of struct gaih_addrtuple objects. The new
  187. addresses are appended to the tuple array in RES. */
  188. static bool
  189. convert_hostent_to_gaih_addrtuple (const struct addrinfo *req, int family,
  190. struct hostent *h, struct gaih_result *res)
  191. {
  192. /* Count the number of addresses in h->h_addr_list. */
  193. size_t count = 0;
  194. for (char **p = h->h_addr_list; *p != NULL; ++p)
  195. ++count;
  196. /* Report no data if no addresses are available, or if the incoming
  197. address size is larger than what we can store. */
  198. if (count == 0 || h->h_length > sizeof (((struct gaih_addrtuple) {}).addr))
  199. return true;
  200. struct gaih_addrtuple *array = res->at;
  201. size_t old = 0;
  202. while (array != NULL)
  203. {
  204. old++;
  205. array = array->next;
  206. }
  207. array = realloc (res->at, (old + count) * sizeof (*array));
  208. if (array == NULL)
  209. return false;
  210. res->got_ipv6 = family == AF_INET6;
  211. res->at = array;
  212. res->free_at = true;
  213. /* Duplicate h_name because it may get reclaimed when the underlying storage
  214. is freed. */
  215. if (res->h_name == NULL)
  216. {
  217. res->h_name = __strdup (h->h_name);
  218. if (res->h_name == NULL)
  219. return false;
  220. }
  221. /* Update the next pointers on reallocation. */
  222. for (size_t i = 0; i < old; i++)
  223. array[i].next = array + i + 1;
  224. array += old;
  225. memset (array, 0, count * sizeof (*array));
  226. for (size_t i = 0; i < count; ++i)
  227. {
  228. if (family == AF_INET && req->ai_family == AF_INET6)
  229. {
  230. /* Perform address mapping. */
  231. array[i].family = AF_INET6;
  232. memcpy(array[i].addr + 3, h->h_addr_list[i], sizeof (uint32_t));
  233. array[i].addr[2] = htonl (0xffff);
  234. }
  235. else
  236. {
  237. array[i].family = family;
  238. memcpy (array[i].addr, h->h_addr_list[i], h->h_length);
  239. }
  240. array[i].next = array + i + 1;
  241. }
  242. array[count - 1].next = NULL;
  243. return true;
  244. }
  245. static int
  246. gethosts (nss_gethostbyname3_r fct, int family, const char *name,
  247. const struct addrinfo *req, struct scratch_buffer *tmpbuf,
  248. struct gaih_result *res, enum nss_status *statusp, int *no_datap)
  249. {
  250. struct hostent th;
  251. char *localcanon = NULL;
  252. enum nss_status status;
  253. *no_datap = 0;
  254. while (1)
  255. {
  256. *statusp = status = DL_CALL_FCT (fct, (name, family, &th,
  257. tmpbuf->data, tmpbuf->length,
  258. &errno, &h_errno, NULL,
  259. &localcanon));
  260. if (status != NSS_STATUS_TRYAGAIN || h_errno != NETDB_INTERNAL
  261. || errno != ERANGE)
  262. break;
  263. if (!scratch_buffer_grow (tmpbuf))
  264. return -EAI_MEMORY;
  265. }
  266. if (status == NSS_STATUS_NOTFOUND
  267. || status == NSS_STATUS_TRYAGAIN || status == NSS_STATUS_UNAVAIL)
  268. {
  269. if (h_errno == NETDB_INTERNAL)
  270. return -EAI_SYSTEM;
  271. if (h_errno == TRY_AGAIN)
  272. *no_datap = EAI_AGAIN;
  273. else
  274. *no_datap = h_errno == NO_DATA;
  275. }
  276. else if (status == NSS_STATUS_SUCCESS)
  277. {
  278. if (!convert_hostent_to_gaih_addrtuple (req, family, &th, res))
  279. return -EAI_MEMORY;
  280. if (localcanon != NULL && res->canon == NULL)
  281. {
  282. char *canonbuf = __strdup (localcanon);
  283. if (canonbuf == NULL)
  284. return -EAI_MEMORY;
  285. res->canon = canonbuf;
  286. }
  287. }
  288. return 0;
  289. }
  290. /* This function is called if a canonical name is requested, but if
  291. the service function did not provide it. It tries to obtain the
  292. name using getcanonname_r from the same service NIP. If the name
  293. cannot be canonicalized, return a copy of NAME. Return NULL on
  294. memory allocation failure. The returned string is allocated on the
  295. heap; the caller has to free it. */
  296. static char *
  297. getcanonname (nss_action_list nip, const char *hname, const char *name)
  298. {
  299. nss_getcanonname_r *cfct = __nss_lookup_function (nip, "getcanonname_r");
  300. char *s = (char *) name;
  301. if (cfct != NULL)
  302. {
  303. char buf[256];
  304. if (DL_CALL_FCT (cfct, (hname ?: name, buf, sizeof (buf), &s, &errno,
  305. &h_errno)) != NSS_STATUS_SUCCESS)
  306. /* If the canonical name cannot be determined, use the passed
  307. string. */
  308. s = (char *) name;
  309. }
  310. return __strdup (s);
  311. }
  312. /* Process looked up canonical name and if necessary, decode to IDNA. Result
  313. is a new string written to CANONP and the earlier string is freed. */
  314. static int
  315. process_canonname (const struct addrinfo *req, const char *orig_name,
  316. struct gaih_result *res)
  317. {
  318. char *canon = res->canon;
  319. if ((req->ai_flags & AI_CANONNAME) != 0)
  320. {
  321. bool do_idn = req->ai_flags & AI_CANONIDN;
  322. if (do_idn)
  323. {
  324. char *out;
  325. int rc = __idna_from_dns_encoding (canon ?: orig_name, &out);
  326. if (rc == 0)
  327. {
  328. free (canon);
  329. canon = out;
  330. }
  331. else if (rc == EAI_IDN_ENCODE)
  332. /* Use the punycode name as a fallback. */
  333. do_idn = false;
  334. else
  335. return -rc;
  336. }
  337. if (!do_idn && canon == NULL && (canon = __strdup (orig_name)) == NULL)
  338. return -EAI_MEMORY;
  339. }
  340. res->canon = canon;
  341. return 0;
  342. }
  343. static int
  344. get_servtuples (const struct gaih_service *service, const struct addrinfo *req,
  345. struct gaih_servtuple *st, struct scratch_buffer *tmpbuf)
  346. {
  347. int i;
  348. const struct gaih_typeproto *tp = gaih_inet_typeproto;
  349. if (req->ai_protocol || req->ai_socktype)
  350. {
  351. ++tp;
  352. while (tp->name[0]
  353. && ((req->ai_socktype != 0 && req->ai_socktype != tp->socktype)
  354. || (req->ai_protocol != 0
  355. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  356. && req->ai_protocol != tp->protocol)))
  357. ++tp;
  358. if (! tp->name[0])
  359. {
  360. if (req->ai_socktype)
  361. return -EAI_SOCKTYPE;
  362. else
  363. return -EAI_SERVICE;
  364. }
  365. }
  366. if (service != NULL && (tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  367. return -EAI_SERVICE;
  368. if (service == NULL || service->num >= 0)
  369. {
  370. int port = service != NULL ? htons (service->num) : 0;
  371. if (req->ai_socktype || req->ai_protocol)
  372. {
  373. st[0].socktype = tp->socktype;
  374. st[0].protocol = ((tp->protoflag & GAI_PROTO_PROTOANY)
  375. ? req->ai_protocol : tp->protocol);
  376. st[0].port = port;
  377. st[0].set = true;
  378. return 0;
  379. }
  380. /* Neither socket type nor protocol is set. Return all socket types
  381. we know about. */
  382. for (i = 0, ++tp; tp->name[0]; ++tp)
  383. if (tp->defaultflag)
  384. {
  385. st[i].socktype = tp->socktype;
  386. st[i].protocol = tp->protocol;
  387. st[i].port = port;
  388. st[i++].set = true;
  389. }
  390. return 0;
  391. }
  392. if (tp->name[0])
  393. return gaih_inet_serv (service->name, tp, req, st, tmpbuf);
  394. for (i = 0, tp++; tp->name[0]; tp++)
  395. {
  396. if ((tp->protoflag & GAI_PROTO_NOSERVICE) != 0)
  397. continue;
  398. if (req->ai_socktype != 0
  399. && req->ai_socktype != tp->socktype)
  400. continue;
  401. if (req->ai_protocol != 0
  402. && !(tp->protoflag & GAI_PROTO_PROTOANY)
  403. && req->ai_protocol != tp->protocol)
  404. continue;
  405. if (gaih_inet_serv (service->name,
  406. tp, req, &st[i], tmpbuf) != 0)
  407. continue;
  408. i++;
  409. }
  410. if (!st[0].set)
  411. return -EAI_SERVICE;
  412. return 0;
  413. }
  414. #ifdef USE_NSCD
  415. /* Query addresses from nscd cache, returning a non-zero value on error.
  416. RES members have the lookup result; RES->AT is NULL if there were no errors
  417. but also no results. */
  418. static int
  419. get_nscd_addresses (const char *name, const struct addrinfo *req,
  420. struct gaih_result *res)
  421. {
  422. if (__nss_not_use_nscd_hosts > 0
  423. && ++__nss_not_use_nscd_hosts > NSS_NSCD_RETRY)
  424. __nss_not_use_nscd_hosts = 0;
  425. res->at = NULL;
  426. if (__nss_not_use_nscd_hosts || __nss_database_custom[NSS_DBSIDX_hosts])
  427. return 0;
  428. /* Try to use nscd. */
  429. struct nscd_ai_result *air = NULL;
  430. int err = __nscd_getai (name, &air, &h_errno);
  431. if (__glibc_unlikely (air == NULL))
  432. {
  433. /* The database contains a negative entry. */
  434. if (err == 0)
  435. return -EAI_NONAME;
  436. if (__nss_not_use_nscd_hosts == 0)
  437. {
  438. if (h_errno == NETDB_INTERNAL && errno == ENOMEM)
  439. return -EAI_MEMORY;
  440. if (h_errno == TRY_AGAIN)
  441. return -EAI_AGAIN;
  442. return -EAI_SYSTEM;
  443. }
  444. return 0;
  445. }
  446. /* Transform into gaih_addrtuple list. */
  447. int result = 0;
  448. char *addrs = air->addrs;
  449. struct gaih_addrtuple *at = calloc (air->naddrs, sizeof (*at));
  450. if (at == NULL)
  451. {
  452. result = -EAI_MEMORY;
  453. goto out;
  454. }
  455. res->free_at = true;
  456. int count = 0;
  457. for (int i = 0; i < air->naddrs; ++i)
  458. {
  459. socklen_t size = (air->family[i] == AF_INET
  460. ? INADDRSZ : IN6ADDRSZ);
  461. if (!((air->family[i] == AF_INET
  462. && req->ai_family == AF_INET6
  463. && (req->ai_flags & AI_V4MAPPED) != 0)
  464. || req->ai_family == AF_UNSPEC
  465. || air->family[i] == req->ai_family))
  466. {
  467. /* Skip over non-matching result. */
  468. addrs += size;
  469. continue;
  470. }
  471. if (air->family[i] == AF_INET && req->ai_family == AF_INET6
  472. && (req->ai_flags & AI_V4MAPPED))
  473. {
  474. at[count].family = AF_INET6;
  475. at[count].addr[3] = *(uint32_t *) addrs;
  476. at[count].addr[2] = htonl (0xffff);
  477. }
  478. else if (req->ai_family == AF_UNSPEC
  479. || air->family[i] == req->ai_family)
  480. {
  481. at[count].family = air->family[i];
  482. memcpy (at[count].addr, addrs, size);
  483. if (air->family[i] == AF_INET6)
  484. res->got_ipv6 = true;
  485. }
  486. at[count].next = at + count + 1;
  487. count++;
  488. addrs += size;
  489. }
  490. if ((req->ai_flags & AI_CANONNAME) && air->canon != NULL)
  491. {
  492. char *canonbuf = __strdup (air->canon);
  493. if (canonbuf == NULL)
  494. {
  495. result = -EAI_MEMORY;
  496. goto out;
  497. }
  498. res->canon = canonbuf;
  499. }
  500. if (count == 0)
  501. {
  502. result = -EAI_NONAME;
  503. goto out;
  504. }
  505. at[count - 1].next = NULL;
  506. res->at = at;
  507. out:
  508. free (air);
  509. if (result != 0)
  510. {
  511. free (at);
  512. res->free_at = false;
  513. }
  514. return result;
  515. }
  516. #endif
  517. static int
  518. get_nss_addresses (const char *name, const struct addrinfo *req,
  519. struct scratch_buffer *tmpbuf, struct gaih_result *res)
  520. {
  521. int no_data = 0;
  522. int no_inet6_data = 0;
  523. nss_action_list nip;
  524. enum nss_status inet6_status = NSS_STATUS_UNAVAIL;
  525. enum nss_status status = NSS_STATUS_UNAVAIL;
  526. int no_more;
  527. struct resolv_context *res_ctx = NULL;
  528. bool do_merge = false;
  529. int result = 0;
  530. no_more = !__nss_database_get (nss_database_hosts, &nip);
  531. /* If we are looking for both IPv4 and IPv6 address we don't
  532. want the lookup functions to automatically promote IPv4
  533. addresses to IPv6 addresses, so we use the no_inet6
  534. function variant. */
  535. res_ctx = __resolv_context_get ();
  536. if (res_ctx == NULL)
  537. {
  538. if (errno == ENOMEM)
  539. {
  540. result = -EAI_MEMORY;
  541. goto out;
  542. }
  543. no_more = 1;
  544. }
  545. while (!no_more)
  546. {
  547. /* Always start afresh; continue should discard previous results
  548. and the hosts database does not support merge. */
  549. gaih_result_reset (res);
  550. if (do_merge)
  551. {
  552. __set_h_errno (NETDB_INTERNAL);
  553. __set_errno (EBUSY);
  554. break;
  555. }
  556. no_data = 0;
  557. nss_gethostbyname4_r *fct4 = NULL;
  558. /* gethostbyname4_r sends out parallel A and AAAA queries and
  559. is thus only suitable for PF_UNSPEC. */
  560. if (req->ai_family == PF_UNSPEC)
  561. fct4 = __nss_lookup_function (nip, "gethostbyname4_r");
  562. if (fct4 != NULL)
  563. {
  564. while (1)
  565. {
  566. status = DL_CALL_FCT (fct4, (name, &res->at,
  567. tmpbuf->data, tmpbuf->length,
  568. &errno, &h_errno,
  569. NULL));
  570. if (status == NSS_STATUS_SUCCESS)
  571. break;
  572. /* gethostbyname4_r may write into AT, so reset it. */
  573. res->at = NULL;
  574. if (status != NSS_STATUS_TRYAGAIN
  575. || errno != ERANGE || h_errno != NETDB_INTERNAL)
  576. {
  577. if (h_errno == TRY_AGAIN)
  578. no_data = EAI_AGAIN;
  579. else
  580. no_data = h_errno == NO_DATA;
  581. break;
  582. }
  583. if (!scratch_buffer_grow (tmpbuf))
  584. {
  585. __resolv_context_put (res_ctx);
  586. result = -EAI_MEMORY;
  587. goto out;
  588. }
  589. }
  590. if (status == NSS_STATUS_SUCCESS)
  591. {
  592. assert (!no_data);
  593. no_data = 1;
  594. if ((req->ai_flags & AI_CANONNAME) != 0 && res->canon == NULL)
  595. {
  596. char *canonbuf = __strdup (res->at->name);
  597. if (canonbuf == NULL)
  598. {
  599. __resolv_context_put (res_ctx);
  600. result = -EAI_MEMORY;
  601. goto out;
  602. }
  603. res->canon = canonbuf;
  604. }
  605. struct gaih_addrtuple **pat = &res->at;
  606. while (*pat != NULL)
  607. {
  608. if ((*pat)->family == AF_INET
  609. && req->ai_family == AF_INET6
  610. && (req->ai_flags & AI_V4MAPPED) != 0)
  611. {
  612. uint32_t *pataddr = (*pat)->addr;
  613. (*pat)->family = AF_INET6;
  614. pataddr[3] = pataddr[0];
  615. pataddr[2] = htonl (0xffff);
  616. pataddr[1] = 0;
  617. pataddr[0] = 0;
  618. pat = &((*pat)->next);
  619. no_data = 0;
  620. }
  621. else if (req->ai_family == AF_UNSPEC
  622. || (*pat)->family == req->ai_family)
  623. {
  624. pat = &((*pat)->next);
  625. no_data = 0;
  626. if (req->ai_family == AF_INET6)
  627. res->got_ipv6 = true;
  628. }
  629. else
  630. *pat = ((*pat)->next);
  631. }
  632. }
  633. no_inet6_data = no_data;
  634. }
  635. else
  636. {
  637. nss_gethostbyname3_r *fct = NULL;
  638. if (req->ai_flags & AI_CANONNAME)
  639. /* No need to use this function if we do not look for
  640. the canonical name. The function does not exist in
  641. all NSS modules and therefore the lookup would
  642. often fail. */
  643. fct = __nss_lookup_function (nip, "gethostbyname3_r");
  644. if (fct == NULL)
  645. /* We are cheating here. The gethostbyname2_r
  646. function does not have the same interface as
  647. gethostbyname3_r but the extra arguments the
  648. latter takes are added at the end. So the
  649. gethostbyname2_r code will just ignore them. */
  650. fct = __nss_lookup_function (nip, "gethostbyname2_r");
  651. if (fct != NULL)
  652. {
  653. if (req->ai_family == AF_INET6
  654. || req->ai_family == AF_UNSPEC)
  655. {
  656. if ((result = gethosts (fct, AF_INET6, name, req, tmpbuf,
  657. res, &status, &no_data)) != 0)
  658. {
  659. __resolv_context_put (res_ctx);
  660. goto out;
  661. }
  662. no_inet6_data = no_data;
  663. inet6_status = status;
  664. }
  665. if (req->ai_family == AF_INET
  666. || req->ai_family == AF_UNSPEC
  667. || (req->ai_family == AF_INET6
  668. && (req->ai_flags & AI_V4MAPPED)
  669. /* Avoid generating the mapped addresses if we
  670. know we are not going to need them. */
  671. && ((req->ai_flags & AI_ALL) || !res->got_ipv6)))
  672. {
  673. if ((result = gethosts (fct, AF_INET, name, req, tmpbuf,
  674. res, &status, &no_data)) != 0)
  675. {
  676. __resolv_context_put (res_ctx);
  677. goto out;
  678. }
  679. if (req->ai_family == AF_INET)
  680. {
  681. no_inet6_data = no_data;
  682. inet6_status = status;
  683. }
  684. }
  685. /* If we found one address for AF_INET or AF_INET6,
  686. don't continue the search. */
  687. if (inet6_status == NSS_STATUS_SUCCESS
  688. || status == NSS_STATUS_SUCCESS)
  689. {
  690. if ((req->ai_flags & AI_CANONNAME) != 0
  691. && res->canon == NULL)
  692. {
  693. char *canonbuf = getcanonname (nip, res->h_name, name);
  694. if (canonbuf == NULL)
  695. {
  696. __resolv_context_put (res_ctx);
  697. result = -EAI_MEMORY;
  698. goto out;
  699. }
  700. res->canon = canonbuf;
  701. }
  702. status = NSS_STATUS_SUCCESS;
  703. }
  704. else
  705. {
  706. /* We can have different states for AF_INET and
  707. AF_INET6. Try to find a useful one for both. */
  708. if (inet6_status == NSS_STATUS_TRYAGAIN)
  709. status = NSS_STATUS_TRYAGAIN;
  710. else if (status == NSS_STATUS_UNAVAIL
  711. && inet6_status != NSS_STATUS_UNAVAIL)
  712. status = inet6_status;
  713. }
  714. }
  715. else
  716. {
  717. /* Could not locate any of the lookup functions.
  718. The NSS lookup code does not consistently set
  719. errno, so we need to supply our own error
  720. code here. The root cause could either be a
  721. resource allocation failure, or a missing
  722. service function in the DSO (so it should not
  723. be listed in /etc/nsswitch.conf). Assume the
  724. former, and return EBUSY. */
  725. status = NSS_STATUS_UNAVAIL;
  726. __set_h_errno (NETDB_INTERNAL);
  727. __set_errno (EBUSY);
  728. }
  729. }
  730. if (nss_next_action (nip, status) == NSS_ACTION_RETURN)
  731. break;
  732. /* The hosts database does not support MERGE. */
  733. if (nss_next_action (nip, status) == NSS_ACTION_MERGE)
  734. do_merge = true;
  735. nip++;
  736. if (nip->module == NULL)
  737. no_more = -1;
  738. }
  739. __resolv_context_put (res_ctx);
  740. /* If we have a failure which sets errno, report it using
  741. EAI_SYSTEM. */
  742. if ((status == NSS_STATUS_TRYAGAIN || status == NSS_STATUS_UNAVAIL)
  743. && h_errno == NETDB_INTERNAL)
  744. {
  745. result = -EAI_SYSTEM;
  746. goto out;
  747. }
  748. if (no_data != 0 && no_inet6_data != 0)
  749. {
  750. /* If both requests timed out report this. */
  751. if (no_data == EAI_AGAIN && no_inet6_data == EAI_AGAIN)
  752. result = -EAI_AGAIN;
  753. else
  754. /* We made requests but they turned out no data. The name
  755. is known, though. */
  756. result = -EAI_NODATA;
  757. }
  758. out:
  759. if (result != 0)
  760. gaih_result_reset (res);
  761. return result;
  762. }
  763. /* Convert numeric addresses to binary into RES. On failure, RES->AT is set to
  764. NULL and an error code is returned. If AI_NUMERIC_HOST is not requested and
  765. the function cannot determine a result, RES->AT is set to NULL and 0
  766. returned. */
  767. static int
  768. text_to_binary_address (const char *name, const struct addrinfo *req,
  769. struct gaih_result *res)
  770. {
  771. struct gaih_addrtuple *at = res->at;
  772. int result = 0;
  773. assert (at != NULL);
  774. memset (at->addr, 0, sizeof (at->addr));
  775. if (__inet_aton_exact (name, (struct in_addr *) at->addr) != 0)
  776. {
  777. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET)
  778. at->family = AF_INET;
  779. else if (req->ai_family == AF_INET6 && (req->ai_flags & AI_V4MAPPED))
  780. {
  781. at->addr[3] = at->addr[0];
  782. at->addr[2] = htonl (0xffff);
  783. at->addr[1] = 0;
  784. at->addr[0] = 0;
  785. at->family = AF_INET6;
  786. }
  787. else
  788. {
  789. result = -EAI_ADDRFAMILY;
  790. goto out;
  791. }
  792. if (req->ai_flags & AI_CANONNAME)
  793. {
  794. char *canonbuf = __strdup (name);
  795. if (canonbuf == NULL)
  796. {
  797. result = -EAI_MEMORY;
  798. goto out;
  799. }
  800. res->canon = canonbuf;
  801. }
  802. return 0;
  803. }
  804. char *scope_delim = strchr (name, SCOPE_DELIMITER);
  805. int e;
  806. if (scope_delim == NULL)
  807. e = inet_pton (AF_INET6, name, at->addr);
  808. else
  809. e = __inet_pton_length (AF_INET6, name, scope_delim - name, at->addr);
  810. if (e > 0)
  811. {
  812. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  813. at->family = AF_INET6;
  814. else if (req->ai_family == AF_INET
  815. && IN6_IS_ADDR_V4MAPPED (at->addr))
  816. {
  817. at->addr[0] = at->addr[3];
  818. at->family = AF_INET;
  819. }
  820. else
  821. {
  822. result = -EAI_ADDRFAMILY;
  823. goto out;
  824. }
  825. if (scope_delim != NULL
  826. && __inet6_scopeid_pton ((struct in6_addr *) at->addr,
  827. scope_delim + 1, &at->scopeid) != 0)
  828. {
  829. result = -EAI_NONAME;
  830. goto out;
  831. }
  832. if (req->ai_flags & AI_CANONNAME)
  833. {
  834. char *canonbuf = __strdup (name);
  835. if (canonbuf == NULL)
  836. {
  837. result = -EAI_MEMORY;
  838. goto out;
  839. }
  840. res->canon = canonbuf;
  841. }
  842. return 0;
  843. }
  844. if ((req->ai_flags & AI_NUMERICHOST))
  845. result = -EAI_NONAME;
  846. out:
  847. res->at = NULL;
  848. return result;
  849. }
  850. /* If possible, call the simple, old functions, which do not support IPv6 scope
  851. ids, nor retrieving the canonical name. */
  852. static int
  853. try_simple_gethostbyname (const char *name, const struct addrinfo *req,
  854. struct scratch_buffer *tmpbuf,
  855. struct gaih_result *res)
  856. {
  857. res->at = NULL;
  858. if (req->ai_family != AF_INET || (req->ai_flags & AI_CANONNAME) != 0)
  859. return 0;
  860. int rc;
  861. struct hostent th;
  862. struct hostent *h;
  863. while (1)
  864. {
  865. rc = __gethostbyname2_r (name, AF_INET, &th, tmpbuf->data,
  866. tmpbuf->length, &h, &h_errno);
  867. if (rc != ERANGE || h_errno != NETDB_INTERNAL)
  868. break;
  869. if (!scratch_buffer_grow (tmpbuf))
  870. return -EAI_MEMORY;
  871. }
  872. if (rc == 0)
  873. {
  874. if (h != NULL)
  875. {
  876. /* We found data, convert it. RES->AT from the conversion will
  877. either be an allocated block or NULL, both of which are safe to
  878. pass to free (). */
  879. if (!convert_hostent_to_gaih_addrtuple (req, AF_INET, h, res))
  880. return -EAI_MEMORY;
  881. res->free_at = true;
  882. return 0;
  883. }
  884. if (h_errno == NO_DATA)
  885. return -EAI_NODATA;
  886. return -EAI_NONAME;
  887. }
  888. if (h_errno == NETDB_INTERNAL)
  889. return -EAI_SYSTEM;
  890. if (h_errno == TRY_AGAIN)
  891. return -EAI_AGAIN;
  892. /* We made requests but they turned out no data.
  893. The name is known, though. */
  894. return -EAI_NODATA;
  895. }
  896. /* Add local address information into RES. RES->AT is assumed to have enough
  897. space for two tuples and is zeroed out. */
  898. static void
  899. get_local_addresses (const struct addrinfo *req, struct gaih_result *res)
  900. {
  901. struct gaih_addrtuple *atr = res->at;
  902. if (req->ai_family == AF_UNSPEC)
  903. res->at->next = res->at + 1;
  904. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET6)
  905. {
  906. res->at->family = AF_INET6;
  907. if ((req->ai_flags & AI_PASSIVE) == 0)
  908. memcpy (res->at->addr, &in6addr_loopback, sizeof (struct in6_addr));
  909. atr = res->at->next;
  910. }
  911. if (req->ai_family == AF_UNSPEC || req->ai_family == AF_INET)
  912. {
  913. atr->family = AF_INET;
  914. if ((req->ai_flags & AI_PASSIVE) == 0)
  915. atr->addr[0] = htonl (INADDR_LOOPBACK);
  916. }
  917. }
  918. /* Generate results in PAI and its count in NADDRS. Return 0 on success or an
  919. error code on failure. */
  920. static int
  921. generate_addrinfo (const struct addrinfo *req, struct gaih_result *res,
  922. const struct gaih_servtuple *st, struct addrinfo **pai,
  923. unsigned int *naddrs)
  924. {
  925. size_t socklen;
  926. sa_family_t family;
  927. /* Buffer is the size of an unformatted IPv6 address in printable format. */
  928. for (struct gaih_addrtuple *at = res->at; at != NULL; at = at->next)
  929. {
  930. family = at->family;
  931. if (family == AF_INET6)
  932. {
  933. socklen = sizeof (struct sockaddr_in6);
  934. /* If we looked up IPv4 mapped address discard them here if
  935. the caller isn't interested in all address and we have
  936. found at least one IPv6 address. */
  937. if (res->got_ipv6
  938. && (req->ai_flags & (AI_V4MAPPED|AI_ALL)) == AI_V4MAPPED
  939. && IN6_IS_ADDR_V4MAPPED (at->addr))
  940. continue;
  941. }
  942. else
  943. socklen = sizeof (struct sockaddr_in);
  944. for (int i = 0; st[i].set; i++)
  945. {
  946. struct addrinfo *ai;
  947. ai = *pai = malloc (sizeof (struct addrinfo) + socklen);
  948. if (ai == NULL)
  949. return -EAI_MEMORY;
  950. ai->ai_flags = req->ai_flags;
  951. ai->ai_family = family;
  952. ai->ai_socktype = st[i].socktype;
  953. ai->ai_protocol = st[i].protocol;
  954. ai->ai_addrlen = socklen;
  955. ai->ai_addr = (void *) (ai + 1);
  956. /* We only add the canonical name once. */
  957. ai->ai_canonname = res->canon;
  958. res->canon = NULL;
  959. #ifdef _HAVE_SA_LEN
  960. ai->ai_addr->sa_len = socklen;
  961. #endif /* _HAVE_SA_LEN */
  962. ai->ai_addr->sa_family = family;
  963. /* In case of an allocation error the list must be NULL
  964. terminated. */
  965. ai->ai_next = NULL;
  966. if (family == AF_INET6)
  967. {
  968. struct sockaddr_in6 *sin6p = (struct sockaddr_in6 *) ai->ai_addr;
  969. sin6p->sin6_port = st[i].port;
  970. sin6p->sin6_flowinfo = 0;
  971. memcpy (&sin6p->sin6_addr, at->addr, sizeof (struct in6_addr));
  972. sin6p->sin6_scope_id = at->scopeid;
  973. }
  974. else
  975. {
  976. struct sockaddr_in *sinp = (struct sockaddr_in *) ai->ai_addr;
  977. sinp->sin_port = st[i].port;
  978. memcpy (&sinp->sin_addr, at->addr, sizeof (struct in_addr));
  979. memset (sinp->sin_zero, '\0', sizeof (sinp->sin_zero));
  980. }
  981. pai = &(ai->ai_next);
  982. }
  983. ++*naddrs;
  984. }
  985. return 0;
  986. }
  987. static int
  988. gaih_inet (const char *name, const struct gaih_service *service,
  989. const struct addrinfo *req, struct addrinfo **pai,
  990. unsigned int *naddrs, struct scratch_buffer *tmpbuf)
  991. {
  992. struct gaih_servtuple st[sizeof (gaih_inet_typeproto)
  993. / sizeof (struct gaih_typeproto)] = { };
  994. const char *orig_name = name;
  995. int rc;
  996. if ((rc = get_servtuples (service, req, st, tmpbuf)) != 0)
  997. return rc;
  998. bool malloc_name = false;
  999. struct gaih_addrtuple *addrmem = NULL;
  1000. int result = 0;
  1001. struct gaih_result res = {};
  1002. struct gaih_addrtuple local_at[2] = {};
  1003. res.at = local_at;
  1004. if (__glibc_unlikely (name == NULL))
  1005. {
  1006. get_local_addresses (req, &res);
  1007. goto process_list;
  1008. }
  1009. if (req->ai_flags & AI_IDN)
  1010. {
  1011. char *out;
  1012. result = __idna_to_dns_encoding (name, &out);
  1013. if (result != 0)
  1014. return -result;
  1015. name = out;
  1016. malloc_name = true;
  1017. }
  1018. if ((result = text_to_binary_address (name, req, &res)) != 0)
  1019. goto free_and_return;
  1020. else if (res.at != NULL)
  1021. goto process_list;
  1022. if ((result = try_simple_gethostbyname (name, req, tmpbuf, &res)) != 0)
  1023. goto free_and_return;
  1024. else if (res.at != NULL)
  1025. goto process_list;
  1026. #ifdef USE_NSCD
  1027. if ((result = get_nscd_addresses (name, req, &res)) != 0)
  1028. goto free_and_return;
  1029. else if (res.at != NULL)
  1030. goto process_list;
  1031. #endif
  1032. if ((result = get_nss_addresses (name, req, tmpbuf, &res)) != 0)
  1033. goto free_and_return;
  1034. else if (res.at != NULL)
  1035. goto process_list;
  1036. /* None of the lookups worked, so name not found. */
  1037. result = -EAI_NONAME;
  1038. goto free_and_return;
  1039. process_list:
  1040. /* Set up the canonical name if we need it. */
  1041. if ((result = process_canonname (req, orig_name, &res)) != 0)
  1042. goto free_and_return;
  1043. result = generate_addrinfo (req, &res, st, pai, naddrs);
  1044. free_and_return:
  1045. if (malloc_name)
  1046. free ((char *) name);
  1047. free (addrmem);
  1048. gaih_result_reset (&res);
  1049. return result;
  1050. }
  1051. struct sort_result
  1052. {
  1053. struct addrinfo *dest_addr;
  1054. /* Using sockaddr_storage is for now overkill. We only support IPv4
  1055. and IPv6 so far. If this changes at some point we can adjust the
  1056. type here. */
  1057. struct sockaddr_in6 source_addr;
  1058. uint8_t source_addr_len;
  1059. bool got_source_addr;
  1060. uint8_t source_addr_flags;
  1061. uint8_t prefixlen;
  1062. uint32_t index;
  1063. int32_t native;
  1064. };
  1065. struct sort_result_combo
  1066. {
  1067. struct sort_result *results;
  1068. int nresults;
  1069. };
  1070. #if __BYTE_ORDER == __BIG_ENDIAN
  1071. # define htonl_c(n) n
  1072. #else
  1073. # define htonl_c(n) __bswap_constant_32 (n)
  1074. #endif
  1075. static const struct scopeentry
  1076. {
  1077. union
  1078. {
  1079. char addr[4];
  1080. uint32_t addr32;
  1081. };
  1082. uint32_t netmask;
  1083. int32_t scope;
  1084. } default_scopes[] =
  1085. {
  1086. /* Link-local addresses: scope 2. */
  1087. { { { 169, 254, 0, 0 } }, htonl_c (0xffff0000), 2 },
  1088. { { { 127, 0, 0, 0 } }, htonl_c (0xff000000), 2 },
  1089. /* Default: scope 14. */
  1090. { { { 0, 0, 0, 0 } }, htonl_c (0x00000000), 14 }
  1091. };
  1092. /* The label table. */
  1093. static const struct scopeentry *scopes;
  1094. static int
  1095. get_scope (const struct sockaddr_in6 *in6)
  1096. {
  1097. int scope;
  1098. if (in6->sin6_family == PF_INET6)
  1099. {
  1100. if (! IN6_IS_ADDR_MULTICAST (&in6->sin6_addr))
  1101. {
  1102. if (IN6_IS_ADDR_LINKLOCAL (&in6->sin6_addr)
  1103. /* RFC 4291 2.5.3 says that the loopback address is to be
  1104. treated like a link-local address. */
  1105. || IN6_IS_ADDR_LOOPBACK (&in6->sin6_addr))
  1106. scope = 2;
  1107. else if (IN6_IS_ADDR_SITELOCAL (&in6->sin6_addr))
  1108. scope = 5;
  1109. else
  1110. /* XXX Is this the correct default behavior? */
  1111. scope = 14;
  1112. }
  1113. else
  1114. scope = in6->sin6_addr.s6_addr[1] & 0xf;
  1115. }
  1116. else if (in6->sin6_family == PF_INET)
  1117. {
  1118. const struct sockaddr_in *in = (const struct sockaddr_in *) in6;
  1119. size_t cnt = 0;
  1120. while (1)
  1121. {
  1122. if ((in->sin_addr.s_addr & scopes[cnt].netmask)
  1123. == scopes[cnt].addr32)
  1124. return scopes[cnt].scope;
  1125. ++cnt;
  1126. }
  1127. /* NOTREACHED */
  1128. }
  1129. else
  1130. /* XXX What is a good default? */
  1131. scope = 15;
  1132. return scope;
  1133. }
  1134. struct prefixentry
  1135. {
  1136. struct in6_addr prefix;
  1137. unsigned int bits;
  1138. int val;
  1139. };
  1140. /* The label table. */
  1141. static const struct prefixentry *labels;
  1142. /* Default labels. */
  1143. static const struct prefixentry default_labels[] =
  1144. {
  1145. /* See RFC 3484 for the details. */
  1146. { { .__in6_u
  1147. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1148. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }
  1149. }, 128, 0 },
  1150. { { .__in6_u
  1151. = { .__u6_addr8 = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1153. }, 16, 2 },
  1154. { { .__in6_u
  1155. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1156. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1157. }, 96, 3 },
  1158. { { .__in6_u
  1159. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1160. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } }
  1161. }, 96, 4 },
  1162. /* The next two entries differ from RFC 3484. We need to treat
  1163. IPv6 site-local addresses special because they are never NATed,
  1164. unlike site-locale IPv4 addresses. If this would not happen, on
  1165. machines which have only IPv4 and IPv6 site-local addresses, the
  1166. sorting would prefer the IPv6 site-local addresses, causing
  1167. unnecessary delays when trying to connect to a global IPv6 address
  1168. through a site-local IPv6 address. */
  1169. { { .__in6_u
  1170. = { .__u6_addr8 = { 0xfe, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1171. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1172. }, 10, 5 },
  1173. { { .__in6_u
  1174. = { .__u6_addr8 = { 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1176. }, 7, 6 },
  1177. /* Additional rule for Teredo tunnels. */
  1178. { { .__in6_u
  1179. = { .__u6_addr8 = { 0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1180. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1181. }, 32, 7 },
  1182. { { .__in6_u
  1183. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1185. }, 0, 1 }
  1186. };
  1187. /* The precedence table. */
  1188. static const struct prefixentry *precedence;
  1189. /* The default precedences. */
  1190. static const struct prefixentry default_precedence[] =
  1191. {
  1192. /* See RFC 3484 for the details. */
  1193. { { .__in6_u
  1194. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1195. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } }
  1196. }, 128, 50 },
  1197. { { .__in6_u
  1198. = { .__u6_addr8 = { 0x20, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1200. }, 16, 30 },
  1201. { { .__in6_u
  1202. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1204. }, 96, 20 },
  1205. { { .__in6_u
  1206. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1207. 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 } }
  1208. }, 96, 10 },
  1209. { { .__in6_u
  1210. = { .__u6_addr8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  1211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } }
  1212. }, 0, 40 }
  1213. };
  1214. static int
  1215. match_prefix (const struct sockaddr_in6 *in6,
  1216. const struct prefixentry *list, int default_val)
  1217. {
  1218. int idx;
  1219. struct sockaddr_in6 in6_mem;
  1220. if (in6->sin6_family == PF_INET)
  1221. {
  1222. const struct sockaddr_in *in = (const struct sockaddr_in *) in6;
  1223. /* Construct a V4-to-6 mapped address. */
  1224. in6_mem.sin6_family = PF_INET6;
  1225. in6_mem.sin6_port = in->sin_port;
  1226. in6_mem.sin6_flowinfo = 0;
  1227. memset (&in6_mem.sin6_addr, '\0', sizeof (in6_mem.sin6_addr));
  1228. in6_mem.sin6_addr.s6_addr16[5] = 0xffff;
  1229. in6_mem.sin6_addr.s6_addr32[3] = in->sin_addr.s_addr;
  1230. in6_mem.sin6_scope_id = 0;
  1231. in6 = &in6_mem;
  1232. }
  1233. else if (in6->sin6_family != PF_INET6)
  1234. return default_val;
  1235. for (idx = 0; ; ++idx)
  1236. {
  1237. unsigned int bits = list[idx].bits;
  1238. const uint8_t *mask = list[idx].prefix.s6_addr;
  1239. const uint8_t *val = in6->sin6_addr.s6_addr;
  1240. while (bits >= 8)
  1241. {
  1242. if (*mask != *val)
  1243. break;
  1244. ++mask;
  1245. ++val;
  1246. bits -= 8;
  1247. }
  1248. if (bits < 8)
  1249. {
  1250. if ((*mask & (0xff00 >> bits)) == (*val & (0xff00 >> bits)))
  1251. /* Match! */
  1252. break;
  1253. }
  1254. }
  1255. return list[idx].val;
  1256. }
  1257. static int
  1258. get_label (const struct sockaddr_in6 *in6)
  1259. {
  1260. /* XXX What is a good default value? */
  1261. return match_prefix (in6, labels, INT_MAX);
  1262. }
  1263. static int
  1264. get_precedence (const struct sockaddr_in6 *in6)
  1265. {
  1266. /* XXX What is a good default value? */
  1267. return match_prefix (in6, precedence, 0);
  1268. }
  1269. static int
  1270. rfc3484_sort (const void *p1, const void *p2, void *arg)
  1271. {
  1272. const size_t idx1 = *(const size_t *) p1;
  1273. const size_t idx2 = *(const size_t *) p2;
  1274. struct sort_result_combo *src = (struct sort_result_combo *) arg;
  1275. struct sort_result *a1 = &src->results[idx1];
  1276. struct sort_result *a2 = &src->results[idx2];
  1277. /* Rule 1: Avoid unusable destinations.
  1278. We have the got_source_addr flag set if the destination is reachable. */
  1279. if (a1->got_source_addr && ! a2->got_source_addr)
  1280. return -1;
  1281. if (! a1->got_source_addr && a2->got_source_addr)
  1282. return 1;
  1283. /* Rule 2: Prefer matching scope. Only interesting if both
  1284. destination addresses are IPv6. */
  1285. int a1_dst_scope
  1286. = get_scope ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
  1287. int a2_dst_scope
  1288. = get_scope ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
  1289. if (a1->got_source_addr)
  1290. {
  1291. int a1_src_scope = get_scope (&a1->source_addr);
  1292. int a2_src_scope = get_scope (&a2->source_addr);
  1293. if (a1_dst_scope == a1_src_scope && a2_dst_scope != a2_src_scope)
  1294. return -1;
  1295. if (a1_dst_scope != a1_src_scope && a2_dst_scope == a2_src_scope)
  1296. return 1;
  1297. }
  1298. /* Rule 3: Avoid deprecated addresses. */
  1299. if (a1->got_source_addr)
  1300. {
  1301. if (!(a1->source_addr_flags & in6ai_deprecated)
  1302. && (a2->source_addr_flags & in6ai_deprecated))
  1303. return -1;
  1304. if ((a1->source_addr_flags & in6ai_deprecated)
  1305. && !(a2->source_addr_flags & in6ai_deprecated))
  1306. return 1;
  1307. }
  1308. /* Rule 4: Prefer home addresses. */
  1309. if (a1->got_source_addr)
  1310. {
  1311. if (!(a1->source_addr_flags & in6ai_homeaddress)
  1312. && (a2->source_addr_flags & in6ai_homeaddress))
  1313. return 1;
  1314. if ((a1->source_addr_flags & in6ai_homeaddress)
  1315. && !(a2->source_addr_flags & in6ai_homeaddress))
  1316. return -1;
  1317. }
  1318. /* Rule 5: Prefer matching label. */
  1319. if (a1->got_source_addr)
  1320. {
  1321. int a1_dst_label
  1322. = get_label ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
  1323. int a1_src_label = get_label (&a1->source_addr);
  1324. int a2_dst_label
  1325. = get_label ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
  1326. int a2_src_label = get_label (&a2->source_addr);
  1327. if (a1_dst_label == a1_src_label && a2_dst_label != a2_src_label)
  1328. return -1;
  1329. if (a1_dst_label != a1_src_label && a2_dst_label == a2_src_label)
  1330. return 1;
  1331. }
  1332. /* Rule 6: Prefer higher precedence. */
  1333. int a1_prec
  1334. = get_precedence ((struct sockaddr_in6 *) a1->dest_addr->ai_addr);
  1335. int a2_prec
  1336. = get_precedence ((struct sockaddr_in6 *) a2->dest_addr->ai_addr);
  1337. if (a1_prec > a2_prec)
  1338. return -1;
  1339. if (a1_prec < a2_prec)
  1340. return 1;
  1341. /* Rule 7: Prefer native transport. */
  1342. if (a1->got_source_addr)
  1343. {
  1344. /* The same interface index means the same interface which means
  1345. there is no difference in transport. This should catch many
  1346. (most?) cases. */
  1347. if (a1->index != a2->index)
  1348. {
  1349. int a1_native = a1->native;
  1350. int a2_native = a2->native;
  1351. if (a1_native == -1 || a2_native == -1)
  1352. {
  1353. uint32_t a1_index;
  1354. if (a1_native == -1)
  1355. {
  1356. /* If we do not have the information use 'native' as
  1357. the default. */
  1358. a1_native = 0;
  1359. a1_index = a1->index;
  1360. }
  1361. else
  1362. a1_index = 0xffffffffu;
  1363. uint32_t a2_index;
  1364. if (a2_native == -1)
  1365. {
  1366. /* If we do not have the information use 'native' as
  1367. the default. */
  1368. a2_native = 0;
  1369. a2_index = a2->index;
  1370. }
  1371. else
  1372. a2_index = 0xffffffffu;
  1373. __check_native (a1_index, &a1_native, a2_index, &a2_native);
  1374. /* Fill in the results in all the records. */
  1375. for (int i = 0; i < src->nresults; ++i)
  1376. if (a1_index != -1 && src->results[i].index == a1_index)
  1377. {
  1378. assert (src->results[i].native == -1
  1379. || src->results[i].native == a1_native);
  1380. src->results[i].native = a1_native;
  1381. }
  1382. else if (a2_index != -1 && src->results[i].index == a2_index)
  1383. {
  1384. assert (src->results[i].native == -1
  1385. || src->results[i].native == a2_native);
  1386. src->results[i].native = a2_native;
  1387. }
  1388. }
  1389. if (a1_native && !a2_native)
  1390. return -1;
  1391. if (!a1_native && a2_native)
  1392. return 1;
  1393. }
  1394. }
  1395. /* Rule 8: Prefer smaller scope. */
  1396. if (a1_dst_scope < a2_dst_scope)
  1397. return -1;
  1398. if (a1_dst_scope > a2_dst_scope)
  1399. return 1;
  1400. /* Rule 9: Use longest matching prefix. */
  1401. if (a1->got_source_addr
  1402. && a1->dest_addr->ai_family == a2->dest_addr->ai_family)
  1403. {
  1404. int bit1 = 0;
  1405. int bit2 = 0;
  1406. if (a1->dest_addr->ai_family == PF_INET)
  1407. {
  1408. assert (a1->source_addr.sin6_family == PF_INET);
  1409. assert (a2->source_addr.sin6_family == PF_INET);
  1410. /* Outside of subnets, as defined by the network masks,
  1411. common address prefixes for IPv4 addresses make no sense.
  1412. So, define a non-zero value only if source and
  1413. destination address are on the same subnet. */
  1414. struct sockaddr_in *in1_dst
  1415. = (struct sockaddr_in *) a1->dest_addr->ai_addr;
  1416. in_addr_t in1_dst_addr = ntohl (in1_dst->sin_addr.s_addr);
  1417. struct sockaddr_in *in1_src
  1418. = (struct sockaddr_in *) &a1->source_addr;
  1419. in_addr_t in1_src_addr = ntohl (in1_src->sin_addr.s_addr);
  1420. in_addr_t netmask1 = 0xffffffffu << (32 - a1->prefixlen);
  1421. if ((in1_src_addr & netmask1) == (in1_dst_addr & netmask1))
  1422. bit1 = stdc_leading_zeros (in1_dst_addr ^ in1_src_addr);
  1423. struct sockaddr_in *in2_dst
  1424. = (struct sockaddr_in *) a2->dest_addr->ai_addr;
  1425. in_addr_t in2_dst_addr = ntohl (in2_dst->sin_addr.s_addr);
  1426. struct sockaddr_in *in2_src
  1427. = (struct sockaddr_in *) &a2->source_addr;
  1428. in_addr_t in2_src_addr = ntohl (in2_src->sin_addr.s_addr);
  1429. in_addr_t netmask2 = 0xffffffffu << (32 - a2->prefixlen);
  1430. if ((in2_src_addr & netmask2) == (in2_dst_addr & netmask2))
  1431. bit2 = stdc_leading_zeros (in2_dst_addr ^ in2_src_addr);
  1432. }
  1433. else if (a1->dest_addr->ai_family == PF_INET6)
  1434. {
  1435. assert (a1->source_addr.sin6_family == PF_INET6);
  1436. assert (a2->source_addr.sin6_family == PF_INET6);
  1437. struct sockaddr_in6 *in1_dst;
  1438. struct sockaddr_in6 *in1_src;
  1439. struct sockaddr_in6 *in2_dst;
  1440. struct sockaddr_in6 *in2_src;
  1441. in1_dst = (struct sockaddr_in6 *) a1->dest_addr->ai_addr;
  1442. in1_src = (struct sockaddr_in6 *) &a1->source_addr;
  1443. in2_dst = (struct sockaddr_in6 *) a2->dest_addr->ai_addr;
  1444. in2_src = (struct sockaddr_in6 *) &a2->source_addr;
  1445. int i;
  1446. for (i = 0; i < 4; ++i)
  1447. if (in1_dst->sin6_addr.s6_addr32[i]
  1448. != in1_src->sin6_addr.s6_addr32[i]
  1449. || (in2_dst->sin6_addr.s6_addr32[i]
  1450. != in2_src->sin6_addr.s6_addr32[i]))
  1451. break;
  1452. if (i < 4)
  1453. {
  1454. uint32_t set_bits1 = (in1_dst->sin6_addr.s6_addr32[i]
  1455. ^ in1_src->sin6_addr.s6_addr32[i]);
  1456. uint32_t set_bits2 = (in2_dst->sin6_addr.s6_addr32[i]
  1457. ^ in2_src->sin6_addr.s6_addr32[i]);
  1458. bit1 = stdc_leading_zeros (ntohl (set_bits1));
  1459. bit2 = stdc_leading_zeros (ntohl (set_bits2));
  1460. }
  1461. }
  1462. if (bit1 > bit2)
  1463. return -1;
  1464. if (bit1 < bit2)
  1465. return 1;
  1466. }
  1467. /* Rule 10: Otherwise, leave the order unchanged. To ensure this
  1468. compare with the value indicating the order in which the entries
  1469. have been received from the services. NB: no two entries can have
  1470. the same order so the test will never return zero. */
  1471. return idx1 < idx2 ? -1 : 1;
  1472. }
  1473. static int
  1474. in6aicmp (const void *p1, const void *p2)
  1475. {
  1476. struct in6addrinfo *a1 = (struct in6addrinfo *) p1;
  1477. struct in6addrinfo *a2 = (struct in6addrinfo *) p2;
  1478. return memcmp (a1->addr, a2->addr, sizeof (a1->addr));
  1479. }
  1480. /* Name of the config file for RFC 3484 sorting (for now). */
  1481. #define GAICONF_FNAME "/etc/gai.conf"
  1482. /* Non-zero if we are supposed to reload the config file automatically
  1483. whenever it changed. */
  1484. static int gaiconf_reload_flag;
  1485. /* Non-zero if gaiconf_reload_flag was ever set to true. */
  1486. static int gaiconf_reload_flag_ever_set;
  1487. /* Last modification time. */
  1488. #ifdef _STATBUF_ST_NSEC
  1489. static struct __timespec64 gaiconf_mtime;
  1490. static inline void
  1491. save_gaiconf_mtime (const struct __stat64_t64 *st)
  1492. {
  1493. gaiconf_mtime = (struct __timespec64) { st->st_mtim.tv_sec,
  1494. st->st_mtim.tv_nsec };
  1495. }
  1496. static inline bool
  1497. check_gaiconf_mtime (const struct __stat64_t64 *st)
  1498. {
  1499. return (st->st_mtim.tv_sec == gaiconf_mtime.tv_sec
  1500. && st->st_mtim.tv_nsec == gaiconf_mtime.tv_nsec);
  1501. }
  1502. #else
  1503. static time_t gaiconf_mtime;
  1504. static inline void
  1505. save_gaiconf_mtime (const struct __stat64_t64 *st)
  1506. {
  1507. gaiconf_mtime = st->st_mtime;
  1508. }
  1509. static inline bool
  1510. check_gaiconf_mtime (const struct __stat64_t64 *st)
  1511. {
  1512. return st->st_mtime == gaiconf_mtime;
  1513. }
  1514. #endif
  1515. void
  1516. __libc_getaddrinfo_freemem (void)
  1517. {
  1518. if (labels != default_labels)
  1519. {
  1520. const struct prefixentry *old = labels;
  1521. labels = default_labels;
  1522. free ((void *) old);
  1523. }
  1524. if (precedence != default_precedence)
  1525. {
  1526. const struct prefixentry *old = precedence;
  1527. precedence = default_precedence;
  1528. free ((void *) old);
  1529. }
  1530. if (scopes != default_scopes)
  1531. {
  1532. const struct scopeentry *old = scopes;
  1533. scopes = default_scopes;
  1534. free ((void *) old);
  1535. }
  1536. }
  1537. struct prefixlist
  1538. {
  1539. struct prefixentry entry;
  1540. struct prefixlist *next;
  1541. };
  1542. struct scopelist
  1543. {
  1544. struct scopeentry entry;
  1545. struct scopelist *next;
  1546. };
  1547. static void
  1548. free_prefixlist (struct prefixlist *list)
  1549. {
  1550. while (list != NULL)
  1551. {
  1552. struct prefixlist *oldp = list;
  1553. list = list->next;
  1554. free (oldp);
  1555. }
  1556. }
  1557. static void
  1558. free_scopelist (struct scopelist *list)
  1559. {
  1560. while (list != NULL)
  1561. {
  1562. struct scopelist *oldp = list;
  1563. list = list->next;
  1564. free (oldp);
  1565. }
  1566. }
  1567. static int
  1568. prefixcmp (const void *p1, const void *p2)
  1569. {
  1570. const struct prefixentry *e1 = (const struct prefixentry *) p1;
  1571. const struct prefixentry *e2 = (const struct prefixentry *) p2;
  1572. if (e1->bits < e2->bits)
  1573. return 1;
  1574. if (e1->bits == e2->bits)
  1575. return 0;
  1576. return -1;
  1577. }
  1578. static int
  1579. scopecmp (const void *p1, const void *p2)
  1580. {
  1581. const struct scopeentry *e1 = (const struct scopeentry *) p1;
  1582. const struct scopeentry *e2 = (const struct scopeentry *) p2;
  1583. if (e1->netmask > e2->netmask)
  1584. return -1;
  1585. if (e1->netmask == e2->netmask)
  1586. return 0;
  1587. return 1;
  1588. }
  1589. /* Return true if PTR points to a valid decimal value string and
  1590. store the value in *VALUE_P. Otherwise, return false. */
  1591. static bool
  1592. valid_decimal_value (const char *str, unsigned long int *value_p)
  1593. {
  1594. char *endp;
  1595. unsigned long int value = strtoul (str, &endp, 10);
  1596. if (str == endp
  1597. || *endp != '\0'
  1598. || (value == ULONG_MAX && errno == ERANGE))
  1599. return false;
  1600. *value_p = value;
  1601. return true;
  1602. }
  1603. static bool
  1604. add_prefixlist (struct prefixlist **listp, size_t *lenp, bool *nullbitsp,
  1605. char *val1, char *val2, char **pos)
  1606. {
  1607. struct in6_addr prefix;
  1608. unsigned long int bits;
  1609. unsigned long int val;
  1610. bits = 128;
  1611. __set_errno (0);
  1612. char *cp = strchr (val1, '/');
  1613. if (cp != NULL)
  1614. *cp++ = '\0';
  1615. *pos = cp;
  1616. if (inet_pton (AF_INET6, val1, &prefix)
  1617. && (cp == NULL || valid_decimal_value (cp, &bits))
  1618. && bits <= 128
  1619. && valid_decimal_value (val2, &val)
  1620. && val <= INT_MAX)
  1621. {
  1622. struct prefixlist *newp = malloc (sizeof (*newp));
  1623. if (newp == NULL)
  1624. return false;
  1625. memcpy (&newp->entry.prefix, &prefix, sizeof (prefix));
  1626. newp->entry.bits = bits;
  1627. newp->entry.val = val;
  1628. newp->next = *listp;
  1629. *listp = newp;
  1630. ++*lenp;
  1631. *nullbitsp |= bits == 0;
  1632. }
  1633. return true;
  1634. }
  1635. static bool
  1636. add_scopelist (struct scopelist **listp, size_t *lenp, bool *nullbitsp,
  1637. const struct in6_addr *prefixp, unsigned long int bits,
  1638. unsigned long int val)
  1639. {
  1640. struct scopelist *newp = malloc (sizeof (*newp));
  1641. if (newp == NULL)
  1642. return false;
  1643. newp->entry.netmask = htonl (bits != 96 ? (0xffffffff << (128 - bits)) : 0);
  1644. newp->entry.addr32 = (prefixp->s6_addr32[3] & newp->entry.netmask);
  1645. newp->entry.scope = val;
  1646. newp->next = *listp;
  1647. *listp = newp;
  1648. ++*lenp;
  1649. *nullbitsp |= bits == 96;
  1650. return true;
  1651. }
  1652. static void
  1653. gaiconf_init (void)
  1654. {
  1655. struct prefixlist *labellist = NULL;
  1656. size_t nlabellist = 0;
  1657. bool labellist_nullbits = false;
  1658. struct prefixlist *precedencelist = NULL;
  1659. size_t nprecedencelist = 0;
  1660. bool precedencelist_nullbits = false;
  1661. struct scopelist *scopelist = NULL;
  1662. size_t nscopelist = 0;
  1663. bool scopelist_nullbits = false;
  1664. FILE *fp = fopen (GAICONF_FNAME, "rce");
  1665. if (fp == NULL)
  1666. goto no_file;
  1667. struct __stat64_t64 st;
  1668. if (__fstat64_time64 (fileno (fp), &st) != 0)
  1669. {
  1670. fclose (fp);
  1671. goto no_file;
  1672. }
  1673. char *line = NULL;
  1674. size_t linelen = 0;
  1675. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  1676. while (!feof_unlocked (fp))
  1677. {
  1678. ssize_t n = __getline (&line, &linelen, fp);
  1679. if (n <= 0)
  1680. break;
  1681. /* Handle comments. No escaping possible so this is easy. */
  1682. char *cp = strchr (line, '#');
  1683. if (cp != NULL)
  1684. *cp = '\0';
  1685. cp = line;
  1686. while (isspace (*cp))
  1687. ++cp;
  1688. char *cmd = cp;
  1689. while (*cp != '\0' && !isspace (*cp))
  1690. ++cp;
  1691. size_t cmdlen = cp - cmd;
  1692. if (*cp != '\0')
  1693. *cp++ = '\0';
  1694. while (isspace (*cp))
  1695. ++cp;
  1696. char *val1 = cp;
  1697. while (*cp != '\0' && !isspace (*cp))
  1698. ++cp;
  1699. size_t val1len = cp - cmd;
  1700. /* We always need at least two values. */
  1701. if (val1len == 0)
  1702. continue;
  1703. if (*cp != '\0')
  1704. *cp++ = '\0';
  1705. while (isspace (*cp))
  1706. ++cp;
  1707. char *val2 = cp;
  1708. while (*cp != '\0' && !isspace (*cp))
  1709. ++cp;
  1710. /* Ignore the rest of the line. */
  1711. *cp = '\0';
  1712. switch (cmdlen)
  1713. {
  1714. case 5:
  1715. if (strcmp (cmd, "label") == 0)
  1716. {
  1717. if (!add_prefixlist (&labellist, &nlabellist,
  1718. &labellist_nullbits, val1, val2, &cp))
  1719. {
  1720. free (line);
  1721. fclose (fp);
  1722. goto no_file;
  1723. }
  1724. }
  1725. break;
  1726. case 6:
  1727. if (strcmp (cmd, "reload") == 0)
  1728. {
  1729. gaiconf_reload_flag = strcmp (val1, "yes") == 0;
  1730. if (gaiconf_reload_flag)
  1731. gaiconf_reload_flag_ever_set = 1;
  1732. }
  1733. break;
  1734. case 7:
  1735. if (strcmp (cmd, "scopev4") == 0)
  1736. {
  1737. struct in6_addr prefix;
  1738. unsigned long int bits;
  1739. unsigned long int val;
  1740. bits = 32;
  1741. __set_errno (0);
  1742. cp = strchr (val1, '/');
  1743. if (cp != NULL)
  1744. *cp++ = '\0';
  1745. if (inet_pton (AF_INET6, val1, &prefix))
  1746. {
  1747. bits = 128;
  1748. if (IN6_IS_ADDR_V4MAPPED (&prefix)
  1749. && (cp == NULL || valid_decimal_value (cp, &bits))
  1750. && bits >= 96
  1751. && bits <= 128
  1752. && valid_decimal_value (val2, &val)
  1753. && val <= INT_MAX)
  1754. {
  1755. if (!add_scopelist (&scopelist, &nscopelist,
  1756. &scopelist_nullbits, &prefix,
  1757. bits, val))
  1758. {
  1759. free (line);
  1760. fclose (fp);
  1761. goto no_file;
  1762. }
  1763. }
  1764. }
  1765. else if (inet_pton (AF_INET, val1, &prefix.s6_addr32[3])
  1766. && (cp == NULL || valid_decimal_value (cp, &bits))
  1767. && bits <= 32
  1768. && valid_decimal_value (val2, &val)
  1769. && val <= INT_MAX)
  1770. {
  1771. if (!add_scopelist (&scopelist, &nscopelist,
  1772. &scopelist_nullbits, &prefix,
  1773. bits + 96, val))
  1774. {
  1775. free (line);
  1776. fclose (fp);
  1777. goto no_file;
  1778. }
  1779. }
  1780. }
  1781. break;
  1782. case 10:
  1783. if (strcmp (cmd, "precedence") == 0)
  1784. {
  1785. if (!add_prefixlist (&precedencelist, &nprecedencelist,
  1786. &precedencelist_nullbits, val1, val2,
  1787. &cp))
  1788. {
  1789. free (line);
  1790. fclose (fp);
  1791. goto no_file;
  1792. }
  1793. }
  1794. break;
  1795. }
  1796. }
  1797. free (line);
  1798. fclose (fp);
  1799. /* Create the array for the labels. */
  1800. struct prefixentry *new_labels;
  1801. if (nlabellist > 0)
  1802. {
  1803. if (!labellist_nullbits)
  1804. ++nlabellist;
  1805. new_labels = malloc (nlabellist * sizeof (*new_labels));
  1806. if (new_labels == NULL)
  1807. goto no_file;
  1808. int i = nlabellist;
  1809. if (!labellist_nullbits)
  1810. {
  1811. --i;
  1812. memset (&new_labels[i].prefix, '\0', sizeof (struct in6_addr));
  1813. new_labels[i].bits = 0;
  1814. new_labels[i].val = 1;
  1815. }
  1816. struct prefixlist *l = labellist;
  1817. while (i-- > 0)
  1818. {
  1819. new_labels[i] = l->entry;
  1820. l = l->next;
  1821. }
  1822. free_prefixlist (labellist);
  1823. labellist = NULL;
  1824. /* Sort the entries so that the most specific ones are at
  1825. the beginning. */
  1826. qsort (new_labels, nlabellist, sizeof (*new_labels), prefixcmp);
  1827. }
  1828. else
  1829. new_labels = (struct prefixentry *) default_labels;
  1830. struct prefixentry *new_precedence;
  1831. if (nprecedencelist > 0)
  1832. {
  1833. if (!precedencelist_nullbits)
  1834. ++nprecedencelist;
  1835. new_precedence = malloc (nprecedencelist * sizeof (*new_precedence));
  1836. if (new_precedence == NULL)
  1837. {
  1838. if (new_labels != default_labels)
  1839. free (new_labels);
  1840. goto no_file;
  1841. }
  1842. int i = nprecedencelist;
  1843. if (!precedencelist_nullbits)
  1844. {
  1845. --i;
  1846. memset (&new_precedence[i].prefix, '\0',
  1847. sizeof (struct in6_addr));
  1848. new_precedence[i].bits = 0;
  1849. new_precedence[i].val = 40;
  1850. }
  1851. struct prefixlist *l = precedencelist;
  1852. while (i-- > 0)
  1853. {
  1854. new_precedence[i] = l->entry;
  1855. l = l->next;
  1856. }
  1857. free_prefixlist (precedencelist);
  1858. precedencelist = NULL;
  1859. /* Sort the entries so that the most specific ones are at
  1860. the beginning. */
  1861. qsort (new_precedence, nprecedencelist, sizeof (*new_precedence),
  1862. prefixcmp);
  1863. }
  1864. else
  1865. new_precedence = (struct prefixentry *) default_precedence;
  1866. struct scopeentry *new_scopes;
  1867. if (nscopelist > 0)
  1868. {
  1869. if (!scopelist_nullbits)
  1870. ++nscopelist;
  1871. new_scopes = malloc (nscopelist * sizeof (*new_scopes));
  1872. if (new_scopes == NULL)
  1873. {
  1874. if (new_labels != default_labels)
  1875. free (new_labels);
  1876. if (new_precedence != default_precedence)
  1877. free (new_precedence);
  1878. goto no_file;
  1879. }
  1880. int i = nscopelist;
  1881. if (!scopelist_nullbits)
  1882. {
  1883. --i;
  1884. new_scopes[i].addr32 = 0;
  1885. new_scopes[i].netmask = 0;
  1886. new_scopes[i].scope = 14;
  1887. }
  1888. struct scopelist *l = scopelist;
  1889. while (i-- > 0)
  1890. {
  1891. new_scopes[i] = l->entry;
  1892. l = l->next;
  1893. }
  1894. free_scopelist (scopelist);
  1895. /* Sort the entries so that the most specific ones are at
  1896. the beginning. */
  1897. qsort (new_scopes, nscopelist, sizeof (*new_scopes),
  1898. scopecmp);
  1899. }
  1900. else
  1901. new_scopes = (struct scopeentry *) default_scopes;
  1902. /* Now we are ready to replace the values. */
  1903. const struct prefixentry *old = labels;
  1904. labels = new_labels;
  1905. if (old != default_labels)
  1906. free ((void *) old);
  1907. old = precedence;
  1908. precedence = new_precedence;
  1909. if (old != default_precedence)
  1910. free ((void *) old);
  1911. const struct scopeentry *oldscope = scopes;
  1912. scopes = new_scopes;
  1913. if (oldscope != default_scopes)
  1914. free ((void *) oldscope);
  1915. save_gaiconf_mtime (&st);
  1916. return;
  1917. no_file:
  1918. free_prefixlist (labellist);
  1919. free_prefixlist (precedencelist);
  1920. free_scopelist (scopelist);
  1921. /* If we previously read the file but it is gone now, free the old data and
  1922. use the builtin one. Leave the reload flag alone. */
  1923. __libc_getaddrinfo_freemem ();
  1924. }
  1925. static void
  1926. gaiconf_reload (void)
  1927. {
  1928. struct __stat64_t64 st;
  1929. if (__stat64_time64 (GAICONF_FNAME, &st) != 0
  1930. || !check_gaiconf_mtime (&st))
  1931. gaiconf_init ();
  1932. }
  1933. static bool
  1934. try_connect (int *fdp, int *afp, struct sockaddr_in6 *source_addrp,
  1935. const struct sockaddr *addr, socklen_t addrlen, int family)
  1936. {
  1937. int fd = *fdp;
  1938. int af = *afp;
  1939. socklen_t sl = sizeof (*source_addrp);
  1940. while (true)
  1941. {
  1942. if (fd != -1 && __connect (fd, addr, addrlen) == 0
  1943. && __getsockname (fd, (struct sockaddr *) source_addrp, &sl) == 0)
  1944. return true;
  1945. if (errno == EAFNOSUPPORT && af == AF_INET6 && family == AF_INET)
  1946. {
  1947. /* This could mean IPv6 sockets are IPv6-only. */
  1948. if (fd != -1)
  1949. __close_nocancel_nostatus (fd);
  1950. *afp = af = AF_INET;
  1951. *fdp = fd = __socket (AF_INET, SOCK_DGRAM | SOCK_CLOEXEC,
  1952. IPPROTO_IP);
  1953. continue;
  1954. }
  1955. return false;
  1956. }
  1957. __builtin_unreachable ();
  1958. }
  1959. int
  1960. getaddrinfo (const char *name, const char *service,
  1961. const struct addrinfo *hints, struct addrinfo **pai)
  1962. {
  1963. int i = 0, last_i = 0;
  1964. int nresults = 0;
  1965. struct addrinfo *p = NULL;
  1966. struct gaih_service gaih_service, *pservice;
  1967. struct addrinfo local_hints;
  1968. if (name != NULL && name[0] == '*' && name[1] == 0)
  1969. name = NULL;
  1970. if (service != NULL && service[0] == '*' && service[1] == 0)
  1971. service = NULL;
  1972. if (name == NULL && service == NULL)
  1973. return EAI_NONAME;
  1974. if (hints == NULL)
  1975. hints = &default_hints;
  1976. if (hints->ai_flags
  1977. & ~(AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST|AI_ADDRCONFIG|AI_V4MAPPED
  1978. |AI_IDN|AI_CANONIDN|DEPRECATED_AI_IDN
  1979. |AI_NUMERICSERV|AI_ALL))
  1980. return EAI_BADFLAGS;
  1981. if ((hints->ai_flags & AI_CANONNAME) && name == NULL)
  1982. return EAI_BADFLAGS;
  1983. if (hints->ai_family != AF_UNSPEC && hints->ai_family != AF_INET
  1984. && hints->ai_family != AF_INET6)
  1985. return EAI_FAMILY;
  1986. struct in6addrinfo *in6ai = NULL;
  1987. size_t in6ailen = 0;
  1988. bool seen_ipv4 = false;
  1989. bool seen_ipv6 = false;
  1990. bool check_pf_called = false;
  1991. if (hints->ai_flags & AI_ADDRCONFIG)
  1992. {
  1993. /* We might need information about what interfaces are available.
  1994. Also determine whether we have IPv4 or IPv6 interfaces or both. We
  1995. cannot cache the results since new interfaces could be added at
  1996. any time. */
  1997. __check_pf (&seen_ipv4, &seen_ipv6, &in6ai, &in6ailen);
  1998. check_pf_called = true;
  1999. /* Now make a decision on what we return, if anything. */
  2000. if (hints->ai_family == PF_UNSPEC && (seen_ipv4 || seen_ipv6))
  2001. {
  2002. /* If we haven't seen both IPv4 and IPv6 interfaces we can
  2003. narrow down the search. */
  2004. if (seen_ipv4 != seen_ipv6)
  2005. {
  2006. local_hints = *hints;
  2007. local_hints.ai_family = seen_ipv4 ? PF_INET : PF_INET6;
  2008. hints = &local_hints;
  2009. }
  2010. }
  2011. else if ((hints->ai_family == PF_INET && ! seen_ipv4)
  2012. || (hints->ai_family == PF_INET6 && ! seen_ipv6))
  2013. {
  2014. /* We cannot possibly return a valid answer. */
  2015. __free_in6ai (in6ai);
  2016. return EAI_NONAME;
  2017. }
  2018. }
  2019. if (service && service[0])
  2020. {
  2021. char *c;
  2022. gaih_service.name = service;
  2023. gaih_service.num = strtoul (gaih_service.name, &c, 10);
  2024. if (*c != '\0')
  2025. {
  2026. if (hints->ai_flags & AI_NUMERICSERV)
  2027. {
  2028. __free_in6ai (in6ai);
  2029. return EAI_NONAME;
  2030. }
  2031. gaih_service.num = -1;
  2032. }
  2033. pservice = &gaih_service;
  2034. }
  2035. else
  2036. pservice = NULL;
  2037. struct addrinfo **end = &p;
  2038. unsigned int naddrs = 0;
  2039. struct scratch_buffer tmpbuf;
  2040. scratch_buffer_init (&tmpbuf);
  2041. last_i = gaih_inet (name, pservice, hints, end, &naddrs, &tmpbuf);
  2042. scratch_buffer_free (&tmpbuf);
  2043. if (last_i != 0)
  2044. {
  2045. freeaddrinfo (p);
  2046. __free_in6ai (in6ai);
  2047. return -last_i;
  2048. }
  2049. while (*end)
  2050. {
  2051. end = &((*end)->ai_next);
  2052. ++nresults;
  2053. }
  2054. if (naddrs > 1)
  2055. {
  2056. /* Read the config file. */
  2057. __libc_once_define (static, once);
  2058. __typeof (once) old_once = once;
  2059. __libc_once (once, gaiconf_init);
  2060. /* Sort results according to RFC 3484. */
  2061. struct sort_result *results;
  2062. size_t *order;
  2063. struct addrinfo *q;
  2064. struct addrinfo *last = NULL;
  2065. char *canonname = NULL;
  2066. struct scratch_buffer buf;
  2067. scratch_buffer_init (&buf);
  2068. if (!scratch_buffer_set_array_size (&buf, nresults,
  2069. sizeof (*results) + sizeof (size_t)))
  2070. {
  2071. __free_in6ai (in6ai);
  2072. return EAI_MEMORY;
  2073. }
  2074. results = buf.data;
  2075. order = (size_t *) (results + nresults);
  2076. /* Now we definitely need the interface information. */
  2077. if (! check_pf_called)
  2078. __check_pf (&seen_ipv4, &seen_ipv6, &in6ai, &in6ailen);
  2079. /* If we have information about deprecated and temporary addresses
  2080. sort the array now. */
  2081. if (in6ai != NULL)
  2082. qsort (in6ai, in6ailen, sizeof (*in6ai), in6aicmp);
  2083. int fd = -1;
  2084. int af = AF_UNSPEC;
  2085. for (i = 0, q = p; q != NULL; ++i, last = q, q = q->ai_next)
  2086. {
  2087. results[i].dest_addr = q;
  2088. results[i].native = -1;
  2089. order[i] = i;
  2090. /* If we just looked up the address for a different
  2091. protocol, reuse the result. */
  2092. if (last != NULL && last->ai_addrlen == q->ai_addrlen
  2093. && memcmp (last->ai_addr, q->ai_addr, q->ai_addrlen) == 0)
  2094. {
  2095. memcpy (&results[i].source_addr, &results[i - 1].source_addr,
  2096. results[i - 1].source_addr_len);
  2097. results[i].source_addr_len = results[i - 1].source_addr_len;
  2098. results[i].got_source_addr = results[i - 1].got_source_addr;
  2099. results[i].source_addr_flags = results[i - 1].source_addr_flags;
  2100. results[i].prefixlen = results[i - 1].prefixlen;
  2101. results[i].index = results[i - 1].index;
  2102. }
  2103. else
  2104. {
  2105. results[i].got_source_addr = false;
  2106. results[i].source_addr_flags = 0;
  2107. results[i].prefixlen = 0;
  2108. results[i].index = 0xffffffffu;
  2109. /* We overwrite the type with SOCK_DGRAM since we do not
  2110. want connect() to connect to the other side. If we
  2111. cannot determine the source address remember this
  2112. fact. */
  2113. if (fd == -1 || (af == AF_INET && q->ai_family == AF_INET6))
  2114. {
  2115. if (fd != -1)
  2116. __close_nocancel_nostatus (fd);
  2117. af = q->ai_family;
  2118. fd = __socket (af, SOCK_DGRAM | SOCK_CLOEXEC, IPPROTO_IP);
  2119. }
  2120. else
  2121. {
  2122. /* Reset the connection. */
  2123. struct sockaddr sa = { .sa_family = AF_UNSPEC };
  2124. __connect (fd, &sa, sizeof (sa));
  2125. }
  2126. if (try_connect (&fd, &af, &results[i].source_addr, q->ai_addr,
  2127. q->ai_addrlen, q->ai_family))
  2128. {
  2129. results[i].source_addr_len = sizeof (results[i].source_addr);
  2130. results[i].got_source_addr = true;
  2131. if (in6ai != NULL)
  2132. {
  2133. /* See whether the source address is on the list of
  2134. deprecated or temporary addresses. */
  2135. struct in6addrinfo tmp;
  2136. if (q->ai_family == AF_INET && af == AF_INET)
  2137. {
  2138. struct sockaddr_in *sinp
  2139. = (struct sockaddr_in *) &results[i].source_addr;
  2140. tmp.addr[0] = 0;
  2141. tmp.addr[1] = 0;
  2142. tmp.addr[2] = htonl (0xffff);
  2143. /* Special case for lo interface, the source address
  2144. being possibly different than the interface
  2145. address. */
  2146. if ((ntohl(sinp->sin_addr.s_addr) & 0xff000000)
  2147. == 0x7f000000)
  2148. tmp.addr[3] = htonl(0x7f000001);
  2149. else
  2150. tmp.addr[3] = sinp->sin_addr.s_addr;
  2151. }
  2152. else
  2153. {
  2154. struct sockaddr_in6 *sin6p
  2155. = (struct sockaddr_in6 *) &results[i].source_addr;
  2156. memcpy (tmp.addr, &sin6p->sin6_addr, IN6ADDRSZ);
  2157. }
  2158. struct in6addrinfo *found
  2159. = bsearch (&tmp, in6ai, in6ailen, sizeof (*in6ai),
  2160. in6aicmp);
  2161. if (found != NULL)
  2162. {
  2163. results[i].source_addr_flags = found->flags;
  2164. results[i].prefixlen = found->prefixlen;
  2165. results[i].index = found->index;
  2166. }
  2167. }
  2168. if (q->ai_family == AF_INET && af == AF_INET6)
  2169. {
  2170. /* We have to convert the address. The socket is
  2171. IPv6 and the request is for IPv4. */
  2172. struct sockaddr_in6 *sin6
  2173. = (struct sockaddr_in6 *) &results[i].source_addr;
  2174. struct sockaddr_in *sin
  2175. = (struct sockaddr_in *) &results[i].source_addr;
  2176. assert (IN6_IS_ADDR_V4MAPPED (sin6->sin6_addr.s6_addr32));
  2177. sin->sin_family = AF_INET;
  2178. /* We do not have to initialize sin_port since this
  2179. fields has the same position and size in the IPv6
  2180. structure. */
  2181. assert (offsetof (struct sockaddr_in, sin_port)
  2182. == offsetof (struct sockaddr_in6, sin6_port));
  2183. assert (sizeof (sin->sin_port)
  2184. == sizeof (sin6->sin6_port));
  2185. memcpy (&sin->sin_addr,
  2186. &sin6->sin6_addr.s6_addr32[3], INADDRSZ);
  2187. results[i].source_addr_len = sizeof (struct sockaddr_in);
  2188. }
  2189. }
  2190. else
  2191. /* Just make sure that if we have to process the same
  2192. address again we do not copy any memory. */
  2193. results[i].source_addr_len = 0;
  2194. }
  2195. /* Remember the canonical name. */
  2196. if (q->ai_canonname != NULL)
  2197. {
  2198. assert (canonname == NULL);
  2199. canonname = q->ai_canonname;
  2200. q->ai_canonname = NULL;
  2201. }
  2202. }
  2203. if (fd != -1)
  2204. __close_nocancel_nostatus (fd);
  2205. /* We got all the source addresses we can get, now sort using
  2206. the information. */
  2207. struct sort_result_combo src
  2208. = { .results = results, .nresults = nresults };
  2209. if (__glibc_unlikely (gaiconf_reload_flag_ever_set))
  2210. {
  2211. __libc_lock_define_initialized (static, lock);
  2212. __libc_lock_lock (lock);
  2213. if (__libc_once_get (old_once) && gaiconf_reload_flag)
  2214. gaiconf_reload ();
  2215. __qsort_r (order, nresults, sizeof (order[0]), rfc3484_sort, &src);
  2216. __libc_lock_unlock (lock);
  2217. }
  2218. else
  2219. __qsort_r (order, nresults, sizeof (order[0]), rfc3484_sort, &src);
  2220. /* Queue the results up as they come out of sorting. */
  2221. q = p = results[order[0]].dest_addr;
  2222. for (i = 1; i < nresults; ++i)
  2223. q = q->ai_next = results[order[i]].dest_addr;
  2224. q->ai_next = NULL;
  2225. /* Fill in the canonical name into the new first entry. */
  2226. p->ai_canonname = canonname;
  2227. scratch_buffer_free (&buf);
  2228. }
  2229. __free_in6ai (in6ai);
  2230. if (p)
  2231. {
  2232. *pai = p;
  2233. return 0;
  2234. }
  2235. return last_i ? -last_i : EAI_NONAME;
  2236. }
  2237. libc_hidden_def (getaddrinfo)
  2238. nss_interface_function (getaddrinfo)
  2239. void
  2240. freeaddrinfo (struct addrinfo *ai)
  2241. {
  2242. struct addrinfo *p;
  2243. while (ai != NULL)
  2244. {
  2245. p = ai;
  2246. ai = ai->ai_next;
  2247. free (p->ai_canonname);
  2248. free (p);
  2249. }
  2250. }
  2251. libc_hidden_def (freeaddrinfo)