libDwm-0.9.45
DwmIpv6Address.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004-2006, 2016, 2020, 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 _DWMIPV6ADDRESS_HH_
42#define _DWMIPV6ADDRESS_HH_
43
44extern "C" {
45 #include <netinet/in.h>
46 #include <bzlib.h>
47 #include <zlib.h>
48}
49
50#include <cstdint>
51#include <string>
52
53#define XXH_INLINE_ALL
54#include <xxhash.h>
55
56#include "DwmASIO.hh"
57
58namespace Dwm {
59
60 //--------------------------------------------------------------------------
62 //--------------------------------------------------------------------------
64 {
65 public:
66 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
70
71 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
74 Ipv6Address(const std::string & addr);
75
76 //------------------------------------------------------------------------
78 //------------------------------------------------------------------------
79 Ipv6Address(const struct in6_addr & addr);
80
81 //------------------------------------------------------------------------
83 //------------------------------------------------------------------------
84 operator std::string () const;
85
86 //------------------------------------------------------------------------
88 //------------------------------------------------------------------------
89 operator struct in6_addr () const;
90
91 //------------------------------------------------------------------------
93 //------------------------------------------------------------------------
94 bool operator < (const Ipv6Address & addr) const;
95
96 //------------------------------------------------------------------------
98 //------------------------------------------------------------------------
99 bool operator > (const Ipv6Address & addr) const;
100
101 //------------------------------------------------------------------------
103 //------------------------------------------------------------------------
104 bool operator == (const Ipv6Address & addr) const;
105
106 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
109 inline bool operator != (const Ipv6Address & addr) const
110 { return (! (*this == addr)); }
111
112 //------------------------------------------------------------------------
114 //------------------------------------------------------------------------
116
117 //------------------------------------------------------------------------
119 //------------------------------------------------------------------------
120 Ipv6Address operator & (const Ipv6Address & netmask) const;
121
122 //------------------------------------------------------------------------
124 //------------------------------------------------------------------------
125 bool IsLinkLocal() const;
126
127 //------------------------------------------------------------------------
129 //------------------------------------------------------------------------
130 bool IsSiteLocal() const;
131
132 //------------------------------------------------------------------------
134 //------------------------------------------------------------------------
135 inline const uint8_t *RawPointer() const
136 {
137 return (const uint8_t *)&_addr;
138 }
139
140 //------------------------------------------------------------------------
142 //------------------------------------------------------------------------
143 inline const in6_addr & In6Addr() const
144 {
145 return _addr;
146 }
147
148 //------------------------------------------------------------------------
151 //------------------------------------------------------------------------
152 uint64_t StreamedLength() const;
153
154 //------------------------------------------------------------------------
156 //------------------------------------------------------------------------
157 std::istream & Read(std::istream & is);
158
159 //------------------------------------------------------------------------
161 //------------------------------------------------------------------------
162 std::ostream & Write(std::ostream & os) const;
163
164 //------------------------------------------------------------------------
167 //------------------------------------------------------------------------
168 ssize_t Read(int fd);
169
170 //------------------------------------------------------------------------
173 //------------------------------------------------------------------------
174 ssize_t Write(int fd) const;
175
176 //------------------------------------------------------------------------
178 //------------------------------------------------------------------------
179 size_t Read(FILE * f);
180
181 //------------------------------------------------------------------------
183 //------------------------------------------------------------------------
184 size_t Write(FILE * f) const;
185
186 //------------------------------------------------------------------------
189 //------------------------------------------------------------------------
190 int Read(gzFile gzf);
191
192 //------------------------------------------------------------------------
195 //------------------------------------------------------------------------
196 int Write(gzFile gzf) const;
197
198 //------------------------------------------------------------------------
201 //------------------------------------------------------------------------
202 int BZRead(BZFILE *bzf);
203
204 //------------------------------------------------------------------------
207 //------------------------------------------------------------------------
208 int BZWrite(BZFILE *bzf) const;
209
210 //------------------------------------------------------------------------
212 //------------------------------------------------------------------------
213 template <typename ST>
215 bool ASIO_Read(ST & s, boost::system::error_code & ec)
216 {
217 using boost::asio::read, boost::asio::buffer;
218 return ((read(s, buffer(_addr.s6_addr, 16), ec) == 16) && (! ec));
219 }
220
221 //------------------------------------------------------------------------
223 //------------------------------------------------------------------------
224 template <typename ST>
226 bool ASIO_Write(ST & s, boost::system::error_code & ec) const
227 {
228 using boost::asio::write, boost::asio::buffer;
229 return ((write(s, buffer(_addr.s6_addr, 16), ec) == 16) && (! ec));
230 }
231
232 //------------------------------------------------------------------------
235 //------------------------------------------------------------------------
236 bool Read(boost::asio::ip::tcp::socket & s,
237 boost::system::error_code & ec)
238 { return ASIO_Read(s, ec); }
239
240 //------------------------------------------------------------------------
243 //------------------------------------------------------------------------
244 bool Write(boost::asio::ip::tcp::socket & s,
245 boost::system::error_code & ec) const
246 { return ASIO_Write(s, ec); }
247
248 //------------------------------------------------------------------------
251 //------------------------------------------------------------------------
252 bool Read(boost::asio::local::stream_protocol::socket & s,
253 boost::system::error_code & ec)
254 { return ASIO_Read(s, ec); }
255
256 //------------------------------------------------------------------------
259 //------------------------------------------------------------------------
260 bool Write(boost::asio::local::stream_protocol::socket & s,
261 boost::system::error_code & ec) const
262 { return ASIO_Write(s, ec); }
263
264 //------------------------------------------------------------------------
267 //------------------------------------------------------------------------
268 bool Read(boost::asio::generic::stream_protocol::socket & s,
269 boost::system::error_code & ec)
270 { return ASIO_Read(s, ec); }
271
272 //------------------------------------------------------------------------
275 //------------------------------------------------------------------------
276 bool Write(boost::asio::generic::stream_protocol::socket & s,
277 boost::system::error_code & ec) const
278 { return ASIO_Write(s, ec); }
279
280 //------------------------------------------------------------------------
282 //------------------------------------------------------------------------
283 friend std::ostream & operator << (std::ostream & os,
284 const Ipv6Address & addr);
285
286 //------------------------------------------------------------------------
288 //------------------------------------------------------------------------
289 inline uint64_t Hash() const
290 {
291 return XXH64(_addr.s6_addr, 16, 0);
292 }
293
294 private:
295 alignas(8) struct in6_addr _addr;
296 };
297
298} // namespace Dwm
299
300#endif // _DWMIPV6ADDRESS_HH_
301
302//---------------------------- emacs settings -----------------------------
303// Local Variables:
304// mode: C++
305// tab-width: 2
306// indent-tabs-mode: nil
307// c-basic-offset: 2
308// End:
309//-------------------------------------------------------------------------
Dwm::ASIO class declaration.
This class encapsulates an IPv6 address.
Definition DwmIpv6Address.hh:64
Ipv6Address & operator&=(const Ipv6Address &netmask)
Mask operator.
Ipv6Address(const struct in6_addr &addr)
Construct from an in6_addr.
uint64_t Hash() const
Returns a 64-bit hash value for the Ipv6Address.
Definition DwmIpv6Address.hh:289
size_t Write(FILE *f) const
Writes to a FILE pointer. Returns 1 on success, 0 on failure.
bool Read(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec)
Reads the Ipv6Address from s.
Definition DwmIpv6Address.hh:236
bool IsLinkLocal() const
Returns true if the address is link-local.
ssize_t Read(int fd)
Reads from a file descriptor.
bool Write(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes the Ipv6Address to s.
Definition DwmIpv6Address.hh:260
int BZRead(BZFILE *bzf)
Reads from a BZFILE pointer.
std::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
Ipv6Address operator&(const Ipv6Address &netmask) const
Mask operator.
int Read(gzFile gzf)
Reads from a gzFile.
bool operator<(const Ipv6Address &addr) const
Less-than operator.
bool Read(boost::asio::local::stream_protocol::socket &s, boost::system::error_code &ec)
Reads the Ipv6Address from s.
Definition DwmIpv6Address.hh:252
Ipv6Address()
Constructor. Sets address to 0::
bool IsSiteLocal() const
Returns true if the address is site-local.
bool Write(boost::asio::ip::tcp::socket &s, boost::system::error_code &ec) const
Writes the Ipv6Address to s.
Definition DwmIpv6Address.hh:244
uint64_t StreamedLength() const
Returns the number of bytes that would be written if we called one of the Write() members.
bool ASIO_Read(ST &s, boost::system::error_code &ec)
Function template intended for boost::asio stream sockets.
Definition DwmIpv6Address.hh:215
bool Write(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec) const
Writes the Ipv6Address to s.
Definition DwmIpv6Address.hh:276
int BZWrite(BZFILE *bzf) const
Writes to a BZFILE pointer.
ssize_t Write(int fd) const
Writes to a file descriptor.
Ipv6Address(const std::string &addr)
Construct from a string.
bool ASIO_Write(ST &s, boost::system::error_code &ec) const
Function template intended for boost::asio stream sockets.
Definition DwmIpv6Address.hh:226
bool operator>(const Ipv6Address &addr) const
Greater-than operator.
std::ostream & Write(std::ostream &os) const
Writes to an ostream. Returns the ostream.
bool operator==(const Ipv6Address &addr) const
Equal-to operator.
bool Read(boost::asio::generic::stream_protocol::socket &s, boost::system::error_code &ec)
Reads the Ipv6Address from s.
Definition DwmIpv6Address.hh:268
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 Ipv6Address &addr)
Prints an Ipv6Address to an ostream in human-readable form.
int Write(gzFile gzf) const
Writes to a gzFile.
const uint8_t * RawPointer() const
Returns a pointer to the memory containing the address.
Definition DwmIpv6Address.hh:135
bool operator!=(const Ipv6Address &addr) const
Not-equal-to operator.
Definition DwmIpv6Address.hh:109
Concept for the types of ASIO sockets we can utilize.
Definition DwmASIO.hh:68