]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/dnsquery.c
games/nes: workaround for truncated chr
[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 nil if not found.
16  */
17 Ndbtuple*
18 dnsquery(char *net, char *val, char *type)
19 {
20         char buf[128];
21         Ndbtuple *t;
22         int fd;
23
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)
26                 return nil;
27
28         if(net == nil)
29                 net = "/net";
30
31         snprint(buf, sizeof(buf), "%s/dns", net);
32         if((fd = open(buf, ORDWR)) < 0)
33                 return nil;
34
35         /* zero out the error string */
36         werrstr("");
37
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");
42         } else
43                 t = doquery(fd, val, type);
44
45         /*
46          * TODO: make fd static and keep it open to reduce 9P traffic
47          * walking to /net*^/dns.
48          */
49         close(fd);
50         ndbsetmalloctag(t, getcallerpc(&net));
51         return t;
52 }
53
54 /*
55  *  convert address into a reverse lookup address
56  */
57 static void
58 mkptrname(char *ip, char *rip, int rlen)
59 {
60         uchar a[IPaddrlen];
61         char *p, *e;
62         int i;
63
64         if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
65                 snprint(rip, rlen, "%s", ip);
66         else if(isv4(a))
67                 snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
68                         a[15], a[14], a[13], a[12]);
69         else{
70                 p = rip;
71                 e = rip + rlen;
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);
75                 }
76                 seprint(p, e, "ip6.arpa");
77         }
78 }
79
80 static Ndbtuple*
81 doquery(int fd, char *dn, char *type)
82 {
83         char buf[1024];
84         int n;
85         Ndbtuple *t, *first, *last;
86
87         seek(fd, 0, 0);
88         snprint(buf, sizeof(buf), "!%s %s", dn, type);
89         if(write(fd, buf, strlen(buf)) < 0)
90                 return nil;
91                 
92         seek(fd, 0, 0);
93
94         first = last = nil;
95         
96         for(;;){
97                 n = read(fd, buf, sizeof(buf)-2);
98                 if(n <= 0)
99                         break;
100                 if(buf[n-1] != '\n')
101                         buf[n++] = '\n';        /* ndbparsline needs a trailing new line */
102                 buf[n] = 0;
103
104                 /* check for the error condition */
105                 if(buf[0] == '!'){
106                         werrstr("%s", buf+1);
107                         return nil;
108                 }
109
110                 t = _ndbparseline(buf);
111                 if(t != nil){
112                         if(first != nil)
113                                 last->entry = t;
114                         else
115                                 first = t;
116                         last = t;
117                         while(last->entry != nil)
118                                 last = last->entry;
119                 }
120         }
121         return first;
122 }