host.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2010 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 <dns/buffer.h>
  23. #include <dns/name.h>
  24. #include <dns/message.h>
  25. #include <dns/messagerenderer.h>
  26. #include <dns/rrclass.h>
  27. #include <dns/rrtype.h>
  28. #include <dns/rrset.h>
  29. #include <dns/message.h>
  30. using namespace std;
  31. using namespace isc::dns;
  32. namespace {
  33. char* dns_type = NULL; // not set, so A, AAAA, MX
  34. const char* server = "127.0.0.1";
  35. const char* server_port = "53";
  36. int verbose = 0;
  37. int first_time = 1;
  38. bool recursive_bit = true;
  39. struct timeval before_time, after_time;
  40. int
  41. host_lookup(const char* const name, const char* const type) {
  42. Message msg(Message::RENDER);
  43. msg.setQid(0); // does this matter?
  44. // TODO: add switch for this
  45. msg.setOpcode(Opcode::QUERY());
  46. msg.setRcode(Rcode::NOERROR());
  47. if (recursive_bit) {
  48. msg.setHeaderFlag(MessageFlag::RD()); // set recursive bit
  49. }
  50. msg.addQuestion(Question(Name(name),
  51. RRClass::IN(), // IN class only for now
  52. RRType(type))); // if NULL then:
  53. OutputBuffer obuffer(512);
  54. MessageRenderer renderer(obuffer);
  55. msg.toWire(renderer);
  56. struct addrinfo hints, *res;
  57. int e;
  58. memset(&hints, 0, sizeof(hints));
  59. hints.ai_family = AF_UNSPEC;
  60. hints.ai_socktype = SOCK_DGRAM;
  61. hints.ai_flags = 0; // not using AI_NUMERICHOST in case to bootstrap
  62. e = getaddrinfo(server, server_port, &hints, &res);
  63. if (verbose) {
  64. cout << "Trying \"" << name << "\"\n";
  65. }
  66. if (verbose && first_time) {
  67. // this is only output the first time
  68. first_time = 0;
  69. cout << "Using domain server:\n";
  70. cout << "Name: " << server << "\n";
  71. // TODO: I guess I have to do a lookup to get that address and aliases
  72. // too
  73. //cout << "Address: " << address << "\n" ; // "#" << port << "\n";
  74. //cout << "Aliases: " << server << "\n";
  75. }
  76. int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  77. if (s < 0) {
  78. cerr << "failed to open socket" << endl;
  79. return (1);
  80. }
  81. if (verbose) {
  82. gettimeofday(&before_time, NULL);
  83. }
  84. sendto(s, obuffer.getData(), obuffer.getLength(), 0, res->ai_addr,
  85. res->ai_addrlen);
  86. struct sockaddr_storage ss;
  87. struct sockaddr* sa;
  88. socklen_t sa_len;
  89. sa_len = sizeof(ss);
  90. sa = static_cast<struct sockaddr*>((void*)&ss);
  91. char recvbuf[4096];
  92. int cc;
  93. if ((cc = recvfrom(s, recvbuf, sizeof(recvbuf), 0, sa, &sa_len)) > 0) {
  94. try {
  95. Message rmsg(Message::PARSE);
  96. InputBuffer ibuffer(recvbuf, cc);
  97. rmsg.fromWire(ibuffer);
  98. if (!verbose) {
  99. for (RRsetIterator it = rmsg.beginSection(Section::ANSWER());
  100. it != rmsg.endSection(Section::ANSWER());
  101. ++it) {
  102. if ((*it)->getType() != RRType::A()) {
  103. continue;
  104. }
  105. RdataIteratorPtr rit = (*it)->getRdataIterator();
  106. for (rit->first(); !rit->isLast(); rit->next()) {
  107. // instead of using my name, maybe use returned label?
  108. cout << name << " has address " <<
  109. (*rit).getCurrent().toText() << endl;
  110. }
  111. }
  112. } else {
  113. gettimeofday(&after_time, NULL);
  114. // HEADER and QUESTION, ANSWER, AUTHORITY, and ADDITIONAL
  115. std::cout << rmsg.toText() << std::endl;
  116. if (before_time.tv_usec > after_time.tv_usec) {
  117. after_time.tv_usec += 1000000;
  118. --after_time.tv_sec;
  119. }
  120. int elapsed_time =
  121. (after_time.tv_sec - before_time.tv_sec)
  122. + ((after_time.tv_usec - before_time.tv_usec))/1000;
  123. // TODO: if NXDOMAIN, host(1) doesn't show HEADER
  124. // Host hsdjkfhksjhdfkj not found: 3(NXDOMAIN)
  125. // TODO: figure out the new libdns way to test if NXDOMAIN
  126. std::cout << "Received " << cc <<
  127. " bytes in " << elapsed_time << " ms\n";
  128. // TODO: " bytes from 127.0.0.1#53 in 0 ms
  129. } //verbose
  130. } catch (const exception& ex) {
  131. std::cerr << "parse failed for " <<
  132. string(name) << "/" << type << ": " << ex.what() << std::endl;
  133. } catch (...) {
  134. std::cerr << "parse failed for " << string(name) << "/" << type;
  135. }
  136. }
  137. freeaddrinfo(res);
  138. return (0);
  139. } // host_lookup()
  140. }
  141. int
  142. main(int argc, char* argv[]) {
  143. int c;
  144. while ((c = getopt(argc, argv, "p:rt:v")) != -1)
  145. switch (c) {
  146. case 'r':
  147. recursive_bit = false;
  148. break;
  149. case 't':
  150. dns_type = optarg;
  151. break;
  152. case 'p':
  153. server_port = optarg;
  154. break;
  155. case 'v':
  156. verbose = 1;
  157. break;
  158. }
  159. argc -= optind;
  160. argv += optind;
  161. if (argc < 1) {
  162. cout << "Usage: host [-vr] [-t type] hostname [server]\n";
  163. exit(1);
  164. }
  165. if (argc >= 2) {
  166. server = argv[1];
  167. }
  168. if (dns_type == NULL) {
  169. host_lookup(argv[0], "A");
  170. // TODO: don't do next if A doesn't exist
  171. host_lookup(argv[0], "AAAA");
  172. host_lookup(argv[0], "MX");
  173. } else {
  174. host_lookup(argv[0], dns_type);
  175. }
  176. return (0);
  177. }