8 static void mkptrname(char*, char*, int);
9 static Ndbtuple *doquery(int, char *dn, char *type);
12 * search for a tuple that has the given 'attr=val' and also 'rattr=x'.
13 * copy 'x' into 'buf' and return the whole tuple.
15 * return 0 if not found.
18 dnsquery(char *net, char *val, char *type)
25 /* if the address is V4 or V6 null address, give up early */
26 if(strcmp(val, "::") == 0 || strcmp(val, "0.0.0.0") == 0)
31 snprint(rip, sizeof(rip), "%s/dns", net);
32 fd = open(rip, ORDWR);
34 if(strcmp(net, "/net") == 0)
35 snprint(rip, sizeof(rip), "/srv/dns");
37 snprint(rip, sizeof(rip), "/srv/dns%s", net);
38 p = strrchr(rip, '/');
41 fd = open(rip, ORDWR);
44 if(mount(fd, -1, net, MBEFORE, "") < 0){
48 /* fd is now closed */
49 snprint(rip, sizeof(rip), "%s/dns", net);
50 fd = open(rip, ORDWR);
55 /* zero out the error string */
58 /* if this is a reverse lookup, first lookup the domain name */
59 if(strcmp(type, "ptr") == 0){
60 mkptrname(val, rip, sizeof rip);
61 t = doquery(fd, rip, "ptr");
63 t = doquery(fd, val, type);
66 * TODO: make fd static and keep it open to reduce 9P traffic
67 * walking to /net*^/dns.
70 ndbsetmalloctag(t, getcallerpc(&net));
75 * convert address into a reverse lookup address
78 mkptrname(char *ip, char *rip, int rlen)
84 if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
85 snprint(rip, rlen, "%s", ip);
87 snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
88 a[15], a[14], a[13], a[12]);
92 for(i = 15; i >= 0; i--){
93 p = seprint(p, e, "%ux.", a[i]&0xf);
94 p = seprint(p, e, "%ux.", a[i]>>4);
96 seprint(p, e, "ip6.arpa");
101 doquery(int fd, char *dn, char *type)
105 Ndbtuple *t, *first, *last;
108 snprint(buf, sizeof(buf), "!%s %s", dn, type);
109 if(write(fd, buf, strlen(buf)) < 0)
117 n = read(fd, buf, sizeof(buf)-2);
121 buf[n++] = '\n'; /* ndbparsline needs a trailing new line */
124 /* check for the error condition */
126 werrstr("%s", buf+1);
130 t = _ndbparseline(buf);
137 while(last->entry != nil)