diff options
Diffstat (limited to 'drivers/unix')
-rw-r--r-- | drivers/unix/packet_peer_udp_posix.cpp | 2 | ||||
-rw-r--r-- | drivers/unix/socket_helpers.h | 14 | ||||
-rw-r--r-- | drivers/unix/tcp_server_posix.cpp | 21 | ||||
-rw-r--r-- | drivers/unix/tcp_server_posix.h | 3 |
4 files changed, 29 insertions, 11 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp index 3d01a23f94..f8a0f556b4 100644 --- a/drivers/unix/packet_peer_udp_posix.cpp +++ b/drivers/unix/packet_peer_udp_posix.cpp @@ -130,7 +130,7 @@ Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size) { 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, ip_type, IP_Address()); if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) { close(); diff --git a/drivers/unix/socket_helpers.h b/drivers/unix/socket_helpers.h index 7dc34811db..bb1a866552 100644 --- a/drivers/unix/socket_helpers.h +++ b/drivers/unix/socket_helpers.h @@ -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/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp index 0178f08b8c..5f3c46451a 100644 --- a/drivers/unix/tcp_server_posix.cpp +++ b/drivers/unix/tcp_server_posix.cpp @@ -68,10 +68,19 @@ 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); + sock_type = ip_type; + + // 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 +97,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 +164,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,6 +177,7 @@ void TCPServerPosix::stop() { }; listen_sockfd = -1; + sock_type = IP::TYPE_NONE; }; @@ -177,6 +185,7 @@ 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(); |