diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-02-12 23:31:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-12 23:31:40 +0100 |
commit | 70b9aa379d99c78f6db87344e3002808dac70bfa (patch) | |
tree | 4f83bb38a0fe8b636640cb9b4eb0cd0b82a0b9c6 /platform/windows | |
parent | 117a83fcb916cb02777dea73fb642216fd2e1d79 (diff) | |
parent | 5dc7c920bf1c4bb160d39e13ad6136d80badd7ae (diff) |
Merge pull request #7581 from Faless/v6_wild_bind
TCP/UDP listen bind to address and bugfixes
Diffstat (limited to 'platform/windows')
-rw-r--r-- | platform/windows/packet_peer_udp_winsock.cpp | 35 | ||||
-rw-r--r-- | platform/windows/packet_peer_udp_winsock.h | 3 | ||||
-rw-r--r-- | platform/windows/stream_peer_winsock.cpp | 21 | ||||
-rw-r--r-- | platform/windows/stream_peer_winsock.h | 4 | ||||
-rw-r--r-- | platform/windows/tcp_server_winsock.cpp | 21 | ||||
-rw-r--r-- | platform/windows/tcp_server_winsock.h | 3 |
6 files changed, 56 insertions, 31 deletions
diff --git a/platform/windows/packet_peer_udp_winsock.cpp b/platform/windows/packet_peer_udp_winsock.cpp index 5baa01c289..276b9b6634 100644 --- a/platform/windows/packet_peer_udp_winsock.cpp +++ b/platform/windows/packet_peer_udp_winsock.cpp @@ -73,10 +73,15 @@ Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer,int &r_buffer_si } Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer,int p_buffer_size){ + 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); _set_blocking(true); @@ -114,15 +119,22 @@ void PacketPeerUDPWinsock::_set_blocking(bool p_blocking) { }; } -Error PacketPeerUDPWinsock::listen(int p_port, int p_recv_buffer_size) { +Error PacketPeerUDPWinsock::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); + + sock_type = IP::TYPE_ANY; + + 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; struct 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(); @@ -141,7 +153,8 @@ void PacketPeerUDPWinsock::close(){ if (sockfd != -1) ::closesocket(sockfd); sockfd=-1; - rb.resize(8); + sock_type = IP::TYPE_NONE; + rb.resize(16); queue_count=0; } @@ -152,6 +165,9 @@ Error PacketPeerUDPWinsock::wait() { } Error PacketPeerUDPWinsock::_poll(bool p_wait) { + if (sockfd==-1) { + return FAILED; + } _set_blocking(p_wait); @@ -159,7 +175,7 @@ Error PacketPeerUDPWinsock::_poll(bool p_wait) { struct sockaddr_storage from = {0}; int len = sizeof(struct sockaddr_storage); int ret; - while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), 0, (struct sockaddr*)&from, &len)) > 0) { + while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-24, 0)), 0, (struct sockaddr*)&from, &len)) > 0) { uint32_t port = 0; @@ -238,10 +254,12 @@ int PacketPeerUDPWinsock::get_packet_port() const{ int PacketPeerUDPWinsock::_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; } @@ -271,7 +289,8 @@ PacketPeerUDPWinsock::PacketPeerUDPWinsock() { packet_port=0; queue_count=0; peer_port=0; - ip_type = IP::TYPE_ANY; + sock_type = IP::TYPE_NONE; + rb.resize(16); } PacketPeerUDPWinsock::~PacketPeerUDPWinsock() { diff --git a/platform/windows/packet_peer_udp_winsock.h b/platform/windows/packet_peer_udp_winsock.h index 18e6bd8f3d..dcd9da9a79 100644 --- a/platform/windows/packet_peer_udp_winsock.h +++ b/platform/windows/packet_peer_udp_winsock.h @@ -46,6 +46,7 @@ class PacketPeerUDPWinsock : public PacketPeerUDP { mutable int packet_port; mutable int queue_count; int sockfd; + IP::Type sock_type; IP_Address peer_addr; int peer_port; @@ -67,7 +68,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/platform/windows/stream_peer_winsock.cpp b/platform/windows/stream_peer_winsock.cpp index 9d3e3623da..106eb6a5f5 100644 --- a/platform/windows/stream_peer_winsock.cpp +++ b/platform/windows/stream_peer_winsock.cpp @@ -78,15 +78,10 @@ Error StreamPeerWinsock::_block(int p_sockfd, bool p_read, bool p_write) const { return ret < 0 ? FAILED : OK; }; -Error StreamPeerWinsock::_poll_connection(bool p_block) const { +Error StreamPeerWinsock::_poll_connection() const { ERR_FAIL_COND_V(status != STATUS_CONNECTING || sockfd == INVALID_SOCKET, 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,7 +117,7 @@ Error StreamPeerWinsock::write(const uint8_t* p_data,int p_bytes, int &r_sent, b if (status != STATUS_CONNECTED) { - if (_poll_connection(p_block) != OK) { + if (_poll_connection() != OK) { return FAILED; }; @@ -180,7 +175,7 @@ Error StreamPeerWinsock::read(uint8_t* p_buffer, int p_bytes,int &r_received, bo if (status != STATUS_CONNECTED) { - if (_poll_connection(p_block) != OK) { + if (_poll_connection() != OK) { return FAILED; }; @@ -254,7 +249,7 @@ Error StreamPeerWinsock::get_partial_data(uint8_t* p_buffer, int p_bytes,int &r_ StreamPeerTCP::Status StreamPeerWinsock::get_status() const { if (status == STATUS_CONNECTING) { - _poll_connection(false); + _poll_connection(); }; return status; @@ -287,11 +282,10 @@ void StreamPeerWinsock::disconnect_from_host() { peer_port = 0; }; -void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_ip_type) { +void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, IP::Type p_sock_type) { - ip_type = p_ip_type; sockfd = p_sockfd; - sock_type = p_ip_type; + sock_type = p_sock_type; status = STATUS_CONNECTING; peer_host = p_host; peer_port = p_port; @@ -299,7 +293,7 @@ void StreamPeerWinsock::set_socket(int p_sockfd, IP_Address p_host, int p_port, Error StreamPeerWinsock::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); @@ -369,7 +363,6 @@ StreamPeerWinsock::StreamPeerWinsock() { sockfd = INVALID_SOCKET; status = STATUS_NONE; peer_port = 0; - ip_type = IP::TYPE_ANY; }; StreamPeerWinsock::~StreamPeerWinsock() { diff --git a/platform/windows/stream_peer_winsock.h b/platform/windows/stream_peer_winsock.h index 59e4dc0d09..7cf60fc10e 100644 --- a/platform/windows/stream_peer_winsock.h +++ b/platform/windows/stream_peer_winsock.h @@ -47,7 +47,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; @@ -69,7 +69,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/platform/windows/tcp_server_winsock.cpp b/platform/windows/tcp_server_winsock.cpp index 1c3682adfa..883741b115 100644 --- a/platform/windows/tcp_server_winsock.cpp +++ b/platform/windows/tcp_server_winsock.cpp @@ -63,10 +63,20 @@ void TCPServerWinsock::cleanup() { }; -Error TCPServerWinsock::listen(uint16_t p_port,const List<String> *p_accepted_hosts) { +Error TCPServerWinsock::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); + sock_type = IP::TYPE_ANY; + + // 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 == INVALID_SOCKET, FAILED); unsigned long par = 1; @@ -77,7 +87,7 @@ Error TCPServerWinsock::listen(uint16_t p_port,const List<String> *p_accepted_ho }; struct sockaddr_storage my_addr; - size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, ip_type, p_accepted_hosts); + size_t addr_size = _set_listen_sockaddr(&my_addr, p_port, sock_type, p_bind_address); int reuse=1; if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) { @@ -150,7 +160,7 @@ Ref<StreamPeerTCP> TCPServerWinsock::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; }; @@ -162,13 +172,14 @@ void TCPServerWinsock::stop() { }; listen_sockfd = -1; + sock_type = IP::TYPE_NONE; }; TCPServerWinsock::TCPServerWinsock() { listen_sockfd = INVALID_SOCKET; - ip_type = IP::TYPE_ANY; + sock_type = IP::TYPE_NONE; }; TCPServerWinsock::~TCPServerWinsock() { diff --git a/platform/windows/tcp_server_winsock.h b/platform/windows/tcp_server_winsock.h index 5c544436a7..afbd1e9c87 100644 --- a/platform/windows/tcp_server_winsock.h +++ b/platform/windows/tcp_server_winsock.h @@ -34,12 +34,13 @@ class TCPServerWinsock : 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, const IP_Address p_bind_address=IP_Address("*")); virtual bool is_connection_available() const; virtual Ref<StreamPeerTCP> take_connection(); |