dnresolve.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. /*
  2. * domain name resolvers, see rfcs 1035 and 1123
  3. */
  4. #include <u.h>
  5. #include <libc.h>
  6. #include <ip.h>
  7. #include <bio.h>
  8. #include <ndb.h>
  9. #include "dns.h"
  10. #define NS2MS(ns) ((ns) / 1000000L)
  11. #define S2MS(s) ((s) * 1000)
  12. #define MS2S(ms) ((ms) / 1000)
  13. typedef struct Dest Dest;
  14. typedef struct Ipaddr Ipaddr;
  15. typedef struct Query Query;
  16. enum
  17. {
  18. Udp, Tcp,
  19. Maxdest= 24, /* maximum destinations for a request message */
  20. Maxtrans= 3, /* maximum transmissions to a server */
  21. Destmagic= 0xcafebabe,
  22. Querymagic= 0xdeadbeef,
  23. };
  24. enum { Hurry, Patient, };
  25. enum { Outns, Inns, };
  26. enum { Remntretry = 15, }; /* min. sec.s between remount attempts */
  27. struct Ipaddr {
  28. Ipaddr *next;
  29. uchar ip[IPaddrlen];
  30. };
  31. struct Dest
  32. {
  33. uchar a[IPaddrlen]; /* ip address */
  34. DN *s; /* name server */
  35. int nx; /* number of transmissions */
  36. int code; /* response code; used to clear dp->respcode */
  37. ulong magic;
  38. };
  39. /*
  40. * Query has a QLock in it, thus it can't be an automatic
  41. * variable, since each process would see a separate copy
  42. * of the lock on its stack.
  43. */
  44. struct Query {
  45. DN *dp; /* domain */
  46. ushort type; /* and type to look up */
  47. Request *req;
  48. RR *nsrp; /* name servers to consult */
  49. /* dest must not be on the stack due to forking in slave() */
  50. Dest *dest; /* array of destinations */
  51. Dest *curdest; /* pointer to one of them */
  52. int ndest;
  53. int udpfd;
  54. QLock tcplock; /* only one tcp call at a time per query */
  55. int tcpset;
  56. int tcpfd; /* if Tcp, read replies from here */
  57. int tcpctlfd;
  58. uchar tcpip[IPaddrlen];
  59. ulong magic;
  60. };
  61. /* estimated % probability of such a record existing at all */
  62. int likely[] = {
  63. [Ta] 95,
  64. [Taaaa] 10,
  65. [Tcname] 15,
  66. [Tmx] 60,
  67. [Tns] 90,
  68. [Tnull] 5,
  69. [Tptr] 35,
  70. [Tsoa] 90,
  71. [Tsrv] 60,
  72. [Ttxt] 15,
  73. [Tall] 95,
  74. };
  75. static RR* dnresolve1(char*, int, int, Request*, int, int);
  76. static int netquery(Query *, int);
  77. /*
  78. * reading /proc/pid/args yields either "name" or "name [display args]",
  79. * so return only display args, if any.
  80. */
  81. static char *
  82. procgetname(void)
  83. {
  84. int fd, n;
  85. char *lp, *rp;
  86. char buf[256];
  87. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  88. if((fd = open(buf, OREAD)) < 0)
  89. return strdup("");
  90. *buf = '\0';
  91. n = read(fd, buf, sizeof buf-1);
  92. close(fd);
  93. if (n >= 0)
  94. buf[n] = '\0';
  95. if ((lp = strchr(buf, '[')) == nil ||
  96. (rp = strrchr(buf, ']')) == nil)
  97. return strdup("");
  98. *rp = '\0';
  99. return strdup(lp+1);
  100. }
  101. /*
  102. * lookup 'type' info for domain name 'name'. If it doesn't exist, try
  103. * looking it up as a canonical name.
  104. */
  105. RR*
  106. dnresolve(char *name, int class, int type, Request *req, RR **cn, int depth,
  107. int recurse, int rooted, int *status)
  108. {
  109. RR *rp, *nrp, *drp;
  110. DN *dp;
  111. int loops;
  112. char *procname;
  113. char nname[Domlen];
  114. if(status)
  115. *status = 0;
  116. if(depth > 12) /* in a recursive loop? */
  117. return nil;
  118. procname = procgetname();
  119. /*
  120. * hack for systems that don't have resolve search
  121. * lists. Just look up the simple name in the database.
  122. */
  123. if(!rooted && strchr(name, '.') == nil){
  124. rp = nil;
  125. drp = domainlist(class);
  126. for(nrp = drp; rp == nil && nrp != nil; nrp = nrp->next){
  127. snprint(nname, sizeof nname, "%s.%s", name,
  128. nrp->ptr->name);
  129. rp = dnresolve(nname, class, type, req, cn, depth+1,
  130. recurse, rooted, status);
  131. rrfreelist(rrremneg(&rp));
  132. }
  133. if(drp != nil)
  134. rrfreelist(drp);
  135. procsetname(procname);
  136. free(procname);
  137. return rp;
  138. }
  139. /*
  140. * try the name directly
  141. */
  142. rp = dnresolve1(name, class, type, req, depth, recurse);
  143. if(rp == nil) {
  144. /*
  145. * try it as a canonical name if we weren't told
  146. * that the name didn't exist
  147. */
  148. dp = dnlookup(name, class, 0);
  149. if(type != Tptr && dp->respcode != Rname)
  150. for(loops = 0; rp == nil && loops < 32; loops++){
  151. rp = dnresolve1(name, class, Tcname, req,
  152. depth, recurse);
  153. if(rp == nil)
  154. break;
  155. /* rp->host == nil shouldn't happen, but does */
  156. if(rp->negative || rp->host == nil){
  157. rrfreelist(rp);
  158. rp = nil;
  159. break;
  160. }
  161. name = rp->host->name;
  162. if(cn)
  163. rrcat(cn, rp);
  164. else
  165. rrfreelist(rp);
  166. rp = dnresolve1(name, class, type, req,
  167. depth, recurse);
  168. }
  169. /* distinction between not found and not good */
  170. if(rp == nil && status != nil && dp->respcode != 0)
  171. *status = dp->respcode;
  172. }
  173. procsetname(procname);
  174. free(procname);
  175. return randomize(rp);
  176. }
  177. static void
  178. queryinit(Query *qp, DN *dp, int type, Request *req)
  179. {
  180. memset(qp, 0, sizeof *qp);
  181. qp->udpfd = qp->tcpfd = qp->tcpctlfd = -1;
  182. qp->dp = dp;
  183. qp->type = type;
  184. if (qp->type != type)
  185. dnslog("queryinit: bogus type %d", type);
  186. qp->req = req;
  187. qp->nsrp = nil;
  188. qp->dest = qp->curdest = nil;
  189. qp->magic = Querymagic;
  190. }
  191. static void
  192. queryck(Query *qp)
  193. {
  194. assert(qp);
  195. assert(qp->magic == Querymagic);
  196. }
  197. static void
  198. querydestroy(Query *qp)
  199. {
  200. queryck(qp);
  201. /* leave udpfd alone */
  202. if (qp->tcpfd > 0)
  203. close(qp->tcpfd);
  204. if (qp->tcpctlfd > 0) {
  205. hangup(qp->tcpctlfd);
  206. close(qp->tcpctlfd);
  207. }
  208. free(qp->dest);
  209. memset(qp, 0, sizeof *qp); /* prevent accidents */
  210. qp->udpfd = qp->tcpfd = qp->tcpctlfd = -1;
  211. }
  212. static void
  213. destinit(Dest *p)
  214. {
  215. memset(p, 0, sizeof *p);
  216. p->magic = Destmagic;
  217. }
  218. static void
  219. destck(Dest *p)
  220. {
  221. assert(p);
  222. assert(p->magic == Destmagic);
  223. }
  224. static void
  225. destdestroy(Dest *p)
  226. {
  227. USED(p);
  228. }
  229. /*
  230. * if the response to a query hasn't arrived within 100 ms.,
  231. * it's unlikely to arrive at all. after 1 s., it's really unlikely.
  232. * queries for missing RRs are likely to produce time-outs rather than
  233. * negative responses, so cname and aaaa queries are likely to time out,
  234. * thus we don't wait very long for them.
  235. */
  236. static void
  237. notestats(vlong start, int tmout, int type)
  238. {
  239. qlock(&stats);
  240. if (tmout) {
  241. stats.tmout++;
  242. if (type == Taaaa)
  243. stats.tmoutv6++;
  244. else if (type == Tcname)
  245. stats.tmoutcname++;
  246. } else {
  247. long wait10ths = NS2MS(nsec() - start) / 100;
  248. if (wait10ths <= 0)
  249. stats.under10ths[0]++;
  250. else if (wait10ths >= nelem(stats.under10ths))
  251. stats.under10ths[nelem(stats.under10ths) - 1]++;
  252. else
  253. stats.under10ths[wait10ths]++;
  254. }
  255. qunlock(&stats);
  256. }
  257. static void
  258. noteinmem(void)
  259. {
  260. qlock(&stats);
  261. stats.answinmem++;
  262. qunlock(&stats);
  263. }
  264. static RR*
  265. issuequery(Query *qp, char *name, int class, int depth, int recurse)
  266. {
  267. char *cp;
  268. DN *nsdp;
  269. RR *rp, *nsrp, *dbnsrp;
  270. /*
  271. * if we're running as just a resolver, query our
  272. * designated name servers
  273. */
  274. if(cfg.resolver){
  275. nsrp = randomize(getdnsservers(class));
  276. if(nsrp != nil) {
  277. qp->nsrp = nsrp;
  278. if(netquery(qp, depth+1)){
  279. rrfreelist(nsrp);
  280. return rrlookup(qp->dp, qp->type, OKneg);
  281. }
  282. rrfreelist(nsrp);
  283. }
  284. }
  285. /*
  286. * walk up the domain name looking for
  287. * a name server for the domain.
  288. */
  289. for(cp = name; cp; cp = walkup(cp)){
  290. /*
  291. * if this is a local (served by us) domain,
  292. * return answer
  293. */
  294. dbnsrp = randomize(dblookup(cp, class, Tns, 0, 0));
  295. if(dbnsrp && dbnsrp->local){
  296. rp = dblookup(name, class, qp->type, 1, dbnsrp->ttl);
  297. rrfreelist(dbnsrp);
  298. return rp;
  299. }
  300. /*
  301. * if recursion isn't set, just accept local
  302. * entries
  303. */
  304. if(recurse == Dontrecurse){
  305. if(dbnsrp)
  306. rrfreelist(dbnsrp);
  307. continue;
  308. }
  309. /* look for ns in cache */
  310. nsdp = dnlookup(cp, class, 0);
  311. nsrp = nil;
  312. if(nsdp)
  313. nsrp = randomize(rrlookup(nsdp, Tns, NOneg));
  314. /* if the entry timed out, ignore it */
  315. if(nsrp && nsrp->ttl < now){
  316. rrfreelist(nsrp);
  317. nsrp = nil;
  318. }
  319. if(nsrp){
  320. rrfreelist(dbnsrp);
  321. /* query the name servers found in cache */
  322. qp->nsrp = nsrp;
  323. if(netquery(qp, depth+1)){
  324. rrfreelist(nsrp);
  325. return rrlookup(qp->dp, qp->type, OKneg);
  326. }
  327. rrfreelist(nsrp);
  328. continue;
  329. }
  330. /* use ns from db */
  331. if(dbnsrp){
  332. /* try the name servers found in db */
  333. qp->nsrp = dbnsrp;
  334. if(netquery(qp, depth+1)){
  335. /* we got an answer */
  336. rrfreelist(dbnsrp);
  337. return rrlookup(qp->dp, qp->type, NOneg);
  338. }
  339. rrfreelist(dbnsrp);
  340. }
  341. }
  342. return nil;
  343. }
  344. static RR*
  345. dnresolve1(char *name, int class, int type, Request *req, int depth,
  346. int recurse)
  347. {
  348. Area *area;
  349. DN *dp;
  350. RR *rp;
  351. Query *qp;
  352. if(debug)
  353. dnslog("[%d] dnresolve1 %s %d %d", getpid(), name, type, class);
  354. /* only class Cin implemented so far */
  355. if(class != Cin)
  356. return nil;
  357. dp = dnlookup(name, class, 1);
  358. /*
  359. * Try the cache first
  360. */
  361. rp = rrlookup(dp, type, OKneg);
  362. if(rp)
  363. if(rp->db){
  364. /* unauthoritative db entries are hints */
  365. if(rp->auth) {
  366. noteinmem();
  367. return rp;
  368. }
  369. } else
  370. /* cached entry must still be valid */
  371. if(rp->ttl > now)
  372. /* but Tall entries are special */
  373. if(type != Tall || rp->query == Tall) {
  374. noteinmem();
  375. return rp;
  376. }
  377. rrfreelist(rp);
  378. rp = nil; /* accident prevention */
  379. USED(rp);
  380. /*
  381. * try the cache for a canonical name. if found punt
  382. * since we'll find it during the canonical name search
  383. * in dnresolve().
  384. */
  385. if(type != Tcname){
  386. rp = rrlookup(dp, Tcname, NOneg);
  387. rrfreelist(rp);
  388. if(rp)
  389. return nil;
  390. }
  391. /*
  392. * if the domain name is within an area of ours,
  393. * we should have found its data in memory by now.
  394. */
  395. area = inmyarea(dp->name);
  396. if (area || strncmp(dp->name, "local#", 6) == 0) {
  397. // char buf[32];
  398. // dnslog("%s %s: no data in area %s", dp->name,
  399. // rrname(type, buf, sizeof buf), area->soarr->owner->name);
  400. return nil;
  401. }
  402. qp = emalloc(sizeof *qp);
  403. queryinit(qp, dp, type, req);
  404. rp = issuequery(qp, name, class, depth, recurse);
  405. querydestroy(qp);
  406. free(qp);
  407. if(rp)
  408. return rp;
  409. /* settle for a non-authoritative answer */
  410. rp = rrlookup(dp, type, OKneg);
  411. if(rp)
  412. return rp;
  413. /* noone answered. try the database, we might have a chance. */
  414. return dblookup(name, class, type, 0, 0);
  415. }
  416. /*
  417. * walk a domain name one element to the right.
  418. * return a pointer to that element.
  419. * in other words, return a pointer to the parent domain name.
  420. */
  421. char*
  422. walkup(char *name)
  423. {
  424. char *cp;
  425. cp = strchr(name, '.');
  426. if(cp)
  427. return cp+1;
  428. else if(*name)
  429. return "";
  430. else
  431. return 0;
  432. }
  433. /*
  434. * Get a udp port for sending requests and reading replies. Put the port
  435. * into "headers" mode.
  436. */
  437. static char *hmsg = "headers";
  438. int
  439. udpport(char *mtpt)
  440. {
  441. int fd, ctl;
  442. char ds[64], adir[64];
  443. /* get a udp port */
  444. snprint(ds, sizeof ds, "%s/udp!*!0", (mtpt? mtpt: "/net"));
  445. ctl = announce(ds, adir);
  446. if(ctl < 0){
  447. /* warning("can't get udp port"); */
  448. return -1;
  449. }
  450. /* turn on header style interface */
  451. if(write(ctl, hmsg, strlen(hmsg)) , 0){
  452. close(ctl);
  453. warning(hmsg);
  454. return -1;
  455. }
  456. /* grab the data file */
  457. snprint(ds, sizeof ds, "%s/data", adir);
  458. fd = open(ds, ORDWR);
  459. close(ctl);
  460. if(fd < 0)
  461. warning("can't open udp port %s: %r", ds);
  462. return fd;
  463. }
  464. /* generate a DNS UDP query packet */
  465. int
  466. mkreq(DN *dp, int type, uchar *buf, int flags, ushort reqno)
  467. {
  468. DNSmsg m;
  469. int len;
  470. Udphdr *uh = (Udphdr*)buf;
  471. /* stuff port number into output buffer */
  472. memset(uh, 0, sizeof *uh);
  473. hnputs(uh->rport, 53);
  474. /* make request and convert it to output format */
  475. memset(&m, 0, sizeof m);
  476. m.flags = flags;
  477. m.id = reqno;
  478. m.qd = rralloc(type);
  479. m.qd->owner = dp;
  480. m.qd->type = type;
  481. if (m.qd->type != type)
  482. dnslog("mkreq: bogus type %d", type);
  483. len = convDNS2M(&m, &buf[Udphdrsize], Maxudp);
  484. rrfree(m.qd);
  485. memset(&m, 0, sizeof m); /* cause trouble */
  486. return len;
  487. }
  488. void
  489. freeanswers(DNSmsg *mp)
  490. {
  491. rrfreelist(mp->qd);
  492. rrfreelist(mp->an);
  493. rrfreelist(mp->ns);
  494. rrfreelist(mp->ar);
  495. mp->qd = mp->an = mp->ns = mp->ar = nil;
  496. }
  497. /* sets srcip */
  498. static int
  499. readnet(Query *qp, int medium, uchar *ibuf, ulong endtime, uchar **replyp,
  500. uchar *srcip)
  501. {
  502. int len, fd;
  503. long ms;
  504. vlong startns = nsec();
  505. uchar *reply;
  506. uchar lenbuf[2];
  507. /* timed read of reply */
  508. ms = S2MS(endtime) - NS2MS(startns);
  509. if (ms < 2000)
  510. ms = 2000; /* give the remote ns a fighting chance */
  511. reply = ibuf;
  512. len = -1; /* pessimism */
  513. memset(srcip, 0, IPaddrlen);
  514. if (medium == Udp)
  515. if (qp->udpfd <= 0)
  516. dnslog("readnet: qp->udpfd closed");
  517. else {
  518. alarm(ms);
  519. len = read(qp->udpfd, ibuf, Udphdrsize+Maxudpin);
  520. alarm(0);
  521. notestats(startns, len < 0, qp->type);
  522. if (len >= IPaddrlen)
  523. memmove(srcip, ibuf, IPaddrlen);
  524. if (len >= Udphdrsize) {
  525. len -= Udphdrsize;
  526. reply += Udphdrsize;
  527. }
  528. }
  529. else {
  530. if (!qp->tcpset)
  531. dnslog("readnet: tcp params not set");
  532. alarm(ms);
  533. fd = qp->tcpfd;
  534. if (fd <= 0)
  535. dnslog("readnet: %s: tcp fd unset for dest %I",
  536. qp->dp->name, qp->tcpip);
  537. else if (readn(fd, lenbuf, 2) != 2) {
  538. dnslog("readnet: short read of tcp size from %I",
  539. qp->tcpip);
  540. /* probably a time-out */
  541. notestats(startns, 1, qp->type);
  542. } else {
  543. len = lenbuf[0]<<8 | lenbuf[1];
  544. if (readn(fd, ibuf, len) != len) {
  545. dnslog("readnet: short read of tcp data from %I",
  546. qp->tcpip);
  547. /* probably a time-out */
  548. notestats(startns, 1, qp->type);
  549. len = -1;
  550. }
  551. }
  552. alarm(0);
  553. memmove(srcip, qp->tcpip, IPaddrlen);
  554. }
  555. *replyp = reply;
  556. return len;
  557. }
  558. /*
  559. * read replies to a request and remember the rrs in the answer(s).
  560. * ignore any of the wrong type.
  561. * wait at most until endtime.
  562. */
  563. static int
  564. readreply(Query *qp, int medium, ushort req, uchar *ibuf, DNSmsg *mp,
  565. ulong endtime)
  566. {
  567. int len, rv;
  568. char *err;
  569. char tbuf[32];
  570. uchar *reply;
  571. uchar srcip[IPaddrlen];
  572. RR *rp;
  573. queryck(qp);
  574. rv = 0;
  575. memset(mp, 0, sizeof *mp);
  576. if (time(nil) >= endtime)
  577. return -1; /* timed out before we started */
  578. memset(srcip, 0, sizeof srcip);
  579. if (0)
  580. len = -1;
  581. for (; time(nil) < endtime &&
  582. (len = readnet(qp, medium, ibuf, endtime, &reply, srcip)) >= 0;
  583. freeanswers(mp)){
  584. /* convert into internal format */
  585. memset(mp, 0, sizeof *mp);
  586. err = convM2DNS(reply, len, mp, nil);
  587. if (mp->flags & Ftrunc) {
  588. free(err);
  589. freeanswers(mp);
  590. /* notify our caller to retry the query via tcp. */
  591. return -1;
  592. } else if(err){
  593. dnslog("readreply: %s: input err, len %d: %s: %I",
  594. qp->dp->name, len, err, srcip);
  595. free(err);
  596. continue;
  597. }
  598. if(debug)
  599. logreply(qp->req->id, srcip, mp);
  600. /* answering the right question? */
  601. if(mp->id != req)
  602. dnslog("%d: id %d instead of %d: %I", qp->req->id,
  603. mp->id, req, srcip);
  604. else if(mp->qd == 0)
  605. dnslog("%d: no question RR: %I", qp->req->id, srcip);
  606. else if(mp->qd->owner != qp->dp)
  607. dnslog("%d: owner %s instead of %s: %I", qp->req->id,
  608. mp->qd->owner->name, qp->dp->name, srcip);
  609. else if(mp->qd->type != qp->type)
  610. dnslog("%d: qp->type %d instead of %d: %I",
  611. qp->req->id, mp->qd->type, qp->type, srcip);
  612. else {
  613. /* remember what request this is in answer to */
  614. for(rp = mp->an; rp; rp = rp->next)
  615. rp->query = qp->type;
  616. return rv;
  617. }
  618. }
  619. if (time(nil) >= endtime) {
  620. ; /* query expired */
  621. } else if (0) {
  622. /* this happens routinely when a read times out */
  623. dnslog("readreply: %s type %s: ns %I read error or eof "
  624. "(returned %d): %r", qp->dp->name, rrname(qp->type,
  625. tbuf, sizeof tbuf), srcip, len);
  626. if (medium == Udp)
  627. for (rp = qp->nsrp; rp != nil; rp = rp->next)
  628. if (rp->type == Tns)
  629. dnslog("readreply: %s: query sent to "
  630. "ns %s", qp->dp->name,
  631. rp->host->name);
  632. }
  633. return -1;
  634. }
  635. /*
  636. * return non-0 if first list includes second list
  637. */
  638. int
  639. contains(RR *rp1, RR *rp2)
  640. {
  641. RR *trp1, *trp2;
  642. for(trp2 = rp2; trp2; trp2 = trp2->next){
  643. for(trp1 = rp1; trp1; trp1 = trp1->next)
  644. if(trp1->type == trp2->type)
  645. if(trp1->host == trp2->host)
  646. if(trp1->owner == trp2->owner)
  647. break;
  648. if(trp1 == nil)
  649. return 0;
  650. }
  651. return 1;
  652. }
  653. /*
  654. * return multicast version if any
  655. */
  656. int
  657. ipisbm(uchar *ip)
  658. {
  659. if(isv4(ip)){
  660. if (ip[IPv4off] >= 0xe0 && ip[IPv4off] < 0xf0 ||
  661. ipcmp(ip, IPv4bcast) == 0)
  662. return 4;
  663. } else
  664. if(ip[0] == 0xff)
  665. return 6;
  666. return 0;
  667. }
  668. /*
  669. * Get next server address
  670. */
  671. static int
  672. serveraddrs(Query *qp, int nd, int depth)
  673. {
  674. RR *rp, *arp, *trp;
  675. Dest *cur;
  676. if(nd >= Maxdest)
  677. return 0;
  678. /*
  679. * look for a server whose address we already know.
  680. * if we find one, mark it so we ignore this on
  681. * subsequent passes.
  682. */
  683. arp = 0;
  684. for(rp = qp->nsrp; rp; rp = rp->next){
  685. assert(rp->magic == RRmagic);
  686. if(rp->marker)
  687. continue;
  688. arp = rrlookup(rp->host, Ta, NOneg);
  689. if(arp){
  690. rp->marker = 1;
  691. break;
  692. }
  693. arp = dblookup(rp->host->name, Cin, Ta, 0, 0);
  694. if(arp){
  695. rp->marker = 1;
  696. break;
  697. }
  698. }
  699. /*
  700. * if the cache and database lookup didn't find any new
  701. * server addresses, try resolving one via the network.
  702. * Mark any we try to resolve so we don't try a second time.
  703. */
  704. if(arp == 0)
  705. for(rp = qp->nsrp; rp; rp = rp->next){
  706. if(rp->marker)
  707. continue;
  708. rp->marker = 1;
  709. /*
  710. * avoid loops looking up a server under itself
  711. */
  712. if(subsume(rp->owner->name, rp->host->name))
  713. continue;
  714. arp = dnresolve(rp->host->name, Cin, Ta, qp->req, 0,
  715. depth+1, Recurse, 1, 0);
  716. rrfreelist(rrremneg(&arp));
  717. if(arp)
  718. break;
  719. }
  720. /* use any addresses that we found */
  721. for(trp = arp; trp && nd < Maxdest; trp = trp->next){
  722. cur = &qp->dest[nd];
  723. parseip(cur->a, trp->ip->name);
  724. /*
  725. * straddling servers can reject all nameservers if they are all
  726. * inside, so be sure to list at least one outside ns at
  727. * the end of the ns list in /lib/ndb for `dom='.
  728. */
  729. if (ipisbm(cur->a) ||
  730. cfg.straddle && !insideaddr(qp->dp->name) && insidens(cur->a))
  731. continue;
  732. cur->nx = 0;
  733. cur->s = trp->owner;
  734. cur->code = Rtimeout;
  735. nd++;
  736. }
  737. rrfreelist(arp);
  738. return nd;
  739. }
  740. /*
  741. * cache negative responses
  742. */
  743. static void
  744. cacheneg(DN *dp, int type, int rcode, RR *soarr)
  745. {
  746. RR *rp;
  747. DN *soaowner;
  748. ulong ttl;
  749. stats.negcached++;
  750. /* no cache time specified, don't make anything up */
  751. if(soarr != nil){
  752. if(soarr->next != nil){
  753. rrfreelist(soarr->next);
  754. soarr->next = nil;
  755. }
  756. soaowner = soarr->owner;
  757. } else
  758. soaowner = nil;
  759. /* the attach can cause soarr to be freed so mine it now */
  760. if(soarr != nil && soarr->soa != nil)
  761. ttl = soarr->soa->minttl+now;
  762. else
  763. ttl = 5*Min;
  764. /* add soa and negative RR to the database */
  765. rrattach(soarr, Authoritative);
  766. rp = rralloc(type);
  767. rp->owner = dp;
  768. rp->negative = 1;
  769. rp->negsoaowner = soaowner;
  770. rp->negrcode = rcode;
  771. rp->ttl = ttl;
  772. rrattach(rp, Authoritative);
  773. }
  774. static int
  775. setdestoutns(Dest *p, int n)
  776. {
  777. uchar *outns = outsidens(n);
  778. destck(p);
  779. destinit(p);
  780. if (outns == nil) {
  781. if (n == 0)
  782. dnslog("[%d] no outside-ns in ndb", getpid());
  783. return -1;
  784. }
  785. memmove(p->a, outns, sizeof p->a);
  786. p->s = dnlookup("outside-ns-ips", Cin, 1);
  787. return 0;
  788. }
  789. /*
  790. * issue query via UDP or TCP as appropriate.
  791. * for TCP, returns with qp->tcpip set from udppkt header.
  792. */
  793. static int
  794. mydnsquery(Query *qp, int medium, uchar *udppkt, int len)
  795. {
  796. int rv = -1, nfd;
  797. char *domain;
  798. char conndir[40];
  799. uchar belen[2];
  800. NetConnInfo *nci;
  801. queryck(qp);
  802. domain = smprint("%I", udppkt);
  803. if (myaddr(domain)) {
  804. dnslog("mydnsquery: trying to send to myself (%s); bzzzt",
  805. domain);
  806. free(domain);
  807. return rv;
  808. }
  809. switch (medium) {
  810. case Udp:
  811. free(domain);
  812. nfd = dup(qp->udpfd, -1);
  813. if (nfd < 0) {
  814. warning("mydnsquery: qp->udpfd %d: %r", qp->udpfd);
  815. close(qp->udpfd); /* ensure it's closed */
  816. qp->udpfd = -1; /* poison it */
  817. return rv;
  818. }
  819. close(nfd);
  820. if (qp->udpfd <= 0)
  821. dnslog("mydnsquery: qp->udpfd %d closed", qp->udpfd);
  822. else {
  823. if (write(qp->udpfd, udppkt, len+Udphdrsize) !=
  824. len+Udphdrsize)
  825. warning("sending udp msg: %r");
  826. else {
  827. stats.qsent++;
  828. rv = 0;
  829. }
  830. }
  831. break;
  832. case Tcp:
  833. /* send via TCP & keep fd around for reply */
  834. alarm(10*1000);
  835. qp->tcpfd = rv = dial(netmkaddr(domain, "tcp", "dns"), nil,
  836. conndir, &qp->tcpctlfd);
  837. alarm(0);
  838. if (qp->tcpfd < 0) {
  839. dnslog("can't dial tcp!%s!dns: %r", domain);
  840. free(domain);
  841. break;
  842. }
  843. free(domain);
  844. nci = getnetconninfo(conndir, qp->tcpfd);
  845. if (nci) {
  846. parseip(qp->tcpip, nci->rsys);
  847. freenetconninfo(nci);
  848. } else
  849. dnslog("mydnsquery: getnetconninfo failed");
  850. qp->tcpset = 1;
  851. belen[0] = len >> 8;
  852. belen[1] = len;
  853. if (write(qp->tcpfd, belen, 2) != 2 ||
  854. write(qp->tcpfd, udppkt + Udphdrsize, len) != len)
  855. warning("sending tcp msg: %r");
  856. break;
  857. default:
  858. sysfatal("mydnsquery: bad medium");
  859. }
  860. return rv;
  861. }
  862. /*
  863. * send query to all UDP destinations or one TCP destination,
  864. * taken from obuf (udp packet) header
  865. */
  866. static int
  867. xmitquery(Query *qp, int medium, int depth, uchar *obuf, int inns, int len)
  868. {
  869. int j, n;
  870. char buf[32];
  871. Dest *p;
  872. queryck(qp);
  873. if(time(nil) >= qp->req->aborttime)
  874. return -1;
  875. /*
  876. * get a nameserver address if we need one.
  877. * serveraddrs populates qp->dest.
  878. */
  879. p = qp->dest;
  880. destck(p);
  881. if (qp->ndest < 0 || qp->ndest > Maxdest)
  882. dnslog("qp->ndest %d out of range", qp->ndest);
  883. if (qp->ndest > qp->curdest - p)
  884. qp->curdest = &qp->dest[serveraddrs(qp, qp->curdest - p, depth)];
  885. destck(qp->curdest);
  886. /* no servers, punt */
  887. if (qp->curdest == qp->dest)
  888. if (cfg.straddle && cfg.inside) {
  889. /* get ips of "outside-ns-ips" */
  890. p = qp->curdest = qp->dest;
  891. for(n = 0; n < Maxdest; n++, qp->curdest++)
  892. if (setdestoutns(qp->curdest, n) < 0)
  893. break;
  894. } else {
  895. /* it's probably just a bogus domain, don't log it */
  896. // dnslog("xmitquery: %s: no nameservers", qp->dp->name);
  897. return -1;
  898. }
  899. /* send to first 'qp->ndest' destinations */
  900. j = 0;
  901. if (medium == Tcp) {
  902. j++;
  903. queryck(qp);
  904. assert(qp->dp);
  905. procsetname("tcp %sside query for %s %s", (inns? "in": "out"),
  906. qp->dp->name, rrname(qp->type, buf, sizeof buf));
  907. mydnsquery(qp, medium, obuf, len); /* sets qp->tcpip from obuf */
  908. if(debug)
  909. logsend(qp->req->id, depth, qp->tcpip, "", qp->dp->name,
  910. qp->type);
  911. } else
  912. for(; p < &qp->dest[qp->ndest] && p < qp->curdest; p++){
  913. /* skip destinations we've finished with */
  914. if(p->nx >= Maxtrans)
  915. continue;
  916. j++;
  917. /* exponential backoff of requests */
  918. if((1<<p->nx) > qp->ndest)
  919. continue;
  920. procsetname("udp %sside query to %I/%s %s %s",
  921. (inns? "in": "out"), p->a, p->s->name,
  922. qp->dp->name, rrname(qp->type, buf, sizeof buf));
  923. if(debug)
  924. logsend(qp->req->id, depth, p->a, p->s->name,
  925. qp->dp->name, qp->type);
  926. /* fill in UDP destination addr & send it */
  927. memmove(obuf, p->a, sizeof p->a);
  928. mydnsquery(qp, medium, obuf, len);
  929. p->nx++;
  930. }
  931. if(j == 0) {
  932. // dnslog("xmitquery: %s: no destinations left", qp->dp->name);
  933. return -1;
  934. }
  935. return 0;
  936. }
  937. static int lckindex[Maxlcks] = {
  938. 0, /* all others map here */
  939. Ta,
  940. Tns,
  941. Tcname,
  942. Tsoa,
  943. Tptr,
  944. Tmx,
  945. Ttxt,
  946. Taaaa,
  947. };
  948. static int
  949. qtype2lck(int qtype) /* map query type to querylck index */
  950. {
  951. int i;
  952. for (i = 1; i < nelem(lckindex); i++)
  953. if (lckindex[i] == qtype)
  954. return i;
  955. return 0;
  956. }
  957. /* is mp a cachable negative response (with Rname set)? */
  958. static int
  959. isnegrname(DNSmsg *mp)
  960. {
  961. /* TODO: could add || cfg.justforw to RHS of && */
  962. return mp->an == nil && (mp->flags & Rmask) == Rname;
  963. }
  964. static int
  965. procansw(Query *qp, DNSmsg *mp, uchar *srcip, int depth, Dest *p)
  966. {
  967. int rv;
  968. // int lcktype;
  969. char buf[32];
  970. DN *ndp;
  971. Query *nqp;
  972. RR *tp, *soarr;
  973. if (mp->an == nil)
  974. stats.negans++;
  975. /* ignore any error replies */
  976. if((mp->flags & Rmask) == Rserver){
  977. stats.negserver++;
  978. freeanswers(mp);
  979. if(p != qp->curdest)
  980. p->code = Rserver;
  981. return -1;
  982. }
  983. /* ignore any bad delegations */
  984. if(mp->ns && baddelegation(mp->ns, qp->nsrp, srcip)){
  985. stats.negbaddeleg++;
  986. if(mp->an == nil){
  987. stats.negbdnoans++;
  988. freeanswers(mp);
  989. if(p != qp->curdest)
  990. p->code = Rserver;
  991. return -1;
  992. }
  993. rrfreelist(mp->ns);
  994. mp->ns = nil;
  995. }
  996. /* remove any soa's from the authority section */
  997. soarr = rrremtype(&mp->ns, Tsoa);
  998. /* incorporate answers */
  999. unique(mp->an);
  1000. unique(mp->ns);
  1001. unique(mp->ar);
  1002. if(mp->an)
  1003. rrattach(mp->an, (mp->flags & Fauth) != 0);
  1004. if(mp->ar)
  1005. rrattach(mp->ar, Notauthoritative);
  1006. if(mp->ns && !cfg.justforw){
  1007. ndp = mp->ns->owner;
  1008. rrattach(mp->ns, Notauthoritative);
  1009. } else {
  1010. ndp = nil;
  1011. rrfreelist(mp->ns);
  1012. mp->ns = nil;
  1013. }
  1014. /* free the question */
  1015. if(mp->qd) {
  1016. rrfreelist(mp->qd);
  1017. mp->qd = nil;
  1018. }
  1019. /*
  1020. * Any reply from an authoritative server,
  1021. * or a positive reply terminates the search.
  1022. * A negative response now also terminates the search.
  1023. */
  1024. if(mp->an != nil || (mp->flags & Fauth)){
  1025. if(isnegrname(mp))
  1026. qp->dp->respcode = Rname;
  1027. else
  1028. qp->dp->respcode = 0;
  1029. /*
  1030. * cache any negative responses, free soarr.
  1031. * negative responses need not be authoritative:
  1032. * they can legitimately come from a cache.
  1033. */
  1034. if( /* (mp->flags & Fauth) && */ mp->an == nil)
  1035. cacheneg(qp->dp, qp->type, (mp->flags & Rmask), soarr);
  1036. else
  1037. rrfreelist(soarr);
  1038. return 1;
  1039. } else if (isnegrname(mp)) {
  1040. qp->dp->respcode = Rname;
  1041. /*
  1042. * cache negative response.
  1043. * negative responses need not be authoritative:
  1044. * they can legitimately come from a cache.
  1045. */
  1046. cacheneg(qp->dp, qp->type, (mp->flags & Rmask), soarr);
  1047. return 1;
  1048. }
  1049. stats.negnorname++;
  1050. rrfreelist(soarr);
  1051. /*
  1052. * if we've been given better name servers, recurse.
  1053. * if we're a pure resolver, don't recurse, we have
  1054. * to forward to a fixed set of named servers.
  1055. */
  1056. if(!mp->ns || cfg.resolver && cfg.justforw)
  1057. return 0;
  1058. tp = rrlookup(ndp, Tns, NOneg);
  1059. if(contains(qp->nsrp, tp)){
  1060. rrfreelist(tp);
  1061. return 0;
  1062. }
  1063. procsetname("recursive query for %s %s", qp->dp->name,
  1064. rrname(qp->type, buf, sizeof buf));
  1065. /*
  1066. * we're called from udpquery, called from
  1067. * netquery, which current holds qp->dp->querylck,
  1068. * so release it now and acquire it upon return.
  1069. */
  1070. // lcktype = qtype2lck(qp->type);
  1071. // qunlock(&qp->dp->querylck[lcktype]);
  1072. nqp = emalloc(sizeof *nqp);
  1073. queryinit(nqp, qp->dp, qp->type, qp->req);
  1074. nqp->nsrp = tp;
  1075. rv = netquery(nqp, depth+1);
  1076. // qlock(&qp->dp->querylck[lcktype]);
  1077. rrfreelist(nqp->nsrp);
  1078. querydestroy(nqp);
  1079. free(nqp);
  1080. return rv;
  1081. }
  1082. /*
  1083. * send a query via tcp to a single address (from ibuf's udp header)
  1084. * and read the answer(s) into mp->an.
  1085. */
  1086. static int
  1087. tcpquery(Query *qp, DNSmsg *mp, int depth, uchar *ibuf, uchar *obuf, int len,
  1088. int waitsecs, int inns, ushort req)
  1089. {
  1090. int rv = 0;
  1091. ulong endtime;
  1092. endtime = time(nil) + waitsecs;
  1093. if(endtime > qp->req->aborttime)
  1094. endtime = qp->req->aborttime;
  1095. if (0)
  1096. dnslog("%s: udp reply truncated; retrying query via tcp to %I",
  1097. qp->dp->name, qp->tcpip);
  1098. qlock(&qp->tcplock);
  1099. memmove(obuf, ibuf, IPaddrlen); /* send back to respondent */
  1100. /* sets qp->tcpip from obuf's udp header */
  1101. if (xmitquery(qp, Tcp, depth, obuf, inns, len) < 0 ||
  1102. readreply(qp, Tcp, req, ibuf, mp, endtime) < 0)
  1103. rv = -1;
  1104. if (qp->tcpfd > 0) {
  1105. hangup(qp->tcpctlfd);
  1106. close(qp->tcpctlfd);
  1107. close(qp->tcpfd);
  1108. }
  1109. qp->tcpfd = qp->tcpctlfd = -1;
  1110. qunlock(&qp->tcplock);
  1111. return rv;
  1112. }
  1113. /*
  1114. * query name servers. If the name server returns a pointer to another
  1115. * name server, recurse.
  1116. */
  1117. static int
  1118. queryns(Query *qp, int depth, uchar *ibuf, uchar *obuf, int waitsecs, int inns)
  1119. {
  1120. int ndest, len, replywaits, rv;
  1121. ushort req;
  1122. ulong endtime;
  1123. char buf[12];
  1124. uchar srcip[IPaddrlen];
  1125. Dest *p, *np, *dest;
  1126. // Dest dest[Maxdest];
  1127. /* pack request into a udp message */
  1128. req = rand();
  1129. len = mkreq(qp->dp, qp->type, obuf, Frecurse|Oquery, req);
  1130. /* no server addresses yet */
  1131. queryck(qp);
  1132. dest = emalloc(Maxdest * sizeof *dest); /* dest can't be on stack */
  1133. for (p = dest; p < dest + Maxdest; p++)
  1134. destinit(p);
  1135. /* this dest array is local to this call of queryns() */
  1136. free(qp->dest);
  1137. qp->curdest = qp->dest = dest;
  1138. /*
  1139. * transmit udp requests and wait for answers.
  1140. * at most Maxtrans attempts to each address.
  1141. * each cycle send one more message than the previous.
  1142. * retry a query via tcp if its response is truncated.
  1143. */
  1144. for(ndest = 1; ndest < Maxdest; ndest++){
  1145. qp->ndest = ndest;
  1146. qp->tcpset = 0;
  1147. if (xmitquery(qp, Udp, depth, obuf, inns, len) < 0)
  1148. break;
  1149. endtime = time(nil) + waitsecs;
  1150. if(endtime > qp->req->aborttime)
  1151. endtime = qp->req->aborttime;
  1152. for(replywaits = 0; replywaits < ndest; replywaits++){
  1153. DNSmsg m;
  1154. procsetname("reading %sside reply from %I: %s %s from %s",
  1155. (inns? "in": "out"), obuf, qp->dp->name,
  1156. rrname(qp->type, buf, sizeof buf), qp->req->from);
  1157. /* read udp answer into m */
  1158. if (readreply(qp, Udp, req, ibuf, &m, endtime) >= 0)
  1159. memmove(srcip, ibuf, IPaddrlen);
  1160. else if (!(m.flags & Ftrunc)) {
  1161. freeanswers(&m);
  1162. break; /* timed out on this dest */
  1163. } else {
  1164. /* whoops, it was truncated! ask again via tcp */
  1165. freeanswers(&m);
  1166. rv = tcpquery(qp, &m, depth, ibuf, obuf, len,
  1167. waitsecs, inns, req); /* answer in m */
  1168. if (rv < 0) {
  1169. freeanswers(&m);
  1170. break; /* failed via tcp too */
  1171. }
  1172. memmove(srcip, qp->tcpip, IPaddrlen);
  1173. }
  1174. /* find responder */
  1175. // dnslog("queryns got reply from %I", srcip);
  1176. for(p = qp->dest; p < qp->curdest; p++)
  1177. if(memcmp(p->a, srcip, sizeof p->a) == 0)
  1178. break;
  1179. /* remove all addrs of responding server from list */
  1180. for(np = qp->dest; np < qp->curdest; np++)
  1181. if(np->s == p->s)
  1182. p->nx = Maxtrans;
  1183. /* free or incorporate RRs in m */
  1184. rv = procansw(qp, &m, srcip, depth, p);
  1185. if (rv > 0) {
  1186. free(qp->dest);
  1187. qp->dest = qp->curdest = nil; /* prevent accidents */
  1188. return rv;
  1189. }
  1190. }
  1191. }
  1192. /* if all servers returned failure, propagate it */
  1193. qp->dp->respcode = Rserver;
  1194. for(p = dest; p < qp->curdest; p++) {
  1195. destck(p);
  1196. if(p->code != Rserver)
  1197. qp->dp->respcode = 0;
  1198. p->magic = 0; /* prevent accidents */
  1199. }
  1200. // if (qp->dp->respcode)
  1201. // dnslog("queryns setting Rserver for %s", qp->dp->name);
  1202. free(qp->dest);
  1203. qp->dest = qp->curdest = nil; /* prevent accidents */
  1204. return 0;
  1205. }
  1206. /*
  1207. * run a command with a supplied fd as standard input
  1208. */
  1209. char *
  1210. system(int fd, char *cmd)
  1211. {
  1212. int pid, p, i;
  1213. static Waitmsg msg;
  1214. if((pid = fork()) == -1)
  1215. sysfatal("fork failed: %r");
  1216. else if(pid == 0){
  1217. dup(fd, 0);
  1218. close(fd);
  1219. for (i = 3; i < 200; i++)
  1220. close(i); /* don't leak fds */
  1221. execl("/bin/rc", "rc", "-c", cmd, nil);
  1222. sysfatal("exec rc: %r");
  1223. }
  1224. for(p = waitpid(); p >= 0; p = waitpid())
  1225. if(p == pid)
  1226. return msg.msg;
  1227. return "lost child";
  1228. }
  1229. /* compute wait, weighted by probability of success, with minimum */
  1230. static ulong
  1231. weight(ulong ms, unsigned pcntprob)
  1232. {
  1233. ulong wait;
  1234. wait = (ms * pcntprob) / 100;
  1235. if (wait < 1500)
  1236. wait = 1500;
  1237. return wait;
  1238. }
  1239. /*
  1240. * in principle we could use a single descriptor for a udp port
  1241. * to send all queries and receive all the answers to them,
  1242. * but we'd have to sort out the answers by dns-query id.
  1243. */
  1244. static int
  1245. udpquery(Query *qp, char *mntpt, int depth, int patient, int inns)
  1246. {
  1247. int fd, rv;
  1248. long now;
  1249. ulong pcntprob, wait, reqtm;
  1250. char *msg;
  1251. uchar *obuf, *ibuf;
  1252. static QLock mntlck;
  1253. static ulong lastmount;
  1254. /* use alloced buffers rather than ones from the stack */
  1255. // ibuf = emalloc(Maxudpin+Udphdrsize);
  1256. ibuf = emalloc(64*1024); /* max. tcp reply size */
  1257. obuf = emalloc(Maxudp+Udphdrsize);
  1258. fd = udpport(mntpt);
  1259. while (fd < 0 && cfg.straddle && strcmp(mntpt, "/net.alt") == 0) {
  1260. /* HACK: remount /net.alt */
  1261. now = time(nil);
  1262. if (now < lastmount + Remntretry)
  1263. sleep((lastmount + Remntretry - now)*1000);
  1264. qlock(&mntlck);
  1265. fd = udpport(mntpt); /* try again under lock */
  1266. if (fd < 0) {
  1267. dnslog("[%d] remounting /net.alt", getpid());
  1268. unmount(nil, "/net.alt");
  1269. msg = system(open("/dev/null", ORDWR), "outside");
  1270. lastmount = time(nil);
  1271. if (msg && *msg) {
  1272. dnslog("[%d] can't remount /net.alt: %s",
  1273. getpid(), msg);
  1274. sleep(10*1000); /* don't spin wildly */
  1275. } else
  1276. fd = udpport(mntpt);
  1277. }
  1278. qunlock(&mntlck);
  1279. }
  1280. if (fd < 0) {
  1281. dnslog("can't get udpport for %s query of name %s: %r",
  1282. mntpt, qp->dp->name);
  1283. sysfatal("out of udp conversations"); /* we're buggered */
  1284. }
  1285. /*
  1286. * Our QIP servers are busted, don't answer AAAA and
  1287. * take forever to answer CNAME if there isn't one.
  1288. * They rarely set Rname.
  1289. * make time-to-wait proportional to estimated probability of an
  1290. * RR of that type existing.
  1291. */
  1292. if (qp->type >= nelem(likely))
  1293. pcntprob = 35; /* unpopular query type */
  1294. else
  1295. pcntprob = likely[qp->type];
  1296. reqtm = (patient? 2*Maxreqtm: Maxreqtm);
  1297. /* time for a single outgoing udp query */
  1298. wait = weight(S2MS(reqtm)/3, pcntprob);
  1299. qp->req->aborttime = time(nil) + MS2S(3*wait); /* for all udp queries */
  1300. qp->udpfd = fd;
  1301. rv = queryns(qp, depth, ibuf, obuf, MS2S(wait), inns);
  1302. close(fd);
  1303. qp->udpfd = -1;
  1304. free(obuf);
  1305. free(ibuf);
  1306. return rv;
  1307. }
  1308. /* look up (qp->dp->name,qp->type) rr in dns, via *nsrp with results in *reqp */
  1309. static int
  1310. netquery(Query *qp, int depth)
  1311. {
  1312. int lock, rv, triedin, inname, cnt;
  1313. // char buf[32];
  1314. RR *rp;
  1315. DN *dp;
  1316. Querylck *qlp;
  1317. static int whined;
  1318. rv = 0; /* pessimism */
  1319. if(depth > 12) /* in a recursive loop? */
  1320. return 0;
  1321. slave(qp->req);
  1322. /*
  1323. * slave might have forked. if so, the parent process longjmped to
  1324. * req->mret; we're usually the child slave, but if there are too
  1325. * many children already, we're still the same process.
  1326. */
  1327. /*
  1328. * don't lock before call to slave so only children can block.
  1329. * just lock at top-level invocation.
  1330. */
  1331. lock = depth <= 1 && qp->req->isslave;
  1332. dp = qp->dp; /* ensure that it doesn't change underfoot */
  1333. qlp = nil;
  1334. if(lock) {
  1335. // procsetname("query lock wait: %s %s from %s", dp->name,
  1336. // rrname(qp->type, buf, sizeof buf), qp->req->from);
  1337. /*
  1338. * don't make concurrent queries for this name.
  1339. * dozens of processes blocking here probably indicates
  1340. * an error in our dns data that causes us to not
  1341. * recognise a zone (area) as one of our own, thus
  1342. * causing us to query other nameservers.
  1343. */
  1344. qlp = &dp->querylck[qtype2lck(qp->type)];
  1345. incref(qlp);
  1346. qlock(qlp);
  1347. cnt = qlp->Ref.ref;
  1348. qunlock(qlp);
  1349. if (cnt > 10) {
  1350. decref(qlp);
  1351. if (!whined) {
  1352. whined = 1;
  1353. dnslog("too many outstanding queries for %s;"
  1354. " dropping this one; no further logging"
  1355. " of drops", dp->name);
  1356. }
  1357. return 0;
  1358. }
  1359. }
  1360. procsetname("netquery: %s", dp->name);
  1361. /* prepare server RR's for incremental lookup */
  1362. for(rp = qp->nsrp; rp; rp = rp->next)
  1363. rp->marker = 0;
  1364. triedin = 0;
  1365. /*
  1366. * normal resolvers and servers will just use mntpt for all addresses,
  1367. * even on the outside. straddling servers will use mntpt (/net)
  1368. * for inside addresses and /net.alt for outside addresses,
  1369. * thus bypassing other inside nameservers.
  1370. */
  1371. inname = insideaddr(dp->name);
  1372. if (!cfg.straddle || inname) {
  1373. rv = udpquery(qp, mntpt, depth, Hurry, (cfg.inside? Inns: Outns));
  1374. triedin = 1;
  1375. }
  1376. /*
  1377. * if we're still looking, are inside, and have an outside domain,
  1378. * try it on our outside interface, if any.
  1379. */
  1380. if (rv == 0 && cfg.inside && !inname) {
  1381. if (triedin)
  1382. dnslog(
  1383. "[%d] netquery: internal nameservers failed for %s; trying external",
  1384. getpid(), dp->name);
  1385. /* prepare server RR's for incremental lookup */
  1386. for(rp = qp->nsrp; rp; rp = rp->next)
  1387. rp->marker = 0;
  1388. rv = udpquery(qp, "/net.alt", depth, Patient, Outns);
  1389. }
  1390. // if (rv == 0) /* could ask /net.alt/dns directly */
  1391. // askoutdns(dp, qp->type);
  1392. if(lock && qlp)
  1393. decref(qlp);
  1394. return rv;
  1395. }
  1396. int
  1397. seerootns(void)
  1398. {
  1399. int rv;
  1400. char root[] = "";
  1401. Request req;
  1402. Query *qp;
  1403. memset(&req, 0, sizeof req);
  1404. req.isslave = 1;
  1405. req.aborttime = now + Maxreqtm;
  1406. req.from = "internal";
  1407. qp = emalloc(sizeof *qp);
  1408. queryinit(qp, dnlookup(root, Cin, 1), Tns, &req);
  1409. qp->nsrp = dblookup(root, Cin, Tns, 0, 0);
  1410. rv = netquery(qp, 0);
  1411. rrfreelist(qp->nsrp);
  1412. querydestroy(qp);
  1413. free(qp);
  1414. return rv;
  1415. }