41#ifndef _DWMIPV4ADDRESS_HH_
42#define _DWMIPV4ADDRESS_HH_
46 #include <sys/types.h>
47 #include <netinet/in.h>
53#include <boost/asio.hpp>
57 using ipv4addr_t = uint32_t;
86 inline ipv4addr_t
Raw()
const
92 explicit operator std::string ()
const;
98 {
return (ntohl(_addr) < ntohl(addr._addr)); }
104 {
return (ntohl(_addr) > ntohl(addr._addr)); }
111 return ((ntohl(_addr) > ntohl(addr._addr))
112 || (_addr == addr._addr));
120 return ((ntohl(_addr) < ntohl(addr._addr))
121 || (_addr == addr._addr));
128 {
return (_addr == addr._addr); }
134 {
return (_addr != addr._addr); }
151 uint32_t a = ntohl(_addr);
152 if (a != 0xFFFFFFFF) {
153 _addr = htonl(a + 1);
163 uint32_t a = ntohl(_addr);
164 if (a != 0xFFFFFFFF) {
165 _addr = htonl(a + 1);
176 uint32_t a = ntohl(_addr);
178 _addr = htonl(a - 1);
188 uint32_t a = ntohl(_addr);
190 _addr = htonl(a - 1);
201 uint32_t a = ntohl(_addr);
203 _addr = htonl(a - i);
218 uint32_t a = ntohl(_addr);
219 if ((uint32_t)(a + i) >= a) {
220 _addr = htonl(a + i);
223 _addr = htonl(0xFFFFFFFF);
231 inline bool BitIsSet(int8_t bit)
const
232 {
return (((ntohl(_addr) >> bit) & 1) != 0); }
237 inline int GetBit(
int bit)
const
238 {
return (ntohl(_addr) >> (31 - bit)) & 1; }
243 static inline uint8_t MaxKeyBits()
255 std::istream &
Read(std::istream & is);
260 std::ostream &
Write(std::ostream & os)
const;
312 bool Read(boost::asio::ip::tcp::socket & s,
313 boost::system::error_code & ec)
314 {
return ASIO_Read(s, ec); }
320 bool Write(boost::asio::ip::tcp::socket & s,
321 boost::system::error_code & ec)
const;
327 bool Read(boost::asio::local::stream_protocol::socket & s,
328 boost::system::error_code & ec);
334 bool Write(boost::asio::local::stream_protocol::socket & s,
335 boost::system::error_code & ec)
const;
341 bool Read(boost::asio::generic::stream_protocol::socket & s,
342 boost::system::error_code & ec)
343 {
return ASIO_Read(s, ec); }
349 bool Write(boost::asio::generic::stream_protocol::socket & s,
350 boost::system::error_code & ec)
const;
378 template <
typename Protocol,
typename Executor>
379 bool ASIO_Read(boost::asio::basic_stream_socket<Protocol,Executor> & s,
380 boost::system::error_code & ec)
382 using boost::asio::read, boost::asio::buffer;
383 return ((read(s, buffer(&_addr,
sizeof(_addr)), ec) ==
sizeof(_addr))
This class encapsulates an IPv4 address.
Definition DwmIpv4Address.hh:63
int BZWrite(BZFILE *bzf) const
Writes to a BZFILE pointer.
Ipv4Address(ipv4addr_t addr)
Construct from an ipv4addr_t (32-bit value in network byte order).
Definition DwmIpv4Address.hh:73
bool operator>=(const Ipv4Address &addr) const
Greater than or equal-to operator.
Definition DwmIpv4Address.hh:109
Ipv4Address & operator+=(uint32_t i)
+= operator.
Definition DwmIpv4Address.hh:216
std::ostream & Write(std::ostream &os) const
Writes to an ostream. Returns the ostream.
bool Write(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes to a boost::asio::generic::stream_protocol::socket.
ssize_t Write(int fd) const
Writes to a file descriptor.
uint64_t StreamedLength() const
Returns the number of bytes that would be written if we called one of the non-compressing Write() mem...
int BZRead(BZFILE *bzf)
Reads from a BZFILE pointer.
bool Read(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec)
Reads from a boost::asio::local::stream_protocol::socket.
bool operator>(const Ipv4Address &addr) const
Greater-than operator.
Definition DwmIpv4Address.hh:103
bool Write(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes to a boost::asio::local::stream_protocol::socket.
Ipv4Address(const std::string &dottedAddr)
Construct from a dotted-decimal string.
Ipv4Address & operator-=(uint32_t i)
-= operator.
Definition DwmIpv4Address.hh:199
bool operator<(const Ipv4Address &addr) const
less-than operator.
Definition DwmIpv4Address.hh:97
size_t Write(FILE *f) const
Writes to a FILE pointer. Returns 1 on success, 0 on failure.
bool Write(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec) const
Writes to a boost::asio::ip::tcp::socket.
Ipv4Address()
Constructor.
friend Ipv4Address operator&(const Ipv4Address &addr, const Ipv4Address &mask)
Apply a mask (bitwise AND) to the given addr and return the result.
bool operator<=(const Ipv4Address &addr) const
Less-than or equal-to operator.
Definition DwmIpv4Address.hh:118
bool Read(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec)
Reads from a boost::asio::ip::tcp::socket.
Definition DwmIpv4Address.hh:312
bool Read(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec)
Reads from a boost::asio::generic::stream_protocol::socket.
Definition DwmIpv4Address.hh:341
ssize_t Read(int fd)
Reads from a file descriptor.
Ipv4Address & operator&=(const Ipv4Address &mask)
Mask operator.
Definition DwmIpv4Address.hh:139
bool operator!=(const Ipv4Address &addr) const
Not-equal-to operator.
Definition DwmIpv4Address.hh:133
Ipv4Address & operator--()
Pre-decrement operator.
Definition DwmIpv4Address.hh:186
ipv4addr_t Raw() const
Returns an ipv4addr_t representation (32-bit value in network byte order).
Definition DwmIpv4Address.hh:86
uint32_t IpSum() const
Returns the partial sum of the IP address, as would be used when calculating the IP header checksum o...
std::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
size_t Read(FILE *f)
Reads from a FILE pointer. Returns 1 on success, 0 on failure.
friend std::ostream & operator<<(std::ostream &os, const Ipv4Address &addr)
Prints an Ipv4Address to an ostream in human-readable form.
Ipv4Address & operator++()
Pre-increment operator.
Definition DwmIpv4Address.hh:161
int Read(gzFile gzf)
Reads from a gzFile.
bool operator==(const Ipv4Address &addr) const
Equal-to operator.
Definition DwmIpv4Address.hh:127
int Write(gzFile gzf) const
Writes to a gzFile.