diff options
Diffstat (limited to 'src/base')
-rw-r--r-- | src/base/inet.cc | 67 | ||||
-rw-r--r-- | src/base/inet.hh | 59 |
2 files changed, 126 insertions, 0 deletions
diff --git a/src/base/inet.cc b/src/base/inet.cc index 1a280e993..93501018e 100644 --- a/src/base/inet.cc +++ b/src/base/inet.cc @@ -117,6 +117,73 @@ operator<<(ostream &stream, const EthAddr &ea) return stream; } +string +IpAddress::string() const +{ + stringstream stream; + stream << *this; + return stream.str(); +} + +bool +operator==(const IpAddress &left, const IpAddress &right) +{ + return left.ip() == right.ip(); +} + +ostream & +operator<<(ostream &stream, const IpAddress &ia) +{ + uint32_t ip = ia.ip(); + ccprintf(stream, "%x.%x.%x.%x", + (uint8_t)(ip >> 0), (uint8_t)(ip >> 8), + (uint8_t)(ip >> 16), (uint8_t)(ip >> 24)); + return stream; +} + +string +IpNetmask::string() const +{ + stringstream stream; + stream << *this; + return stream.str(); +} + +bool +operator==(const IpNetmask &left, const IpNetmask &right) +{ + return (left.ip() == right.ip()) && + (left.netmask() == right.netmask()); +} + +ostream & +operator<<(ostream &stream, const IpNetmask &in) +{ + ccprintf(stream, "%s/%d", (const IpAddress &)in, in.netmask()); + return stream; +} + +string +IpWithPort::string() const +{ + stringstream stream; + stream << *this; + return stream.str(); +} + +bool +operator==(const IpWithPort &left, const IpWithPort &right) +{ + return (left.ip() == right.ip()) && (left.port() == right.port()); +} + +ostream & +operator<<(ostream &stream, const IpWithPort &iwp) +{ + ccprintf(stream, "%s:%d", (const IpAddress &)iwp, iwp.port()); + return stream; +} + uint16_t cksum(const IpPtr &ptr) { diff --git a/src/base/inet.hh b/src/base/inet.hh index ef9a7d81c..12387087b 100644 --- a/src/base/inet.hh +++ b/src/base/inet.hh @@ -147,6 +147,65 @@ class EthPtr /* * IP Stuff */ +struct IpAddress +{ + protected: + uint32_t _ip; + + public: + IpAddress() : _ip(0) + {} + IpAddress(const uint32_t __ip) : _ip(__ip) + {} + + uint32_t ip() const { return _ip; } + + std::string string() const; +}; + +std::ostream &operator<<(std::ostream &stream, const IpAddress &ia); +bool operator==(const IpAddress &left, const IpAddress &right); + +struct IpNetmask : public IpAddress +{ + protected: + uint8_t _netmask; + + public: + IpNetmask() : IpAddress(), _netmask(0) + {} + IpNetmask(const uint32_t __ip, const uint8_t __netmask) : + IpAddress(__ip), _netmask(__netmask) + {} + + uint8_t netmask() const { return _netmask; } + + std::string string() const; +}; + +std::ostream &operator<<(std::ostream &stream, const IpNetmask &in); +bool operator==(const IpNetmask &left, const IpNetmask &right); + +struct IpWithPort : public IpAddress +{ + protected: + uint16_t _port; + + public: + IpWithPort() : IpAddress(), _port(0) + {} + IpWithPort(const uint32_t __ip, const uint16_t __port) : + IpAddress(__ip), _port(__port) + {} + + uint8_t port() const { return _port; } + + std::string string() const; +}; + +std::ostream &operator<<(std::ostream &stream, const IpWithPort &iwp); +bool operator==(const IpWithPort &left, const IpWithPort &right); + struct IpOpt; struct IpHdr : public ip_hdr { |