Browse Source

Don't set default lookup type, so will do A, AAAA, MX (MX not done in lib yet).
Fix verbose output to mimic host(1) more.
Move lookup functionality to own function.


git-svn-id: svn://bind10.isc.org/svn/bind10/branches/f2f200910@174 e5f2f494-b856-4b98-b285-d166d9295462

Jeremy C. Reed 15 years ago
parent
commit
dc0fe08ec9
1 changed files with 84 additions and 64 deletions
  1. 84 64
      src/bin/host/host.cc

+ 84 - 64
src/bin/host/host.cc

@@ -11,92 +11,112 @@ using namespace std;    // I don't understand why this is needed for cout
 
 using namespace isc::dns;
 
-char* dns_type = "A";    // A is the default lookup type
+char* dns_type = NULL;    // not set, so A, AAAA, MX
 char* server = "127.0.0.1";
 int   verbose = 1;       // later make this an option and default to 0
+int   first_time = 1;
 
 int
-main(int argc, char* argv[])
+host_lookup(char* name, char* type)
 {
+
     Message msg;
 
-    if (argc < 2) {
-        cout << "Usage: host _hostname_\n";
-    }
-    else {
+    msg.setQid(0); // does this matter?
 
-        if (verbose) {
-            cout << "Trying \"" << argv[1] << "\"\n";
-        }
+// TODO: add switch for this
+    msg.setRD(true);    // set recursive bit
 
-        if (argc >= 3) {
-          server = argv[2];
-        }
+    msg.addQuestion(Name(name),
+    RRClass::IN,    // IN class only for now
+    RRType(type));  // if NULL then:
+// terminate called after throwing an instance of 'std::logic_error'
+//  what():  basic_string::_S_construct NULL not valid
 
-        msg.setQid(0); // does this matter?
 
-// TODO: add switch for this
-        msg.setRD(true);    // set recursive bit
-
-        msg.addQuestion(Name(argv[1]),
-            RRClass::IN,    // IN class only for now
-            RRType(dns_type));
-
-        msg.toWire();
-
-        struct addrinfo hints, *res;
-        int e;
-        memset(&hints, 0, sizeof(hints));
-        hints.ai_family = AF_UNSPEC;
-        hints.ai_socktype = SOCK_DGRAM;
-        hints.ai_flags = 0; // not using AI_NUMERICHOST in case to bootstrap
-        e = getaddrinfo(server, "53", &hints, &res);
-
-        if (verbose) {
-            cout << "Using domain server:\n";
-            cout << "Name: " << server << "\n";
-//            cout << "Address: " << server << "#" << port << "\n";
-//            cout << "Aliases: " << server << "\n";
-        }
+    msg.toWire();
 
+    struct addrinfo hints, *res;
+    int e;
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_UNSPEC;
+    hints.ai_socktype = SOCK_DGRAM;
+    hints.ai_flags = 0; // not using AI_NUMERICHOST in case to bootstrap
+    e = getaddrinfo(server, "53", &hints, &res);
 
-        int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+    if (verbose) {
+        cout << "Trying \"" << name << "\"\n";
+    }
 
-        if (s < 0) {
-            cerr << "failed to open socket" << endl;
-            return (1);
-        }
+    if (verbose && first_time) {
+        // this is only output the first time
+        first_time = 0;
+        cout << "Using domain server:\n";
+        cout << "Name: " << server << "\n";
+// TODO: I guess I have to do a lookup to get that address and aliases too
+//        cout << "Address: " << address << "\n" ; // "#" << port << "\n";
+//        cout << "Aliases: " << server << "\n";
+    }
 
-        msg.getBuffer().sendTo(s, *res->ai_addr, res->ai_addrlen);
-
-        Message rmsg;
-        struct sockaddr_storage ss;
-        struct sockaddr* sa;
-        socklen_t sa_len;
-
-        sa_len = sizeof(ss);
-        sa = static_cast<struct sockaddr*>((void*)&ss);
-        if (rmsg.getBuffer().recvFrom(s, sa, &sa_len) > 0) {
-            try {
-                rmsg.fromWire();
-                std::cout << rmsg.toText() << std::endl;
-            } catch (...) {
-                std::cerr << "parse failed" << std::endl;
-            }
-        }
+    int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+
+    if (s < 0) {
+        cerr << "failed to open socket" << endl;
+        return (1);
+    }
 
-// QUESTION SECTION:
+    msg.getBuffer().sendTo(s, *res->ai_addr, res->ai_addrlen);
 
+    Message rmsg;
+    struct sockaddr_storage ss;
+    struct sockaddr* sa;
+    socklen_t sa_len;
+
+    sa_len = sizeof(ss);
+    sa = static_cast<struct sockaddr*>((void*)&ss);
+    if (rmsg.getBuffer().recvFrom(s, sa, &sa_len) > 0) {
+        try {
+            rmsg.fromWire();
+            // HEADER and QUESTION SECTION:
+            std::cout << rmsg.toText() << std::endl;
 // ;; ANSWER SECTION:
+//        std::cout << rmsg.getBuffer().getSection(SECTION_ANSWER).toText();
+// SECTION_AUTHORITY
+// SECTION_ADDITIONAL
 
-        std::cout << "\nReceived " <<
-            boost::lexical_cast<string>(rmsg.getBuffer().getSize()) <<
-            " bytes\n";
+            std::cout << "\nReceived " <<
+                boost::lexical_cast<string>(rmsg.getBuffer().getSize()) <<
+                " bytes\n";
 // TODO: " bytes from 127.0.0.1#53 in 0 ms
+        } catch (...) {
+            std::cerr << "parse failed" << std::endl;
+        }
+    }
+
+    freeaddrinfo(res);
+
+} // host_lookup()
+
+int
+main(int argc, char* argv[])
+{
 
-// TODO: do this for A, AAAA, MX
+    if (argc < 2) {
+        cout << "Usage: host _hostname_\n";
+    }
+    else {
 
-        freeaddrinfo(res);
+        if (argc >= 3) {
+          server = argv[2];
+        }
+
+        if (!dns_type) {
+            host_lookup(argv[1], "A");
+            host_lookup(argv[1], "AAAA");
+//          host_lookup(argv[1], "MX");
+// No MX yet
+// terminate called after throwing an instance of 'isc::dns::DNSInvalidRRType'
+        }
 
     }