diff --git a/src/evhelper.cpp b/src/evhelper.cpp index 3eb3b2b..3a42477 100644 --- a/src/evhelper.cpp +++ b/src/evhelper.cpp @@ -837,6 +837,21 @@ uint64_t IfaceMap::index_of(const std::string& name) return ret; } +uint64_t IfaceMap::index_of(const SockAddr &addr) +{ + Guard G(lock); + + uint64_t ret = 0u; + try_cache(*this, [&ret, this, addr]() { + auto it = byAddr.find(addr); + bool hit = it!=byAddr.end() && !it->second.second; + if(hit) + ret = it->second.first->index; + return hit; + }); + return ret; +} + bool IfaceMap::is_iface(const SockAddr& addr) { Guard G(lock); @@ -847,6 +862,19 @@ bool IfaceMap::is_iface(const SockAddr& addr) }); } +bool IfaceMap::is_lo(uint64_t index) +{ + bool is_lo = false; + (void)try_cache(*this, [this, &is_lo, index]() { + auto ifit(byIndex.find(index)); + if(ifit!=byIndex.end()) { // hit + is_lo = ifit->second.isLO; + } + return false; + }); + return is_lo; +} + bool IfaceMap::is_broadcast(const SockAddr& addr) { Guard G(lock); diff --git a/src/evhelper.h b/src/evhelper.h index ce45cd6..6a9904a 100644 --- a/src/evhelper.h +++ b/src/evhelper.h @@ -316,8 +316,12 @@ struct PVXS_API IfaceMap { std::string name_of(const SockAddr& addr); // returns 0 if not found uint64_t index_of(const std::string& name); + // lookup interface index by interface address (not broadcast addr) + uint64_t index_of(const SockAddr& addr); // is this a valid interface or broadcast address? bool is_iface(const SockAddr& addr); + // is this index the/a loopback interface? + bool is_lo(uint64_t index); // is this a valid interface or broadcast address? bool is_broadcast(const SockAddr& addr); // look up interface address. useful for IPV4. returns AF_UNSPEC if not found