forked from kavika13/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcute_net.h
2005 lines (1722 loc) · 57.1 KB
/
cute_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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
cute_net.h - v0.0
*/
/*
docs, blah
*/
// TODO
// make asserts compile out in release
// audit the asserts and add in release mode if statements
#if !defined(CUTE_NET_H)
#define CUTE_NET_WINDOWS 1
#define CUTE_NET_MAC 2
#define CUTE_NET_UNIX 3
#if defined(_WIN32)
#define CUTE_NET_PLATFORM CUTE_NET_WINDOWS
#elif defined(__APPLE__)
#define CUTE_NET_PLATFORM CUTE_NET_MAC
#else
#define CUTE_NET_PLATFORM CUTE_NET_UNIX
#endif
const char* cn_error_reason;
#define CUTE_NET_RELIABLE_BYTE_COUNT 256
#define CUTE_NET_RELIABLE_WORD_COUNT (CUTE_NET_RELIABLE_BYTE_COUNT / sizeof(uint32_t))
// clang is a bitch and aggressively deletes *(int*) = 0
#if CUTE_NET_PLATFORM == CUTE_NET_MAC && defined(__clang__)
#define CUTE_NET_ASSERT_INTERNAL __builtin_trap()
#else
#define CUTE_NET_ASSERT_INTERNAL *(int*)0 = 0
#endif
#define CUTE_NET_CHECK(X, Y) do { if (!(X)) { cn_error_reason = Y; return 0; } } while (0)
#define CUTE_NET_ASSERT(X) do { if (!(X)) CUTE_NET_ASSERT_INTERNAL; } while (0)
#define CUTE_NET_ALIGN(X, Y) ((((size_t)X) + ((Y) - 1)) & ~((Y) - 1))
#define CUTE_NET_MAX_ADDRESS_LEN 256
#define CUTE_NET_PROTOCOL_ID 0xC883FC1D
#define CUTE_NET_MTU 1200
#define CUTE_NET_MTU_WORDCOUNT (CUTE_NET_MTU / sizeof(uint32_t))
#define CUTE_NET_PACKET_TYPE_BYTES 4
#define CUTE_NET_CRC_BYTES 4
#define CUTE_NET_PACKET_DATA_MAX_SIZE 1024
#define CUTE_NET_MIN(a, b) ((a) < (b) ? a : b)
#define CUTE_NET_MAX(a, b) ((a) > (b) ? a : b)
#define CUTE_NET_INT16_MAX ((uint16_t)32768)
#define CUTE_NET_UINT16_MAX ((uint16_t)~0)
// TODO: create macros to detect platform and setup as necessary
#define CUTE_NET_BIG_ENDIAN 0
#if CUTE_NET_BIG_ENDIAN
#define cn_endian(a) a = cn_swap_internal(a)
#else
#define cn_endian(a) a
#endif
#if CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
#define NOMINMAX
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS FUCK_YOU
#define CUTE_NET_SNPRINTF _snprintf
#include <winsock2.h> // socket
#include <ws2tcpip.h> // WSA stuff
#pragma comment(lib, "ws2_32.lib")
#elif CUTE_NET_PLATFORM == CUTE_NET_MAC || CUTE_NET_PLATFORM == CUTE_NET_UNIX
#include <sys/socket.h> // socket
#include <fcntl.h> // fcntl
#include <arpa/inet.h> // inet_pton
#include <unistd.h> // close
#include <errno.h>
#endif
// TODO
// add in preprocessor stuff to use stb_sprintf
#include <stdio.h> // printf (debug only), sprintf
#include <stdint.h>
#include <string.h> // memcpy, memset
#include <stdlib.h> // atoi
#if 1
#define CUTE_NET_DEBUG_PRINT(...) printf(__VA_ARGS__)
#else
#define CUTE_NET_DEBUG_PRINT(...)
#endif
uint16_t cn_swap_internal(uint16_t a)
{
return ((a & 0x00FF) << 8)
| ((a & 0xFF00) >> 8);
}
int16_t cn_swap_internal(int16_t a)
{
return ((a & 0x00FF) << 8)
| ((a & 0xFF00) >> 8);
}
uint32_t cn_swap_internal(uint32_t a)
{
return ((a & 0x000000FF) << 24)
| ((a & 0x0000FF00) << 8)
| ((a & 0x00FF0000) >> 8)
| ((a & 0xFF000000) >> 24);
}
int32_t cn_swap_internal(int32_t a)
{
return ((a & 0x000000FF) << 24)
| ((a & 0x0000FF00) << 8)
| ((a & 0x00FF0000) >> 8)
| ((a & 0xFF000000) >> 24);
}
union cn_u32_f32_t
{
uint32_t uval;
float fval;
};
union cn_u64_f64_t
{
uint64_t uval;
double fval;
};
float cn_swap_internal(float a)
{
cn_u32_f32_t u;
u.fval = a;
u.uval = cn_swap_internal(u.uval);
return u.fval;
}
uint32_t cn_pop_count(uint32_t x)
{
uint32_t a = x - ((x >> 1) & 0x55555555);
uint32_t b = (((a >> 2) & 0x33333333) + (a & 0x33333333));
uint32_t c = (((b >> 4) + b) & 0x0f0f0f0f);
uint32_t d = c + (c >> 8);
uint32_t e = d + (d >> 16);
uint32_t f = e & 0x0000003f;
return f;
}
uint32_t cn_log2(uint32_t x)
{
uint32_t a = x | (x >> 1);
uint32_t b = a | (a >> 2);
uint32_t c = b | (b >> 4);
uint32_t d = c | (c >> 8);
uint32_t e = d | (d >> 16);
uint32_t f = e >> 1;
return cn_pop_count(f);
}
uint32_t cn_bits_required(uint32_t min, uint32_t max)
{
return (min == max) ? 0 : cn_log2(max - min) + 1;
}
struct cn_buffer_t
{
uint64_t bits;
uint32_t count;
uint32_t* words;
uint32_t word_index;
int32_t bits_left;
int32_t bits_total;
};
cn_buffer_t cn_make_buffer(uint32_t* words, uint32_t word_count)
{
cn_buffer_t buffer;
buffer.bits = 0;
buffer.count = 0;
buffer.words = words;
buffer.word_index = 0;
buffer.bits_left = word_count * sizeof(uint32_t) * 8;
buffer.bits_total = buffer.bits_left;
return buffer;
}
size_t cn_size(cn_buffer_t* buffer)
{
return CUTE_NET_ALIGN(buffer->bits_total - buffer->bits_left, 32) / 8;
}
int cn_would_overflow(cn_buffer_t* buffer, uint32_t num_bits)
{
return buffer->bits_left - (int32_t)num_bits < 0;
}
uint32_t cn_read_bits_internal(cn_buffer_t* buffer, uint32_t num_bits_to_read)
{
CUTE_NET_ASSERT(num_bits_to_read <= 32);
CUTE_NET_ASSERT(num_bits_to_read > 0);
CUTE_NET_ASSERT(buffer->bits_left > 0);
CUTE_NET_ASSERT(buffer->count <= 64);
CUTE_NET_ASSERT(!cn_would_overflow(buffer, num_bits_to_read));
if (buffer->count < num_bits_to_read)
{
buffer->bits |= (uint64_t)(cn_endian(buffer->words[buffer->word_index])) << buffer->count;
buffer->count += 32;
buffer->word_index += 1;
}
CUTE_NET_ASSERT(buffer->count >= num_bits_to_read);
uint32_t bits = buffer->bits & (((uint64_t)1 << num_bits_to_read) - 1);
buffer->bits >>= num_bits_to_read;
buffer->count -= num_bits_to_read;
buffer->bits_left -= num_bits_to_read;
return bits;
}
void cn_write_bits(cn_buffer_t* buffer, uint32_t value, uint32_t num_bits_to_write)
{
CUTE_NET_ASSERT(buffer);
CUTE_NET_ASSERT(num_bits_to_write <= 32);
CUTE_NET_ASSERT(buffer->bits_left > 0);
CUTE_NET_ASSERT(buffer->count <= 32);
CUTE_NET_ASSERT(!cn_would_overflow(buffer, num_bits_to_write));
buffer->bits |= (uint64_t)(value & (((uint64_t)1 << num_bits_to_write) - 1)) << buffer->count;
buffer->count += num_bits_to_write;
buffer->bits_left -= num_bits_to_write;
if (buffer->count >= 32)
{
buffer->words[buffer->word_index] = cn_endian((uint32_t)(buffer->bits & ((uint32_t)~0)));
buffer->bits >>= 32;
buffer->count -= 32;
buffer->word_index += 1;
}
}
void cn_flush(cn_buffer_t* buffer)
{
CUTE_NET_ASSERT(buffer->count <= 32);
if (buffer->count)
{
buffer->words[buffer->word_index] = cn_endian((uint32_t)(buffer->bits & ((uint32_t)~0)));
}
}
static const uint32_t cn_crc32_table[256] = {
0x00000000,0x77073096,0xEE0E612C,0x990951BA,0x076DC419,0x706AF48F,0xE963A535,0x9E6495A3,
0x0EDB8832,0x79DCB8A4,0xE0D5E91E,0x97D2D988,0x09B64C2B,0x7EB17CBD,0xE7B82D07,0x90BF1D91,
0x1DB71064,0x6AB020F2,0xF3B97148,0x84BE41DE,0x1ADAD47D,0x6DDDE4EB,0xF4D4B551,0x83D385C7,
0x136C9856,0x646BA8C0,0xFD62F97A,0x8A65C9EC,0x14015C4F,0x63066CD9,0xFA0F3D63,0x8D080DF5,
0x3B6E20C8,0x4C69105E,0xD56041E4,0xA2677172,0x3C03E4D1,0x4B04D447,0xD20D85FD,0xA50AB56B,
0x35B5A8FA,0x42B2986C,0xDBBBC9D6,0xACBCF940,0x32D86CE3,0x45DF5C75,0xDCD60DCF,0xABD13D59,
0x26D930AC,0x51DE003A,0xC8D75180,0xBFD06116,0x21B4F4B5,0x56B3C423,0xCFBA9599,0xB8BDA50F,
0x2802B89E,0x5F058808,0xC60CD9B2,0xB10BE924,0x2F6F7C87,0x58684C11,0xC1611DAB,0xB6662D3D,
0x76DC4190,0x01DB7106,0x98D220BC,0xEFD5102A,0x71B18589,0x06B6B51F,0x9FBFE4A5,0xE8B8D433,
0x7807C9A2,0x0F00F934,0x9609A88E,0xE10E9818,0x7F6A0DBB,0x086D3D2D,0x91646C97,0xE6635C01,
0x6B6B51F4,0x1C6C6162,0x856530D8,0xF262004E,0x6C0695ED,0x1B01A57B,0x8208F4C1,0xF50FC457,
0x65B0D9C6,0x12B7E950,0x8BBEB8EA,0xFCB9887C,0x62DD1DDF,0x15DA2D49,0x8CD37CF3,0xFBD44C65,
0x4DB26158,0x3AB551CE,0xA3BC0074,0xD4BB30E2,0x4ADFA541,0x3DD895D7,0xA4D1C46D,0xD3D6F4FB,
0x4369E96A,0x346ED9FC,0xAD678846,0xDA60B8D0,0x44042D73,0x33031DE5,0xAA0A4C5F,0xDD0D7CC9,
0x5005713C,0x270241AA,0xBE0B1010,0xC90C2086,0x5768B525,0x206F85B3,0xB966D409,0xCE61E49F,
0x5EDEF90E,0x29D9C998,0xB0D09822,0xC7D7A8B4,0x59B33D17,0x2EB40D81,0xB7BD5C3B,0xC0BA6CAD,
0xEDB88320,0x9ABFB3B6,0x03B6E20C,0x74B1D29A,0xEAD54739,0x9DD277AF,0x04DB2615,0x73DC1683,
0xE3630B12,0x94643B84,0x0D6D6A3E,0x7A6A5AA8,0xE40ECF0B,0x9309FF9D,0x0A00AE27,0x7D079EB1,
0xF00F9344,0x8708A3D2,0x1E01F268,0x6906C2FE,0xF762575D,0x806567CB,0x196C3671,0x6E6B06E7,
0xFED41B76,0x89D32BE0,0x10DA7A5A,0x67DD4ACC,0xF9B9DF6F,0x8EBEEFF9,0x17B7BE43,0x60B08ED5,
0xD6D6A3E8,0xA1D1937E,0x38D8C2C4,0x4FDFF252,0xD1BB67F1,0xA6BC5767,0x3FB506DD,0x48B2364B,
0xD80D2BDA,0xAF0A1B4C,0x36034AF6,0x41047A60,0xDF60EFC3,0xA867DF55,0x316E8EEF,0x4669BE79,
0xCB61B38C,0xBC66831A,0x256FD2A0,0x5268E236,0xCC0C7795,0xBB0B4703,0x220216B9,0x5505262F,
0xC5BA3BBE,0xB2BD0B28,0x2BB45A92,0x5CB36A04,0xC2D7FFA7,0xB5D0CF31,0x2CD99E8B,0x5BDEAE1D,
0x9B64C2B0,0xEC63F226,0x756AA39C,0x026D930A,0x9C0906A9,0xEB0E363F,0x72076785,0x05005713,
0x95BF4A82,0xE2B87A14,0x7BB12BAE,0x0CB61B38,0x92D28E9B,0xE5D5BE0D,0x7CDCEFB7,0x0BDBDF21,
0x86D3D2D4,0xF1D4E242,0x68DDB3F8,0x1FDA836E,0x81BE16CD,0xF6B9265B,0x6FB077E1,0x18B74777,
0x88085AE6,0xFF0F6A70,0x66063BCA,0x11010B5C,0x8F659EFF,0xF862AE69,0x616BFFD3,0x166CCF45,
0xA00AE278,0xD70DD2EE,0x4E048354,0x3903B3C2,0xA7672661,0xD06016F7,0x4969474D,0x3E6E77DB,
0xAED16A4A,0xD9D65ADC,0x40DF0B66,0x37D83BF0,0xA9BCAE53,0xDEBB9EC5,0x47B2CF7F,0x30B5FFE9,
0xBDBDF21C,0xCABAC28A,0x53B39330,0x24B4A3A6,0xBAD03605,0xCDD70693,0x54DE5729,0x23D967BF,
0xB3667A2E,0xC4614AB8,0x5D681B02,0x2A6F2B94,0xB40BBE37,0xC30C8EA1,0x5A05DF1B,0x2D02EF8D
};
uint32_t cn_crc32(const void* memory, size_t bytes, uint32_t crc32)
{
uint8_t* buffer = (uint8_t*)memory;
crc32 = ~crc32;
for (size_t i = 0; i < bytes; ++i)
crc32 = (crc32 >> 8) ^ cn_crc32_table[(crc32 ^ buffer[i]) & 0xFF];
return ~crc32;
}
enum cn_address_type
{
CUTE_NET_ADDRESS_NONE,
CUTE_NET_ADDRESS_IPV4,
CUTE_NET_ADDRESS_IPV6
};
struct cn_address_t
{
cn_address_type type;
uint16_t port;
union
{
uint32_t ipv4;
uint16_t ipv6[8];
};
};
cn_address_t cn_make_address(uint32_t address, int16_t port)
{
cn_address_t addr;
addr.type = CUTE_NET_ADDRESS_IPV4;
addr.port = port;
addr.ipv4 = htonl(address);
return addr;
}
cn_address_t cn_make_address(int16_t port)
{
cn_address_t addr;
addr.type = CUTE_NET_ADDRESS_IPV4;
addr.port = port;
addr.ipv4 = htonl(INADDR_ANY);
return addr;
}
cn_address_t cn_make_address(uint8_t a, uint8_t b, uint8_t c, uint8_t d, int16_t port)
{
uint32_t ipv4 = (uint32_t)a | (uint32_t)b << 8 | (uint32_t)c << 16 | (uint32_t)d << 24;
return cn_make_address(ipv4, port);
}
cn_address_t cn_make_address(sockaddr_storage* sockaddr)
{
CUTE_NET_ASSERT(sockaddr);
cn_address_t addr;
switch (sockaddr->ss_family)
{
case AF_INET:
{
sockaddr_in* addr_ipv4 = (sockaddr_in*)sockaddr;
addr.type = CUTE_NET_ADDRESS_IPV4;
addr.port = ntohs(addr_ipv4->sin_port);
addr.ipv4 = addr_ipv4->sin_addr.s_addr;
} break;
case AF_INET6:
{
sockaddr_in6* addr_ipv6 = (sockaddr_in6*)sockaddr;
addr.type = CUTE_NET_ADDRESS_IPV6;
addr.port = ntohs(addr_ipv6->sin6_port);
memcpy(addr.ipv6, &addr_ipv6->sin6_addr, 16);
} break;
default: CUTE_NET_ASSERT(0);
}
return addr;
}
cn_address_t cn_make_address(const char* string)
{
CUTE_NET_ASSERT(string);
char memory[CUTE_NET_MAX_ADDRESS_LEN];
strncpy(memory, string, CUTE_NET_MAX_ADDRESS_LEN - 1);
memory[CUTE_NET_MAX_ADDRESS_LEN - 1] = 0;
char* buffer = memory;
cn_address_t address;
address.type = CUTE_NET_ADDRESS_NONE;
address.port = 0;
// ipv6 first
// handle [address]:port format first
// then try inet_pton
if (*buffer == '[')
{
buffer += 1;
char* search = buffer;
char c;
while ((c = *search++))
{
if (c == ']')
{
if (*search == ':')
{
address.port = (uint16_t)atoi(search + 1);
search[-1] = 0;
break;
}
}
}
}
in6_addr sockaddr6;
if (inet_pton(AF_INET6, buffer, &sockaddr6) == 1)
{
memcpy(address.ipv6, &sockaddr6, 16);
address.type = CUTE_NET_ADDRESS_IPV6;
return address;
}
// now try ipv4
// first handle format of "address:port"
// then try inet_pton
char* search = buffer;
char c;
while ((c = *search++))
{
if (c == ':')
{
address.port = (uint16_t)atoi(search);
search[-1] = 0;
break;
}
}
sockaddr_in sockaddr4;
if (inet_pton(AF_INET, buffer, &sockaddr4.sin_addr) == 1)
{
address.type = CUTE_NET_ADDRESS_IPV4;
address.ipv4 = sockaddr4.sin_addr.s_addr;
return address;
}
return address;
}
void cn_address_tString(cn_address_t address, char* buffer, int max_buffer_bytes)
{
switch (address.type)
{
case CUTE_NET_ADDRESS_IPV4:
{
uint8_t a = address.ipv4 & 0xFF;
uint8_t b = (address.ipv4 >> 8) & 0xFF;
uint8_t c = (address.ipv4 >> 16) & 0xFF;
uint8_t d = (address.ipv4 >> 24) & 0xFF;
if (address.port) CUTE_NET_SNPRINTF(buffer, max_buffer_bytes, "%d.%d.%d.%d:%d", a, b, c, d, address.port);
else CUTE_NET_SNPRINTF(buffer, max_buffer_bytes, "%d.%d.%d.%d", a, b, c, d);
} break;
case CUTE_NET_ADDRESS_IPV6:
{
if (address.port)
{
char inet6_addrstr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, (void*)address.ipv6, inet6_addrstr, INET6_ADDRSTRLEN);
CUTE_NET_SNPRINTF(buffer, max_buffer_bytes, "[%s]:%d", inet6_addrstr, address.port);
}
else inet_ntop(AF_INET6, (void*)address.ipv6, buffer, max_buffer_bytes);
} break;
default: CUTE_NET_ASSERT(0);
}
}
int cn_address_tEqu(cn_address_t a, cn_address_t b)
{
if (a.type != b.type) return 0;
if (a.port != b.port) return 0;
switch (a.type)
{
case CUTE_NET_ADDRESS_IPV4: if (a.ipv4 != b.ipv4) return 0; break;
case CUTE_NET_ADDRESS_IPV6: if (memcmp(a.ipv6, b.ipv6, sizeof(a.ipv6))) return 0; break;
default: CUTE_NET_ASSERT(0);
}
return 1;
}
#if CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
typedef SOCKET cn_socket_handle_t;
#else
typedef int cn_socket_handle_t;
#endif
enum cn_socket_tError
{
CUTE_NET_SOCKET_ERROR_NONE,
CUTE_NET_SOCKET_ERROR_MAKE_FAILED,
CUTE_NET_SOCKET_ERROR_SET_NON_BLOCKING_FAILED,
CUTE_NET_SOCKET_ERROR_SETSOCKOPT_IPV6_ONLY_FAILED,
CUTE_NET_SOCKET_ERROR_SETSOCKOPT_RCVBUF_FAILED,
CUTE_NET_SOCKET_ERROR_SETSOCKOPT_SNDBUF_FAILED,
CUTE_NET_SOCKET_ERROR_BIND_IPV4_FAILED,
CUTE_NET_SOCKET_ERROR_BIND_IPV6_FAILED,
CUTE_NET_SOCKET_ERROR_GETSOCKNAME_IPV4_FAILED,
CUTE_NET_SOCKET_ERROR_GETSOCKNAME_IPV6_FAILED
};
struct cn_socket_t
{
cn_socket_handle_t handle;
cn_address_t address;
cn_socket_tError error_code;
};
cn_socket_t cn_make_socket(cn_address_t address, int buffer_size, int true_for_nonblocking)
{
cn_socket_t socket;
socket.error_code = CUTE_NET_SOCKET_ERROR_NONE;
socket.handle = ::socket(address.type == CUTE_NET_ADDRESS_IPV6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#if CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
if (socket.handle == INVALID_SOCKET)
#else
if (socket.handle <= 0)
#endif
{
socket.error_code = CUTE_NET_SOCKET_ERROR_MAKE_FAILED;
return socket;
}
// allow users to enforce ipv6 only
// see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms738574(v=vs.85).aspx
if (address.type == CUTE_NET_ADDRESS_IPV6)
{
int enable = 1;
if (setsockopt(socket.handle, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&enable, sizeof(enable)))
{
socket.error_code = CUTE_NET_SOCKET_ERROR_SETSOCKOPT_IPV6_ONLY_FAILED;
return socket;
}
}
// set socket send/recieve buffer sizes to our chosen size
if (setsockopt(socket.handle, SOL_SOCKET, SO_RCVBUF, (char*)&buffer_size, sizeof(int)))
{
socket.error_code = CUTE_NET_SOCKET_ERROR_SETSOCKOPT_RCVBUF_FAILED;
return socket;
}
if (setsockopt(socket.handle, SOL_SOCKET, SO_SNDBUF, (char*)&buffer_size, sizeof(int)))
{
socket.error_code = CUTE_NET_SOCKET_ERROR_SETSOCKOPT_SNDBUF_FAILED;
return socket;
}
// bind port
switch (address.type)
{
case CUTE_NET_ADDRESS_IPV4:
{
sockaddr_in sock_address;
sock_address.sin_family = AF_INET;
sock_address.sin_addr.s_addr = address.ipv4;
sock_address.sin_port = htons(address.port);
if (bind(socket.handle, (const sockaddr*)&sock_address, sizeof(sock_address)) < 0)
{
socket.error_code = CUTE_NET_SOCKET_ERROR_BIND_IPV4_FAILED;
return socket;
}
} break;
case CUTE_NET_ADDRESS_IPV6:
{
sockaddr_in6 sock_address;
memset(&sock_address, 0, sizeof(sockaddr_in6));
sock_address.sin6_family = AF_INET6;
memcpy(&sock_address.sin6_addr, address.ipv6, sizeof(sock_address.sin6_addr));
sock_address.sin6_port = htons(address.port);
if (bind(socket.handle, (const sockaddr*)&sock_address, sizeof(sock_address)) < 0)
{
socket.error_code = CUTE_NET_SOCKET_ERROR_BIND_IPV6_FAILED;
return socket;
}
} break;
default: CUTE_NET_ASSERT(0);
}
// handle auto-picked ports
if (!address.port)
{
if (address.type == CUTE_NET_ADDRESS_IPV6)
{
struct sockaddr_in6 sin;
socklen_t len = sizeof(sin);
if (getsockname(socket.handle, (struct sockaddr*)&sin, &len) == -1)
{
socket.error_code = CUTE_NET_SOCKET_ERROR_GETSOCKNAME_IPV6_FAILED;
return socket;
}
address.port = ntohs(sin.sin6_port);
}
else
{
struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(socket.handle, (struct sockaddr*)&sin, &len) == -1)
{
socket.error_code = CUTE_NET_SOCKET_ERROR_GETSOCKNAME_IPV4_FAILED;
return socket;
}
address.port = ntohs(sin.sin_port);
}
}
socket.address = address;
// set blocking/non-blocking io
#if CUTE_NET_PLATFORM == CUTE_NET_MAC || CUTE_NET_PLATFORM == CUTE_NET_UNIX
int nonBlocking = true_for_nonblocking;
if (fcntl(socket.handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1)
{
socket.error_code = CUTE_NET_SOCKET_ERROR_SET_NON_BLOCKING_FAILED;
return socket;
}
#elif CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
DWORD nonBlocking = true_for_nonblocking;
if (ioctlsocket(socket.handle, FIONBIO, &nonBlocking) != 0)
{
socket.error_code = CUTE_NET_SOCKET_ERROR_SET_NON_BLOCKING_FAILED;
return socket;
}
#endif
return socket;
}
void cn_close_socket(cn_socket_t* socket)
{
if (socket->handle)
{
#if CUTE_NET_PLATFORM == CUTE_NET_MAC || CUTE_NET_PLATFORM == CUTE_NET_UNIX
close(socket->handle);
#elif CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
closesocket(socket->handle);
#endif
socket->handle = 0;
}
}
typedef void (cn_write_t)(cn_buffer_t* buffer, void* data);
typedef int (cn_read_t)(cn_buffer_t* buffer, void* data);
typedef int (cn_measure_t)();
struct cn_vtable_t
{
cn_write_t* Write;
cn_read_t* Read;
cn_measure_t* Measure;
int runtime_size;
};
struct cn_sim_packet
{
int size;
int64_t delay;
struct cn_transport_t* transport;
cn_sim_packet* next;
uint32_t words[CUTE_NET_MTU_WORDCOUNT];
};
struct cn_sim_t
{
int latency;
int jitter;
int drop;
int corruption;
int duplicates;
int duplicates_min;
int duplicates_max;
int pool_size;
cn_sim_packet* packets;
cn_sim_packet* free_list;
cn_sim_packet* live_packets;
};
struct cn_sim_def_t
{
int latency; // milliseconds, delay before sending packets
int jitter; // milliseconds, random value/sign from 0-jitter
int drop; // percent chance, 0-100, of dropping an outgoing packet
int corruption; // percent chance, 0-100, of corrupting outgoing packets
int duplicates; // percent chance, 0-100, of duplicating outgoing packets
int duplicates_min; // min of range of duplicate packet count
int duplicates_max; // max of range of duplicate packet count
int pool_size; // num of entries for internal pool to buffer outgoing packets
};
// OPTIMIZE
// Can remove this? Modify sequence buffer to handle NO DATA somehow
// perhaps return (void*)1 if sequence exists, keep data pointer 0
struct cn_incoming_packet_data_t
{
};
#define CUTE_NET_MAX_RELIABLES 64
#define CUTE_NET_MAX_RELIABLES_BITS_REQUIRED 7
struct cn_outgoing_packet_data_t
{
int acked;
int64_t send_time;
int count;
uint16_t ids[CUTE_NET_MAX_RELIABLES];
};
struct cn_reliable_data_t
{
int user_type;
uint32_t data[CUTE_NET_RELIABLE_WORD_COUNT];
};
#define CUTE_NET_SEQUENCE_BUFFER_SIZE 256
struct cn_sequence_buffer_t
{
uint16_t sequence;
uint32_t buffer[CUTE_NET_SEQUENCE_BUFFER_SIZE];
int stride;
char* data;
};
void cn_make_sequence_buffer(cn_sequence_buffer_t* buffer, int stride)
{
CUTE_NET_ASSERT(stride >= 0);
buffer->sequence = 0;
buffer->data = (char*)malloc(stride * CUTE_NET_SEQUENCE_BUFFER_SIZE);
buffer->stride = stride;
CUTE_NET_ASSERT(buffer->data);
memset(buffer->data, 0, stride * CUTE_NET_SEQUENCE_BUFFER_SIZE);
for (int i = 0; i < CUTE_NET_SEQUENCE_BUFFER_SIZE; ++i) buffer->buffer[i] = ~0;
}
void cn_free_sequence_buffer(cn_sequence_buffer_t* seq_buf)
{
free(seq_buf->data);
memset(seq_buf, 0, sizeof(cn_sequence_buffer_t));
}
void* cn_get_sequence_data(cn_sequence_buffer_t* seq_buf, uint16_t sequence)
{
int index = sequence % CUTE_NET_SEQUENCE_BUFFER_SIZE;
if (seq_buf->buffer[index] == sequence) return seq_buf->data + index * seq_buf->stride;
else return 0;
}
int cn_sequence_exists(cn_sequence_buffer_t* seq_buf, uint16_t sequence)
{
int index = sequence % CUTE_NET_SEQUENCE_BUFFER_SIZE;
return seq_buf->buffer[index] != ~0;
}
void cn_sequence_remove(cn_sequence_buffer_t* seq_buf, uint16_t sequence)
{
int index = sequence % CUTE_NET_SEQUENCE_BUFFER_SIZE;
seq_buf->buffer[index] = ~0;
}
int cn_more_recent(uint16_t a, uint16_t b)
{
int yes = (a > b) && (a - b <= CUTE_NET_INT16_MAX);
int yes_wrap = (a < b) && (b - a > CUTE_NET_INT16_MAX);
return yes || yes_wrap;
}
int cn_less_recent(uint16_t a, uint16_t b)
{
return cn_more_recent(b, a);
}
void cn_clear_entries(uint32_t* seq, int a, int b)
{
if (b < a) b += CUTE_NET_UINT16_MAX;
for (int i = a; i <= b; ++i) seq[i % CUTE_NET_SEQUENCE_BUFFER_SIZE] = ~0;
}
void* cn_insert_sequence(cn_sequence_buffer_t* seq_buf, uint16_t sequence)
{
if (cn_more_recent(sequence + 1, seq_buf->sequence))
{
cn_clear_entries(seq_buf->buffer, seq_buf->sequence, sequence);
seq_buf->sequence = sequence + 1;
}
else if (cn_more_recent(seq_buf->sequence - CUTE_NET_SEQUENCE_BUFFER_SIZE, sequence)) return 0;
int index = sequence % CUTE_NET_SEQUENCE_BUFFER_SIZE;
seq_buf->buffer[index] = sequence;
return seq_buf->data + index * seq_buf->stride;
}
void cn_make_ack(cn_sequence_buffer_t* seq, uint16_t* ack, uint32_t* ack_bits)
{
uint16_t local = seq->sequence - 1;
*ack = local;
uint32_t bits = 0;
for (int i = 0; i < 32; ++i)
{
uint16_t sequence = local - (uint16_t)i;
if (cn_get_sequence_data(seq, sequence)) bits |= (1 << i);
}
*ack_bits = bits;
}
struct cn_context_t
{
int vtable_count;
cn_vtable_t* vtables;
int use_sim;
int running;
cn_sim_t sim;
};
cn_vtable_t* cn_get_table(cn_context_t* ctx, int user_type)
{
CUTE_NET_ASSERT(user_type >= 0);
CUTE_NET_ASSERT(user_type < ctx->vtable_count);
return ctx->vtables + user_type;
}
#if CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
typedef struct cn_platform_t
{
CRITICAL_SECTION critical_section;
LARGE_INTEGER prev;
LARGE_INTEGER freq;
} cn_platform_t;
#elif CUTE_NET_PLATFORM == CUTE_NET_MAC
typedef struct cn_platform_t
{
pthread_t thread;
pthread_mutex_t mutex;
} cn_platform_t;
#endif
enum cn_queue_packet_status_t
{
CUTE_NET_QUEUE_EMPTY,
CUTE_NET_QUEUE_NOT_PROCESSED,
CUTE_NET_QUEUE_PROCESSED
};
typedef struct cn_queue_packet_t
{
cn_queue_packet_status_t state;
int64_t timestamp;
int size;
int user_type;
cn_address_t from;
uint32_t words[CUTE_NET_MTU_WORDCOUNT];
} cn_queue_packet_t;
#define CUTE_NET_QUEUE_CAPACITY 1024
typedef struct cn_queue_t
{
int insert_count;
int insert_index;
int process_count;
int process_index;
int pop_index;
cn_queue_packet_t packets[CUTE_NET_QUEUE_CAPACITY];
} cn_queue_t;
static int cn_pop(cn_queue_t* q, void* out, int64_t* ticks)
{
if (q->insert_count == CUTE_NET_QUEUE_CAPACITY) return 0;
cn_queue_packet_t* p = q->packets + q->pop_index;
if (p->state != CUTE_NET_QUEUE_PROCESSED) return 0;
memcpy(out, p->words, p->size);
*ticks = p->timestamp;
q->pop_index++;
q->pop_index %= CUTE_NET_QUEUE_CAPACITY;
q->insert_count++;
return p->size;
}
static int cn_push(cn_queue_t* q, void* data, int size, cn_address_t from, int64_t ticks)
{
if (size > CUTE_NET_MTU) return 0;
if (!q->insert_count) return 0;
CUTE_NET_ASSERT(q->insert_count > 0);
CUTE_NET_ASSERT(q->insert_count <= CUTE_NET_QUEUE_CAPACITY);
int index = q->insert_index++;
q->insert_index %= CUTE_NET_QUEUE_CAPACITY;
cn_queue_packet_t* p = q->packets + index;
p->state = CUTE_NET_QUEUE_NOT_PROCESSED;
p->timestamp = ticks;
p->size = size;
p->from = from;
memcpy(p->words, data, size);
q->insert_count--;
q->process_count++;
return 1;
}
static void cn_process_packet(cn_queue_packet_t* p)
{
// decrypt
// decompress
}
static int cn_process(cn_queue_t* q)
{
int did_work = 0;
while (q->process_count)
{
cn_queue_packet_t* p = q->packets + q->process_index;
cn_process_packet(p);
p->state = CUTE_NET_QUEUE_PROCESSED;
q->process_count--;
q->process_index++;
q->process_index %= CUTE_NET_QUEUE_CAPACITY;
did_work = 1;
}
return did_work;
}
struct cn_transport_t
{
const char* debug_name;
cn_context_t* ctx;
cn_socket_t socket;
cn_address_t to;
cn_sequence_buffer_t incoming;
cn_sequence_buffer_t outgoing;
uint16_t reliable_next_incoming;
uint16_t reliable_oldest_unacked;
cn_sequence_buffer_t reliable_incoming;
cn_sequence_buffer_t reliable_outgoing;
int64_t round_trip_time;
int round_trip_time_millis;
// worker thread data
int using_worker_thread;
int sleep_milliseconds;
cn_queue_t* q;
// platform data
cn_platform_t pd;
};
static void cn_add_queue(cn_transport_t* transport)
{
cn_queue_t* q = (cn_queue_t*)malloc(sizeof(cn_queue_t));
q->insert_count = CUTE_NET_QUEUE_CAPACITY;
q->insert_index = 0;
q->process_count = 0;
q->process_index = 0;
q->pop_index = 0;
for (int i = 0; i < CUTE_NET_QUEUE_CAPACITY; ++i)
{
cn_queue_packet_t* p = q->packets + i;
p->state = CUTE_NET_QUEUE_EMPTY;
}
transport->q = q;
}
int cn_do_work(cn_transport_t* transport);
#if CUTE_NET_PLATFORM == CUTE_NET_WINDOWS
void cn_sleep(int milliseconds)
{
Sleep(milliseconds);
}
static void cn_lock(cn_transport_t* transport)
{
if (transport->using_worker_thread) EnterCriticalSection(&transport->pd.critical_section);
}
static void cn_unlock(cn_transport_t* transport)
{
if (transport->using_worker_thread) LeaveCriticalSection(&transport->pd.critical_section);
}
static DWORD WINAPI cn_worker_thread(LPVOID lpParameter)
{
cn_transport_t* transport = (cn_transport_t*)lpParameter;