hello-dns/tdns/tdig.cc

79 lines
1.6 KiB
C++
Raw Normal View History

#include <cstdint>
#include <vector>
#include <map>
#include <stdexcept>
#include "sclasses.hh"
#include <thread>
#include <signal.h>
#include "record-types.hh"
2018-04-17 19:19:44 +07:00
/*!
@file
@brief Tiny 'dig'-like utility to create DNS queries & print responses
*/
using namespace std;
2018-04-17 21:22:44 +07:00
//! This function is plain wrong and does unescape its input
DNSName makeDNSName(const std::string& str)
{
DNSName ret;
string part;
for(const auto& c: str) {
if(c=='.') {
ret.push_back(part);
part.clear();
}
else part.append(1, c);
}
if(!part.empty())
ret.push_back(part);
return ret;
}
int main(int argc, char** argv)
try
{
if(argc != 4) {
cerr<<"Syntax: tdig name type ip[:port]"<<endl;
return(EXIT_FAILURE);
}
signal(SIGPIPE, SIG_IGN);
2018-04-17 21:22:44 +07:00
DNSName dn = makeDNSName(argv[1]);
DNSType dt = makeDNSType(argv[2]);
ComboAddress server(argv[3]);
DNSMessageWriter dmw(dn, dt);
2018-04-17 21:22:44 +07:00
dmw.dh.rd = true;
2018-04-17 21:22:44 +07:00
dmw.randomizeID();
dmw.setEDNS(4000, false);
Socket sock(server.sin4.sin_family, SOCK_DGRAM);
SConnect(sock, server);
SWrite(sock, dmw.serialize());
string resp =SRecvfrom(sock, 65535, server);
DNSMessageReader dmr(resp);
2018-04-17 21:22:44 +07:00
DNSSection rrsection;
uint32_t ttl;
2018-04-17 21:22:44 +07:00
dmr.getQuestion(dn, dt);
cout<<"Received "<<resp.size()<<" byte response with RCode "<<(RCode)dmr.dh.rcode<<", qname " <<dn<<", qtype "<<dt<<endl;
std::unique_ptr<RRGen> rr;
2018-04-17 21:22:44 +07:00
while(dmr.getRR(rrsection, dn, dt, ttl, rr)) {
cout << dn<< " IN " << dt << " " << ttl << " " <<rr->toString()<<endl;
}
}
catch(std::exception& e)
{
cerr<<"Fatal error: "<<e.what()<<endl;
return EXIT_FAILURE;
}