summaryrefslogtreecommitdiff
path: root/drivers/unix/tcp_server_posix.cpp
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2016-10-30 09:00:45 -0300
committerJuan Linietsky <reduzio@gmail.com>2016-10-30 09:00:45 -0300
commitab4126f51061277e87b41c48b40e7b54942d4eca (patch)
treec58168b60323c4d43b58743b099e562a89e60a56 /drivers/unix/tcp_server_posix.cpp
parent8b15b26eedad4fdd33d50f5f9aa0fcc1875d503f (diff)
parent914015f3b63dd956e72ea937d46ea4b2db005ada (diff)
Merge branch 'master' of https://github.com/godotengine/godot
Diffstat (limited to 'drivers/unix/tcp_server_posix.cpp')
-rw-r--r--drivers/unix/tcp_server_posix.cpp32
1 files changed, 19 insertions, 13 deletions
diff --git a/drivers/unix/tcp_server_posix.cpp b/drivers/unix/tcp_server_posix.cpp
index c67bb51334..05b739804c 100644
--- a/drivers/unix/tcp_server_posix.cpp
+++ b/drivers/unix/tcp_server_posix.cpp
@@ -55,6 +55,9 @@
#include <netinet/in.h>
#include <sys/socket.h>
#include <assert.h>
+
+#include "drivers/unix/socket_helpers.h"
+
TCP_Server* TCPServerPosix::_create() {
return memnew(TCPServerPosix);
@@ -65,10 +68,11 @@ 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, IP_Address::AddrType p_type, const List<String> *p_accepted_hosts) {
int sockfd;
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ int family = p_type == IP_Address::TYPE_IPV6 ? AF_INET6 : AF_INET;
+ sockfd = socket(family, SOCK_STREAM, 0);
ERR_FAIL_COND_V(sockfd == -1, FAILED);
#ifndef NO_FCNTL
fcntl(sockfd, F_SETFL, O_NONBLOCK);
@@ -82,13 +86,12 @@ Error TCPServerPosix::listen(uint16_t p_port,const List<String> *p_accepted_host
WARN_PRINT("REUSEADDR failed!")
}
- struct sockaddr_in my_addr;
- my_addr.sin_family = AF_INET; // host byte order
- my_addr.sin_port = htons(p_port); // short, network byte order
- my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP TODO: use p_accepted_hosts
- memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
+ struct sockaddr_storage addr;
+ 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 *)&my_addr, sizeof my_addr) != -1) {
+ if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
if (::listen(sockfd, 1) == -1) {
@@ -136,9 +139,9 @@ Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
return Ref<StreamPeerTCP>();
};
- struct sockaddr_in their_addr;
- socklen_t sin_size = sizeof(their_addr);
- int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &sin_size);
+ struct sockaddr_storage their_addr;
+ socklen_t size = sizeof(their_addr);
+ int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size);
ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>());
#ifndef NO_FCNTL
fcntl(fd, F_SETFL, O_NONBLOCK);
@@ -149,8 +152,11 @@ Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix);
IP_Address ip;
- ip.host = (uint32_t)their_addr.sin_addr.s_addr;
- conn->set_socket(fd, ip, ntohs(their_addr.sin_port));
+
+ int port;
+ _set_ip_addr_port(ip, port, &their_addr);
+
+ conn->set_socket(fd, ip, port);
return conn;
};