IfaceMap add look up index by address, and loopback by index

This commit is contained in:
Michael Davidsaver
2025-02-15 18:52:30 -08:00
parent 80c63888ed
commit 25f5f1dcee
2 changed files with 32 additions and 0 deletions
+28
View File
@@ -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);
+4
View File
@@ -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