Click
Loading...
Searching...
No Matches
icmpflowid.hh
1// -*- related-file-name: "../../lib/ICMPFlowID.cc" -*-
2#ifndef CLICK_ICMPFlowID_HH
3#define CLICK_ICMPFlowID_HH
4#include <click/ipaddress.hh>
5#include <click/hashcode.hh>
6#include <clicknet/ip.h>
7#include <clicknet/icmp.h>
8CLICK_DECLS
9class Packet;
10
11class ICMPFlowID { public:
12
13 typedef uninitialized_type uninitialized_t;
14
15
19 ICMPFlowID()
20 : _saddr(), _daddr(), _id(0), _seq(0) {
21 }
22
28 ICMPFlowID(IPAddress saddr, IPAddress daddr, uint16_t id, uint16_t seq)
29 : _saddr(saddr), _daddr(daddr), _id(id), _seq(seq) {
30 }
31
32 inline explicit ICMPFlowID(const Packet *p, bool reverse = false);
33 inline explicit ICMPFlowID(const click_ip *iph, bool reverse = false);
34
36 inline ICMPFlowID(const uninitialized_type &unused) {
37 (void) unused;
38 }
39
40
41 typedef IPAddress (ICMPFlowID::*unspecified_bool_type)() const;
43 operator unspecified_bool_type() const {
44 return _saddr || _daddr ? &ICMPFlowID::saddr : 0;
45 }
46
47
49 IPAddress saddr() const {
50 return _saddr;
51 }
52
54 IPAddress daddr() const {
55 return _daddr;
56 }
57
59 uint16_t seq() const {
60 return _seq;
61 }
62
64 uint16_t id() const {
65 return _id;
66 }
67
69 void set_saddr(IPAddress a) {
70 _saddr = a;
71 }
72
74 void set_daddr(IPAddress a) {
75 _daddr = a;
76 }
77
78
79 void assign(IPAddress saddr, IPAddress daddr, uint16_t id, uint16_t seq) {
80 _saddr = saddr;
81 _daddr = daddr;
82 _id = id;
83 _seq = seq;
84 }
85
86
87 ICMPFlowID reverse() const {
88 return ICMPFlowID(_daddr,_saddr, _id, _seq);
89 }
90 inline ICMPFlowID rev() const CLICK_DEPRECATED;
91
96 inline hashcode_t hashcode() const;
97
98
99 protected:
100
101 // note: several functions depend on this field order!
102 IPAddress _saddr;
103 IPAddress _daddr;
104 uint16_t _id; // network byte order
105 uint16_t _seq; // network byte order
106
107 int unparse(char *s) const;
108 friend StringAccum &operator<<(StringAccum &sa, const ICMPFlowID &flow_id);
109
110};
111
112
113inline ICMPFlowID ICMPFlowID::rev() const
114{
115 return reverse();
116}
117
118
119#define ROT(v, r) ((v)<<(r) | ((unsigned)(v))>>(32-(r)))
120
121inline hashcode_t ICMPFlowID::hashcode() const
122{
123 // more complicated hashcode, but causes less collision
124 uint16_t s = ntohs(id());
125 uint16_t d = ntohs(seq());
126 hashcode_t sx = CLICK_NAME(hashcode)(saddr());
127 hashcode_t dx = CLICK_NAME(hashcode)(daddr());
128 return (ROT(sx, (s % 16) + 1) ^ ROT(dx, 31 - (d % 16)))
129 ^ ((d << 16) | s);
130}
131
132#undef ROT
133
134inline bool operator==(const ICMPFlowID &a, const ICMPFlowID &b)
135{
136 return a.seq() == b.seq() && a.id() == b.id()
137 && a.saddr() == b.saddr() && a.daddr() == b.daddr();
138}
139
140inline bool operator!=(const ICMPFlowID &a, const ICMPFlowID &b)
141{
142 return a.seq() != b.seq() || a.id() != b.id()
143 || a.saddr() != b.saddr() || a.daddr() != b.daddr();
144}
145
146
147
148inline
149ICMPFlowID::ICMPFlowID(const Packet *p, bool reverse)
150{
151 const click_ip *iph = p->ip_header();
152 const click_icmp_sequenced *icmph = reinterpret_cast<const click_icmp_sequenced*>(p->transport_header());
153 assert(p->has_network_header() && p->has_transport_header()
154 && IP_FIRSTFRAG(iph));
155
156 if (likely(!reverse))
157 assign(iph->ip_src.s_addr,
158 iph->ip_dst.s_addr, icmph->icmp_identifier, icmph->icmp_sequence);
159 else
160 assign(iph->ip_dst.s_addr,
161 iph->ip_src.s_addr, icmph->icmp_identifier, icmph->icmp_sequence);
162}
163
164inline
165ICMPFlowID::ICMPFlowID(const click_ip *iph, bool reverse)
166{
167 const click_icmp_sequenced *icmph = reinterpret_cast<const click_icmp_sequenced*>(iph + 1);
168 assert(IP_FIRSTFRAG(iph));
169
170 if (likely(!reverse))
171 assign(iph->ip_src.s_addr,
172 iph->ip_dst.s_addr, icmph->icmp_identifier, icmph->icmp_sequence);
173 else
174 assign(iph->ip_dst.s_addr,
175 iph->ip_src.s_addr, icmph->icmp_identifier, icmph->icmp_sequence);
176}
177
178CLICK_ENDDECLS
179#endif
An IPv4 address.
Definition ipaddress.hh:16
A network packet.
Definition packet.hh:68
const click_ip * ip_header() const
Return the packet's network header pointer as IPv4.
Definition packet.hh:1523
bool has_transport_header() const
Return true iff the packet's transport header pointer is set.
Definition packet.hh:1473
const unsigned char * transport_header() const
Return the packet's transport header pointer.
Definition packet.hh:1495
bool has_network_header() const
Return true iff the packet's network header pointer is set.
Definition packet.hh:1435
Efficiently build up Strings from pieces.
Definition straccum.hh:28
The IPAddress type for IPv4 addresses.