From: bert hubert Date: Thu, 11 Jul 2013 20:05:50 +0000 (+0200) Subject: teach dnspcap to provide ComboAddresses for src/dst of IPv4 and IPv6 packets, move... X-Git-Tag: rec-3.6.0-rc1~574 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a5d9353ea58f2a9bb3a495c22337ae2283727e93;p=pdns teach dnspcap to provide ComboAddresses for src/dst of IPv4 and IPv6 packets, move dnsscope and dnsreplay to this new API. I don't think dnsreplay is really IPv6 ready though. Might even crash on it right now. --- diff --git a/pdns/anadns.hh b/pdns/anadns.hh index 84c1e00b4..a64897345 100644 --- a/pdns/anadns.hh +++ b/pdns/anadns.hh @@ -23,34 +23,17 @@ struct QuestionIdentifier } // the canonical direction is that of the question - static QuestionIdentifier create(const struct ip* ip, const struct udphdr* udp, const MOADNSParser& mdp) + static QuestionIdentifier create(const ComboAddress& src, const ComboAddress& dst, const MOADNSParser& mdp) { QuestionIdentifier ret; - struct ip6_hdr* ip6 = (struct ip6_hdr*)ip; + if(mdp.d_header.qr) { - if(ip->ip_v!=6) { - ret.d_source.sin4.sin_addr = ip->ip_dst; - ret.d_dest.sin4.sin_addr = ip->ip_src; - } - else { - ret.d_source.sin6.sin6_addr = ip6->ip6_dst; - ret.d_dest.sin6.sin6_addr = ip6->ip6_src; - } - ret.d_dest.sin4.sin_port = udp->uh_sport; - ret.d_source.sin4.sin_port = udp->uh_dport; + ret.d_source = dst; + ret.d_dest = src; } else { - if(ip->ip_v != 6) { - ret.d_source.sin4.sin_addr = ip->ip_src; - ret.d_dest.sin4.sin_addr = ip->ip_dst; - } - else { - ret.d_source.sin6.sin6_addr = ip6->ip6_src; - ret.d_dest.sin6.sin6_addr = ip6->ip6_dst; - } - ret.d_source.sin4.sin_port = udp->uh_sport; - ret.d_dest.sin4.sin_port = udp->uh_dport; - + ret.d_source = src; + ret.d_dest = dst; } ret.d_qname=mdp.d_qname; ret.d_qtype=mdp.d_qtype; diff --git a/pdns/dnspcap.cc b/pdns/dnspcap.cc index dde33327a..c5effcc53 100644 --- a/pdns/dnspcap.cc +++ b/pdns/dnspcap.cc @@ -102,6 +102,36 @@ catch(EofException) { return false; } +ComboAddress PcapPacketReader::getSource() const +{ + ComboAddress ret; + if(d_ip->ip_v == 4) { + ret.sin4.sin_family = AF_INET; + ret.sin4.sin_addr = d_ip->ip_src; + ret.sin4.sin_port = d_udp->uh_sport; // should deal with TCP too! + } else { + ret.sin6.sin6_family = AF_INET6; + ret.sin6.sin6_addr = d_ip6->ip6_src; + ret.sin6.sin6_port = d_udp->uh_sport; // should deal with TCP too! + } + return ret; +} + +ComboAddress PcapPacketReader::getDest() const +{ + ComboAddress ret; + if(d_ip->ip_v == 4) { + ret.sin4.sin_family = AF_INET; + ret.sin4.sin_addr = d_ip->ip_dst; + ret.sin4.sin_port = d_udp->uh_dport; // should deal with TCP too! + } else { + ret.sin6.sin6_family = AF_INET6; + ret.sin6.sin6_addr = d_ip6->ip6_dst; + ret.sin6.sin6_port = d_udp->uh_dport; // should deal with TCP too! + } + return ret; +} + PcapPacketWriter::PcapPacketWriter(const string& fname, PcapPacketReader& ppr) : d_fname(fname), d_ppr(ppr) { diff --git a/pdns/dnspcap.hh b/pdns/dnspcap.hh index b18132761..1abc40c5a 100644 --- a/pdns/dnspcap.hh +++ b/pdns/dnspcap.hh @@ -3,6 +3,7 @@ #include #include +#include "iputils.hh" #include #include "misc.hh" #include @@ -82,6 +83,10 @@ public: bool getUDPPacket(); + + ComboAddress getSource() const; + ComboAddress getDest() const; + struct pdns_lcc_header* d_lcc; struct ether_header* d_ether; struct ip *d_ip; diff --git a/pdns/dnsreplay.cc b/pdns/dnsreplay.cc index a77a7a0ff..805ca1ae9 100644 --- a/pdns/dnsreplay.cc +++ b/pdns/dnsreplay.cc @@ -495,7 +495,7 @@ bool sendPacketFromPR(PcapPacketReader& pr, const ComboAddress& remote) dh->id=tmp; } MOADNSParser mdp((const char*)pr.d_payload, pr.d_len); - QuestionIdentifier qi=QuestionIdentifier::create(pr.d_ip, pr.d_udp, mdp); + QuestionIdentifier qi=QuestionIdentifier::create(pr.getSource(), pr.getDest(), mdp); if(!mdp.d_header.qr) { s_questions++; diff --git a/pdns/dnsscope.cc b/pdns/dnsscope.cc index a687462de..6e6176915 100644 --- a/pdns/dnsscope.cc +++ b/pdns/dnsscope.cc @@ -49,19 +49,24 @@ try typedef map cumul_t; cumul_t cumul; unsigned int untracked=0, errorresult=0, reallylate=0, nonRDQueries=0, queries=0; + unsigned int ipv4Packets=0, ipv6Packets=0; typedef map rcodes_t; rcodes_t rcodes; time_t lowestTime=2000000000, highestTime=0; - while(pr.getUDPPacket()) { if((ntohs(pr.d_udp->uh_dport)==5300 || ntohs(pr.d_udp->uh_sport)==5300 || ntohs(pr.d_udp->uh_dport)==53 || ntohs(pr.d_udp->uh_sport)==53) && pr.d_len > 12) { try { MOADNSParser mdp((const char*)pr.d_payload, pr.d_len); + if(pr.d_ip->ip_v == 4) + ++ipv4Packets; + else + ++ipv6Packets; + if(!mdp.d_header.qr) { if(!mdp.d_header.rd) nonRDQueries++; @@ -73,7 +78,7 @@ try string name=mdp.d_qname+"|"+DNSRecordContent::NumberToType(mdp.d_qtype); - QuestionIdentifier qi=QuestionIdentifier::create(pr.d_ip, pr.d_udp, mdp); + QuestionIdentifier qi=QuestionIdentifier::create(pr.getSource(), pr.getDest(), mdp); if(!mdp.d_header.qr) { // cout<<"Question for '"<< name <<"'\n"; @@ -127,11 +132,11 @@ try } } } - cerr<<"Timespan: "<<(highestTime-lowestTime)/3600.0<<" hours"<second.d_answercount) { @@ -140,11 +145,11 @@ try } } - cerr<< boost::format("%d (%.02f%% of all) queries did not request recursion") % nonRDQueries % ((nonRDQueries*100.0)/queries) << endl; - cerr<first<<"\t"<second<first<<"\t"<second<