]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/dnsquery.c
fc45adff54cd2fbfeb59bf927f969764aded338d
[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
7 static void nstrcpy(char*, char*, int);
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         char buf[128];
81         char *p, *np;
82         int len;
83
84         if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa")){
85                 nstrcpy(rip, ip, rlen);
86                 return;
87         }
88         nstrcpy(buf, ip, sizeof buf);
89
90         /* truncate if result wont fit in rip[rlen] */
91         assert(rlen > 14);
92         if((rlen-14) < sizeof(buf))
93                 buf[rlen-14] = 0;
94
95         for(p = buf; *p; p++)
96                 ;
97         *p = '.';
98         np = rip;
99         len = 0;
100         while(p >= buf){
101                 len++;
102                 p--;
103                 if(*p == '.'){
104                         memmove(np, p+1, len);
105                         np += len;
106                         len = 0;
107                 }
108         }
109         memmove(np, p+1, len);
110         np += len;
111         strcpy(np, "in-addr.arpa");
112 }
113
114 static void
115 nstrcpy(char *to, char *from, int len)
116 {
117         strncpy(to, from, len);
118         to[len-1] = 0;
119 }
120
121 static Ndbtuple*
122 doquery(int fd, char *dn, char *type)
123 {
124         char buf[1024];
125         int n;
126         Ndbtuple *t, *first, *last;
127
128         seek(fd, 0, 0);
129         snprint(buf, sizeof(buf), "!%s %s", dn, type);
130         if(write(fd, buf, strlen(buf)) < 0)
131                 return nil;
132                 
133         seek(fd, 0, 0);
134
135         first = last = nil;
136         
137         for(;;){
138                 n = read(fd, buf, sizeof(buf)-2);
139                 if(n <= 0)
140                         break;
141                 if(buf[n-1] != '\n')
142                         buf[n++] = '\n';        /* ndbparsline needs a trailing new line */
143                 buf[n] = 0;
144
145                 /* check for the error condition */
146                 if(buf[0] == '!'){
147                         werrstr("%s", buf+1);
148                         return nil;
149                 }
150
151                 t = _ndbparseline(buf);
152                 if(t != nil){
153                         if(first)
154                                 last->entry = t;
155                         else
156                                 first = t;
157                         last = t;
158
159                         while(last->entry)
160                                 last = last->entry;
161                 }
162         }
163
164         ndbsetmalloctag(first, getcallerpc(&fd));
165         return first;
166 }