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 vwhoi*/
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);
70 * convert address into a reverse lookup address
73 mkptrname(char *ip, char *rip, int rlen)
79 if(strstr(ip, "in-addr.arpa") || strstr(ip, "IN-ADDR.ARPA")){
80 nstrcpy(rip, ip, rlen);
84 nstrcpy(buf, ip, sizeof buf);
94 memmove(np, p+1, len);
99 memmove(np, p+1, len);
101 strcpy(np, "in-addr.arpa");
105 nstrcpy(char *to, char *from, int len)
107 strncpy(to, from, len);
112 doquery(int fd, char *dn, char *type)
116 Ndbtuple *t, *first, *last;
119 snprint(buf, sizeof(buf), "!%s %s", dn, type);
120 if(write(fd, buf, strlen(buf)) < 0)
128 n = read(fd, buf, sizeof(buf)-2);
132 buf[n++] = '\n'; /* ndbparsline needs a trailing new line */
135 /* check for the error condition */
137 werrstr("%s", buf+1);
141 t = _ndbparseline(buf);
154 setmalloctag(first, getcallerpc(&fd));