libtins  4.0
routing_utils.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_ROUTING_UTILS_H
31 #define TINS_ROUTING_UTILS_H
32 
33 #include <vector>
34 #include <set>
35 #include <tins/macros.h>
36 #include <tins/ip_address.h>
37 #include <tins/ipv6_address.h>
38 
39 // Fix for Windows interface define on combaseapi.h
40 #undef interface
41 
42 namespace Tins {
43 namespace Utils {
44 
48 struct RouteEntry {
52  std::string interface;
53 
58 
63 
68 
72  int metric;
73 };
74 
78 struct Route6Entry {
82  std::string interface;
83 
88 
93 
98 
102  int metric;
103 };
104 
110 template<typename ForwardIterator>
111 void route_entries(ForwardIterator output);
112 
118 template<typename ForwardIterator>
119 void route6_entries(ForwardIterator output);
120 
126 TINS_API std::vector<RouteEntry> route_entries();
127 
128 
134 TINS_API std::vector<Route6Entry> route6_entries();
135 
143 TINS_API std::set<std::string> network_interfaces();
144 
156 TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address& gw_addr);
157 
169 TINS_API bool gateway_from_ip(IPv6Address ip, IPv6Address& gw_addr);
170 
171 } // Utils
172 } // Tins
173 
174 template<typename ForwardIterator>
175 void Tins::Utils::route_entries(ForwardIterator output) {
176  std::vector<RouteEntry> entries = route_entries();
177  for (size_t i = 0; i < entries.size(); ++i) {
178  *output = entries[i];
179  ++output;
180  }
181 }
182 
183 template<typename ForwardIterator>
184 void Tins::Utils::route6_entries(ForwardIterator output) {
185  std::vector<Route6Entry> entries = route6_entries();
186  for (size_t i = 0; i < entries.size(); ++i) {
187  *output = entries[i];
188  ++output;
189  }
190 }
191 
192 #endif // TINS_ROUTING_UTILS_H
TINS_API std::set< std::string > network_interfaces()
List all network interfaces.
Definition: routing_utils.cpp:414
IPv6Address destination
Definition: routing_utils.h:87
void route6_entries(ForwardIterator output)
Retrieves entries in the routing table.
Definition: routing_utils.h:184
IPv4Address destination
Definition: routing_utils.h:57
int metric
Definition: routing_utils.h:102
Definition: routing_utils.h:48
std::string interface
Definition: routing_utils.h:82
IPv6Address gateway
Definition: routing_utils.h:97
std::string interface
Definition: routing_utils.h:52
The Tins namespace.
Definition: address_range.h:38
IPv4Address mask
Definition: routing_utils.h:67
IPv6Address mask
Definition: routing_utils.h:92
Abstraction of an IPv4 address.
Definition: ip_address.h:45
IPv4Address gateway
Definition: routing_utils.h:62
Definition: ipv6_address.h:45
TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr)
Finds the gateway&#39;s IP address for the given IP address.
Definition: routing_utils.cpp:429
Definition: routing_utils.h:78
int metric
Definition: routing_utils.h:72
void route_entries(ForwardIterator output)
Retrieves entries in the routing table.
Definition: routing_utils.h:175