2#ifndef CLICK_IPADDRESS_HH
3#define CLICK_IPADDRESS_HH
5#include <click/glue.hh>
7#include <click/hashcode.hh>
8#include <clicknet/ip.h>
14template <
typename T,
size_t ALIGNMENT>
class Vector;
18 typedef uninitialized_type uninitialized_t;
52#if HAVE_INDIFFERENT_ALIGNMENT
53 _addr = *(
reinterpret_cast<const unsigned *
>(
data));
55 memcpy(&_addr,
data, 4);
67 inline IPAddress(
const uninitialized_type &unused) {
97 inline uint32_t
addr()
const {
105 inline operator uint32_t()
const {
113 return (_addr & htonl(0xF0000000U)) == htonl(0xE0000000U);
116 inline bool is_link_local()
const {
117 return (_addr & htonl(0xFFFF0000)) == htonl(0xA9FE0000);
121 inline operator struct in_addr() const;
123 inline unsigned char*
data();
124 inline const unsigned char*
data()
const;
152 inline operator String() const CLICK_DEPRECATED;
168 return a.
addr() == b.addr();
179 return a.
addr() == b;
198 return a.
addr() != b;
205inline const unsigned char*
208 return reinterpret_cast<const unsigned char*
>(&_addr);
218 return reinterpret_cast<unsigned char*
>(&_addr);
249 return ((this->
addr() ^ addr.
addr()) & mask.
addr()) == 0;
374 static const char *basic_parse(
const char *begin,
const char *end,
375 unsigned char value[4],
int &nbytes);
378 static bool parse(
const String &str,
struct in_addr &result,
380 return parse(str,
reinterpret_cast<IPAddress &
>(result), args);
390 : allow_bare_address(allow_bare_address_) {
392 bool parse(
const String &str,
395 bool parse(
const String &str,
396 struct in_addr &result_addr,
struct in_addr &result_mask,
398 return parse(str,
reinterpret_cast<IPAddress &
>(result_addr),
399 reinterpret_cast<IPAddress &
>(result_mask), args);
401 bool allow_bare_address;
417 assert(ip_p > 0 && ip_p < 256);
419 bool parse(
const String &str, uint16_t &result,
440 return (foo == src) || (foo == dst);
444 return (src == foo) ? dst : src;
447 inline hashcode_t hashcode()
const {
448 return CLICK_NAME(hashcode)(src) + CLICK_NAME(hashcode)(dst);
451 inline bool operator==(IPPair other)
const {
452 return (other.src == src && other.dst == dst);
460 IPPort(
IPAddress addr, uint16_t port) : ip(addr), port(port) {
463 inline hashcode_t hashcode()
const {
464 return CLICK_NAME(hashcode)(ip) + CLICK_NAME(hashcode)(port);
467 inline bool operator==(IPPort other)
const {
468 return (other.ip == ip && other.port == port);
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