do not export 0.0.0.0 in ipv6 by default

Signed-off-by: stesen <stesen.ma@huawei.com>
Change-Id: Icda15bcd8bf6e3c430e8054ce90b6cc05d235bfb
This commit is contained in:
stesen 2022-03-19 12:11:56 +08:00
parent 36a917957c
commit a1b60eb9ad
5 changed files with 42 additions and 15 deletions

View File

@ -480,7 +480,7 @@ namespace Base {
if (memcpy_s(bufString, sizeof(bufString), connectKey, sizeof(bufString))) {
return ERR_BUF_COPY;
}
char *p = strchr(bufString, ':');
char *p = strrchr(bufString, ':');
if (!p) {
return ERR_PARM_FORMAT;
}

View File

@ -58,11 +58,23 @@ bool HdcChannelBase::SetChannelTCPString(const string &addrString)
if (addrString.find(":") == string::npos) {
break;
}
string host = addrString.substr(0, addrString.find(":"));
string port = addrString.substr(addrString.find(":") + 1);
std::size_t found = addrString.find_last_of(":");
if (found == string::npos) {
break;
}
string host = addrString.substr(0, found);
string port = addrString.substr(found + 1);
channelPort = std::atoi(port.c_str());
sockaddr_in addr;
if (!channelPort || uv_ip4_addr(host.c_str(), channelPort, &addr) != 0) {
sockaddr_in addrv4;
sockaddr_in6 addrv6;
if (!channelPort) {
break;
}
if (uv_ip6_addr(host.c_str(), channelPort, &addrv6) != 0 &&
uv_ip4_addr(host.c_str(), channelPort, &addrv4) != 0) {
break;
}
channelHost = host;

View File

@ -22,7 +22,8 @@ constexpr uint8_t SIZE_THREAD_POOL = 16;
constexpr uint8_t GLOBAL_TIMEOUT = 30;
constexpr uint16_t DEFAULT_PORT = 8710;
constexpr bool ENABLE_IO_CHECKSUM = false;
const string DEFAULT_SERVER_ADDR = "127.0.0.1:8710";
const string IPV4_MAPPING_PREFIX = "::ffff:";
const string DEFAULT_SERVER_ADDR = "::ffff:127.0.0.1:8710";
// ################################ macro define ###################################
constexpr uint8_t MINOR_TIMEOUT = 5;

View File

@ -155,7 +155,10 @@ int HdcClient::ExecuteCommand(const string &commandIn)
{
char ip[BUF_SIZE_TINY] = "";
uint16_t port = 0;
if (Base::ConnectKey2IPPort(channelHostPort.c_str(), ip, &port) < 0) {
int ret = Base::ConnectKey2IPPort(channelHostPort.c_str(), ip, &port);
if (ret < 0) {
WRITE_LOG(LOG_FATAL, "ConnectKey2IPPort %s failed with %d",
channelHostPort.c_str(), ret);
return -1;
}
command = commandIn;
@ -184,8 +187,8 @@ int HdcClient::ConnectServerForClient(const char *ip, uint16_t port)
return ERR_SOCKET_FAIL;
}
WRITE_LOG(LOG_DEBUG, "Try to connect %s:%d", ip, port);
struct sockaddr_in dest;
uv_ip4_addr(ip, port, &dest);
struct sockaddr_in6 dest;
uv_ip6_addr(ip, port, &dest);
uv_connect_t *conn = new(std::nothrow) uv_connect_t();
if (conn == nullptr) {
WRITE_LOG(LOG_FATAL, "ConnectServerForClient new conn failed");

View File

@ -196,7 +196,7 @@ bool ParseServerListenString(string &serverListenString, char *optarg)
Base::PrintMessage("strcpy_s error %d", errno);
return false;
}
char *p = strchr(buf, ':');
char *p = strrchr(buf, ':');
if (!p) { // Only port
if (strlen(buf) > 5) {
Base::PrintMessage("The port-string's length must < 5");
@ -207,17 +207,28 @@ bool ParseServerListenString(string &serverListenString, char *optarg)
Base::PrintMessage("Port range incorrect");
return false;
}
(void)snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "127.0.0.1:%d", port);
(void)snprintf_s(buf, sizeof(buf), sizeof(buf) - 1, "::ffff:127.0.0.1:%d", port);
serverListenString = buf;
} else {
*p = '\0';
int port = atoi(p + 1);
sockaddr_in addr;
if ((port <= 0 || port > MAX_IP_PORT) || uv_ip4_addr(buf, port, &addr) < 0) {
Base::PrintMessage("-s content incorrect.");
sockaddr_in addrv4;
sockaddr_in6 addrv6;
if ((port <= 0 || port > MAX_IP_PORT)) {
Base::PrintMessage("-s content port incorrect.");
return false;
}
if (uv_ip4_addr(buf, port, &addrv4) == 0) {
serverListenString = IPV4_MAPPING_PREFIX;
serverListenString += optarg;
} else if (uv_ip6_addr(buf, port, &addrv6) == 0) {
serverListenString = optarg;
} else {
Base::PrintMessage("-s content IP incorrect.");
return false;
}
serverListenString = optarg;
}
return true;
}