7 static void nstrcpy(char*, char*, int);
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")){
85 nstrcpy(rip, ip, rlen);
88 nstrcpy(buf, ip, sizeof buf);
90 /* truncate if result wont fit in rip[rlen] */
92 if((rlen-14) < sizeof(buf))
104 memmove(np, p+1, len);
109 memmove(np, p+1, len);
111 strcpy(np, "in-addr.arpa");
115 nstrcpy(char *to, char *from, int len)
117 strncpy(to, from, len);
122 doquery(int fd, char *dn, char *type)
126 Ndbtuple *t, *first, *last;
129 snprint(buf, sizeof(buf), "!%s %s", dn, type);
130 if(write(fd, buf, strlen(buf)) < 0)
138 n = read(fd, buf, sizeof(buf)-2);
142 buf[n++] = '\n'; /* ndbparsline needs a trailing new line */
145 /* check for the error condition */
147 werrstr("%s", buf+1);
151 t = _ndbparseline(buf);
164 ndbsetmalloctag(first, getcallerpc(&fd));