mirror of
https://github.com/mfontanini/libtins
synced 2026-01-28 04:34:27 +01:00
Utils::route_entries now works on BSD.
This commit is contained in:
@@ -37,9 +37,20 @@
|
||||
#include <iphlpapi.h>
|
||||
#undef interface
|
||||
#endif
|
||||
#include "arch.h"
|
||||
#ifdef BSD
|
||||
#include <sys/file.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysctl.h>
|
||||
|
||||
#include <net/if.h>
|
||||
#include <net/route.h>
|
||||
#include <netinet/in.h>
|
||||
#endif
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <stdint.h>
|
||||
#include "ip_address.h"
|
||||
#include "ipv6_address.h"
|
||||
@@ -293,7 +304,72 @@ namespace Tins {
|
||||
}
|
||||
}
|
||||
}
|
||||
#ifdef BSD
|
||||
inline std::vector<char> query_route_table()
|
||||
{
|
||||
int mib[6];
|
||||
std::vector<char> buf;
|
||||
size_t len;
|
||||
|
||||
mib[0] = CTL_NET;
|
||||
mib[1] = AF_ROUTE;
|
||||
mib[2] = 0;
|
||||
mib[3] = AF_INET;
|
||||
mib[4] = NET_RT_DUMP;
|
||||
mib[5] = 0;
|
||||
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
|
||||
throw std::runtime_error("Failed to ioctl");
|
||||
|
||||
buf.resize(len);
|
||||
if (sysctl(mib, 6, &buf[0], &len, NULL, 0) < 0) {
|
||||
throw std::runtime_error("Failed to ioctl");
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
template<typename ForwardIterator>
|
||||
void parse_header(struct rt_msghdr *rtm, ForwardIterator iter)
|
||||
{
|
||||
char *ptr = (char *)(rtm + 1);
|
||||
sockaddr *sa;
|
||||
|
||||
for (int i = 0; i < RTAX_MAX; i++) {
|
||||
if (rtm->rtm_addrs & (1 << i)) {
|
||||
sa = (struct sockaddr *)ptr;
|
||||
ptr += sa->sa_len;
|
||||
if (sa->sa_family == 0)
|
||||
sa = 0;
|
||||
}
|
||||
*iter++ = sa;
|
||||
}
|
||||
}
|
||||
|
||||
template<class ForwardIterator>
|
||||
void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
std::vector<char> buffer = query_route_table();
|
||||
char *next = &buffer[0], *end = &buffer[buffer.size()];
|
||||
rt_msghdr *rtm;
|
||||
std::vector<sockaddr*> sa(RTAX_MAX);
|
||||
char iface_name[IF_NAMESIZE];
|
||||
while(next < end) {
|
||||
rtm = (rt_msghdr*)next;
|
||||
parse_header(rtm, sa.begin());
|
||||
if (sa[RTAX_DST] && sa[RTAX_GATEWAY] && if_indextoname(rtm->rtm_index, iface_name)) {
|
||||
RouteEntry entry;
|
||||
entry.destination = IPv4Address(((struct sockaddr_in *)sa[RTAX_DST])->sin_addr.s_addr);
|
||||
entry.gateway = IPv4Address(((struct sockaddr_in *)sa[RTAX_GATEWAY])->sin_addr.s_addr);
|
||||
if(sa[RTAX_GENMASK])
|
||||
entry.mask = IPv4Address(((struct sockaddr_in *)sa[RTAX_GENMASK])->sin_addr.s_addr);
|
||||
else
|
||||
entry.mask = IPv4Address(uint32_t());
|
||||
entry.interface = iface_name;
|
||||
*output++ = entry;
|
||||
}
|
||||
next += rtm->rtm_msglen;
|
||||
}
|
||||
}
|
||||
#else
|
||||
template<class ForwardIterator>
|
||||
void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
using namespace Utils::Internals;
|
||||
@@ -316,5 +392,6 @@ void Tins::Utils::route_entries(ForwardIterator output) {
|
||||
++output;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // TINS_UTILS_H
|
||||
|
||||
@@ -146,18 +146,20 @@ NetworkInterface::NetworkInterface(IPv4Address ip) : iface_id(0) {
|
||||
iface_id = resolve_index("lo");
|
||||
#endif
|
||||
else {
|
||||
Utils::RouteEntry *best_match = 0;
|
||||
const Utils::RouteEntry *best_match = 0;
|
||||
entries_type entries;
|
||||
uint32_t ip_int = ip;
|
||||
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)
|
||||
iface_id = resolve_index(it->interface.c_str());
|
||||
if(!best_match || it->mask > best_match->mask) {
|
||||
best_match = &*it;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(best_match)
|
||||
if(!best_match)
|
||||
throw std::runtime_error("Error looking up interface");
|
||||
iface_id = resolve_index(best_match->interface.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include "arch.h"
|
||||
#include "utils.h"
|
||||
#ifndef WIN32
|
||||
#ifdef BSD
|
||||
#include <sys/socket.h>
|
||||
@@ -44,7 +44,6 @@
|
||||
#include <netdb.h>
|
||||
#include <net/if.h>
|
||||
#endif
|
||||
#include "utils.h"
|
||||
#include "pdu.h"
|
||||
#include "arp.h"
|
||||
#include "ethernetII.h"
|
||||
|
||||
Reference in New Issue
Block a user