libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 "macros.h"
37 #include "hw_address.h"
38 #include "ip_address.h"
39 #include "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 
127  id_type id() const {
128  return iface_id_;
129  }
130 
140  std::string name() const;
141 
160  std::wstring friendly_name() const;
161 
169  Info addresses() const;
170 
178  Info info() const;
179 
186  operator bool() const {
187  return iface_id_ != 0;
188  }
189 
194  bool is_loopback() const;
195 
202  bool is_up() const;
203 
207  address_type hw_address() const;
208 
212  IPv4Address ipv4_address() const;
213 
217  IPv4Address ipv4_mask() const;
218 
222  IPv4Address ipv4_broadcast() const;
223 
227  std::vector<IPv6Prefix> ipv6_addresses() const;
228 
234  bool operator==(const NetworkInterface& rhs) const {
235  return iface_id_ == rhs.iface_id_;
236  }
237 
243  bool operator!=(const NetworkInterface& rhs) const {
244  return !(*this == rhs);
245  }
246 private:
247  id_type resolve_index(const char* name);
248 
249  id_type iface_id_;
250 };
251 
252 } // Tins
253 
254 #endif // TINS_NETWORK_INTERFACE_H
id_type id() const
Getter for this interface's identifier.
Definition: network_interface.h:127
bool operator==(const NetworkInterface &rhs) const
Compares this interface for equality.
Definition: network_interface.h:234
bool operator!=(const NetworkInterface &rhs) const
Compares this interface for inequality.
Definition: network_interface.h:243
uint32_t id_type
The type used to store the interface'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:44
Definition: network_interface.h:62
Definition: ipv6_address.h:44
HWAddress< 6 > address_type
The type of this interface's address.
Definition: network_interface.h:57
Struct that holds an interface's addresses.
Definition: network_interface.h:70