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 <errno.h>
23#include <fcntl.h>
24#include <netinet/in.h>
25#include <netinet/tcp.h>
26#include <sys/select.h>
27#include <sys/socket.h>
28
29#define INVALID_SOCKET (-1)
30#define SOCKET_FAILED(s) ((s) < 0)
31typedef int Socket;
32#endif
33
34enum IP {
35 IPV4,
36 IPV6
37};
38
39struct Address {
40 enum IP version;
41 union {
42 uint32_t ipv4;
43 uint8_t ipv6[16];
44 };
45};
46
47static inline void SocketSubsystemInit() {
48#ifdef _WIN32
49 WSADATA data;
50 WSAStartup(MAKEWORD(2, 2), &data);
51#endif
52}
53
54static inline int SocketError() {
55#ifdef _WIN32
56 return WSAGetLastError();
57#else
58 return errno;
59#endif
60}
61
62static inline bool SocketWouldBlock() {
63#ifdef _WIN32
64 return SocketError() == WSAEWOULDBLOCK;
65#else
66 return SocketError() == EWOULDBLOCK || SocketError() == EAGAIN;
67#endif
68}
69
70static inline ssize_t SocketSend(Socket socket, const void* buffer, size_t size) {
71#ifdef _WIN32
72 return send(socket, (const char*) buffer, size, 0);
73#else
74 return write(socket, buffer, size);
75#endif
76}
77
78static inline ssize_t SocketRecv(Socket socket, void* buffer, size_t size) {
79#ifdef _WIN32
80 return recv(socket, (char*) buffer, size, 0);
81#else
82 return read(socket, buffer, size);
83#endif
84}
85
86static inline Socket SocketOpenTCP(int port, const struct Address* bindAddress) {
87 Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
88 if (SOCKET_FAILED(sock)) {
89 return sock;
90 }
91
92 int err;
93 if (!bindAddress) {
94 struct sockaddr_in bindInfo;
95 memset(&bindInfo, 0, sizeof(bindInfo));
96 bindInfo.sin_family = AF_INET;
97 bindInfo.sin_port = htons(port);
98 err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
99 } else if (bindAddress->version == IPV4) {
100 struct sockaddr_in bindInfo;
101 memset(&bindInfo, 0, sizeof(bindInfo));
102 bindInfo.sin_family = AF_INET;
103 bindInfo.sin_port = htons(port);
104 bindInfo.sin_addr.s_addr = bindAddress->ipv4;
105 err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
106 } else {
107 struct sockaddr_in6 bindInfo;
108 memset(&bindInfo, 0, sizeof(bindInfo));
109 bindInfo.sin6_family = AF_INET6;
110 bindInfo.sin6_port = htons(port);
111 memcpy(bindInfo.sin6_addr.s6_addr, bindAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
112 err = bind(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
113
114 }
115 if (err) {
116 close(sock);
117 return INVALID_SOCKET;
118 }
119 return sock;
120}
121
122static inline Socket SocketConnectTCP(int port, const struct Address* destinationAddress) {
123 Socket sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
124 if (SOCKET_FAILED(sock)) {
125 return sock;
126 }
127
128 int err;
129 if (!destinationAddress) {
130 struct sockaddr_in bindInfo;
131 memset(&bindInfo, 0, sizeof(bindInfo));
132 bindInfo.sin_family = AF_INET;
133 bindInfo.sin_port = htons(port);
134 err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
135 } else if (destinationAddress->version == IPV4) {
136 struct sockaddr_in bindInfo;
137 memset(&bindInfo, 0, sizeof(bindInfo));
138 bindInfo.sin_family = AF_INET;
139 bindInfo.sin_port = htons(port);
140 bindInfo.sin_addr.s_addr = destinationAddress->ipv4;
141 err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
142 } else {
143 struct sockaddr_in6 bindInfo;
144 memset(&bindInfo, 0, sizeof(bindInfo));
145 bindInfo.sin6_family = AF_INET6;
146 bindInfo.sin6_port = htons(port);
147 memcpy(bindInfo.sin6_addr.s6_addr, destinationAddress->ipv6, sizeof(bindInfo.sin6_addr.s6_addr));
148 err = connect(sock, (const struct sockaddr*) &bindInfo, sizeof(bindInfo));
149 }
150
151 if (err) {
152 close(sock);
153 return INVALID_SOCKET;
154 }
155 return sock;
156}
157
158static inline Socket SocketListen(Socket socket, int queueLength) {
159 return listen(socket, queueLength);
160}
161
162static inline Socket SocketAccept(Socket socket, struct Address* address) {
163 if (!address) {
164 return accept(socket, 0, 0);
165 }
166 if (address->version == IPV4) {
167 struct sockaddr_in addrInfo;
168 memset(&addrInfo, 0, sizeof(addrInfo));
169 addrInfo.sin_family = AF_INET;
170 addrInfo.sin_addr.s_addr = address->ipv4;
171 socklen_t len = sizeof(addrInfo);
172 return accept(socket, (struct sockaddr*) &addrInfo, &len);
173 } else {
174 struct sockaddr_in6 addrInfo;
175 memset(&addrInfo, 0, sizeof(addrInfo));
176 addrInfo.sin6_family = AF_INET6;
177 memcpy(addrInfo.sin6_addr.s6_addr, address->ipv6, sizeof(addrInfo.sin6_addr.s6_addr));
178 socklen_t len = sizeof(addrInfo);
179 return accept(socket, (struct sockaddr*) &addrInfo, &len);
180 }
181}
182
183static inline int SocketClose(Socket socket) {
184 return close(socket) >= 0;
185}
186
187static inline int SocketSetBlocking(Socket socket, bool blocking) {
188#ifdef _WIN32
189 u_long unblocking = !blocking;
190 return ioctlsocket(socket, FIONBIO, &unblocking) == NO_ERROR;
191#else
192 int flags = fcntl(socket, F_GETFL);
193 if (flags == -1) {
194 return 0;
195 }
196 if (blocking) {
197 flags &= ~O_NONBLOCK;
198 } else {
199 flags |= O_NONBLOCK;
200 }
201 return fcntl(socket, F_SETFL, flags) >= 0;
202#endif
203}
204
205static inline int SocketSetTCPPush(Socket socket, int push) {
206 return setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, (char*) &push, sizeof(int)) >= 0;
207}
208
209static inline int SocketPoll(size_t nSockets, Socket* reads, Socket* writes, Socket* errors, int64_t timeoutMillis) {
210 fd_set rset;
211 fd_set wset;
212 fd_set eset;
213 FD_ZERO(&rset);
214 FD_ZERO(&wset);
215 FD_ZERO(&eset);
216 size_t i;
217 Socket maxFd = 0;
218 if (reads) {
219 for (i = 0; i < nSockets; ++i) {
220 if (SOCKET_FAILED(reads[i])) {
221 break;
222 }
223 if (reads[i] > maxFd) {
224 maxFd = reads[i];
225 }
226 FD_SET(reads[i], &rset);
227 reads[i] = INVALID_SOCKET;
228 }
229 }
230 if (writes) {
231 for (i = 0; i < nSockets; ++i) {
232 if (SOCKET_FAILED(writes[i])) {
233 break;
234 }
235 if (writes[i] > maxFd) {
236 maxFd = writes[i];
237 }
238 FD_SET(writes[i], &wset);
239 writes[i] = INVALID_SOCKET;
240 }
241 }
242 if (errors) {
243 for (i = 0; i < nSockets; ++i) {
244 if (SOCKET_FAILED(errors[i])) {
245 break;
246 }
247 if (errors[i] > maxFd) {
248 maxFd = errors[i];
249 }
250 FD_SET(errors[i], &eset);
251 errors[i] = INVALID_SOCKET;
252 }
253 }
254 struct timeval tv;
255 tv.tv_sec = timeoutMillis / 1000;
256 tv.tv_usec = (timeoutMillis % 1000) * 1000;
257 int result = select(maxFd + 1, &rset, &wset, &eset, timeoutMillis < 0 ? 0 : &tv);
258 int r = 0;
259 int w = 0;
260 int e = 0;
261 Socket j;
262 for (j = 0; j < maxFd; ++j) {
263 if (reads && FD_ISSET(j, &rset)) {
264 reads[r] = j;
265 ++r;
266 }
267 if (writes && FD_ISSET(j, &wset)) {
268 writes[w] = j;
269 ++w;
270 }
271 if (errors && FD_ISSET(j, &eset)) {
272 errors[e] = j;
273 ++e;
274 }
275 }
276 return result;
277}
278
279#endif