host.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright (C) 2010-2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // host rewritten in C++ using BIND 10 DNS library
  15. #include <arpa/inet.h>
  16. #include <netdb.h> // for getaddrinfo
  17. #include <sys/time.h> // for gettimeofday
  18. #include <sys/socket.h> // networking functions and definitions on FreeBSD
  19. #include <unistd.h>
  20. #include <string>
  21. #include <iostream>
  22. #include <util/buffer.h>
  23. #include <dns/name.h>
  24. #include <dns/message.h>
  25. #include <dns/messagerenderer.h>
  26. #include <dns/opcode.h>
  27. #include <dns/rcode.h>
  28. #include <dns/rrclass.h>
  29. #include <dns/rrtype.h>
  30. #include <dns/rrset.h>
  31. #include <dns/message.h>
  32. using namespace std;
  33. using namespace isc::dns;
  34. using namespace isc::util;
  35. namespace {
  36. char* dns_type = NULL; // not set, so A, AAAA, MX
  37. const char* server = "127.0.0.1";
  38. const char* server_port = "53";
  39. const char* dns_class = "IN";
  40. bool verbose = false;
  41. bool dns_any = false;
  42. int first_time = 1;
  43. bool recursive_bit = true;
  44. struct timeval before_time, after_time;
  45. int
  46. host_lookup(const char* const name, const char* const dns_class,
  47. const char* const type, bool any) {
  48. Message msg(Message::RENDER);
  49. msg.setQid(0); // does this matter?
  50. // TODO: add switch for this
  51. msg.setOpcode(Opcode::QUERY());
  52. msg.setRcode(Rcode::NOERROR());
  53. if (recursive_bit) {
  54. msg.setHeaderFlag(Message::HEADERFLAG_RD); // set recursive bit
  55. }
  56. msg.addQuestion(Question(Name(name),
  57. RRClass(dns_class),
  58. any ? RRType::ANY() : RRType(type))); // if NULL then:
  59. MessageRenderer renderer;
  60. msg.toWire(renderer);
  61. struct addrinfo hints, *res;
  62. memset(&hints, 0, sizeof(hints));
  63. hints.ai_family = AF_UNSPEC;
  64. hints.ai_socktype = SOCK_DGRAM;
  65. hints.ai_flags = 0; // not using AI_NUMERICHOST in case to bootstrap
  66. if (getaddrinfo(server, server_port, &hints, &res) != 0) {
  67. cerr << "address/port conversion for " << server << ":"
  68. << server_port << " failed" << endl;
  69. return (1);
  70. }
  71. if (verbose) {
  72. cout << "Trying \"" << name << "\"\n";
  73. }
  74. if (verbose && first_time) {
  75. // this is only output the first time
  76. first_time = 0;
  77. cout << "Using domain server:\n";
  78. cout << "Name: " << server << "\n";
  79. // TODO: I guess I have to do a lookup to get that address and aliases
  80. // too
  81. //cout << "Address: " << address << "\n" ;
  82. //cout << "Aliases: " << server << "\n";
  83. }
  84. int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  85. if (s < 0) {
  86. cerr << "failed to open socket" << endl;
  87. return (1);
  88. }
  89. if (verbose) {
  90. gettimeofday(&before_time, NULL);
  91. }
  92. sendto(s, renderer.getData(), renderer.getLength(), 0, res->ai_addr,
  93. res->ai_addrlen);
  94. struct sockaddr_storage ss;
  95. struct sockaddr* sa;
  96. socklen_t sa_len;
  97. sa_len = sizeof(ss);
  98. sa = static_cast<struct sockaddr*>((void*)&ss);
  99. char recvbuf[4096];
  100. int cc;
  101. if ((cc = recvfrom(s, recvbuf, sizeof(recvbuf), 0, sa, &sa_len)) > 0) {
  102. try {
  103. Message rmsg(Message::PARSE);
  104. InputBuffer ibuffer(recvbuf, cc);
  105. rmsg.fromWire(ibuffer);
  106. if (!verbose) {
  107. string description = "";
  108. for (RRsetIterator it =
  109. rmsg.beginSection(Message::SECTION_ANSWER);
  110. it != rmsg.endSection(Message::SECTION_ANSWER);
  111. ++it) {
  112. if ((*it)->getType() == RRType::A()) {
  113. description = "has address";
  114. }
  115. else if ((*it)->getType() == RRType::AAAA()) {
  116. description = "has IPv6 address";
  117. }
  118. else if ((*it)->getType() == RRType::MX()) {
  119. description = "mail is handled by";
  120. }
  121. else if ((*it)->getType() == RRType::TXT()) {
  122. description = "descriptive text";
  123. }
  124. RdataIteratorPtr rit = (*it)->getRdataIterator();
  125. for (; !rit->isLast(); rit->next()) {
  126. // instead of using my name, maybe use returned label?
  127. cout << name << " " << description << " " <<
  128. (*rit).getCurrent().toText() << endl;
  129. }
  130. }
  131. } else {
  132. gettimeofday(&after_time, NULL);
  133. // HEADER and QUESTION, ANSWER, AUTHORITY, and ADDITIONAL
  134. std::cout << rmsg.toText() << std::endl;
  135. if (before_time.tv_usec > after_time.tv_usec) {
  136. after_time.tv_usec += 1000000;
  137. --after_time.tv_sec;
  138. }
  139. int elapsed_time =
  140. (after_time.tv_sec - before_time.tv_sec)
  141. + ((after_time.tv_usec - before_time.tv_usec))/1000;
  142. // TODO: if NXDOMAIN, host(1) doesn't show HEADER
  143. // Host hsdjkfhksjhdfkj not found: 3(NXDOMAIN)
  144. // TODO: test if NXDOMAIN
  145. std::cout << "Received " << cc <<
  146. " bytes in " << elapsed_time << " ms\n";
  147. // TODO: " bytes from 127.0.0.1#53 in 0 ms
  148. } //verbose
  149. /*
  150. TODO: handle InvalidRRClass
  151. TODO: handle invalid type exception
  152. } catch (InvalidType ivt) {
  153. std::cerr << "invalid type:" << ivt.what();
  154. */
  155. } catch (const exception& ex) {
  156. std::cerr << "parse failed for " <<
  157. string(name) << "/" << type << ": " << ex.what() << std::endl;
  158. } catch (...) {
  159. std::cerr << "parse failed for " << string(name) << "/" << type;
  160. }
  161. }
  162. freeaddrinfo(res);
  163. return (0);
  164. } // host_lookup()
  165. }
  166. int
  167. main(int argc, char* argv[]) {
  168. int c;
  169. while ((c = getopt(argc, argv, "ac:dp:rt:v")) != -1)
  170. switch (c) {
  171. case 'a':
  172. dns_any = true;
  173. verbose = true;
  174. break;
  175. case 'c':
  176. dns_class = optarg;
  177. break;
  178. // p for port is a non-standard switch
  179. case 'p':
  180. server_port = optarg;
  181. break;
  182. case 'r':
  183. recursive_bit = false;
  184. break;
  185. case 't':
  186. dns_type = optarg;
  187. break;
  188. case 'd':
  189. // drop through to v, because debug and verbose are equivalent
  190. case 'v':
  191. verbose = true;
  192. break;
  193. }
  194. argc -= optind;
  195. argv += optind;
  196. if (argc < 1) {
  197. cout << "Usage: host [-adrv] [-c class] [-p port] [-t type] hostname [server]\n";
  198. exit(1);
  199. }
  200. if (argc >= 2) {
  201. server = argv[1];
  202. }
  203. if (dns_type == NULL) {
  204. host_lookup(argv[0], dns_class, "A", dns_any);
  205. // TODO: don't do next if A doesn't exist
  206. host_lookup(argv[0], dns_class, "AAAA", dns_any);
  207. host_lookup(argv[0], dns_class, "MX", dns_any);
  208. } else {
  209. // -t overrides -a, regardless of order
  210. host_lookup(argv[0], dns_class, dns_type, false);
  211. }
  212. return (0);
  213. }