all repos — mgba @ 8beac67f5620196669fbef0c538e9d428c993ced

mGBA Game Boy Advance Emulator

src/util/socket.h (view raw)

  1/* Copyright (c) 2013-2014 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#ifndef SOCKET_H
  7#define SOCKET_H
  8
  9#include "util/common.h"
 10
 11#ifdef __cplusplus
 12#define restrict __restrict__
 13#endif
 14
 15#ifdef _WIN32
 16#include <winsock2.h>
 17#include <ws2tcpip.h>
 18
 19#define SOCKET_FAILED(s) ((s) == INVALID_SOCKET)
 20typedef SOCKET Socket;
 21#else
 22#include <arpa/inet.h>
 23#include <errno.h>
 24#include <fcntl.h>
 25#include <netinet/in.h>
 26#include <netinet/tcp.h>
 27#include <sys/select.h>
 28#include <sys/socket.h>
 29
 30#define INVALID_SOCKET (-1)
 31#define SOCKET_FAILED(s) ((s) < 0)
 32typedef int Socket;
 33#endif
 34
 35enum IP {
 36	IPV4,
 37	IPV6
 38};
 39
 40struct Address {
 41	enum IP version;
 42	union {
 43		uint32_t ipv4;
 44		uint8_t ipv6[16];
 45	};
 46};
 47
 48#ifdef _3DS
 49#include <3ds.h>
 50#include <malloc.h>
 51
 52#define SOCU_ALIGN 0x1000
 53#define SOCU_BUFFERSIZE 0x100000
 54
 55extern u32* SOCUBuffer;
 56#endif
 57
 58static inline void SocketSubsystemInit() {
 59#ifdef _WIN32
 60	WSADATA data;
 61	WSAStartup(MAKEWORD(2, 2), &data);
 62#elif defined(_3DS)
 63	if (!SOCUBuffer) {
 64		SOCUBuffer = memalign(SOCU_ALIGN, SOCU_BUFFERSIZE);
 65		socInit(SOCUBuffer, SOCU_BUFFERSIZE);
 66	}
 67#endif
 68}
 69
 70static inline void SocketSubsystemDeinit() {
 71#ifdef _WIN32
 72	WSACleanup();
 73#elif defined(_3DS)
 74	socExit();
 75	free(SOCUBuffer);
 76	SOCUBuffer = NULL;
 77#endif
 78}
 79
 80static inline int SocketError() {
 81#ifdef _WIN32
 82	return WSAGetLastError();
 83#else
 84	return errno;
 85#endif
 86}
 87
 88static inline bool SocketWouldBlock() {
 89#ifdef _WIN32
 90	return SocketError() == WSAEWOULDBLOCK;
 91#else
 92	return SocketError() == EWOULDBLOCK || SocketError() == EAGAIN;
 93#endif
 94}
 95
 96static inline ssize_t SocketSend(Socket socket, const void* buffer, size_t size) {
 97#ifdef _WIN32
 98	return send(socket, (const char*) buffer, size, 0);
 99#else
100	return write(socket, buffer, size);
101#endif
102}
103
104static inline ssize_t SocketRecv(Socket socket, void* buffer, size_t size) {
105#ifdef _WIN32
106	return recv(socket, (char*) buffer, size, 0);
107#else
108	return read(socket, buffer, size);
109#endif
110}
111
112static inline Socket SocketOpenTCP(int port, const struct Address* bindAddress) {
113	Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
114	if (SOCKET_FAILED(sock)) {
115		return sock;
116	}
117
118	int err;
119	if (!bindAddress) {
120		struct sockaddr_in bindInfo;
121		memset(&bindInfo, 0, sizeof(bindInfo));
122		bindInfo.sin_family = AF_INET;
123		bindInfo.sin_port = htons(port);
124#ifndef _3DS
125		bindInfo.sin_addr.s_addr = INADDR_ANY;
126#else
127		bindInfo.sin_addr.s_addr = gethostid();
128#endif
129		err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
130	} else if (bindAddress->version == IPV4) {
131		struct sockaddr_in bindInfo;
132		memset(&bindInfo, 0, sizeof(bindInfo));
133		bindInfo.sin_family = AF_INET;
134		bindInfo.sin_port = htons(port);
135		bindInfo.sin_addr.s_addr = bindAddress->ipv4;
136		err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
137#ifndef _3DS
138	} else {
139		struct sockaddr_in6 bindInfo;
140		memset(&bindInfo, 0, sizeof(bindInfo));
141		bindInfo.sin6_family = AF_INET6;
142		bindInfo.sin6_port = htons(port);
143		memcpy(bindInfo.sin6_addr.s6_addr, bindAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
144		err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
145#endif
146	}
147	if (err) {
148		close(sock);
149		return INVALID_SOCKET;
150	}
151	return sock;
152}
153
154static inline Socket SocketConnectTCP(int port, const struct Address* destinationAddress) {
155	Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
156	if (SOCKET_FAILED(sock)) {
157		return sock;
158	}
159
160	int err;
161	if (!destinationAddress) {
162		struct sockaddr_in bindInfo;
163		memset(&bindInfo, 0, sizeof(bindInfo));
164		bindInfo.sin_family = AF_INET;
165		bindInfo.sin_port = htons(port);
166		err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
167	} else if (destinationAddress->version == IPV4) {
168		struct sockaddr_in bindInfo;
169		memset(&bindInfo, 0, sizeof(bindInfo));
170		bindInfo.sin_family = AF_INET;
171		bindInfo.sin_port = htons(port);
172		bindInfo.sin_addr.s_addr = destinationAddress->ipv4;
173		err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
174#ifndef _3DS
175	} else {
176		struct sockaddr_in6 bindInfo;
177		memset(&bindInfo, 0, sizeof(bindInfo));
178		bindInfo.sin6_family = AF_INET6;
179		bindInfo.sin6_port = htons(port);
180		memcpy(bindInfo.sin6_addr.s6_addr, destinationAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
181		err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
182#endif
183	}
184
185	if (err) {
186		close(sock);
187		return INVALID_SOCKET;
188	}
189	return sock;
190}
191
192static inline Socket SocketListen(Socket socket, int queueLength) {
193	return listen(socket, queueLength);
194}
195
196static inline Socket SocketAccept(Socket socket, struct Address* address) {
197	if (!address) {
198		return accept(socket, 0, 0);
199	}
200	if (address->version == IPV4) {
201		struct sockaddr_in addrInfo;
202		memset(&addrInfo, 0, sizeof(addrInfo));
203		addrInfo.sin_family = AF_INET;
204		addrInfo.sin_addr.s_addr = address->ipv4;
205		socklen_t len = sizeof(addrInfo);
206		return accept(socket, (struct sockaddr*) &addrInfo, &len);
207#ifndef _3DS
208	} else {
209		struct sockaddr_in6 addrInfo;
210		memset(&addrInfo, 0, sizeof(addrInfo));
211		addrInfo.sin6_family = AF_INET6;
212		memcpy(addrInfo.sin6_addr.s6_addr, address->ipv6, sizeof(addrInfo.sin6_addr.s6_addr));
213		socklen_t len = sizeof(addrInfo);
214		return accept(socket, (struct sockaddr*) &addrInfo, &len);
215#endif
216	}
217	return INVALID_SOCKET;
218}
219
220static inline int SocketClose(Socket socket) {
221	return close(socket) >= 0;
222}
223
224static inline int SocketSetBlocking(Socket socket, bool blocking) {
225#ifdef _WIN32
226	u_long unblocking = !blocking;
227	return ioctlsocket(socket, FIONBIO, &unblocking) == NO_ERROR;
228#else
229	int flags = fcntl(socket, F_GETFL);
230	if (flags == -1) {
231		return 0;
232	}
233	if (blocking) {
234		flags &= ~O_NONBLOCK;
235	} else {
236		flags |= O_NONBLOCK;
237	}
238	return fcntl(socket, F_SETFL, flags) >= 0;
239#endif
240}
241
242static inline int SocketSetTCPPush(Socket socket, int push) {
243	return setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char*) &push, sizeof(int)) >= 0;
244}
245
246static inline int SocketPoll(size_t nSockets, Socket* reads, Socket* writes, Socket* errors, int64_t timeoutMillis) {
247	fd_set rset;
248	fd_set wset;
249	fd_set eset;
250	FD_ZERO(&rset);
251	FD_ZERO(&wset);
252	FD_ZERO(&eset);
253	size_t i;
254	Socket maxFd = 0;
255	if (reads) {
256		for (i = 0; i < nSockets; ++i) {
257			if (SOCKET_FAILED(reads[i])) {
258				break;
259			}
260			if (reads[i] > maxFd) {
261				maxFd = reads[i];
262			}
263			FD_SET(reads[i], &rset);
264			reads[i] = INVALID_SOCKET;
265		}
266	}
267	if (writes) {
268		for (i = 0; i < nSockets; ++i) {
269			if (SOCKET_FAILED(writes[i])) {
270				break;
271			}
272			if (writes[i] > maxFd) {
273				maxFd = writes[i];
274			}
275			FD_SET(writes[i], &wset);
276			writes[i] = INVALID_SOCKET;
277		}
278	}
279	if (errors) {
280		for (i = 0; i < nSockets; ++i) {
281			if (SOCKET_FAILED(errors[i])) {
282				break;
283			}
284			if (errors[i] > maxFd) {
285				maxFd = errors[i];
286			}
287			FD_SET(errors[i], &eset);
288			errors[i] = INVALID_SOCKET;
289		}
290	}
291	struct timeval tv;
292	tv.tv_sec = timeoutMillis / 1000;
293	tv.tv_usec = (timeoutMillis % 1000) * 1000;
294	int result = select(maxFd + 1, &rset, &wset, &eset, timeoutMillis < 0 ? 0 : &tv);
295	int r = 0;
296	int w = 0;
297	int e = 0;
298	Socket j;
299	for (j = 0; j < maxFd; ++j) {
300		if (reads && FD_ISSET(j, &rset)) {
301			reads[r] = j;
302			++r;
303		}
304		if (writes && FD_ISSET(j, &wset)) {
305			writes[w] = j;
306			++w;
307		}
308		if (errors && FD_ISSET(j, &eset)) {
309			errors[e] = j;
310			++e;
311		}
312	}
313	return result;
314}
315
316#endif