wangrong
2025-03-19 291afb9c81aae1d15d9e3edcea618d0a2e1fc5a6
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
/*
 * MIT License
 *
 * Copyright (c) 2018-2020 Arthur Brainville (Ybalrid) and with the help of
 * Comunity Contributors!
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * INTRODUCTION
 * ============
 *
 * Kissnet is a simple C++17 layer around the raw OS provided socket API to be
 * used on IP networks with the TCP and UDP protocols.
 *
 * Kissnet is not a networking framework, and it will not process your data or
 * assist you in any way. Kissnet's only goal is to provide a simple API to send
 * and receive bytes,
 * without having to play around with a bunch of structure, file descriptors,
 * handles and pointers given to a C-style API. The other goal of kissnet is to
 * provide an API that will works in a cross platform setting.
 *
 * Kissnet will automatically manage the eventual startup/shutdown of the
 * library needed to perform socket operations on a particular platform. (e.g.
 * the Windows Socket API on MS-Windows.
 *
 * Kissnet leverages (and expect you to do so), multiple features from C++17,
 * including: std::byte, if constexpr, structured bindings, if-initializer and
 * template parameter type deduction.
 *
 * The library is structured across 4 exposed data types:
 *
 *  - buffer<size_t> : a static array of std::byte implemented via std::array.
 *  This is what you should use to hold raw data you are getting from a socket,
 *  before extracting what you need from the bytes
 *  - port_t : a 16 bit unsigned number. Represent a network port number
 *  - endpoint : a structure that represent a location where you need to connect
 *  to. Contains a hostname (as std::string) and a port number (as port_t)
 *  - socket<protocol> : a templated class that represents an ipv4 or ipv6 socket.
 *  Protocol is either TCP or UDP
 *
 * Kissnet does error handling in 2 ways:
 *
 *  1:
 *  When an operation can generate an error that the user should handle by hand
 *  anyway, a tuple containing the expected type returned, and an object that
 *  represent the status of what happens is returned.
 *
 *  For example, socket send/receive operation can discover that the connection
 *  was closed, or was shut down properly. It could also be the fact that a
 *  socket was configured "non blocking" and would have blocked in this
 *  situation. On both occasion, these methods will return the fact that 0 bytes
 *  came across as the transaction size, and the status will indicate either an
 *  error (socket no longer valid), or an actual status message (connection
 *  closed, socket would have blocked)
 *
 *  These status objects will behave like a const bool that equals "false" when
 *  an error occurred, and "true" when it's just a status notification
 *
 *  2:
 *  Fatal errors are by default handled by throwing a runtime_error exception.
 *  But, for many reasons, you may want to
 *  not use exceptions entirely.
 *
 *  kissnet give you some facilities to get fatal errors information back, and
 *  to choose how to handle it. Kissnet give you a few levers you can use:
 *
 *  - You can deactivate the exception support by #defining KISSNET_NO_EXCEP
 *  before #including kissnet.hpp. Instead, kissnet will use a function based
 *  error handler
 *  - By default, the error handler prints to stderr the error message, and
 *  abort the program
 *  - kissnet::error::callback is a function pointer that gets a string, and a
 *  context pointer. The string is the error message, and the context pointer
 * what ever you gave kissnet for the occasion. This is a global pointer that
 * you can set as you want. This will override the "print to stderr" behavior
 * at fatal error time.
 *  - kissnet::error::ctx is a void*, this will be passed to your error handler
 *  as a "context" pointer. If you need your handler to write to a log,
 *  or to turn on the HTCPCP enabled teapot on John's desk, you can.
 *  - kissnet::abortOnFatalError is a boolean that will control the call to
 *  abort(). This is independent to the fact that you did set or not an error
 *  callback. please note that any object involved with the operation that
 * triggered the fatal error is probably in an invalid state, and probably
 * deserve to be thrown away.
 */
 
#ifndef KISS_NET
#define KISS_NET
 
///Define this to not use exceptions
#ifndef KISSNET_NO_EXCEP
#define kissnet_fatal_error(STR) throw std::runtime_error(STR)
#else
#define kissnet_fatal_error(STR) kissnet::error::handle(STR);
#endif
 
#include <array>
#include <memory>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <cassert>
#include <stdexcept>
#include <string>
#include <utility>
 
#ifdef _WIN32
 
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define WIN32_LEAN_AND_MEAN
 
#ifndef NOMINMAX
#define NOMINMAX
#endif //endif nominmax
 
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
 
using ioctl_setting = u_long;
using buffsize_t    = int;
 
#define AI_ADDRCONFIG 0x00000400
 
#ifndef SHUT_RDWR
#define SHUT_RDWR SD_BOTH
#endif
 
// taken from: https://github.com/rxi/dyad/blob/915ae4939529b9aaaf6ebfd2f65c6cff45fc0eac/src/dyad.c#L58
inline const char* inet_ntop(int af, const void* src, char* dst, socklen_t size)
{
    union
    {
        struct sockaddr sa;
        struct sockaddr_in sai;
        struct sockaddr_in6 sai6;
    } addr;
    int res;
    memset(&addr, 0, sizeof(addr));
    addr.sa.sa_family = af;
    if (af == AF_INET6)
    {
        memcpy(&addr.sai6.sin6_addr, src, sizeof(addr.sai6.sin6_addr));
    }
    else
    {
        memcpy(&addr.sai.sin_addr, src, sizeof(addr.sai.sin_addr));
    }
    res = WSAAddressToStringA(&addr.sa, sizeof(addr), 0, dst, reinterpret_cast<LPDWORD>(&size));
    if (res != 0) return NULL;
    return dst;
}
 
//Handle WinSock2/Windows Socket API initialization and cleanup
#pragma comment(lib, "Ws2_32.lib")
namespace kissnet
{
 
    namespace win32_specific
    {
        ///Forward declare the object that will permit to manage the WSAStartup/Cleanup automatically
        struct WSA;
 
        ///Enclose the global pointer in this namespace. Only use this inside a shared_ptr
        namespace internal_state
        {
            static WSA* global_WSA = nullptr;
        }
 
        ///WSA object. Only to be constructed with std::make_shared()
        struct WSA : std::enable_shared_from_this<WSA>
        {
            //For safety, only initialize Windows Socket API once, and delete it once
            ///Prevent copy construct
            WSA(const WSA&) = delete;
            ///Prevent copy assignment
            WSA& operator=(const WSA&) = delete;
            ///Prevent moving
            WSA(WSA&&) = delete;
            ///Prevent move assignment
            WSA& operator=(WSA&&) = delete;
 
            ///data storage
            WSADATA wsa_data;
 
            ///Startup
            WSA() :
             wsa_data {}
            {
                if (const auto status = WSAStartup(MAKEWORD(2, 2), &wsa_data); status != 0)
                {
                    std::string error_message;
                    switch (status) // https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup#return-value
                    {
                        default:
                            error_message = "Unknown error happened.";
                            break;
                        case WSASYSNOTREADY:
                            error_message = "The underlying network subsystem is not ready for network communication.";
                            break;
                        case WSAVERNOTSUPPORTED: //unlikely, we specify 2.2!
                            error_message = " The version of Windows Sockets support requested "
                                            "(2.2)" //we know here the version was 2.2, add that to the error message copied from MSDN
                                            " is not provided by this particular Windows Sockets implementation. ";
                            break;
                        case WSAEINPROGRESS:
                            error_message = "A blocking Windows Sockets 1.1 operation is in progress.";
                            break;
                        case WSAEPROCLIM:
                            error_message = "A limit on the number of tasks supported by the Windows Sockets implementation has been reached.";
                            break;
                        case WSAEFAULT: //unlikely, if this ctor is running, wsa_data is part of this object's "stack" data
                            error_message = "The lpWSAData parameter is not a valid pointer.";
                            break;
                    }
                    kissnet_fatal_error(error_message);
                }
#ifdef KISSNET_WSA_DEBUG
                std::cerr << "Initialized Windows Socket API\n";
#endif
            }
 
            ///Cleanup
            ~WSA()
            {
                WSACleanup();
                internal_state::global_WSA = nullptr;
#ifdef KISSNET_WSA_DEBUG
                std::cerr << "Cleanup Windows Socket API\n";
#endif
            }
 
            ///get the shared pointer
            std::shared_ptr<WSA> getPtr()
            {
                return shared_from_this();
            }
        };
 
        ///Get-or-create the global pointer
        inline std::shared_ptr<WSA> getWSA()
        {
            //If it has been created already:
            if (internal_state::global_WSA)
                return internal_state::global_WSA->getPtr(); //fetch the smart pointer from the naked pointer
 
            //Create in wsa
            auto wsa = std::make_shared<WSA>();
 
            //Save the raw address in the global state
            internal_state::global_WSA = wsa.get();
 
            //Return the smart pointer
            return wsa;
        }
    }
 
#define KISSNET_OS_SPECIFIC_PAYLOAD_NAME wsa_ptr
#define KISSNET_OS_SPECIFIC std::shared_ptr<kissnet::win32_specific::WSA> KISSNET_OS_SPECIFIC_PAYLOAD_NAME
#define KISSNET_OS_INIT KISSNET_OS_SPECIFIC_PAYLOAD_NAME = kissnet::win32_specific::getWSA()
 
    ///Return the last error code
    inline int get_error_code()
    {
        const auto error = WSAGetLastError();
 
        //We need to posixify the values that we are actually using inside this header.
        switch (error)
        {
            case WSAEWOULDBLOCK:
                return EWOULDBLOCK;
            case WSAEBADF:
                return EBADF;
            case WSAEINTR:
                return EINTR;
            default:
                return error;
        }
    }
}
#else //UNIX platform
 
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
 
using ioctl_setting = int;
using buffsize_t    = size_t;
 
//To get consistent socket API between Windows and Linux:
static const int INVALID_SOCKET = -1;
static const int SOCKET_ERROR    = -1;
using SOCKET                    = int;
using SOCKADDR_IN                = sockaddr_in;
using SOCKADDR                    = sockaddr;
using IN_ADDR                    = in_addr;
 
//Wrap them in their WIN32 names
inline int closesocket(SOCKET in)
{
    return close(in);
}
 
template <typename... Params>
inline int ioctlsocket(int fd, int request, Params&&... params)
{
    return ioctl(fd, request, params...);
}
 
#define KISSNET_OS_SPECIFIC_PAYLOAD_NAME dummy
#define KISSNET_OS_SPECIFIC char dummy
#define KISSNET_OS_INIT dummy = 42;
 
namespace unix_specific
{
}
 
inline int get_error_code()
{
    return errno;
}
 
#endif //ifdef WIN32
 
#ifdef KISSNET_USE_OPENSSL
 
#include <openssl/ssl.h>
#include <openssl/err.h>
 
#include <vector>
#include <mutex>
 
#endif //Kissnet use OpenSSL
 
#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif
 
///Main namespace of kissnet
namespace kissnet
{
 
    ///Exception-less error handling infrastructure
    namespace error
    {
        static void (*callback)(const std::string&, void* ctx) = nullptr;
        static void* ctx                                       = nullptr;
        static bool abortOnFatalError                           = true;
 
        inline void handle(const std::string& str)
        {
            //if the error::callback function has been provided, call that
            if (callback)
            {
                callback(str, ctx);
            }
            //Print error into the standard error output
            else
            {
                fputs(str.c_str(), stderr);
            }
 
            //If the error abort hasn't been deactivated
            if (abortOnFatalError)
            {
                abort();
            }
        }
    }
 
    ///low level protocol used, between TCP\TCP_SSL and UDP
    enum class protocol {
        tcp,
        tcp_ssl,
        udp
    };
 
    ///Address information structs
    struct addr_collection {
        sockaddr_storage adrinf = {0};
        socklen_t sock_size = 0;
    };
 
    ///File descriptor set types
    static constexpr int fds_read    = 0x1;
    static constexpr int fds_write    = 0x2;
    static constexpr int fds_except = 0x4;
 
    ///buffer is an array of std::byte
    template <size_t buff_size>
    using buffer = std::array<std::byte, buff_size>;
 
    ///port_t is the port
    using port_t = uint16_t;
 
    ///An endpoint is where the network will connect to (address and port)
    struct endpoint
    {
        ///The address to connect to
        std::string address {};
 
        ///The port to connect to
        port_t port {};
 
        ///Default constructor, the endpoint is not valid at that point, but you can set the address/port manually
        endpoint() = default;
 
        ///Basically create the endpoint with what you give it
        endpoint(std::string addr, port_t prt) :
         address { std::move(addr) }, port { prt }
        { }
 
        static bool is_valid_port_number(unsigned long n)
        {
            return n < 1 << 16;
        }
 
        ///Construct the endpoint from "address:port"
        endpoint(std::string addr)
        {
            const auto separator = addr.find_last_of(':');
 
            //Check if input wasn't missformed
            if (separator == std::string::npos)
                kissnet_fatal_error("string is not of address:port form");
            if (separator == addr.size() - 1)
                kissnet_fatal_error("string has ':' as last character. Expected port number here");
 
            //Isolate address
            address = addr.substr(0, separator);
 
            //Read from string as unsigned
            const auto parsed_port = strtoul(addr.substr(separator + 1).c_str(), nullptr, 10);
 
            //In all other cases, port was always given as a port_t type, strongly preventing it to be a number outside of the [0; 65535] range. Here it's not the case.
            //To detect errors early, check it here :
            if (!is_valid_port_number(parsed_port))
                kissnet_fatal_error("Invalid port number " + std::to_string(parsed_port));
 
            //Store it
            port = static_cast<port_t>(parsed_port);
        }
 
        ///Construct an endpoint from a SOCKADDR
        endpoint(SOCKADDR* addr)
        {
 
            switch (addr->sa_family)
            {
                case AF_INET: {
                    auto ip_addr = (SOCKADDR_IN*)(addr);
                    address         = inet_ntoa(ip_addr->sin_addr);
                    port         = ntohs(ip_addr->sin_port);
                }
                break;
 
                case AF_INET6: {
                    auto ip_addr = (sockaddr_in6*)(addr);
                    char buffer[INET6_ADDRSTRLEN];
                    address = inet_ntop(AF_INET6, &(ip_addr->sin6_addr), buffer, INET6_ADDRSTRLEN);
                    port    = ntohs(ip_addr->sin6_port);
                }
                break;
 
                default: {
                    kissnet_fatal_error("Trying to construct an endpoint for a protocol familly that is neither AF_INET or AF_INET6");
                }
            }
 
            if (address.empty())
                kissnet_fatal_error("Couldn't construct endpoint from sockaddr(_storage) struct");
        }
    };
 
    //Wrap "system calls" here to avoid conflicts with the names used in the socket class
 
    ///socket()
    inline auto syscall_socket = [](int af, int type, int protocol) {
        return ::socket(af, type, protocol);
    };
 
    ///select()
    inline auto syscall_select = [](int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, struct timeval* timeout) {
        return ::select(nfds, readfds, writefds, exceptfds, timeout);
    };
 
    ///recv()
    inline auto syscall_recv = [](SOCKET s, char* buff, buffsize_t len, int flags) {
        return ::recv(s, buff, len, flags);
    };
 
    ///send()
    inline auto syscall_send = [](SOCKET s, const char* buff, buffsize_t len, int flags) {
        return ::send(s, buff, len, flags);
    };
 
    ///bind()
    inline auto syscall_bind = [](SOCKET s, const struct sockaddr* name, socklen_t namelen) {
        return ::bind(s, name, namelen);
    };
 
    ///connect()
    inline auto syscall_connect = [](SOCKET s, const struct sockaddr* name, socklen_t namelen) {
        return ::connect(s, name, namelen);
    };
 
    ///listen()
    inline auto syscall_listen = [](SOCKET s, int backlog) {
        return ::listen(s, backlog);
    };
 
    ///accept()
    inline auto syscall_accept = [](SOCKET s, struct sockaddr* addr, socklen_t* addrlen) {
        return ::accept(s, addr, addrlen);
    };
 
    ///shutdown()
    inline auto syscall_shutdown = [](SOCKET s) {
        return ::shutdown(s, SHUT_RDWR);
    };
 
    ///Represent the status of a socket as returned by a socket operation (send, received). Implicitly convertible to bool
    struct socket_status
    {
        ///Enumeration of socket status, with a 1 byte footprint
        enum values : int8_t {
            errored                            = 0x0,
            valid                            = 0x1,
            cleanly_disconnected            = 0x2,
            non_blocking_would_have_blocked = 0x3,
            timed_out                        = 0x4
 
            /* ... any other info on a "still valid socket" goes here ... */
 
        };
 
        ///Actual value of the socket_status.
        const values value;
 
        ///Use the default constructor
        socket_status() :
         value { errored } { }
 
        ///Construct a "errored/valid" status for a true/false
        explicit socket_status(bool state) :
         value(values(state ? valid : errored)) { }
 
        socket_status(values v) :
         value(v) { }
 
        ///Copy socket status by default
        socket_status(const socket_status&) = default;
 
        ///Move socket status by default
        socket_status(socket_status&&) = default;
 
        ///implicitly convert this object to const bool (as the status should not change)
        operator bool() const
        {
            //See the above enum: every value <= 0 correspond to an error, and will return false. Every value > 0 returns true
            return value > 0;
        }
 
        int8_t get_value() const
        {
            return value;
        }
 
        bool operator==(values v) const
        {
            return v == value;
        }
    };
 
#ifdef KISSNET_USE_OPENSSL
#if OPENSSL_VERSION_NUMBER < 0x10100000L
    static std::shared_ptr<std::vector<std::mutex>> SSL_lock_cs;
 
    class ThreadSafe_SSL
    {
    public:
        ThreadSafe_SSL()
        {
            SSL_lock_cs = std::make_shared<std::vector<std::mutex>>(CRYPTO_num_locks());
 
            CRYPTO_set_locking_callback((void (*)(int, int, const char*, int))
                                            win32_locking_callback);
        }
 
        ~ThreadSafe_SSL() { CRYPTO_set_locking_callback(nullptr); }
 
    private:
        static void win32_locking_callback(int mode, int type, const char* file, int line)
        {
            auto& locks = *SSL_lock_cs;
 
            if (mode & CRYPTO_LOCK)
            {
                locks[type].lock();
            }
            else
            {
                locks[type].unlock();
            }
        }
    };
 
#endif
 
    class Initialize_SSL
    {
    public:
        Initialize_SSL()
        {
#if OPENSSL_VERSION_NUMBER < 0x1010001fL
            SSL_load_error_strings();
            SSL_library_init();
#else
            OPENSSL_init_ssl(
                OPENSSL_INIT_LOAD_SSL_STRINGS | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
 
            OPENSSL_init_crypto(
                OPENSSL_INIT_LOAD_CONFIG | OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS,
                nullptr);
#endif
        }
 
        ~Initialize_SSL()
        {
#if OPENSSL_VERSION_NUMBER < 0x1010001fL
            ERR_free_strings();
#endif
        }
 
#if OPENSSL_VERSION_NUMBER < 0x10100000L
    private:
        ThreadSafe_SSL thread_setup;
#endif
    };
 
    static Initialize_SSL InitializeSSL;
#endif
 
    ///Class that represent a socket
    template <protocol sock_proto>
    class socket
    {
        ///Represent a number of bytes with a status information. Some of the methods of this class returns this.
        using bytes_with_status = std::tuple<size_t, socket_status>;
 
        ///OS specific stuff. payload we have to hold onto for RAII management of the Operating System's socket library (e.g. Windows Socket API WinSock2)
        KISSNET_OS_SPECIFIC;
 
        ///operatic-system type for a socket object
        SOCKET sock = INVALID_SOCKET;
 
#ifdef KISSNET_USE_OPENSSL
        SSL* pSSL          = nullptr;
        SSL_CTX* pContext = nullptr;
#endif
 
        ///Location where this socket is bound
        endpoint bind_loc = {};
 
        ///Address information structures
        addrinfo getaddrinfo_hints      = {};
        addrinfo* getaddrinfo_results = nullptr;
        addrinfo* socket_addrinfo      = nullptr;
 
        void initialize_addrinfo()
        {
            int type {};
            int iprotocol {};
            if constexpr (sock_proto == protocol::tcp || sock_proto == protocol::tcp_ssl)
            {
                type      = SOCK_STREAM;
                iprotocol = IPPROTO_TCP;
            }
 
            else if constexpr (sock_proto == protocol::udp)
            {
                type      = SOCK_DGRAM;
                iprotocol = IPPROTO_UDP;
            }
 
            getaddrinfo_hints              = {};
            getaddrinfo_hints.ai_family      = AF_UNSPEC;
            getaddrinfo_hints.ai_socktype = type;
            getaddrinfo_hints.ai_protocol = iprotocol;
            getaddrinfo_hints.ai_flags      = AI_ADDRCONFIG;
        }
 
        ///Create and connect to socket
        socket_status connect(addrinfo* addr, int64_t timeout, bool createsocket)
        {
            if constexpr (sock_proto == protocol::tcp || sock_proto == protocol::tcp_ssl) //only TCP is a connected protocol
            {
                if (createsocket)
                {
                    close();
                    socket_addrinfo = nullptr;
                    sock            = syscall_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
                }
 
                if (sock == INVALID_SOCKET)
                    return socket_status::errored;
 
                socket_addrinfo = addr;
 
                if (timeout > 0)
                    set_non_blocking(true);
 
                int error = syscall_connect(sock, addr->ai_addr, socklen_t(addr->ai_addrlen));
                if (error == SOCKET_ERROR)
                {
                    error = get_error_code();
                    if (error == EWOULDBLOCK || error == EAGAIN || error == EINPROGRESS)
                    {
                        struct timeval tv;
                        tv.tv_sec  = static_cast<long>(timeout / 1000);
                        tv.tv_usec = 1000 * static_cast<long>(timeout % 1000);
 
                        fd_set fd_write, fd_except;
                        ;
                        FD_ZERO(&fd_write);
                        FD_SET(sock, &fd_write);
                        FD_ZERO(&fd_except);
                        FD_SET(sock, &fd_except);
 
                        int ret = syscall_select(static_cast<int>(sock) + 1, NULL, &fd_write, &fd_except, &tv);
                        if (ret == -1)
                            error = get_error_code();
                        else if (ret == 0)
                            error = ETIMEDOUT;
                        else
                        {
                            socklen_t errlen = sizeof(error);
                            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&error), &errlen) != 0)
                                kissnet_fatal_error("getting socket error returned an error");
                        }
                    }
                }
 
                if (timeout > 0)
                    set_non_blocking(false);
 
                if (error == 0)
                {
                    return socket_status::valid;
                }
                else
                {
                    close();
                    socket_addrinfo = nullptr;
                    return socket_status::errored;
                }
            }
 
            kissnet_fatal_error("connect called for non-tcp socket");
        }
 
        ///sockaddr struct
        sockaddr_storage socket_input  = {};
        socklen_t socket_input_socklen = 0;
 
    public:
 
        ///Construct an invalid socket
        socket() = default;
 
        ///socket<> isn't copyable
        socket(const socket&) = delete;
 
        ///socket<> isn't copyable
        socket& operator=(const socket&) = delete;
 
        ///Move constructor. socket<> isn't copyable
        socket(socket&& other) noexcept
        {
            KISSNET_OS_SPECIFIC_PAYLOAD_NAME = std::move(other.KISSNET_OS_SPECIFIC_PAYLOAD_NAME);
            bind_loc                         = std::move(other.bind_loc);
            sock                             = std::move(other.sock);
            socket_input                     = std::move(other.socket_input);
            socket_input_socklen             = std::move(other.socket_input_socklen);
            getaddrinfo_results                 = std::move(other.getaddrinfo_results);
            socket_addrinfo                     = std::move(other.socket_addrinfo);
 
#ifdef KISSNET_USE_OPENSSL
            pSSL           = other.pSSL;
            pContext       = other.pContext;
            other.pSSL       = nullptr;
            other.pContext = nullptr;
#endif
 
            other.sock                  = INVALID_SOCKET;
            other.getaddrinfo_results = nullptr;
            other.socket_addrinfo      = nullptr;
        }
 
        ///Move assign operation
        socket& operator=(socket&& other) noexcept
        {
            if (this != &other)
            {
                if (!(sock < 0) || sock != INVALID_SOCKET)
                    closesocket(sock);
 
                KISSNET_OS_SPECIFIC_PAYLOAD_NAME = std::move(other.KISSNET_OS_SPECIFIC_PAYLOAD_NAME);
                bind_loc                         = std::move(other.bind_loc);
                sock                             = std::move(other.sock);
                socket_input                     = std::move(other.socket_input);
                socket_input_socklen             = std::move(other.socket_input_socklen);
                getaddrinfo_results                 = std::move(other.getaddrinfo_results);
                socket_addrinfo                     = std::move(other.socket_addrinfo);
 
#ifdef KISSNET_USE_OPENSSL
                pSSL           = other.pSSL;
                pContext       = other.pContext;
                other.pSSL       = nullptr;
                other.pContext = nullptr;
#endif
 
                other.sock                  = INVALID_SOCKET;
                other.getaddrinfo_results = nullptr;
                other.socket_addrinfo      = nullptr;
            }
            return *this;
        }
 
        ///Return true if the underlying OS provided socket representation (file descriptor, handle...). Both socket are pointing to the same thing in this case
        bool operator==(const socket& other) const
        {
            return sock == other.sock;
        }
 
        ///Return true if socket is valid. If this is false, you probably shouldn't attempt to send/receive anything, it will probably explode in your face!
        bool is_valid() const
        {
            return sock != INVALID_SOCKET;
        }
 
        inline operator bool() const
        {
            return is_valid();
        }
 
        ///Construct socket and (if applicable) connect to the endpoint
        socket(endpoint bind_to) :
         bind_loc { std::move(bind_to) }
        {
            //operating system related housekeeping
            KISSNET_OS_INIT;
 
            //Do we use streams or datagrams
            initialize_addrinfo();
 
            if (getaddrinfo(bind_loc.address.c_str(), std::to_string(bind_loc.port).c_str(), &getaddrinfo_hints, &getaddrinfo_results) != 0)
            {
                kissnet_fatal_error("getaddrinfo failed!");
            }
 
            for (auto* addr = getaddrinfo_results; addr; addr = addr->ai_next)
            {
                sock = syscall_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
                if (sock != INVALID_SOCKET)
                {
                    socket_addrinfo = addr;
                    break;
                }
            }
 
            if (sock == INVALID_SOCKET)
            {
                kissnet_fatal_error("unable to create socket!");
            }
        }
 
        ///Construct a socket from an operating system socket, an additional endpoint to remember from where we are
        socket(SOCKET native_sock, endpoint bind_to) :
         sock { native_sock }, bind_loc(std::move(bind_to))
        {
            KISSNET_OS_INIT;
 
            initialize_addrinfo();
        }
 
        ///Set the socket in non blocking mode
        /// \param state By default "true". If put to false, it will set the socket back into blocking, normal mode
        void set_non_blocking(bool state = true) const
        {
#ifdef _WIN32
            ioctl_setting set = state ? 1 : 0;
            if (ioctlsocket(sock, FIONBIO, &set) < 0)
#else
            const auto flags    = fcntl(sock, F_GETFL, 0);
            const auto newflags = state ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK);
            if (fcntl(sock, F_SETFL, newflags) < 0)
#endif
                kissnet_fatal_error("setting socket to nonblock returned an error");
        }
 
        ///Set the socket option for broadcasts
        /// \param state By default "true". If put to false, it will disable broadcasts
        void set_broadcast(bool state = true) const
        {
            const int broadcast = state ? 1 : 0;
            if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<const char*>(&broadcast), sizeof(broadcast)) != 0)
                kissnet_fatal_error("setting socket broadcast mode returned an error");
        }
 
        /// Set the socket option for TCPNoDelay
        /// \param state By default "true". If put to false, it will disable TCPNoDelay
        void set_tcp_no_delay(bool state = true) const
        {
            if constexpr (sock_proto == protocol::tcp)
            {
                const int tcpnodelay = state ? 1 : 0;
                if (setsockopt(sock, SOL_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&tcpnodelay), sizeof(tcpnodelay)) != 0)
                    kissnet_fatal_error("setting socket tcpnodelay mode returned an error");
            }
        }
 
        /// Get socket status
        socket_status get_status() const
        {
            int sockerror     = 0;
            socklen_t errlen = sizeof(sockerror);
            if (getsockopt(sock, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&sockerror), &errlen) != 0)
                kissnet_fatal_error("getting socket error returned an error");
 
            return sockerror == SOCKET_ERROR ? socket_status::errored : socket_status::valid;
        }
 
        ///Bind socket locally using the address and port of the endpoint
        void bind()
        {
            if (syscall_bind(sock, static_cast<SOCKADDR*>(socket_addrinfo->ai_addr), socklen_t(socket_addrinfo->ai_addrlen)) == SOCKET_ERROR)
            {
                kissnet_fatal_error("bind() failed\n");
            }
        }
 
        ///Join a multicast group
        void join(const endpoint& multi_cast_endpoint, const std::string& interface = "")
        {
            if (sock_proto != protocol::udp)
            {
                kissnet_fatal_error("joining a multicast is only possible in UDP mode\n");
            }
 
            addrinfo *multicast_addr;
            addrinfo *local_addr;
            addrinfo  hints = {0};
            hints.ai_family = PF_UNSPEC;
            hints.ai_flags  = AI_NUMERICHOST;
            if (getaddrinfo(multi_cast_endpoint.address.c_str(), nullptr, &hints, &multicast_addr) != 0)
            {
                kissnet_fatal_error("getaddrinfo() failed\n");
            }
 
            hints.ai_family   = multicast_addr->ai_family;
            hints.ai_socktype = SOCK_DGRAM;
            hints.ai_flags    = AI_PASSIVE;
            if (getaddrinfo(nullptr, std::to_string(multi_cast_endpoint.port).c_str(), &hints, &local_addr) != 0)
            {
                kissnet_fatal_error("getaddrinfo() failed\n");
            }
 
            sock = syscall_socket(local_addr->ai_family, local_addr->ai_socktype, local_addr->ai_protocol);
            if (sock != INVALID_SOCKET)
            {
                socket_addrinfo = local_addr;
            } else {
                kissnet_fatal_error("syscall_socket() failed\n");
            }
 
            bind();
 
            //IPv4
            if (multicast_addr->ai_family  == PF_INET && multicast_addr->ai_addrlen == sizeof(struct sockaddr_in))
            {
                struct ip_mreq multicastRequest = {0};
                memcpy(&multicastRequest.imr_multiaddr,
                       &((struct sockaddr_in*)(multicast_addr->ai_addr))->sin_addr,
                       sizeof(multicastRequest.imr_multiaddr));
 
                if (interface.length()) {
                    multicastRequest.imr_interface.s_addr = inet_addr(interface.c_str());;
                } else {
                    multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
                }
 
                if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &multicastRequest, sizeof(multicastRequest)) != 0)
                {
                    kissnet_fatal_error("setsockopt() failed\n");
                }
            }
 
            //IPv6
            else if (multicast_addr->ai_family  == PF_INET6 && multicast_addr->ai_addrlen == sizeof(struct sockaddr_in6))
            {
                struct ipv6_mreq multicastRequest = {0};
                memcpy(&multicastRequest.ipv6mr_multiaddr,
                       &((struct sockaddr_in6*)(multicast_addr->ai_addr))->sin6_addr,
                       sizeof(multicastRequest.ipv6mr_multiaddr));
 
                if (interface.length()) {
                    struct addrinfo *reslocal;
                    if (getaddrinfo(interface.c_str(), nullptr, nullptr, &reslocal)){
                        kissnet_fatal_error("getaddrinfo() failed\n");
                    }
                    multicastRequest.ipv6mr_interface = ((sockaddr_in6 *)reslocal->ai_addr)->sin6_scope_id;
                    freeaddrinfo(reslocal);
                } else {
                    multicastRequest.ipv6mr_interface = 0;
                }
 
 
                if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char*) &multicastRequest, sizeof(multicastRequest)) != 0)
                {
                    kissnet_fatal_error("setsockopt() failed\n");
                }
            }
            else
            {
                kissnet_fatal_error("unknown AI family.\n");
            }
 
            freeaddrinfo(multicast_addr);
        }
 
        ///(For TCP) connect to the endpoint as client
        socket_status connect(int64_t timeout = 0)
        {
            if constexpr (sock_proto == protocol::tcp) //only TCP is a connected protocol
            {
                // try to connect to existing native socket, if any.
                auto curr_addr = socket_addrinfo;
                if (connect(curr_addr, timeout, false) != socket_status::valid)
                {
                    // try to create/connect native socket for one of the other addrinfo, if any
                    for (auto* addr = getaddrinfo_results; addr; addr = addr->ai_next)
                    {
                        if (addr == curr_addr)
                            continue; // already checked
 
                        if (connect(addr, timeout, true) == socket_status::valid)
                            break; // success
                    }
                }
 
                if (sock == INVALID_SOCKET)
                    kissnet_fatal_error("unable to create connectable socket!");
 
                return socket_status::valid;
            }
#ifdef KISSNET_USE_OPENSSL
            else if constexpr (sock_proto == protocol::tcp_ssl) //only TCP is a connected protocol
            {
                // try to connect to existing native socket, if any.
                auto curr_addr = socket_addrinfo;
                if (connect(curr_addr, timeout, false) != socket_status::valid)
                {
                    // try to create/connect native socket for one of the other addrinfo, if any
                    for (auto* addr = getaddrinfo_results; addr; addr = addr->ai_next)
                    {
                        if (addr == curr_addr)
                            continue; // already checked
 
                        if (connect(addr, timeout, true) == socket_status::valid)
                            break; // success
                    }
                }
 
                if (sock == INVALID_SOCKET)
                    kissnet_fatal_error("unable to create connectable socket!");
 
                auto* pMethod =
#if (OPENSSL_VERSION_NUMBER < 0x10100000L)
                    TLSv1_2_client_method();
#else
                    TLS_client_method();
#endif
 
                pContext = SSL_CTX_new(pMethod);
                pSSL     = SSL_new(pContext);
                if (!pSSL)
                    return socket_status::errored;
 
                if (!(static_cast<bool>(SSL_set_fd(pSSL, sock))))
                    return socket_status::errored;
 
                if (SSL_connect(pSSL) != 1)
                    return socket_status::errored;
 
                return socket_status::valid;
            }
#endif
        }
 
        ///(for TCP= setup socket to listen to connection. Need to be called on binded socket, before being able to accept()
        void listen()
        {
            if constexpr (sock_proto == protocol::tcp)
            {
                if (syscall_listen(sock, SOMAXCONN) == SOCKET_ERROR)
                {
                    kissnet_fatal_error("listen failed\n");
                }
            }
        }
 
        ///(for TCP) Wait for incoming connection, return socket connect to the client. Blocking.
        socket accept()
        {
            if constexpr (sock_proto != protocol::tcp)
            {
                return { INVALID_SOCKET, {} };
            }
 
            sockaddr_storage socket_address;
            SOCKET s;
            socklen_t size = sizeof socket_address;
 
            if ((s = syscall_accept(sock, reinterpret_cast<SOCKADDR*>(&socket_address), &size)) == INVALID_SOCKET)
            {
                const auto error = get_error_code();
                switch (error)
                {
                    case EWOULDBLOCK: //if socket "would have blocked" from the call, ignore
                    case EINTR:          //if blocking call got interrupted, ignore;
                        return {};
                }
 
                kissnet_fatal_error("accept() returned an invalid socket\n");
            }
 
            return { s, endpoint(reinterpret_cast<SOCKADDR*>(&socket_address)) };
        }
 
        void close()
        {
            if (sock != INVALID_SOCKET)
            {
#ifdef KISSNET_USE_OPENSSL
                if constexpr (sock_proto == protocol::tcp_ssl)
                {
                    if (pSSL)
                    {
                        SSL_set_shutdown(pSSL, SSL_RECEIVED_SHUTDOWN | SSL_SENT_SHUTDOWN);
                        SSL_shutdown(pSSL);
                        SSL_free(pSSL);
                        if (pContext)
                            SSL_CTX_free(pContext);
                    }
                }
#endif
 
                closesocket(sock);
            }
 
            sock = INVALID_SOCKET;
        }
 
        void shutdown()
        {
            if (sock != INVALID_SOCKET)
            {
                syscall_shutdown(sock);
            }
        }
 
        ///Close socket on destruction
        ~socket()
        {
            close();
 
            if (getaddrinfo_results)
                freeaddrinfo(getaddrinfo_results);
        }
 
        ///Select socket with timeout
        socket_status select(int fds, int64_t timeout)
        {
            fd_set fd_read, fd_write, fd_except;
            ;
            struct timeval tv;
 
            tv.tv_sec  = static_cast<long>(timeout / 1000);
            tv.tv_usec = 1000 * static_cast<long>(timeout % 1000);
 
            if (fds & fds_read)
            {
                FD_ZERO(&fd_read);
                FD_SET(sock, &fd_read);
            }
            if (fds & fds_write)
            {
                FD_ZERO(&fd_write);
                FD_SET(sock, &fd_write);
            }
            if (fds & fds_except)
            {
                FD_ZERO(&fd_except);
                FD_SET(sock, &fd_except);
            }
 
            int ret = syscall_select(static_cast<int>(sock) + 1,
                                     fds & fds_read ? &fd_read : NULL,
                                     fds & fds_write ? &fd_write : NULL,
                                     fds & fds_except ? &fd_except : NULL,
                                     &tv);
            if (ret == -1)
                return socket_status::errored;
            else if (ret == 0)
                return socket_status::timed_out;
            return socket_status::valid;
        }
 
        template <size_t buff_size>
        bytes_with_status send(const buffer<buff_size>& buff, const size_t length = buff_size, addr_collection* addr = nullptr)
        {
            assert(buff_size >= length);
            return send(buff.data(), length, addr);
        }
 
        ///Send some bytes through the pipe
        bytes_with_status send(const std::byte* read_buff, size_t length, addr_collection* addr = nullptr)
        {
            auto received_bytes { 0 };
            if constexpr (sock_proto == protocol::tcp)
            {
                received_bytes = syscall_send(sock, reinterpret_cast<const char*>(read_buff), static_cast<buffsize_t>(length), 0);
            }
#ifdef KISSNET_USE_OPENSSL
            else if constexpr (sock_proto == protocol::tcp_ssl)
            {
                received_bytes = SSL_write(pSSL, reinterpret_cast<const char*>(read_buff), static_cast<buffsize_t>(length));
            }
#endif
            else if constexpr (sock_proto == protocol::udp)
            {
                if (addr) {
                    received_bytes = sendto(sock, reinterpret_cast<const char*>(read_buff), static_cast<buffsize_t>(length), 0, reinterpret_cast<sockaddr*>(&addr->adrinf) , addr->sock_size);
                } else {
                    received_bytes = sendto(sock, reinterpret_cast<const char*>(read_buff), static_cast<buffsize_t>(length), 0, static_cast<SOCKADDR*>(socket_addrinfo->ai_addr), socklen_t(socket_addrinfo->ai_addrlen));
                }
            }
 
            if (received_bytes < 0)
            {
                if (get_error_code() == EWOULDBLOCK)
                {
                    return { 0, socket_status::non_blocking_would_have_blocked };
                }
 
                return { 0, socket_status::errored };
            }
 
            return { received_bytes, socket_status::valid };
        }
 
        ///receive bytes inside the buffer, return the number of bytes you got. You can choose to write inside the buffer at a specific start offset (in number of bytes)
        template <size_t buff_size>
        bytes_with_status recv(buffer<buff_size>& write_buff, size_t start_offset = 0, addr_collection* addr_info = nullptr)
        {
            auto received_bytes = 0;
            if constexpr (sock_proto == protocol::tcp)
            {
                received_bytes = syscall_recv(sock, reinterpret_cast<char*>(write_buff.data()) + start_offset, static_cast<buffsize_t>(buff_size - start_offset), 0);
            }
#ifdef KISSNET_USE_OPENSSL
            else if constexpr (sock_proto == protocol::tcp_ssl)
            {
                received_bytes = SSL_read(pSSL, reinterpret_cast<char*>(write_buff.data()) + start_offset, static_cast<buffsize_t>(buff_size - start_offset));
            }
#endif
            else if constexpr (sock_proto == protocol::udp)
            {
                socket_input_socklen = sizeof socket_input;
 
                received_bytes = ::recvfrom(sock, reinterpret_cast<char*>(write_buff.data()) + start_offset, static_cast<buffsize_t>(buff_size - start_offset), 0, reinterpret_cast<sockaddr*>(&socket_input), &socket_input_socklen);
                if (addr_info) {
                    addr_info->adrinf = socket_input;
                    addr_info->sock_size = socket_input_socklen;
                }
            }
 
            if (received_bytes < 0)
            {
                const auto error = get_error_code();
                if (error == EWOULDBLOCK)
                    return { 0, socket_status::non_blocking_would_have_blocked };
                if (error == EAGAIN)
                    return { 0, socket_status::non_blocking_would_have_blocked };
                return { 0, socket_status::errored };
            }
 
            if (received_bytes == 0)
            {
                return { received_bytes, socket_status::cleanly_disconnected };
            }
 
            return { size_t(received_bytes), socket_status::valid };
        }
 
        ///receive up-to len bytes inside the memory location pointed by buffer
        bytes_with_status recv(std::byte* buffer, size_t len, bool wait = true, addr_collection* addr_info = nullptr)
        {
            auto received_bytes = 0;
            if constexpr (sock_proto == protocol::tcp)
            {
                int flags;
                if (wait)
                    flags = MSG_WAITALL;
                else
                {
#ifdef _WIN32
                    flags = 0; // MSG_DONTWAIT not avail on windows, need to make socket nonblockingto emulate
                    set_non_blocking(true);
#else
                    flags = MSG_DONTWAIT;
#endif
                }
                received_bytes = syscall_recv(sock, reinterpret_cast<char*>(buffer), static_cast<buffsize_t>(len), flags);
#ifdef _WIN32
                set_non_blocking(false);
#endif
            }
 
#ifdef KISSNET_USE_OPENSSL
            else if constexpr (sock_proto == protocol::tcp_ssl)
            {
                received_bytes = SSL_read(pSSL, reinterpret_cast<char*>(buffer), static_cast<buffsize_t>(len));
            }
#endif
 
            else if constexpr (sock_proto == protocol::udp)
            {
                socket_input_socklen = sizeof socket_input;
 
                received_bytes = ::recvfrom(sock, reinterpret_cast<char*>(buffer), static_cast<buffsize_t>(len), 0, reinterpret_cast<sockaddr*>(&socket_input), &socket_input_socklen);
                if (addr_info) {
                    addr_info->adrinf = socket_input;
                    addr_info->sock_size = socket_input_socklen;
                }
            }
 
            if (received_bytes < 0)
            {
                const auto error = get_error_code();
                if (error == EWOULDBLOCK)
                    return { 0, socket_status::non_blocking_would_have_blocked };
                if (error == EAGAIN)
                    return { 0, socket_status::non_blocking_would_have_blocked };
                return { 0, socket_status::errored };
            }
 
            if (received_bytes == 0)
            {
                return { received_bytes, socket_status::cleanly_disconnected };
            }
 
            return { size_t(received_bytes), socket_status::valid };
        }
 
        ///Return the endpoint where this socket is talking to
        endpoint get_bind_loc() const
        {
            return bind_loc;
        }
 
        ///Return an endpoint that originated the data in the last recv
        endpoint get_recv_endpoint() const
        {
            if constexpr (sock_proto == protocol::tcp)
            {
                return get_bind_loc();
            }
            if constexpr (sock_proto == protocol::udp)
            {
                const SOCKADDR* addr = reinterpret_cast<const SOCKADDR*>(&socket_input);
                return endpoint(const_cast<SOCKADDR*>(addr));
            }
        }
 
        ///Return the number of bytes available inside the socket
        size_t bytes_available() const
        {
            static ioctl_setting size = 0;
            const auto status          = ioctlsocket(sock, FIONREAD, &size);
 
            if (status < 0)
            {
                kissnet_fatal_error("ioctlsocket status is negative when getting FIONREAD\n");
            }
 
            return size > 0 ? size : 0;
        }
 
        ///Return the protocol used by this socket
        static protocol get_protocol()
        {
            return sock_proto;
        }
    };
 
    ///Alias for socket<protocol::tcp>
    using tcp_socket = socket<protocol::tcp>;
#ifdef KISSNET_USE_OPENSSL
    ///Alias for socket<protocol::tcp_ssl>
    using tcp_ssl_socket = socket<protocol::tcp_ssl>;
#endif //KISSNET_USE_OPENSSL
    ///Alias for socket<protocol::udp>
    using udp_socket = socket<protocol::udp>;
}
 
//cleanup preprocessor macros
#undef KISSNET_OS_SPECIFIC_PAYLOAD_NAME
#undef KISSNET_OS_SPECIFIC
#undef KISSNET_OS_INIT
#undef kissnet_fatal_error
 
#endif //KISS_NET