xref: /plan9/sys/src/cmd/ndb/dnsquery.c (revision 850dd0ca1bdf035e1410f3ad77ab360045f4571d)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <ndb.h>
6 #include "dns.h"
7 #include "ip.h"
8 
9 static int domount;
10 static char *mtpt, *dns, *srv;
11 
12 static int
13 setup(int argc, char **argv)
14 {
15 	int fd;
16 
17 	if(argc == 1){
18 		domount = 0;
19 		mtpt = argv[0];
20 	}
21 
22 	fd = open(dns, ORDWR);
23 	if(fd < 0){
24 		if(domount == 0){
25 			fprint(2, "can't open %s: %r\n", mtpt);
26 			exits(0);
27 		}
28 		fd = open(srv, ORDWR);
29 		if(fd < 0){
30 			print("can't open %s: %r\n", srv);
31 			exits(0);
32 		}
33 		if(mount(fd, -1, mtpt, MBEFORE, "") < 0){
34 			print("can't mount(%s, %s): %r\n", srv, mtpt);
35 			exits(0);
36 		}
37 		fd = open(mtpt, ORDWR);
38 		if(fd < 0){
39 			print("can't open %s: %r\n", mtpt);
40 			exits(0);
41 		}
42 	}
43 	return fd;
44 }
45 
46 static void
47 querydns(int fd, char *line, int n)
48 {
49 	char buf[1024];
50 
51 	seek(fd, 0, 0);
52 	if(write(fd, line, n) != n) {
53 		print("!%r\n");
54 		return;
55 	}
56 	seek(fd, 0, 0);
57 	buf[0] = '\0';
58 	while((n = read(fd, buf, sizeof(buf))) > 0){
59 		buf[n] = '\0';
60 		print("%s\n", buf);
61 	}
62 }
63 
64 static void
65 query(int fd)
66 {
67 	int n, len;
68 	char line[1024], *lp, *p, *np;
69 	char buf[1024];
70 	Biobuf in;
71 
72 	Binit(&in, 0, OREAD);
73 	for(print("> "); lp = Brdline(&in, '\n'); print("> ")){
74 		n = Blinelen(&in) -1;
75 		while(isspace(lp[n]))
76 			lp[n--] = 0;
77 		n++;
78 		while(isspace(*lp)){
79 			lp++;
80 			n--;
81 		}
82 		if(!*lp)
83 			continue;
84 		strcpy(line, lp);
85 
86 		/* default to an "ip" request if alpha, "ptr" if numeric */
87 		if(strchr(line, ' ') == nil)
88 			if(strcmp(ipattr(line), "ip") == 0) {
89 				strcat(line, " ptr");
90 				n += 4;
91 			} else {
92 				strcat(line, " ip");
93 				n += 3;
94 			}
95 
96 		/* inverse queries may need to be permuted */
97 		if(n > 4 && strcmp("ptr", &line[n-3]) == 0 &&
98 		    cistrstr(line, "in-addr") == 0){
99 			for(p = line; *p; p++)
100 				if(*p == ' '){
101 					*p = '.';
102 					break;
103 				}
104 			np = buf;
105 			len = 0;
106 			while(p >= line){
107 				len++;
108 				p--;
109 				if(*p == '.'){
110 					memmove(np, p+1, len);
111 					np += len;
112 					len = 0;
113 				}
114 			}
115 			memmove(np, p+1, len);
116 			np += len;
117 			strcpy(np, "in-addr.arpa ptr");
118 			strcpy(line, buf);
119 			n = strlen(line);
120 		}
121 
122 		querydns(fd, line, n);
123 	}
124 	Bterm(&in);
125 }
126 
127 void
128 main(int argc, char *argv[])
129 {
130 	mtpt = "/net";
131 	dns = "/net/dns";
132 	srv = "/srv/dns";
133 	domount = 1;
134 	ARGBEGIN {
135 	case 'x':
136 		mtpt = "/net.alt";
137 		dns = "/net.alt/dns";
138 		srv = "/srv/dns_net.alt";
139 		break;
140 	default:
141 		fprint(2, "usage: %s [-x] [dns-mount-point]\n", argv0);
142 		exits("usage");
143 	} ARGEND;
144 
145 	query(setup(argc, argv));
146 	exits(0);
147 }
148