From 8575bd4f960a56eeca361b29f1fda38d3eb0757d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Fri, 6 Nov 2015 01:49:13 -0500
Subject: [PATCH] dht: allow to specify address family for getPublicAddress

---
 include/opendht/dht.h       | 2 +-
 include/opendht/dhtrunner.h | 8 ++++----
 src/dht.cpp                 | 6 +++---
 3 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/include/opendht/dht.h b/include/opendht/dht.h
index 5b1c06dc..77af07f9 100644
--- a/include/opendht/dht.h
+++ b/include/opendht/dht.h
@@ -369,7 +369,7 @@ public:
     /* This must be provided by the user. */
     static bool isBlacklisted(const sockaddr*, socklen_t) { return false; }
 
-    std::vector<Address> getPublicAddress();
+    std::vector<Address> getPublicAddress(sa_family_t family = 0);
 
 protected:
     LogMethod DHT_DEBUG = NOLOG;
diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h
index f24625cd..22b08fee 100644
--- a/include/opendht/dhtrunner.h
+++ b/include/opendht/dhtrunner.h
@@ -293,14 +293,14 @@ public:
         std::lock_guard<std::mutex> lck(dht_mtx);
         return dht_->getSearchesLog(af);
     }
-    std::vector<Address> getPublicAddress()
+    std::vector<Address> getPublicAddress(sa_family_t af = 0)
     {
         std::lock_guard<std::mutex> lck(dht_mtx);
-        return dht_->getPublicAddress();
+        return dht_->getPublicAddress(af);
     }
-    std::vector<std::string> getPublicAddressStr()
+    std::vector<std::string> getPublicAddressStr(sa_family_t af = 0)
     {
-        auto addrs = getPublicAddress();
+        auto addrs = getPublicAddress(af);
         std::vector<std::string> ret(addrs.size());
         std::transform(addrs.begin(), addrs.end(), ret.begin(), dht::printAddr);
         return ret;
diff --git a/src/dht.cpp b/src/dht.cpp
index f0593836..17276b3e 100644
--- a/src/dht.cpp
+++ b/src/dht.cpp
@@ -573,15 +573,15 @@ Dht::isNodeBlacklisted(const sockaddr *sa, socklen_t salen) const
 }
 
 std::vector<Address>
-Dht::getPublicAddress()
+Dht::getPublicAddress(sa_family_t family)
 {
     std::sort(reported_addr.begin(), reported_addr.end(), [](const ReportedAddr& a, const ReportedAddr& b) {
         return a.first < b.first;
     });
     std::vector<Address> ret;
-    ret.reserve(reported_addr.size());
     for (const auto& addr : reported_addr)
-        ret.emplace_back(addr.second);
+        if (!family || family == addr.second.first.ss_family)
+            ret.emplace_back(addr.second);
     return ret;
 }
 
-- 
GitLab