From c803e25db4358f3df9b25a03a80a579b48bd37ef Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Mon, 15 Aug 2011 00:10:02 -0300 Subject: [PATCH] Added Utils::interface_id. --- include/utils.h | 8 ++++++++ src/utils.cpp | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/include/utils.h b/include/utils.h index ae7cc63..91c9700 100644 --- a/include/utils.h +++ b/include/utils.h @@ -102,6 +102,14 @@ namespace Tins { */ bool interface_hwaddr(const std::string &iface, uint8_t *buffer); + /** \brief Lookup the interface identifier. + * + * If the lookup fails, false will be returned, true otherwise. + * \param iface The interface from which to extract the identifier. + * \param flag The interface id will be returned in this parameter. + */ + bool interface_id(const std::string &iface, uint32_t &id); + /** \brief Convert 32 bit integer into network byte order. * * \param data The data to convert. diff --git a/src/utils.cpp b/src/utils.cpp index 3d889c6..c0f8de6 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -57,6 +57,21 @@ struct IPv4Collector { } }; +struct InterfaceIDCollector { + uint32_t id; + bool found; + const char *iface; + + InterfaceIDCollector(const char *interface) : id(0), found(false), iface(interface) { } + + void operator() (struct ifaddrs *addr) { + if(!found && !strcmp(addr->ifa_name, iface)) { + id = addr->ifa_flags; + found = true; + } + } +}; + struct HWAddressCollector { uint8_t *result; bool found; @@ -173,6 +188,13 @@ bool Tins::Utils::interface_hwaddr(const string &iface, uint8_t *buffer) { return collector.found; } +bool Tins::Utils::interface_id(const string &iface, uint32_t &id) { + InterfaceIDCollector collector(iface.c_str()); + generic_iface_loop(collector); + id = collector.id; + return collector.found; +} + uint32_t Tins::Utils::crc32(uint8_t* data, uint32_t data_size) { uint32_t i, crc = 0; static uint32_t crc_table[] = {