| 1 | #include <Common/isLocalAddress.h> |
|---|---|
| 2 | |
| 3 | #include <cstring> |
| 4 | #include <Core/Types.h> |
| 5 | #include <Poco/Util/Application.h> |
| 6 | #include <Poco/Net/NetworkInterface.h> |
| 7 | #include <Poco/Net/SocketAddress.h> |
| 8 | |
| 9 | |
| 10 | namespace DB |
| 11 | { |
| 12 | |
| 13 | bool isLocalAddress(const Poco::Net::IPAddress & address) |
| 14 | { |
| 15 | static auto interfaces = Poco::Net::NetworkInterface::list(); |
| 16 | |
| 17 | return interfaces.end() != std::find_if(interfaces.begin(), interfaces.end(), |
| 18 | [&] (const Poco::Net::NetworkInterface & interface) |
| 19 | { |
| 20 | /** Compare the addresses without taking into account `scope`. |
| 21 | * Theoretically, this may not be correct - depends on `route` setting |
| 22 | * - through which interface we will actually access the specified address. |
| 23 | */ |
| 24 | return interface.address().length() == address.length() |
| 25 | && 0 == memcmp(interface.address().addr(), address.addr(), address.length()); |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | bool isLocalAddress(const Poco::Net::SocketAddress & address, UInt16 clickhouse_port) |
| 30 | { |
| 31 | return clickhouse_port == address.port() && isLocalAddress(address.host()); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | size_t getHostNameDifference(const std::string & local_hostname, const std::string & host) |
| 36 | { |
| 37 | size_t hostname_difference = 0; |
| 38 | for (size_t i = 0; i < std::min(local_hostname.length(), host.length()); ++i) |
| 39 | if (local_hostname[i] != host[i]) |
| 40 | ++hostname_difference; |
| 41 | return hostname_difference; |
| 42 | } |
| 43 | |
| 44 | } |
| 45 |