libDwm-0.6.0
DwmIpv4Address.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmIpv4Address.hh 8939 $
3 // @(#) $Id: DwmIpv4Address.hh 8939 2017-03-31 04:02:30Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2004-2006, 2016
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 _DWMIPV4ADDRESS_HH_
43 #define _DWMIPV4ADDRESS_HH_
44 
45 extern "C" {
46  #include <inttypes.h>
47  #include <sys/types.h>
48  #include <netinet/in.h>
49 }
50 
51 #include <string>
52 
53 #include "DwmIpAddress.hh"
54 
55 typedef uint32_t ipv4addr_t;
56 
57 namespace Dwm {
58 
59  //--------------------------------------------------------------------------
61  //--------------------------------------------------------------------------
63  : public IpAddress
64  {
65  public:
66  //------------------------------------------------------------------------
68  //------------------------------------------------------------------------
69  Ipv4Address();
70 
71  //------------------------------------------------------------------------
73  //------------------------------------------------------------------------
74  inline Ipv4Address(ipv4addr_t addr)
75  : _addr(addr)
76  {}
77 
78  //------------------------------------------------------------------------
80  //------------------------------------------------------------------------
81  Ipv4Address(const std::string & dottedAddr);
82 
83  //------------------------------------------------------------------------
86  //------------------------------------------------------------------------
87  inline ipv4addr_t Raw() const
88  {
89  return(_addr);
90  }
91 
92  //------------------------------------------------------------------------
94  //------------------------------------------------------------------------
95  operator std::string () const;
96 
97  //------------------------------------------------------------------------
99  //------------------------------------------------------------------------
100  inline bool IsV6() const
101  {
102  return(false);
103  }
104 
105  //------------------------------------------------------------------------
107  //------------------------------------------------------------------------
108  inline bool operator < (const Ipv4Address & addr) const
109  {
110  return(ntohl(_addr) < ntohl(addr._addr));
111  }
112 
113  //------------------------------------------------------------------------
115  //------------------------------------------------------------------------
116  inline bool operator > (const Ipv4Address & addr) const
117  {
118  return(ntohl(_addr) > ntohl(addr._addr));
119  }
120 
121  //------------------------------------------------------------------------
123  //------------------------------------------------------------------------
124  inline bool operator >= (const Ipv4Address & addr) const
125  {
126  return((ntohl(_addr) > ntohl(addr._addr))
127  || (_addr == addr._addr));
128  }
129 
130  //------------------------------------------------------------------------
132  //------------------------------------------------------------------------
133  inline bool operator <= (const Ipv4Address & addr) const
134  {
135  return((ntohl(_addr) < ntohl(addr._addr))
136  || (_addr == addr._addr));
137  }
138 
139  //------------------------------------------------------------------------
141  //------------------------------------------------------------------------
142  inline bool operator == (const Ipv4Address & addr) const
143  {
144  return(_addr == addr._addr);
145  }
146 
147  //------------------------------------------------------------------------
149  //------------------------------------------------------------------------
150  inline bool operator != (const Ipv4Address & addr) const
151  {
152  return(_addr != addr._addr);
153  }
154 
155  //------------------------------------------------------------------------
157  //------------------------------------------------------------------------
158  inline Ipv4Address & operator &= (const Ipv4Address & mask)
159  {
160  _addr &= mask._addr;
161  return(*this);
162  }
163 
164  //------------------------------------------------------------------------
166  //------------------------------------------------------------------------
168  {
169  Ipv4Address rc(*this);
170  uint32_t a = ntohl(_addr);
171  if (a != 0xFFFFFFFF) {
172  _addr = htonl(a + 1);
173  }
174  return rc;
175  }
176 
177  //------------------------------------------------------------------------
179  //------------------------------------------------------------------------
181  {
182  uint32_t a = ntohl(_addr);
183  if (a != 0xFFFFFFFF) {
184  _addr = htonl(a + 1);
185  }
186  return *this;
187  }
188 
189  //------------------------------------------------------------------------
191  //------------------------------------------------------------------------
193  {
194  Ipv4Address rc(*this);
195  uint32_t a = ntohl(_addr);
196  if (a != 0) {
197  _addr = htonl(a - 1);
198  }
199  return rc;
200  }
201 
202  //------------------------------------------------------------------------
204  //------------------------------------------------------------------------
206  {
207  uint32_t a = ntohl(_addr);
208  if (a != 0) {
209  _addr = htonl(a - 1);
210  }
211  return *this;
212  }
213 
214  //------------------------------------------------------------------------
217  //------------------------------------------------------------------------
218  inline Ipv4Address & operator -= (uint32_t i)
219  {
220  uint32_t a = ntohl(_addr);
221  if (a >= i) {
222  _addr = htonl(a - i);
223  }
224  else {
225  _addr = htonl(0);
226  }
227  return *this;
228  }
229 
230  //------------------------------------------------------------------------
234  //------------------------------------------------------------------------
235  inline Ipv4Address & operator += (uint32_t i)
236  {
237  uint32_t a = ntohl(_addr);
238  if ((uint32_t)(a + i) >= a) {
239  _addr = htonl(a + i);
240  }
241  else {
242  _addr = htonl(0xFFFFFFFF);
243  }
244  return *this;
245  }
246 
247  inline bool BitIsSet(int8_t bit) const
248  {
249  return(((ntohl(_addr) >> bit) & 1) != 0);
250  }
251 
252  inline int GetBit(int bit) const
253  {
254  // return (((const char *)(&_addr))[bit >> 3] >> (7 - (bit & 0x7))) & 0x1;
255  // const uint8_t *p = ((const uint8_t *)(&_addr)) + (bit >> 3);
256  // return (*p >> (7 - (bit & 0x7))) & 0x1;
257 
258  return (ntohl(_addr) >> (31 - bit)) & 1;
259  /*
260  const uint8_t *p = (const uint8_t *)(&_addr);
261  return ((p[bit >> 3]) >> (7 - (bit & 0x7))) & 0x1;
262  */
263  }
264 
265  static inline uint8_t MaxKeyBits()
266  {
267  return(32);
268  }
269 
270  //------------------------------------------------------------------------
273  //------------------------------------------------------------------------
274  uint32_t StreamedLength() const;
275 
276  //------------------------------------------------------------------------
278  //------------------------------------------------------------------------
279  std::istream & Read(std::istream & is);
280 
281  //------------------------------------------------------------------------
283  //------------------------------------------------------------------------
284  std::ostream & Write(std::ostream & os) const;
285 
286  //------------------------------------------------------------------------
289  //------------------------------------------------------------------------
290  ssize_t Read(int fd);
291 
292  //------------------------------------------------------------------------
295  //------------------------------------------------------------------------
296  ssize_t Write(int fd) const;
297 
298  //------------------------------------------------------------------------
300  //------------------------------------------------------------------------
301  size_t Read(FILE * f);
302 
303  //------------------------------------------------------------------------
305  //------------------------------------------------------------------------
306  size_t Write(FILE * f) const;
307 
308  //------------------------------------------------------------------------
311  //------------------------------------------------------------------------
312  int Read(gzFile gzf);
313 
314  //------------------------------------------------------------------------
317  //------------------------------------------------------------------------
318  int Write(gzFile gzf) const;
319 
320  //------------------------------------------------------------------------
323  //------------------------------------------------------------------------
324  int BZRead(BZFILE *bzf);
325 
326  //------------------------------------------------------------------------
329  //------------------------------------------------------------------------
330  int BZWrite(BZFILE *bzf) const;
331 
332  //------------------------------------------------------------------------
334  //------------------------------------------------------------------------
335  friend std::ostream & operator << (std::ostream & os,
336  const Ipv4Address & addr);
337 
338  friend Ipv4Address operator & (const Ipv4Address & addr,
339  const Ipv4Address & mask);
340 
341  //------------------------------------------------------------------------
345  //------------------------------------------------------------------------
346  uint32_t IpSum() const;
347 
348  protected:
349  uint32_t _addr;
350  };
351 
352 } // namespace Dwm
353 
354 #endif // _DWMIPV4ADDRESS_HH_
355 
356 //---------------------------- emacs settings -----------------------------
357 // Local Variables:
358 // mode: C++/la
359 // tab-width: 2
360 // indent-tabs-mode: nil
361 // c-basic-offset: 2
362 // End:
363 //-------------------------------------------------------------------------
Ipv4Address & operator--()
Pre-decrement operator.
Definition: DwmIpv4Address.hh:205
int BZRead(BZFILE *bzf)
Reads from a BZFILE pointer.
Ipv4Address()
Constructor.
Ipv4Address & operator-=(uint32_t i)
-= operator.
Definition: DwmIpv4Address.hh:218
uint32_t IpSum() const
Returns the partial sum of the IP address, as would be used when calculating the IP header checksum o...
This class encapsulates an IPv4 address.
Definition: DwmIpv4Address.hh:62
Dwm::IpAddress class definition.
uint32_t StreamedLength() const
Returns the number of bytes that would be written if we called one of the Write() members...
Ipv4Address & operator&=(const Ipv4Address &mask)
Mask operator.
Definition: DwmIpv4Address.hh:158
friend std::ostream & operator<<(std::ostream &os, const Ipv4Address &addr)
Prints an Ipv4Address to an ostream in human-readable form.
bool operator>=(const Ipv4Address &addr) const
Greater than or equal-to operator.
Definition: DwmIpv4Address.hh:124
int BZWrite(BZFILE *bzf) const
Writes to a BZFILE pointer.
Ipv4Address & operator++()
Pre-increment operator.
Definition: DwmIpv4Address.hh:180
Ipv4Address & operator+=(uint32_t i)
+= operator.
Definition: DwmIpv4Address.hh:235
bool operator==(const Ipv4Address &addr) const
Equal-to operator.
Definition: DwmIpv4Address.hh:142
Definition: DwmBZ2IO.hh:67
bool operator<=(const Ipv4Address &addr) const
Less-than or equal-to operator.
Definition: DwmIpv4Address.hh:133
Ipv4Address(ipv4addr_t addr)
Construct from an ipv4addr_t (32-bit value in network byte order).
Definition: DwmIpv4Address.hh:74
std::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
This class encapsulates an IP address.
Definition: DwmIpAddress.hh:63
bool operator>(const Ipv4Address &addr) const
Greater-than operator.
Definition: DwmIpv4Address.hh:116
ipv4addr_t Raw() const
Returns an ipv4addr_t representation (32-bit value in network byte order).
Definition: DwmIpv4Address.hh:87
std::ostream & Write(std::ostream &os) const
Writes to an ostream. Returns the ostream.
bool operator!=(const Ipv4Address &addr) const
Not-equal-to operator.
Definition: DwmIpv4Address.hh:150
bool operator<(const Ipv4Address &addr) const
less-than operator.
Definition: DwmIpv4Address.hh:108