Blob
1 #include <u.h>2 #include <libc.h>3 #include <bio.h>4 #include <ndb.h>5 #include "dns.h"6 #include "ip.h"8 void9 main(int argc, char *argv[])10 {11 int fd, n, len, domount;12 Biobuf in;13 char line[1024], *lp, *p, *np, *mtpt, *srv, *dns;14 char buf[1024];16 dns = "/net/dns";17 mtpt = "/net";18 srv = "/srv/dns";19 domount = 1;20 ARGBEGIN {21 case 'x':22 dns = "/net.alt/dns";23 mtpt = "/net.alt";24 srv = "/srv/dns_net.alt";25 break;26 default:27 fprint(2, "usage: %s -x [dns-mount-point]\n", argv0);28 exits("usage");29 } ARGEND;31 if(argc == 1){32 domount = 0;33 mtpt = argv[0];34 }36 fd = open(dns, ORDWR);37 if(fd < 0){38 if(domount == 0){39 fprint(2, "can't open %s: %r\n", mtpt);40 exits(0);41 }42 fd = open(srv, ORDWR);43 if(fd < 0){44 print("can't open %s: %r\n", srv);45 exits(0);46 }47 if(mount(fd, -1, mtpt, MBEFORE, "") < 0){48 print("can't mount(%s, %s): %r\n", srv, mtpt);49 exits(0);50 }51 fd = open(mtpt, ORDWR);52 if(fd < 0){53 print("can't open %s: %r\n", mtpt);54 exits(0);55 }56 }57 Binit(&in, 0, OREAD);58 for(print("> "); lp = Brdline(&in, '\n'); print("> ")){59 n = Blinelen(&in)-1;60 strncpy(line, lp, n);61 line[n] = 0;62 if (n<=1)63 continue;64 /* default to an "ip" request if alpha, "ptr" if numeric */65 if (strchr(line, ' ')==0) {66 if(strcmp(ipattr(line), "ip") == 0) {67 strcat(line, " ptr");68 n += 4;69 } else {70 strcat(line, " ip");71 n += 3;72 }73 }75 /* inverse queries may need to be permuted */76 if(n > 4 && strcmp("ptr", &line[n-3]) == 077 && strstr(line, "IN-ADDR") == 0 && strstr(line, "in-addr") == 0){78 for(p = line; *p; p++)79 if(*p == ' '){80 *p = '.';81 break;82 }83 np = buf;84 len = 0;85 while(p >= line){86 len++;87 p--;88 if(*p == '.'){89 memmove(np, p+1, len);90 np += len;91 len = 0;92 }93 }94 memmove(np, p+1, len);95 np += len;96 strcpy(np, "in-addr.arpa ptr");97 strcpy(line, buf);98 n = strlen(line);99 }101 seek(fd, 0, 0);102 if(write(fd, line, n) < 0) {103 print("!%r\n");104 continue;105 }106 seek(fd, 0, 0);107 while((n = read(fd, buf, sizeof(buf))) > 0){108 buf[n] = 0;109 print("%s\n", buf);110 }111 }112 exits(0);113 }