]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/dnsquery.c
libndb: apply eriks dnsquery() ipv6 reverse lookup patch
[plan9front.git] / sys / src / libndb / dnsquery.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ndb.h>
5 #include <ndbhf.h>
6 #include <ip.h>
7
8 static void mkptrname(char*, char*, int);
9 static Ndbtuple *doquery(int, char *dn, char *type);
10
11 /*
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.
14  *
15  *  return 0 if not found.
16  */
17 Ndbtuple*
18 dnsquery(char *net, char *val, char *type)
19 {
20         char rip[128];
21         char *p;
22         Ndbtuple *t;
23         int fd;
24
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)
27                 return nil;
28
29         if(net == nil)
30                 net = "/net";
31         snprint(rip, sizeof(rip), "%s/dns", net);
32         fd = open(rip, ORDWR);
33         if(fd < 0){
34                 if(strcmp(net, "/net") == 0)
35                         snprint(rip, sizeof(rip), "/srv/dns");
36                 else {
37                         snprint(rip, sizeof(rip), "/srv/dns%s", net);
38                         p = strrchr(rip, '/');
39                         *p = '_';
40                 }
41                 fd = open(rip, ORDWR);
42                 if(fd < 0)
43                         return nil;
44                 if(mount(fd, -1, net, MBEFORE, "") < 0){
45                         close(fd);
46                         return nil;
47                 }
48                 /* fd is now closed */
49                 snprint(rip, sizeof(rip), "%s/dns", net);
50                 fd = open(rip, ORDWR);
51                 if(fd < 0)
52                         return nil;
53         }
54
55         /* zero out the error string */
56         werrstr("");
57
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");
62         } else
63                 t = doquery(fd, val, type);
64
65         /*
66          * TODO: make fd static and keep it open to reduce 9P traffic
67          * walking to /net*^/dns.
68          */
69         close(fd);
70         ndbsetmalloctag(t, getcallerpc(&net));
71         return t;
72 }
73
74 /*
75  *  convert address into a reverse lookup address
76  */
77 static void
78 mkptrname(char *ip, char *rip, int rlen)
79 {
80         uchar a[IPaddrlen];
81         char *p, *e;
82         int i;
83
84         if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
85                 snprint(rip, rlen, "%s", ip);
86         else if(isv4(a))
87                 snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
88                         a[15], a[14], a[13], a[12]);
89         else{
90                 p = rip;
91                 e = rip + rlen;
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);
95                 }
96                 seprint(p, e, "ip6.arpa");
97         }
98 }
99
100 static Ndbtuple*
101 doquery(int fd, char *dn, char *type)
102 {
103         char buf[1024];
104         int n;
105         Ndbtuple *t, *first, *last;
106
107         seek(fd, 0, 0);
108         snprint(buf, sizeof(buf), "!%s %s", dn, type);
109         if(write(fd, buf, strlen(buf)) < 0)
110                 return nil;
111                 
112         seek(fd, 0, 0);
113
114         first = last = nil;
115         
116         for(;;){
117                 n = read(fd, buf, sizeof(buf)-2);
118                 if(n <= 0)
119                         break;
120                 if(buf[n-1] != '\n')
121                         buf[n++] = '\n';        /* ndbparsline needs a trailing new line */
122                 buf[n] = 0;
123
124                 /* check for the error condition */
125                 if(buf[0] == '!'){
126                         werrstr("%s", buf+1);
127                         return nil;
128                 }
129
130                 t = _ndbparseline(buf);
131                 if(t != nil){
132                         if(first)
133                                 last->entry = t;
134                         else
135                                 first = t;
136                         last = t;
137
138                         while(last->entry)
139                                 last = last->entry;
140                 }
141         }
142
143         ndbsetmalloctag(first, getcallerpc(&fd));
144         return first;
145 }