Automatically map IPv4 address to IPv6 when needed
(cherry picked from commit 9200da58e4
)
This commit is contained in:
parent
a46a643f90
commit
5e79ac72b7
5 changed files with 22 additions and 10 deletions
|
@ -99,7 +99,7 @@ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
|
|||
int sock = _get_socket();
|
||||
ERR_FAIL_COND_V( sock == -1, FAILED );
|
||||
struct sockaddr_storage addr;
|
||||
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port);
|
||||
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
|
||||
|
||||
errno = 0;
|
||||
int err;
|
||||
|
|
|
@ -12,18 +12,30 @@
|
|||
|
||||
// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this
|
||||
|
||||
static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port) {
|
||||
static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_ip, int p_port, IP_Address::AddrType p_sock_type = IP_Address::TYPE_ANY) {
|
||||
|
||||
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
||||
if (p_ip.type == IP_Address::TYPE_IPV6) {
|
||||
|
||||
// Dual stack (ANY) or matching ip type is required
|
||||
ERR_FAIL_COND_V(p_sock_type != IP_Address::TYPE_ANY && p_sock_type != p_ip.type,0);
|
||||
|
||||
// IPv6 socket
|
||||
if (p_sock_type == IP_Address::TYPE_IPV6 || p_sock_type == IP_Address::TYPE_ANY) {
|
||||
|
||||
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
|
||||
addr6->sin6_family = AF_INET6;
|
||||
addr6->sin6_port = htons(p_port);
|
||||
copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16);
|
||||
if(p_ip.type == IP_Address::TYPE_IPV4) {
|
||||
// Remapping needed
|
||||
uint16_t base[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0xffff, p_ip.field16[0], p_ip.field16[1]};
|
||||
copymem(&addr6->sin6_addr.s6_addr, base, 16);
|
||||
|
||||
} else {
|
||||
copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16);
|
||||
}
|
||||
return sizeof(sockaddr_in6);
|
||||
|
||||
} else {
|
||||
} else { // IPv4 socket
|
||||
|
||||
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
|
||||
addr4->sin_family = AF_INET; // host byte order
|
||||
|
|
|
@ -98,7 +98,7 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
|
|||
};
|
||||
|
||||
struct sockaddr_storage their_addr;
|
||||
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port);
|
||||
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, ip_type);
|
||||
|
||||
if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1) {
|
||||
|
||||
|
@ -159,7 +159,7 @@ Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
|
|||
#endif
|
||||
|
||||
struct sockaddr_storage their_addr;
|
||||
size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port);
|
||||
size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, ip_type);
|
||||
|
||||
errno = 0;
|
||||
if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1 && errno != EINPROGRESS) {
|
||||
|
|
|
@ -74,7 +74,7 @@ Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer,int p_buffer_size
|
|||
int sock = _get_socket();
|
||||
ERR_FAIL_COND_V( sock == -1, FAILED );
|
||||
struct sockaddr_storage addr;
|
||||
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port);
|
||||
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
|
||||
|
||||
_set_blocking(true);
|
||||
|
||||
|
|
|
@ -88,7 +88,7 @@ Error StreamPeerWinsock::_poll_connection(bool p_block) const {
|
|||
};
|
||||
|
||||
struct sockaddr_storage their_addr;
|
||||
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port);
|
||||
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, ip_type);
|
||||
|
||||
if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == SOCKET_ERROR) {
|
||||
|
||||
|
@ -318,7 +318,7 @@ Error StreamPeerWinsock::connect(const IP_Address& p_host, uint16_t p_port) {
|
|||
};
|
||||
|
||||
struct sockaddr_storage their_addr;
|
||||
size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port);
|
||||
size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port, ip_type);
|
||||
|
||||
if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == SOCKET_ERROR) {
|
||||
|
||||
|
|
Loading…
Reference in a new issue