resolv.c 44 KB

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