Fixed mishandling of public/private IP addresses

This commit is contained in:
2024-11-17 07:18:03 +00:00
parent 0f259307df
commit db4542dd7a
9 changed files with 61 additions and 38 deletions

View File

@ -21,12 +21,12 @@
using std::string;
// Networking
char g_szPublicBindIP[16] = "0.0.0.0";
char g_szPublicIP[16] = "0";
string g_szPublicBindIP = "0.0.0.0";
string g_szPublicIP;
WORD mother_port = 50080;
char g_szInternalBindIP[16] = "0.0.0.0";
char g_szInternalIP[16] = "0";
string g_szInternalBindIP = "0.0.0.0";
string g_szInternalIP;
WORD p2p_port = 50900;
char db_addr[ADDRESS_MAX_LEN + 1];
@ -263,16 +263,16 @@ bool GetIPInfo()
sai->sin_addr.s_addr == 16777343) // ignore if address is 127.0.0.1
continue;
if (IsPrivateIP(sai->sin_addr) && g_szInternalIP[0] == '0')
if (IsPrivateIP(sai->sin_addr) && g_szInternalIP.empty())
{
char * ip = inet_ntoa(sai->sin_addr);
strlcpy(g_szInternalIP, ip, sizeof(g_szInternalIP));
g_szInternalIP = string(ip);
SPDLOG_WARN("Internal IP automatically configured: {} interface {}", ip, ifap->ifa_name);
}
else if (g_szPublicIP[0] == '0')
else if (g_szPublicIP.empty())
{
char * ip = inet_ntoa(sai->sin_addr);
strlcpy(g_szPublicIP, ip, sizeof(g_szPublicIP));
g_szPublicIP = string(ip);
SPDLOG_WARN("Public IP automatically configured: {} interface {}", ip, ifap->ifa_name);
}
}
@ -355,6 +355,10 @@ void config_init(const string& st_localeServiceName)
{
parse_token(buf, token_string, value_string);
// If the configuration string is empty, ignore this line
if (strcmp(value_string, "") == 0)
continue;
TOKEN("BLOCK_LOGIN")
{
g_stBlockDate = value_string;
@ -662,6 +666,10 @@ void config_init(const string& st_localeServiceName)
{
parse_token(buf, token_string, value_string);
// If the configuration string is empty, ignore this line
if (strcmp(value_string, "") == 0)
continue;
TOKEN("empire_whisper")
{
bool b_value = 0;
@ -699,13 +707,14 @@ void config_init(const string& st_localeServiceName)
TOKEN("public_ip")
{
strlcpy(g_szPublicIP, value_string, sizeof(g_szPublicIP));
g_szPublicIP = string(value_string);
SPDLOG_INFO("Setting public IP address to '{}'...", g_szPublicIP);
continue;
}
TOKEN("public_bind_ip")
{
strlcpy(g_szPublicBindIP, value_string, sizeof(g_szPublicBindIP));
g_szPublicBindIP = string(value_string);
continue;
}
@ -717,13 +726,14 @@ void config_init(const string& st_localeServiceName)
TOKEN("internal_ip")
{
strlcpy(g_szInternalIP, value_string, sizeof(g_szInternalIP));
g_szInternalIP = string(value_string);
SPDLOG_INFO("Setting internal IP address to '{}'...", g_szInternalIP);
continue;
}
TOKEN("internal_bind_ip")
{
strlcpy(g_szInternalBindIP, value_string, sizeof(g_szInternalBindIP));
g_szInternalBindIP = string(value_string);
continue;
}

View File

@ -9,12 +9,12 @@ enum
void config_init(const std::string& st_localeServiceName); // default "" is CONFIG
extern char g_szPublicBindIP[16];
extern char g_szPublicIP[16];
extern std::string g_szPublicBindIP;
extern std::string g_szPublicIP;
extern WORD mother_port;
extern char g_szInternalBindIP[16];
extern char g_szInternalIP[16];
extern std::string g_szInternalBindIP;
extern std::string g_szInternalIP;
extern WORD p2p_port;
extern char db_addr[ADDRESS_MAX_LEN + 1];

View File

@ -158,7 +158,7 @@ void CLIENT_DESC::SetPhase(int iPhase)
TPacketGDBoot p;
p.dwItemIDRange[0] = 0;
p.dwItemIDRange[1] = 0;
memcpy(p.szIP, g_szPublicIP, 16);
memcpy(p.szIP, g_szPublicIP.c_str(), 16);
DBPacket(HEADER_GD_BOOT, 0, &p, sizeof(p));
}
}
@ -168,7 +168,8 @@ void CLIENT_DESC::SetPhase(int iPhase)
TPacketGDSetup p;
memset(&p, 0, sizeof(p));
strlcpy(p.szPublicIP, g_szPublicIP, sizeof(p.szPublicIP));
strlcpy(p.szPublicIP, g_szPublicIP.c_str(), sizeof(p.szPublicIP));
strlcpy(p.szInternalIP, g_szInternalIP.c_str(), sizeof(p.szInternalIP));
if (!g_bAuthServer)
{

View File

@ -1304,13 +1304,13 @@ void CInputDB::P2P(const char * c_pData)
extern event_base* ev_base;
extern evdns_base* dns_base;
TPacketDGP2P * p = (TPacketDGP2P *) c_pData;
auto * p = (TPacketDGP2P *) c_pData;
P2P_MANAGER& mgr = P2P_MANAGER::instance();
if (false == DESC_MANAGER::instance().IsP2PDescExist(p->szHost, p->wPort))
if (!DESC_MANAGER::instance().IsP2PDescExist(p->szHost, p->wPort))
{
LPCLIENT_DESC pkDesc = NULL;
LPCLIENT_DESC pkDesc = nullptr;
SPDLOG_DEBUG("InputDB:P2P {}:{}", p->szHost, p->wPort);
pkDesc = DESC_MANAGER::instance().CreateConnectionDesc(ev_base, dns_base, p->szHost, p->wPort, PHASE_P2P, false);
mgr.RegisterConnector(pkDesc);

View File

@ -513,9 +513,9 @@ int start(int argc, char **argv)
switch (ch)
{
case 'I': // IP
strlcpy(g_szPublicIP, optarg, sizeof(g_szPublicIP));
g_szPublicIP = std::string(optarg);
printf("IP %s\n", g_szPublicIP);
printf("IP %s\n", g_szPublicIP.c_str());
break;
@ -596,11 +596,11 @@ int start(int argc, char **argv)
// Initialize the network stack
// Check if the public and internal IP addresses were configured
if (g_szInternalIP[0] == '0') {
SPDLOG_CRITICAL("Public IP address could not be automatically detected. Manually set the IP and try again.");
if (g_szInternalIP.empty()) {
SPDLOG_CRITICAL("Internal IP address could not be automatically detected. Manually set the IP and try again.");
exit(EXIT_FAILURE);
}
if (g_szPublicIP[0] == '0') {
if (g_szPublicIP.empty()) {
SPDLOG_CRITICAL("Public IP address could not be automatically detected. Manually set the IP and try again.");
exit(EXIT_FAILURE);
}
@ -622,7 +622,7 @@ int start(int argc, char **argv)
// Main TCP listener
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(g_szPublicBindIP);
sin.sin_addr.s_addr = inet_addr(g_szPublicBindIP.c_str());
sin.sin_port = htons(mother_port);
tcp_listener = evconnlistener_new_bind(
@ -640,7 +640,7 @@ int start(int argc, char **argv)
// Game P2P listener
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(g_szInternalBindIP);
sin.sin_addr.s_addr = inet_addr(g_szInternalBindIP.c_str());
sin.sin_port = htons(p2p_port);
p2p_listener = evconnlistener_new_bind(