1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
#include <ndbhf.h>
#include <ip.h>
static void mkptrname(char*, char*, int);
static Ndbtuple *doquery(int, char *dn, char *type);
/*
* search for a tuple that has the given 'attr=val' and also 'rattr=x'.
* copy 'x' into 'buf' and return the whole tuple.
*
* return nil if not found.
*/
Ndbtuple*
dnsquery(char *net, char *val, char *type)
{
char buf[128];
Ndbtuple *t;
int fd;
/* if the address is V4 or V6 null address, give up early */
if(strcmp(val, "::") == 0 || strcmp(val, "0.0.0.0") == 0)
return nil;
if(net == nil)
net = "/net";
snprint(buf, sizeof(buf), "%s/dns", net);
if((fd = open(buf, ORDWR)) < 0)
return nil;
/* zero out the error string */
werrstr("");
/* if this is a reverse lookup, first lookup the domain name */
if(strcmp(type, "ptr") == 0){
mkptrname(val, buf, sizeof buf);
t = doquery(fd, buf, "ptr");
} else
t = doquery(fd, val, type);
/*
* TODO: make fd static and keep it open to reduce 9P traffic
* walking to /net*^/dns.
*/
close(fd);
ndbsetmalloctag(t, getcallerpc(&net));
return t;
}
/*
* convert address into a reverse lookup address
*/
static void
mkptrname(char *ip, char *rip, int rlen)
{
uchar a[IPaddrlen];
char *p, *e;
int i;
if(cistrstr(ip, "in-addr.arpa") || cistrstr(ip, "ip6.arpa") || parseip(a, ip) == -1)
snprint(rip, rlen, "%s", ip);
else if(isv4(a))
snprint(rip, rlen, "%ud.%ud.%ud.%ud.in-addr.arpa",
a[15], a[14], a[13], a[12]);
else{
p = rip;
e = rip + rlen;
for(i = 15; i >= 0; i--){
p = seprint(p, e, "%ux.", a[i]&0xf);
p = seprint(p, e, "%ux.", a[i]>>4);
}
seprint(p, e, "ip6.arpa");
}
}
static Ndbtuple*
doquery(int fd, char *dn, char *type)
{
char buf[1024];
int n;
Ndbtuple *t, *first, *last;
seek(fd, 0, 0);
snprint(buf, sizeof(buf), "!%s %s", dn, type);
if(write(fd, buf, strlen(buf)) < 0)
return nil;
seek(fd, 0, 0);
first = last = nil;
for(;;){
n = read(fd, buf, sizeof(buf)-2);
if(n <= 0)
break;
if(buf[n-1] != '\n')
buf[n++] = '\n'; /* ndbparsline needs a trailing new line */
buf[n] = 0;
/* check for the error condition */
if(buf[0] == '!'){
werrstr("%s", buf+1);
return nil;
}
t = _ndbparseline(buf);
if(t != nil){
if(first != nil)
last->entry = t;
else
first = t;
last = t;
while(last->entry != nil)
last = last->entry;
}
}
return first;
}
|