41#ifndef _DWMIPV4PREFIX_HH_
42#define _DWMIPV4PREFIX_HH_
48#include <boost/asio.hpp>
94 memcpy(_data,prefix._data,5);
102 if (&prefix !=
this) {
103 memcpy(_data,prefix._data,5);
118 return(*(ipv4addr_t *)this->_data);
124 inline ipv4addr_t NetworkRaw()
const
126 return(((ipv4addr_t *)this->_data)[0]);
138 {
return this->_data[4]; }
145 if (maskLength < _data[4]) {
146 if (maskLength == 0) {
147 *(uint32_t *)_data = 0;
151 uint32_t mask = htonl(0xffffffff << (32 - maskLength));
152 *(uint32_t *)_data &= mask;
154 _data[4] = maskLength;
156 else if (maskLength < 33) {
157 _data[4] = maskLength;
282 std::istream &
Read(std::istream & is);
287 std::ostream &
Write(std::ostream & os)
const;
317 bool Read(boost::asio::ip::tcp::socket & s,
318 boost::system::error_code & ec);
324 bool Write(boost::asio::ip::tcp::socket & s,
325 boost::system::error_code & ec)
const;
331 bool Read(boost::asio::local::stream_protocol::socket & s,
332 boost::system::error_code & ec);
338 bool Write(boost::asio::local::stream_protocol::socket & s,
339 boost::system::error_code & ec)
const;
345 bool Read(boost::asio::generic::stream_protocol::socket & s,
346 boost::system::error_code & ec);
352 bool Write(boost::asio::generic::stream_protocol::socket & s,
353 boost::system::error_code & ec)
const;
385 inline const uint8_t *
Data()
const
389 alignas(4) uint8_t _data[5];
394 template <
typename S>
396 bool ASIO_Read(S & s, boost::system::error_code & ec)
398 using boost::asio::read, boost::asio::buffer;
399 return ((read(s, buffer(_data,
sizeof(_data)), ec) ==
sizeof(_data))
406 template <
typename S>
407 requires IsSupportedASIOSocket<S>
408 bool ASIO_Write(S & s, boost::system::error_code & ec)
const
410 using boost::asio::write, boost::asio::buffer;
411 return ((write(s, buffer(_data,
sizeof(_data)), ec) ==
sizeof(_data))
Dwm::ASIO class declaration.
Dwm::Ipv4Address class definition.
std::vector< Ipv4Prefix > Ipv4RangePrefixes(const Ipv4Address &addr1, const Ipv4Address &addr2)
Returns the minimal set of IPv4 prefixes that cover the entire range between addr1 and addr2.
This class encapsulates an IPv4 address.
Definition DwmIpv4Address.hh:63
This class encapsulates an IPv4 address and netmask.
Definition DwmIpv4Prefix.hh:59
Ipv4Prefix & operator--()
Pre-decrement operator.
ssize_t Write(int fd) const
Writes a prefix to a file descriptor.
Ipv4Prefix()
Constructor. Initializes the prefix to 255.255.255.255/32.
Ipv4Prefix(const Ipv4Address &network, const Ipv4Address &netmask)
Construct from an IP address and netmask.
bool Adjacent(const Ipv4Prefix &prefix) const
Returns true if the given prefix is adjacent.
const uint8_t * Data() const
Returns a pointer to the 5-byte data.
Definition DwmIpv4Prefix.hh:385
Ipv4Prefix(const Ipv4Address &addr)
Construct from an Ipv4Address. Sets the netmask to 255.255.255.255.
Ipv4Prefix(const Ipv4Prefix &prefix)
Copy constructor.
Definition DwmIpv4Prefix.hh:92
size_t Write(FILE *f) const
Writes a prefix to a FILE pointer.
bool Read(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec)
Reads the prefix from s.
bool Read(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec)
Reads the prefix from s.
std::ostream & Write(std::ostream &os) const
Writes a prefix to an ostream. Returns the ostream.
bool operator>(const Ipv4Prefix &prefix) const
Returns true if this is greater than prefix.
int Write(gzFile gzf) const
Writes a prefix to a gzFile.
Ipv4Prefix(const std::string &prefix)
Construct from a string of the form a.b.c.d/n.
Ipv4Prefix & operator++()
Pre-increment operator.
bool Write(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec) const
Writes the prefix to s.
ssize_t Read(int fd)
Reads a prefix from a file descriptor.
Ipv4Address LastAddress() const
Returns the last IPv4 address in the prefix's range.
bool operator!=(const Ipv4Prefix &prefix) const
Not-equal-to operator.
Ipv4Prefix(const Ipv4Address &network, uint8_t maskLength)
Constructor accepting an IP address and a netmask length.
int BZWrite(BZFILE *bzf) const
Writes a prefix to a BZFILE pointer.
virtual ~Ipv4Prefix()
Destructor.
bool Write(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes the prefix to s.
Ipv4Address Netmask() const
Returns the netmask portion of the prefix.
bool Set(const Ipv4Address &network, uint8_t maskLength)
Initializes the prefix using an IP address and a netmask length.
uint8_t MaskLength(uint8_t maskLength)
Changes the length of the netmask (number of significant bits).
Definition DwmIpv4Prefix.hh:143
std::pair< bool, Ipv4Prefix > Combine(const Ipv4Prefix &prefix) const
If the given prefix can be combined with this prefix to form a single prefix, returns [true,...
std::string ToString() const
Returns a string representation of the prefix in 'a.b.c.d/n' form.
Ipv4Prefix & operator=(const Ipv4Prefix &prefix)
operator =
Definition DwmIpv4Prefix.hh:100
bool operator==(const Ipv4Prefix &prefix) const
Equal-to operator.
bool Write(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes the prefix to s.
size_t Read(FILE *f)
Reads a prefix from a FILE pointer.
std::istream & Read(std::istream &is)
Reads a prefix from an istream. Returns the istream.
bool operator<(const Ipv4Prefix &prefix) const
Returns true if this is less than prefix.
uint8_t MaskLength() const
Returns the length of the netmask (number of significant bits).
Definition DwmIpv4Prefix.hh:137
bool Contains(const Ipv4Address &address) const
Returs true if address falls within the prefix.
friend std::ostream & operator<<(std::ostream &os, const Ipv4Prefix &prefix)
Prints an Ipv4Prefix to an ostream in 'a.b.c.d/n' form.
Ipv4Address FirstAddress() const
Returns the first IPv4 address in the prefix's range.
int BZRead(BZFILE *bzf)
Reads a prefix from a BZFILE pointer.
Ipv4Address Network() const
Returns the network portion of the prefix.
Definition DwmIpv4Prefix.hh:116
bool Read(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec)
Reads the prefix from s.
std::string ToShortString() const
Returns a string representation of the prefix in 'a.b.c.d/n' form, but with unnecessary trailing zero...
bool Contains(const Ipv4Prefix &prefix) const
Returns true if prefix is a subnet of this.
int Read(gzFile gzf)
Reads a prefix from a gzFile.
void Set(const Ipv4Address &network, const Ipv4Address &netmask)
Initializes the prefix using an IP address and a netmask.
uint64_t StreamedLength() const
Returns the number of bytes that should be written if a Write() member was called.
Definition DwmIpv4Prefix.hh:377
Concept for the types of ASIO sockets we can utilize.
Definition DwmASIO.hh:68