libnpupnp: /home/dockes/projets/mpdupnp/npupnp/src/inc/netif.h Source File

libnpupnp 4.1.0 An almost compatible successor for the Portable UPnP reference library

netif.h
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * Copyright (c) 2020 Jean-Francois Dockes
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * - Neither name of Intel Corporation nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 ******************************************************************************/
30 #ifndef _NETIF_H_INCLUDED_
31 #define _NETIF_H_INCLUDED_
32
38 #include <stdio.h>
39 #include <memory>
40 #include <string>
41 #include <ostream>
42 #include <vector>
43 #ifdef _WIN32
44 #include <winsock2.h>
45 #else
46 #include <sys/socket.h>
47 #include <netinet/in.h>
48 #endif
49
50 #include "UpnpGlobal.h"
51
52 namespace NetIF {
53
55 class EXPORT_SPEC IPAddr {
56 public:
59 enum class Family {Invalid = -1, IPV4 = AF_INET, IPV6 = AF_INET6};
61 enum class Scope {Invalid = -1, LINK, SITE, GLOBAL};
62 IPAddr();
64 explicit IPAddr(const char *);
66 explicit IPAddr(const std::string& s)
67 : IPAddr(s.c_str()) {}
71 explicit IPAddr(const struct sockaddr *sa, bool unmapv4=true);
72
73 IPAddr(const IPAddr&);
74 IPAddr& operator=(const IPAddr&);
75 ~IPAddr();
76
78 bool ok() const;
80 bool setScopeIdx(const IPAddr& other);
82 Family family() const;
84 Scope scopetype() const;
87 bool copyToStorage(struct sockaddr_storage *dest) const;
90 bool copyToAddr(struct sockaddr *dest) const;
91
93 const struct sockaddr_storage& getaddr() const;
94
96 std::string straddr() const;
99 std::string straddr(bool setscope, bool forurl) const;
100
101 friend class Interface;
102 class Internal;
103 private:
104 std::unique_ptr<Internal> m;
105 };
106
110 class EXPORT_SPEC Interface {
111 public:
113 enum class Flags {NONE = 0, HASIPV4 = 1, HASIPV6 = 2, LOOPBACK=4,
114 UP=8, MULTICAST=0x10, HASHWADDR=0x20};
115 Interface();
117 Interface(const char *nm);
119 Interface(const std::string &nm);
120 ~Interface();
121 Interface(const Interface&);
122 Interface& operator=(const Interface&);
123
125 const std::string& getname() const;
128 const std::string& getfriendlyname() const;
129
133 const std::string& gethwaddr() const;
135 std::string gethexhwaddr() const;
137 bool hasflag(Flags f) const;
139 bool trimto(const std::vector<IPAddr>& keep);
141 const IPAddr *firstipv4addr() const;
143 const IPAddr *firstipv6addr(
144 IPAddr::Scope scope = IPAddr::Scope::Invalid) const;
147 std::pair<const std::vector<IPAddr>&, const std::vector<IPAddr>&>
148 getaddresses() const;
150 int getindex() const;
151
153 std::ostream& print(std::ostream&) const;
154
155 class Internal;
156 friend class Interfaces;
157 private:
158 std::unique_ptr<Internal> m;
159 };
160
161
163 class EXPORT_SPEC Interfaces {
164 public:
167 static Interfaces *theInterfaces();
168
170 bool refresh();
171
173 Interface *findByName(const char*nm) const;
175 Interface *findByName(const std::string& nm) const{
176 return findByName(nm.c_str());
177 }
178
180 struct Filter {
182 std::vector<Interface::Flags> needs;
184 std::vector<Interface::Flags> rejects;
185 };
186
188 std::vector<Interface> select(const Filter& f) const;
189
191 std::ostream& print(std::ostream&);
192
200 static const Interface *interfaceForAddress(
201 const IPAddr& addr, const std::vector<Interface>& vifs,IPAddr& hostaddr);
202
208 const Interface *interfaceForAddress(const IPAddr& addr, IPAddr& hostaddr);
209
211 static void setlogfp(FILE *fp);
212
213 private:
214 Interfaces(const Interfaces &) = delete;
215 Interfaces& operator=(const Interfaces &) = delete;
216 Interfaces();
217 ~Interfaces();
218
219 class Internal;
220 std::unique_ptr<Internal> m;
221 };
222
223 } /* namespace NetIF */
224
225 #endif /* _NETIF_H_INCLUDED_ */
NetIF::Interface::Flags
Flags
Interface attributes.
Definition: netif.h:113
NetIF::Interfaces::Filter::needs
std::vector< Interface::Flags > needs
flags we want.
Definition: netif.h:182
NetIF::Interfaces
Represent the system&#39;s network interfaces. Singleton class.
Definition: netif.h:163
NetIF
Definition: netif.h:52
NetIF::Interface
Represent a system network interface, its attributes and its addresses. Usually built by the module i...
Definition: netif.h:110
NetIF::IPAddr::Family
Family
address family types. The values are identifal to the system&#39;s definitions
Definition: netif.h:59
NetIF::IPAddr::IPAddr
IPAddr(const std::string &s)
Build from textual representation (e.g. 192.168.4.4)
Definition: netif.h:66
NetIF::IPAddr::Scope
Scope
IPV6 address scope type.
Definition: netif.h:61
NetIF::Interfaces::Filter
Argument to the select() method: flags which we want or don&#39;t.
Definition: netif.h:180
NetIF::IPAddr
Represent an IPV4 or IPV6 address.
Definition: netif.h:55
NetIF::Interfaces::findByName
Interface * findByName(const std::string &nm) const
Find interface by name or friendlyname.
Definition: netif.h:175
NetIF::Interfaces::Filter::rejects
std::vector< Interface::Flags > rejects
flags we don&#39;t want.
Definition: netif.h:184