PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/commands/nonamed/nonamed.c

http://www.minix3.org/
C | 2308 lines | 1849 code | 252 blank | 207 comment | 542 complexity | 24b5e51156e0acc06b53816b4edef30f MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD

Large files files are truncated, but you can click here to view the full file

  1. /* nonamed - Not a name daemon, but plays one on TV.
  2. * Author: Kees J. Bot
  3. * 29 Nov 1994
  4. */
  5. static const char version[] = "2.7";
  6. /* Use the file reading gethostent() family of functions. */
  7. #define sethostent _sethostent
  8. #define gethostent _gethostent
  9. #define endhostent _endhostent
  10. #define nil ((void*)0)
  11. #include <sys/types.h>
  12. #include <stdio.h>
  13. #include <syslog.h>
  14. #include <stddef.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <fcntl.h>
  18. #include <errno.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <limits.h>
  22. #include <signal.h>
  23. #include <assert.h>
  24. #include <sys/stat.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/asynchio.h>
  27. #ifdef __NBSD_LIBC
  28. #include <netinet/in.h>
  29. #include <arpa/nameser.h>
  30. #endif
  31. #include <net/hton.h>
  32. #include <net/netlib.h>
  33. #include <net/gen/in.h>
  34. #include <net/gen/inet.h>
  35. #include <net/gen/nameser.h>
  36. #include <net/gen/resolv.h>
  37. #include <net/gen/netdb.h>
  38. #include <net/gen/socket.h>
  39. #include <net/gen/tcp.h>
  40. #include <net/gen/tcp_io.h>
  41. #include <net/gen/udp.h>
  42. #include <net/gen/udp_hdr.h>
  43. #include <net/gen/udp_io.h>
  44. #include <net/gen/dhcp.h>
  45. #include <paths.h>
  46. #ifdef __NBSD_LIBC
  47. #undef HTONL
  48. #undef HTONS
  49. #define HTONL htonl
  50. #define HTONS htons
  51. #endif
  52. #define HTTL 3600L /* Default time to live for /etc/hosts data. */
  53. #define SHORT_TIMEOUT 2 /* If you expect an answer soon. */
  54. #define MEDIUM_TIMEOUT 4 /* Soon, but not that soon. */
  55. #define LONG_TIMEOUT 300 /* For stream connections to a real named. */
  56. #define N_IDS 256 /* Keep track of this many queries. */
  57. #define N_DATAMAX (4096*sizeof(char *)) /* Default response cache size. */
  58. #define N_NAMEDS 8 /* Max # name daemons we can keep track of. */
  59. #define NO_FD (-1) /* No name daemon channel here. */
  60. #define T_NXD ((u16_t) -1) /* A "type" signalling a nonexistent domain. */
  61. /* Can't do async I/O under standard Minix, so forget about TCP. */
  62. #define DO_TCP (__minix_vmd || !__minix)
  63. /* Host data, file to store our process id in, our cache, DHCP's cache. */
  64. static char HOSTS[]= _PATH_HOSTS;
  65. static char PIDFILE[]= "/usr/run/nonamed.pid";
  66. static char NNCACHE[]= "/usr/adm/nonamed.cache";
  67. static char DHCPCACHE[]= _PATH_DHCPCACHE;
  68. /* Magic string to head the cache file. */
  69. static char MAGIC[4]= "NND\2";
  70. #define arraysize(a) (sizeof(a) / sizeof((a)[0]))
  71. #define arraylimit(a) ((a) + arraysize(a))
  72. #define between(a, c, z) ((unsigned) ((c) - (a)) <= (unsigned) ((z) - (a)))
  73. /* The start of time and the far future. */
  74. #define IMMEDIATE ((time_t) 0)
  75. #define NEVER ((time_t) ((time_t) -1 < 0 ? LONG_MAX : ULONG_MAX))
  76. static unsigned debug; /* Debug level. */
  77. static time_t now; /* Current time. */
  78. static u32_t stale; /* Extension time for stale data. */
  79. static u32_t httl; /* TTL for /etc/hosts data. */
  80. static int reinit, done; /* Reinit config / program is done. */
  81. static int single; /* Run single on a nondefault interface. */
  82. static int localonly; /* Only accept local queries. */
  83. #define LOCALHOST 0x7F000001
  84. static void report(const char *label)
  85. {
  86. fprintf(stderr, "nonamed: %s: %s\n", label, strerror(errno));
  87. }
  88. static void fatal(const char *label)
  89. {
  90. report(label);
  91. if (debug >= 3) { fflush(nil); abort(); }
  92. exit(1);
  93. }
  94. static void *allocate(void *mem, size_t size)
  95. {
  96. if ((mem= realloc(mem, size)) == nil) fatal("malloc()");
  97. return mem;
  98. }
  99. static void deallocate(void *mem)
  100. {
  101. free(mem);
  102. }
  103. static char *timegmt(time_t t)
  104. /* Simple "time in seconds to GMT time today" converter. */
  105. {
  106. unsigned h, m, s;
  107. static char asctime[sizeof("00:00:00")];
  108. s= t % 60;
  109. t /= 60;
  110. m= t % 60;
  111. t /= 60;
  112. h= t % 24;
  113. sprintf(asctime, "%02u:%02u:%02u", h, m, s);
  114. return asctime;
  115. }
  116. static char *nowgmt(void)
  117. {
  118. return timegmt(now);
  119. }
  120. #define PC(n) ((void) sizeof(char [sizeof(*(n)) == 1]), (char *) (n))
  121. #define namecpy(n1, n2) strcpy(PC(n1), PC(n2))
  122. #define namecat(n1, n2) strcat(PC(n1), PC(n2))
  123. #define namechr(n, c) ((u8_t *) strchr(PC(n), (c)))
  124. #define namecmp(n1, n2) strcasecmp(PC(n1), PC(n2))
  125. #define namencmp(n1, n2, len) strncasecmp(PC(n1), PC(n2), len)
  126. typedef struct dns { /* A DNS packet. */
  127. HEADER hdr; /* DNS header. */
  128. u8_t data[PACKETSZ - sizeof(HEADER)]; /* DNS data. */
  129. } dns_t;
  130. /* Addres of DNS packet to octet address, or vv. */
  131. #define dns2oct(dp) ((u8_t *) (dp))
  132. #define oct2dns(dp) ((dns_t *) (dp))
  133. typedef struct query { /* One cached answer to a query. */
  134. struct query *less; /* Less recently used. */
  135. struct query *more; /* More recently used. */
  136. time_t age; /* Time it was added. */
  137. time_t stale; /* Time it goes stale by TTL. */
  138. u16_t usage; /* Counts of queries answered. */
  139. u8_t flags; /* QF_REFRESH. */
  140. size_t size; /* Size of DNS packet. */
  141. dns_t dns; /* Answer to query as a DNS packet. */
  142. } query_t;
  143. #define QF_REFRESH 0x01 /* This stale data must be refreshed. */
  144. #define QU_SHIFT 1 /* To shift usage by when evicting. */
  145. /* Size of new query_t or existing query_t. */
  146. #define query_allocsize(dnssize) (offsetof(query_t, dns) + (dnssize))
  147. #define query_size(qp) query_allocsize((qp)->size)
  148. static query_t *mru, *lru; /* Most and least recently used answers. */
  149. static int q_refresh; /* Set when an entry needs refreshing. */
  150. static void pack16(u8_t *buf, u16_t s)
  151. /* Pack a 16 bit value into a byte array. */
  152. {
  153. buf[0]= ((u8_t *) &s)[0];
  154. buf[1]= ((u8_t *) &s)[1];
  155. }
  156. static void pack32(u8_t *buf, u32_t l)
  157. /* Pack a 32 bit value into a byte array. */
  158. {
  159. buf[0]= ((u8_t *) &l)[0];
  160. buf[1]= ((u8_t *) &l)[1];
  161. buf[2]= ((u8_t *) &l)[2];
  162. buf[3]= ((u8_t *) &l)[3];
  163. }
  164. static u16_t upack16(u8_t *buf)
  165. /* Unpack a 16 bit value from a byte array. */
  166. {
  167. u16_t s;
  168. ((u8_t *) &s)[0]= buf[0];
  169. ((u8_t *) &s)[1]= buf[1];
  170. return s;
  171. }
  172. static u32_t upack32(u8_t *buf)
  173. /* Unpack a 32 bit value from a byte array. */
  174. {
  175. u32_t l;
  176. ((u8_t *) &l)[0]= buf[0];
  177. ((u8_t *) &l)[1]= buf[1];
  178. ((u8_t *) &l)[2]= buf[2];
  179. ((u8_t *) &l)[3]= buf[3];
  180. return l;
  181. }
  182. /* Encoding of RRs: i(paddr), d(omain), l(ong), c(har), s(tring), (s)h(ort). */
  183. static char *encoding[] = {
  184. "c*", /* anything unknown is c* */
  185. "i", /* A */
  186. "d", /* NS */
  187. "d", /* MD */
  188. "d", /* MF */
  189. "d", /* CNAME */
  190. "ddlllll", /* SOA */
  191. "d", /* MB */
  192. "d", /* MG */
  193. "d", /* MR */
  194. "c*", /* NULL */
  195. "icc*", /* WKS */
  196. "d", /* PTR */
  197. "ss", /* HINFO */
  198. "dd", /* MINFO */
  199. "hd", /* MX */
  200. "s*", /* TXT */
  201. };
  202. static char *itoa(char *fmt, u32_t i)
  203. {
  204. static char output[32 + 3 * sizeof(i)];
  205. sprintf(output, fmt, (unsigned long) i);
  206. return output;
  207. }
  208. static char *classname(unsigned class)
  209. /* Class name of a resource record, for debug purposes. */
  210. {
  211. static char *classes[] = { "IN", "CS", "CHAOS", "HS" };
  212. if ((class - C_IN) < arraysize(classes)) return classes[class - C_IN];
  213. return itoa("C_%u", class);
  214. }
  215. static char *typename(unsigned type)
  216. /* Type name of a resource record, for debug purposes. */
  217. {
  218. static char type_A[][6] = {
  219. "A", "NS", "MD", "MF", "CNAME", "SOA", "MB", "MG", "MR", "NULL",
  220. "WKS", "PTR", "HINFO", "MINFO", "MX", "TXT",
  221. };
  222. static char type_AXFR[][6] = {
  223. "AXFR", "MAILB", "MAILA", "ANY",
  224. };
  225. if ((type - T_A) < arraysize(type_A)) return type_A[type - T_A];
  226. if ((type - T_AXFR) < arraysize(type_AXFR)) return type_AXFR[type - T_AXFR];
  227. return itoa("T_%u", type);
  228. }
  229. static int print_qrr(dns_t *dp, size_t size, u8_t *cp0, int q)
  230. /* Print a query (q) or resource record (!q) from 'cp0' in a DNS packet for
  231. * debug purposes. Return number of bytes skipped or -1 on error.
  232. */
  233. {
  234. u8_t name[MAXDNAME+1];
  235. u8_t *cp;
  236. char *ep;
  237. u8_t *dlim, *rlim;
  238. u16_t type, class, rdlength;
  239. u32_t ttl;
  240. int r;
  241. cp= cp0;
  242. dlim= dns2oct(dp) + size;
  243. r= dn_expand(dns2oct(dp), dlim, cp, name, MAXDNAME);
  244. if (r == -1) return -1;
  245. cp += r;
  246. if (cp + 2 * sizeof(u16_t) > dlim) return -1;
  247. type= ntohs(upack16(cp));
  248. cp += sizeof(u16_t);
  249. class= ntohs(upack16(cp));
  250. cp += sizeof(u16_t);
  251. printf("%-25s", (char *) name);
  252. if (q) {
  253. /* We're just printing a query segment, stop right here. */
  254. printf(" %8s", classname(class));
  255. printf(" %-5s", typename(type));
  256. return cp - cp0;
  257. }
  258. if (cp + sizeof(u32_t) + sizeof(u16_t) > dlim) return -1;
  259. ttl= ntohl(upack32(cp));
  260. cp += sizeof(u32_t);
  261. rdlength= ntohs(upack16(cp));
  262. cp += sizeof(u16_t);
  263. if (cp + rdlength > dlim) return -1;
  264. rlim = cp + rdlength;
  265. printf(" %5lu", (unsigned long) ttl);
  266. printf(" %s", classname(class));
  267. printf(" %-5s", typename(type));
  268. ep= type < arraysize(encoding) ? encoding[type] : encoding[0];
  269. while (*ep != 0) {
  270. switch (*ep++) {
  271. case 'i':
  272. if (cp + sizeof(u32_t) > rlim) return -1;
  273. printf(" %s", inet_ntoa(upack32(cp)));
  274. cp += sizeof(u32_t);
  275. break;
  276. case 'l':
  277. if (cp + sizeof(u32_t) > rlim) return -1;
  278. printf(" %ld", (long)(i32_t) ntohl(upack32(cp)));
  279. cp += sizeof(u32_t);
  280. break;
  281. case 'd':
  282. r= dn_expand(dns2oct(dp), dlim, cp, name, MAXDNAME);
  283. if (r == -1) return -1;
  284. printf(" %s", (char *) name);
  285. cp += r;
  286. break;
  287. case 'c':
  288. if (cp >= rlim) return -1;
  289. printf(" %02X", *cp++);
  290. break;
  291. case 's':
  292. r= *cp + 1;
  293. if (cp + r > rlim) return -1;
  294. printf(" \"%.*s\"", *cp, (char *) (cp + 1));
  295. cp += r;
  296. break;
  297. case 'h':
  298. if (cp + sizeof(u16_t) > rlim) return -1;
  299. printf(" %u", ntohs(upack16(cp)));
  300. cp += sizeof(u16_t);
  301. break;
  302. }
  303. if (*ep == '*') ep= cp < rlim ? ep-1 : ep+1;
  304. }
  305. return cp - cp0;
  306. }
  307. static void dns_tell(int indent, dns_t *dp, size_t size)
  308. /* Explain a DNS packet, for debug purposes. */
  309. {
  310. u8_t *cp;
  311. int r, i;
  312. unsigned count[4];
  313. static char label[4][4]= { "QD:", "AN:", "NS:", "AR:" };
  314. static char rcodes[][9] = {
  315. "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP", "REFUSED"
  316. };
  317. if (size < sizeof(HEADER)) return;
  318. printf("%*s", indent, "");
  319. #ifdef __NBSD_LIBC
  320. printf("DNS %s:", (dp->hdr.qr) ? "reply" : "query");
  321. r = dp->hdr.rcode;
  322. #else
  323. printf("DNS %s:", (dp->hdr.dh_flag1 & DHF_QR) ? "reply" : "query");
  324. r= dp->hdr.dh_flag2 & DHF_RCODE;
  325. #endif
  326. printf(" %s", r < arraysize(rcodes) ? rcodes[r] : itoa("ERR_%lu", r));
  327. #ifdef __NBSD_LIBC
  328. if (dp->hdr.aa) printf(" AA");
  329. if (dp->hdr.tc) printf(" TC");
  330. if (dp->hdr.rd) printf(" RD");
  331. if (dp->hdr.ra) printf(" RA");
  332. if (dp->hdr.ad) printf(" AD");
  333. if (dp->hdr.cd) printf(" CD");
  334. #else
  335. if (dp->hdr.dh_flag1 & DHF_AA) printf(" AA");
  336. if (dp->hdr.dh_flag1 & DHF_TC) printf(" TC");
  337. if (dp->hdr.dh_flag1 & DHF_RD) printf(" RD");
  338. if (dp->hdr.dh_flag2 & DHF_RA) printf(" RA");
  339. #ifdef DHF_AD
  340. if (dp->hdr.dh_flag2 & DHF_AD) printf(" AD");
  341. if (dp->hdr.dh_flag2 & DHF_CD) printf(" CD");
  342. #endif
  343. #endif
  344. fputc('\n', stdout);
  345. count[0]= ntohs(dp->hdr.dh_qdcount);
  346. count[1]= ntohs(dp->hdr.dh_ancount);
  347. count[2]= ntohs(dp->hdr.dh_nscount);
  348. count[3]= ntohs(dp->hdr.dh_arcount);
  349. cp = dp->data;
  350. for (i= 0; i < 4; i++) {
  351. while (count[i] > 0) {
  352. printf("%*s", indent, "");
  353. printf(" %s ", label[i]);
  354. r= print_qrr(dp, size, cp, (i == 0));
  355. fputc('\n', stdout);
  356. if (r == -1) return;
  357. cp += r;
  358. count[i]--;
  359. }
  360. }
  361. }
  362. static u32_t dns_ttl(dns_t *dp, size_t size, u32_t delta)
  363. /* Compute the minimum TTL of all RRs in a DNS packet and subtract delta from
  364. * all TTLs. (We are actually only interested in the minimum (delta = 0) or
  365. * the subtraction (delta > 0). It was easier to roll this into one routine.)
  366. */
  367. {
  368. u8_t *cp, *rdp, *dlim;
  369. int r, i, hasttl, hassoa;
  370. unsigned type, count[4];
  371. u32_t ttl, minimum, minttl;
  372. unsigned rcode;
  373. u8_t name[MAXDNAME+1];
  374. hasttl= hassoa= 0;
  375. minttl= 365*24*3600L;
  376. dlim= dns2oct(dp) + size;
  377. if (size < sizeof(HEADER)) return 0;
  378. #ifdef __NBSD_LIBC
  379. rcode= dp->hdr.rcode;
  380. #else
  381. rcode= dp->hdr.dh_flag2 & DHF_RCODE;
  382. #endif
  383. count[0]= ntohs(dp->hdr.dh_qdcount);
  384. count[1]= ntohs(dp->hdr.dh_ancount);
  385. count[2]= ntohs(dp->hdr.dh_nscount);
  386. count[3]= ntohs(dp->hdr.dh_arcount);
  387. cp = dp->data;
  388. for (i= 0; i < 4 && cp < dlim; i++) {
  389. while (count[i] > 0) {
  390. r= dn_expand(dns2oct(dp), dlim, cp, name, MAXDNAME);
  391. if (r == -1) break;
  392. cp += r + 2 * sizeof(u16_t);
  393. if (i != 0) {
  394. if (cp + sizeof(u32_t) + sizeof(u16_t) > dlim) break;
  395. type= upack16(cp - 2 * sizeof(u16_t));
  396. ttl= ntohl(upack32(cp));
  397. ttl= ttl < delta ? 0 : ttl - delta;
  398. if (rcode == NXDOMAIN && i == 2 && type == HTONS(T_SOA)) {
  399. rdp= cp + sizeof(u32_t) + sizeof(u16_t);
  400. r= dn_expand(dns2oct(dp), dlim, rdp, name, MAXDNAME);
  401. if (r == -1) break;
  402. rdp += r;
  403. r= dn_expand(dns2oct(dp), dlim, rdp, name, MAXDNAME);
  404. if (r == -1) break;
  405. rdp += r + 4 * sizeof(u32_t);
  406. if (rdp + sizeof(u32_t) > dlim) break;
  407. minimum= ntohl(upack32(rdp));
  408. if (ttl > minimum) ttl= minimum;
  409. hassoa= 1;
  410. }
  411. if (delta != 0) pack32(cp, htonl(ttl));
  412. if (ttl < minttl) minttl= ttl;
  413. hasttl= 1;
  414. cp += sizeof(u32_t);
  415. cp += sizeof(u16_t) + ntohs(upack16(cp));
  416. }
  417. count[i]--;
  418. }
  419. }
  420. return ((rcode == NOERROR && hasttl) || (rcode == NXDOMAIN && hassoa))
  421. ? minttl : 0;
  422. }
  423. /* Total cached query data. */
  424. static size_t n_datamax= N_DATAMAX;
  425. static size_t n_data;
  426. static query_t *extract_query(query_t *qp)
  427. /* Take a query out of the query cache. */
  428. {
  429. assert(qp != nil);
  430. *(qp->less != nil ? &qp->less->more : &lru) = qp->more;
  431. *(qp->more != nil ? &qp->more->less : &mru) = qp->less;
  432. n_data -= query_size(qp);
  433. return qp;
  434. }
  435. static query_t *get_query(u8_t *name, unsigned type)
  436. /* Find a query and if so remove it from the cache and return it. */
  437. {
  438. query_t *qp, *less;
  439. u8_t qname[MAXDNAME+1];
  440. int r;
  441. for (qp= mru; qp != nil; qp= less) {
  442. less= qp->less;
  443. if (qp->stale <= now - stale) {
  444. /* This answer has expired. */
  445. deallocate(extract_query(qp));
  446. } else {
  447. r= dn_expand(dns2oct(&qp->dns), dns2oct(&qp->dns) + qp->size,
  448. qp->dns.data, qname, MAXDNAME);
  449. if (r == -1) continue;
  450. if (namecmp(qname, name) == 0 && upack16(qp->dns.data+r) == type) {
  451. /* Found an answer to the query. */
  452. return extract_query(qp);
  453. }
  454. }
  455. }
  456. return nil;
  457. }
  458. static void insert_query(query_t *qp)
  459. /* (Re)insert a query into the cache. */
  460. {
  461. *(qp->less != nil ? &qp->less->more : &lru) = qp;
  462. *(qp->more != nil ? &qp->more->less : &mru) = qp;
  463. n_data += query_size(qp);
  464. /* Try to delete the LRU while there is too much memory in use. If
  465. * its usage count is too high then it gets a second chance.
  466. */
  467. while (n_data > n_datamax && lru != nil) {
  468. if ((lru->usage >>= QU_SHIFT) == 0 || lru->stale <= now - stale) {
  469. deallocate(extract_query(lru));
  470. } else {
  471. lru->less= mru; /* Make list circular. */
  472. mru->more= lru;
  473. mru= lru; /* Move one over, making LRU the MRU. */
  474. lru= lru->more;
  475. lru->less= nil; /* Break the circle. */
  476. mru->more= nil;
  477. }
  478. }
  479. if (debug >= 2) {
  480. unsigned n= 0;
  481. for (qp= mru; qp != nil; qp= qp->less) n++;
  482. printf("%u cached repl%s, %u bytes, sbrk(0) = %u\n",
  483. n, n == 1 ? "y" : "ies",
  484. (unsigned) n_data,
  485. (unsigned) sbrk(0));
  486. }
  487. }
  488. static void put_query(query_t *qp)
  489. /* Add a new query to the cache as the MRU. */
  490. {
  491. qp->less= mru;
  492. qp->more= nil;
  493. insert_query(qp);
  494. }
  495. static void cache2file(void)
  496. /* Store the cached data into the cache file. */
  497. {
  498. FILE *fp;
  499. query_t *qp;
  500. u8_t data[4+1+2+2];
  501. u16_t usage;
  502. char newcache[sizeof(NNCACHE) + sizeof(".new")];
  503. if (single) return;
  504. strcpy(newcache, NNCACHE);
  505. strcat(newcache, ".new");
  506. if ((fp= fopen(newcache, "w")) == nil) {
  507. if ((errno != ENOENT && errno != EROFS) || debug >= 2) report(newcache);
  508. return;
  509. }
  510. if (debug >= 2) printf("Writing %s:\n", newcache);
  511. /* Magic number: */
  512. fwrite(MAGIC, 1, sizeof(MAGIC), fp);
  513. for (qp= lru; qp != nil; qp= qp->more) {
  514. if (qp->stale <= now - stale) continue;
  515. if (debug >= 2) {
  516. printf("Usage = %u, Age = %ld, Flags = %02X:\n",
  517. qp->usage, (long) (now - qp->age), qp->flags);
  518. dns_tell(2, &qp->dns, qp->size);
  519. }
  520. pack32(data+0, htonl(qp->age));
  521. data[4]= qp->flags;
  522. pack16(data+5, htons(qp->size));
  523. pack16(data+7, htons(qp->usage));
  524. fwrite(data, 1, sizeof(data), fp);
  525. fwrite(&qp->dns, 1, qp->size, fp);
  526. if (ferror(fp)) break;
  527. }
  528. if (ferror(fp) || fclose(fp) == EOF) {
  529. report(newcache);
  530. (void) unlink(newcache);
  531. return;
  532. }
  533. if (debug >= 2) printf("mv %s %s\n", newcache, NNCACHE);
  534. if (rename(newcache, NNCACHE) < 0) {
  535. fprintf(stderr, "nonamed: mv %s %s: %s\n",
  536. newcache, NNCACHE, strerror(errno));
  537. (void) unlink(newcache);
  538. }
  539. }
  540. static void file2cache(void)
  541. /* Read cached data from the cache file. */
  542. {
  543. query_t *qp;
  544. FILE *fp;
  545. u8_t data[4+1+2+2];
  546. size_t dlen;
  547. if (single) return;
  548. if ((fp= fopen(NNCACHE, "r")) == nil) {
  549. if (errno != ENOENT || debug >= 2) report(NNCACHE);
  550. return;
  551. }
  552. if (debug >= 2) printf("Reading %s:\n", NNCACHE);
  553. /* Magic number? */
  554. fread(data, 1, sizeof(MAGIC), fp);
  555. if (ferror(fp) || memcmp(MAGIC, data, sizeof(MAGIC)) != 0) goto err;
  556. for (;;) {
  557. fread(data, 1, sizeof(data), fp);
  558. if (feof(fp) || ferror(fp)) break;
  559. dlen= ntohs(upack16(data+5));
  560. qp= allocate(nil, query_allocsize(dlen));
  561. qp->age= htonl(upack32(data+0));
  562. qp->flags= data[4];
  563. if (qp->flags & QF_REFRESH) q_refresh= 1;
  564. qp->size= dlen;
  565. qp->usage= htons(upack16(data+7));
  566. fread(&qp->dns, 1, qp->size, fp);
  567. if (feof(fp) || ferror(fp)) {
  568. deallocate(qp);
  569. goto err;
  570. }
  571. qp->stale= qp->age + dns_ttl(&qp->dns, dlen, 0);
  572. if (debug >= 2) {
  573. printf("Usage = %u, Age = %ld, Flags = %02X:\n",
  574. qp->usage, (long) (now - qp->age), qp->flags);
  575. dns_tell(2, &qp->dns, dlen);
  576. }
  577. put_query(qp);
  578. }
  579. if (ferror(fp)) {
  580. err:
  581. /* The cache file did not end at EOF or is otherwise a mess. */
  582. fprintf(stderr, "nonamed: %s: %s\n", NNCACHE,
  583. ferror(fp) ? strerror(errno) : "Corrupt");
  584. while (lru != nil) deallocate(extract_query(lru));
  585. }
  586. fclose(fp);
  587. }
  588. typedef int handler_t(void *data, int expired);
  589. /* All actions are in the form of "jobs". */
  590. typedef struct job {
  591. struct job *next, **prev; /* To make a job queue. */
  592. handler_t *handler; /* Function to handle this job. */
  593. time_t timeout; /* Moment it times out. */
  594. void *data; /* Data associated with the job. */
  595. } job_t;
  596. static job_t *queue; /* Main job queue. */
  597. static void newjob(handler_t *handler, time_t timeout, void *data)
  598. /* Create a new job with the given handler, timeout time and data. */
  599. {
  600. job_t *job, **prev;
  601. job= allocate(nil, sizeof(*job));
  602. job->handler= handler;
  603. job->timeout= timeout;
  604. job->data= data;
  605. for (prev= &queue; *prev != nil; prev= &(*prev)->next) {
  606. if (job->timeout < (*prev)->timeout) break;
  607. }
  608. job->next= *prev;
  609. job->prev= prev;
  610. *prev= job;
  611. if (job->next != nil) job->next->prev= &job->next;
  612. }
  613. static int execjob(job_t *job, int expired)
  614. /* Execute a job by calling the handler. Remove the job if it returns true,
  615. * indicating that it is done. Expired is set if the job timed out. It is
  616. * otherwise called to check for I/O.
  617. */
  618. {
  619. if ((*job->handler)(job->data, expired)) {
  620. *job->prev= job->next;
  621. if (job->next != nil) job->next->prev= job->prev;
  622. deallocate(job);
  623. return 1;
  624. }
  625. return 0;
  626. }
  627. static void force_expire(handler_t *handler)
  628. /* Force jobs to expire immediately, the named searcher for instance. */
  629. {
  630. job_t *job, **prev= &queue;
  631. while ((job= *prev) != nil) {
  632. if (job->handler == handler && job->timeout != IMMEDIATE) {
  633. *prev= job->next;
  634. if (job->next != nil) job->next->prev= prev;
  635. newjob(job->handler, IMMEDIATE, job->data);
  636. deallocate(job);
  637. } else {
  638. prev= &job->next;
  639. }
  640. }
  641. }
  642. static int nxdomain(u8_t *name)
  643. /* True iff the two top level components in a name are repeated in the name,
  644. * or if in-addr.arpa is found within a name. Such things happen often in a
  645. * search for an already fully qualified local name. For instance:
  646. * flotsam.cs.vu.nl.cs.vu.nl. (We don't want this at boot time.)
  647. */
  648. {
  649. u8_t *end, *top, *p;
  650. size_t n;
  651. end= namechr(name, 0);
  652. top= end;
  653. while (top > name && *--top != '.') {}
  654. while (top > name && *--top != '.') {}
  655. n= end - top;
  656. p= top;
  657. for (;;) {
  658. if (p == name) return 0;
  659. if (*--p == '.') {
  660. if (namencmp(p, top, n) == 0 && p[n] == '.') return 1;
  661. if (namencmp(p, ".in-addr.arpa.", 14) == 0) return 1;
  662. }
  663. }
  664. }
  665. typedef struct id2id {
  666. u16_t id; /* ID of old query. */
  667. u16_t port; /* Reply port. */
  668. ipaddr_t ip; /* Reply address. */
  669. } id2id_t;
  670. static id2id_t id2id[N_IDS];
  671. static u16_t id_counter;
  672. static u16_t new_id(u16_t in_id, u16_t in_port, ipaddr_t in_ip)
  673. /* An incoming UDP query must be relabeled with a new ID before it can be
  674. * send on to a real name daemon.
  675. */
  676. {
  677. id2id_t *idp;
  678. u16_t id;
  679. id= id_counter++;
  680. idp= &id2id[id % N_IDS];
  681. idp->id= in_id;
  682. idp->port= in_port;
  683. idp->ip= in_ip;
  684. return htons(id);
  685. }
  686. static int old_id(u16_t id, u16_t *out_id, u16_t *out_port, ipaddr_t *out_ip)
  687. /* Translate a reply id back to the id, port, and address used in the query.
  688. * Return true if the translation is possible.
  689. */
  690. {
  691. id= ntohs(id);
  692. if ((u16_t) (id_counter - id) > N_IDS) {
  693. /* Too old. */
  694. return 0;
  695. } else {
  696. /* We know this one. */
  697. id2id_t *idp= &id2id[id % N_IDS];
  698. if (idp->port == 0) return 0; /* Named is trying to fool us? */
  699. *out_id= idp->id;
  700. *out_port= idp->port;
  701. *out_ip= idp->ip;
  702. idp->port= 0;
  703. return 1;
  704. }
  705. }
  706. /* IDs used to mark my own queries to name servers, must be new_id translated
  707. * to make them unique "on the wire".
  708. */
  709. #define ID_IPSELF HTONL(0) /* "I did it myself" address. */
  710. #define ID_PROBE HTONS(0) /* Name server probe. */
  711. #define ID_REFRESH HTONS(1) /* Query to refresh a cache entry. */
  712. static char *tcp_device, *udp_device; /* TCP and UDP device names. */
  713. static int udp_fd; /* To send or receive UDP packets. */
  714. static asynchio_t asyn; /* For I/O in progress. */
  715. static ipaddr_t my_ip; /* My IP address. */
  716. static u16_t my_port, named_port; /* Port numbers, normally "domain". */
  717. static ipaddr_t named[N_NAMEDS]; /* Addresses of all name servers. */
  718. static unsigned n_nameds; /* Number of configured name daemons. */
  719. static unsigned i_named; /* Index to current name server. */
  720. static int expect; /* Set when we expect an answer. */
  721. static int search_ct= -1; /* Named search count and state. */
  722. static int dirty; /* True when new entry put in cache. */
  723. #define current_named() (+named[i_named])
  724. #define searching() (search_ct > 0)
  725. #define start_searching() ((void) (search_ct= -1))
  726. #define stop_searching() ((void) (search_ct= 0))
  727. #define expecting() (+expect)
  728. #define start_expecting() ((void) (expect= 1))
  729. #define stop_expecting() ((void) (expect= 0))
  730. static time_t filetime(const char *file)
  731. /* Get the modified time of a file. */
  732. {
  733. struct stat st;
  734. return stat(file, &st) == 0 ? st.st_mtime : 0;
  735. }
  736. static void init_config(ipaddr_t ifip)
  737. /* Read name daemon list and other special stuff from the hosts file. */
  738. {
  739. struct hostent *he;
  740. u32_t nip, hip;
  741. static time_t hosts_time, dhcp_time;
  742. time_t ht, dt;
  743. /* See if anything really changed. */
  744. if (((ifip ^ HTONL(LOCALHOST)) & HTONL(0xFF000000)) == 0) ifip= my_ip;
  745. ht= filetime(HOSTS);
  746. dt= filetime(DHCPCACHE);
  747. if (ifip == my_ip && ht == hosts_time && dt == dhcp_time) return;
  748. my_ip= ifip;
  749. hosts_time= ht;
  750. dhcp_time= dt;
  751. if (debug >= 2) {
  752. printf("%s: I am nonamed %s at %s:%u\n",
  753. nowgmt(), version, inet_ntoa(my_ip), ntohs(my_port));
  754. }
  755. httl= HTONL(HTTL);
  756. stale= 0;
  757. n_nameds= 0;
  758. if (!single) {
  759. sethostent(0);
  760. while ((he= gethostent()) != nil) {
  761. memcpy(&nip, he->h_addr, sizeof(u32_t));
  762. hip= ntohl(nip);
  763. if (namecmp(he->h_name, "%ttl") == 0) httl= nip;
  764. if (namecmp(he->h_name, "%stale") == 0) stale= hip;
  765. if (namecmp(he->h_name, "%memory") == 0) n_datamax= hip;
  766. if (namecmp(he->h_name, "%nameserver") == 0) {
  767. if (nip != my_ip || named_port != my_port) {
  768. if (n_nameds < N_NAMEDS) named[n_nameds++]= nip;
  769. }
  770. }
  771. }
  772. endhostent();
  773. }
  774. if (n_nameds == 0) {
  775. /* No name daemons found in the host file. What about DHCP? */
  776. int fd;
  777. dhcp_t d;
  778. ssize_t r;
  779. u8_t *data;
  780. size_t len;
  781. if ((fd= open(DHCPCACHE, O_RDONLY)) < 0) {
  782. if (errno != ENOENT) fatal(DHCPCACHE);
  783. } else {
  784. while ((r= read(fd, &d, sizeof(d))) == sizeof(d)) {
  785. if (d.yiaddr == my_ip) break;
  786. }
  787. if (r < 0) fatal(DHCPCACHE);
  788. close(fd);
  789. if (r == sizeof(d) && dhcp_gettag(&d, DHCP_TAG_DNS, &data, &len)) {
  790. while (len >= sizeof(nip)) {
  791. memcpy(&nip, data, sizeof(nip));
  792. data += sizeof(nip);
  793. len -= sizeof(nip);
  794. if (nip != my_ip || named_port != my_port) {
  795. if (n_nameds < N_NAMEDS) named[n_nameds++]= nip;
  796. }
  797. }
  798. }
  799. }
  800. }
  801. i_named= 0;
  802. }
  803. static handler_t job_save_cache, job_read_udp, job_find_named, job_expect_named;
  804. #if DO_TCP
  805. static handler_t job_setup_listen, job_listen, job_setup_connect, job_connect;
  806. static handler_t job_read_query, job_write_query;
  807. static handler_t job_read_reply, job_write_reply;
  808. #endif
  809. static int query_hosts(u8_t *qname, unsigned type, dns_t *dp, size_t *pdlen)
  810. /* Read the /etc/hosts file to try and answer an A or PTR query. Return
  811. * true iff an answer can be found, with the answer copied to *dp.
  812. */
  813. {
  814. struct hostent *he;
  815. int i, r;
  816. dns_t dns;
  817. u8_t *domain;
  818. u8_t *cp;
  819. u8_t name[MAXDNAME+1];
  820. u8_t *dnvec[40];
  821. unsigned ancount;
  822. struct hostent localhost;
  823. static char *noaliases[]= { nil };
  824. static ipaddr_t localaddr;
  825. static char *localaddrlist[]= { (char *) &localaddr, nil };
  826. localaddr = HTONL(LOCALHOST);
  827. if (single) return 0;
  828. /* Assume we can answer. */
  829. #ifdef __NBSD_LIBC
  830. dns.hdr.qr = 1;
  831. dns.hdr.opcode = 0;
  832. dns.hdr.aa = 1;
  833. dns.hdr.tc = 0;
  834. dns.hdr.rd = 0;
  835. dns.hdr.ra = 1;
  836. dns.hdr.unused = 0;
  837. dns.hdr.ad = 0;
  838. dns.hdr.cd = 0;
  839. dns.hdr.rcode = 0;
  840. #else
  841. dns.hdr.dh_flag1= DHF_QR | DHF_AA;
  842. dns.hdr.dh_flag2= DHF_RA;
  843. #endif
  844. dns.hdr.dh_qdcount= HTONS(1);
  845. ancount= 0;
  846. dns.hdr.dh_nscount= HTONS(0);
  847. dns.hdr.dh_arcount= HTONS(0);
  848. dnvec[0]= dns2oct(&dns);
  849. dnvec[1]= nil;
  850. cp= dns.data;
  851. r= dn_comp(qname, cp, arraysize(dns.data), dnvec, arraylimit(dnvec));
  852. if (r == -1) return 0;
  853. cp += r;
  854. pack16(cp, type);
  855. cp += sizeof(u16_t);
  856. pack16(cp, HTONS(C_IN));
  857. cp += sizeof(u16_t);
  858. /* Localhost is fixed to 127.0.0.1. */
  859. localhost.h_name=
  860. namencmp(qname, "localhost.", 10) == 0 ? (char *) qname : "localhost";
  861. localhost.h_aliases= noaliases;
  862. localhost.h_addr_list= localaddrlist;
  863. he= &localhost;
  864. sethostent(0);
  865. do {
  866. int type_host = NTOHS(type);
  867. switch (type_host) {
  868. case T_A:
  869. if (namecmp(qname, he->h_name) == 0) {
  870. addA:
  871. r= dn_comp((u8_t *) he->h_name, cp, arraylimit(dns.data) - cp,
  872. dnvec, arraylimit(dnvec));
  873. if (r == -1) return 0;
  874. cp += r;
  875. if (cp + 3 * sizeof(u16_t) + 2 * sizeof(u32_t)
  876. > arraylimit(dns.data)) { r= -1; break; }
  877. pack16(cp, HTONS(T_A));
  878. cp += sizeof(u16_t);
  879. pack16(cp, HTONS(C_IN));
  880. cp += sizeof(u16_t);
  881. pack32(cp, httl);
  882. cp += sizeof(u32_t);
  883. pack16(cp, HTONS(sizeof(u32_t)));
  884. cp += sizeof(u16_t);
  885. memcpy(cp, he->h_addr, sizeof(u32_t));
  886. cp += sizeof(u32_t);
  887. ancount++;
  888. break;
  889. }
  890. /*FALL THROUGH*/
  891. case T_CNAME:
  892. domain= namechr(he->h_name, '.');
  893. for (i= 0; he->h_aliases[i] != nil; i++) {
  894. namecpy(name, he->h_aliases[i]);
  895. if (domain != nil && namechr(name, '.') == nil) {
  896. namecat(name, domain);
  897. }
  898. if (namecmp(qname, name) == 0) {
  899. r= dn_comp(name, cp, arraylimit(dns.data) - cp,
  900. dnvec, arraylimit(dnvec));
  901. if (r == -1) break;
  902. cp += r;
  903. if (cp + 3 * sizeof(u16_t)
  904. + 1 * sizeof(u32_t) > arraylimit(dns.data)) return 0;
  905. pack16(cp, HTONS(T_CNAME));
  906. cp += sizeof(u16_t);
  907. pack16(cp, HTONS(C_IN));
  908. cp += sizeof(u16_t);
  909. pack32(cp, httl);
  910. cp += sizeof(u32_t);
  911. /* pack16(cp, htonl(RDLENGTH)) */
  912. cp += sizeof(u16_t);
  913. r= dn_comp((u8_t *) he->h_name, cp,
  914. arraylimit(dns.data) - cp,
  915. dnvec, arraylimit(dnvec));
  916. if (r == -1) break;
  917. pack16(cp - sizeof(u16_t), htons(r));
  918. cp += r;
  919. ancount++;
  920. if (type == HTONS(T_A)) goto addA; /* really wants A */
  921. break;
  922. }
  923. }
  924. break;
  925. case T_PTR:
  926. if (ancount > 0) break;
  927. if (he->h_name[0] == '%') break;
  928. sprintf((char *) name, "%d.%d.%d.%d.in-addr.arpa",
  929. ((u8_t *) he->h_addr)[3],
  930. ((u8_t *) he->h_addr)[2],
  931. ((u8_t *) he->h_addr)[1],
  932. ((u8_t *) he->h_addr)[0]);
  933. if (namecmp(qname, name) == 0) {
  934. r= dn_comp(name, cp, arraylimit(dns.data) - cp,
  935. dnvec, arraylimit(dnvec));
  936. if (r == -1) break;
  937. cp += r;
  938. if (cp + 3 * sizeof(u16_t) + 1 * sizeof(u32_t)
  939. > arraylimit(dns.data)) { r= -1; break; }
  940. pack16(cp, HTONS(T_PTR));
  941. cp += sizeof(u16_t);
  942. pack16(cp, HTONS(C_IN));
  943. cp += sizeof(u16_t);
  944. pack32(cp, httl);
  945. cp += sizeof(u32_t);
  946. /* pack16(cp, htonl(RDLENGTH)) */
  947. cp += sizeof(u16_t);
  948. r= dn_comp((u8_t *) he->h_name, cp,
  949. arraylimit(dns.data) - cp, dnvec, arraylimit(dnvec));
  950. if (r == -1) return 0;
  951. pack16(cp - sizeof(u16_t), htons(r));
  952. cp += r;
  953. ancount++;
  954. }
  955. break;
  956. }
  957. } while (r != -1 && (he= gethostent()) != nil);
  958. endhostent();
  959. if (r == -1 || ancount == 0) return 0;
  960. dns.hdr.dh_ancount= htons(ancount);
  961. memcpy(dp, &dns, *pdlen= cp - dns2oct(&dns));
  962. return 1;
  963. }
  964. static int query_chaos(u8_t *qname, unsigned type, dns_t *dp, size_t *pdlen)
  965. /* Report my version. Can't let BIND take all the credit. :-) */
  966. {
  967. int i, n, r;
  968. dns_t dns;
  969. u8_t *cp;
  970. u8_t *dnvec[40];
  971. if (type != HTONS(T_TXT) || namecmp(qname, "version.bind") != 0) return 0;
  972. #ifdef __NBSD_LIBC
  973. dns.hdr.qr = 1;
  974. dns.hdr.opcode = 0;
  975. dns.hdr.aa = 1;
  976. dns.hdr.tc = 0;
  977. dns.hdr.rd = 0;
  978. dns.hdr.ra = 1;
  979. dns.hdr.unused = 0;
  980. dns.hdr.ad = 0;
  981. dns.hdr.cd = 0;
  982. dns.hdr.rcode = 0;
  983. #else
  984. dns.hdr.dh_flag1= DHF_QR | DHF_AA;
  985. dns.hdr.dh_flag2= DHF_RA;
  986. #endif
  987. dns.hdr.dh_qdcount= HTONS(1);
  988. dns.hdr.dh_ancount= HTONS(1);
  989. dns.hdr.dh_nscount= HTONS(0);
  990. dns.hdr.dh_arcount= htons(n_nameds);
  991. dnvec[0]= dns2oct(&dns);
  992. dnvec[1]= nil;
  993. cp= dns.data;
  994. r= dn_comp(qname, cp, arraysize(dns.data), dnvec, arraylimit(dnvec));
  995. if (r == -1) return 0;
  996. cp += r;
  997. pack16(cp, type);
  998. cp += sizeof(u16_t);
  999. pack16(cp, HTONS(C_CHAOS));
  1000. cp += sizeof(u16_t);
  1001. r= dn_comp(qname, cp, arraylimit(dns.data) - cp, dnvec, arraylimit(dnvec));
  1002. if (r == -1) return 0;
  1003. cp += r;
  1004. pack16(cp, HTONS(T_TXT));
  1005. cp += sizeof(u16_t);
  1006. pack16(cp, HTONS(C_CHAOS));
  1007. cp += sizeof(u16_t);
  1008. pack32(cp, HTONL(0));
  1009. cp += sizeof(u32_t);
  1010. /* pack16(cp, htonl(RDLENGTH)) */
  1011. cp += sizeof(u16_t);
  1012. sprintf((char *) cp + 1, "nonamed %s at %s:%u",
  1013. version, inet_ntoa(my_ip), ntohs(my_port));
  1014. r= strlen((char *) cp + 1) + 1;
  1015. pack16(cp - sizeof(u16_t), htons(r));
  1016. *cp= r-1;
  1017. cp += r;
  1018. for (n= 0, i= i_named; n < n_nameds; n++, i= (i+1) % n_nameds) {
  1019. r= dn_comp((u8_t *) "%nameserver", cp, arraylimit(dns.data) - cp,
  1020. dnvec, arraylimit(dnvec));
  1021. if (r == -1) return 0;
  1022. cp += r;
  1023. if (cp + 3 * sizeof(u16_t)
  1024. + 2 * sizeof(u32_t) > arraylimit(dns.data)) return 0;
  1025. pack16(cp, HTONS(T_A));
  1026. cp += sizeof(u16_t);
  1027. pack16(cp, HTONS(C_IN));
  1028. cp += sizeof(u16_t);
  1029. pack32(cp, HTONL(0));
  1030. cp += sizeof(u32_t);
  1031. pack16(cp, HTONS(sizeof(u32_t)));
  1032. cp += sizeof(u16_t);
  1033. memcpy(cp, &named[i], sizeof(u32_t));
  1034. cp += sizeof(u32_t);
  1035. }
  1036. memcpy(dp, &dns, *pdlen= cp - dns2oct(&dns));
  1037. return 1;
  1038. }
  1039. static void cache_reply(dns_t *dp, size_t dlen)
  1040. /* Store a DNS packet in the cache. */
  1041. {
  1042. int r;
  1043. query_t *qp, *less, *more;
  1044. unsigned usage;
  1045. u16_t type;
  1046. u8_t *cp;
  1047. u8_t name[MAXDNAME];
  1048. u32_t minttl;
  1049. #if __NBSD_LIBC
  1050. if ((dp->hdr.rd && !dp->hdr.tc)) return;
  1051. #else
  1052. if ((dp->hdr.dh_flag1 & (DHF_RD | DHF_TC)) != DHF_RD) return;
  1053. #endif
  1054. if (dp->hdr.dh_qdcount != HTONS(1)) return;
  1055. cp= dp->data;
  1056. r= dn_expand(dns2oct(dp), dns2oct(dp) + dlen, cp, name, MAXDNAME);
  1057. if (r == -1) return;
  1058. cp += r;
  1059. type= upack16(cp);
  1060. cp += sizeof(u16_t);
  1061. if (upack16(cp) != HTONS(C_IN)) return;
  1062. /* Delete old cached data, if any. Note where it is in the LRU. */
  1063. if ((qp= get_query(name, type)) != nil) {
  1064. less= qp->less;
  1065. more= qp->more;
  1066. usage= qp->usage;
  1067. deallocate(qp);
  1068. } else {
  1069. /* Not yet in the cache. */
  1070. less= mru;
  1071. more= nil;
  1072. usage= 1;
  1073. }
  1074. /* Determine minimum TTL. Discard if zero, never cache zero TTLs. */
  1075. if ((minttl= dns_ttl(dp, dlen, 0)) == 0) return;
  1076. /* Enter new reply in cache. */
  1077. qp= allocate(nil, query_allocsize(dlen));
  1078. qp->less= less;
  1079. qp->more= more;
  1080. qp->age= now;
  1081. qp->flags= 0;
  1082. qp->usage= usage;
  1083. qp->size= dlen;
  1084. memcpy(&qp->dns, dp, dlen);
  1085. qp->stale= qp->age + minttl;
  1086. insert_query(qp);
  1087. if (debug >= 1) printf("Answer cached\n");
  1088. /* Save the cache soon. */
  1089. if (!dirty) {
  1090. dirty= 1;
  1091. newjob(job_save_cache, now + LONG_TIMEOUT, nil);
  1092. }
  1093. }
  1094. static int job_save_cache(void *data, int expired)
  1095. /* Some time after the cache is changed it is written back to disk. */
  1096. {
  1097. if (!expired) return 0;
  1098. cache2file();
  1099. dirty= 0;
  1100. return 1;
  1101. }
  1102. static int compose_reply(dns_t *dp, size_t *pdlen)
  1103. /* Try to compose a reply to a request in *dp using the hosts file or
  1104. * cached data. Return answer in *dp with its size in *pdlen. Return true
  1105. * iff an answer is given.
  1106. */
  1107. {
  1108. size_t dlen= *pdlen;
  1109. int r, rd;
  1110. query_t *qp;
  1111. unsigned id, type, class;
  1112. u8_t *cp;
  1113. u8_t name[MAXDNAME];
  1114. cp= dp->data;
  1115. r= dn_expand(dns2oct(dp), dns2oct(dp) + dlen, cp, name, MAXDNAME);
  1116. if (r != -1) {
  1117. cp += r;
  1118. if (cp + 2 * sizeof(u16_t) > dns2oct(dp) + dlen) {
  1119. r= -1;
  1120. } else {
  1121. type= upack16(cp);
  1122. cp += sizeof(u16_t);
  1123. class= upack16(cp);
  1124. cp += sizeof(u16_t);
  1125. }
  1126. }
  1127. /* Remember ID and RD. */
  1128. id= dp->hdr.dh_id;
  1129. #ifdef __NBSD_LIBC
  1130. rd= dp->hdr.rd;
  1131. #else
  1132. rd= dp->hdr.dh_flag1 & DHF_RD;
  1133. #endif
  1134. if (r == -1) {
  1135. /* Malformed query, reply "FORMERR". */
  1136. #ifdef __NBSD_LIBC
  1137. dp->hdr.tc = 0;
  1138. dp->hdr.qr = 1;
  1139. dp->hdr.aa = 1;
  1140. dp->hdr.unused = 0;
  1141. dp->hdr.ra = 1;
  1142. dp->hdr.rcode = FORMERR;
  1143. #else
  1144. dp->hdr.dh_flag1 &= ~(DHF_TC);
  1145. dp->hdr.dh_flag1 |= DHF_QR | DHF_AA;
  1146. dp->hdr.dh_flag2 &= ~(DHF_UNUSED | DHF_RCODE);
  1147. dp->hdr.dh_flag2 |= DHF_RA | FORMERR;
  1148. #endif
  1149. } else
  1150. if (class == HTONS(C_IN) && query_hosts(name, type, dp, pdlen)) {
  1151. /* Answer to this query is in the hosts file. */
  1152. dlen= *pdlen;
  1153. } else
  1154. if (class == HTONS(C_IN) && (qp= get_query(name, type)) != nil) {
  1155. /* Answer to this query is present in the cache. */
  1156. memcpy(dp, &qp->dns, dlen= qp->size);
  1157. #ifdef __NBSD_LIBC
  1158. dp->hdr.aa = 1;
  1159. #else
  1160. dp->hdr.dh_flag1 &= ~DHF_AA;
  1161. #endif
  1162. (void) dns_ttl(dp, dlen, now - qp->age);
  1163. if (rd) {
  1164. if (qp->stale <= now) {
  1165. qp->flags |= QF_REFRESH;
  1166. q_refresh= 1;
  1167. }
  1168. qp->usage++;
  1169. }
  1170. put_query(qp);
  1171. } else
  1172. if (class == HTONS(C_CHAOS) && query_chaos(name, type, dp, pdlen)) {
  1173. /* Return our version numbers. */
  1174. dlen= *pdlen;
  1175. } else
  1176. if (n_nameds == 0 || nxdomain(name)) {
  1177. /* No real name daemon present, or this name has a repeated top level
  1178. * domain sequence. Reply "no such domain".
  1179. */
  1180. #ifdef __NBSD_LIBC
  1181. dp->hdr.tc = 0;
  1182. dp->hdr.qr = 1;
  1183. dp->hdr.aa = 1;
  1184. dp->hdr.unused = 0;
  1185. dp->hdr.ra = 1;
  1186. dp->hdr.rcode = NXDOMAIN;
  1187. #else
  1188. dp->hdr.dh_flag1 &= ~(DHF_TC);
  1189. dp->hdr.dh_flag1 |= DHF_QR | DHF_AA;
  1190. dp->hdr.dh_flag2 &= ~(DHF_UNUSED | DHF_RCODE);
  1191. dp->hdr.dh_flag2 |= DHF_RA | NXDOMAIN;
  1192. #endif
  1193. } else
  1194. if (!rd) {
  1195. /* "Recursion Desired" is off, so don't bother to relay. */
  1196. #ifdef __NBSD_LIBC
  1197. dp->hdr.tc = 0;
  1198. dp->hdr.qr = 1;
  1199. dp->hdr.unused = 0;
  1200. dp->hdr.ra = 1;
  1201. dp->hdr.rcode = NOERROR;
  1202. #else
  1203. dp->hdr.dh_flag1 &= ~(DHF_TC);
  1204. dp->hdr.dh_flag1 |= DHF_QR;
  1205. dp->hdr.dh_flag2 &= ~(DHF_UNUSED | DHF_RCODE);
  1206. dp->hdr.dh_flag2 |= DHF_RA | NOERROR;
  1207. #endif
  1208. } else {
  1209. /* Caller needs to consult with a real name daemon. */
  1210. return 0;
  1211. }
  1212. /* Copy ID and RD back to answer. */
  1213. dp->hdr.dh_id= id;
  1214. #ifdef __NBSD_LIBC
  1215. dp->hdr.rd = rd;
  1216. #else
  1217. dp->hdr.dh_flag1 &= ~DHF_RD;
  1218. dp->hdr.dh_flag1 |= rd;
  1219. #endif
  1220. *pdlen= dlen;
  1221. return 1;
  1222. }
  1223. typedef struct udp_dns { /* One DNS packet over UDP. */
  1224. udp_io_hdr_t hdr; /* UDP header (source/destination). */
  1225. dns_t dns; /* DNS packet. */
  1226. } udp_dns_t;
  1227. static void refresh_cache(void)
  1228. /* Find a stale entry in the cache that was used to answer a query, and send
  1229. * a request to a name server that should refresh this entry.
  1230. */
  1231. {
  1232. query_t *qp;
  1233. unsigned type;
  1234. int r;
  1235. u8_t *cp;
  1236. size_t dlen, ulen;
  1237. u8_t qname[MAXDNAME+1];
  1238. u8_t *dnvec[40];
  1239. udp_dns_t udp;
  1240. if (!q_refresh) return;
  1241. for (qp= lru; qp != nil; qp= qp->more) {
  1242. if ((qp->flags & QF_REFRESH) && qp->stale > now - stale) break;
  1243. }
  1244. if (qp == nil) {
  1245. q_refresh= 0;
  1246. return;
  1247. }
  1248. /* Found one to refresh. */
  1249. qp->flags &= ~QF_REFRESH;
  1250. r= dn_expand(dns2oct(&qp->dns), dns2oct(&qp->dns) + qp->size,
  1251. qp->dns.data, qname, MAXDNAME);
  1252. if (r == -1) return;
  1253. type= upack16(qp->dns.data+r);
  1254. dnvec[0]= dns2oct(&udp.dns);
  1255. dnvec[1]= nil;
  1256. cp= udp.dns.data;
  1257. r= dn_comp(qname, cp, arraysize(udp.dns.data), dnvec, arraylimit(dnvec));
  1258. if (r == -1) return;
  1259. cp += r;
  1260. pack16(cp, type);
  1261. cp += sizeof(u16_t);
  1262. pack16(cp, HTONS(C_IN));
  1263. cp += sizeof(u16_t);
  1264. dlen= cp - dns2oct(&udp.dns);
  1265. udp.dns.hdr.dh_id= new_id(ID_REFRESH, my_port, ID_IPSELF);
  1266. #ifdef __NBSD_LIBC
  1267. udp.dns.hdr.qr = 0;
  1268. udp.dns.hdr.opcode = 0;
  1269. udp.dns.hdr.aa = 0;
  1270. udp.dns.hdr.tc = 0;
  1271. udp.dns.hdr.rd = 1;
  1272. udp.dns.hdr.ra = 0;
  1273. udp.dns.hdr.unused = 0;
  1274. udp.dns.hdr.ad = 0;
  1275. udp.dns.hdr.cd = 0;
  1276. udp.dns.hdr.rcode = 0;
  1277. #else
  1278. udp.dns.hdr.dh_flag1= DHF_RD;
  1279. udp.dns.hdr.dh_flag2= 0;
  1280. #endif
  1281. udp.dns.hdr.dh_qdcount= HTONS(1);
  1282. udp.dns.hdr.dh_ancount= HTONS(0);
  1283. udp.dns.hdr.dh_nscount= HTONS(0);
  1284. udp.dns.hdr.dh_arcount= HTONS(0);
  1285. udp.hdr.uih_dst_addr= current_named();
  1286. udp.hdr.uih_dst_port= named_port;
  1287. udp.hdr.uih_ip_opt_len= 0;
  1288. udp.hdr.uih_data_len= dlen;
  1289. if (debug >= 1) {
  1290. printf("Refresh to %s:%u:\n",
  1291. inet_ntoa(current_named()), ntohs(named_port));
  1292. dns_tell(0, &udp.dns, dlen);
  1293. }
  1294. ulen= offsetof(udp_dns_t, dns) + dlen;
  1295. if (write(udp_fd, &udp, ulen) < 0) fatal(udp_device);
  1296. }
  1297. static int job_read_udp(void *data, int expired)
  1298. /* Read UDP queries and replies. */
  1299. {
  1300. ssize_t ulen;
  1301. size_t dlen;
  1302. static udp_dns_t udp;
  1303. u16_t id, port;
  1304. ipaddr_t ip;
  1305. time_t dtime;
  1306. assert(!expired);
  1307. /* Try to read a packet. */
  1308. ulen= asyn_read(&asyn, udp_fd, &udp, sizeof(udp));
  1309. dlen= ulen - offsetof(udp_dns_t, dns);
  1310. if (ulen == -1) {
  1311. if (errno == EINPROGRESS && !expired) return 0;
  1312. if (errno == EIO) fatal(udp_device);
  1313. if (debug >= 2) {
  1314. printf("%s: UDP read: %s\n", nowgmt(), strerror(errno));
  1315. }
  1316. } else {
  1317. if (debug >= 2) {
  1318. printf("%s: UDP read, %d bytes\n", nowgmt(), (int) ulen);
  1319. }
  1320. }
  1321. /* Restart this job no matter what. */
  1322. newjob(job_read_udp, NEVER, nil);
  1323. if (ulen < (ssize_t) (sizeof(udp_io_hdr_t) + sizeof(HEADER))) return 1;
  1324. if (debug >= 1) {
  1325. printf("%s:%u UDP ", inet_ntoa(udp.hdr.uih_src_addr),
  1326. ntohs(udp.hdr.uih_src_port));
  1327. dns_tell(0, &udp.dns, dlen);
  1328. }
  1329. /* Check, and if necessary reinitialize my configuration. */
  1330. init_config(udp.hdr.uih_dst_addr);
  1331. if (
  1332. #ifdef __NBSD_LIBC
  1333. udp.dns.hdr.qr
  1334. #else
  1335. udp.dns.hdr.dh_flag1 & DHF_QR
  1336. #endif
  1337. ) {
  1338. /* This is a remote named reply, not a query. */
  1339. /* Response to a query previously relayed? */
  1340. if (!old_id(udp.dns.hdr.dh_id, &id, &port, &ip)) return 1;
  1341. if (ip == ID_IPSELF && id == ID_PROBE) {
  1342. if (searching()) {
  1343. /* We have found a name server! */
  1344. int i;
  1345. /* In my list? */
  1346. for (i= 0; i < n_nameds; i++) {
  1347. if (named[i] == udp.hdr.uih_src_addr) {
  1348. i_named= i;
  1349. if (debug >= 1) {
  1350. printf("Current named = %s\n",
  1351. inet_ntoa(current_named()));
  1352. }
  1353. stop_searching();
  1354. force_expire(job_find_named);
  1355. }
  1356. }
  1357. }
  1358. }
  1359. /* We got an answer, so stop worrying. */
  1360. if (expecting()) {
  1361. stop_expecting();
  1362. force_expire(job_expect_named);
  1363. }
  1364. /* Put the information in the cache. */
  1365. cache_reply(&udp.dns, dlen);
  1366. /* Refresh a cached entry that was used when stale. */
  1367. refresh_cache();
  1368. /* Discard reply to myself. */
  1369. if (ip == ID_IPSELF) return 1;
  1370. /* Send the reply to the process that asked for it. */
  1371. udp.dns.hdr.dh_id= id;
  1372. udp.hdr.uih_dst_addr= ip;
  1373. udp.hdr.uih_dst_port= port;
  1374. if (debug >= 1) printf("To client %s:%u\n", inet_ntoa(ip), ntohs(port));
  1375. } else {
  1376. /* A query. */
  1377. if (udp.dns.hdr.dh_qdcount != HTONS(1)) return 1;
  1378. if(localonly) {
  1379. /* Check if it's a local query. */
  1380. if(ntohl(udp.hdr.uih_src_addr) != LOCALHOST) {
  1381. syslog(LOG_WARNING, "nonamed: dropped query from %s",
  1382. inet_ntoa(udp.hdr.uih_src_addr));
  1383. return 1;
  1384. }
  1385. }
  1386. /* Try to compose a reply from local data. */
  1387. if (compose_reply(&udp.dns, &dlen)) {
  1388. udp.hdr.uih_dst_addr= udp.hdr.uih_src_addr;
  1389. udp.hdr.uih_dst_port= udp.hdr.uih_src_port;
  1390. udp.hdr.uih_ip_opt_len= 0;
  1391. udp.hdr.uih_data_len= dlen;
  1392. ulen= offsetof(udp_dns_t, dns) + dlen;
  1393. /* Send an UDP DNS reply. */
  1394. if (debug >= 1) {
  1395. printf("%s:%u UDP ", inet_ntoa(udp.hdr.uih_dst_addr),
  1396. ntohs(udp.hdr.uih_dst_port));
  1397. dns_tell(0, &udp.dns, dlen);
  1398. }
  1399. } else {
  1400. /* Let a real name daemon handle the query. */
  1401. udp.dns.hdr.dh_id= new_id(udp.dns.hdr.dh_id,
  1402. udp.hdr.uih_src_port, udp.hdr.uih_src_addr);
  1403. udp.hdr.uih_dst_addr= current_named();
  1404. udp.hdr.uih_dst_port= named_port;
  1405. if (!expecting()) {
  1406. start_expecting();
  1407. newjob(job_expect_named, now + MEDIUM_TIMEOUT, nil);
  1408. }
  1409. if (debug >= 1) {
  1410. printf("To named %s:%u\n",
  1411. inet_ntoa(current_named()), ntohs(named_port));
  1412. }
  1413. }
  1414. }
  1415. if (write(udp_fd, &udp, ulen) < 0) fatal(udp_device);
  1416. return 1;
  1417. }
  1418. #if DO_TCP
  1419. typedef struct data_cl { /* Data for connect or listen jobs. */
  1420. int fd; /* Open TCP channel. */
  1421. int dn_fd; /* TCP channel to the name daemon. */
  1422. int retry; /* Retrying a connect? */
  1423. nwio_tcpcl_t tcpcl; /* Flags. */
  1424. } data_cl_t;
  1425. typedef struct data_rw { /* Data for TCP read or write jobs. */
  1426. int r_fd; /* Read from this TCP channel. */
  1427. int w_fd; /* And write to this TCP channel. */
  1428. struct data_rw *rev; /* Optional reverse TCP channel. */
  1429. u8_t *buf; /* Buffer for bytes to transfer. */
  1430. ssize_t offset; /* Offset in buf to r/w at. */
  1431. size_t size; /* Size of buf. */
  1432. } data_rw_t;
  1433. static int job_setup_listen(void *data, int expired)
  1434. /* Set up a listening channel for TCP DNS queries. */
  1435. {
  1436. data_cl_t *data_cl= data;
  1437. nwio_tcpconf_t tcpconf;
  1438. nwio_tcpopt_t tcpopt;
  1439. int fd;
  1440. if (!expired) return 0;
  1441. if (debug >= 2) printf("%s: Setup listen\n", nowgmt());
  1442. if (data_cl == nil) {
  1443. if ((fd= open(tcp_device, O_RDWR)) < 0) {
  1444. if (errno != EMFILE) report(tcp_device);
  1445. newjob(job_setup_listen, now + SHORT_TIMEOUT, nil);
  1446. return 1;
  1447. }
  1448. tcpconf.nwtc_flags= NWTC_SHARED | NWTC_LP_SET | NWTC_UNSET_RA
  1449. | NWTC_UNSET_RP;
  1450. tcpconf.nwtc_locport= my_port;
  1451. if (ioctl(fd, NWIOSTCPCONF, &tcpconf) == -1) fatal(tcp_device);
  1452. tcpopt.nwto_flags= NWTO_DEL_RST;
  1453. if (ioctl(fd, NWIOSTCPOPT, &tcpopt) == -1) fatal(tcp_device);
  1454. data_cl= allocate(nil, sizeof(*data_cl));
  1455. data_cl->fd= fd;
  1456. data_cl->tcpcl.nwtcl_flags= 0;
  1457. }
  1458. /* And listen. */
  1459. newjob(job_listen, NEVER, data_cl);
  1460. return 1;
  1461. }
  1462. static int job_listen(void *data, int expired)
  1463. /* A connection on the TCP DNS query channel. */
  1464. {
  1465. data_cl_t *data_cl= data;
  1466. /* Wait for a client. */
  1467. if (asyn_ioctl(&asyn, data_cl->fd, NWIOTCPLISTEN, &data_cl->tcpcl) < 0) {
  1468. if (errno == EINPROGRESS) return 0;
  1469. report(tcp_device);
  1470. /* Try again after a short time. */
  1471. newjob(job_setup_listen, now + SHORT_TIMEOUT, data_cl);
  1472. return 1;
  1473. }
  1474. if (debug >= 2) printf("%s: Listen\n", nowgmt());
  1475. /* Immediately resume listening. */
  1476. newjob(job_setup_listen, IMMEDIATE, nil);
  1477. /* Set up a connect to the real name daemon. */
  1478. data_cl->retry= 0;
  1479. newjob(job_setup_connect, IMMEDIATE, data_cl);
  1480. return 1;
  1481. }
  1482. static void start_relay(int fd, int dn_fd)
  1483. /* Start one or two read jobs after job_setup_connect() or job_connect(). */
  1484. {
  1485. data_rw_t *query; /* Client to DNS daemon relay. */
  1486. data_rw_t *reply; /* DNS daemon to client relay. */
  1487. query= allocate(nil, sizeof(*query));
  1488. query->r_fd= fd;
  1489. query->buf= allocate(nil, sizeof(u16_t));
  1490. query->offset= 0;
  1491. query->size= sizeof(u16_t);
  1492. if (dn_fd == NO_FD) {
  1493. /* Answer mode. */
  1494. query->w_fd= fd;
  1495. query->rev= nil;
  1496. } else {
  1497. /* Relay mode. */
  1498. reply= allocate(nil, sizeof(*reply));
  1499. reply->r_fd= dn_fd;
  1500. reply->w_fd= fd;
  1501. reply->buf= allocate(nil, sizeof(u16_t));
  1502. reply->offset= 0;
  1503. reply->size= sizeof(u16_t);
  1504. reply->rev= query;
  1505. query->w_fd= dn_fd;
  1506. query->rev= reply;
  1507. newjob(job_read_reply, now + LONG_TIMEOUT, reply);
  1508. }
  1509. newjob(job_read_query, now + LONG_TIMEOUT, query);
  1510. }
  1511. static void close_relay(data_rw_t *data_rw)
  1512. /* Close a relay channel. */
  1513. {
  1514. if (data_rw->rev != nil) {
  1515. /* Other end still active, signal EOF. */
  1516. (void) ioctl(data_rw->w_fd, NWIOTCPSHUTDOWN, nil);
  1517. data_rw->rev->rev= nil;
  1518. } else {
  1519. /* Close both ends down. */
  1520. asyn_close(&asyn, data_rw->r_fd);
  1521. close(data_rw->r_fd);
  1522. if (data_rw->w_fd != data_rw->r_fd) {
  1523. asyn_close(&asyn, data_rw->w_fd);
  1524. close(data_rw->w_fd);
  1525. }
  1526. }
  1527. deallocate(data_rw->buf);
  1528. deallocate(data_rw);
  1529. }
  1530. static int job_setup_connect(void *data, int expired)
  1531. /* Set up a connect for a TCP channel to the real name daemon. */
  1532. {
  1533. nwio_tcpconf_t tcpconf;
  1534. int dn_fd;
  1535. data_cl_t *data_cl= data;
  1536. if (!expired) return 0;
  1537. if (debug >= 2) printf("%s: Setup connect\n", nowgmt());
  1538. if (n_nameds == 0) {
  1539. /* No name daemons to relay to, answer myself. */
  1540. start_relay(data_cl->fd, NO_FD);
  1541. deallocate(data_cl);
  1542. return 1;
  1543. }
  1544. if ((dn_fd= open(tcp_device, O_RDWR)) < 0) {
  1545. if (errno != EMFILE) report(tcp_device);
  1546. if (++data_cl->retry < 5) {
  1547. /* Retry. */
  1548. newjob(job_setup_connect, now + SHORT_TIMEOUT, data_cl);
  1549. } else {
  1550. /* Reply myself (bound to fail). */
  1551. start_relay(data_cl->fd, NO_FD);
  1552. deallocate(data_cl);
  1553. }
  1554. return 1;
  1555. }
  1556. tcpconf.nwtc_flags= NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
  1557. tcpconf.nwtc_remaddr= current_named();
  1558. tcpconf.nwtc_remport= named_port;
  1559. if (ioctl(dn_fd, NWIOSTCPCONF, &tcpconf) == -1) fatal(tcp_device);
  1560. /* And connect. */
  1561. data_cl->dn_fd= dn_fd;
  1562. data_cl->tcpcl.nwtcl_flags= 0;
  1563. newjob(job_connect, NEVER, data_cl);
  1564. return 1;
  1565. }
  1566. static int job_connect(void *data, int expired)
  1567. /* Connect to a TCP DNS query channel. */
  1568. {
  1569. data_cl_t *data_cl= data;
  1570. /* Try to connect. */
  1571. if (asyn_ioctl(&asyn, data_cl->dn_fd, NWIOTCPCONN, &data_cl->tcpcl) < 0) {
  1572. if (errno == EINPROGRESS) return 0;
  1573. if (errno == EIO) fatal(tcp_device);
  1574. /* Connection refused. */
  1575. if (debug >= 2) printf("%s: Connect: %s\n", nowgmt(), strerror(errno));
  1576. asyn_close(&asyn, data_cl->dn_fd);
  1577. close(data_cl->dn_fd);
  1578. data_cl->dn_fd= NO_FD;
  1579. if (++data_cl->retry < 5) {
  1580. /* Search a new name daemon. */
  1581. if (!searching()) {
  1582. start_searching();
  1583. force_expire(job_find_named);
  1584. }
  1585. newjob(job_setup_connect, NEVER, data_cl);
  1586. return 1;
  1587. }
  1588. /* Reply with a failure eventually. */
  1589. }
  1590. if (debug >= 2) printf("%s: Connect\n", nowgmt());
  1591. /* Read the query from the user, send on to the name daemon, etc. */
  1592. start_relay(data_cl->fd, data_cl->dn_fd);
  1593. deallocate(data_cl);
  1594. return 1;
  1595. }
  1596. static void tcp_dns_tell(int fd, u8_t *buf)
  1597. /* Tell about a DNS packet on a TCP channel. */
  1598. {
  1599. nwio_tcpconf_t tcpconf;
  1600. if (ioctl(fd, NWIOGTCPCONF, &tcpconf) < 0) {
  1601. printf("??\?:?? TCP ");
  1602. } else {
  1603. printf("%s:%u TCP ", inet_ntoa(tcpconf.nwtc_remaddr),
  1604. ntohs(tcpconf.nwtc_remport));
  1605. }
  1606. dns_tell(0, oct2dns(buf + sizeof(u16_t)), ntohs(upack16(buf)));
  1607. }
  1608. static int job_read_query(void *data, int expired)
  1609. /* Read TCP queries from the client. */
  1610. {
  1611. data_rw_t *data_rw= data;
  1612. ssize_t count;
  1613. /* Try to read count bytes. */
  1614. count= asyn_read(&asyn, data_rw->r_fd,
  1615. data_rw->buf + data_rw->offset,
  1616. data_rw->size - data_rw->offset);
  1617. if (count < 0) {
  1618. if (errno == EINPROGRESS && !expired) return 0;
  1619. if (errno == EIO) fatal(tcp_device);
  1620. /* Remote end is late, or an error occurred. */
  1621. if (debug >= 2) {
  1622. printf("%s: TCP read query: %s\n", nowgmt(), strerror(errno));
  1623. }
  1624. close_relay(data_rw);
  1625. return 1;
  1626. }
  1627. if (debug >= 2) {
  1628. printf("%s: TCP read query, %d/%u bytes\n",
  1629. nowgmt(), data_rw->offset + count, data_rw->size);
  1630. }
  1631. if (count == 0) {
  1632. /* EOF. */
  1633. close_relay(data_rw);
  1634. return 1;
  1635. }
  1636. data_rw->offset += count;
  1637. if (data_rw->offset == data_rw->size) {
  1638. data_rw->size= sizeof(u16_t) + ntohs(upack16(data_rw->buf));
  1639. if (data_rw->size < sizeof(u16_t)) {
  1640. /* Malformed. */
  1641. close_relay(data_rw);
  1642. return 1;
  1643. }
  1644. if (data_rw->offset < data_rw->size) {
  1645. /* Query not complete, read more. */
  1646. data_rw->buf= allocate(data_rw->buf, data_rw->size);
  1647. newjob(job_read_query, now + LONG_TIMEOUT, data_rw);
  1648. return 1;
  1649. }
  1650. }
  1651. if (data_rw->size < sizeof(u16_t) + sizeof(dns_hdr_t)) {
  1652. close_relay(data_rw);
  1653. return 1;
  1654. }
  1655. if (debug >= 1) tcp_dns_tell(data_rw->r_fd, dat

Large files files are truncated, but you can click here to view the full file