summaryrefslogtreecommitdiff
path: root/drivers/unix
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2016-10-22 07:23:07 +0200
committerRĂ©mi Verschelde <rverschelde@gmail.com>2016-10-26 08:20:31 +0200
commit80e911647c5df21c5b6a06876f1d48e21cd1f5fc (patch)
tree6fc3a52e19b24492bdcbf6e231ccb7b9fa536c42 /drivers/unix
parenta3131a6b5bf5357e5c70ba6fea4a0963f4b341b4 (diff)
Pass correct address size (ipv4,ipv6) to socket connect, bind, sendto
The address size passed to network system calls now reflects the the actual IP type (v4 or v6). Fix Windows and OSX ipv6 sockets
Diffstat (limited to 'drivers/unix')
-rw-r--r--drivers/unix/packet_peer_udp_posix.cpp8
-rw-r--r--drivers/unix/socket_helpers.h8
-rw-r--r--drivers/unix/stream_peer_tcp_posix.cpp8
-rw-r--r--drivers/unix/tcp_server_posix.cpp4
4 files changed, 16 insertions, 12 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp
index cdc3bfce28..d6fc697c6d 100644
--- a/drivers/unix/packet_peer_udp_posix.cpp
+++ b/drivers/unix/packet_peer_udp_posix.cpp
@@ -99,12 +99,12 @@ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
int sock = _get_socket(peer_addr.type);
ERR_FAIL_COND_V( sock == -1, FAILED );
struct sockaddr_storage addr;
- _set_sockaddr(&addr, peer_addr, peer_port);
+ size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port);
errno = 0;
int err;
- while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, sizeof(addr))) != p_buffer_size) {
+ while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) {
if (errno != EAGAIN) {
return FAILED;
@@ -129,9 +129,9 @@ Error PacketPeerUDPPosix::listen(int p_port, IP_Address::AddrType p_address_type
return ERR_CANT_CREATE;
sockaddr_storage addr = {0};
- _set_listen_sockaddr(&addr, p_port, p_address_type, NULL);
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_address_type, NULL);
- if (bind(sock, (struct sockaddr*)&addr, sizeof(sockaddr_storage)) == -1 ) {
+ if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();
return ERR_UNAVAILABLE;
}
diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h
index 622b45c500..4ee40e3d3d 100644
--- a/drivers/unix/socket_helpers.h
+++ b/drivers/unix/socket_helpers.h
@@ -5,7 +5,7 @@
// helpers for sockaddr -> IP_Address and back, should work for posix and winsock. All implementations should use this
-static void _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) {
memset(p_addr, 0, sizeof(struct sockaddr_storage));
if (p_ip.type == IP_Address::TYPE_IPV6) {
@@ -14,6 +14,7 @@ static void _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_i
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(p_port);
copymem(&addr6->sin6_addr.s6_addr, p_ip.field8, 16);
+ return sizeof(sockaddr_in6);
} else {
@@ -21,10 +22,11 @@ static void _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p_i
addr4->sin_family = AF_INET; // host byte order
addr4->sin_port = htons(p_port); // short, network byte order
addr4->sin_addr = *((struct in_addr*)&p_ip.field32[0]);
+ return sizeof(sockaddr_in);
};
};
-static void _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP_Address::AddrType p_address_type, const List<String> *p_accepted_hosts) {
+static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP_Address::AddrType p_address_type, const List<String> *p_accepted_hosts) {
memset(p_addr, 0, sizeof(struct sockaddr_storage));
if (p_address_type == IP_Address::TYPE_IPV4) {
@@ -32,12 +34,14 @@ static void _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP
addr4->sin_family = AF_INET;
addr4->sin_port = htons(p_port);
addr4->sin_addr.s_addr = INADDR_ANY; // TODO: use accepted hosts list
+ return sizeof(sockaddr_in);
} else {
struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
addr6->sin6_family = AF_INET6;
addr6->sin6_port = htons(p_port);
addr6->sin6_addr = in6addr_any; // TODO: use accepted hosts list
+ return sizeof(sockaddr_in6);
};
};
diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp
index 5a63a512e4..f2915b0a16 100644
--- a/drivers/unix/stream_peer_tcp_posix.cpp
+++ b/drivers/unix/stream_peer_tcp_posix.cpp
@@ -98,9 +98,9 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
};
struct sockaddr_storage their_addr;
- _set_sockaddr(&their_addr, peer_host, peer_port);
+ size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port);
- if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(their_addr)) == -1) {
+ if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1) {
if (errno == EISCONN) {
status = STATUS_CONNECTED;
@@ -153,10 +153,10 @@ Error StreamPeerTCPPosix::connect(const IP_Address& p_host, uint16_t p_port) {
#endif
struct sockaddr_storage their_addr;
- _set_sockaddr(&their_addr, p_host, p_port);
+ size_t addr_size = _set_sockaddr(&their_addr, p_host, p_port);
errno = 0;
- if (::connect(sockfd, (struct sockaddr *)&their_addr,sizeof(their_addr)) == -1 && errno != EINPROGRESS) {
+ if (::connect(sockfd, (struct sockaddr *)&their_addr,addr_size) == -1 && errno != EINPROGRESS) {
ERR_PRINT("Connection to remote host failed!");
disconnect();
diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp
index 27c5618479..05b739804c 100644
--- a/drivers/unix/tcp_server_posix.cpp
+++ b/drivers/unix/tcp_server_posix.cpp
@@ -87,11 +87,11 @@ Error TCPServerPosix::listen(uint16_t p_port, IP_Address::AddrType p_type, const
}
struct sockaddr_storage addr;
- _set_listen_sockaddr(&addr, p_port, p_type, p_accepted_hosts);
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, p_type, p_accepted_hosts);
// automatically fill with my IP TODO: use p_accepted_hosts
- if (bind(sockfd, (struct sockaddr *)&addr, sizeof addr) != -1) {
+ if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
if (::listen(sockfd, 1) == -1) {