(Network) Move the addr_6to4 function to a proper place (#14100)

This commit is contained in:
Cthulhu-throwaway 2022-06-25 04:18:32 -03:00 committed by GitHub
parent 865993ffa7
commit a7dc57f68f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 83 deletions

View File

@ -299,6 +299,8 @@ int getaddrinfo_retro(const char *node, const char *service,
void freeaddrinfo_retro(struct addrinfo *res);
bool addr_6to4(struct sockaddr_storage *addr);
/**
* network_init:
*

View File

@ -461,6 +461,43 @@ int inet_ptrton(int af, const char *src, void *dst)
#endif
}
bool addr_6to4(struct sockaddr_storage *addr)
{
#ifdef HAVE_INET6
/* ::ffff:a.b.c.d */
static const uint16_t preffix[] = {0,0,0,0,0,0xffff};
uint32_t address;
uint16_t port;
struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)addr;
struct sockaddr_in *addr4 = (struct sockaddr_in*)addr;
switch (addr->ss_family)
{
case AF_INET:
/* No need to convert. */
return true;
case AF_INET6:
/* Is the address provided an IPv4? */
if (!memcmp(&addr6->sin6_addr, preffix, sizeof(preffix)))
break;
default:
/* We don't know how to handle this. */
return false;
}
memcpy(&address, ((uint8_t*)&addr6->sin6_addr) + sizeof(preffix),
sizeof(address));
port = addr6->sin6_port;
memset(addr, 0, sizeof(*addr));
addr4->sin_family = AF_INET;
addr4->sin_port = port;
memcpy(&addr4->sin_addr, &address, sizeof(addr4->sin_addr));
#endif
return true;
}
struct in_addr6_compat
{
unsigned char ip_addr[16];

View File

@ -65,43 +65,6 @@ static bool translate_addr(struct sockaddr_in *addr,
return true;
}
static bool addr_6to4(struct sockaddr_storage *addr)
{
#if defined(HAVE_INET6)
/* ::ffff:a.b.c.d */
static const uint16_t preffix[] = {0,0,0,0,0,0xffff};
uint32_t address;
uint16_t port;
struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)addr;
struct sockaddr_in *addr4 = (struct sockaddr_in*)addr;
switch (addr->ss_family)
{
case AF_INET:
/* No need to convert. */
return true;
case AF_INET6:
/* Is the address provided an IPv4? */
if (!memcmp(&addr6->sin6_addr, preffix, sizeof(preffix)))
break;
default:
/* We don't know how to handle this. */
return false;
}
memcpy(&address, ((uint8_t*)&addr6->sin6_addr) + sizeof(preffix),
sizeof(address));
port = addr6->sin6_port;
memset(addr, 0, sizeof(*addr));
addr4->sin_family = AF_INET;
addr4->sin_port = port;
memcpy(&addr4->sin_addr, &address, sizeof(addr4->sin_addr));
#endif
return true;
}
bool natt_init(struct natt_discovery *discovery)
{
static const char msearch[] =

View File

@ -259,7 +259,6 @@ net_driver_state_t *networking_state_get_ptr(void);
bool netplay_decode_hostname(const char *hostname,
char *address, unsigned *port, char *session, size_t len);
bool netplay_is_lan_address(struct sockaddr_in *addr);
bool netplay_6to4(struct sockaddr_storage *addr);
int netplay_rooms_parse(const char *buf);
int netplay_rooms_get_count(void);

View File

@ -330,16 +330,8 @@ static bool netplay_lan_ad_client_response(void)
continue;
/* And that we know how to handle it */
#ifdef HAVE_INET6
if (their_addr.ss_family == AF_INET6)
{
/* Check for IPv4 tunneling */
if (!netplay_6to4(&their_addr))
continue;
}
else if (their_addr.ss_family != AF_INET)
if (!addr_6to4(&their_addr))
continue;
#endif
if (!netplay_is_lan_address((struct sockaddr_in*)&their_addr))
continue;
@ -545,16 +537,8 @@ static bool netplay_lan_ad_server(netplay_t *netplay)
return true;
}
#ifdef HAVE_INET6
if (their_addr.ss_family == AF_INET6)
{
/* Check for IPv4 tunneling */
if (!netplay_6to4(&their_addr))
return true;
}
else if (their_addr.ss_family != AF_INET)
if (!addr_6to4(&their_addr))
return true;
#endif
if (!netplay_is_lan_address((struct sockaddr_in*)&their_addr))
return true;
@ -8866,33 +8850,6 @@ bool netplay_is_lan_address(struct sockaddr_in *addr)
return false;
}
bool netplay_6to4(struct sockaddr_storage *addr)
{
#ifdef HAVE_INET6
/* ::ffff:a.b.c.d */
static const uint16_t preffix[] = {0,0,0,0,0,0xffff};
uint32_t address;
uint16_t port;
struct sockaddr_in6 *addr6 = (struct sockaddr_in6*)addr;
struct sockaddr_in *addr4 = (struct sockaddr_in*)addr;
/* Is the address provided an IPv4? */
if (memcmp(&addr6->sin6_addr, preffix, sizeof(preffix)))
return false;
memcpy(&address, ((uint8_t*)&addr6->sin6_addr) + sizeof(preffix),
sizeof(address));
port = addr6->sin6_port;
memset(addr, 0, sizeof(*addr));
addr4->sin_family = AF_INET;
addr4->sin_port = port;
memcpy(&addr4->sin_addr, &address, sizeof(addr4->sin_addr));
#endif
return true;
}
/* Netplay Widgets */
#ifdef HAVE_GFX_WIDGETS