plan9port/src/cmd/ndb/dnsquery.c

99 lines
1.7 KiB
C
Raw Normal View History

2005-12-27 04:30:05 +00:00
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <ndb.h>
2006-02-14 19:43:19 +00:00
#include <thread.h>
2006-02-15 18:37:21 +00:00
#include <9pclient.h>
2005-12-27 04:30:05 +00:00
#include "dns.h"
#include "ip.h"
2006-02-14 19:43:19 +00:00
void
usage(void)
{
2006-02-20 19:38:29 +00:00
fprint(2, "usage: dnsquery [-x service]\n");
2006-02-14 19:43:19 +00:00
threadexitsall("usage");
}
2005-12-27 04:30:05 +00:00
void
2006-02-15 18:37:21 +00:00
threadmain(int argc, char *argv[])
2005-12-27 04:30:05 +00:00
{
2006-02-14 19:43:19 +00:00
CFid *fd;
int n, len;
2005-12-27 04:30:05 +00:00
Biobuf in;
2006-02-15 18:45:28 +00:00
char line[1024], *lp, *p, *np, *dns;
2005-12-27 04:30:05 +00:00
char buf[1024];
2006-02-14 19:43:19 +00:00
dns = "dns";
ARGBEGIN{
2005-12-27 04:30:05 +00:00
case 'x':
2006-02-14 19:43:19 +00:00
dns = EARGF(usage());
2005-12-27 04:30:05 +00:00
break;
default:
2006-02-14 19:43:19 +00:00
usage();
}ARGEND;
2005-12-27 04:30:05 +00:00
2006-02-14 19:43:19 +00:00
if(argc)
usage();
fd = nsopen(dns, nil, "dns", ORDWR);
if(fd == nil)
sysfatal("open %s!dns: %r", dns);
2005-12-27 04:30:05 +00:00
Binit(&in, 0, OREAD);
for(print("> "); lp = Brdline(&in, '\n'); print("> ")){
n = Blinelen(&in)-1;
strncpy(line, lp, n);
line[n] = 0;
if (n<=1)
continue;
/* default to an "ip" request if alpha, "ptr" if numeric */
if (strchr(line, ' ')==0) {
if(strcmp(ipattr(line), "ip") == 0) {
strcat(line, " ptr");
n += 4;
} else {
strcat(line, " ip");
n += 3;
}
}
/* inverse queries may need to be permuted */
if(n > 4 && strcmp("ptr", &line[n-3]) == 0
&& strstr(line, "IN-ADDR") == 0 && strstr(line, "in-addr") == 0){
for(p = line; *p; p++)
if(*p == ' '){
*p = '.';
break;
}
np = buf;
len = 0;
while(p >= line){
len++;
p--;
if(*p == '.'){
memmove(np, p+1, len);
np += len;
len = 0;
}
}
memmove(np, p+1, len);
np += len;
strcpy(np, "in-addr.arpa ptr");
strcpy(line, buf);
n = strlen(line);
}
2006-02-14 19:43:19 +00:00
fsseek(fd, 0, 0);
if(fswrite(fd, line, n) < 0) {
2005-12-27 04:30:05 +00:00
print("!%r\n");
continue;
}
2006-02-14 19:43:19 +00:00
fsseek(fd, 0, 0);
while((n = fsread(fd, buf, sizeof(buf))) > 0){
2005-12-27 04:30:05 +00:00
buf[n] = 0;
print("%s\n", buf);
}
}
2006-02-14 19:43:19 +00:00
threadexitsall(0);
2005-12-27 04:30:05 +00:00
}