libDwm-0.9.45
DwmIpv4Prefix.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004-2006, 2023, 2024
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11// 1. Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// 2. Redistributions in binary form must reproduce the above copyright
14// notice, this list of conditions and the following disclaimer in the
15// documentation and/or other materials provided with the distribution.
16// 3. The names of the authors and copyright holders may not be used to
17// endorse or promote products derived from this software without
18// specific prior written permission.
19//
20// IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
21// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
22// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
23// EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
24// DAMAGE.
25//
26// THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
27// DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
28// UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
29// REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
30// IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31// WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
32// PURPOSE, OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY
33// PATENT, TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMIPV4PREFIX_HH_
42#define _DWMIPV4PREFIX_HH_
43
44#include <cstring>
45#include <iostream>
46#include <utility>
47#include <vector>
48#include <boost/asio.hpp>
49
50#include "DwmIpv4Address.hh"
51#include "DwmASIO.hh"
52
53namespace Dwm {
54
55 //--------------------------------------------------------------------------
57 //--------------------------------------------------------------------------
59 {
60 public:
61 //------------------------------------------------------------------------
63 //------------------------------------------------------------------------
65
66 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
69 Ipv4Prefix(const Ipv4Address & network, uint8_t maskLength);
70
71 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
74 Ipv4Prefix(const Ipv4Address & network, const Ipv4Address & netmask);
75
76 //------------------------------------------------------------------------
80 // For example, 128.35.104.1/16 will result in 128.35/16.
81 //------------------------------------------------------------------------
82 Ipv4Prefix(const std::string & prefix);
83
84 //------------------------------------------------------------------------
86 //------------------------------------------------------------------------
87 Ipv4Prefix(const Ipv4Address & addr);
88
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
92 inline Ipv4Prefix(const Ipv4Prefix & prefix)
93 {
94 memcpy(_data,prefix._data,5);
95 }
96
97 //------------------------------------------------------------------------
99 //------------------------------------------------------------------------
100 inline Ipv4Prefix & operator = (const Ipv4Prefix & prefix)
101 {
102 if (&prefix != this) {
103 memcpy(_data,prefix._data,5);
104 }
105 return(*this);
106 }
107
108 //------------------------------------------------------------------------
110 //------------------------------------------------------------------------
111 virtual ~Ipv4Prefix();
112
113 //------------------------------------------------------------------------
115 //------------------------------------------------------------------------
116 inline Ipv4Address Network() const
117 {
118 return(*(ipv4addr_t *)this->_data);
119 }
120
121 //------------------------------------------------------------------------
123 //------------------------------------------------------------------------
124 inline ipv4addr_t NetworkRaw() const
125 {
126 return(((ipv4addr_t *)this->_data)[0]);
127 }
128
129 //------------------------------------------------------------------------
131 //------------------------------------------------------------------------
133
134 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 inline uint8_t MaskLength() const
138 { return this->_data[4]; }
139
140 //------------------------------------------------------------------------
142 //------------------------------------------------------------------------
143 inline uint8_t MaskLength(uint8_t maskLength)
144 {
145 if (maskLength < _data[4]) {
146 if (maskLength == 0) {
147 *(uint32_t *)_data = 0;
148 }
149 else {
150 // need to zero some bits
151 uint32_t mask = htonl(0xffffffff << (32 - maskLength));
152 *(uint32_t *)_data &= mask;
153 }
154 _data[4] = maskLength;
155 }
156 else if (maskLength < 33) {
157 _data[4] = maskLength;
158 }
159 return(_data[4]);
160 }
161
162 //------------------------------------------------------------------------
164 //------------------------------------------------------------------------
166
167 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
171
172 //------------------------------------------------------------------------
174 //------------------------------------------------------------------------
175 bool Set(const Ipv4Address & network, uint8_t maskLength);
176
177 //------------------------------------------------------------------------
179 //------------------------------------------------------------------------
180 void Set(const Ipv4Address & network, const Ipv4Address & netmask);
181
182 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
190 bool operator < (const Ipv4Prefix & prefix) const;
191
192 //------------------------------------------------------------------------
199 //------------------------------------------------------------------------
200 bool operator > (const Ipv4Prefix & prefix) const;;
201
202 //------------------------------------------------------------------------
204 //------------------------------------------------------------------------
205 bool operator == (const Ipv4Prefix & prefix) const;
206
207 //------------------------------------------------------------------------
209 //------------------------------------------------------------------------
210 bool operator != (const Ipv4Prefix & prefix) const;
211
212 //------------------------------------------------------------------------
214 //------------------------------------------------------------------------
216
217 //------------------------------------------------------------------------
219 //------------------------------------------------------------------------
221
222 //------------------------------------------------------------------------
224 //------------------------------------------------------------------------
226
227 //------------------------------------------------------------------------
229 //------------------------------------------------------------------------
231
232 //------------------------------------------------------------------------
234 //------------------------------------------------------------------------
235 bool Contains(const Ipv4Address & address) const;
236
237 //------------------------------------------------------------------------
239 //------------------------------------------------------------------------
240 bool Contains(const Ipv4Prefix & prefix) const;
241
242 //------------------------------------------------------------------------
244 //------------------------------------------------------------------------
245 bool Adjacent(const Ipv4Prefix & prefix) const;
246
247 //------------------------------------------------------------------------
252 //------------------------------------------------------------------------
253 std::pair<bool,Ipv4Prefix> Combine(const Ipv4Prefix & prefix) const;
254
255 //------------------------------------------------------------------------
258 //------------------------------------------------------------------------
259 ssize_t Read(int fd);
260
261 //------------------------------------------------------------------------
264 //------------------------------------------------------------------------
265 ssize_t Write(int fd) const;
266
267 //------------------------------------------------------------------------
270 //------------------------------------------------------------------------
271 size_t Read(FILE *f);
272
273 //------------------------------------------------------------------------
276 //------------------------------------------------------------------------
277 size_t Write(FILE *f) const;
278
279 //------------------------------------------------------------------------
281 //------------------------------------------------------------------------
282 std::istream & Read(std::istream & is);
283
284 //------------------------------------------------------------------------
286 //------------------------------------------------------------------------
287 std::ostream & Write(std::ostream & os) const;
288
289 //------------------------------------------------------------------------
292 //------------------------------------------------------------------------
293 int Read(gzFile gzf);
294
295 //------------------------------------------------------------------------
298 //------------------------------------------------------------------------
299 int Write(gzFile gzf) const;
300
301 //------------------------------------------------------------------------
304 //------------------------------------------------------------------------
305 int BZRead(BZFILE *bzf);
306
307 //------------------------------------------------------------------------
310 //------------------------------------------------------------------------
311 int BZWrite(BZFILE *bzf) const;
312
313 //------------------------------------------------------------------------
316 //------------------------------------------------------------------------
317 bool Read(boost::asio::ip::tcp::socket & s,
318 boost::system::error_code & ec);
319
320 //------------------------------------------------------------------------
323 //------------------------------------------------------------------------
324 bool Write(boost::asio::ip::tcp::socket & s,
325 boost::system::error_code & ec) const;
326
327 //------------------------------------------------------------------------
330 //------------------------------------------------------------------------
331 bool Read(boost::asio::local::stream_protocol::socket & s,
332 boost::system::error_code & ec);
333
334 //------------------------------------------------------------------------
337 //------------------------------------------------------------------------
338 bool Write(boost::asio::local::stream_protocol::socket & s,
339 boost::system::error_code & ec) const;
340
341 //------------------------------------------------------------------------
344 //------------------------------------------------------------------------
345 bool Read(boost::asio::generic::stream_protocol::socket & s,
346 boost::system::error_code & ec);
347
348 //------------------------------------------------------------------------
351 //------------------------------------------------------------------------
352 bool Write(boost::asio::generic::stream_protocol::socket & s,
353 boost::system::error_code & ec) const;
354
355 //------------------------------------------------------------------------
358 //------------------------------------------------------------------------
359 friend std::ostream & operator << (std::ostream & os,
360 const Ipv4Prefix & prefix);
361
362 //------------------------------------------------------------------------
364 //------------------------------------------------------------------------
365 std::string ToString() const;
366
367 //------------------------------------------------------------------------
370 //------------------------------------------------------------------------
371 std::string ToShortString() const;
372
373 //------------------------------------------------------------------------
376 //------------------------------------------------------------------------
377 inline uint64_t StreamedLength() const
378 { return(5); }
379
380 //------------------------------------------------------------------------
384 //------------------------------------------------------------------------
385 inline const uint8_t *Data() const
386 { return(_data); }
387
388 private:
389 alignas(4) uint8_t _data[5];
390
391 //------------------------------------------------------------------------
393 //------------------------------------------------------------------------
394 template <typename S>
396 bool ASIO_Read(S & s, boost::system::error_code & ec)
397 {
398 using boost::asio::read, boost::asio::buffer;
399 return ((read(s, buffer(_data, sizeof(_data)), ec) == sizeof(_data))
400 && (! ec));
401 }
402
403 //------------------------------------------------------------------------
405 //------------------------------------------------------------------------
406 template <typename S>
407 requires IsSupportedASIOSocket<S>
408 bool ASIO_Write(S & s, boost::system::error_code & ec) const
409 {
410 using boost::asio::write, boost::asio::buffer;
411 return ((write(s, buffer(_data, sizeof(_data)), ec) == sizeof(_data))
412 && (! ec));
413 }
414 };
415
416 //--------------------------------------------------------------------------
420 //--------------------------------------------------------------------------
421 std::vector<Ipv4Prefix> Ipv4RangePrefixes(const Ipv4Address & addr1,
422 const Ipv4Address & addr2);
423
424} // namespace Dwm
425
426#endif // _DWMIPV4PREFIX_HH_
427
428//---------------------------- emacs settings -----------------------------
429// Local Variables:
430// mode: C++
431// tab-width: 2
432// indent-tabs-mode: nil
433// c-basic-offset: 2
434// End:
435//-------------------------------------------------------------------------
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