summaryrefslogtreecommitdiff
path: root/drivers
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2016-12-01 04:23:57 +0100
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2016-12-09 18:24:59 +0100
commit9200da58e4c2498c833d9f2505600c7049e80940 (patch)
tree0e80aff3e0c031a6177521d12283e17a1b728444 /drivers
parent95bdd977686005d3d813eb09aca625384f1774c1 (diff)
Automatically map IPv4 address to IPv6 when needed
Diffstat (limited to 'drivers')
-rw-r--r--drivers/unix/packet_peer_udp_posix.cpp2
-rw-r--r--drivers/unix/socket_helpers.h20
-rw-r--r--drivers/unix/stream_peer_tcp_posix.cpp4
3 files changed, 19 insertions, 7 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp
index 52f2c07134..b1049582c4 100644
--- a/drivers/unix/packet_peer_udp_posix.cpp
+++ b/drivers/unix/packet_peer_udp_posix.cpp
@@ -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;
diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h
index 84b1ca5740..a860c2f80e 100644
--- a/drivers/unix/socket_helpers.h
+++ b/drivers/unix/socket_helpers.h
@@ -16,18 +16,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
diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp
index 95e56aa40b..8aae1e77a8 100644
--- a/drivers/unix/stream_peer_tcp_posix.cpp
+++ b/drivers/unix/stream_peer_tcp_posix.cpp
@@ -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) {