libDwm-0.9.45
DwmIpv4Address.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004-2006, 2016, 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 _DWMIPV4ADDRESS_HH_
42#define _DWMIPV4ADDRESS_HH_
43
44extern "C" {
45 #include <inttypes.h>
46 #include <sys/types.h>
47 #include <netinet/in.h>
48 #include <bzlib.h>
49 #include <zlib.h>
50}
51
52#include <string>
53#include <boost/asio.hpp>
54
55namespace Dwm {
56
57 using ipv4addr_t = uint32_t;
58
59 //--------------------------------------------------------------------------
61 //--------------------------------------------------------------------------
63 {
64 public:
65 //------------------------------------------------------------------------
67 //------------------------------------------------------------------------
69
70 //------------------------------------------------------------------------
72 //------------------------------------------------------------------------
73 inline Ipv4Address(ipv4addr_t addr)
74 : _addr(addr)
75 {}
76
77 //------------------------------------------------------------------------
79 //------------------------------------------------------------------------
80 Ipv4Address(const std::string & dottedAddr);
81
82 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
86 inline ipv4addr_t Raw() const
87 { return(_addr); }
88
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
92 explicit operator std::string () const;
93
94 //------------------------------------------------------------------------
96 //------------------------------------------------------------------------
97 inline bool operator < (const Ipv4Address & addr) const
98 { return (ntohl(_addr) < ntohl(addr._addr)); }
99
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
103 inline bool operator > (const Ipv4Address & addr) const
104 { return (ntohl(_addr) > ntohl(addr._addr)); }
105
106 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
109 inline bool operator >= (const Ipv4Address & addr) const
110 {
111 return ((ntohl(_addr) > ntohl(addr._addr))
112 || (_addr == addr._addr));
113 }
114
115 //------------------------------------------------------------------------
117 //------------------------------------------------------------------------
118 inline bool operator <= (const Ipv4Address & addr) const
119 {
120 return ((ntohl(_addr) < ntohl(addr._addr))
121 || (_addr == addr._addr));
122 }
123
124 //------------------------------------------------------------------------
126 //------------------------------------------------------------------------
127 inline bool operator == (const Ipv4Address & addr) const
128 { return (_addr == addr._addr); }
129
130 //------------------------------------------------------------------------
132 //------------------------------------------------------------------------
133 inline bool operator != (const Ipv4Address & addr) const
134 { return (_addr != addr._addr); }
135
136 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 inline Ipv4Address & operator &= (const Ipv4Address & mask)
140 {
141 _addr &= mask._addr;
142 return *this;
143 }
144
145 //------------------------------------------------------------------------
147 //------------------------------------------------------------------------
149 {
150 Ipv4Address rc(*this);
151 uint32_t a = ntohl(_addr);
152 if (a != 0xFFFFFFFF) {
153 _addr = htonl(a + 1);
154 }
155 return rc;
156 }
157
158 //------------------------------------------------------------------------
160 //------------------------------------------------------------------------
162 {
163 uint32_t a = ntohl(_addr);
164 if (a != 0xFFFFFFFF) {
165 _addr = htonl(a + 1);
166 }
167 return *this;
168 }
169
170 //------------------------------------------------------------------------
172 //------------------------------------------------------------------------
174 {
175 Ipv4Address rc(*this);
176 uint32_t a = ntohl(_addr);
177 if (a != 0) {
178 _addr = htonl(a - 1);
179 }
180 return rc;
181 }
182
183 //------------------------------------------------------------------------
185 //------------------------------------------------------------------------
187 {
188 uint32_t a = ntohl(_addr);
189 if (a != 0) {
190 _addr = htonl(a - 1);
191 }
192 return *this;
193 }
194
195 //------------------------------------------------------------------------
198 //------------------------------------------------------------------------
199 inline Ipv4Address & operator -= (uint32_t i)
200 {
201 uint32_t a = ntohl(_addr);
202 if (a >= i) {
203 _addr = htonl(a - i);
204 }
205 else {
206 _addr = htonl(0);
207 }
208 return *this;
209 }
210
211 //------------------------------------------------------------------------
215 //------------------------------------------------------------------------
216 inline Ipv4Address & operator += (uint32_t i)
217 {
218 uint32_t a = ntohl(_addr);
219 if ((uint32_t)(a + i) >= a) {
220 _addr = htonl(a + i);
221 }
222 else {
223 _addr = htonl(0xFFFFFFFF);
224 }
225 return *this;
226 }
227
228 //------------------------------------------------------------------------
230 //------------------------------------------------------------------------
231 inline bool BitIsSet(int8_t bit) const
232 { return (((ntohl(_addr) >> bit) & 1) != 0); }
233
234 //------------------------------------------------------------------------
236 //------------------------------------------------------------------------
237 inline int GetBit(int bit) const
238 { return (ntohl(_addr) >> (31 - bit)) & 1; }
239
240 //------------------------------------------------------------------------
242 //------------------------------------------------------------------------
243 static inline uint8_t MaxKeyBits()
244 { return 32; }
245
246 //------------------------------------------------------------------------
249 //------------------------------------------------------------------------
250 uint64_t StreamedLength() const;
251
252 //------------------------------------------------------------------------
254 //------------------------------------------------------------------------
255 std::istream & Read(std::istream & is);
256
257 //------------------------------------------------------------------------
259 //------------------------------------------------------------------------
260 std::ostream & Write(std::ostream & os) const;
261
262 //------------------------------------------------------------------------
265 //------------------------------------------------------------------------
266 ssize_t Read(int fd);
267
268 //------------------------------------------------------------------------
271 //------------------------------------------------------------------------
272 ssize_t Write(int fd) const;
273
274 //------------------------------------------------------------------------
276 //------------------------------------------------------------------------
277 size_t Read(FILE * f);
278
279 //------------------------------------------------------------------------
281 //------------------------------------------------------------------------
282 size_t Write(FILE * f) const;
283
284 //------------------------------------------------------------------------
287 //------------------------------------------------------------------------
288 int Read(gzFile gzf);
289
290 //------------------------------------------------------------------------
293 //------------------------------------------------------------------------
294 int Write(gzFile gzf) const;
295
296 //------------------------------------------------------------------------
299 //------------------------------------------------------------------------
300 int BZRead(BZFILE *bzf);
301
302 //------------------------------------------------------------------------
305 //------------------------------------------------------------------------
306 int BZWrite(BZFILE *bzf) const;
307
308 //------------------------------------------------------------------------
311 //------------------------------------------------------------------------
312 bool Read(boost::asio::ip::tcp::socket & s,
313 boost::system::error_code & ec)
314 { return ASIO_Read(s, ec); }
315
316 //------------------------------------------------------------------------
319 //------------------------------------------------------------------------
320 bool Write(boost::asio::ip::tcp::socket & s,
321 boost::system::error_code & ec) const;
322
323 //------------------------------------------------------------------------
326 //------------------------------------------------------------------------
327 bool Read(boost::asio::local::stream_protocol::socket & s,
328 boost::system::error_code & ec);
329
330 //------------------------------------------------------------------------
333 //------------------------------------------------------------------------
334 bool Write(boost::asio::local::stream_protocol::socket & s,
335 boost::system::error_code & ec) const;
336
337 //------------------------------------------------------------------------
340 //------------------------------------------------------------------------
341 bool Read(boost::asio::generic::stream_protocol::socket & s,
342 boost::system::error_code & ec)
343 { return ASIO_Read(s, ec); }
344
345 //------------------------------------------------------------------------
348 //------------------------------------------------------------------------
349 bool Write(boost::asio::generic::stream_protocol::socket & s,
350 boost::system::error_code & ec) const;
351
352 //------------------------------------------------------------------------
354 //------------------------------------------------------------------------
355 friend std::ostream & operator << (std::ostream & os,
356 const Ipv4Address & addr);
357
358 //------------------------------------------------------------------------
361 //------------------------------------------------------------------------
363 const Ipv4Address & mask);
364
365 //------------------------------------------------------------------------
369 //------------------------------------------------------------------------
370 uint32_t IpSum() const;
371
372 protected:
373 uint32_t _addr;
374
375 //------------------------------------------------------------------------
377 //------------------------------------------------------------------------
378 template <typename Protocol, typename Executor>
379 bool ASIO_Read(boost::asio::basic_stream_socket<Protocol,Executor> & s,
380 boost::system::error_code & ec)
381 {
382 using boost::asio::read, boost::asio::buffer;
383 return ((read(s, buffer(&_addr, sizeof(_addr)), ec) == sizeof(_addr))
384 && (! ec));
385 }
386 };
387
388} // namespace Dwm
389
390#endif // _DWMIPV4ADDRESS_HH_
391
392//---------------------------- emacs settings -----------------------------
393// Local Variables:
394// mode: C++
395// tab-width: 2
396// indent-tabs-mode: nil
397// c-basic-offset: 2
398// End:
399//-------------------------------------------------------------------------
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.