]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/sdig.cc
Merge pull request #4877 from skolot/issue-4579
[thirdparty/pdns.git] / pdns / sdig.cc
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 #include "dnsparser.hh"
5 #include "sstuff.hh"
6 #include "misc.hh"
7 #include "dnswriter.hh"
8 #include "dnsrecords.hh"
9 #include "statbag.hh"
10 #include <boost/array.hpp>
11 #include "ednssubnet.hh"
12 StatBag S;
13
14 bool hidettl=false;
15
16 string ttl(uint32_t ttl)
17 {
18 if(hidettl)
19 return "[ttl]";
20 else
21 return std::to_string(ttl);
22 }
23
24 void usage() {
25 cerr<<"sdig"<<endl;
26 cerr<<"Syntax: sdig IP-ADDRESS PORT QUESTION QUESTION-TYPE [dnssec] [recurse] [showflags] [hidesoadetails] [hidettl] [tcp] [ednssubnet SUBNET]"<<endl;
27 }
28
29 int main(int argc, char** argv)
30 try
31 {
32 bool dnssec=false;
33 bool recurse=false;
34 bool tcp=false;
35 bool showflags=false;
36 bool hidesoadetails=false;
37 boost::optional<Netmask> ednsnm;
38
39
40 for(int i=1; i<argc; i++) {
41 if ((string) argv[i] == "--help") {
42 usage();
43 exit(EXIT_SUCCESS);
44 }
45
46 if ((string) argv[i] == "--version") {
47 cerr<<"sdig "<<VERSION<<endl;
48 exit(EXIT_SUCCESS);
49 }
50 }
51
52 if(argc < 5) {
53 usage();
54 exit(EXIT_FAILURE);
55 }
56
57 reportAllTypes();
58
59 if (argc > 5) {
60 for(int i=5; i<argc; i++) {
61 if (strcmp(argv[i], "dnssec") == 0)
62 dnssec=true;
63 if (strcmp(argv[i], "recurse") == 0)
64 recurse=true;
65 if (strcmp(argv[i], "showflags") == 0)
66 showflags=true;
67 if (strcmp(argv[i], "hidesoadetails") == 0)
68 hidesoadetails=true;
69 if (strcmp(argv[i], "hidettl") == 0)
70 hidettl=true;
71 if (strcmp(argv[i], "tcp") == 0)
72 tcp=true;
73 if (strcmp(argv[i], "ednssubnet") == 0) {
74 ednsnm=Netmask(argv[++i]);
75 }
76 }
77 }
78
79 vector<uint8_t> packet;
80
81 DNSPacketWriter pw(packet, DNSName(argv[3]), DNSRecordContent::TypeToNumber(argv[4]));
82
83 if(dnssec || ednsnm || getenv("SDIGBUFSIZE"))
84 {
85 char *sbuf=getenv("SDIGBUFSIZE");
86 int bufsize;
87 if(sbuf)
88 bufsize=atoi(sbuf);
89 else
90 bufsize=2800;
91 DNSPacketWriter::optvect_t opts;
92 if(ednsnm) {
93 EDNSSubnetOpts eo;
94 eo.source = *ednsnm;
95 opts.push_back(make_pair(8, makeEDNSSubnetOptsString(eo)));
96 }
97
98 pw.addOpt(bufsize, 0, dnssec ? EDNSOpts::DNSSECOK : 0, opts);
99 pw.commit();
100 }
101
102 if(recurse)
103 {
104 pw.getHeader()->rd=true;
105 }
106
107 string reply;
108 ComboAddress dest(argv[1] + (*argv[1]=='@'), atoi(argv[2]));
109
110 if(tcp) {
111 Socket sock(dest.sin4.sin_family, SOCK_STREAM);
112 sock.connect(dest);
113 uint16_t len;
114 len = htons(packet.size());
115 if(sock.write((char *) &len, 2) != 2)
116 throw PDNSException("tcp write failed");
117
118 sock.writen(string((char*)&*packet.begin(), (char*)&*packet.end()));
119
120 if(sock.read((char *) &len, 2) != 2)
121 throw PDNSException("tcp read failed");
122
123 len=ntohs(len);
124 char *creply = new char[len];
125 int n=0;
126 int numread;
127 while(n<len) {
128 numread=sock.read(creply+n, len-n);
129 if(numread<0)
130 throw PDNSException("tcp read failed");
131 n+=numread;
132 }
133
134 reply=string(creply, len);
135 delete[] creply;
136 }
137 else //udp
138 {
139 Socket sock(dest.sin4.sin_family, SOCK_DGRAM);
140 sock.sendTo(string((char*)&*packet.begin(), (char*)&*packet.end()), dest);
141 int result=waitForData(sock.getHandle(), 10);
142 if(result < 0)
143 throw std::runtime_error("Error waiting for data: "+string(strerror(errno)));
144 if(!result)
145 throw std::runtime_error("Timeout waiting for data");
146 sock.recvFrom(reply, dest);
147 }
148 MOADNSParser mdp(false, reply);
149 cout<<"Reply to question for qname='"<<mdp.d_qname.toString()<<"', qtype="<<DNSRecordContent::NumberToType(mdp.d_qtype)<<endl;
150 cout<<"Rcode: "<<mdp.d_header.rcode<<" ("<<RCode::to_s(mdp.d_header.rcode)<<"), RD: "<<mdp.d_header.rd<<", QR: "<<mdp.d_header.qr;
151 cout<<", TC: "<<mdp.d_header.tc<<", AA: "<<mdp.d_header.aa<<", opcode: "<<mdp.d_header.opcode<<endl;
152
153 for(MOADNSParser::answers_t::const_iterator i=mdp.d_answers.begin(); i!=mdp.d_answers.end(); ++i) {
154 cout<<i->first.d_place-1<<"\t"<<i->first.d_name.toString()<<"\tIN\t"<<DNSRecordContent::NumberToType(i->first.d_type);
155 if(i->first.d_type == QType::RRSIG)
156 {
157 string zoneRep = i->first.d_content->getZoneRepresentation();
158 vector<string> parts;
159 stringtok(parts, zoneRep);
160 cout<<"\t"<<ttl(i->first.d_ttl)<<"\t"<< parts[0]<<" "<<parts[1]<<" "<<parts[2]<<" "<<parts[3]<<" [expiry] [inception] [keytag] "<<parts[7]<<" ...\n";
161 }
162 else if(!showflags && i->first.d_type == QType::NSEC3)
163 {
164 string zoneRep = i->first.d_content->getZoneRepresentation();
165 vector<string> parts;
166 stringtok(parts, zoneRep);
167 cout<<"\t"<<ttl(i->first.d_ttl)<<"\t"<< parts[0]<<" [flags] "<<parts[2]<<" "<<parts[3]<<" "<<parts[4];
168 for(vector<string>::iterator iter = parts.begin()+5; iter != parts.end(); ++iter)
169 cout<<" "<<*iter;
170 cout<<"\n";
171 }
172 else if(i->first.d_type == QType::DNSKEY)
173 {
174 string zoneRep = i->first.d_content->getZoneRepresentation();
175 vector<string> parts;
176 stringtok(parts, zoneRep);
177 cout<<"\t"<<ttl(i->first.d_ttl)<<"\t"<< parts[0]<<" "<<parts[1]<<" "<<parts[2]<<" ...\n";
178 }
179 else if (i->first.d_type == QType::SOA && hidesoadetails)
180 {
181 string zoneRep = i->first.d_content->getZoneRepresentation();
182 vector<string> parts;
183 stringtok(parts, zoneRep);
184 cout<<"\t"<<ttl(i->first.d_ttl)<<"\t"<<parts[0]<<" "<<parts[1]<<" [serial] "<<parts[3]<<" "<<parts[4]<<" "<<parts[5]<<" "<<parts[6]<<"\n";
185 }
186 else
187 {
188 cout<<"\t"<<ttl(i->first.d_ttl)<<"\t"<< i->first.d_content->getZoneRepresentation()<<"\n";
189 }
190
191 }
192
193 EDNSOpts edo;
194 if(getEDNSOpts(mdp, &edo)) {
195 // cerr<<"Have "<<edo.d_options.size()<<" options!"<<endl;
196 for(vector<pair<uint16_t, string> >::const_iterator iter = edo.d_options.begin();
197 iter != edo.d_options.end();
198 ++iter) {
199 if(iter->first == 5) {// 'EDNS PING'
200 cerr<<"Have ednsping: '"<<iter->second<<"'\n";
201 //if(iter->second == ping)
202 // cerr<<"It is correct!"<<endl;
203 }
204 if(iter->first == 8) {// 'EDNS subnet'
205 EDNSSubnetOpts reso;
206 if(getEDNSSubnetOptsFromString(iter->second, &reso)) {
207 cerr<<"EDNS Subnet response: "<<reso.source.toString()<<", scope: "<<reso.scope.toString()<<", family = "<<reso.scope.getNetwork().sin4.sin_family<<endl;
208 }
209 }
210
211 else {
212 cerr<<"Have unknown option "<<(int)iter->first<<endl;
213 }
214 }
215
216 }
217 }
218 catch(std::exception &e)
219 {
220 cerr<<"Fatal: "<<e.what()<<endl;
221 }
222 catch(PDNSException &e)
223 {
224 cerr<<"Fatal: "<<e.reason<<endl;
225 }