1 /*************************************************************************/
2 /*  tcp_server_posix.cpp                                                 */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #include "tcp_server_posix.h"
31 #include "stream_peer_tcp_posix.h"
32 
33 #ifdef UNIX_ENABLED
34 
35 #include <poll.h>
36 
37 #include <errno.h>
38 #include <netdb.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #ifndef NO_FCNTL
45 #ifdef __HAIKU__
46 #include <fcntl.h>
47 #else
48 #include <sys/fcntl.h>
49 #endif
50 #else
51 #include <sys/ioctl.h>
52 #endif
53 #ifdef JAVASCRIPT_ENABLED
54 #include <arpa/inet.h>
55 #endif
56 #include <assert.h>
57 #include <netinet/in.h>
58 #include <sys/socket.h>
59 
60 #include "drivers/unix/socket_helpers.h"
61 
_create()62 TCP_Server *TCPServerPosix::_create() {
63 
64 	return memnew(TCPServerPosix);
65 };
66 
make_default()67 void TCPServerPosix::make_default() {
68 
69 	TCP_Server::_create = TCPServerPosix::_create;
70 };
71 
listen(uint16_t p_port,const IP_Address p_bind_address)72 Error TCPServerPosix::listen(uint16_t p_port, const IP_Address p_bind_address) {
73 
74 	ERR_FAIL_COND_V(listen_sockfd != -1, ERR_ALREADY_IN_USE);
75 	ERR_FAIL_COND_V(!p_bind_address.is_valid() && !p_bind_address.is_wildcard(), ERR_INVALID_PARAMETER);
76 
77 	int sockfd;
78 #ifdef __OpenBSD__
79 	sock_type = IP::TYPE_IPV4; // OpenBSD does not support dual stacking, fallback to IPv4 only.
80 #else
81 	sock_type = IP::TYPE_ANY;
82 #endif
83 
84 	// If the bind address is valid use its type as the socket type
85 	if (p_bind_address.is_valid())
86 		sock_type = p_bind_address.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;
87 
88 	sockfd = _socket_create(sock_type, SOCK_STREAM, IPPROTO_TCP);
89 
90 	ERR_FAIL_COND_V(sockfd == -1, FAILED);
91 
92 #ifndef NO_FCNTL
93 	fcntl(sockfd, F_SETFL, O_NONBLOCK);
94 #else
95 	int bval = 1;
96 	ioctl(sockfd, FIONBIO, &bval);
97 #endif
98 
99 	int reuse = 1;
100 	if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) {
101 		WARN_PRINT("REUSEADDR failed!")
102 	}
103 
104 	struct sockaddr_storage addr;
105 	size_t addr_size = _set_listen_sockaddr(&addr, p_port, sock_type, p_bind_address);
106 
107 	if (bind(sockfd, (struct sockaddr *)&addr, addr_size) != -1) {
108 
109 		if (::listen(sockfd, 1) == -1) {
110 
111 			close(sockfd);
112 			ERR_FAIL_V(FAILED);
113 		};
114 	} else {
115 		return ERR_ALREADY_IN_USE;
116 	};
117 
118 	if (listen_sockfd != -1) {
119 		stop();
120 	};
121 
122 	listen_sockfd = sockfd;
123 
124 	return OK;
125 };
126 
is_connection_available() const127 bool TCPServerPosix::is_connection_available() const {
128 
129 	if (listen_sockfd == -1) {
130 		return false;
131 	};
132 
133 	struct pollfd pfd;
134 	pfd.fd = listen_sockfd;
135 	pfd.events = POLLIN;
136 	pfd.revents = 0;
137 
138 	int ret = poll(&pfd, 1, 0);
139 	ERR_FAIL_COND_V(ret < 0, FAILED);
140 
141 	if (ret && (pfd.revents & POLLIN)) {
142 		return true;
143 	};
144 
145 	return false;
146 };
147 
take_connection()148 Ref<StreamPeerTCP> TCPServerPosix::take_connection() {
149 
150 	if (!is_connection_available()) {
151 		return Ref<StreamPeerTCP>();
152 	};
153 
154 	struct sockaddr_storage their_addr;
155 	socklen_t size = sizeof(their_addr);
156 	int fd = accept(listen_sockfd, (struct sockaddr *)&their_addr, &size);
157 	ERR_FAIL_COND_V(fd == -1, Ref<StreamPeerTCP>());
158 #ifndef NO_FCNTL
159 	fcntl(fd, F_SETFL, O_NONBLOCK);
160 #else
161 	int bval = 1;
162 	ioctl(fd, FIONBIO, &bval);
163 #endif
164 
165 	Ref<StreamPeerTCPPosix> conn = memnew(StreamPeerTCPPosix);
166 	IP_Address ip;
167 
168 	int port;
169 	_set_ip_addr_port(ip, port, &their_addr);
170 
171 	conn->set_socket(fd, ip, port, sock_type);
172 
173 	return conn;
174 };
175 
stop()176 void TCPServerPosix::stop() {
177 
178 	if (listen_sockfd != -1) {
179 		int ret = close(listen_sockfd);
180 		ERR_FAIL_COND(ret != 0);
181 	};
182 
183 	listen_sockfd = -1;
184 	sock_type = IP::TYPE_NONE;
185 };
186 
TCPServerPosix()187 TCPServerPosix::TCPServerPosix() {
188 
189 	listen_sockfd = -1;
190 	sock_type = IP::TYPE_NONE;
191 };
192 
~TCPServerPosix()193 TCPServerPosix::~TCPServerPosix() {
194 
195 	stop();
196 };
197 #endif
198