forked from lukaszle/zed_net
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathzed_net.h
566 lines (466 loc) · 16.4 KB
/
zed_net.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/////////////////////////////////////////////////////////////////////////////////////////
//
// zed_net - v0.21 - public domain networking library
// (inspired by the excellent stb libraries: https://github.com/nothings/stb)
//
// This library is intended primarily for use in games and provides a simple wrapper
// around BSD sockets (Winsock 2.2 on Windows). Sockets can be set to be blocking or
// non-blocking.
//
// Only UDP sockets are supported at this time, but this may later expand to include TCP.
//
// VERSION HISTORY
//
// 0.21 (14/01/2021) Win compilation fixes.
// 0.20 (7/28/2019) OSX compilation fixes.
// 0.19 (3/4/2016) TCP added and malloc/free calls removed.
// Not backwards compatible. - Ian T. Jacobsen (itjac.me)
// 0.18 (9/13/2015) minor polishing
// 0.17 (8/8/2015) initial release
//
// LICENSE
//
// This software is in the public domain. Where that dedication is not recognized, you
// are granted a perpetual, irrevocable license to copy, distribute, and modify this
// file as you see fit.
//
// USAGE
//
// #define the symbol ZED_NET_IMPLEMENTATION in *one* C/C++ file before the #include
// of this file; the implementation will be generated in that file.
//
// If you define the symbol ZED_NET_STATIC, then the implementation will be private to
// that file.
//
// Immediately after this block comment is the "header file" section. This section
// includes documentation for each API function.
//
#ifndef INCLUDE_ZED_NET_H
#define INCLUDE_ZED_NET_H
#ifdef ZED_NET_STATIC
#define ZED_NET_DEF static
#else
#define ZED_NET_DEF extern
#endif
#ifdef __cplusplus
extern "C" {
#endif
/////////////////////////////////////////////////////////////////////////////////////////
//
// INITIALIZATION AND SHUTDOWN
//
// Get a brief reason for failure
ZED_NET_DEF const char *zed_net_get_error(void);
// Perform platform-specific socket initialization;
// *must* be called before using any other function
//
// Returns 0 on success, -1 otherwise (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_init(void);
// Perform platform-specific socket de-initialization;
// *must* be called when finished using the other functions
ZED_NET_DEF void zed_net_shutdown(void);
/////////////////////////////////////////////////////////////////////////////////////////
//
// INTERNET ADDRESS API
//
// Represents an internet address usable by sockets
typedef struct {
unsigned int host;
unsigned short port;
} zed_net_address_t;
// Obtain an address from a host name and a port
//
// 'host' may contain a decimal formatted IP (such as "127.0.0.1"), a human readable
// name (such as "localhost"), or NULL for the default address
//
// Returns 0 on success, -1 otherwise (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_get_address(zed_net_address_t *address, const char *host, unsigned short port);
// Converts an address's host name into a decimal formatted string
//
// Returns NULL on failure (call 'zed_net_get_error' for more info)
ZED_NET_DEF const char *zed_net_host_to_str(unsigned int host);
/////////////////////////////////////////////////////////////////////////////////////////
//
// SOCKET HANDLE API
//
// Wraps the system handle for a UDP/TCP socket
typedef struct {
int handle;
unsigned long non_blocking;
int ready;
} zed_net_socket_t;
// Closes a previously opened socket
ZED_NET_DEF void zed_net_socket_close(zed_net_socket_t *socket);
/////////////////////////////////////////////////////////////////////////////////////////
//
// UDP SOCKETS API
//
// Opens a UDP socket and binds it to a specified port
// (use 0 to select a random open port)
//
// Socket will not block if 'non-blocking' is non-zero
//
// Returns 0 on success
// Returns -1 on failure (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_udp_socket_open(zed_net_socket_t *socket, unsigned int port, unsigned long non_blocking);
// Sends a specific amount of data to 'destination'
//
// Returns 0 on success, -1 otherwise (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_udp_socket_send(zed_net_socket_t *socket, zed_net_address_t destination, const void *data, int size);
// Receives a specific amount of data from 'sender'
//
// Returns the number of bytes received, -1 otherwise (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_udp_socket_receive(zed_net_socket_t *socket, zed_net_address_t *sender, void *data, int size);
/////////////////////////////////////////////////////////////////////////////////////////
//
// TCP SOCKETS API
//
// Opens a TCP socket and binds it to a specified port
// (use 0 to select a random open port)
//
// Socket will not block if 'non-blocking' is non-zero
//
// Returns NULL on failure (call 'zed_net_get_error' for more info)
// Socket will listen for incoming connections if 'listen_socket' is non-zero
// Returns 0 on success
// Returns -1 on failure (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_tcp_socket_open(zed_net_socket_t *socket, unsigned int port, unsigned long non_blocking, int listen_socket);
// Connect to a remote endpoint
// Returns 0 on success.
// if the socket is non-blocking, then this can return 1 if the socket isn't ready
// returns -1 otherwise. (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_tcp_connect(zed_net_socket_t *socket, zed_net_address_t remote_addr);
// Accept connection
// New remote_socket inherits non-blocking from listening_socket
// Returns 0 on success.
// if the socket is non-blocking, then this can return 1 if the socket isn't ready
// if the socket is non_blocking and there was no connection to accept, returns 2
// returns -1 otherwise. (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_tcp_accept(zed_net_socket_t *listening_socket, zed_net_socket_t *remote_socket, zed_net_address_t *remote_addr);
// Returns 0 on success.
// if the socket is non-blocking, then this can return 1 if the socket isn't ready
// returns -1 otherwise. (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_tcp_socket_send(zed_net_socket_t *remote_socket, const void *data, int size);
// Returns 0 on success.
// if the socket is non-blocking, then this can return 1 if the socket isn't ready
// returns -1 otherwise. (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_tcp_socket_receive(zed_net_socket_t *remote_socket, void *data, int size);
// Blocks until the TCP socket is ready. Only makes sense for non-blocking socket.
// Returns 0 on success.
// returns -1 otherwise. (call 'zed_net_get_error' for more info)
ZED_NET_DEF int zed_net_tcp_make_socket_ready(zed_net_socket_t *socket);
#ifdef __cplusplus
}
#endif
#endif // INCLUDE_ZED_NET_H
#ifdef ZED_NET_IMPLEMENTATION
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef _WIN32
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <WinSock2.h>
#pragma comment(lib, "wsock32.lib")
#define ZED_NET_SOCKET_ERROR SOCKET_ERROR
#define ZED_NET_INVALID_SOCKET INVALID_SOCKET
#else
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#define ZED_NET_SOCKET_ERROR -1
#define ZED_NET_INVALID_SOCKET -1
#endif
static const char *zed_net__g_error;
static int zed_net__error(const char *message) {
zed_net__g_error = message;
return -1;
}
ZED_NET_DEF const char *zed_net_get_error(void) {
return zed_net__g_error;
}
ZED_NET_DEF int zed_net_init(void) {
#ifdef _WIN32
WSADATA wsa_data;
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
{
return zed_net__error("Windows Sockets failed to start");
}
return 0;
#else
return 0;
#endif
}
ZED_NET_DEF void zed_net_shutdown(void) {
#ifdef _WIN32
WSACleanup();
#endif
}
ZED_NET_DEF int zed_net_get_address(zed_net_address_t *address, const char *host, unsigned short port) {
if (host == NULL) {
address->host = INADDR_ANY;
} else {
address->host = inet_addr(host);
if (address->host == INADDR_NONE) {
struct hostent *hostent = gethostbyname(host);
if (hostent) {
memcpy(&address->host, hostent->h_addr, hostent->h_length);
} else {
return zed_net__error("Invalid host name");
}
}
}
address->port = port;
return 0;
}
ZED_NET_DEF const char *zed_net_host_to_str(unsigned int host) {
struct in_addr in;
in.s_addr = host;
return inet_ntoa(in);
}
ZED_NET_DEF int zed_net_udp_socket_open(zed_net_socket_t *sock, unsigned int port, unsigned long non_blocking) {
if (!sock)
return zed_net__error("Socket is NULL");
// Create the socket
sock->handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock->handle <= 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to create socket");
}
// Bind the socket to the port
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(sock->handle, (const struct sockaddr *) &address, sizeof(struct sockaddr_in)) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to bind socket");
}
// Set the socket to non-blocking if neccessary
if (non_blocking) {
#ifdef _WIN32
if (ioctlsocket(sock->handle, FIONBIO, &non_blocking) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to set socket to non-blocking");
}
#else
if (fcntl(sock->handle, F_SETFL, O_NONBLOCK, non_blocking) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to set socket to non-blocking");
}
#endif
}
sock->non_blocking = non_blocking;
return 0;
}
ZED_NET_DEF int zed_net_tcp_socket_open(zed_net_socket_t *sock, unsigned int port, unsigned long non_blocking, int listen_socket) {
if (!sock)
return zed_net__error("Socket is NULL");
// Create the socket
sock->handle = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock->handle <= 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to create socket");
}
// Bind the socket to the port
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(port);
if (bind(sock->handle, (const struct sockaddr *) &address, sizeof(struct sockaddr_in)) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to bind socket");
}
// Set the socket to non-blocking if neccessary
if (non_blocking) {
#ifdef _WIN32
if (ioctlsocket(sock->handle, FIONBIO, &non_blocking) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to set socket to non-blocking");
}
#else
if (fcntl(sock->handle, F_SETFL, O_NONBLOCK, non_blocking) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed to set socket to non-blocking");
}
#endif
sock->ready = 0;
}
if (listen_socket) {
#ifndef SOMAXCONN
#define SOMAXCONN 10
#endif
if (listen(sock->handle, SOMAXCONN) != 0) {
zed_net_socket_close(sock);
return zed_net__error("Failed make socket listen");
}
}
sock->non_blocking = non_blocking;
return 0;
}
// Returns 1 if it would block, <0 if there's an error.
ZED_NET_DEF int zed_net_check_would_block(zed_net_socket_t *socket) {
struct timeval timer;
fd_set writefd;
int retval;
if (socket->non_blocking && !socket->ready) {
FD_ZERO(&writefd);
FD_SET(socket->handle, &writefd);
timer.tv_sec = 0;
timer.tv_usec = 0;
retval = select(0, NULL, &writefd, NULL, &timer);
if (retval == 0)
return 1;
else if (retval == ZED_NET_SOCKET_ERROR) {
zed_net_socket_close(socket);
return zed_net__error("Got socket error from select()");
}
socket->ready = 1;
}
return 0;
}
ZED_NET_DEF int zed_net_tcp_make_socket_ready(zed_net_socket_t *socket) {
if (!socket->non_blocking)
return 0;
if (socket->ready)
return 0;
fd_set writefd;
int retval;
FD_ZERO(&writefd);
FD_SET(socket->handle, &writefd);
retval = select(0, NULL, &writefd, NULL, NULL);
if (retval != 1)
return zed_net__error("Failed to make non-blocking socket ready");
socket->ready = 1;
return 0;
}
ZED_NET_DEF int zed_net_tcp_connect(zed_net_socket_t *socket, zed_net_address_t remote_addr) {
struct sockaddr_in address;
int retval;
if (!socket)
return zed_net__error("Socket is NULL");
retval = zed_net_check_would_block(socket);
if (retval == 1)
return 1;
else if (retval)
return -1;
address.sin_family = AF_INET;
address.sin_addr.s_addr = remote_addr.host;
address.sin_port = htons(remote_addr.port);
retval = connect(socket->handle, (const struct sockaddr *) &address, sizeof(address));
if (retval == ZED_NET_SOCKET_ERROR) {
zed_net_socket_close(socket);
return zed_net__error("Failed to connect socket");
}
return 0;
}
ZED_NET_DEF int zed_net_tcp_accept(zed_net_socket_t *listening_socket, zed_net_socket_t *remote_socket, zed_net_address_t *remote_addr) {
struct sockaddr_in address;
int retval, handle;
if (!listening_socket)
return zed_net__error("Listening socket is NULL");
if (!remote_socket)
return zed_net__error("Remote socket is NULL");
if (!remote_addr)
return zed_net__error("Address pointer is NULL");
retval = zed_net_check_would_block(listening_socket);
if (retval == 1)
return 1;
else if (retval)
return -1;
#ifdef _WIN32
typedef int socklen_t;
#endif
socklen_t addrlen = sizeof(address);
handle = accept(listening_socket->handle, (struct sockaddr *)&address, &addrlen);
if (handle == ZED_NET_INVALID_SOCKET)
return 2;
remote_addr->host = address.sin_addr.s_addr;
remote_addr->port = ntohs(address.sin_port);
remote_socket->non_blocking = listening_socket->non_blocking;
remote_socket->ready = 0;
remote_socket->handle = handle;
return 0;
}
ZED_NET_DEF void zed_net_socket_close(zed_net_socket_t *socket) {
if (!socket) {
return;
}
if (socket->handle) {
#ifdef _WIN32
closesocket(socket->handle);
#else
close(socket->handle);
#endif
}
}
ZED_NET_DEF int zed_net_udp_socket_send(zed_net_socket_t *socket, zed_net_address_t destination, const void *data, int size) {
if (!socket) {
return zed_net__error("Socket is NULL");
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = destination.host;
address.sin_port = htons(destination.port);
int sent_bytes = sendto(socket->handle, (const char *) data, size, 0, (const struct sockaddr *) &address, sizeof(struct sockaddr_in));
if (sent_bytes != size) {
return zed_net__error("Failed to send data");
}
return 0;
}
ZED_NET_DEF int zed_net_udp_socket_receive(zed_net_socket_t *socket, zed_net_address_t *sender, void *data, int size) {
if (!socket) {
return zed_net__error("Socket is NULL");
}
#ifdef _WIN32
typedef int socklen_t;
#endif
struct sockaddr_in from;
socklen_t from_length = sizeof(from);
int received_bytes = recvfrom(socket->handle, (char *) data, size, 0, (struct sockaddr *) &from, &from_length);
if (received_bytes <= 0) {
return 0;
}
sender->host = from.sin_addr.s_addr;
sender->port = ntohs(from.sin_port);
return received_bytes;
}
ZED_NET_DEF int zed_net_tcp_socket_send(zed_net_socket_t *remote_socket, const void *data, int size) {
int retval;
if (!remote_socket) {
return zed_net__error("Socket is NULL");
}
retval = zed_net_check_would_block(remote_socket);
if (retval == 1)
return 1;
else if (retval)
return -1;
int sent_bytes = send(remote_socket->handle, (const char *) data, size, 0);
if (sent_bytes != size) {
return zed_net__error("Failed to send data");
}
return 0;
}
ZED_NET_DEF int zed_net_tcp_socket_receive(zed_net_socket_t *remote_socket, void *data, int size) {
int retval;
if (!remote_socket) {
return zed_net__error("Socket is NULL");
}
retval = zed_net_check_would_block(remote_socket);
if (retval == 1)
return 1;
else if (retval)
return -1;
#ifdef _WIN32
typedef int socklen_t;
#endif
int received_bytes = recv(remote_socket->handle, (char *) data, size, 0);
if (received_bytes <= 0) {
return 0;
}
return received_bytes;
}
#endif // ZED_NET_IMPLEMENTATION
// vim: tabstop=4 shiftwidth=4 expandtab