resolv.c 43 KB

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