libDwm-0.6.0
DwmIpv4Prefix.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4Prefix.hh 8951 $
3 // @(#) $Id: DwmIpv4Prefix.hh 8951 2017-04-01 05:19:20Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2004-2006
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
33 // PURPOSE, OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY
34 // PATENT, TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMIPV4PREFIX_HH_
43 #define _DWMIPV4PREFIX_HH_
44 
45 extern "C" {
46 #include <inttypes.h>
47 
48  typedef uint32_t ipv4addr_t;
49 }
50 
51 #include <cstring>
52 #include <iostream>
53 #include <utility>
54 #include <vector>
55 
56 #include "DwmIpv4Address.hh"
57 #include "DwmReadable.hh"
58 #include "DwmWritable.hh"
59 #include "DwmGZReadable.hh"
60 #include "DwmGZWritable.hh"
61 #include "DwmBZ2Readable.hh"
62 #include "DwmBZ2Writable.hh"
63 
64 namespace Dwm {
65 
66  //--------------------------------------------------------------------------
68  //--------------------------------------------------------------------------
69  class Ipv4Prefix
70  : public Readable, public Writable,
71  public GZReadable, public GZWritable,
72  public BZ2Readable, public BZ2Writable
73  {
74  public:
75  //------------------------------------------------------------------------
77  //------------------------------------------------------------------------
78  Ipv4Prefix();
79 
80  //------------------------------------------------------------------------
82  //------------------------------------------------------------------------
83  Ipv4Prefix(const Ipv4Address & network, uint8_t maskLength);
84 
85  //------------------------------------------------------------------------
89  // For example, 128.35.104.1/16 will result in 128.35/16.
90  //------------------------------------------------------------------------
91  Ipv4Prefix(const std::string & prefix);
92 
93  //------------------------------------------------------------------------
95  //------------------------------------------------------------------------
96  Ipv4Prefix(const Ipv4Address & addr);
97 
98  //------------------------------------------------------------------------
100  //------------------------------------------------------------------------
101  inline Ipv4Prefix(const Ipv4Prefix & prefix)
102  {
103  memcpy(_data,prefix._data,5);
104  }
105 
106  //------------------------------------------------------------------------
108  //------------------------------------------------------------------------
109  inline Ipv4Prefix & operator = (const Ipv4Prefix & prefix)
110  {
111  if (&prefix != this)
112  memcpy(_data,prefix._data,5);
113  return(*this);
114  }
115 
116  //------------------------------------------------------------------------
118  //------------------------------------------------------------------------
119  virtual ~Ipv4Prefix();
120 
121  //------------------------------------------------------------------------
123  //------------------------------------------------------------------------
124  inline Ipv4Address Network() const
125  {
126  return(*(ipv4addr_t *)this->_data);
127  }
128 
129  inline ipv4addr_t NetworkRaw() const
130  {
131  return(*(ipv4addr_t *)this->_data);
132  }
133 
134  //------------------------------------------------------------------------
136  //------------------------------------------------------------------------
137  Ipv4Address Netmask() const;
138 
139  //------------------------------------------------------------------------
141  //------------------------------------------------------------------------
142  uint8_t MaskLength() const;
143 
144  //------------------------------------------------------------------------
146  //------------------------------------------------------------------------
147  inline uint8_t MaskLength(uint8_t maskLength)
148  {
149  if (maskLength < _data[4]) {
150  if (maskLength == 0) {
151  *(uint32_t *)_data = 0;
152  }
153  else {
154  // need to zero some bits
155  uint32_t mask = htonl(0xffffffff << (32 - maskLength));
156  *(uint32_t *)_data &= mask;
157  }
158  _data[4] = maskLength;
159  }
160  else if (maskLength < 33) {
161  _data[4] = maskLength;
162  }
163  return(_data[4]);
164  }
165 
166  //------------------------------------------------------------------------
168  //------------------------------------------------------------------------
169  Ipv4Address FirstAddress() const;
170 
171  //------------------------------------------------------------------------
173  //------------------------------------------------------------------------
174  Ipv4Address LastAddress() const;
175 
176  //------------------------------------------------------------------------
178  //------------------------------------------------------------------------
179  bool Set(const Ipv4Address & network, uint8_t maskLength);
180 
181  //------------------------------------------------------------------------
188  //------------------------------------------------------------------------
189  bool operator < (const Ipv4Prefix & prefix) const;
190 
191  //------------------------------------------------------------------------
198  //------------------------------------------------------------------------
199  bool operator > (const Ipv4Prefix & prefix) const;;
200 
201  //------------------------------------------------------------------------
203  //------------------------------------------------------------------------
204  bool operator == (const Ipv4Prefix & prefix) const;
205 
206  //------------------------------------------------------------------------
208  //------------------------------------------------------------------------
209  bool operator != (const Ipv4Prefix & prefix) const;
210 
211  //------------------------------------------------------------------------
213  //------------------------------------------------------------------------
214  bool Contains(const Ipv4Address & address) const;
215 
216  //------------------------------------------------------------------------
218  //------------------------------------------------------------------------
219  bool Contains(const Ipv4Prefix & prefix) const;
220 
221  //------------------------------------------------------------------------
223  //------------------------------------------------------------------------
224  bool Adjacent(const Ipv4Prefix & prefix) const;
225 
226  //------------------------------------------------------------------------
231  //------------------------------------------------------------------------
232  std::pair<bool,Ipv4Prefix> Combine(const Ipv4Prefix & prefix) const;
233 
234  //------------------------------------------------------------------------
237  //------------------------------------------------------------------------
238  ssize_t Read(int fd);
239 
240  //------------------------------------------------------------------------
243  //------------------------------------------------------------------------
244  ssize_t Write(int fd) const;
245 
246  //------------------------------------------------------------------------
249  //------------------------------------------------------------------------
250  size_t Read(FILE *f);
251 
252  //------------------------------------------------------------------------
255  //------------------------------------------------------------------------
256  size_t Write(FILE *f) const;
257 
258  //------------------------------------------------------------------------
260  //------------------------------------------------------------------------
261  std::istream & Read(std::istream & is);
262 
263  //------------------------------------------------------------------------
265  //------------------------------------------------------------------------
266  std::ostream & Write(std::ostream & os) const;
267 
268  //------------------------------------------------------------------------
271  //------------------------------------------------------------------------
272  int Read(gzFile gzf);
273 
274  //------------------------------------------------------------------------
277  //------------------------------------------------------------------------
278  int Write(gzFile gzf) const;
279 
280  //------------------------------------------------------------------------
283  //------------------------------------------------------------------------
284  int BZRead(BZFILE *bzf);
285 
286  //------------------------------------------------------------------------
289  //------------------------------------------------------------------------
290  int BZWrite(BZFILE *bzf) const;
291 
292  //------------------------------------------------------------------------
295  //------------------------------------------------------------------------
296  friend std::ostream & operator << (std::ostream & os,
297  const Ipv4Prefix & prefix);
298 
299  //------------------------------------------------------------------------
301  //------------------------------------------------------------------------
302  std::string ToString() const;
303 
304  //------------------------------------------------------------------------
307  //------------------------------------------------------------------------
308  std::string ToShortString() const;
309 
310  //------------------------------------------------------------------------
313  //------------------------------------------------------------------------
314  inline uint32_t StreamedLength() const
315  {
316  return(5);
317  }
318 
319  //------------------------------------------------------------------------
323  //------------------------------------------------------------------------
324  inline const uint8_t *Data() const
325  {
326  return(_data);
327  }
328 
329  private:
330  uint8_t _data[5];
331  };
332 
333  //--------------------------------------------------------------------------
337  //--------------------------------------------------------------------------
338  std::vector<Ipv4Prefix> Ipv4RangePrefixes(const Ipv4Address & addr1,
339  const Ipv4Address & addr2);
340 
341 } // namespace Dwm
342 
343 #endif // _DWMIPV4PREFIX_HH_
344 
345 //---------------------------- emacs settings -----------------------------
346 // Local Variables:
347 // mode: C++/la
348 // tab-width: 2
349 // indent-tabs-mode: nil
350 // c-basic-offset: 2
351 // End:
352 //-------------------------------------------------------------------------
friend std::ostream & operator<<(std::ostream &os, const Ipv4Prefix &prefix)
Prints an Ipv4Prefix to an ostream in &#39;a.b.c.d/n&#39; form.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmGZWritable.hh:57
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...
int BZRead(BZFILE *bzf)
Reads a prefix from a BZFILE pointer.
std::string ToShortString() const
Returns a string representation of the prefix in &#39;a.b.c.d/n&#39; form, but with unnecessary trailing zero...
Dwm::Writable pure virtual class definition.
Dwm::BZ2Readable pure virtual class definition.
bool operator!=(const Ipv4Prefix &prefix) const
Not-equal-to operator.
virtual ~Ipv4Prefix()
Destructor.
Dwm::GZReadable pure virtual class definition.
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
Dwm::Readable pure virtual class definition.
bool operator<(const Ipv4Prefix &prefix) const
Returns true if this is less than prefix.
ssize_t Read(int fd)
Reads a prefix from a file descriptor.
This class encapsulates an IPv4 address and netmask.
Definition: DwmIpv4Prefix.hh:69
Ipv4Address Netmask() const
Returns the netmask portion of the prefix.
ssize_t Write(int fd) const
Writes a prefix to a file descriptor.
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmBZ2Readable.hh:57
bool operator>(const Ipv4Prefix &prefix) const
Returns true if this is greater than prefix.
bool operator==(const Ipv4Prefix &prefix) const
Equal-to operator.
Ipv4Prefix(const Ipv4Prefix &prefix)
Copy constructor.
Definition: DwmIpv4Prefix.hh:101
std::string ToString() const
Returns a string representation of the prefix in &#39;a.b.c.d/n&#39; form.
bool Adjacent(const Ipv4Prefix &prefix) const
Returns true if the given prefix is adjacent.
Ipv4Prefix & operator=(const Ipv4Prefix &prefix)
operator =
Definition: DwmIpv4Prefix.hh:109
Dwm::BZ2Writable pure virtual class definition.
bool Contains(const Ipv4Address &address) const
Returs true if address falls within the prefix.
Definition: DwmBZ2IO.hh:67
uint8_t MaskLength() const
Returns the length of the netmask (number of significant bits).
Ipv4Address Network() const
Returns the network portion of the prefix.
Definition: DwmIpv4Prefix.hh:124
const uint8_t * Data() const
Returns a pointer to the 5-byte data.
Definition: DwmIpv4Prefix.hh:324
This class is a pure virtual class, defining an interface for classes that can read their contents fr...
Definition: DwmGZReadable.hh:57
uint32_t StreamedLength() const
Returns the number of bytes that should be written if a Write() member was called.
Definition: DwmIpv4Prefix.hh:314
Dwm::Ipv4Address class definition.
This class defines an interface for classes that can write their contents to an ostream, file descriptor or FILE pointer.
Definition: DwmWritable.hh:58
int BZWrite(BZFILE *bzf) const
Writes a prefix to a BZFILE pointer.
This class is a pure virtual class, defining an interface for classes that can write their contents t...
Definition: DwmBZ2Writable.hh:57
Dwm::GZWritable pure virtual class definition.
This class defines an interface for classes that can read their contents from an istream, file descriptor or FILE pointer.
Definition: DwmReadable.hh:58
bool Set(const Ipv4Address &network, uint8_t maskLength)
Initializes the prefix using an IP address and a netmask length.
Ipv4Prefix()
Constructor. Initializes the prefix to 255.255.255.255/32.
uint8_t MaskLength(uint8_t maskLength)
Changes the length of the netmask (number of significant bits).
Definition: DwmIpv4Prefix.hh:147
Ipv4Address LastAddress() const
Returns the last IPv4 address in the prefix&#39;s range.
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...
Ipv4Address FirstAddress() const
Returns the first IPv4 address in the prefix&#39;s range.