Implement TCP Server bind address
(cherry picked from commit b2839343ca
)
This commit is contained in:
parent
90a747a52d
commit
43d992fc34
9 changed files with 49 additions and 29 deletions
|
@ -44,15 +44,6 @@ TCP_Server *TCP_Server::create() {
|
||||||
return _create();
|
return _create();
|
||||||
}
|
}
|
||||||
|
|
||||||
Error TCP_Server::_listen(uint16_t p_port, DVector<String> p_accepted_hosts) {
|
|
||||||
|
|
||||||
List<String> hosts;
|
|
||||||
for (int i = 0; i < p_accepted_hosts.size(); i++)
|
|
||||||
hosts.push_back(p_accepted_hosts.get(i));
|
|
||||||
|
|
||||||
return listen(p_port, hosts.size() ? &hosts : NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TCP_Server::set_ip_type(IP::Type p_type) {
|
void TCP_Server::set_ip_type(IP::Type p_type) {
|
||||||
stop();
|
stop();
|
||||||
ip_type = p_type;
|
ip_type = p_type;
|
||||||
|
@ -61,7 +52,7 @@ void TCP_Server::set_ip_type(IP::Type p_type) {
|
||||||
void TCP_Server::_bind_methods() {
|
void TCP_Server::_bind_methods() {
|
||||||
|
|
||||||
ObjectTypeDB::bind_method(_MD("set_ip_type", "ip_type"), &TCP_Server::set_ip_type);
|
ObjectTypeDB::bind_method(_MD("set_ip_type", "ip_type"), &TCP_Server::set_ip_type);
|
||||||
ObjectTypeDB::bind_method(_MD("listen", "port", "accepted_hosts"), &TCP_Server::_listen, DEFVAL(DVector<String>()));
|
ObjectTypeDB::bind_method(_MD("listen", "port", "bind_address"), &TCP_Server::listen, DEFVAL("*"));
|
||||||
ObjectTypeDB::bind_method(_MD("is_connection_available"), &TCP_Server::is_connection_available);
|
ObjectTypeDB::bind_method(_MD("is_connection_available"), &TCP_Server::is_connection_available);
|
||||||
ObjectTypeDB::bind_method(_MD("take_connection"), &TCP_Server::take_connection);
|
ObjectTypeDB::bind_method(_MD("take_connection"), &TCP_Server::take_connection);
|
||||||
ObjectTypeDB::bind_method(_MD("stop"), &TCP_Server::stop);
|
ObjectTypeDB::bind_method(_MD("stop"), &TCP_Server::stop);
|
||||||
|
|
|
@ -43,12 +43,11 @@ protected:
|
||||||
static TCP_Server *(*_create)();
|
static TCP_Server *(*_create)();
|
||||||
|
|
||||||
//bind helper
|
//bind helper
|
||||||
Error _listen(uint16_t p_port, DVector<String> p_accepted_hosts = DVector<String>());
|
|
||||||
static void _bind_methods();
|
static void _bind_methods();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void set_ip_type(IP::Type p_type);
|
virtual void set_ip_type(IP::Type p_type);
|
||||||
virtual Error listen(uint16_t p_port, const List<String> *p_accepted_hosts = NULL) = 0;
|
virtual Error listen(uint16_t p_port, const IP_Address p_bind_address = IP_Address("*")) = 0;
|
||||||
virtual bool is_connection_available() const = 0;
|
virtual bool is_connection_available() const = 0;
|
||||||
virtual Ref<StreamPeerTCP> take_connection() = 0;
|
virtual Ref<StreamPeerTCP> take_connection() = 0;
|
||||||
|
|
||||||
|
|
|
@ -128,7 +128,7 @@ Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size) {
|
||||||
return ERR_CANT_CREATE;
|
return ERR_CANT_CREATE;
|
||||||
|
|
||||||
sockaddr_storage addr = { 0 };
|
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) {
|
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
||||||
close();
|
close();
|
||||||
|
|
|
@ -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));
|
memset(p_addr, 0, sizeof(struct sockaddr_storage));
|
||||||
if (p_sock_type == IP::TYPE_IPV4) {
|
if (p_sock_type == IP::TYPE_IPV4) {
|
||||||
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
struct sockaddr_in *addr4 = (struct sockaddr_in *)p_addr;
|
||||||
addr4->sin_family = AF_INET;
|
addr4->sin_family = AF_INET;
|
||||||
addr4->sin_port = htons(p_port);
|
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);
|
return sizeof(sockaddr_in);
|
||||||
} else {
|
} else {
|
||||||
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
|
||||||
|
|
||||||
addr6->sin6_family = AF_INET6;
|
addr6->sin6_family = AF_INET6;
|
||||||
addr6->sin6_port = htons(p_port);
|
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);
|
return sizeof(sockaddr_in6);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -68,10 +68,19 @@ void TCPServerPosix::make_default() {
|
||||||
TCP_Server::_create = TCPServerPosix::_create;
|
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;
|
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);
|
ERR_FAIL_COND_V(sockfd == -1, FAILED);
|
||||||
|
|
||||||
|
@ -88,9 +97,7 @@ Error TCPServerPosix::listen(uint16_t p_port, const List<String> *p_accepted_hos
|
||||||
}
|
}
|
||||||
|
|
||||||
struct sockaddr_storage addr;
|
struct sockaddr_storage addr;
|
||||||
size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, p_accepted_hosts);
|
size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, p_bind_address);
|
||||||
|
|
||||||
// automatically fill with my IP TODO: use p_accepted_hosts
|
|
||||||
|
|
||||||
if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
|
if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
|
||||||
|
|
||||||
|
@ -156,7 +163,7 @@ Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
|
||||||
int port;
|
int port;
|
||||||
_set_ip_addr_port(ip, port, &their_addr);
|
_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;
|
return conn;
|
||||||
};
|
};
|
||||||
|
@ -169,12 +176,14 @@ void TCPServerPosix::stop() {
|
||||||
};
|
};
|
||||||
|
|
||||||
listen_sockfd = -1;
|
listen_sockfd = -1;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
TCPServerPosix::TCPServerPosix() {
|
TCPServerPosix::TCPServerPosix() {
|
||||||
|
|
||||||
listen_sockfd = -1;
|
listen_sockfd = -1;
|
||||||
ip_type = IP::TYPE_ANY;
|
ip_type = IP::TYPE_ANY;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
TCPServerPosix::~TCPServerPosix() {
|
TCPServerPosix::~TCPServerPosix() {
|
||||||
|
|
|
@ -35,11 +35,12 @@
|
||||||
class TCPServerPosix : public TCP_Server {
|
class TCPServerPosix : public TCP_Server {
|
||||||
|
|
||||||
int listen_sockfd;
|
int listen_sockfd;
|
||||||
|
IP::Type sock_type;
|
||||||
|
|
||||||
static TCP_Server *_create();
|
static TCP_Server *_create();
|
||||||
|
|
||||||
public:
|
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 bool is_connection_available() const;
|
||||||
virtual Ref<StreamPeerTCP> take_connection();
|
virtual Ref<StreamPeerTCP> take_connection();
|
||||||
|
|
||||||
|
|
|
@ -120,7 +120,7 @@ Error PacketPeerUDPWinsock::listen(int p_port, int p_recv_buffer_size) {
|
||||||
return ERR_CANT_CREATE;
|
return ERR_CANT_CREATE;
|
||||||
|
|
||||||
struct sockaddr_storage addr = { 0 };
|
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, ip_type, IP_Address());
|
||||||
|
|
||||||
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
if (bind(sock, (struct sockaddr *)&addr, addr_size) == -1) {
|
||||||
close();
|
close();
|
||||||
|
|
|
@ -62,10 +62,19 @@ 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;
|
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 == INVALID_SOCKET, FAILED);
|
ERR_FAIL_COND_V(sockfd == INVALID_SOCKET, FAILED);
|
||||||
|
|
||||||
unsigned long par = 1;
|
unsigned long par = 1;
|
||||||
|
@ -76,7 +85,7 @@ Error TCPServerWinsock::listen(uint16_t p_port, const List<String> *p_accepted_h
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sockaddr_storage my_addr;
|
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;
|
int reuse = 1;
|
||||||
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
|
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
|
||||||
|
@ -146,7 +155,7 @@ Ref<StreamPeerTCP> TCPServerWinsock::take_connection() {
|
||||||
int port;
|
int port;
|
||||||
_set_ip_addr_port(ip, port, &their_addr);
|
_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;
|
return conn;
|
||||||
};
|
};
|
||||||
|
@ -158,12 +167,14 @@ void TCPServerWinsock::stop() {
|
||||||
};
|
};
|
||||||
|
|
||||||
listen_sockfd = -1;
|
listen_sockfd = -1;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
TCPServerWinsock::TCPServerWinsock() {
|
TCPServerWinsock::TCPServerWinsock() {
|
||||||
|
|
||||||
listen_sockfd = INVALID_SOCKET;
|
listen_sockfd = INVALID_SOCKET;
|
||||||
ip_type = IP::TYPE_ANY;
|
ip_type = IP::TYPE_ANY;
|
||||||
|
sock_type = IP::TYPE_NONE;
|
||||||
};
|
};
|
||||||
|
|
||||||
TCPServerWinsock::~TCPServerWinsock() {
|
TCPServerWinsock::~TCPServerWinsock() {
|
||||||
|
|
|
@ -34,11 +34,12 @@
|
||||||
class TCPServerWinsock : public TCP_Server {
|
class TCPServerWinsock : public TCP_Server {
|
||||||
|
|
||||||
int listen_sockfd;
|
int listen_sockfd;
|
||||||
|
IP::Type sock_type;
|
||||||
|
|
||||||
static TCP_Server *_create();
|
static TCP_Server *_create();
|
||||||
|
|
||||||
public:
|
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 bool is_connection_available() const;
|
||||||
virtual Ref<StreamPeerTCP> take_connection();
|
virtual Ref<StreamPeerTCP> take_connection();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue