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 nil if not found.
18 dnsquery(char *net, char *val, char *type)
24 /* if the address is V4 or V6 null address, give up early */
25 if(strcmp(val, "::") == 0 || strcmp(val, "0.0.0.0") == 0)
31 snprint(buf, sizeof(buf), "%s/dns", net);
32 if((fd = open(buf, ORDWR)) < 0)
35 /* zero out the error string */
38 /* if this is a reverse lookup, first lookup the domain name */
39 if(strcmp(type, "ptr") == 0){
40 mkptrname(val, buf, sizeof buf);
41 t = doquery(fd, buf, "ptr");
43 t = doquery(fd, val, type);
46 * TODO: make fd static and keep it open to reduce 9P traffic
47 * walking to /net*^/dns.
50 ndbsetmalloctag(t, getcallerpc(&net));
55 * convert address into a reverse lookup address
58 mkptrname(char *ip, char *rip, int rlen)
64 if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
65 snprint(rip, rlen, "%s", ip);
67 snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
68 a[15], a[14], a[13], a[12]);
72 for(i = 15; i >= 0; i--){
73 p = seprint(p, e, "%ux.", a[i]&0xf);
74 p = seprint(p, e, "%ux.", a[i]>>4);
76 seprint(p, e, "ip6.arpa");
81 doquery(int fd, char *dn, char *type)
85 Ndbtuple *t, *first, *last;
88 snprint(buf, sizeof(buf), "!%s %s", dn, type);
89 if(write(fd, buf, strlen(buf)) < 0)
97 n = read(fd, buf, sizeof(buf)-2);
101 buf[n++] = '\n'; /* ndbparsline needs a trailing new line */
104 /* check for the error condition */
106 werrstr("%s", buf+1);
110 t = _ndbparseline(buf);
117 while(last->entry != nil)