dnsquery.c 2.1 KB

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