libtins  4.0
network_interface.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_NETWORK_INTERFACE_H
31 #define TINS_NETWORK_INTERFACE_H
32 
33 #include <string>
34 #include <vector>
35 #include <stdint.h>
36 #include <tins/macros.h>
37 #include <tins/hw_address.h>
38 #include <tins/ip_address.h>
39 #include <tins/ipv6_address.h>
40 
41 namespace Tins {
42 
47 class TINS_API NetworkInterface {
48 public:
52  typedef uint32_t id_type;
53 
58 
62  struct IPv6Prefix {
63  IPv6Address address;
64  uint32_t prefix_length;
65  };
66 
70  struct Info {
71  IPv4Address ip_addr, netmask, bcast_addr;
72  std::vector<IPv6Prefix> ipv6_addrs;
73  address_type hw_addr;
74  bool is_up;
75  };
76 
81  static NetworkInterface default_interface();
82 
86  static std::vector<NetworkInterface> all();
87 
91  static NetworkInterface from_index(id_type identifier);
92 
97 
103  NetworkInterface(const std::string& name);
104 
110  NetworkInterface(const char* name);
111 
121 
122 
132 
138  id_type id() const {
139  return iface_id_;
140  }
141 
151  std::string name() const;
152 
171  std::wstring friendly_name() const;
172 
180  Info addresses() const;
181 
189  Info info() const;
190 
197  operator bool() const {
198  return iface_id_ != 0;
199  }
200 
205  bool is_loopback() const;
206 
213  bool is_up() const;
214 
218  address_type hw_address() const;
219 
223  IPv4Address ipv4_address() const;
224 
228  IPv4Address ipv4_mask() const;
229 
233  IPv4Address ipv4_broadcast() const;
234 
238  std::vector<IPv6Prefix> ipv6_addresses() const;
239 
245  bool operator==(const NetworkInterface& rhs) const {
246  return iface_id_ == rhs.iface_id_;
247  }
248 
254  bool operator!=(const NetworkInterface& rhs) const {
255  return !(*this == rhs);
256  }
257 private:
258  id_type resolve_index(const char* name);
259 
260  id_type iface_id_;
261 };
262 
263 } // Tins
264 
265 #endif // TINS_NETWORK_INTERFACE_H
id_type id() const
Getter for this interface&#39;s identifier.
Definition: network_interface.h:138
bool operator==(const NetworkInterface &rhs) const
Compares this interface for equality.
Definition: network_interface.h:245
bool operator!=(const NetworkInterface &rhs) const
Compares this interface for inequality.
Definition: network_interface.h:254
The Tins namespace.
Definition: address_range.h:38
uint32_t id_type
The type used to store the interface&#39;s identifier.
Definition: network_interface.h:52
Abstraction of a network interface.
Definition: network_interface.h:47
Abstraction of an IPv4 address.
Definition: ip_address.h:45
Definition: network_interface.h:62
Definition: ipv6_address.h:45
HWAddress< 6 > address_type
The type of this interface&#39;s address.
Definition: network_interface.h:57
Struct that holds an interface&#39;s addresses.
Definition: network_interface.h:70