diff --git a/mooncake-transfer-engine/include/common.h b/mooncake-transfer-engine/include/common.h index 7d19ada5..e83d473a 100644 --- a/mooncake-transfer-engine/include/common.h +++ b/mooncake-transfer-engine/include/common.h @@ -15,6 +15,7 @@ #ifndef COMMON_H #define COMMON_H +#include #include #include #include @@ -22,13 +23,13 @@ #include #include +#include #include #include -#include -#include -#include #include +#include #include +#include #include "error.h" @@ -106,9 +107,12 @@ static inline std::string getCurrentDateTime() { auto now = std::chrono::system_clock::now(); auto time_t_now = std::chrono::system_clock::to_time_t(now); auto local_time = *std::localtime(&time_t_now); - auto micros = std::chrono::duration_cast(now.time_since_epoch()) % 1000000; + auto micros = std::chrono::duration_cast( + now.time_since_epoch()) % + 1000000; std::ostringstream oss; - oss << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S") << "." << std::setw(6) << std::setfill('0') << micros.count(); + oss << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S") << "." + << std::setw(6) << std::setfill('0') << micros.count(); return oss.str(); } @@ -117,6 +121,29 @@ uint16_t getDefaultHandshakePort(); static inline std::pair parseHostNameWithPort( const std::string &server_name) { uint16_t port = getDefaultHandshakePort(); + + // IPv6 check + in6_addr addr; + if (inet_pton(AF_INET6, server_name.c_str(), &addr) == 1) + return {server_name, port}; + + size_t start_pos = 0; + size_t end_pos = server_name.find_last_of(']'); + size_t port_pos = server_name.find_last_of(':'); + if (server_name.front() == '[' && end_pos != std::string::npos && + port_pos > end_pos) { + auto ip = server_name.substr(start_pos + 1, end_pos - start_pos - 1); + std::string port_str = server_name.substr(port_pos + 1); + int val = std::atoi(port_str.c_str()); + if (val <= 0 || val > 65535) { + LOG(WARNING) << "Illegal port number in " << server_name + << ". Use default port " << port << " instead"; + } else { + port = static_cast(val); + } + return std::make_pair(port_str, port); + } + auto pos = server_name.find(':'); if (pos == server_name.npos) return std::make_pair(server_name, port); auto trimmed_server_name = server_name.substr(0, pos);