Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions mooncake-transfer-engine/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
#ifndef COMMON_H
#define COMMON_H

#include <arpa/inet.h>
#include <glog/logging.h>
#include <numa.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <unistd.h>

#include <atomic>
#include <chrono>
#include <cstdint>
#include <ctime>
#include <thread>
#include <iostream>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>

#include "error.h"

Expand Down Expand Up @@ -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<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
auto micros = std::chrono::duration_cast<std::chrono::microseconds>(
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();
}

Expand All @@ -117,6 +121,29 @@ uint16_t getDefaultHandshakePort();
static inline std::pair<std::string, uint16_t> 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it do not support ipv6 + port, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We add code to support ipv6 + port

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<uint16_t>(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);
Expand Down
Loading