diff --git a/.gitmodules b/.gitmodules index 7e14f2b..0df5ce3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "googletest"] path = googletest - url = https://github.com/smarr/googletest.git + url = https://github.com/smarr/googletest.git + ignore = dirty diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 710e0c6..b7bd997 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -30,9 +30,11 @@ IF(libtins_FOUND) ${LIBTINS_CXX11_EXAMPLES} beacon_display portscan + route_table ) ADD_EXECUTABLE(arpspoofing EXCLUDE_FROM_ALL arpspoofing.cpp) + ADD_EXECUTABLE(route_table EXCLUDE_FROM_ALL route_table.cpp) IF(HAVE_CXX11) ADD_EXECUTABLE(arpmonitor EXCLUDE_FROM_ALL arpmonitor.cpp) ADD_EXECUTABLE(dns_queries EXCLUDE_FROM_ALL dns_queries.cpp) diff --git a/examples/route_table.cpp b/examples/route_table.cpp new file mode 100644 index 0000000..1e3e2ae --- /dev/null +++ b/examples/route_table.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2015, Matias Fontanini + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include +#include +#include + +using namespace std; +using namespace Tins; + +int main() { + vector entries = Utils::route_entries(); + for (size_t i = 0; i < entries.size(); ++i) { + cout << "Entry " << setw(2) << i << ": " << endl + << "Interface: " << entries[i].interface << endl + << "Destination: " << entries[i].destination << endl + << "Gateway: " << entries[i].gateway << endl + << "Genmask: " << entries[i].mask << endl + << "Metric: " << entries[i].metric << endl << endl; + } +} \ No newline at end of file diff --git a/include/tins/utils.h b/include/tins/utils.h index 6b67903..83877da 100644 --- a/include/tins/utils.h +++ b/include/tins/utils.h @@ -94,6 +94,11 @@ namespace Tins { * This route entry's subnet mask. */ IPv4Address mask; + + /** + * This route entry's metric. + */ + int metric; }; /** @@ -371,6 +376,7 @@ void Tins::Utils::route_entries(ForwardIterator output) { else entry.mask = IPv4Address(uint32_t()); entry.interface = iface_name; + entry.metric = 0; *output++ = entry; } next += rtm->rtm_msglen; @@ -388,12 +394,14 @@ void Tins::Utils::route_entries(ForwardIterator output) { for (DWORD i = 0; i < table->dwNumEntries; i++) { MIB_IPFORWARDROW *row = &table->table[i]; - if(row->dwForwardType == MIB_IPROUTE_TYPE_INDIRECT) { + if(row->dwForwardType == MIB_IPROUTE_TYPE_INDIRECT || + row->dwForwardType == MIB_IPROUTE_TYPE_DIRECT) { RouteEntry entry; entry.interface = NetworkInterface::from_index(row->dwForwardIfIndex).name(); entry.destination = IPv4Address(row->dwForwardDest); entry.mask = IPv4Address(row->dwForwardMask); entry.gateway = IPv4Address(row->dwForwardNextHop); + entry.metric = row->dwForwardMetric1; *output++ = entry; } } @@ -403,19 +411,22 @@ template void Tins::Utils::route_entries(ForwardIterator output) { using namespace Tins::Internals; std::ifstream input("/proc/net/route"); - std::string destination, mask, gw; + std::string destination, mask, metric, gw; uint32_t dummy; skip_line(input); RouteEntry entry; while(input >> entry.interface >> destination >> gw) { - for(unsigned i(0); i < 5; ++i) - input >> mask; + for(unsigned i(0); i < 4; ++i) + input >> metric; + input >> mask; from_hex(destination, dummy); entry.destination = IPv4Address(dummy); from_hex(mask, dummy); entry.mask = IPv4Address(dummy); from_hex(gw, dummy); entry.gateway = IPv4Address(dummy); + from_hex(metric, dummy); + entry.metric = dummy; skip_line(input); *output = entry; ++output; diff --git a/src/network_interface.cpp b/src/network_interface.cpp index 1e3aaa2..0ec8361 100644 --- a/src/network_interface.cpp +++ b/src/network_interface.cpp @@ -180,7 +180,7 @@ NetworkInterface::NetworkInterface(IPv4Address ip) : iface_id(0) { Utils::route_entries(std::back_inserter(entries)); for(entries_type::const_iterator it(entries.begin()); it != entries.end(); ++it) { if((ip_int & it->mask) == it->destination) { - if(!best_match || it->mask > best_match->mask) { + if(!best_match || it->mask > best_match->mask || it->metric < best_match->metric) { best_match = &*it; } }