URI:
       tdnsquery.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tdnsquery.c (1722B)
       ---
            1 #include <u.h>
            2 #include <libc.h>
            3 #include <bio.h>
            4 #include <ndb.h>
            5 #include <thread.h>
            6 #include <9pclient.h>
            7 #include "dns.h"
            8 #include "ip.h"
            9 
           10 void
           11 usage(void)
           12 {
           13         fprint(2, "usage: dnsquery [-x service]\n");
           14         threadexitsall("usage");
           15 }
           16 
           17 void
           18 threadmain(int argc, char *argv[])
           19 {
           20         CFid *fd;
           21         int n, len;
           22         Biobuf in;
           23         char line[1024], *lp, *p, *np, *dns;
           24         char buf[1024];
           25 
           26         dns = "dns";
           27         ARGBEGIN{
           28         case 'x':
           29                 dns = EARGF(usage());
           30                 break;
           31         default:
           32                 usage();
           33         }ARGEND;
           34 
           35         if(argc)
           36                 usage();
           37 
           38         fd = nsopen(dns, nil, "dns", ORDWR);
           39         if(fd == nil)
           40                 sysfatal("open %s!dns: %r", dns);
           41 
           42         Binit(&in, 0, OREAD);
           43         for(print("> "); lp = Brdline(&in, '\n'); print("> ")){
           44                 n = Blinelen(&in)-1;
           45                 strncpy(line, lp, n);
           46                 line[n] = 0;
           47                 if (n<=1)
           48                         continue;
           49                 /* default to an "ip" request if alpha, "ptr" if numeric */
           50                 if (strchr(line, ' ')==0) {
           51                         if(strcmp(ipattr(line), "ip") == 0) {
           52                                 strcat(line, " ptr");
           53                                 n += 4;
           54                         } else {
           55                                 strcat(line, " ip");
           56                                 n += 3;
           57                         }
           58                 }
           59 
           60                 /* inverse queries may need to be permuted */
           61                 if(n > 4 && strcmp("ptr", &line[n-3]) == 0
           62                 && strstr(line, "IN-ADDR") == 0 && strstr(line, "in-addr") == 0){
           63                         for(p = line; *p; p++)
           64                                 if(*p == ' '){
           65                                         *p = '.';
           66                                         break;
           67                                 }
           68                         np = buf;
           69                         len = 0;
           70                         while(p >= line){
           71                                 len++;
           72                                 p--;
           73                                 if(*p == '.'){
           74                                         memmove(np, p+1, len);
           75                                         np += len;
           76                                         len = 0;
           77                                 }
           78                         }
           79                         memmove(np, p+1, len);
           80                         np += len;
           81                         strcpy(np, "in-addr.arpa ptr");
           82                         strcpy(line, buf);
           83                         n = strlen(line);
           84                 }
           85 
           86                 fsseek(fd, 0, 0);
           87                 if(fswrite(fd, line, n) < 0) {
           88                         print("!%r\n");
           89                         continue;
           90                 }
           91                 fsseek(fd, 0, 0);
           92                 while((n = fsread(fd, buf, sizeof(buf))) > 0){
           93                         buf[n] = 0;
           94                         print("%s\n", buf);
           95                 }
           96         }
           97         threadexitsall(0);
           98 }