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