2014-12-03 08:39:06 +00:00
|
|
|
/* Copyright (c) 2013-2014 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2014-02-04 07:01:26 +00:00
|
|
|
#ifndef SOCKET_H
|
|
|
|
#define SOCKET_H
|
|
|
|
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba-util/common.h>
|
2014-04-03 06:50:20 +00:00
|
|
|
|
2016-12-27 05:01:55 +00:00
|
|
|
CXX_GUARD_START
|
|
|
|
|
2016-04-18 01:03:13 +00:00
|
|
|
#if defined(__cplusplus) && !defined(restrict)
|
2014-07-20 23:37:54 +00:00
|
|
|
#define restrict __restrict__
|
|
|
|
#endif
|
|
|
|
|
2014-02-04 07:01:26 +00:00
|
|
|
#ifdef _WIN32
|
2014-04-24 04:42:08 +00:00
|
|
|
#include <ws2tcpip.h>
|
2014-02-04 07:01:26 +00:00
|
|
|
|
2015-01-16 08:11:50 +00:00
|
|
|
#define SOCKET_FAILED(s) ((s) == INVALID_SOCKET)
|
2014-02-04 07:01:26 +00:00
|
|
|
typedef SOCKET Socket;
|
|
|
|
#else
|
2016-03-02 06:01:54 +00:00
|
|
|
#include <arpa/inet.h>
|
2015-02-24 07:49:40 +00:00
|
|
|
#include <errno.h>
|
2014-02-04 07:01:26 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
2014-02-10 09:42:40 +00:00
|
|
|
#include <netinet/tcp.h>
|
2015-03-23 09:47:10 +00:00
|
|
|
#include <sys/select.h>
|
2014-02-04 07:01:26 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2014-04-24 04:42:08 +00:00
|
|
|
#define INVALID_SOCKET (-1)
|
2015-01-16 08:11:50 +00:00
|
|
|
#define SOCKET_FAILED(s) ((s) < 0)
|
2014-02-04 07:01:26 +00:00
|
|
|
typedef int Socket;
|
|
|
|
#endif
|
|
|
|
|
2015-01-09 10:03:42 +00:00
|
|
|
enum IP {
|
|
|
|
IPV4,
|
|
|
|
IPV6
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Address {
|
|
|
|
enum IP version;
|
|
|
|
union {
|
|
|
|
uint32_t ipv4;
|
|
|
|
uint8_t ipv6[16];
|
|
|
|
};
|
|
|
|
};
|
2014-02-04 07:01:26 +00:00
|
|
|
|
2016-03-02 06:01:54 +00:00
|
|
|
#ifdef _3DS
|
|
|
|
#include <3ds.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#define SOCU_ALIGN 0x1000
|
|
|
|
#define SOCU_BUFFERSIZE 0x100000
|
|
|
|
|
|
|
|
extern u32* SOCUBuffer;
|
|
|
|
#endif
|
|
|
|
|
2015-02-24 07:27:28 +00:00
|
|
|
static inline void SocketSubsystemInit() {
|
2014-02-04 07:01:26 +00:00
|
|
|
#ifdef _WIN32
|
2015-02-24 07:27:28 +00:00
|
|
|
WSADATA data;
|
|
|
|
WSAStartup(MAKEWORD(2, 2), &data);
|
2016-03-02 06:01:54 +00:00
|
|
|
#elif defined(_3DS)
|
|
|
|
if (!SOCUBuffer) {
|
|
|
|
SOCUBuffer = memalign(SOCU_ALIGN, SOCU_BUFFERSIZE);
|
|
|
|
socInit(SOCUBuffer, SOCU_BUFFERSIZE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void SocketSubsystemDeinit() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
WSACleanup();
|
|
|
|
#elif defined(_3DS)
|
|
|
|
socExit();
|
|
|
|
free(SOCUBuffer);
|
|
|
|
SOCUBuffer = NULL;
|
2015-02-24 07:27:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int SocketError() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return WSAGetLastError();
|
|
|
|
#else
|
|
|
|
return errno;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool SocketWouldBlock() {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return SocketError() == WSAEWOULDBLOCK;
|
|
|
|
#else
|
|
|
|
return SocketError() == EWOULDBLOCK || SocketError() == EAGAIN;
|
2014-02-04 07:01:26 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-05 09:22:34 +00:00
|
|
|
static inline ssize_t SocketSend(Socket socket, const void* buffer, size_t size) {
|
2015-02-24 08:02:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return send(socket, (const char*) buffer, size, 0);
|
|
|
|
#else
|
|
|
|
return write(socket, buffer, size);
|
|
|
|
#endif
|
2014-02-04 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2014-02-05 09:22:34 +00:00
|
|
|
static inline ssize_t SocketRecv(Socket socket, void* buffer, size_t size) {
|
2015-02-24 08:02:24 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
return recv(socket, (char*) buffer, size, 0);
|
|
|
|
#else
|
|
|
|
return read(socket, buffer, size);
|
|
|
|
#endif
|
2014-02-04 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2016-03-05 19:33:36 +00:00
|
|
|
static inline int SocketClose(Socket socket) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
return closesocket(socket) == 0;
|
|
|
|
#else
|
|
|
|
return close(socket) >= 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-09 10:03:42 +00:00
|
|
|
static inline Socket SocketOpenTCP(int port, const struct Address* bindAddress) {
|
2014-02-04 07:01:26 +00:00
|
|
|
Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2014-04-24 04:42:08 +00:00
|
|
|
if (SOCKET_FAILED(sock)) {
|
2014-02-04 07:01:26 +00:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2015-01-09 10:03:42 +00:00
|
|
|
int err;
|
|
|
|
if (!bindAddress) {
|
|
|
|
struct sockaddr_in bindInfo;
|
|
|
|
memset(&bindInfo, 0, sizeof(bindInfo));
|
|
|
|
bindInfo.sin_family = AF_INET;
|
|
|
|
bindInfo.sin_port = htons(port);
|
2016-03-02 06:01:54 +00:00
|
|
|
#ifndef _3DS
|
|
|
|
bindInfo.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
#else
|
|
|
|
bindInfo.sin_addr.s_addr = gethostid();
|
|
|
|
#endif
|
2015-01-09 10:03:42 +00:00
|
|
|
err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
|
|
|
|
} else if (bindAddress->version == IPV4) {
|
|
|
|
struct sockaddr_in bindInfo;
|
|
|
|
memset(&bindInfo, 0, sizeof(bindInfo));
|
|
|
|
bindInfo.sin_family = AF_INET;
|
|
|
|
bindInfo.sin_port = htons(port);
|
2016-04-28 05:17:21 +00:00
|
|
|
bindInfo.sin_addr.s_addr = htonl(bindAddress->ipv4);
|
2015-01-09 10:03:42 +00:00
|
|
|
err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
|
2016-03-02 06:01:54 +00:00
|
|
|
#ifndef _3DS
|
2015-01-09 10:03:42 +00:00
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 bindInfo;
|
|
|
|
memset(&bindInfo, 0, sizeof(bindInfo));
|
|
|
|
bindInfo.sin6_family = AF_INET6;
|
|
|
|
bindInfo.sin6_port = htons(port);
|
|
|
|
memcpy(bindInfo.sin6_addr.s6_addr, bindAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
|
|
|
|
err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
|
2016-03-02 06:01:54 +00:00
|
|
|
#endif
|
2015-01-09 10:03:42 +00:00
|
|
|
}
|
2014-02-04 07:01:26 +00:00
|
|
|
if (err) {
|
2016-03-05 19:33:36 +00:00
|
|
|
SocketClose(sock);
|
2015-02-24 07:27:28 +00:00
|
|
|
return INVALID_SOCKET;
|
2014-02-04 07:01:26 +00:00
|
|
|
}
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2015-01-09 10:03:42 +00:00
|
|
|
static inline Socket SocketConnectTCP(int port, const struct Address* destinationAddress) {
|
2014-02-11 08:19:29 +00:00
|
|
|
Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2014-04-24 04:42:08 +00:00
|
|
|
if (SOCKET_FAILED(sock)) {
|
2014-02-11 08:19:29 +00:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2015-01-09 10:03:42 +00:00
|
|
|
int err;
|
|
|
|
if (!destinationAddress) {
|
|
|
|
struct sockaddr_in bindInfo;
|
|
|
|
memset(&bindInfo, 0, sizeof(bindInfo));
|
|
|
|
bindInfo.sin_family = AF_INET;
|
|
|
|
bindInfo.sin_port = htons(port);
|
|
|
|
err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
|
|
|
|
} else if (destinationAddress->version == IPV4) {
|
|
|
|
struct sockaddr_in bindInfo;
|
|
|
|
memset(&bindInfo, 0, sizeof(bindInfo));
|
|
|
|
bindInfo.sin_family = AF_INET;
|
|
|
|
bindInfo.sin_port = htons(port);
|
2016-04-28 05:17:21 +00:00
|
|
|
bindInfo.sin_addr.s_addr = htonl(destinationAddress->ipv4);
|
2015-01-09 10:03:42 +00:00
|
|
|
err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
|
2016-03-02 06:01:54 +00:00
|
|
|
#ifndef _3DS
|
2015-01-09 10:03:42 +00:00
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 bindInfo;
|
|
|
|
memset(&bindInfo, 0, sizeof(bindInfo));
|
|
|
|
bindInfo.sin6_family = AF_INET6;
|
|
|
|
bindInfo.sin6_port = htons(port);
|
|
|
|
memcpy(bindInfo.sin6_addr.s6_addr, destinationAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
|
|
|
|
err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
|
2016-03-02 06:01:54 +00:00
|
|
|
#endif
|
2015-01-09 10:03:42 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 08:19:29 +00:00
|
|
|
if (err) {
|
2016-03-05 19:33:36 +00:00
|
|
|
SocketClose(sock);
|
2015-02-24 07:27:28 +00:00
|
|
|
return INVALID_SOCKET;
|
2014-02-11 08:19:29 +00:00
|
|
|
}
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2014-02-05 09:22:34 +00:00
|
|
|
static inline Socket SocketListen(Socket socket, int queueLength) {
|
2014-02-04 07:01:26 +00:00
|
|
|
return listen(socket, queueLength);
|
|
|
|
}
|
|
|
|
|
2015-01-09 10:03:42 +00:00
|
|
|
static inline Socket SocketAccept(Socket socket, struct Address* address) {
|
|
|
|
if (!address) {
|
|
|
|
return accept(socket, 0, 0);
|
|
|
|
}
|
|
|
|
if (address->version == IPV4) {
|
|
|
|
struct sockaddr_in addrInfo;
|
|
|
|
memset(&addrInfo, 0, sizeof(addrInfo));
|
|
|
|
addrInfo.sin_family = AF_INET;
|
|
|
|
addrInfo.sin_addr.s_addr = address->ipv4;
|
|
|
|
socklen_t len = sizeof(addrInfo);
|
|
|
|
return accept(socket, (struct sockaddr*) &addrInfo, &len);
|
2016-03-02 06:01:54 +00:00
|
|
|
#ifndef _3DS
|
2015-01-09 10:03:42 +00:00
|
|
|
} else {
|
|
|
|
struct sockaddr_in6 addrInfo;
|
|
|
|
memset(&addrInfo, 0, sizeof(addrInfo));
|
|
|
|
addrInfo.sin6_family = AF_INET6;
|
|
|
|
memcpy(addrInfo.sin6_addr.s6_addr, address->ipv6, sizeof(addrInfo.sin6_addr.s6_addr));
|
|
|
|
socklen_t len = sizeof(addrInfo);
|
|
|
|
return accept(socket, (struct sockaddr*) &addrInfo, &len);
|
2016-03-02 06:01:54 +00:00
|
|
|
#endif
|
2015-01-09 10:03:42 +00:00
|
|
|
}
|
2016-03-02 06:01:54 +00:00
|
|
|
return INVALID_SOCKET;
|
2014-02-04 07:01:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-16 08:50:15 +00:00
|
|
|
static inline int SocketSetBlocking(Socket socket, bool blocking) {
|
2014-02-04 07:01:26 +00:00
|
|
|
#ifdef _WIN32
|
2014-04-24 04:42:08 +00:00
|
|
|
u_long unblocking = !blocking;
|
|
|
|
return ioctlsocket(socket, FIONBIO, &unblocking) == NO_ERROR;
|
2014-02-04 07:01:26 +00:00
|
|
|
#else
|
|
|
|
int flags = fcntl(socket, F_GETFL);
|
|
|
|
if (flags == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (blocking) {
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
} else {
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
|
|
|
return fcntl(socket, F_SETFL, flags) >= 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-02-10 09:42:40 +00:00
|
|
|
static inline int SocketSetTCPPush(Socket socket, int push) {
|
|
|
|
return setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char*) &push, sizeof(int)) >= 0;
|
|
|
|
}
|
|
|
|
|
2015-02-15 08:31:57 +00:00
|
|
|
static inline int SocketPoll(size_t nSockets, Socket* reads, Socket* writes, Socket* errors, int64_t timeoutMillis) {
|
2015-01-19 10:34:36 +00:00
|
|
|
fd_set rset;
|
|
|
|
fd_set wset;
|
|
|
|
fd_set eset;
|
|
|
|
FD_ZERO(&rset);
|
|
|
|
FD_ZERO(&wset);
|
|
|
|
FD_ZERO(&eset);
|
2015-02-15 08:31:57 +00:00
|
|
|
size_t i;
|
2015-02-15 07:40:59 +00:00
|
|
|
Socket maxFd = 0;
|
2015-01-19 10:34:36 +00:00
|
|
|
if (reads) {
|
|
|
|
for (i = 0; i < nSockets; ++i) {
|
|
|
|
if (SOCKET_FAILED(reads[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (reads[i] > maxFd) {
|
|
|
|
maxFd = reads[i];
|
|
|
|
}
|
|
|
|
FD_SET(reads[i], &rset);
|
|
|
|
reads[i] = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (writes) {
|
|
|
|
for (i = 0; i < nSockets; ++i) {
|
|
|
|
if (SOCKET_FAILED(writes[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (writes[i] > maxFd) {
|
|
|
|
maxFd = writes[i];
|
|
|
|
}
|
|
|
|
FD_SET(writes[i], &wset);
|
|
|
|
writes[i] = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (errors) {
|
|
|
|
for (i = 0; i < nSockets; ++i) {
|
|
|
|
if (SOCKET_FAILED(errors[i])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (errors[i] > maxFd) {
|
|
|
|
maxFd = errors[i];
|
|
|
|
}
|
|
|
|
FD_SET(errors[i], &eset);
|
|
|
|
errors[i] = INVALID_SOCKET;
|
|
|
|
}
|
|
|
|
}
|
2017-02-07 21:19:45 +00:00
|
|
|
++maxFd;
|
2015-01-19 10:34:36 +00:00
|
|
|
struct timeval tv;
|
|
|
|
tv.tv_sec = timeoutMillis / 1000;
|
|
|
|
tv.tv_usec = (timeoutMillis % 1000) * 1000;
|
2017-02-07 21:19:45 +00:00
|
|
|
int result = select(maxFd, &rset, &wset, &eset, timeoutMillis < 0 ? 0 : &tv);
|
2015-01-19 10:34:36 +00:00
|
|
|
int r = 0;
|
|
|
|
int w = 0;
|
|
|
|
int e = 0;
|
2015-02-15 08:31:57 +00:00
|
|
|
Socket j;
|
|
|
|
for (j = 0; j < maxFd; ++j) {
|
|
|
|
if (reads && FD_ISSET(j, &rset)) {
|
|
|
|
reads[r] = j;
|
2015-01-19 10:34:36 +00:00
|
|
|
++r;
|
|
|
|
}
|
2015-02-15 08:31:57 +00:00
|
|
|
if (writes && FD_ISSET(j, &wset)) {
|
|
|
|
writes[w] = j;
|
2015-01-19 10:34:36 +00:00
|
|
|
++w;
|
|
|
|
}
|
2015-02-15 08:31:57 +00:00
|
|
|
if (errors && FD_ISSET(j, &eset)) {
|
|
|
|
errors[e] = j;
|
2015-01-19 10:34:36 +00:00
|
|
|
++e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-27 05:01:55 +00:00
|
|
|
CXX_GUARD_END
|
|
|
|
|
2014-02-04 07:01:26 +00:00
|
|
|
#endif
|