libDwm-0.9.45
DwmIpPrefix.hh
Go to the documentation of this file.
1//===========================================================================
2// @(#) $DwmPath$
3//===========================================================================
4// Copyright (c) Daniel W. McRobb 2020, 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 PURPOSE,
32// OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
33// TRADEMARK OR OTHER RIGHTS.
34//===========================================================================
35
36//---------------------------------------------------------------------------
40//---------------------------------------------------------------------------
41
42#ifndef _DWMIPPREFIX_HH_
43#define _DWMIPPREFIX_HH_
44
45#include <variant>
46
47#include "DwmIpAddress.hh"
48#include "DwmIpv4Prefix.hh"
49#include "DwmIpv6Prefix.hh"
50
51namespace Dwm {
52
53 //--------------------------------------------------------------------------
55 //--------------------------------------------------------------------------
57 {
58 public:
59 IpPrefix();
60 IpPrefix(const IpAddress & network, uint8_t maskLength);
61 IpPrefix(const Ipv4Prefix & prefix);
62 IpPrefix(const Ipv6Prefix & prefix);
63 IpPrefix(const std::string & prefix);
64
65 int Family() const;
66
67 template <typename T>
68 const T * Prefix() const { return std::get_if<T>(&_prefix); }
69
70 operator std::string () const;
71
72 bool Set(const IpAddress & network, uint8_t maskLength);
73
74 bool Contains(const IpAddress & addr) const;
75 bool Contains(const Ipv4Address & addr) const;
76 bool Contains(const Ipv6Address & addr) const;
77
78 bool operator < (const IpPrefix & prefix) const;
79 bool operator > (const IpPrefix & prefix) const;
80 bool operator == (const IpPrefix & prefix) const;
81 bool operator != (const IpPrefix & prefix) const;
82
83 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
86 friend std::ostream &
87 operator << (std::ostream & os, const IpPrefix & addr);
88
89 //------------------------------------------------------------------------
91 //------------------------------------------------------------------------
92 ssize_t Read(int fd);
93
94 //------------------------------------------------------------------------
96 //------------------------------------------------------------------------
97 ssize_t Write(int fd) const;
98
99 //------------------------------------------------------------------------
102 //------------------------------------------------------------------------
103 size_t Read(FILE *f);
104
105 //------------------------------------------------------------------------
108 //------------------------------------------------------------------------
109 size_t Write(FILE *f) const;
110
111 //------------------------------------------------------------------------
113 //------------------------------------------------------------------------
114 std::istream & Read(std::istream & is);
115
116 //------------------------------------------------------------------------
118 //------------------------------------------------------------------------
119 std::ostream & Write(std::ostream & os) const;
120
121 //------------------------------------------------------------------------
123 //------------------------------------------------------------------------
124 int Read(gzFile gzf);
125
126 //------------------------------------------------------------------------
128 //------------------------------------------------------------------------
129 int Write(gzFile gzf) const;
130
131 //------------------------------------------------------------------------
133 //------------------------------------------------------------------------
134 int BZRead(BZFILE *bzf);
135
136 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 int BZWrite(BZFILE *bzf) const;
140
141 //------------------------------------------------------------------------
144 //------------------------------------------------------------------------
145 uint64_t StreamedLength() const;
146
147 private:
148 std::variant<Ipv6Prefix,Ipv4Prefix> _prefix;
149
150 static constexpr int k_families[2] = { AF_INET6, AF_INET };
151 };
152
153} // namespace Dwm
154
155#endif // _DWMIPPREFIX_HH_
Dwm::IpAddress class definition.
Dwm::Ipv4Prefix class definition.
Dwm::Ipv6Prefix class definition.
This class encapsulates an IP address.
Definition DwmIpAddress.hh:61
Encapsulates an IP prefix (v4 or v6).
Definition DwmIpPrefix.hh:57
int BZRead(BZFILE *bzf)
Reads the prefix from a BZFILE pointer.
ssize_t Read(int fd)
Reads the prefix from a file descriptor.
int Write(gzFile gzf) const
Writes the prefix to a gzFile.
int BZWrite(BZFILE *bzf) const
Writes the prefix to a BZFILE pointer.
std::istream & Read(std::istream &is)
Reads the prefix from an istream. Returns the istream.
int Read(gzFile gzf)
Reads the prefix from a gzFile.
std::ostream & Write(std::ostream &os) const
Writes the prefix to an ostream. Returns the ostream.
uint64_t StreamedLength() const
Returns the number of bytes that would be written if the prefix was written to a file descriptor or o...
ssize_t Write(int fd) const
Writes the prefix to a file descriptor.
friend std::ostream & operator<<(std::ostream &os, const IpPrefix &addr)
ostream output operator
size_t Read(FILE *f)
Reads the prefix from a FILE pointer.
size_t Write(FILE *f) const
Writes the prefix to a FILE pointer.
This class encapsulates an IPv4 address.
Definition DwmIpv4Address.hh:63
This class encapsulates an IPv4 address and netmask.
Definition DwmIpv4Prefix.hh:59
This class encapsulates an IPv6 address.
Definition DwmIpv6Address.hh:64
This class encapsulates an IPv6 network prefix.
Definition DwmIpv6Prefix.hh:52