mirror of
https://github.com/mfontanini/libtins
synced 2026-01-23 02:35:57 +01:00
Add metric to RouteEntry.
This commit is contained in:
1
.gitmodules
vendored
1
.gitmodules
vendored
@@ -1,3 +1,4 @@
|
||||
[submodule "googletest"]
|
||||
path = googletest
|
||||
url = https://github.com/smarr/googletest.git
|
||||
ignore = dirty
|
||||
|
||||
@@ -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)
|
||||
|
||||
48
examples/route_table.cpp
Normal file
48
examples/route_table.cpp
Normal file
@@ -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 <iostream>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <tins/tins.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace Tins;
|
||||
|
||||
int main() {
|
||||
vector<Utils::RouteEntry> 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;
|
||||
}
|
||||
}
|
||||
@@ -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,12 +411,13 @@ template<class ForwardIterator>
|
||||
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)
|
||||
for(unsigned i(0); i < 4; ++i)
|
||||
input >> metric;
|
||||
input >> mask;
|
||||
from_hex(destination, dummy);
|
||||
entry.destination = IPv4Address(dummy);
|
||||
@@ -416,6 +425,8 @@ void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user