]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/anadns.hh
make dns analysis tools more bsd-compatible
[thirdparty/pdns.git] / pdns / anadns.hh
1 #ifndef PDNS_ANADNS_HH
2 #define PDNS_ANADNS_HH
3 #include <boost/tuple/tuple.hpp>
4 #include <boost/tuple/tuple_comparison.hpp>
5 #include <string>
6 #include <netinet/ip.h>
7 #include <netinet/udp.h>
8 #include "dnsparser.hh"
9
10 using namespace boost;
11 using namespace std;
12
13 struct QuestionIdentifier
14 {
15 QuestionIdentifier()
16 {}
17
18 bool operator<(const QuestionIdentifier& rhs) const
19 {
20 return
21 tie(d_sourceip, d_destip, d_sourceport, d_destport, d_qname, d_qtype, d_id) <
22 tie(rhs.d_sourceip, rhs.d_destip, rhs.d_sourceport, rhs.d_destport, rhs.d_qname, rhs.d_qtype, rhs.d_id);
23 }
24
25 // the canonical direction is that of the question
26 static QuestionIdentifier create(const struct ip* ip, const struct udphdr* udp, const MOADNSParser& mdp)
27 {
28 QuestionIdentifier ret;
29 if(mdp.d_header.qr) {
30 memcpy(&ret.d_sourceip, &ip->ip_dst, sizeof(ret.d_sourceip));
31 ret.d_sourceip=htonl(ret.d_sourceip);
32
33 memcpy(&ret.d_destip, &ip->ip_src, sizeof(ret.d_destip));
34 ret.d_destip=htonl(ret.d_destip);
35
36 ret.d_sourceport=htons(udp->uh_dport);
37 ret.d_destport=htons(udp->uh_sport);
38 }
39 else {
40 memcpy(&ret.d_sourceip, &ip->ip_src, sizeof(ret.d_sourceip));
41 ret.d_sourceip=htonl(ret.d_sourceip);
42
43 memcpy(&ret.d_destip, &ip->ip_dst, sizeof(ret.d_destip));
44 ret.d_destip=htonl(ret.d_destip);
45
46 ret.d_sourceport=htons(udp->uh_sport);
47 ret.d_destport=htons(udp->uh_dport);
48 }
49 ret.d_qname=mdp.d_qname;
50 ret.d_qtype=mdp.d_qtype;
51 ret.d_id=mdp.d_header.id;
52 return ret;
53 }
54
55
56 uint32_t d_sourceip;
57 uint32_t d_destip;
58 uint16_t d_sourceport;
59 uint16_t d_destport;
60
61 string d_qname;
62 uint16_t d_qtype;
63 uint16_t d_id;
64
65
66 };
67
68 inline ostream& operator<<(ostream &s, const QuestionIdentifier& qi)
69 {
70 s<< "'"<<qi.d_qname<<"|"<<DNSRecordContent::NumberToType(qi.d_qtype)<<"', with id " << qi.d_id <<" from ";
71 u_int32_t rint=qi.d_sourceip;
72
73 s<< (rint>>24 & 0xff)<<".";
74 s<< (rint>>16 & 0xff)<<".";
75 s<< (rint>>8 & 0xff)<<".";
76 s<< (rint & 0xff);
77 s<<":"<<qi.d_sourceport;
78
79 s<<" to ";
80 rint=qi.d_destip;
81 s<< (rint>>24 & 0xff)<<".";
82 s<< (rint>>16 & 0xff)<<".";
83 s<< (rint>>8 & 0xff)<<".";
84 s<< (rint & 0xff);
85 return s<<":"<<qi.d_destport;
86 }
87
88
89 #endif