dnsquery.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* Portions of this file are Copyright (C) 2015-2018 Giacomo Tesio <giacomo@tesio.it>
  10. * See /doc/license/gpl-2.0.txt for details about the licensing.
  11. */
  12. #include <u.h>
  13. #include <lib9.h>
  14. #include <bio.h>
  15. #include <chartypes.h>
  16. #include <ndb.h>
  17. #include "dns.h"
  18. #include "ip.h"
  19. static int domount;
  20. static char *mtpt, *dns, *srv;
  21. static int
  22. setup(int argc, char **argv)
  23. {
  24. int fd;
  25. if(argc == 1){
  26. domount = 0;
  27. mtpt = argv[0];
  28. }
  29. fd = sys_open(dns, ORDWR);
  30. if(fd < 0){
  31. if(domount == 0)
  32. sysfatal("can't open %s: %r", dns);
  33. fd = sys_open(srv, ORDWR);
  34. if(fd < 0)
  35. sysfatal("can't open %s: %r", srv);
  36. if(sys_mount(fd, -1, mtpt, MBEFORE, "", '9') < 0)
  37. sysfatal("can't sys_mount(%s, %s): %r", srv, mtpt);
  38. fd = sys_open(dns, ORDWR);
  39. if(fd < 0)
  40. sysfatal("can't open %s: %r", dns);
  41. }
  42. return fd;
  43. }
  44. static void
  45. querydns(int fd, char *line, int n)
  46. {
  47. char buf[1024];
  48. sys_seek(fd, 0, 0);
  49. if(jehanne_write(fd, line, n) != n) {
  50. print("!%r\n");
  51. return;
  52. }
  53. sys_seek(fd, 0, 0);
  54. buf[0] = '\0';
  55. while((n = jehanne_read(fd, buf, sizeof(buf))) > 0){
  56. buf[n] = '\0';
  57. print("%s\n", buf);
  58. }
  59. }
  60. static void
  61. query(int fd)
  62. {
  63. int n, len;
  64. char *lp, *p, *np;
  65. char buf[1024], line[1024];
  66. Biobuf in;
  67. Binit(&in, 0, OREAD);
  68. for(print("> "); lp = Brdline(&in, '\n'); print("> ")){
  69. n = Blinelen(&in) -1;
  70. while(isspace(lp[n]))
  71. lp[n--] = 0;
  72. n++;
  73. while(isspace(*lp)){
  74. lp++;
  75. n--;
  76. }
  77. if(!*lp)
  78. continue;
  79. strcpy(line, lp);
  80. /* default to an "ip" request if alpha, "ptr" if numeric */
  81. if(strchr(line, ' ') == nil)
  82. if(strcmp(ipattr(line), "ip") == 0) {
  83. strcat(line, " ptr");
  84. n += 4;
  85. } else {
  86. strcat(line, " ip");
  87. n += 3;
  88. }
  89. /* inverse queries may need to be permuted */
  90. if(n > 4 && strcmp(" ptr", &line[n-4]) == 0 &&
  91. cistrstr(line, ".arpa") == nil){
  92. /* TODO: reversing v6 addrs is harder */
  93. for(p = line; *p; p++)
  94. if(*p == ' '){
  95. *p = '.';
  96. break;
  97. }
  98. np = buf;
  99. len = 0;
  100. while(p >= line){
  101. len++;
  102. p--;
  103. if(*p == '.'){
  104. memmove(np, p+1, len);
  105. np += len;
  106. len = 0;
  107. }
  108. }
  109. memmove(np, p+1, len);
  110. np += len;
  111. strcpy(np, "in-addr.arpa ptr"); /* TODO: ip6.arpa for v6 */
  112. strcpy(line, buf);
  113. n = strlen(line);
  114. }
  115. querydns(fd, line, n);
  116. }
  117. Bterm(&in);
  118. }
  119. void
  120. main(int argc, char *argv[])
  121. {
  122. mtpt = "/net";
  123. dns = "/net/dns";
  124. srv = "/srv/dns";
  125. domount = 1;
  126. ARGBEGIN {
  127. case 'x':
  128. mtpt = "/net.alt";
  129. dns = "/net.alt/dns";
  130. srv = "/srv/dns_net.alt";
  131. break;
  132. default:
  133. fprint(2, "usage: %s [-x] [dns-mount-point]\n", argv0);
  134. exits("usage");
  135. } ARGEND;
  136. query(setup(argc, argv));
  137. exits(0);
  138. }