libDwm-0.9.45
DwmIpv6Prefix.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2005-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 _DWMIPV6PREFIX_HH_
42#define _DWMIPV6PREFIX_HH_
43
44#include "DwmIpv6Address.hh"
45
46namespace Dwm {
47
48 //--------------------------------------------------------------------------
50 //--------------------------------------------------------------------------
52 {
53 public:
54 //------------------------------------------------------------------------
56 //------------------------------------------------------------------------
58
59 //------------------------------------------------------------------------
61 //------------------------------------------------------------------------
62 Ipv6Prefix(const std::string & prefix);
63
64 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
69 Ipv6Prefix(const Ipv6Address & network, uint8_t maskLength);
70
71 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
74 Ipv6Prefix(const Ipv6Prefix & prefix);
75
76 //------------------------------------------------------------------------
78 //------------------------------------------------------------------------
80
81 //------------------------------------------------------------------------
83 //------------------------------------------------------------------------
84 inline const in6_addr & In6Addr() const
85 { return _addr; }
86
87 //------------------------------------------------------------------------
89 //------------------------------------------------------------------------
91
92 //------------------------------------------------------------------------
94 //------------------------------------------------------------------------
95 inline uint8_t MaskLength() const
96 { return _length; }
97
98 //------------------------------------------------------------------------
100 //------------------------------------------------------------------------
101 uint8_t MaskLength(uint8_t maskLen);
102
103 //------------------------------------------------------------------------
106 //------------------------------------------------------------------------
107 bool Set(const Ipv6Address & network, uint8_t maskLength);
108
109 //------------------------------------------------------------------------
111 //------------------------------------------------------------------------
112 bool operator < (const Ipv6Prefix & prefix) const;
113
114 //------------------------------------------------------------------------
116 //------------------------------------------------------------------------
117 bool operator > (const Ipv6Prefix & prefix) const;
118
119 //------------------------------------------------------------------------
121 //------------------------------------------------------------------------
122 bool operator == (const Ipv6Prefix & prefix) const;
123
124 //------------------------------------------------------------------------
126 //------------------------------------------------------------------------
127 bool operator != (const Ipv6Prefix & prefix) const;
128
129 //------------------------------------------------------------------------
131 //------------------------------------------------------------------------
132 bool Contains(const Ipv6Address & addr) const;
133
134 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 ssize_t Read(int fd);
138
139 //------------------------------------------------------------------------
141 //------------------------------------------------------------------------
142 ssize_t Write(int fd) const;
143
144 //------------------------------------------------------------------------
147 //------------------------------------------------------------------------
148 size_t Read(FILE *f);
149
150 //------------------------------------------------------------------------
153 //------------------------------------------------------------------------
154 size_t Write(FILE *f) const;
155
156 //------------------------------------------------------------------------
158 //------------------------------------------------------------------------
159 std::istream & Read(std::istream & is);
160
161 //------------------------------------------------------------------------
163 //------------------------------------------------------------------------
164 std::ostream & Write(std::ostream & os) const;
165
166 //------------------------------------------------------------------------
168 //------------------------------------------------------------------------
169 int Read(gzFile gzf);
170
171 //------------------------------------------------------------------------
173 //------------------------------------------------------------------------
174 int Write(gzFile gzf) const;
175
176 //------------------------------------------------------------------------
178 //------------------------------------------------------------------------
179 int BZRead(BZFILE *bzf);
180
181 //------------------------------------------------------------------------
183 //------------------------------------------------------------------------
184 int BZWrite(BZFILE *bzf) const;
185
186 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
190 bool Read(boost::asio::ip::tcp::socket & s,
191 boost::system::error_code & ec);
192
193 //------------------------------------------------------------------------
196 //------------------------------------------------------------------------
197 bool Write(boost::asio::ip::tcp::socket & s,
198 boost::system::error_code & ec) const;
199
200 //------------------------------------------------------------------------
203 //------------------------------------------------------------------------
204 bool Read(boost::asio::local::stream_protocol::socket & s,
205 boost::system::error_code & ec);
206
207 //------------------------------------------------------------------------
210 //------------------------------------------------------------------------
211 bool Write(boost::asio::local::stream_protocol::socket & s,
212 boost::system::error_code & ec) const;
213
214 //------------------------------------------------------------------------
217 //------------------------------------------------------------------------
218 bool Read(boost::asio::generic::stream_protocol::socket & s,
219 boost::system::error_code & ec);
220
221 //------------------------------------------------------------------------
224 //------------------------------------------------------------------------
225 bool Write(boost::asio::generic::stream_protocol::socket & s,
226 boost::system::error_code & ec) const;
227
228 //------------------------------------------------------------------------
230 //------------------------------------------------------------------------
231 uint64_t StreamedLength() const
232 { return (sizeof(_length) + ((_length + 7) >> 3)); }
233
234 //------------------------------------------------------------------------
236 //------------------------------------------------------------------------
237 friend std::ostream & operator << (std::ostream & os,
238 const Ipv6Prefix & prefix);
239
240 //------------------------------------------------------------------------
243 //------------------------------------------------------------------------
244 std::string ToString() const;
245
246 //------------------------------------------------------------------------
254 //------------------------------------------------------------------------
255 inline uint64_t Hash() const
256 {
257 return XXH32(_addr.s6_addr, (_length + 7) >> 3, 0);
258 // return XXH3_64bits(_addr.s6_addr, 16);
259 }
260
261 private:
262 alignas(8) struct in6_addr _addr;
263 uint8_t _length;
264
265 //------------------------------------------------------------------------
267 //------------------------------------------------------------------------
268 inline uint8_t NumAddrBytes() const
269 {
270 return((_length + 7) >> 3);
271 }
272
273 //------------------------------------------------------------------------
275 //------------------------------------------------------------------------
276 template <typename S>
277 requires IsSupportedASIOSocket<S>
278 bool ASIO_Read(S & s, boost::system::error_code & ec)
279 {
280 using boost::asio::read, boost::asio::buffer;
281 bool rc = false;
282 if ((read(s, buffer(&_length, sizeof(_length)), ec) == sizeof(_length))
283 && (! ec)) {
284 size_t len = (_length + 7) >> 3;
285 if ((read(s, buffer(_addr.s6_addr, len), ec) == sizeof(_length))
286 && (! ec)) {
287 rc = true;
288 }
289 }
290 return rc;
291 }
292
293 //------------------------------------------------------------------------
295 //------------------------------------------------------------------------
296 template <typename S>
297 requires IsSupportedASIOSocket<S>
298 bool ASIO_Write(S & s, boost::system::error_code & ec) const
299 {
300 using boost::asio::write, boost::asio::buffer;
301 bool rc = false;
302 if ((write(s, buffer(&_length, sizeof(_length)), ec) == sizeof(_length))
303 && (! ec)) {
304 size_t len = (_length + 7) >> 3;
305 if ((write(s, buffer(_addr.s6_addr, len), ec) == sizeof(_addr.s6_addr))
306 && (! ec)) {
307 rc = true;
308 }
309 }
310 return rc;
311 }
312
313 };
314
315} // namespace Dwm
316
317#endif // _DWMIPV6PREFIX_HH_
318
319//---------------------------- emacs settings -----------------------------
320// Local Variables:
321// mode: C++
322// tab-width: 2
323// indent-tabs-mode: nil
324// c-basic-offset: 2
325// End:
326//-------------------------------------------------------------------------
Dwm::Ipv6Address class definition.
This class encapsulates an IPv6 address.
Definition DwmIpv6Address.hh:64
This class encapsulates an IPv6 network prefix.
Definition DwmIpv6Prefix.hh:52
bool operator==(const Ipv6Prefix &prefix) const
Equal-to operator.
std::ostream & Write(std::ostream &os) const
Writes the prefix to an ostream. Returns the ostream.
Ipv6Prefix()
Constructor.
bool Read(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec)
Reads the prefix from s.
Ipv6Address Network() const
Returns the network address.
bool operator<(const Ipv6Prefix &prefix) const
Less-than operator.
bool Read(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec)
Reads the prefix from s.
int BZRead(BZFILE *bzf)
Reads the prefix from a BZFILE pointer.
ssize_t Read(int fd)
Reads the prefix from a file descriptor.
std::string ToString() const
Returns a string representation of the prefix in the usual form, for example "1234:5678::/32".
Ipv6Prefix(const Ipv6Prefix &prefix)
Copy constructor.
Ipv6Prefix(const Ipv6Address &network, uint8_t maskLength)
Construct from an IPv6 network address and netmask length.
bool operator>(const Ipv6Prefix &prefix) const
Greater-than operator.
Ipv6Prefix(const std::string &prefix)
Construct from a string.
bool Set(const Ipv6Address &network, uint8_t maskLength)
Set the prefix using a network address and netmask length.
friend std::ostream & operator<<(std::ostream &os, const Ipv6Prefix &prefix)
Prints a prefix to an ostream in the usual colon-delimited form.
bool Read(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec)
Reads the prefix from s.
bool Write(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes the prefix to s.
size_t Write(FILE *f) const
Writes the prefix to a FILE pointer.
size_t Read(FILE *f)
Reads the prefix from a FILE pointer.
bool operator!=(const Ipv6Prefix &prefix) const
Not-equal-to operator.
uint64_t Hash() const
Given that the number of IPv6 addresses I typically deal with is nowhere near 4 billion,...
Definition DwmIpv6Prefix.hh:255
ssize_t Write(int fd) const
Writes the prefix to a file descriptor.
bool Write(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec) const
Writes the prefix to s.
uint8_t MaskLength(uint8_t maskLen)
Sets and returns the netmask length.
int Read(gzFile gzf)
Reads the prefix from a gzFile.
bool Write(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes the prefix to s.
std::istream & Read(std::istream &is)
Reads the prefix from an istream. Returns the istream.
uint8_t MaskLength() const
Returns the netmask length.
Definition DwmIpv6Prefix.hh:95
int BZWrite(BZFILE *bzf) const
Writes the prefix to a BZFILE pointer.
bool Contains(const Ipv6Address &addr) const
Returns true if addr falls within the prefix.
Ipv6Address Netmask() const
Returns the netmask.
int Write(gzFile gzf) const
Writes the prefix to a gzFile.