diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-01-28 14:48:45 +0000 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-01-28 14:57:47 +0000 |
commit | 9bd9906e8f60439328ed9e125b2c65c5ce45bc1f (patch) | |
tree | dcc9fb06f8c8dfd033b0588969989a36c81f9875 /src/common | |
parent | Merge pull request #1618 (diff) | |
download | monero-9bd9906e8f60439328ed9e125b2c65c5ce45bc1f.tar.xz |
Factor is_address_local code into a tools function
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/util.cpp | 37 | ||||
-rw-r--r-- | src/common/util.h | 2 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/common/util.cpp b/src/common/util.cpp index 6dec6af2a..bfcf86bc6 100644 --- a/src/common/util.cpp +++ b/src/common/util.cpp @@ -35,6 +35,7 @@ using namespace epee; #include "util.h" #include "cryptonote_config.h" +#include "net/http_client.h" // epee::net_utils::... #ifdef WIN32 #include <windows.h> @@ -44,6 +45,7 @@ using namespace epee; #include <sys/utsname.h> #endif #include <boost/filesystem.hpp> +#include <boost/asio.hpp> namespace tools @@ -531,4 +533,39 @@ std::string get_nix_version_display_string() boost::lock_guard<boost::mutex> lock(max_concurrency_lock); return max_concurrency; } + + bool is_local_address(const std::string &address) + { + // extract host + epee::net_utils::http::url_content u_c; + if (!epee::net_utils::parse_url(address, u_c)) + { + MWARNING("Failed to determine whether address '" << address << "' is local, assuming not"); + return false; + } + if (u_c.host.empty()) + { + MWARNING("Failed to determine whether address '" << address << "' is local, assuming not"); + return false; + } + + // resolve to IP + boost::asio::io_service io_service; + boost::asio::ip::tcp::resolver resolver(io_service); + boost::asio::ip::tcp::resolver::query query(u_c.host, ""); + boost::asio::ip::tcp::resolver::iterator i = resolver.resolve(query); + while (i != boost::asio::ip::tcp::resolver::iterator()) + { + const boost::asio::ip::tcp::endpoint &ep = *i; + if (ep.address().is_loopback()) + { + MDEBUG("Address '" << address << "' is local"); + return true; + } + ++i; + } + + MDEBUG("Address '" << address << "' is not local"); + return false; + } } diff --git a/src/common/util.h b/src/common/util.h index 4437d821f..c2ffc44ca 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -181,4 +181,6 @@ namespace tools void set_max_concurrency(unsigned n); unsigned get_max_concurrency(); + + bool is_local_address(const std::string &address); } |