summaryrefslogtreecommitdiff
path: root/drivers/unix
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2017-02-12 23:31:40 +0100
committerGitHub <noreply@github.com>2017-02-12 23:31:40 +0100
commit70b9aa379d99c78f6db87344e3002808dac70bfa (patch)
tree4f83bb38a0fe8b636640cb9b4eb0cd0b82a0b9c6 /drivers/unix
parent117a83fcb916cb02777dea73fb642216fd2e1d79 (diff)
parent5dc7c920bf1c4bb160d39e13ad6136d80badd7ae (diff)
Merge pull request #7581 from Faless/v6_wild_bind
TCP/UDP listen bind to address and bugfixes
Diffstat (limited to 'drivers/unix')
-rw-r--r--drivers/unix/packet_peer_udp_posix.cpp40
-rw-r--r--drivers/unix/packet_peer_udp_posix.h3
-rw-r--r--drivers/unix/socket_helpers.h16
-rw-r--r--drivers/unix/stream_peer_tcp_posix.cpp21
-rw-r--r--drivers/unix/stream_peer_tcp_posix.h4
-rw-r--r--drivers/unix/tcp_server_posix.cpp26
-rw-r--r--drivers/unix/tcp_server_posix.h3
7 files changed, 75 insertions, 38 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp
index 6adb3eea70..7696a5fcb5 100644
--- a/drivers/unix/packet_peer_udp_posix.cpp
+++ b/drivers/unix/packet_peer_udp_posix.cpp
@@ -96,12 +96,15 @@ Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size
}
Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
- ERR_FAIL_COND_V(peer_addr == IP_Address(), ERR_UNCONFIGURED);
+ ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);
+
+ if (sock_type==IP::TYPE_NONE)
+ sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
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, ip_type);
+ size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);
errno = 0;
int err;
@@ -121,16 +124,27 @@ int PacketPeerUDPPosix::get_max_packet_size() const{
return 512; // uhm maybe not
}
-Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size) {
+Error PacketPeerUDPPosix::listen(int p_port, IP_Address p_bind_address, int p_recv_buffer_size) {
+
+ ERR_FAIL_COND_V(sockfd!=-1,ERR_ALREADY_IN_USE);
+ ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(),ERR_INVALID_PARAMETER);
+
+#ifdef __OpenBSD__
+ sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
+#else
+ sock_type = IP::TYPE_ANY;
+#endif
+
+ if(p_bind_address.is_valid())
+ sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
- close();
int sock = _get_socket();
if (sock == -1 )
return ERR_CANT_CREATE;
sockaddr_storage addr = {0};
- size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, NULL);
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, IP_Address());
if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
close();
@@ -145,7 +159,8 @@ void PacketPeerUDPPosix::close(){
if (sockfd != -1)
::close(sockfd);
sockfd=-1;
- rb.resize(8);
+ sock_type = IP::TYPE_NONE;
+ rb.resize(16);
queue_count=0;
}
@@ -157,10 +172,14 @@ Error PacketPeerUDPPosix::wait() {
Error PacketPeerUDPPosix::_poll(bool p_wait) {
+ if (sockfd==-1) {
+ return FAILED;
+ }
+
struct sockaddr_storage from = {0};
socklen_t len = sizeof(struct sockaddr_storage);
int ret;
- while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
+ while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-24, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
uint32_t port = 0;
@@ -221,10 +240,12 @@ int PacketPeerUDPPosix::get_packet_port() const{
int PacketPeerUDPPosix::_get_socket() {
+ ERR_FAIL_COND_V(sock_type==IP::TYPE_NONE, -1);
+
if (sockfd != -1)
return sockfd;
- sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
+ sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP);
return sockfd;
}
@@ -253,7 +274,8 @@ PacketPeerUDPPosix::PacketPeerUDPPosix() {
packet_port=0;
queue_count=0;
peer_port=0;
- ip_type = IP::TYPE_ANY;
+ sock_type = IP::TYPE_NONE;
+ rb.resize(16);
}
PacketPeerUDPPosix::~PacketPeerUDPPosix() {
diff --git a/drivers/unix/packet_peer_udp_posix.h b/drivers/unix/packet_peer_udp_posix.h
index ead7174a9d..ac68344d78 100644
--- a/drivers/unix/packet_peer_udp_posix.h
+++ b/drivers/unix/packet_peer_udp_posix.h
@@ -48,6 +48,7 @@ class PacketPeerUDPPosix : public PacketPeerUDP {
mutable int packet_port;
mutable int queue_count;
int sockfd;
+ IP::Type sock_type;
IP_Address peer_addr;
int peer_port;
@@ -65,7 +66,7 @@ public:
virtual int get_max_packet_size() const;
- virtual Error listen(int p_port, int p_recv_buffer_size=65536);
+ virtual Error listen(int p_port, IP_Address p_bind_address=IP_Address("*"), int p_recv_buffer_size=65536);
virtual void close();
virtual Error wait();
virtual bool is_listening() const;
diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h
index 6d4f7e7519..bb1a866552 100644
--- a/drivers/unix/socket_helpers.h
+++ b/drivers/unix/socket_helpers.h
@@ -16,7 +16,7 @@ static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p
memset(p_addr, 0, sizeof(struct sockaddr_storage));
- ERR_FAIL_COND_V(p_ip==IP_Address(),0);
+ ERR_FAIL_COND_V(!p_ip.is_valid(),0);
// IPv6 socket
if (p_sock_type == IP::TYPE_IPV6 || p_sock_type == IP::TYPE_ANY) {
@@ -44,21 +44,29 @@ static size_t _set_sockaddr(struct sockaddr_storage* p_addr, const IP_Address& p
};
};
-static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const List<String> *p_accepted_hosts) {
+static size_t _set_listen_sockaddr(struct sockaddr_storage* p_addr, int p_port, IP::Type p_sock_type, const IP_Address p_bind_address) {
memset(p_addr, 0, sizeof(struct sockaddr_storage));
if (p_sock_type == IP::TYPE_IPV4) {
struct sockaddr_in* addr4 = (struct sockaddr_in*)p_addr;
addr4->sin_family = AF_INET;
addr4->sin_port = htons(p_port);
- addr4->sin_addr.s_addr = INADDR_ANY; // TODO: use accepted hosts list
+ if(p_bind_address.is_valid()) {
+ copymem(&addr4->sin_addr.s_addr, p_bind_address.get_ipv4(), 4);
+ } else {
+ addr4->sin_addr.s_addr = INADDR_ANY;
+ }
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
+ if(p_bind_address.is_valid()) {
+ copymem(&addr6->sin6_addr.s6_addr, p_bind_address.get_ipv6(), 16);
+ } else {
+ addr6->sin6_addr = in6addr_any;
+ }
return sizeof(sockaddr_in6);
};
};
diff --git a/drivers/unix/stream_peer_tcp_posix.cpp b/drivers/unix/stream_peer_tcp_posix.cpp
index ae4bf77c36..08a2954617 100644
--- a/drivers/unix/stream_peer_tcp_posix.cpp
+++ b/drivers/unix/stream_peer_tcp_posix.cpp
@@ -88,15 +88,10 @@ Error StreamPeerTCPPosix::_block(int p_sockfd, bool p_read, bool p_write) const
return ret < 0 ? FAILED : OK;
};
-Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
+Error StreamPeerTCPPosix::_poll_connection() const {
ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == -1, FAILED);
- if (p_block) {
-
- _block(sockfd, false, true);
- };
-
struct sockaddr_storage their_addr;
size_t addr_size = _set_sockaddr(&their_addr, peer_host, peer_port, sock_type);
@@ -122,10 +117,9 @@ Error StreamPeerTCPPosix::_poll_connection(bool p_block) const {
return OK;
};
-void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type) {
+void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) {
- ip_type = p_ip_type;
- sock_type = p_ip_type;
+ sock_type = p_sock_type;
sockfd = p_sockfd;
#ifndef NO_FCNTL
fcntl(sockfd, F_SETFL, O_NONBLOCK);
@@ -142,7 +136,7 @@ void StreamPeerTCPPosix::set_socket(int p_sockfd, IP_Address p_host, int p_port,
Error StreamPeerTCPPosix::connect_to_host(const IP_Address& p_host, uint16_t p_port) {
- ERR_FAIL_COND_V( p_host == IP_Address(), ERR_INVALID_PARAMETER);
+ ERR_FAIL_COND_V( !p_host.is_valid(), ERR_INVALID_PARAMETER);
sock_type = p_host.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
@@ -192,7 +186,7 @@ Error StreamPeerTCPPosix::write(const uint8_t* p_data,int p_bytes, int &r_sent,
if (status != STATUS_CONNECTED) {
- if (_poll_connection(p_block) != OK) {
+ if (_poll_connection() != OK) {
return FAILED;
};
@@ -252,7 +246,7 @@ Error StreamPeerTCPPosix::read(uint8_t* p_buffer, int p_bytes,int &r_received, b
if (status == STATUS_CONNECTING) {
- if (_poll_connection(p_block) != OK) {
+ if (_poll_connection() != OK) {
return FAILED;
};
@@ -331,7 +325,7 @@ bool StreamPeerTCPPosix::is_connected_to_host() const {
StreamPeerTCP::Status StreamPeerTCPPosix::get_status() const {
if (status == STATUS_CONNECTING) {
- _poll_connection(false);
+ _poll_connection();
};
return status;
@@ -398,7 +392,6 @@ StreamPeerTCPPosix::StreamPeerTCPPosix() {
sockfd = -1;
status = STATUS_NONE;
peer_port = 0;
- ip_type = IP::TYPE_ANY;
};
StreamPeerTCPPosix::~StreamPeerTCPPosix() {
diff --git a/drivers/unix/stream_peer_tcp_posix.h b/drivers/unix/stream_peer_tcp_posix.h
index d33883b159..7f8d90a448 100644
--- a/drivers/unix/stream_peer_tcp_posix.h
+++ b/drivers/unix/stream_peer_tcp_posix.h
@@ -46,7 +46,7 @@ protected:
Error _block(int p_sockfd, bool p_read, bool p_write) const;
- Error _poll_connection(bool p_block) const;
+ Error _poll_connection() const;
IP_Address peer_host;
int peer_port;
@@ -68,7 +68,7 @@ public:
virtual int get_available_bytes() const;
- void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type);
+ void set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type);
virtual IP_Address get_connected_host() const;
virtual uint16_t get_connected_port() const;
diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp
index 0178f08b8c..7e9970453f 100644
--- a/drivers/unix/tcp_server_posix.cpp
+++ b/drivers/unix/tcp_server_posix.cpp
@@ -68,10 +68,23 @@ void TCPServerPosix::make_default() {
TCP_Server::_create = TCPServerPosix::_create;
};
-Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_hosts) {
+Error TCPServerPosix::listen(uint16_t p_port,const IP_Address p_bind_address) {
+
+ ERR_FAIL_COND_V(listen_sockfd!=-1,ERR_ALREADY_IN_USE);
+ ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
int sockfd;
- sockfd = _socket_create(ip_type, SOCK_STREAM, IPPROTO_TCP);
+#ifdef __OpenBSD__
+ sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
+#else
+ sock_type = IP::TYPE_ANY;
+#endif
+
+ // If the bind address is valid use its type as the socket type
+ if (p_bind_address.is_valid())
+ sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
+
+ sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
ERR_FAIL_COND_V(sockfd == -1, FAILED);
@@ -88,9 +101,7 @@ Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_host
}
struct sockaddr_storage addr;
- size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, p_accepted_hosts);
-
- // automatically fill with my IP TODO: use p_accepted_hosts
+ size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, p_bind_address);
if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
@@ -157,7 +168,7 @@ Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
int port;
_set_ip_addr_port(ip, port, &their_addr);
- conn->set_socket(fd, ip, port, ip_type);
+ conn->set_socket(fd, ip, port, sock_type);
return conn;
};
@@ -170,13 +181,14 @@ void TCPServerPosix::stop() {
};
listen_sockfd = -1;
+ sock_type = IP::TYPE_NONE;
};
TCPServerPosix::TCPServerPosix() {
listen_sockfd = -1;
- ip_type = IP::TYPE_ANY;
+ sock_type = IP::TYPE_NONE;
};
TCPServerPosix::~TCPServerPosix() {
diff --git a/drivers/unix/tcp_server_posix.h b/drivers/unix/tcp_server_posix.h
index 6f9fa8cb5b..ea42d0fc0c 100644
--- a/drivers/unix/tcp_server_posix.h
+++ b/drivers/unix/tcp_server_posix.h
@@ -35,12 +35,13 @@
class TCPServerPosix : public TCP_Server {
int listen_sockfd;
+ IP::Type sock_type;
static TCP_Server* _create();
public:
- virtual Error listen(uint16_t p_port,const List<String> *p_accepted_hosts=NULL);
+ virtual Error listen(uint16_t p_port, IP_Address p_bind_address=IP_Address("*"));
virtual bool is_connection_available() const;
virtual Ref<StreamPeerTCP> take_connection();