Merge pull request #8716 from ChillerDragon/pr_hide_url_for_06

Hide address url for 0.6 ips (Closed #8712)
This commit is contained in:
Dennis Felsing 2024-08-17 17:42:30 +00:00 committed by GitHub
commit b16bc401a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 34 additions and 57 deletions

View file

@ -1130,22 +1130,6 @@ bool net_addr_str(const NETADDR *addr, char *string, int max_length, int add_por
return true; return true;
} }
void net_addr_url_str(const NETADDR *addr, char *string, int max_length, int add_port)
{
char ipaddr[512];
if(!net_addr_str(addr, ipaddr, sizeof(ipaddr), add_port))
{
str_copy(string, ipaddr, max_length);
return;
}
str_format(
string,
max_length,
"tw-%s+udp://%s",
addr->type & NETTYPE_TW7 ? "0.7" : "0.6",
ipaddr);
}
static int priv_net_extract(const char *hostname, char *host, int max_host, int *port) static int priv_net_extract(const char *hostname, char *host, int max_host, int *port)
{ {
int i; int i;

View file

@ -818,23 +818,6 @@ int net_addr_comp_noport(const NETADDR *a, const NETADDR *b);
*/ */
bool net_addr_str(const NETADDR *addr, char *string, int max_length, int add_port); bool net_addr_str(const NETADDR *addr, char *string, int max_length, int add_port);
/**
* Turns a network address into a url string.
* Examples:
* tw-0.6+udp://127.0.0.1:8303
* tw-0.7+udp://127.0.0.1
*
* @ingroup Network-General
*
* @param addr Address to turn into a string.
* @param string Buffer to fill with the url string.
* @param max_length Maximum size of the url string.
* @param add_port add port to url string or not
*
* @remark The string will always be zero terminated
*/
void net_addr_url_str(const NETADDR *addr, char *string, int max_length, int add_port);
/** /**
* Turns url string into a network address struct. * Turns url string into a network address struct.
* The url format is tw-0.6+udp://{ipaddr}[:{port}] * The url format is tw-0.6+udp://{ipaddr}[:{port}]

View file

@ -704,7 +704,22 @@ void ServerBrowserFormatAddresses(char *pBuffer, int BufferSize, NETADDR *pAddrs
{ {
return; return;
} }
net_addr_url_str(&pAddrs[i], pBuffer, BufferSize, true); char aIpAddr[512];
if(!net_addr_str(&pAddrs[i], aIpAddr, sizeof(aIpAddr), true))
{
str_copy(pBuffer, aIpAddr, BufferSize);
return;
}
if(pAddrs[i].type & NETTYPE_TW7)
{
str_format(
pBuffer,
BufferSize,
"tw-0.7+udp://%s",
aIpAddr);
return;
}
str_copy(pBuffer, aIpAddr, BufferSize);
int Length = str_length(pBuffer); int Length = str_length(pBuffer);
pBuffer += Length; pBuffer += Length;
BufferSize -= Length; BufferSize -= Length;

View file

@ -472,6 +472,7 @@ bool CServerBrowserHttp::Parse(json_value *pJson, std::vector<CServerInfo> *pvSe
CServerInfo SetInfo = ParsedInfo; CServerInfo SetInfo = ParsedInfo;
SetInfo.m_Location = ParsedLocation; SetInfo.m_Location = ParsedLocation;
SetInfo.m_NumAddresses = 0; SetInfo.m_NumAddresses = 0;
bool GotVersion6 = false;
for(unsigned int a = 0; a < Addresses.u.array.length; a++) for(unsigned int a = 0; a < Addresses.u.array.length; a++)
{ {
const json_value &Address = Addresses[a]; const json_value &Address = Addresses[a];
@ -479,6 +480,23 @@ bool CServerBrowserHttp::Parse(json_value *pJson, std::vector<CServerInfo> *pvSe
{ {
return true; return true;
} }
if(str_startswith(Addresses[a], "tw-0.6+udp://"))
{
GotVersion6 = true;
break;
}
}
for(unsigned int a = 0; a < Addresses.u.array.length; a++)
{
const json_value &Address = Addresses[a];
if(Address.type != json_string)
{
return true;
}
if(GotVersion6 && str_startswith(Addresses[a], "tw-0.7+udp://"))
{
continue;
}
NETADDR ParsedAddr; NETADDR ParsedAddr;
if(ServerbrowserParseUrl(&ParsedAddr, Addresses[a])) if(ServerbrowserParseUrl(&ParsedAddr, Addresses[a]))
{ {

View file

@ -24,10 +24,6 @@ TEST(NetAddr, FromUrlStringValid)
net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false); net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf1, "127.0.0.1:0"); EXPECT_STREQ(aBuf1, "127.0.0.1:0");
EXPECT_STREQ(aBuf2, "127.0.0.1"); EXPECT_STREQ(aBuf2, "127.0.0.1");
net_addr_url_str(&Addr, aBuf1, sizeof(aBuf1), true);
net_addr_url_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf1, "tw-0.7+udp://127.0.0.1:0");
EXPECT_STREQ(aBuf2, "tw-0.7+udp://127.0.0.1");
EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://127.0.0.1", nullptr, 0), 0); EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://127.0.0.1", nullptr, 0), 0);
net_addr_str(&Addr, aBuf1, sizeof(aBuf1), true); net_addr_str(&Addr, aBuf1, sizeof(aBuf1), true);
@ -40,20 +36,12 @@ TEST(NetAddr, FromUrlStringValid)
net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false); net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf1, "127.0.0.1:0"); EXPECT_STREQ(aBuf1, "127.0.0.1:0");
EXPECT_STREQ(aBuf2, "127.0.0.1"); EXPECT_STREQ(aBuf2, "127.0.0.1");
net_addr_url_str(&Addr, aBuf1, sizeof(aBuf1), true);
net_addr_url_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf1, "tw-0.6+udp://127.0.0.1:0");
EXPECT_STREQ(aBuf2, "tw-0.6+udp://127.0.0.1");
EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://[0123:4567:89ab:cdef:1:2:3:4]:5678", nullptr, 0), 0); EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://[0123:4567:89ab:cdef:1:2:3:4]:5678", nullptr, 0), 0);
net_addr_str(&Addr, aBuf1, sizeof(aBuf1), true); net_addr_str(&Addr, aBuf1, sizeof(aBuf1), true);
net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false); net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf1, "[123:4567:89ab:cdef:1:2:3:4]:5678"); EXPECT_STREQ(aBuf1, "[123:4567:89ab:cdef:1:2:3:4]:5678");
EXPECT_STREQ(aBuf2, "[123:4567:89ab:cdef:1:2:3:4]"); EXPECT_STREQ(aBuf2, "[123:4567:89ab:cdef:1:2:3:4]");
net_addr_url_str(&Addr, aBuf1, sizeof(aBuf1), true);
net_addr_url_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf1, "tw-0.6+udp://[123:4567:89ab:cdef:1:2:3:4]:5678");
EXPECT_STREQ(aBuf2, "tw-0.6+udp://[123:4567:89ab:cdef:1:2:3:4]");
char aHost[128]; char aHost[128];
EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://ger10.ddnet.org:5678", aHost, sizeof(aHost)), -1); EXPECT_EQ(net_addr_from_url(&Addr, "tw-0.6+udp://ger10.ddnet.org:5678", aHost, sizeof(aHost)), -1);
@ -156,14 +144,3 @@ TEST(NetAddr, StrInvalid)
net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false); net_addr_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf2, "unknown type 0"); EXPECT_STREQ(aBuf2, "unknown type 0");
} }
TEST(NetAddr, UrlStrInvalid)
{
NETADDR Addr = {0};
char aBuf1[NETADDR_MAXSTRSIZE];
char aBuf2[NETADDR_MAXSTRSIZE];
net_addr_url_str(&Addr, aBuf1, sizeof(aBuf1), true);
EXPECT_STREQ(aBuf1, "unknown type 0");
net_addr_url_str(&Addr, aBuf2, sizeof(aBuf2), false);
EXPECT_STREQ(aBuf2, "unknown type 0");
}