resolv.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989
  1. /* resolv.c: DNS Resolver
  2. *
  3. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>,
  4. * The Silver Hammer Group, Ltd.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * 5-Oct-2000 W. Greathouse wgreathouse@smva.com
  12. * Fix memory leak and memory corruption.
  13. * -- Every name resolution resulted in
  14. * a new parse of resolv.conf and new
  15. * copy of nameservers allocated by
  16. * strdup.
  17. * -- Every name resolution resulted in
  18. * a new read of resolv.conf without
  19. * resetting index from prior read...
  20. * resulting in exceeding array bounds.
  21. *
  22. * Limit nameservers read from resolv.conf
  23. *
  24. * Add "search" domains from resolv.conf
  25. *
  26. * Some systems will return a security
  27. * signature along with query answer for
  28. * dynamic DNS entries.
  29. * -- skip/ignore this answer
  30. *
  31. * Include arpa/nameser.h for defines.
  32. *
  33. * General cleanup
  34. *
  35. * 20-Jun-2001 Michal Moskal <malekith@pld.org.pl>
  36. * partial IPv6 support (i.e. gethostbyname2() and resolve_address2()
  37. * functions added), IPv6 nameservers are also supported.
  38. *
  39. * 6-Oct-2001 Jari Korva <jari.korva@iki.fi>
  40. * more IPv6 support (IPv6 support for gethostbyaddr();
  41. * address family parameter and improved IPv6 support for get_hosts_byname
  42. * and read_etc_hosts; getnameinfo() port from glibc; defined
  43. * defined ip6addr_any and in6addr_loopback)
  44. *
  45. * 2-Feb-2002 Erik Andersen <andersee@debian.org>
  46. * Added gethostent(), sethostent(), and endhostent()
  47. *
  48. * 17-Aug-2002 Manuel Novoa III <mjn3@codepoet.org>
  49. * Fixed __read_etc_hosts_r to return alias list, and modified buffer
  50. * allocation accordingly. See MAX_ALIASES and ALIAS_DIM below.
  51. * This fixes the segfault in the Python 2.2.1 socket test.
  52. *
  53. * 04-Jan-2003 Jay Kulpinski <jskulpin@berkshire.rr.com>
  54. * Fixed __decode_dotted to count the terminating null character
  55. * in a host name.
  56. *
  57. *
  58. */
  59. #define __FORCE_GLIBC
  60. #include <features.h>
  61. #include <string.h>
  62. #include <stdio.h>
  63. #include <signal.h>
  64. #include <errno.h>
  65. #include <sys/socket.h>
  66. #include <sys/types.h>
  67. #include <sys/time.h>
  68. #include <netinet/in.h>
  69. #include <arpa/inet.h>
  70. #include <stdlib.h>
  71. #include <unistd.h>
  72. #include <resolv.h>
  73. #include <netdb.h>
  74. #include <ctype.h>
  75. #include <arpa/nameser.h>
  76. #include <sys/utsname.h>
  77. #include <sys/un.h>
  78. #define MAX_RECURSE 5
  79. #define REPLY_TIMEOUT 10
  80. #define MAX_RETRIES 15
  81. #define MAX_SERVERS 3
  82. #define MAX_SEARCH 4
  83. #define MAX_ALIASES 5
  84. /* 1:ip + 1:full + MAX_ALIASES:aliases + 1:NULL */
  85. #define ALIAS_DIM (2 + MAX_ALIASES + 1)
  86. #undef DEBUG
  87. /*#define DEBUG*/
  88. #ifdef DEBUG
  89. #define DPRINTF(X,args...) fprintf(stderr, X, ##args)
  90. #else
  91. #define DPRINTF(X,args...)
  92. #endif /* DEBUG */
  93. /* Global stuff (stuff needing to be locked to be thread safe)... */
  94. extern int __nameservers;
  95. extern char * __nameserver[MAX_SERVERS];
  96. extern int __searchdomains;
  97. extern char * __searchdomain[MAX_SEARCH];
  98. #ifdef __UCLIBC_HAS_THREADS__
  99. #include <pthread.h>
  100. extern pthread_mutex_t __resolv_lock;
  101. # define BIGLOCK pthread_mutex_lock(&__resolv_lock)
  102. # define BIGUNLOCK pthread_mutex_unlock(&__resolv_lock);
  103. #else
  104. # define BIGLOCK
  105. # define BIGUNLOCK
  106. #endif
  107. /* Structs */
  108. struct resolv_header {
  109. int id;
  110. int qr,opcode,aa,tc,rd,ra,rcode;
  111. int qdcount;
  112. int ancount;
  113. int nscount;
  114. int arcount;
  115. };
  116. struct resolv_question {
  117. char * dotted;
  118. int qtype;
  119. int qclass;
  120. };
  121. struct resolv_answer {
  122. char * dotted;
  123. int atype;
  124. int aclass;
  125. int ttl;
  126. int rdlength;
  127. unsigned char * rdata;
  128. int rdoffset;
  129. };
  130. enum etc_hosts_action {
  131. GET_HOSTS_BYNAME = 0,
  132. GETHOSTENT,
  133. GET_HOSTS_BYADDR,
  134. };
  135. /* function prototypes */
  136. extern int __get_hosts_byname_r(const char * name, int type,
  137. struct hostent * result_buf,
  138. char * buf, size_t buflen,
  139. struct hostent ** result,
  140. int * h_errnop);
  141. extern int __get_hosts_byaddr_r(const char * addr, int len, int type,
  142. struct hostent * result_buf,
  143. char * buf, size_t buflen,
  144. struct hostent ** result,
  145. int * h_errnop);
  146. extern void __open_etc_hosts(FILE **fp);
  147. extern int __read_etc_hosts_r(FILE *fp, const char * name, int type,
  148. enum etc_hosts_action action,
  149. struct hostent * result_buf,
  150. char * buf, size_t buflen,
  151. struct hostent ** result,
  152. int * h_errnop);
  153. extern int __dns_lookup(const char * name, int type, int nscount,
  154. char ** nsip, unsigned char ** outpacket, struct resolv_answer * a);
  155. extern int __encode_dotted(const char * dotted, unsigned char * dest, int maxlen);
  156. extern int __decode_dotted(const unsigned char * message, int offset,
  157. char * dest, int maxlen);
  158. extern int __length_dotted(const unsigned char * message, int offset);
  159. extern int __encode_header(struct resolv_header * h, unsigned char * dest, int maxlen);
  160. extern int __decode_header(unsigned char * data, struct resolv_header * h);
  161. extern int __encode_question(struct resolv_question * q,
  162. unsigned char * dest, int maxlen);
  163. extern int __decode_question(unsigned char * message, int offset,
  164. struct resolv_question * q);
  165. extern int __encode_answer(struct resolv_answer * a,
  166. unsigned char * dest, int maxlen);
  167. extern int __decode_answer(unsigned char * message, int offset,
  168. struct resolv_answer * a);
  169. extern int __length_question(unsigned char * message, int offset);
  170. extern int __open_nameservers(void);
  171. extern void __close_nameservers(void);
  172. #ifdef L_encodeh
  173. int __encode_header(struct resolv_header *h, unsigned char *dest, int maxlen)
  174. {
  175. if (maxlen < HFIXEDSZ)
  176. return -1;
  177. dest[0] = (h->id & 0xff00) >> 8;
  178. dest[1] = (h->id & 0x00ff) >> 0;
  179. dest[2] = (h->qr ? 0x80 : 0) |
  180. ((h->opcode & 0x0f) << 3) |
  181. (h->aa ? 0x04 : 0) |
  182. (h->tc ? 0x02 : 0) |
  183. (h->rd ? 0x01 : 0);
  184. dest[3] = (h->ra ? 0x80 : 0) | (h->rcode & 0x0f);
  185. dest[4] = (h->qdcount & 0xff00) >> 8;
  186. dest[5] = (h->qdcount & 0x00ff) >> 0;
  187. dest[6] = (h->ancount & 0xff00) >> 8;
  188. dest[7] = (h->ancount & 0x00ff) >> 0;
  189. dest[8] = (h->nscount & 0xff00) >> 8;
  190. dest[9] = (h->nscount & 0x00ff) >> 0;
  191. dest[10] = (h->arcount & 0xff00) >> 8;
  192. dest[11] = (h->arcount & 0x00ff) >> 0;
  193. return HFIXEDSZ;
  194. }
  195. #endif
  196. #ifdef L_decodeh
  197. int __decode_header(unsigned char *data, struct resolv_header *h)
  198. {
  199. h->id = (data[0] << 8) | data[1];
  200. h->qr = (data[2] & 0x80) ? 1 : 0;
  201. h->opcode = (data[2] >> 3) & 0x0f;
  202. h->aa = (data[2] & 0x04) ? 1 : 0;
  203. h->tc = (data[2] & 0x02) ? 1 : 0;
  204. h->rd = (data[2] & 0x01) ? 1 : 0;
  205. h->ra = (data[3] & 0x80) ? 1 : 0;
  206. h->rcode = data[3] & 0x0f;
  207. h->qdcount = (data[4] << 8) | data[5];
  208. h->ancount = (data[6] << 8) | data[7];
  209. h->nscount = (data[8] << 8) | data[9];
  210. h->arcount = (data[10] << 8) | data[11];
  211. return HFIXEDSZ;
  212. }
  213. #endif
  214. #ifdef L_encoded
  215. /* Encode a dotted string into nameserver transport-level encoding.
  216. This routine is fairly dumb, and doesn't attempt to compress
  217. the data */
  218. int __encode_dotted(const char *dotted, unsigned char *dest, int maxlen)
  219. {
  220. int used = 0;
  221. while (dotted && *dotted) {
  222. char *c = strchr(dotted, '.');
  223. int l = c ? c - dotted : strlen(dotted);
  224. if (l >= (maxlen - used - 1))
  225. return -1;
  226. dest[used++] = l;
  227. memcpy(dest + used, dotted, l);
  228. used += l;
  229. if (c)
  230. dotted = c + 1;
  231. else
  232. break;
  233. }
  234. if (maxlen < 1)
  235. return -1;
  236. dest[used++] = 0;
  237. return used;
  238. }
  239. #endif
  240. #ifdef L_decoded
  241. /* Decode a dotted string from nameserver transport-level encoding.
  242. This routine understands compressed data. */
  243. int __decode_dotted(const unsigned char *data, int offset,
  244. char *dest, int maxlen)
  245. {
  246. int l;
  247. int measure = 1;
  248. int total = 0;
  249. int used = 0;
  250. if (!data)
  251. return -1;
  252. while ((l=data[offset++])) {
  253. if (measure)
  254. total++;
  255. if ((l & 0xc0) == (0xc0)) {
  256. if (measure)
  257. total++;
  258. /* compressed item, redirect */
  259. offset = ((l & 0x3f) << 8) | data[offset];
  260. measure = 0;
  261. continue;
  262. }
  263. if ((used + l + 1) >= maxlen)
  264. return -1;
  265. memcpy(dest + used, data + offset, l);
  266. offset += l;
  267. used += l;
  268. if (measure)
  269. total += l;
  270. if (data[offset] != 0)
  271. dest[used++] = '.';
  272. else
  273. dest[used++] = '\0';
  274. }
  275. /* The null byte must be counted too */
  276. if (measure) {
  277. total++;
  278. }
  279. DPRINTF("Total decode len = %d\n", total);
  280. return total;
  281. }
  282. #endif
  283. #ifdef L_lengthd
  284. int __length_dotted(const unsigned char *data, int offset)
  285. {
  286. int orig_offset = offset;
  287. int l;
  288. if (!data)
  289. return -1;
  290. while ((l = data[offset++])) {
  291. if ((l & 0xc0) == (0xc0)) {
  292. offset++;
  293. break;
  294. }
  295. offset += l;
  296. }
  297. return offset - orig_offset;
  298. }
  299. #endif
  300. #ifdef L_encodeq
  301. int __encode_question(struct resolv_question *q,
  302. unsigned char *dest, int maxlen)
  303. {
  304. int i;
  305. i = __encode_dotted(q->dotted, dest, maxlen);
  306. if (i < 0)
  307. return i;
  308. dest += i;
  309. maxlen -= i;
  310. if (maxlen < 4)
  311. return -1;
  312. dest[0] = (q->qtype & 0xff00) >> 8;
  313. dest[1] = (q->qtype & 0x00ff) >> 0;
  314. dest[2] = (q->qclass & 0xff00) >> 8;
  315. dest[3] = (q->qclass & 0x00ff) >> 0;
  316. return i + 4;
  317. }
  318. #endif
  319. #ifdef L_decodeq
  320. int __decode_question(unsigned char *message, int offset,
  321. struct resolv_question *q)
  322. {
  323. char temp[256];
  324. int i;
  325. i = __decode_dotted(message, offset, temp, sizeof(temp));
  326. if (i < 0)
  327. return i;
  328. offset += i;
  329. q->dotted = strdup(temp);
  330. q->qtype = (message[offset + 0] << 8) | message[offset + 1];
  331. q->qclass = (message[offset + 2] << 8) | message[offset + 3];
  332. return i + 4;
  333. }
  334. #endif
  335. #ifdef L_lengthq
  336. int __length_question(unsigned char *message, int offset)
  337. {
  338. int i;
  339. i = __length_dotted(message, offset);
  340. if (i < 0)
  341. return i;
  342. return i + 4;
  343. }
  344. #endif
  345. #ifdef L_encodea
  346. int __encode_answer(struct resolv_answer *a, unsigned char *dest, int maxlen)
  347. {
  348. int i;
  349. i = __encode_dotted(a->dotted, dest, maxlen);
  350. if (i < 0)
  351. return i;
  352. dest += i;
  353. maxlen -= i;
  354. if (maxlen < (RRFIXEDSZ+a->rdlength))
  355. return -1;
  356. *dest++ = (a->atype & 0xff00) >> 8;
  357. *dest++ = (a->atype & 0x00ff) >> 0;
  358. *dest++ = (a->aclass & 0xff00) >> 8;
  359. *dest++ = (a->aclass & 0x00ff) >> 0;
  360. *dest++ = (a->ttl & 0xff000000) >> 24;
  361. *dest++ = (a->ttl & 0x00ff0000) >> 16;
  362. *dest++ = (a->ttl & 0x0000ff00) >> 8;
  363. *dest++ = (a->ttl & 0x000000ff) >> 0;
  364. *dest++ = (a->rdlength & 0xff00) >> 8;
  365. *dest++ = (a->rdlength & 0x00ff) >> 0;
  366. memcpy(dest, a->rdata, a->rdlength);
  367. return i + RRFIXEDSZ + a->rdlength;
  368. }
  369. #endif
  370. #ifdef L_decodea
  371. int __decode_answer(unsigned char *message, int offset,
  372. struct resolv_answer *a)
  373. {
  374. char temp[256];
  375. int i;
  376. i = __decode_dotted(message, offset, temp, sizeof(temp));
  377. if (i < 0)
  378. return i;
  379. message += offset + i;
  380. a->dotted = strdup(temp);
  381. a->atype = (message[0] << 8) | message[1];
  382. message += 2;
  383. a->aclass = (message[0] << 8) | message[1];
  384. message += 2;
  385. a->ttl = (message[0] << 24) |
  386. (message[1] << 16) | (message[2] << 8) | (message[3] << 0);
  387. message += 4;
  388. a->rdlength = (message[0] << 8) | message[1];
  389. message += 2;
  390. a->rdata = message;
  391. a->rdoffset = offset + i + RRFIXEDSZ;
  392. DPRINTF("i=%d,rdlength=%d\n", i, a->rdlength);
  393. return i + RRFIXEDSZ + a->rdlength;
  394. }
  395. #endif
  396. #ifdef L_encodep
  397. int __encode_packet(struct resolv_header *h,
  398. struct resolv_question **q,
  399. struct resolv_answer **an,
  400. struct resolv_answer **ns,
  401. struct resolv_answer **ar,
  402. unsigned char *dest, int maxlen)
  403. {
  404. int i, total = 0;
  405. int j;
  406. i = __encode_header(h, dest, maxlen);
  407. if (i < 0)
  408. return i;
  409. dest += i;
  410. maxlen -= i;
  411. total += i;
  412. for (j = 0; j < h->qdcount; j++) {
  413. i = __encode_question(q[j], dest, maxlen);
  414. if (i < 0)
  415. return i;
  416. dest += i;
  417. maxlen -= i;
  418. total += i;
  419. }
  420. for (j = 0; j < h->ancount; j++) {
  421. i = __encode_answer(an[j], dest, maxlen);
  422. if (i < 0)
  423. return i;
  424. dest += i;
  425. maxlen -= i;
  426. total += i;
  427. }
  428. for (j = 0; j < h->nscount; j++) {
  429. i = __encode_answer(ns[j], dest, maxlen);
  430. if (i < 0)
  431. return i;
  432. dest += i;
  433. maxlen -= i;
  434. total += i;
  435. }
  436. for (j = 0; j < h->arcount; j++) {
  437. i = __encode_answer(ar[j], dest, maxlen);
  438. if (i < 0)
  439. return i;
  440. dest += i;
  441. maxlen -= i;
  442. total += i;
  443. }
  444. return total;
  445. }
  446. #endif
  447. #ifdef L_decodep
  448. int __decode_packet(unsigned char *data, struct resolv_header *h)
  449. {
  450. return __decode_header(data, h);
  451. }
  452. #endif
  453. #ifdef L_formquery
  454. int __form_query(int id, const char *name, int type, unsigned char *packet,
  455. int maxlen)
  456. {
  457. struct resolv_header h;
  458. struct resolv_question q;
  459. int i, j;
  460. memset(&h, 0, sizeof(h));
  461. h.id = id;
  462. h.qdcount = 1;
  463. q.dotted = (char *) name;
  464. q.qtype = type;
  465. q.qclass = C_IN; /* CLASS_IN */
  466. i = __encode_header(&h, packet, maxlen);
  467. if (i < 0)
  468. return i;
  469. j = __encode_question(&q, packet + i, maxlen - i);
  470. if (j < 0)
  471. return j;
  472. return i + j;
  473. }
  474. #endif
  475. #ifdef L_dnslookup
  476. #ifdef __UCLIBC_HAS_THREADS__
  477. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  478. # define LOCK pthread_mutex_lock(&mylock)
  479. # define UNLOCK pthread_mutex_unlock(&mylock);
  480. #else
  481. # define LOCK
  482. # define UNLOCK
  483. #endif
  484. /* Just for the record, having to lock __dns_lookup() just for these two globals
  485. * is pretty lame. I think these two variables can probably be de-global-ized,
  486. * which should eliminate the need for doing locking here... Needs a closer
  487. * look anyways. */
  488. static int ns=0, id=1;
  489. int __dns_lookup(const char *name, int type, int nscount, char **nsip,
  490. unsigned char **outpacket, struct resolv_answer *a)
  491. {
  492. int i, j, len, fd, pos, rc;
  493. struct timeval tv;
  494. fd_set fds;
  495. struct resolv_header h;
  496. struct resolv_question q;
  497. int retries = 0;
  498. unsigned char * packet = malloc(PACKETSZ);
  499. char *dns, *lookup = malloc(MAXDNAME);
  500. int variant = 0;
  501. struct sockaddr_in sa;
  502. #ifdef __UCLIBC_HAS_IPV6__
  503. int v6;
  504. struct sockaddr_in6 sa6;
  505. #endif
  506. fd = -1;
  507. if (!packet || !lookup || !nscount)
  508. goto fail;
  509. DPRINTF("Looking up type %d answer for '%s'\n", type, name);
  510. LOCK;
  511. ns %= nscount;
  512. UNLOCK;
  513. while (retries++ < MAX_RETRIES) {
  514. if (fd != -1)
  515. close(fd);
  516. memset(packet, 0, PACKETSZ);
  517. memset(&h, 0, sizeof(h));
  518. /* Mess with globals while under lock */
  519. LOCK;
  520. ++id;
  521. id &= 0xffff;
  522. h.id = id;
  523. dns = nsip[ns];
  524. UNLOCK;
  525. h.qdcount = 1;
  526. h.rd = 1;
  527. DPRINTF("encoding header\n", h.rd);
  528. i = __encode_header(&h, packet, PACKETSZ);
  529. if (i < 0)
  530. goto fail;
  531. strncpy(lookup,name,MAXDNAME);
  532. BIGLOCK;
  533. if (variant < __searchdomains && strchr(lookup, '.') == NULL)
  534. {
  535. strncat(lookup,".", MAXDNAME);
  536. strncat(lookup,__searchdomain[variant], MAXDNAME);
  537. }
  538. BIGUNLOCK;
  539. DPRINTF("lookup name: %s\n", lookup);
  540. q.dotted = (char *)lookup;
  541. q.qtype = type;
  542. q.qclass = C_IN; /* CLASS_IN */
  543. j = __encode_question(&q, packet+i, PACKETSZ-i);
  544. if (j < 0)
  545. goto fail;
  546. len = i + j;
  547. DPRINTF("On try %d, sending query to port %d of machine %s\n",
  548. retries, NAMESERVER_PORT, dns);
  549. #ifdef __UCLIBC_HAS_IPV6__
  550. v6 = inet_pton(AF_INET6, dns, &sa6.sin6_addr) > 0;
  551. fd = socket(v6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  552. #else
  553. fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  554. #endif
  555. if (fd < 0) {
  556. continue;
  557. }
  558. /* Connect to the UDP socket so that asyncronous errors are returned */
  559. #ifdef __UCLIBC_HAS_IPV6__
  560. if (v6) {
  561. sa6.sin6_family = AF_INET6;
  562. sa6.sin6_port = htons(NAMESERVER_PORT);
  563. /* sa6.sin6_addr is already here */
  564. rc = connect(fd, (struct sockaddr *) &sa6, sizeof(sa6));
  565. } else {
  566. #endif
  567. sa.sin_family = AF_INET;
  568. sa.sin_port = htons(NAMESERVER_PORT);
  569. sa.sin_addr.s_addr = inet_addr(dns);
  570. rc = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
  571. #ifdef __UCLIBC_HAS_IPV6__
  572. }
  573. #endif
  574. if (rc < 0) {
  575. if (errno == ENETUNREACH) {
  576. /* routing error, presume not transient */
  577. goto tryall;
  578. } else
  579. /* retry */
  580. continue;
  581. }
  582. DPRINTF("Transmitting packet of length %d, id=%d, qr=%d\n",
  583. len, h.id, h.qr);
  584. send(fd, packet, len, 0);
  585. FD_ZERO(&fds);
  586. FD_SET(fd, &fds);
  587. tv.tv_sec = REPLY_TIMEOUT;
  588. tv.tv_usec = 0;
  589. if (select(fd + 1, &fds, NULL, NULL, &tv) <= 0) {
  590. DPRINTF("Timeout\n");
  591. /* timed out, so retry send and receive,
  592. * to next nameserver on queue */
  593. goto again;
  594. }
  595. i = recv(fd, packet, 512, 0);
  596. if (i < HFIXEDSZ) {
  597. /* too short ! */
  598. goto again;
  599. }
  600. __decode_header(packet, &h);
  601. DPRINTF("id = %d, qr = %d\n", h.id, h.qr);
  602. LOCK;
  603. if ((h.id != id) || (!h.qr)) {
  604. UNLOCK;
  605. /* unsolicited */
  606. goto again;
  607. }
  608. UNLOCK;
  609. DPRINTF("Got response %s\n", "(i think)!");
  610. DPRINTF("qrcount=%d,ancount=%d,nscount=%d,arcount=%d\n",
  611. h.qdcount, h.ancount, h.nscount, h.arcount);
  612. DPRINTF("opcode=%d,aa=%d,tc=%d,rd=%d,ra=%d,rcode=%d\n",
  613. h.opcode, h.aa, h.tc, h.rd, h.ra, h.rcode);
  614. if ((h.rcode) || (h.ancount < 1)) {
  615. /* negative result, not present */
  616. goto again;
  617. }
  618. pos = HFIXEDSZ;
  619. for (j = 0; j < h.qdcount; j++) {
  620. DPRINTF("Skipping question %d at %d\n", j, pos);
  621. i = __length_question(packet, pos);
  622. DPRINTF("Length of question %d is %d\n", j, i);
  623. if (i < 0)
  624. goto again;
  625. pos += i;
  626. }
  627. DPRINTF("Decoding answer at pos %d\n", pos);
  628. for (j=0;j<h.ancount;j++)
  629. {
  630. i = __decode_answer(packet, pos, a);
  631. if (i<0) {
  632. DPRINTF("failed decode %d\n", i);
  633. goto again;
  634. }
  635. /* For all but T_SIG, accept first answer */
  636. if (a->atype != T_SIG)
  637. break;
  638. DPRINTF("skipping T_SIG %d\n", i);
  639. free(a->dotted);
  640. pos += i;
  641. }
  642. DPRINTF("Answer name = |%s|\n", a->dotted);
  643. DPRINTF("Answer type = |%d|\n", a->atype);
  644. close(fd);
  645. if (outpacket)
  646. *outpacket = packet;
  647. else
  648. free(packet);
  649. free(lookup);
  650. return (0); /* success! */
  651. tryall:
  652. /* if there are other nameservers, give them a go,
  653. otherwise return with error */
  654. {
  655. int sdomains;
  656. BIGLOCK;
  657. sdomains=__searchdomains;
  658. BIGUNLOCK;
  659. variant = 0;
  660. if (retries >= nscount*(sdomains+1))
  661. goto fail;
  662. }
  663. again:
  664. /* if there are searchdomains, try them or fallback as passed */
  665. {
  666. int sdomains;
  667. BIGLOCK;
  668. sdomains=__searchdomains;
  669. BIGUNLOCK;
  670. if (variant < sdomains) {
  671. /* next search */
  672. variant++;
  673. } else {
  674. /* next server, first search */
  675. LOCK;
  676. ns = (ns + 1) % nscount;
  677. UNLOCK;
  678. variant = 0;
  679. }
  680. }
  681. }
  682. fail:
  683. if (fd != -1)
  684. close(fd);
  685. if (lookup)
  686. free(lookup);
  687. if (packet)
  688. free(packet);
  689. return -1;
  690. }
  691. #endif
  692. #ifdef L_opennameservers
  693. int __nameservers;
  694. char * __nameserver[MAX_SERVERS];
  695. int __searchdomains;
  696. char * __searchdomain[MAX_SEARCH];
  697. #ifdef __UCLIBC_HAS_THREADS__
  698. pthread_mutex_t __resolv_lock = PTHREAD_MUTEX_INITIALIZER;
  699. #endif
  700. /*
  701. * we currently read formats not quite the same as that on normal
  702. * unix systems, we can have a list of nameservers after the keyword.
  703. */
  704. int __open_nameservers()
  705. {
  706. FILE *fp;
  707. int i;
  708. #define RESOLV_ARGS 5
  709. char szBuffer[128], *p, *argv[RESOLV_ARGS];
  710. int argc;
  711. BIGLOCK;
  712. if (__nameservers > 0) {
  713. BIGUNLOCK;
  714. return 0;
  715. }
  716. if ((fp = fopen("/etc/resolv.conf", "r")) ||
  717. (fp = fopen("/etc/config/resolv.conf", "r"))) {
  718. while (fgets(szBuffer, sizeof(szBuffer), fp) != NULL) {
  719. for (p = szBuffer; *p && isspace(*p); p++)
  720. /* skip white space */;
  721. if (*p == '\0' || *p == '\n' || *p == '#') /* skip comments etc */
  722. continue;
  723. argc = 0;
  724. while (*p && argc < RESOLV_ARGS) {
  725. argv[argc++] = p;
  726. while (*p && !isspace(*p) && *p != '\n')
  727. p++;
  728. while (*p && (isspace(*p) || *p == '\n')) /* remove spaces */
  729. *p++ = '\0';
  730. }
  731. if (strcmp(argv[0], "nameserver") == 0) {
  732. for (i = 1; i < argc && __nameservers < MAX_SERVERS; i++) {
  733. __nameserver[__nameservers++] = strdup(argv[i]);
  734. DPRINTF("adding nameserver %s\n", argv[i]);
  735. }
  736. }
  737. /* domain and search are mutually exclusive, the last one wins */
  738. if (strcmp(argv[0],"domain")==0 || strcmp(argv[0],"search")==0) {
  739. while (__searchdomains > 0) {
  740. free(__searchdomain[--__searchdomains]);
  741. __searchdomain[__searchdomains] = NULL;
  742. }
  743. for (i=1; i < argc && __searchdomains < MAX_SEARCH; i++) {
  744. __searchdomain[__searchdomains++] = strdup(argv[i]);
  745. DPRINTF("adding search %s\n", argv[i]);
  746. }
  747. }
  748. }
  749. fclose(fp);
  750. } else {
  751. DPRINTF("failed to open %s\n", "resolv.conf");
  752. }
  753. DPRINTF("nameservers = %d\n", __nameservers);
  754. BIGUNLOCK;
  755. return 0;
  756. }
  757. #endif
  758. #ifdef L_closenameservers
  759. void __close_nameservers(void)
  760. {
  761. BIGLOCK;
  762. while (__nameservers > 0) {
  763. free(__nameserver[--__nameservers]);
  764. __nameserver[__nameservers] = NULL;
  765. }
  766. while (__searchdomains > 0) {
  767. free(__searchdomain[--__searchdomains]);
  768. __searchdomain[__searchdomains] = NULL;
  769. }
  770. BIGUNLOCK;
  771. }
  772. #endif
  773. #ifdef L_gethostbyname
  774. struct hostent *gethostbyname(const char *name)
  775. {
  776. static struct hostent h;
  777. static char buf[sizeof(struct in_addr) +
  778. sizeof(struct in_addr *)*2 +
  779. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  780. struct hostent *hp;
  781. gethostbyname_r(name, &h, buf, sizeof(buf), &hp, &h_errno);
  782. return hp;
  783. }
  784. #endif
  785. #ifdef L_gethostbyname2
  786. struct hostent *gethostbyname2(const char *name, int family)
  787. {
  788. #ifndef __UCLIBC_HAS_IPV6__
  789. return family == AF_INET ? gethostbyname(name) : (struct hostent*)0;
  790. #else /* __UCLIBC_HAS_IPV6__ */
  791. static struct hostent h;
  792. static char buf[sizeof(struct in6_addr) +
  793. sizeof(struct in6_addr *)*2 +
  794. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  795. struct hostent *hp;
  796. gethostbyname2_r(name, family, &h, buf, sizeof(buf), &hp, &h_errno);
  797. return hp;
  798. #endif /* __UCLIBC_HAS_IPV6__ */
  799. }
  800. #endif
  801. #ifdef L_getnetbyname
  802. struct netent * getnetbyname(const char * name)
  803. {
  804. return NULL;
  805. }
  806. #endif
  807. #ifdef L_res_init
  808. struct __res_state _res;
  809. int res_init(void)
  810. {
  811. struct __res_state *rp = &(_res);
  812. __close_nameservers();
  813. __open_nameservers();
  814. rp->retrans = RES_TIMEOUT;
  815. rp->retry = 4;
  816. rp->options = RES_INIT;
  817. rp->id = (u_int) random();
  818. rp->nsaddr.sin_addr.s_addr = INADDR_ANY;
  819. rp->nsaddr.sin_family = AF_INET;
  820. rp->nsaddr.sin_port = htons(NAMESERVER_PORT);
  821. rp->ndots = 1;
  822. /** rp->pfcode = 0; **/
  823. rp->_vcsock = -1;
  824. /** rp->_flags = 0; **/
  825. /** rp->qhook = NULL; **/
  826. /** rp->rhook = NULL; **/
  827. /** rp->_u._ext.nsinit = 0; **/
  828. BIGLOCK;
  829. if(__searchdomains) {
  830. int i;
  831. for(i=0; i<__searchdomains; i++) {
  832. rp->dnsrch[i] = __searchdomain[i];
  833. }
  834. }
  835. if(__nameservers) {
  836. int i;
  837. struct in_addr a;
  838. for(i=0; i<__nameservers; i++) {
  839. if (inet_aton(__nameserver[i], &a)) {
  840. rp->nsaddr_list[i].sin_addr = a;
  841. rp->nsaddr_list[i].sin_family = AF_INET;
  842. rp->nsaddr_list[i].sin_port = htons(NAMESERVER_PORT);
  843. }
  844. }
  845. }
  846. rp->nscount = __nameservers;
  847. BIGUNLOCK;
  848. return(0);
  849. }
  850. void res_close( void )
  851. {
  852. return;
  853. }
  854. #endif
  855. #ifdef L_res_query
  856. #ifndef MIN
  857. #define MIN(x, y) ((x) < (y) ? (x) : (y))
  858. #endif
  859. int res_query(const char *dname, int class, int type,
  860. unsigned char *answer, int anslen)
  861. {
  862. int i;
  863. unsigned char * packet = 0;
  864. struct resolv_answer a;
  865. int __nameserversXX;
  866. char ** __nameserverXX;
  867. __open_nameservers();
  868. if (!dname || class != 1 /* CLASS_IN */)
  869. return(-1);
  870. memset((char *) &a, '\0', sizeof(a));
  871. BIGLOCK;
  872. __nameserversXX=__nameservers;
  873. __nameserverXX=__nameserver;
  874. BIGUNLOCK;
  875. i = __dns_lookup(dname, type, __nameserversXX, __nameserverXX, &packet, &a);
  876. if (i < 0)
  877. return(-1);
  878. free(a.dotted);
  879. if (a.atype == type) { /* CNAME*/
  880. if (anslen && answer)
  881. memcpy(answer, a.rdata, MIN(anslen, a.rdlength));
  882. if (packet)
  883. free(packet);
  884. return(MIN(anslen, a.rdlength));
  885. }
  886. if (packet)
  887. free(packet);
  888. return 0;
  889. }
  890. #endif
  891. #ifdef L_gethostbyaddr
  892. struct hostent *gethostbyaddr (const void *addr, socklen_t len, int type)
  893. {
  894. static struct hostent h;
  895. static char buf[
  896. #ifndef __UCLIBC_HAS_IPV6__
  897. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  898. #else
  899. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  900. #endif /* __UCLIBC_HAS_IPV6__ */
  901. sizeof(char *)*(ALIAS_DIM) + 256/*namebuffer*/ + 32/* margin */];
  902. struct hostent *hp;
  903. gethostbyaddr_r(addr, len, type, &h, buf, sizeof(buf), &hp, &h_errno);
  904. return hp;
  905. }
  906. #endif
  907. #ifdef L_read_etc_hosts_r
  908. void __open_etc_hosts(FILE **fp)
  909. {
  910. if ((*fp = fopen("/etc/hosts", "r")) == NULL) {
  911. *fp = fopen("/etc/config/hosts", "r");
  912. }
  913. return;
  914. }
  915. int __read_etc_hosts_r(FILE * fp, const char * name, int type,
  916. enum etc_hosts_action action,
  917. struct hostent * result_buf,
  918. char * buf, size_t buflen,
  919. struct hostent ** result,
  920. int * h_errnop)
  921. {
  922. struct in_addr *in=NULL;
  923. struct in_addr **addr_list=NULL;
  924. #ifdef __UCLIBC_HAS_IPV6__
  925. struct in6_addr *in6=NULL;
  926. struct in6_addr **addr_list6=NULL;
  927. #endif /* __UCLIBC_HAS_IPV6__ */
  928. char *cp;
  929. char **alias;
  930. int aliases, i;
  931. int ret=HOST_NOT_FOUND;
  932. if (buflen < sizeof(char *)*(ALIAS_DIM))
  933. return ERANGE;
  934. alias=(char **)buf;
  935. buf+=sizeof(char **)*(ALIAS_DIM);
  936. buflen-=sizeof(char **)*(ALIAS_DIM);
  937. if (action!=GETHOSTENT) {
  938. #ifdef __UCLIBC_HAS_IPV6__
  939. char *p=buf;
  940. size_t len=buflen;
  941. #endif /* __UCLIBC_HAS_IPV6__ */
  942. *h_errnop=NETDB_INTERNAL;
  943. if (buflen < sizeof(*in))
  944. return ERANGE;
  945. in=(struct in_addr*)buf;
  946. buf+=sizeof(*in);
  947. buflen-=sizeof(*in);
  948. if (buflen < sizeof(*addr_list)*2)
  949. return ERANGE;
  950. addr_list=(struct in_addr **)buf;
  951. buf+=sizeof(*addr_list)*2;
  952. buflen-=sizeof(*addr_list)*2;
  953. #ifdef __UCLIBC_HAS_IPV6__
  954. if (len < sizeof(*in6))
  955. return ERANGE;
  956. in6=(struct in6_addr*)p;
  957. p+=sizeof(*in6);
  958. len-=sizeof(*in6);
  959. if (len < sizeof(*addr_list6)*2)
  960. return ERANGE;
  961. addr_list6=(struct in6_addr**)p;
  962. p+=sizeof(*addr_list6)*2;
  963. len-=sizeof(*addr_list6)*2;
  964. if (len < buflen) {
  965. buflen=len;
  966. buf=p;
  967. }
  968. #endif /* __UCLIBC_HAS_IPV6__ */
  969. if (buflen < 80)
  970. return ERANGE;
  971. __open_etc_hosts(&fp);
  972. if (fp == NULL) {
  973. result=NULL;
  974. return errno;
  975. }
  976. }
  977. *h_errnop=HOST_NOT_FOUND;
  978. while (fgets(buf, buflen, fp)) {
  979. if ((cp = strchr(buf, '#')))
  980. *cp = '\0';
  981. DPRINTF("Looking at: %s\n", buf);
  982. aliases = 0;
  983. cp = buf;
  984. while (*cp) {
  985. while (*cp && isspace(*cp))
  986. *cp++ = '\0';
  987. if (!*cp)
  988. continue;
  989. if (aliases < (2+MAX_ALIASES))
  990. alias[aliases++] = cp;
  991. while (*cp && !isspace(*cp))
  992. cp++;
  993. }
  994. alias[aliases] = 0;
  995. if (aliases < 2)
  996. continue; /* syntax error really */
  997. if (action==GETHOSTENT) {
  998. /* Return whatever the next entry happens to be. */
  999. break;
  1000. } else if (action==GET_HOSTS_BYADDR) {
  1001. if (strcmp(name, alias[0]) != 0)
  1002. continue;
  1003. } else {
  1004. /* GET_HOSTS_BYNAME */
  1005. for (i = 1; i < aliases; i++)
  1006. if (strcasecmp(name, alias[i]) == 0)
  1007. break;
  1008. if (i >= aliases)
  1009. continue;
  1010. }
  1011. if (type == AF_INET && inet_pton(AF_INET, alias[0], in) > 0) {
  1012. DPRINTF("Found INET\n");
  1013. addr_list[0] = in;
  1014. addr_list[1] = 0;
  1015. result_buf->h_name = alias[1];
  1016. result_buf->h_addrtype = AF_INET;
  1017. result_buf->h_length = sizeof(*in);
  1018. result_buf->h_addr_list = (char**) addr_list;
  1019. result_buf->h_aliases = alias + 2;
  1020. *result=result_buf;
  1021. ret=NETDB_SUCCESS;
  1022. #ifdef __UCLIBC_HAS_IPV6__
  1023. } else if (type == AF_INET6 && inet_pton(AF_INET6, alias[0], in6) > 0) {
  1024. DPRINTF("Found INET6\n");
  1025. addr_list6[0] = in6;
  1026. addr_list6[1] = 0;
  1027. result_buf->h_name = alias[1];
  1028. result_buf->h_addrtype = AF_INET6;
  1029. result_buf->h_length = sizeof(*in6);
  1030. result_buf->h_addr_list = (char**) addr_list6;
  1031. result_buf->h_aliases = alias + 2;
  1032. *result=result_buf;
  1033. ret=NETDB_SUCCESS;
  1034. #endif /* __UCLIBC_HAS_IPV6__ */
  1035. } else {
  1036. DPRINTF("Error\n");
  1037. ret=TRY_AGAIN;
  1038. break; /* bad ip address */
  1039. }
  1040. if (action!=GETHOSTENT) {
  1041. fclose(fp);
  1042. }
  1043. return ret;
  1044. }
  1045. if (action!=GETHOSTENT) {
  1046. fclose(fp);
  1047. }
  1048. return ret;
  1049. }
  1050. #endif
  1051. #ifdef L_gethostent
  1052. #ifdef __UCLIBC_HAS_THREADS__
  1053. static pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;
  1054. # define LOCK pthread_mutex_lock(&mylock)
  1055. # define UNLOCK pthread_mutex_unlock(&mylock);
  1056. #else
  1057. # define LOCK
  1058. # define UNLOCK
  1059. #endif
  1060. static int __stay_open;
  1061. static FILE * __gethostent_fp;
  1062. void endhostent (void)
  1063. {
  1064. LOCK;
  1065. __stay_open = 0;
  1066. if (__gethostent_fp) {
  1067. fclose(__gethostent_fp);
  1068. }
  1069. UNLOCK;
  1070. }
  1071. void sethostent (int stay_open)
  1072. {
  1073. LOCK;
  1074. __stay_open = stay_open;
  1075. UNLOCK;
  1076. }
  1077. struct hostent *gethostent (void)
  1078. {
  1079. static struct hostent h;
  1080. static char buf[
  1081. #ifndef __UCLIBC_HAS_IPV6__
  1082. sizeof(struct in_addr) + sizeof(struct in_addr *)*2 +
  1083. #else
  1084. sizeof(struct in6_addr) + sizeof(struct in6_addr *)*2 +
  1085. #endif /* __UCLIBC_HAS_IPV6__ */
  1086. sizeof(char *)*(ALIAS_DIM) +
  1087. 80/*namebuffer*/ + 2/* margin */];
  1088. struct hostent *host;
  1089. LOCK;
  1090. if (__gethostent_fp == NULL) {
  1091. __open_etc_hosts(&__gethostent_fp);
  1092. if (__gethostent_fp == NULL) {
  1093. UNLOCK;
  1094. return((struct hostent *)NULL);
  1095. }
  1096. }
  1097. __read_etc_hosts_r(__gethostent_fp, NULL, AF_INET, GETHOSTENT,
  1098. &h, buf, sizeof(buf), &host, &h_errno);
  1099. if (__stay_open==0) {
  1100. fclose(__gethostent_fp);
  1101. }
  1102. UNLOCK;
  1103. return(host);
  1104. }
  1105. #endif
  1106. #ifdef L_get_hosts_byname_r
  1107. int __get_hosts_byname_r(const char * name, int type,
  1108. struct hostent * result_buf,
  1109. char * buf, size_t buflen,
  1110. struct hostent ** result,
  1111. int * h_errnop)
  1112. {
  1113. return(__read_etc_hosts_r(NULL, name, type, GET_HOSTS_BYNAME, result_buf, buf, buflen, result, h_errnop));
  1114. }
  1115. #endif
  1116. #ifdef L_get_hosts_byaddr_r
  1117. int __get_hosts_byaddr_r(const char * addr, int len, int type,
  1118. struct hostent * result_buf,
  1119. char * buf, size_t buflen,
  1120. struct hostent ** result,
  1121. int * h_errnop)
  1122. {
  1123. #ifndef __UCLIBC_HAS_IPV6__
  1124. char ipaddr[INET_ADDRSTRLEN];
  1125. #else
  1126. char ipaddr[INET6_ADDRSTRLEN];
  1127. #endif /* __UCLIBC_HAS_IPV6__ */
  1128. switch (type) {
  1129. case AF_INET:
  1130. if (len != sizeof(struct in_addr))
  1131. return 0;
  1132. break;
  1133. #ifdef __UCLIBC_HAS_IPV6__
  1134. case AF_INET6:
  1135. if (len != sizeof(struct in6_addr))
  1136. return 0;
  1137. break;
  1138. #endif /* __UCLIBC_HAS_IPV6__ */
  1139. default:
  1140. return 0;
  1141. }
  1142. inet_ntop(type, addr, ipaddr, sizeof(ipaddr));
  1143. return(__read_etc_hosts_r(NULL, ipaddr, type, GET_HOSTS_BYADDR,
  1144. result_buf, buf, buflen, result, h_errnop));
  1145. }
  1146. #endif
  1147. #ifdef L_getnameinfo
  1148. #ifndef min
  1149. # define min(x,y) (((x) > (y)) ? (y) : (x))
  1150. #endif /* min */
  1151. int getnameinfo (const struct sockaddr *sa, socklen_t addrlen, char *host,
  1152. socklen_t hostlen, char *serv, socklen_t servlen,
  1153. unsigned int flags)
  1154. {
  1155. int serrno = errno;
  1156. int ok = 0;
  1157. struct hostent *h = NULL;
  1158. char domain[256];
  1159. if (flags & ~(NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|NI_DGRAM))
  1160. return EAI_BADFLAGS;
  1161. if (sa == NULL || addrlen < sizeof (sa_family_t))
  1162. return EAI_FAMILY;
  1163. switch (sa->sa_family) {
  1164. case AF_LOCAL:
  1165. break;
  1166. case AF_INET:
  1167. if (addrlen < sizeof (struct sockaddr_in))
  1168. return EAI_FAMILY;
  1169. break;
  1170. #ifdef __UCLIBC_HAS_IPV6__
  1171. case AF_INET6:
  1172. if (addrlen < sizeof (struct sockaddr_in6))
  1173. return EAI_FAMILY;
  1174. break;
  1175. #endif /* __UCLIBC_HAS_IPV6__ */
  1176. default:
  1177. return EAI_FAMILY;
  1178. }
  1179. if (host != NULL && hostlen > 0)
  1180. switch (sa->sa_family) {
  1181. case AF_INET:
  1182. #ifdef __UCLIBC_HAS_IPV6__
  1183. case AF_INET6:
  1184. #endif /* __UCLIBC_HAS_IPV6__ */
  1185. if (!(flags & NI_NUMERICHOST)) {
  1186. #ifdef __UCLIBC_HAS_IPV6__
  1187. if (sa->sa_family == AF_INET6)
  1188. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in6 *) sa)->sin6_addr),
  1189. sizeof(struct in6_addr), AF_INET6);
  1190. else
  1191. #endif /* __UCLIBC_HAS_IPV6__ */
  1192. h = gethostbyaddr ((const void *) &(((const struct sockaddr_in *)sa)->sin_addr),
  1193. sizeof(struct in_addr), AF_INET);
  1194. if (h) {
  1195. char *c;
  1196. if ((flags & NI_NOFQDN)
  1197. && (getdomainname (domain, sizeof(domain)) == 0)
  1198. && (c = strstr (h->h_name, domain))
  1199. && (c != h->h_name) && (*(--c) == '.')) {
  1200. strncpy (host, h->h_name,
  1201. min(hostlen, (size_t) (c - h->h_name)));
  1202. host[min(hostlen - 1, (size_t) (c - h->h_name))] = '\0';
  1203. ok = 1;
  1204. } else {
  1205. strncpy (host, h->h_name, hostlen);
  1206. ok = 1;
  1207. }
  1208. }
  1209. }
  1210. if (!ok) {
  1211. if (flags & NI_NAMEREQD) {
  1212. errno = serrno;
  1213. return EAI_NONAME;
  1214. } else {
  1215. const char *c;
  1216. #ifdef __UCLIBC_HAS_IPV6__
  1217. if (sa->sa_family == AF_INET6) {
  1218. const struct sockaddr_in6 *sin6p;
  1219. sin6p = (const struct sockaddr_in6 *) sa;
  1220. c = inet_ntop (AF_INET6,
  1221. (const void *) &sin6p->sin6_addr, host, hostlen);
  1222. #if 0
  1223. /* Does scope id need to be supported? */
  1224. uint32_t scopeid;
  1225. scopeid = sin6p->sin6_scope_id;
  1226. if (scopeid != 0) {
  1227. /* Buffer is >= IFNAMSIZ+1. */
  1228. char scopebuf[IFNAMSIZ + 1];
  1229. char *scopeptr;
  1230. int ni_numericscope = 0;
  1231. size_t real_hostlen = __strnlen (host, hostlen);
  1232. size_t scopelen = 0;
  1233. scopebuf[0] = SCOPE_DELIMITER;
  1234. scopebuf[1] = '\0';
  1235. scopeptr = &scopebuf[1];
  1236. if (IN6_IS_ADDR_LINKLOCAL (&sin6p->sin6_addr)
  1237. || IN6_IS_ADDR_MC_LINKLOCAL (&sin6p->sin6_addr)) {
  1238. if (if_indextoname (scopeid, scopeptr) == NULL)
  1239. ++ni_numericscope;
  1240. else
  1241. scopelen = strlen (scopebuf);
  1242. } else {
  1243. ++ni_numericscope;
  1244. }
  1245. if (ni_numericscope)
  1246. scopelen = 1 + snprintf (scopeptr,
  1247. (scopebuf
  1248. + sizeof scopebuf
  1249. - scopeptr),
  1250. "%u", scopeid);
  1251. if (real_hostlen + scopelen + 1 > hostlen)
  1252. return EAI_SYSTEM;
  1253. memcpy (host + real_hostlen, scopebuf, scopelen + 1);
  1254. }
  1255. #endif
  1256. } else
  1257. #endif /* __UCLIBC_HAS_IPV6__ */
  1258. c = inet_ntop (AF_INET,
  1259. (const void *) &(((const struct sockaddr_in *) sa)->sin_addr),
  1260. host, hostlen);
  1261. if (c == NULL) {
  1262. errno = serrno;
  1263. return EAI_SYSTEM;
  1264. }
  1265. }
  1266. ok = 1;
  1267. }
  1268. break;
  1269. case AF_LOCAL:
  1270. if (!(flags & NI_NUMERICHOST)) {
  1271. struct utsname utsname;
  1272. if (!uname (&utsname)) {
  1273. strncpy (host, utsname.nodename, hostlen);
  1274. break;
  1275. };
  1276. };
  1277. if (flags & NI_NAMEREQD) {
  1278. errno = serrno;
  1279. return EAI_NONAME;
  1280. }
  1281. strncpy (host, "localhost", hostlen);
  1282. break;
  1283. default:
  1284. return EAI_FAMILY;
  1285. }
  1286. if (serv && (servlen > 0)) {
  1287. switch (sa->sa_family) {
  1288. case AF_INET:
  1289. #ifdef __UCLIBC_HAS_IPV6__
  1290. case AF_INET6:
  1291. #endif /* __UCLIBC_HAS_IPV6__ */
  1292. if (!(flags & NI_NUMERICSERV)) {
  1293. struct servent *s;
  1294. s = getservbyport (((const struct sockaddr_in *) sa)->sin_port,
  1295. ((flags & NI_DGRAM) ? "udp" : "tcp"));
  1296. if (s) {
  1297. strncpy (serv, s->s_name, servlen);
  1298. break;
  1299. }
  1300. }
  1301. snprintf (serv, servlen, "%d",
  1302. ntohs (((const struct sockaddr_in *) sa)->sin_port));
  1303. break;
  1304. case AF_LOCAL:
  1305. strncpy (serv, ((const struct sockaddr_un *) sa)->sun_path, servlen);
  1306. break;
  1307. }
  1308. }
  1309. if (host && (hostlen > 0))
  1310. host[hostlen-1] = 0;
  1311. if (serv && (servlen > 0))
  1312. serv[servlen-1] = 0;
  1313. errno = serrno;
  1314. return 0;
  1315. }
  1316. #endif
  1317. #ifdef L_gethostbyname_r
  1318. int gethostbyname_r(const char * name,
  1319. struct hostent * result_buf,
  1320. char * buf, size_t buflen,
  1321. struct hostent ** result,
  1322. int * h_errnop)
  1323. {
  1324. struct in_addr *in;
  1325. struct in_addr **addr_list;
  1326. unsigned char *packet;
  1327. struct resolv_answer a;
  1328. int i;
  1329. int nest = 0;
  1330. int __nameserversXX;
  1331. char ** __nameserverXX;
  1332. __open_nameservers();
  1333. *result=NULL;
  1334. if (!name)
  1335. return EINVAL;
  1336. /* do /etc/hosts first */
  1337. if ((i=__get_hosts_byname_r(name, AF_INET, result_buf,
  1338. buf, buflen, result, h_errnop))==0)
  1339. return i;
  1340. switch (*h_errnop) {
  1341. case HOST_NOT_FOUND:
  1342. case NO_ADDRESS:
  1343. break;
  1344. case NETDB_INTERNAL:
  1345. if (errno == ENOENT) {
  1346. break;
  1347. }
  1348. /* else fall through */
  1349. default:
  1350. return i;
  1351. }
  1352. DPRINTF("Nothing found in /etc/hosts\n");
  1353. *h_errnop = NETDB_INTERNAL;
  1354. if (buflen < sizeof(*in))
  1355. return ERANGE;
  1356. in=(struct in_addr*)buf;
  1357. buf+=sizeof(*in);
  1358. buflen-=sizeof(*in);
  1359. if (buflen < sizeof(*addr_list)*2)
  1360. return ERANGE;
  1361. addr_list=(struct in_addr**)buf;
  1362. buf+=sizeof(*addr_list)*2;
  1363. buflen-=sizeof(*addr_list)*2;
  1364. addr_list[0] = in;
  1365. addr_list[1] = 0;
  1366. if (buflen<256)
  1367. return ERANGE;
  1368. strncpy(buf, name, buflen);
  1369. /* First check if this is already an address */
  1370. if (inet_aton(name, in)) {
  1371. result_buf->h_name = buf;
  1372. result_buf->h_addrtype = AF_INET;
  1373. result_buf->h_length = sizeof(*in);
  1374. result_buf->h_addr_list = (char **) addr_list;
  1375. *result=result_buf;
  1376. *h_errnop = NETDB_SUCCESS;
  1377. return NETDB_SUCCESS;
  1378. }
  1379. for (;;) {
  1380. BIGLOCK;
  1381. __nameserversXX=__nameservers;
  1382. __nameserverXX=__nameserver;
  1383. BIGUNLOCK;
  1384. i = __dns_lookup(buf, T_A, __nameserversXX, __nameserverXX, &packet, &a);
  1385. if (i < 0) {
  1386. *h_errnop = HOST_NOT_FOUND;
  1387. DPRINTF("__dns_lookup\n");
  1388. return TRY_AGAIN;
  1389. }
  1390. strncpy(buf, a.dotted, buflen);
  1391. free(a.dotted);
  1392. if (a.atype == T_CNAME) { /* CNAME */
  1393. DPRINTF("Got a CNAME in gethostbyname()\n");
  1394. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1395. free(packet);
  1396. if (i < 0) {
  1397. *h_errnop = NO_RECOVERY;
  1398. DPRINTF("__decode_dotted\n");
  1399. return -1;
  1400. }
  1401. if (++nest > MAX_RECURSE) {
  1402. *h_errnop = NO_RECOVERY;
  1403. DPRINTF("recursion\n");
  1404. return -1;
  1405. }
  1406. continue;
  1407. } else if (a.atype == T_A) { /* ADDRESS */
  1408. memcpy(in, a.rdata, sizeof(*in));
  1409. result_buf->h_name = buf;
  1410. result_buf->h_addrtype = AF_INET;
  1411. result_buf->h_length = sizeof(*in);
  1412. result_buf->h_addr_list = (char **) addr_list;
  1413. free(packet);
  1414. break;
  1415. } else {
  1416. free(packet);
  1417. *h_errnop=HOST_NOT_FOUND;
  1418. return TRY_AGAIN;
  1419. }
  1420. }
  1421. *result=result_buf;
  1422. return NETDB_SUCCESS;
  1423. }
  1424. #endif
  1425. #ifdef L_gethostbyname2_r
  1426. int gethostbyname2_r(const char *name, int family,
  1427. struct hostent * result_buf,
  1428. char * buf, size_t buflen,
  1429. struct hostent ** result,
  1430. int * h_errnop)
  1431. {
  1432. #ifndef __UCLIBC_HAS_IPV6__
  1433. return family == AF_INET ? gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop) : HOST_NOT_FOUND;
  1434. #else /* __UCLIBC_HAS_IPV6__ */
  1435. struct in6_addr *in;
  1436. struct in6_addr **addr_list;
  1437. unsigned char *packet;
  1438. struct resolv_answer a;
  1439. int i;
  1440. int nest = 0;
  1441. int __nameserversXX;
  1442. char ** __nameserverXX;
  1443. if (family == AF_INET)
  1444. return gethostbyname_r(name, result_buf, buf, buflen, result, h_errnop);
  1445. if (family != AF_INET6)
  1446. return EINVAL;
  1447. __open_nameservers();
  1448. *result=NULL;
  1449. if (!name)
  1450. return EINVAL;
  1451. /* do /etc/hosts first */
  1452. if ((i=__get_hosts_byname_r(name, family, result_buf,
  1453. buf, buflen, result, h_errnop))==0)
  1454. return i;
  1455. switch (*h_errnop) {
  1456. case HOST_NOT_FOUND:
  1457. case NO_ADDRESS:
  1458. break;
  1459. default:
  1460. return i;
  1461. }
  1462. DPRINTF("Nothing found in /etc/hosts\n");
  1463. *h_errnop = NETDB_INTERNAL;
  1464. if (buflen < sizeof(*in))
  1465. return ERANGE;
  1466. in=(struct in6_addr*)buf;
  1467. buf+=sizeof(*in);
  1468. buflen-=sizeof(*in);
  1469. if (buflen < sizeof(*addr_list)*2)
  1470. return ERANGE;
  1471. addr_list=(struct in6_addr**)buf;
  1472. buf+=sizeof(*addr_list)*2;
  1473. buflen-=sizeof(*addr_list)*2;
  1474. addr_list[0] = in;
  1475. addr_list[1] = 0;
  1476. if (buflen<256)
  1477. return ERANGE;
  1478. strncpy(buf, name, buflen);
  1479. /* First check if this is already an address */
  1480. if (inet_pton(AF_INET6, name, in)) {
  1481. result_buf->h_name = buf;
  1482. result_buf->h_addrtype = AF_INET6;
  1483. result_buf->h_length = sizeof(*in);
  1484. result_buf->h_addr_list = (char **) addr_list;
  1485. *result=result_buf;
  1486. *h_errnop = NETDB_SUCCESS;
  1487. return NETDB_SUCCESS;
  1488. }
  1489. for (;;) {
  1490. BIGLOCK;
  1491. __nameserversXX=__nameservers;
  1492. __nameserverXX=__nameserver;
  1493. BIGUNLOCK;
  1494. i = __dns_lookup(buf, T_AAAA, __nameserversXX, __nameserverXX, &packet, &a);
  1495. if (i < 0) {
  1496. *h_errnop = HOST_NOT_FOUND;
  1497. return TRY_AGAIN;
  1498. }
  1499. strncpy(buf, a.dotted, buflen);
  1500. free(a.dotted);
  1501. if (a.atype == T_CNAME) { /* CNAME */
  1502. DPRINTF("Got a CNAME in gethostbyname()\n");
  1503. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1504. free(packet);
  1505. if (i < 0) {
  1506. *h_errnop = NO_RECOVERY;
  1507. return -1;
  1508. }
  1509. if (++nest > MAX_RECURSE) {
  1510. *h_errnop = NO_RECOVERY;
  1511. return -1;
  1512. }
  1513. continue;
  1514. } else if (a.atype == T_AAAA) { /* ADDRESS */
  1515. memcpy(in, a.rdata, sizeof(*in));
  1516. result_buf->h_name = buf;
  1517. result_buf->h_addrtype = AF_INET6;
  1518. result_buf->h_length = sizeof(*in);
  1519. result_buf->h_addr_list = (char **) addr_list;
  1520. free(packet);
  1521. break;
  1522. } else {
  1523. free(packet);
  1524. *h_errnop=HOST_NOT_FOUND;
  1525. return TRY_AGAIN;
  1526. }
  1527. }
  1528. *result=result_buf;
  1529. return NETDB_SUCCESS;
  1530. #endif /* __UCLIBC_HAS_IPV6__ */
  1531. }
  1532. #endif
  1533. #ifdef L_gethostbyaddr_r
  1534. int gethostbyaddr_r (const void *addr, socklen_t len, int type,
  1535. struct hostent * result_buf,
  1536. char * buf, size_t buflen,
  1537. struct hostent ** result,
  1538. int * h_errnop)
  1539. {
  1540. struct in_addr *in;
  1541. struct in_addr **addr_list;
  1542. #ifdef __UCLIBC_HAS_IPV6__
  1543. char *qp;
  1544. size_t plen;
  1545. struct in6_addr *in6;
  1546. struct in6_addr **addr_list6;
  1547. #endif /* __UCLIBC_HAS_IPV6__ */
  1548. unsigned char *packet;
  1549. struct resolv_answer a;
  1550. int i;
  1551. int nest = 0;
  1552. int __nameserversXX;
  1553. char ** __nameserverXX;
  1554. *result=NULL;
  1555. if (!addr)
  1556. return EINVAL;
  1557. switch (type) {
  1558. case AF_INET:
  1559. if (len != sizeof(struct in_addr))
  1560. return EINVAL;
  1561. break;
  1562. #ifdef __UCLIBC_HAS_IPV6__
  1563. case AF_INET6:
  1564. if (len != sizeof(struct in6_addr))
  1565. return EINVAL;
  1566. break;
  1567. #endif /* __UCLIBC_HAS_IPV6__ */
  1568. default:
  1569. return EINVAL;
  1570. }
  1571. /* do /etc/hosts first */
  1572. if ((i=__get_hosts_byaddr_r(addr, len, type, result_buf,
  1573. buf, buflen, result, h_errnop))==0)
  1574. return i;
  1575. switch (*h_errnop) {
  1576. case HOST_NOT_FOUND:
  1577. case NO_ADDRESS:
  1578. break;
  1579. default:
  1580. return i;
  1581. }
  1582. __open_nameservers();
  1583. #ifdef __UCLIBC_HAS_IPV6__
  1584. qp=buf;
  1585. plen=buflen;
  1586. #endif /* __UCLIBC_HAS_IPV6__ */
  1587. *h_errnop = NETDB_INTERNAL;
  1588. if (buflen < sizeof(*in))
  1589. return ERANGE;
  1590. in=(struct in_addr*)buf;
  1591. buf+=sizeof(*in);
  1592. buflen-=sizeof(*in);
  1593. if (buflen < sizeof(*addr_list)*2)
  1594. return ERANGE;
  1595. addr_list=(struct in_addr**)buf;
  1596. buf+=sizeof(*addr_list)*2;
  1597. buflen-=sizeof(*addr_list)*2;
  1598. #ifdef __UCLIBC_HAS_IPV6__
  1599. if (plen < sizeof(*in6))
  1600. return ERANGE;
  1601. in6=(struct in6_addr*)qp;
  1602. qp+=sizeof(*in6);
  1603. plen-=sizeof(*in6);
  1604. if (plen < sizeof(*addr_list6)*2)
  1605. return ERANGE;
  1606. addr_list6=(struct in6_addr**)qp;
  1607. qp+=sizeof(*addr_list6)*2;
  1608. plen-=sizeof(*addr_list6)*2;
  1609. if (plen < buflen) {
  1610. buflen=plen;
  1611. buf=qp;
  1612. }
  1613. #endif /* __UCLIBC_HAS_IPV6__ */
  1614. if (buflen<256)
  1615. return ERANGE;
  1616. if(type == AF_INET) {
  1617. unsigned char *tmp_addr = (unsigned char *)addr;
  1618. memcpy(&in->s_addr, addr, len);
  1619. addr_list[0] = in;
  1620. sprintf(buf, "%u.%u.%u.%u.in-addr.arpa",
  1621. tmp_addr[3], tmp_addr[2], tmp_addr[1], tmp_addr[0]);
  1622. #ifdef __UCLIBC_HAS_IPV6__
  1623. } else {
  1624. memcpy(in6->s6_addr, addr, len);
  1625. addr_list6[0] = in6;
  1626. qp = buf;
  1627. for (i = len - 1; i >= 0; i--) {
  1628. qp += sprintf(qp, "%x.%x.", in6->s6_addr[i] & 0xf,
  1629. (in6->s6_addr[i] >> 4) & 0xf);
  1630. }
  1631. strcpy(qp, "ip6.int");
  1632. #endif /* __UCLIBC_HAS_IPV6__ */
  1633. }
  1634. addr_list[1] = 0;
  1635. for (;;) {
  1636. BIGLOCK;
  1637. __nameserversXX=__nameservers;
  1638. __nameserverXX=__nameserver;
  1639. BIGUNLOCK;
  1640. i = __dns_lookup(buf, T_PTR, __nameserversXX, __nameserverXX, &packet, &a);
  1641. if (i < 0) {
  1642. *h_errnop = HOST_NOT_FOUND;
  1643. return TRY_AGAIN;
  1644. }
  1645. strncpy(buf, a.dotted, buflen);
  1646. free(a.dotted);
  1647. if (a.atype == T_CNAME) { /* CNAME */
  1648. DPRINTF("Got a CNAME in gethostbyaddr()\n");
  1649. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1650. free(packet);
  1651. if (i < 0) {
  1652. *h_errnop = NO_RECOVERY;
  1653. return -1;
  1654. }
  1655. if (++nest > MAX_RECURSE) {
  1656. *h_errnop = NO_RECOVERY;
  1657. return -1;
  1658. }
  1659. continue;
  1660. } else if (a.atype == T_PTR) { /* ADDRESS */
  1661. i = __decode_dotted(packet, a.rdoffset, buf, buflen);
  1662. free(packet);
  1663. result_buf->h_name = buf;
  1664. result_buf->h_addrtype = type;
  1665. if(type == AF_INET) {
  1666. result_buf->h_length = sizeof(*in);
  1667. #ifdef __UCLIBC_HAS_IPV6__
  1668. } else {
  1669. result_buf->h_length = sizeof(*in6);
  1670. #endif /* __UCLIBC_HAS_IPV6__ */
  1671. }
  1672. result_buf->h_addr_list = (char **) addr_list;
  1673. break;
  1674. } else {
  1675. free(packet);
  1676. *h_errnop = NO_ADDRESS;
  1677. return TRY_AGAIN;
  1678. }
  1679. }
  1680. *result=result_buf;
  1681. return NETDB_SUCCESS;
  1682. }
  1683. #endif