libDwm-0.9.45
DwmIpAddress.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2004, 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 PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
39//---------------------------------------------------------------------------
40
41#ifndef _DWMIPADDRESS_HH_
42#define _DWMIPADDRESS_HH_
43
44extern "C" {
45 #include <sys/socket.h>
46}
47
48#include <cstdint>
49#include <string>
50#include <variant>
51
52#include "DwmIpv4Address.hh"
53#include "DwmIpv6Address.hh"
54
55namespace Dwm {
56
57 //--------------------------------------------------------------------------
59 //--------------------------------------------------------------------------
61 {
62 public:
63 //------------------------------------------------------------------------
65 //------------------------------------------------------------------------
66 IpAddress()
67 : _addr(Ipv4Address(INADDR_NONE))
68 {}
69
70 //------------------------------------------------------------------------
72 //------------------------------------------------------------------------
73 IpAddress(const IpAddress & addr);
74
75 //------------------------------------------------------------------------
77 //------------------------------------------------------------------------
78 IpAddress & operator = (const IpAddress & addr);
79
80 //------------------------------------------------------------------------
82 //------------------------------------------------------------------------
83 IpAddress(const Ipv6Address & addr);
84
85 //------------------------------------------------------------------------
87 //------------------------------------------------------------------------
88 IpAddress(const Ipv4Address & addr);
89
90 //------------------------------------------------------------------------
92 //------------------------------------------------------------------------
93 IpAddress(const std::string & addr);
94
95 //------------------------------------------------------------------------
97 //------------------------------------------------------------------------
98 int Family() const { return k_families[_addr.index()]; }
99
100 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
103 template <typename T>
104 const T * Addr() const { return std::get_if<T>(&_addr); };
105
106 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
109 operator std::string () const;
110
111 //------------------------------------------------------------------------
113 //------------------------------------------------------------------------
114 bool operator < (const IpAddress & addr) const;
115
116 //------------------------------------------------------------------------
118 //------------------------------------------------------------------------
119 bool operator == (const IpAddress & addr) const;
120
121 //------------------------------------------------------------------------
123 //------------------------------------------------------------------------
124 friend std::ostream &
125 operator << (std::ostream & os, const IpAddress & addr);
126
127 //------------------------------------------------------------------------
130 //------------------------------------------------------------------------
131 uint64_t StreamedLength() const;
132
133 //------------------------------------------------------------------------
135 //------------------------------------------------------------------------
136 std::istream & Read(std::istream & is);
137
138 //------------------------------------------------------------------------
140 //------------------------------------------------------------------------
141 std::ostream & Write(std::ostream & os) const;
142
143 //------------------------------------------------------------------------
146 //------------------------------------------------------------------------
147 ssize_t Read(int fd);
148
149 //------------------------------------------------------------------------
152 //------------------------------------------------------------------------
153 ssize_t Write(int fd) const;
154
155 //------------------------------------------------------------------------
157 //------------------------------------------------------------------------
158 size_t Read(FILE * f);
159
160 //------------------------------------------------------------------------
162 //------------------------------------------------------------------------
163 size_t Write(FILE * f) const;
164
165 //------------------------------------------------------------------------
168 //------------------------------------------------------------------------
169 int Read(gzFile gzf);
170
171 //------------------------------------------------------------------------
174 //------------------------------------------------------------------------
175 int Write(gzFile gzf) const;
176
177 //------------------------------------------------------------------------
180 //------------------------------------------------------------------------
181 int BZRead(BZFILE *bzf);
182
183 //------------------------------------------------------------------------
186 //------------------------------------------------------------------------
187 int BZWrite(BZFILE *bzf) const;
188
189 private:
190 std::variant<Ipv6Address,Ipv4Address> _addr;
191
192 static constexpr int k_families[2] = { AF_INET6, AF_INET };
193 };
194
195} // namespace Dwm
196
197#endif // _DWMIPADDRESS_HH_
198
199//---------------------------- emacs settings -----------------------------
200// Local Variables:
201// mode: C++
202// tab-width: 2
203// indent-tabs-mode: nil
204// c-basic-offset: 2
205// End:
206//-------------------------------------------------------------------------
Dwm::Ipv4Address class definition.
Dwm::Ipv6Address class definition.
This class encapsulates an IP address.
Definition DwmIpAddress.hh:61
int Read(gzFile gzf)
Reads from a gzFile.
ssize_t Read(int fd)
Reads from 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...
ssize_t Write(int fd) const
Writes to a file descriptor.
int Write(gzFile gzf) const
Writes to a gzFile.
int BZRead(BZFILE *bzf)
Reads from a BZFILE pointer.
int BZWrite(BZFILE *bzf) const
Writes to a BZFILE pointer.
size_t Write(FILE *f) const
Writes to a FILE pointer. Returns 1 on success, 0 on failure.
std::istream & Read(std::istream &is)
Reads from an istream. Returns the istream.
std::ostream & Write(std::ostream &os) const
Writes to an ostream. Returns the ostream.
size_t Read(FILE *f)
Reads from a FILE pointer. Returns 1 on success, 0 on failure.
This class encapsulates an IPv4 address.
Definition DwmIpv4Address.hh:63
This class encapsulates an IPv6 address.
Definition DwmIpv6Address.hh:64