host.cc 5.5 KB

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