dnsquery.c 2.4 KB

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