]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/sdig.cc
remove unused files
[thirdparty/pdns.git] / pdns / sdig.cc
CommitLineData
ff6a1e7b
BH
1#include "dnsparser.hh"
2#include "sstuff.hh"
3#include "misc.hh"
a0a276c2
BH
4#include "dnswriter.hh"
5#include "dnsrecords.hh"
b0d4fb45 6#include "statbag.hh"
c5c4fbdc 7#include <boost/array.hpp>
b0d4fb45 8StatBag S;
ff6a1e7b
BH
9
10int main(int argc, char** argv)
11try
12{
ee6c3a6b 13 bool dnssec=false;
b97043ee 14 bool recurse=false;
c5c4fbdc 15 bool tcp=false;
b8adb30d 16 bool showflags=false;
b19ad29b 17 bool hidesoadetails=false;
ee6c3a6b 18
b0d4fb45 19 reportAllTypes();
a0a276c2 20
ee6c3a6b 21 if(argc < 5) {
b19ad29b 22 cerr<<"Syntax: sdig IP-address port question question-type [dnssec|dnssec-tcp|recurse|hidesoadetails] [showflags]\n";
a8ad4624
BH
23 exit(EXIT_FAILURE);
24 }
a0a276c2 25
b19ad29b
RA
26 if (argc > 5) {
27 for(int i=5; i<argc; i++) {
28 if (strcmp(argv[i], "dnssec") == 0)
29 dnssec=true;
30 if (strcmp(argv[i], "recurse") == 0)
31 recurse=true;
32 if (strcmp(argv[i], "hidesoadetails") == 0)
33 hidesoadetails=true;
34 if (strcmp(argv[i], "dnssec-tcp") == 0) {
35 dnssec=true;
36 tcp=true;
37 }
38 }
b97043ee
PD
39 }
40
b8adb30d
KM
41 if((argc > 5 && strcmp(argv[5], "showflags")==0) || (argc > 6 && strcmp(argv[6], "showflags")==0))
42 {
43 showflags=true;
44 }
45
a0a276c2 46 vector<uint8_t> packet;
bca6643b 47
a0a276c2
BH
48 DNSPacketWriter pw(packet, argv[3], DNSRecordContent::TypeToNumber(argv[4]));
49
794c2f92 50 if(dnssec || getenv("SDIGBUFSIZE"))
ee6c3a6b 51 {
794c2f92
PD
52 char *sbuf=getenv("SDIGBUFSIZE");
53 int bufsize;
54 if(sbuf)
55 bufsize=atoi(sbuf);
56 else
57 bufsize=2800;
58
a40a830c 59 pw.addOpt(bufsize, 0, dnssec ? EDNSOpts::DNSSECOK : 0);
ee6c3a6b
PD
60 pw.commit();
61 }
b97043ee
PD
62
63 if(recurse)
64 {
65 pw.getHeader()->rd=true;
66 }
bca6643b 67 // pw.setRD(true);
bca6643b
BH
68
69 /*
70 pw.startRecord("powerdns.com", DNSRecordContent::TypeToNumber("NS"));
71 NSRecordContent nrc("ns1.powerdns.com");
a0a276c2 72 nrc.toPacket(pw);
878435ce 73
bca6643b
BH
74 pw.startRecord("powerdns.com", DNSRecordContent::TypeToNumber("NS"));
75 NSRecordContent nrc2("ns2.powerdns.com");
76 nrc2.toPacket(pw);
bca6643b 77 */
edb1c9ee 78
edb1c9ee 79/* DNSPacketWriter::optvect_t opts;
fdf05fd4
BH
80
81 opts.push_back(make_pair(5, ping));
a5d9f5f9 82
0c70797e 83 pw.commit();
edb1c9ee 84*/
a5d9f5f9
BH
85 // pw.addOpt(5200, 0, 0);
86 // pw.commit();
87
ff6a1e7b 88 string reply;
ff6a1e7b 89
c5c4fbdc
PD
90 if(tcp) {
91 Socket sock(InterNetwork, Stream);
92 ComboAddress dest(argv[1] + (*argv[1]=='@'), atoi(argv[2]));
93 sock.connect(dest);
94 uint16_t len;
95 len = htons(packet.size());
96 if(sock.write((char *) &len, 2) != 2)
3f81d239 97 throw PDNSException("tcp write failed");
c5c4fbdc
PD
98
99 sock.writen(string((char*)&*packet.begin(), (char*)&*packet.end()));
100
101 if(sock.read((char *) &len, 2) != 2)
3f81d239 102 throw PDNSException("tcp read failed");
c5c4fbdc
PD
103
104 len=ntohs(len);
105 char *creply = new char[len];
106 int n=0;
107 int numread;
108 while(n<len) {
109 numread=sock.read(creply+n, len-n);
110 if(numread<0)
3f81d239 111 throw PDNSException("tcp read failed");
c5c4fbdc
PD
112 n+=numread;
113 }
114
115 reply=string(creply, len);
116 delete[] creply;
117 }
118 else //udp
119 {
120 Socket sock(InterNetwork, Datagram);
121 ComboAddress dest(argv[1] + (*argv[1]=='@'), atoi(argv[2]));
122 sock.sendTo(string((char*)&*packet.begin(), (char*)&*packet.end()), dest);
123
124 sock.recvFrom(reply, dest);
125 }
ff6a1e7b 126 MOADNSParser mdp(reply);
2a09f6a1 127 cout<<"Reply to question for qname='"<<mdp.d_qname<<"', qtype="<<DNSRecordContent::NumberToType(mdp.d_qtype)<<endl;
0c70797e 128 cout<<"Rcode: "<<mdp.d_header.rcode<<", RD: "<<mdp.d_header.rd<<", QR: "<<mdp.d_header.qr;
7fc69fd0 129 cout<<", TC: "<<mdp.d_header.tc<<", AA: "<<mdp.d_header.aa<<", opcode: "<<mdp.d_header.opcode<<endl;
945a9ad4 130
ff6a1e7b 131 for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i!=mdp.d_answers.end(); ++i) {
2a09f6a1 132 cout<<i->first.d_place-1<<"\t"<<i->first.d_label<<"\tIN\t"<<DNSRecordContent::NumberToType(i->first.d_type);
794c2f92
PD
133 if(i->first.d_type == QType::RRSIG)
134 {
135 string zoneRep = i->first.d_content->getZoneRepresentation();
136 vector<string> parts;
137 stringtok(parts, zoneRep);
138 cout<<"\t"<<i->first.d_ttl<<"\t"<< parts[0]<<" "<<parts[1]<<" "<<parts[2]<<" "<<parts[3]<<" [expiry] [inception] [keytag] "<<parts[7]<<" ...\n";
139 }
b8adb30d
KM
140 else if(!showflags && i->first.d_type == QType::NSEC3)
141 {
142 string zoneRep = i->first.d_content->getZoneRepresentation();
143 vector<string> parts;
144 stringtok(parts, zoneRep);
145 cout<<"\t"<<i->first.d_ttl<<"\t"<< parts[0]<<" [flags] "<<parts[2]<<" "<<parts[3]<<" "<<parts[4];
146 for(vector<string>::iterator iter = parts.begin()+5; iter != parts.end(); ++iter)
147 cout<<" "<<*iter;
148 cout<<"\n";
149 }
794c2f92
PD
150 else if(i->first.d_type == QType::DNSKEY)
151 {
152 string zoneRep = i->first.d_content->getZoneRepresentation();
153 vector<string> parts;
154 stringtok(parts, zoneRep);
155 cout<<"\t"<<i->first.d_ttl<<"\t"<< parts[0]<<" "<<parts[1]<<" "<<parts[2]<<" ...\n";
156 }
b19ad29b
RA
157 else if (i->first.d_type == QType::SOA && hidesoadetails)
158 {
159 string zoneRep = i->first.d_content->getZoneRepresentation();
160 vector<string> parts;
161 stringtok(parts, zoneRep);
162 cout<<"\t"<<i->first.d_ttl<<"\t"<<parts[0]<<" "<<parts[1]<<" [serial] "<<parts[3]<<" "<<parts[4]<<" "<<parts[5]<<" "<<parts[6]<<"\n";
163 }
794c2f92
PD
164 else
165 {
166 cout<<"\t"<<i->first.d_ttl<<"\t"<< i->first.d_content->getZoneRepresentation()<<"\n";
167 }
168
ff6a1e7b 169 }
fdf05fd4 170
0c70797e
BH
171 EDNSOpts edo;
172 if(getEDNSOpts(mdp, &edo)) {
a5d9f5f9 173// cerr<<"Have "<<edo.d_options.size()<<" options!"<<endl;
0c70797e 174 for(vector<pair<uint16_t, string> >::const_iterator iter = edo.d_options.begin();
4957a608
BH
175 iter != edo.d_options.end();
176 ++iter) {
fdf05fd4 177 if(iter->first == 5) {// 'EDNS PING'
4957a608 178 cerr<<"Have ednsping: '"<<iter->second<<"'\n";
a5d9f5f9
BH
179 //if(iter->second == ping)
180 // cerr<<"It is correct!"<<endl;
0c70797e 181 }
fdf05fd4 182 else {
4957a608 183 cerr<<"Have unknown option "<<(int)iter->first<<endl;
fdf05fd4 184 }
0c70797e
BH
185 }
186
187 }
fdf05fd4 188
ff6a1e7b 189}
0c70797e 190catch(std::exception &e)
ff6a1e7b
BH
191{
192 cerr<<"Fatal: "<<e.what()<<endl;
193}