host.cc 5.47 KB
Newer Older
1
// host rewritten in C++ using BIND 10 DNS library
Jeremy C. Reed's avatar
Jeremy C. Reed committed
2 3

#include <arpa/inet.h>
4
#include <netdb.h>          // for getaddrinfo
5
#include <sys/time.h>       // for gettimeofday
Jeremy C. Reed's avatar
Jeremy C. Reed committed
6

7
#include <string>
Jeremy C. Reed's avatar
Jeremy C. Reed committed
8 9
#include <iostream>

10 11 12 13 14 15 16 17
#include "dns/buffer.h"
#include "dns/name.h"
#include "dns/message.h"
#include "dns/messagerenderer.h"
#include "dns/rrclass.h"
#include "dns/rrtype.h"
#include "dns/rrset.h"
#include "dns/message.h"
Jeremy C. Reed's avatar
Jeremy C. Reed committed
18

19
using namespace std;
Jeremy C. Reed's avatar
Jeremy C. Reed committed
20 21
using namespace isc::dns;

22
char* dns_type = NULL;    // not set, so A, AAAA, MX
23
std::string server = "127.0.0.1";
Jeremy C. Reed's avatar
Jeremy C. Reed committed
24
int   verbose = 0;
25
int   first_time = 1;
Jeremy C. Reed's avatar
Jeremy C. Reed committed
26
bool  recursive_bit = true;
27
struct timeval before_time, after_time;
Jeremy C. Reed's avatar
Jeremy C. Reed committed
28 29

int
30
host_lookup(char* name, std::string type)
Jeremy C. Reed's avatar
Jeremy C. Reed committed
31
{
32

Jeremy C. Reed's avatar
Jeremy C. Reed committed
33 34
    Message msg;

35
    msg.setQid(0); // does this matter?
Jeremy C. Reed's avatar
Jeremy C. Reed committed
36

37
// TODO: add switch for this
38 39 40 41 42
    msg.setOpcode(Opcode::QUERY());
    msg.setRcode(Rcode::NOERROR());
    if (recursive_bit) {
        msg.setHeaderFlag(MessageFlag::RD());    // set recursive bit
    }
43

44 45 46
    msg.addQuestion(Question(Name(name),
                             RRClass::IN(),    // IN class only for now
                             RRType(type)));  // if NULL then:
47 48
// terminate called after throwing an instance of 'std::logic_error'
//  what():  basic_string::_S_construct NULL not valid
49

50 51 52
    OutputBuffer obuffer(512);
    MessageRenderer renderer(obuffer);
    msg.toWire(renderer);
53

54 55 56 57 58 59
    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
60
    e = getaddrinfo(server.c_str(), "53", &hints, &res);
61

62 63 64
    if (verbose) {
        cout << "Trying \"" << name << "\"\n";
    }
65

66 67 68 69 70 71 72 73 74
    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";
    }
Jeremy C. Reed's avatar
Jeremy C. Reed committed
75

76 77 78 79 80 81
    int s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

    if (s < 0) {
        cerr << "failed to open socket" << endl;
        return (1);
    }
82

83 84 85 86
    if (verbose) {
        gettimeofday(&before_time, NULL);
    }

87 88
    sendto(s, obuffer.getData(), obuffer.getLength(), 0, res->ai_addr,
           res->ai_addrlen);
89

90 91 92 93 94 95
    struct sockaddr_storage ss;
    struct sockaddr* sa;
    socklen_t sa_len;

    sa_len = sizeof(ss);
    sa = static_cast<struct sockaddr*>((void*)&ss);
96

97 98 99 100
    char recvbuf[4096];
    int cc;
    if ((cc = recvfrom(s, recvbuf, sizeof(recvbuf), 0, sa, &sa_len)) > 0) {
        try {
101 102 103
            Message rmsg;
            InputBuffer ibuffer(recvbuf, cc);

104
            rmsg.fromWire(ibuffer);
105
            if (!verbose) {
106 107 108 109
                  for (RRsetIterator it = rmsg.beginSection(Section::ANSWER());
                       it != rmsg.endSection(Section::ANSWER());
                       ++it) {
                      if ((*it)->getType() != RRType::A()) {
110 111
                          continue;
                      }
112 113 114

                      RdataIteratorPtr rit = (*it)->getRdataIterator();
                      for (rit->first(); !rit->isLast(); rit->next()) {
115 116
                          // instead of using my name, maybe use returned label?
                          cout << name << " has address " <<
117
                              (*rit).getCurrent().toText() << endl;
JINMEI Tatuya's avatar
JINMEI Tatuya committed
118
                      }
119 120
                  }
            } else {
121
                gettimeofday(&after_time, NULL);
Jeremy C. Reed's avatar
Jeremy C. Reed committed
122 123 124 125 126 127 128 129 130 131 132 133

                // HEADER and QUESTION, ANSWER, AUTHORITY, and ADDITIONAL
                std::cout << rmsg.toText() << std::endl;

                if (before_time.tv_usec > after_time.tv_usec) {
                    after_time.tv_usec += 1000000;
                    --after_time.tv_sec;
                }

                int elapsed_time =
                    (after_time.tv_sec - before_time.tv_sec)
                    + ((after_time.tv_usec - before_time.tv_usec))/1000;
134

135 136 137 138
// TODO: if NXDOMAIN, host(1) doesn't show HEADER
// Host hsdjkfhksjhdfkj not found: 3(NXDOMAIN)
// TODO: figure out the new libdns way to test if NXDOMAIN

139
                std::cout << "Received " << cc <<
Jeremy C. Reed's avatar
Jeremy C. Reed committed
140 141 142 143
                    " bytes in " << elapsed_time << " ms\n";
                // TODO: " bytes from 127.0.0.1#53 in 0 ms

            } //verbose
144 145 146
        } catch (const exception& ex) {
            std::cerr << "parse failed for " <<
                string(name) << "/" << type << ": " << ex.what() << std::endl;
147
        } catch (...) {
148
            std::cerr << "parse failed for " << string(name) << "/" << type;
149 150 151 152 153
        }
    }

    freeaddrinfo(res);

154
    return (0);
155 156 157 158 159
} // host_lookup()

int
main(int argc, char* argv[])
{
160

Jeremy C. Reed's avatar
Jeremy C. Reed committed
161
    int c;
162

Jeremy C. Reed's avatar
Jeremy C. Reed committed
163 164
    while ((c = getopt(argc, argv, "rt:v")) != -1)
        switch (c) {
165

Jeremy C. Reed's avatar
Jeremy C. Reed committed
166 167 168 169 170 171 172 173 174 175 176 177 178
        case 'r':
            recursive_bit = false;
            break;
        case 't':
            dns_type = optarg;
            break;
        case 'v':
            verbose = 1;
            break;

    }
    argc -= optind;
    argv += optind;
Jeremy C. Reed's avatar
Jeremy C. Reed committed
179

Jeremy C. Reed's avatar
Jeremy C. Reed committed
180 181 182
    if (argc < 1) {
        cout << "Usage: host [-vr] [-t type] hostname [server]\n";
        exit(1);
Jeremy C. Reed's avatar
Jeremy C. Reed committed
183
    }
184

Jeremy C. Reed's avatar
Jeremy C. Reed committed
185 186 187 188 189 190 191 192 193 194 195 196
    if (argc >= 2) {
      server = argv[1];
    }

    if (!dns_type) {
        host_lookup(argv[0], "A");
// TODO: don't do next if A doesn't exist
        host_lookup(argv[0], "AAAA");
        host_lookup(argv[0], "MX");
    } else {
        host_lookup(argv[0], dns_type); 
    }
197
    return (0);
Jeremy C. Reed's avatar
Jeremy C. Reed committed
198 199 200
}