Click
Loading...
Searching...
No Matches
ipaddress.hh
Go to the documentation of this file.
1// -*- c-basic-offset: 4; related-file-name: "../../lib/ipaddress.cc" -*-
2#ifndef CLICK_IPADDRESS_HH
3#define CLICK_IPADDRESS_HH
4#include <click/string.hh>
5#include <click/glue.hh>
7#include <click/hashcode.hh>
8#include <clicknet/ip.h>
9CLICK_DECLS
10class StringAccum;
11class ArgContext;
12extern const ArgContext blank_args;
13class IPAddressArg;
14template <typename T, size_t ALIGNMENT> class Vector;
15
16class IPAddress { public:
17
18 typedef uninitialized_type uninitialized_t;
19
21 inline IPAddress()
22 : _addr(0) {
23 }
24
26 inline IPAddress(unsigned x)
27 : _addr(x) {
28 }
30 explicit inline IPAddress(int x)
31 : _addr(x) {
32 }
34 explicit inline IPAddress(unsigned long x)
35 : _addr(x) {
36 }
38 explicit inline IPAddress(long x)
39 : _addr(x) {
40 }
41
43 inline IPAddress(struct in_addr x)
44 : _addr(x.s_addr) {
45 }
46
51 explicit IPAddress(const unsigned char *data) {
52#if HAVE_INDIFFERENT_ALIGNMENT
53 _addr = *(reinterpret_cast<const unsigned *>(data));
54#else
55 memcpy(&_addr, data, 4);
56#endif
57 }
58
64 explicit IPAddress(const String &x);
65
67 inline IPAddress(const uninitialized_type &unused) {
68 (void) unused;
69 }
70
79 static IPAddress make_prefix(int prefix_len);
80
82 static inline IPAddress make_broadcast() {
83 return IPAddress(0xFFFFFFFF);
84 }
85
87 static inline IPAddress make_random() {
88 return IPAddress(rand());
89 }
90
92 inline bool empty() const {
93 return !_addr;
94 }
95
97 inline uint32_t addr() const {
98 return _addr;
99 }
100
105 inline operator uint32_t() const {
106 return _addr;
107 }
108
112 inline bool is_multicast() const {
113 return (_addr & htonl(0xF0000000U)) == htonl(0xE0000000U);
114 }
115
116 inline bool is_link_local() const {
117 return (_addr & htonl(0xFFFF0000)) == htonl(0xA9FE0000);
118 }
119
120 inline struct in_addr in_addr() const;
121 inline operator struct in_addr() const;
122
123 inline unsigned char* data();
124 inline const unsigned char* data() const;
125
126 inline uint32_t hashcode() const;
127
128 int mask_to_prefix_len() const;
129 inline bool matches_prefix(IPAddress addr, IPAddress mask) const;
130 inline bool mask_as_specific(IPAddress mask) const;
131 inline bool mask_more_specific(IPAddress mask) const;
132
133 // bool operator==(IPAddress, IPAddress);
134 // bool operator==(IPAddress, uint32_t);
135 // bool operator!=(IPAddress, IPAddress);
136 // bool operator!=(IPAddress, uint32_t);
137
138 // IPAddress operator&(IPAddress, IPAddress);
139 // IPAddress operator|(IPAddress, IPAddress);
140 // IPAddress operator^(IPAddress, IPAddress);
141 // IPAddress operator~(IPAddress);
142
146
147 String unparse() const;
148 String unparse_mask() const;
150
151 inline String s() const;
152 inline operator String() const CLICK_DEPRECATED;
153
154 typedef IPAddress parameter_type;
155
156 private:
157
158 uint32_t _addr;
159
160};
161
162
165inline bool
166operator==(IPAddress a, IPAddress b)
167{
168 return a.addr() == b.addr();
169}
170
176inline bool
177operator==(IPAddress a, uint32_t b)
178{
179 return a.addr() == b;
180}
181
184inline bool
186{
187 return a.addr() != b.addr();
188}
189
195inline bool
196operator!=(IPAddress a, uint32_t b)
197{
198 return a.addr() != b;
199}
200
205inline const unsigned char*
207{
208 return reinterpret_cast<const unsigned char*>(&_addr);
209}
210
215inline unsigned char*
217{
218 return reinterpret_cast<unsigned char*>(&_addr);
219}
220
222inline struct in_addr
224{
225 struct in_addr ia;
226 ia.s_addr = _addr;
227 return ia;
228}
229
231inline
232IPAddress::operator struct in_addr() const
233{
234 return in_addr();
235}
236
237StringAccum& operator<<(StringAccum&, IPAddress);
238
246inline bool
248{
249 return ((this->addr() ^ addr.addr()) & mask.addr()) == 0;
250}
251
261inline bool
263{
264 return (addr() & mask.addr()) == mask.addr();
265}
266
275inline bool
277{
278 return ((addr() << 1) & mask.addr()) == mask.addr();
279}
280
284inline IPAddress
286{
287 return IPAddress(a.addr() & b.addr());
288}
289
291inline IPAddress&
293{
294 _addr &= a._addr;
295 return *this;
296}
297
301inline IPAddress
303{
304 return IPAddress(a.addr() | b.addr());
305}
306
308inline IPAddress&
310{
311 _addr |= a._addr;
312 return *this;
313}
314
318inline IPAddress
320{
321 return IPAddress(a.addr() ^ b.addr());
322}
323
325inline IPAddress&
327{
328 _addr ^= a._addr;
329 return *this;
330}
331
335inline IPAddress
337{
338 return IPAddress(~a.addr());
339}
340
346inline uint32_t
348{
349 return addr();
350}
351
355inline
356IPAddress::operator String() const
357{
358 return unparse();
359}
360
364inline String
366{
367 return unparse();
368}
369
370
373class IPAddressArg { public:
374 static const char *basic_parse(const char *begin, const char *end,
375 unsigned char value[4], int &nbytes);
376 static bool parse(const String &str, IPAddress &result,
377 const ArgContext &args = blank_args);
378 static bool parse(const String &str, struct in_addr &result,
379 const ArgContext &args = blank_args) {
380 return parse(str, reinterpret_cast<IPAddress &>(result), args);
381 }
382 static bool parse(const String &str, Vector<IPAddress> &result,
383 const ArgContext &args = blank_args);
384};
385
388class IPPrefixArg { public:
389 IPPrefixArg(bool allow_bare_address_ = false)
390 : allow_bare_address(allow_bare_address_) {
391 }
392 bool parse(const String &str,
393 IPAddress &result_addr, IPAddress &result_mask,
394 const ArgContext &args = blank_args) const;
395 bool parse(const String &str,
396 struct in_addr &result_addr, struct in_addr &result_mask,
397 const ArgContext &args = blank_args) const {
398 return parse(str, reinterpret_cast<IPAddress &>(result_addr),
399 reinterpret_cast<IPAddress &>(result_mask), args);
400 }
401 bool allow_bare_address;
402};
403
404template<> struct DefaultArg<IPAddress> : public IPAddressArg {};
405template<> struct DefaultArg<struct in_addr> : public IPAddressArg {};
406template<> struct DefaultArg<Vector<IPAddress> > : public IPAddressArg {};
407/* template<> struct has_trivial_copy<IPAddress> : public true_type {}; -- in type_traits.hh */
408
409
414class IPPortArg { public:
415 IPPortArg(int p)
416 : ip_p(p) {
417 assert(ip_p > 0 && ip_p < 256);
418 }
419 bool parse(const String &str, uint16_t &result,
420 const ArgContext &args = blank_args) const;
421 int ip_p;
422};
423
424
425class IPPair {
426 public:
427
428 IPAddress src;
429 IPAddress dst;
430 IPPair() {
431 src = 0;
432 dst = 0;
433 }
434 IPPair(IPAddress a, IPAddress b) {
435 src = a;
436 dst = b;
437 }
438
439 bool contains(IPAddress foo) const {
440 return (foo == src) || (foo == dst);
441 }
442
443 bool other(IPAddress foo) const {
444 return (src == foo) ? dst : src;
445 }
446
447 inline hashcode_t hashcode() const {
448 return CLICK_NAME(hashcode)(src) + CLICK_NAME(hashcode)(dst);
449 }
450
451 inline bool operator==(IPPair other) const {
452 return (other.src == src && other.dst == dst);
453 }
454};
455
456class IPPort {
457public:
458 IPAddress ip;
459 uint16_t port;
460 IPPort(IPAddress addr, uint16_t port) : ip(addr), port(port) {
461
462 }
463 inline hashcode_t hashcode() const {
464 return CLICK_NAME(hashcode)(ip) + CLICK_NAME(hashcode)(port);
465 }
466
467 inline bool operator==(IPPort other) const {
468 return (other.ip == ip && other.port == port);
469 }
470};
471
472
473CLICK_ENDDECLS
474#endif
Argument context class.
Definition args.hh:31
Parser class for IPv4 addresses.
Definition ipaddress.hh:373
An IPv4 address.
Definition ipaddress.hh:16
String unparse_with_mask(IPAddress) const
Unparses an address prefix, specified by address and mask, into "address/mask" format.
Definition ipaddress.cc:114
IPAddress operator~(IPAddress a)
Calculate the IPAddress representing the bitwise complement of a.
Definition ipaddress.hh:336
bool operator==(IPAddress a, uint32_t b)
Compare an IPAddress with a network-byte-order address value for equality.
Definition ipaddress.hh:177
IPAddress & operator&=(IPAddress)
Assign this address to its bitwise-and with a.
Definition ipaddress.hh:292
bool matches_prefix(IPAddress addr, IPAddress mask) const
Return true iff this address matches the address prefix addr/mask.
Definition ipaddress.hh:247
int mask_to_prefix_len() const
Returns the prefix length equivalent to this prefix mask, or -1 if this is not a prefix mask.
Definition ipaddress.cc:67
struct in_addr in_addr() const
Return a struct in_addr corresponding to the address.
Definition ipaddress.hh:223
bool operator!=(IPAddress a, IPAddress b)
Compare two IPAddress objects for inequality.
Definition ipaddress.hh:185
IPAddress & operator|=(IPAddress)
Assign this address to its bitwise-or with a.
Definition ipaddress.hh:309
IPAddress(struct in_addr x)
Construct an IPAddress from a struct in_addr.
Definition ipaddress.hh:43
IPAddress(long x)
Definition ipaddress.hh:38
IPAddress(const unsigned char *data)
Construct an IPAddress from data.
Definition ipaddress.hh:51
bool operator!=(IPAddress a, uint32_t b)
Compare an IPAddress with a network-byte-order address value for inequality.
Definition ipaddress.hh:196
uint32_t hashcode() const
Hash function.
Definition ipaddress.hh:347
static IPAddress make_prefix(int prefix_len)
Return an IPAddress equal to the prefix mask of length prefix.
Definition ipaddress.cc:51
bool is_multicast() const
Return true iff the address is a multicast address.
Definition ipaddress.hh:112
IPAddress()
Construct an IPAddress equal to 0.0.0.0.
Definition ipaddress.hh:21
String unparse() const
Unparses this address into a dotted-quad format String.
Definition ipaddress.cc:84
IPAddress(int x)
Definition ipaddress.hh:30
IPAddress operator|(IPAddress a, IPAddress b)
Calculate the IPAddress representing the bitwise-or of a and b.
Definition ipaddress.hh:302
IPAddress(const uninitialized_type &unused)
Construct an uninitialized IPAddress.
Definition ipaddress.hh:67
IPAddress & operator^=(IPAddress)
Assign this address to its bitwise-xor with a.
Definition ipaddress.hh:326
IPAddress(unsigned x)
Construct an IPAddress from an integer in network byte order.
Definition ipaddress.hh:26
String unparse_mask() const
Unparses this address into IP address mask format: either a prefix length or unparse().
Definition ipaddress.cc:99
String s() const
Unparse this address into a dotted-quad format String.
Definition ipaddress.hh:365
bool mask_as_specific(IPAddress mask) const
Return true iff this address, considered as a prefix mask, is at least as specific as mask.
Definition ipaddress.hh:262
bool empty() const
Test if the address is 0.0.0.0.
Definition ipaddress.hh:92
unsigned char * data()
Return a pointer to the address data.
Definition ipaddress.hh:216
IPAddress operator&(IPAddress a, IPAddress b)
Calculate the IPAddress representing the bitwise-and of a and b.
Definition ipaddress.hh:285
IPAddress operator^(IPAddress a, IPAddress b)
Calculate the IPAddress representing the bitwise-xor of a and b.
Definition ipaddress.hh:319
uint32_t addr() const
Return the address as a uint32_t in network byte order.
Definition ipaddress.hh:97
IPAddress(unsigned long x)
Definition ipaddress.hh:34
static IPAddress make_random()
Return the broadcast IP address, 255.255.255.255.
Definition ipaddress.hh:87
bool mask_more_specific(IPAddress mask) const
Return true iff this prefix mask is more specific than mask.
Definition ipaddress.hh:276
static IPAddress make_broadcast()
Return the broadcast IP address, 255.255.255.255.
Definition ipaddress.hh:82
Parser class for TCP/UDP ports.
Definition ipaddress.hh:414
Parser class for IPv4 prefixes.
Definition ipaddress.hh:388
Efficiently build up Strings from pieces.
Definition straccum.hh:28
A string of characters.
Definition string.hh:22
Vector template.
Definition vector.hh:111
Click's String class.
Type traits structures.