libtins  4.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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 "../macros.h"
36 #include "../ip_address.h"
37 #include "../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 TINS_API std::vector<RouteEntry> route_entries();
119 
125 TINS_API std::vector<Route6Entry> route6_entries();
126 
134 TINS_API std::set<std::string> network_interfaces();
135 
147 TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address& gw_addr);
148 
149 } // Utils
150 } // Tins
151 
152 template<typename ForwardIterator>
153 void Tins::Utils::route_entries(ForwardIterator output) {
154  std::vector<RouteEntry> entries = route_entries();
155  for (size_t i = 0; i < entries.size(); ++i) {
156  *output = entries[i];
157  ++output;
158  }
159 }
160 
161 #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
TINS_API std::vector< Route6Entry > route6_entries()
Retrieves entries in the routing table.
Definition: routing_utils.cpp:359
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
IPv4Address mask
Definition: routing_utils.h:67
IPv6Address mask
Definition: routing_utils.h:92
Abstraction of an IPv4 address.
Definition: ip_address.h:44
IPv4Address gateway
Definition: routing_utils.h:62
Definition: ipv6_address.h:44
TINS_API bool gateway_from_ip(IPv4Address ip, IPv4Address &gw_addr)
Finds the gateway'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:153