From acff776d56e5049116ce2c670d5281b6716758ff Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Thu, 29 Nov 2012 17:11:10 -0300 Subject: [PATCH] Utils::route_entries now works on BSD. --- include/utils.h | 77 +++++++++++++++++++++++++++++++++++++++ src/network_interface.cpp | 10 +++-- src/utils.cpp | 3 +- 3 files changed, 84 insertions(+), 6 deletions(-) diff --git a/include/utils.h b/include/utils.h index 543942a..0f00986 100644 --- a/include/utils.h +++ b/include/utils.h @@ -37,9 +37,20 @@ #include #undef interface #endif +#include "arch.h" +#ifdef BSD + #include + #include + #include + + #include + #include + #include +#endif #include #include #include +#include #include #include "ip_address.h" #include "ipv6_address.h" @@ -293,7 +304,72 @@ namespace Tins { } } } +#ifdef BSD +inline std::vector query_route_table() +{ + int mib[6]; + std::vector 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 +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 +void Tins::Utils::route_entries(ForwardIterator output) { + std::vector buffer = query_route_table(); + char *next = &buffer[0], *end = &buffer[buffer.size()]; + rt_msghdr *rtm; + std::vector 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 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 diff --git a/src/network_interface.cpp b/src/network_interface.cpp index 2985cf9..cc75490 100644 --- a/src/network_interface.cpp +++ b/src/network_interface.cpp @@ -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()); } } diff --git a/src/utils.cpp b/src/utils.cpp index 0895e24..d4180b7 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -32,7 +32,7 @@ #include #include #include -#include "arch.h" +#include "utils.h" #ifndef WIN32 #ifdef BSD #include @@ -44,7 +44,6 @@ #include #include #endif -#include "utils.h" #include "pdu.h" #include "arp.h" #include "ethernetII.h"