forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkInterface.h
More file actions
361 lines (281 loc) · 11.5 KB
/
NetworkInterface.h
File metadata and controls
361 lines (281 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
//
// NetworkInterface.h
//
// $Id: //poco/1.4/Net/include/Poco/Net/NetworkInterface.h#4 $
//
// Library: Net
// Package: Sockets
// Module: NetworkInterface
//
// Definition of the NetworkInterface class.
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef Net_NetworkInterface_INCLUDED
#define Net_NetworkInterface_INCLUDED
#include "Poco/Net/Net.h"
#ifdef POCO_NET_HAS_INTERFACE
#include "Poco/Net/IPAddress.h"
#include "Poco/Mutex.h"
#include "Poco/Tuple.h"
#include <map>
namespace Poco {
namespace Net {
class NetworkInterfaceImpl;
class Net_API NetworkInterface
/// This class represents a network interface.
///
/// NetworkInterface is used with MulticastSocket to specify
/// multicast interfaces for sending and receiving multicast
/// messages.
///
/// The class also provides static member functions for
/// enumerating or searching network interfaces and their
/// respective configuration values.
{
public:
typedef std::vector<NetworkInterface> List;
typedef List NetworkInterfaceList;//@deprecated
typedef std::map<unsigned, NetworkInterface> Map;
typedef Poco::Tuple<IPAddress, IPAddress, IPAddress> AddressTuple;
typedef std::vector<AddressTuple> AddressList;
typedef AddressList::iterator AddressIterator;
typedef AddressList::const_iterator ConstAddressIterator;
typedef std::vector<unsigned char> MACAddress;
enum AddressType
{
IP_ADDRESS,
SUBNET_MASK,
BROADCAST_ADDRESS
};
enum Type
{
NI_TYPE_ETHERNET_CSMACD,
NI_TYPE_ISO88025_TOKENRING,
NI_TYPE_FRAMERELAY,
NI_TYPE_PPP,
NI_TYPE_SOFTWARE_LOOPBACK,
NI_TYPE_ATM,
NI_TYPE_IEEE80211,
NI_TYPE_TUNNEL,
NI_TYPE_IEEE1394,
NI_TYPE_OTHER
};
enum IPVersion
{
IPv4_ONLY, /// Return interfaces with IPv4 address only
IPv6_ONLY, /// Return interfaces with IPv6 address only
IPv4_OR_IPv6 /// Return interfaces with IPv4 or IPv6 address
};
static const unsigned NO_INDEX = ~0;
#if defined(POCO_OS_FAMILY_WINDOWS)
static const char MAC_SEPARATOR = '-';
#else
static const char MAC_SEPARATOR = ':';
#endif
NetworkInterface(unsigned index = NO_INDEX);
/// Creates a NetworkInterface representing the
/// default interface.
///
/// The name is empty, the IP address is the wildcard
/// address and the index is max value of unsigned integer.
NetworkInterface(const NetworkInterface& interfc);
/// Creates the NetworkInterface by copying another one.
~NetworkInterface();
/// Destroys the NetworkInterface.
NetworkInterface& operator = (const NetworkInterface& interfc);
/// Assigns another NetworkInterface.
bool operator < (const NetworkInterface& other) const;
/// Operatorr less-than.
bool operator == (const NetworkInterface& other) const;
/// Operator equal. Compares interface indices.
void swap(NetworkInterface& other);
/// Swaps the NetworkInterface with another one.
unsigned index() const;
/// Returns the interface OS index.
const std::string& name() const;
/// Returns the interface name.
const std::string& displayName() const;
/// Returns the interface display name.
///
/// On Windows platforms, this is currently the network adapter
/// name. This may change to the "friendly name" of the network
/// connection in a future version, however.
///
/// On other platforms this is the same as name().
const std::string& adapterName() const;
/// Returns the interface adapter name.
///
/// On Windows platforms, this is the network adapter LUID.
/// The adapter name is used by some Windows Net APIs like Dhcp.
///
/// On other platforms this is the same as name().
const IPAddress& firstAddress(IPAddress::Family family) const;
/// Returns the first IP address bound to the interface.
/// Throws NotFoundException if the address family is not
/// configured on the interface.
void firstAddress(IPAddress& addr, IPAddress::Family family = IPAddress::IPv4) const;
/// Returns the first IP address bound to the interface.
/// If the address family is not configured on the interface,
/// the address returned in addr will be unspecified (wildcard).
const IPAddress& address(unsigned index = 0) const;
/// Returns the IP address bound to the interface at index position.
void addAddress(const IPAddress& address);
/// Adds address to the interface.
void addAddress(const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress);
/// Adds address to the interface.
const AddressList& addressList() const;
/// Returns the list of IP addresses bound to the interface.
const IPAddress& subnetMask(unsigned index) const;
/// Returns the subnet mask for this network interface.
const IPAddress& broadcastAddress(unsigned index) const;
/// Returns the broadcast address for this network interface.
const IPAddress& destAddress(unsigned index) const;
/// Returns the IPv4 point-to-point destiation address for this network interface.
const MACAddress& macAddress() const;
/// Returns MAC (Media Access Control) address for the interface.
unsigned mtu() const;
/// Returns the MTU for this interface.
NetworkInterface::Type type() const;
/// returns the MIB IfType of the interface.
bool supportsIP() const;
/// Returns true if the interface supports IP.
bool supportsIPv4() const;
/// Returns true if the interface supports IPv4.
bool supportsIPv6() const;
/// Returns true if the interface supports IPv6.
bool supportsBroadcast() const;
/// Returns true if the interface supports broadcast.
bool supportsMulticast() const;
/// Returns true if the interface supports multicast.
bool isLoopback() const;
/// Returns true if the interface is loopback.
bool isPointToPoint() const;
/// Returns true if the interface is point-to-point.
bool isRunning() const;
/// Returns true if the interface is running.
bool isUp() const;
/// Returns true if the interface is up.
static NetworkInterface forName(const std::string& name, bool requireIPv6 = false);
/// Returns the NetworkInterface for the given name.
///
/// If requireIPv6 is false, an IPv4 interface is returned.
/// Otherwise, an IPv6 interface is returned.
///
/// Throws an InterfaceNotFoundException if an interface
/// with the give name does not exist.
static NetworkInterface forName(const std::string& name, IPVersion ipVersion);
/// Returns the NetworkInterface for the given name.
///
/// The ipVersion argument can be used to specify whether
/// an IPv4 (IPv4_ONLY) or IPv6 (IPv6_ONLY) interface is required,
/// or whether the caller does not care (IPv4_OR_IPv6).
static NetworkInterface forAddress(const IPAddress& address);
/// Returns the NetworkInterface for the given IP address.
///
/// Throws an InterfaceNotFoundException if an interface
/// with the give address does not exist.
static NetworkInterface forIndex(unsigned index);
/// Returns the NetworkInterface for the given interface index.
///
/// Throws an InterfaceNotFoundException if an interface
/// with the given index does not exist (or IPv6 is not
/// available).
static List list(bool ipOnly = true, bool upOnly = true);
/// Returns a list with all network interfaces
/// on the system.
///
/// If ipOnly is true, only interfaces supporting IP
/// are returned. Otherwise, all system network interfaces
/// are returned.
///
/// If upOnly is true, only interfaces being up are returned.
/// Otherwise, both interfaces being up and down are returned.
///
/// If there are multiple addresses bound to one interface,
/// multiple NetworkInterface entries are listed for
/// the same interface.
static Map map(bool ipOnly = true, bool upOnly = true);
/// Returns a map containing system network interfaces
/// Map is keyed by interface system indices.
///
/// If ipOnly is true, only interfaces supporting IP
/// are returned. Otherwise, all system network interfaces
/// are returned.
///
/// If upOnly is true, only interfaces being up are returned.
/// Otherwise, both interfaces being up and down are returned.
///
/// If there are multiple addresses bound to one interface,
/// they are contained within the NetworkInterface (second)
/// member of the pair.
protected:
NetworkInterface(const std::string& name, const std::string& displayName, const std::string& adapterName, const IPAddress& address, unsigned index, MACAddress* pMACAddress = 0);
/// Creates the NetworkInterface.
NetworkInterface(const std::string& name, const std::string& displayName, const std::string& adapterName, unsigned index, MACAddress* pMACAddress = 0);
/// Creates the NetworkInterface.
NetworkInterface(const std::string& name, const IPAddress& address, unsigned index, MACAddress* pMACAddress = 0);
/// Creates the NetworkInterface.
NetworkInterface(const std::string& name,
const std::string& displayName,
const std::string& adapterName,
const IPAddress& address,
const IPAddress& subnetMask,
const IPAddress& broadcastAddress,
unsigned index,
MACAddress* pMACAddress = 0);
/// Creates the NetworkInterface.
NetworkInterface(const std::string& name,
const IPAddress& address,
const IPAddress& subnetMask,
const IPAddress& broadcastAddress,
unsigned index,
MACAddress* pMACAddress = 0);
/// Creates the NetworkInterface.
IPAddress interfaceNameToAddress(const std::string& interfaceName) const;
/// Determines the IPAddress bound to the interface with the given name.
unsigned interfaceNameToIndex(const std::string& interfaceName) const;
/// Determines the interface index of the interface with the given name.
NetworkInterfaceImpl& impl() { return *_pImpl; };
private:
NetworkInterfaceImpl* _pImpl;
static Poco::FastMutex _mutex;
};
///
/// inlines
///
inline bool NetworkInterface::operator < (const NetworkInterface& other) const
{
return this->index() < other.index();
}
inline bool NetworkInterface::operator == (const NetworkInterface& other) const
{
return this->index() == other.index();
}
} } // namespace Poco::Net
Net_API std::ostream& operator<<(std::ostream& os, const Poco::Net::NetworkInterface::MACAddress& mac);
#endif // POCO_NET_HAS_INTERFACE
#endif // Net_NetworkInterface_INCLUDED