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
|
/* $Id$ */
/** @file
* IPRT - TCP/IP.
*/
/*
* Copyright (C) 2006-2020 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#ifdef RT_OS_WINDOWS
# include <iprt/win/winsock2.h>
#else
# include <sys/types.h>
# include <sys/socket.h>
# include <errno.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <arpa/inet.h>
# include <netdb.h>
# ifdef FIX_FOR_3_2
# include <fcntl.h>
# endif
#endif
#include <limits.h>
#include "internal/iprt.h"
#include <iprt/tcp.h>
#include <iprt/asm.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/log.h>
#include <iprt/mempool.h>
#include <iprt/mem.h>
#include <iprt/string.h>
#include <iprt/socket.h>
#include <iprt/thread.h>
#include <iprt/time.h>
#include "internal/magics.h"
#include "internal/socket.h"
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/* non-standard linux stuff (it seems). */
#ifndef MSG_NOSIGNAL
# define MSG_NOSIGNAL 0
#endif
#ifndef SHUT_RDWR
# ifdef SD_BOTH
# define SHUT_RDWR SD_BOTH
# else
# define SHUT_RDWR 2
# endif
#endif
#ifndef SHUT_WR
# ifdef SD_SEND
# define SHUT_WR SD_SEND
# else
# define SHUT_WR 1
# endif
#endif
/* fixup backlevel OSes. */
#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
# define socklen_t int
#endif
/** How many pending connection. */
#define RTTCP_SERVER_BACKLOG 10
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* TCP Server state.
*/
typedef enum RTTCPSERVERSTATE
{
/** Invalid. */
RTTCPSERVERSTATE_INVALID = 0,
/** Created. */
RTTCPSERVERSTATE_CREATED,
/** Listener thread is starting up. */
RTTCPSERVERSTATE_STARTING,
/** Accepting client connections. */
RTTCPSERVERSTATE_ACCEPTING,
/** Serving a client. */
RTTCPSERVERSTATE_SERVING,
/** Listener terminating. */
RTTCPSERVERSTATE_STOPPING,
/** Listener terminated. */
RTTCPSERVERSTATE_STOPPED,
/** Listener cleans up. */
RTTCPSERVERSTATE_DESTROYING
} RTTCPSERVERSTATE;
/*
* Internal representation of the TCP Server handle.
*/
typedef struct RTTCPSERVER
{
/** The magic value (RTTCPSERVER_MAGIC). */
uint32_t volatile u32Magic;
/** The server state. */
RTTCPSERVERSTATE volatile enmState;
/** The server thread. */
RTTHREAD Thread;
/** The server socket. */
RTSOCKET volatile hServerSocket;
/** The socket to the client currently being serviced.
* This is NIL_RTSOCKET when no client is serviced. */
RTSOCKET volatile hClientSocket;
/** The connection function. */
PFNRTTCPSERVE pfnServe;
/** Argument to pfnServer. */
void *pvUser;
} RTTCPSERVER;
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static DECLCALLBACK(int) rtTcpServerThread(RTTHREAD ThreadSelf, void *pvServer);
static int rtTcpServerListen(PRTTCPSERVER pServer);
static int rtTcpServerListenCleanup(PRTTCPSERVER pServer);
static int rtTcpClose(RTSOCKET Sock, const char *pszMsg, bool fTryGracefulShutdown);
/**
* Atomicly updates a socket variable.
* @returns The old handle value.
* @param phSock The socket handle variable to update.
* @param hNew The new socket handle value.
*/
DECLINLINE(RTSOCKET) rtTcpAtomicXchgSock(RTSOCKET volatile *phSock, const RTSOCKET hNew)
{
RTSOCKET hRet;
ASMAtomicXchgHandle(phSock, hNew, &hRet);
return hRet;
}
/**
* Tries to change the TCP server state.
*/
DECLINLINE(bool) rtTcpServerTrySetState(PRTTCPSERVER pServer, RTTCPSERVERSTATE enmStateNew, RTTCPSERVERSTATE enmStateOld)
{
bool fRc;
ASMAtomicCmpXchgSize(&pServer->enmState, enmStateNew, enmStateOld, fRc);
return fRc;
}
/**
* Changes the TCP server state.
*/
DECLINLINE(void) rtTcpServerSetState(PRTTCPSERVER pServer, RTTCPSERVERSTATE enmStateNew, RTTCPSERVERSTATE enmStateOld)
{
bool fRc;
ASMAtomicCmpXchgSize(&pServer->enmState, enmStateNew, enmStateOld, fRc);
Assert(fRc); NOREF(fRc);
}
/**
* Closes the a socket (client or server).
*
* @returns IPRT status code.
*/
static int rtTcpServerDestroySocket(RTSOCKET volatile *pSock, const char *pszMsg, bool fTryGracefulShutdown)
{
RTSOCKET hSocket = rtTcpAtomicXchgSock(pSock, NIL_RTSOCKET);
if (hSocket != NIL_RTSOCKET)
{
if (!fTryGracefulShutdown)
RTSocketShutdown(hSocket, true /*fRead*/, true /*fWrite*/);
return rtTcpClose(hSocket, pszMsg, fTryGracefulShutdown);
}
return VINF_TCP_SERVER_NO_CLIENT;
}
/**
* Create single connection at a time TCP Server in a separate thread.
*
* The thread will loop accepting connections and call pfnServe for
* each of the incoming connections in turn. The pfnServe function can
* return VERR_TCP_SERVER_STOP too terminate this loop. RTTcpServerDestroy()
* should be used to terminate the server.
*
* @returns iprt status code.
* @param pszAddress The address for creating a listening socket.
* If NULL or empty string the server is bound to all interfaces.
* @param uPort The port for creating a listening socket.
* @param enmType The thread type.
* @param pszThrdName The name of the worker thread.
* @param pfnServe The function which will serve a new client connection.
* @param pvUser User argument passed to pfnServe.
* @param ppServer Where to store the serverhandle.
*/
RTR3DECL(int) RTTcpServerCreate(const char *pszAddress, unsigned uPort, RTTHREADTYPE enmType, const char *pszThrdName,
PFNRTTCPSERVE pfnServe, void *pvUser, PPRTTCPSERVER ppServer)
{
/*
* Validate input.
*/
AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
AssertPtrReturn(pfnServe, VERR_INVALID_POINTER);
AssertPtrReturn(pszThrdName, VERR_INVALID_POINTER);
AssertPtrReturn(ppServer, VERR_INVALID_POINTER);
/*
* Create the server.
*/
PRTTCPSERVER pServer;
int rc = RTTcpServerCreateEx(pszAddress, uPort, &pServer);
if (RT_SUCCESS(rc))
{
/*
* Create the listener thread.
*/
RTMemPoolRetain(pServer);
pServer->enmState = RTTCPSERVERSTATE_STARTING;
pServer->pvUser = pvUser;
pServer->pfnServe = pfnServe;
rc = RTThreadCreate(&pServer->Thread, rtTcpServerThread, pServer, 0, enmType, /*RTTHREADFLAGS_WAITABLE*/0, pszThrdName);
if (RT_SUCCESS(rc))
{
/* done */
if (ppServer)
*ppServer = pServer;
else
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return rc;
}
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
/*
* Destroy the server.
*/
rtTcpServerSetState(pServer, RTTCPSERVERSTATE_CREATED, RTTCPSERVERSTATE_STARTING);
RTTcpServerDestroy(pServer);
}
return rc;
}
/**
* Server thread, loops accepting connections until it's terminated.
*
* @returns iprt status code. (ignored).
* @param ThreadSelf Thread handle.
* @param pvServer Server handle.
*/
static DECLCALLBACK(int) rtTcpServerThread(RTTHREAD ThreadSelf, void *pvServer)
{
PRTTCPSERVER pServer = (PRTTCPSERVER)pvServer;
int rc;
if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_ACCEPTING, RTTCPSERVERSTATE_STARTING))
rc = rtTcpServerListen(pServer);
else
rc = rtTcpServerListenCleanup(pServer);
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
NOREF(ThreadSelf);
return VINF_SUCCESS;
}
/**
* Create single connection at a time TCP Server.
* The caller must call RTTcpServerListen() to actually start the server.
*
* @returns iprt status code.
* @param pszAddress The address for creating a listening socket.
* If NULL the server is bound to all interfaces.
* @param uPort The port for creating a listening socket.
* @param ppServer Where to store the serverhandle.
*/
RTR3DECL(int) RTTcpServerCreateEx(const char *pszAddress, uint32_t uPort, PPRTTCPSERVER ppServer)
{
/*
* Validate input.
*/
AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
AssertPtrReturn(ppServer, VERR_INVALID_PARAMETER);
/*
* Resolve the address.
*/
RTNETADDR LocalAddr;
int rc = RTSocketParseInetAddress(pszAddress, uPort, &LocalAddr);
if (RT_FAILURE(rc))
return rc;
/*
* Setting up socket.
*/
RTSOCKET WaitSock;
rc = rtSocketCreate(&WaitSock, AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (RT_SUCCESS(rc))
{
RTSocketSetInheritance(WaitSock, false /*fInheritable*/);
/*
* Set socket options.
*/
int fFlag = 1;
if (!rtSocketSetOpt(WaitSock, SOL_SOCKET, SO_REUSEADDR, &fFlag, sizeof(fFlag)))
{
/*
* Bind a name to a socket and set it listening for connections.
*/
rc = rtSocketBind(WaitSock, &LocalAddr);
if (RT_SUCCESS(rc))
rc = rtSocketListen(WaitSock, RTTCP_SERVER_BACKLOG);
if (RT_SUCCESS(rc))
{
/*
* Create the server handle.
*/
PRTTCPSERVER pServer = (PRTTCPSERVER)RTMemPoolAlloc(RTMEMPOOL_DEFAULT, sizeof(*pServer));
if (pServer)
{
pServer->u32Magic = RTTCPSERVER_MAGIC;
pServer->enmState = RTTCPSERVERSTATE_CREATED;
pServer->Thread = NIL_RTTHREAD;
pServer->hServerSocket = WaitSock;
pServer->hClientSocket = NIL_RTSOCKET;
pServer->pfnServe = NULL;
pServer->pvUser = NULL;
*ppServer = pServer;
return VINF_SUCCESS;
}
/* bail out */
rc = VERR_NO_MEMORY;
}
}
else
AssertMsgFailed(("rtSocketSetOpt: %Rrc\n", rc));
rtTcpClose(WaitSock, "RTServerCreateEx", false /*fTryGracefulShutdown*/);
}
return rc;
}
/**
* Listen for incoming connections.
*
* The function will loop accepting connections and call pfnServe for
* each of the incoming connections in turn. The pfnServe function can
* return VERR_TCP_SERVER_STOP too terminate this loop. A stopped server
* can only be destroyed.
*
* @returns IPRT status code.
* @retval VERR_TCP_SERVER_STOP if stopped by pfnServe.
* @retval VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
*
* @param pServer The server handle as returned from RTTcpServerCreateEx().
* @param pfnServe The function which will serve a new client connection.
* @param pvUser User argument passed to pfnServe.
*/
RTR3DECL(int) RTTcpServerListen(PRTTCPSERVER pServer, PFNRTTCPSERVE pfnServe, void *pvUser)
{
/*
* Validate input and retain the instance.
*/
AssertPtrReturn(pfnServe, VERR_INVALID_POINTER);
AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
AssertReturn(pServer->u32Magic == RTTCPSERVER_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
int rc = VERR_INVALID_STATE;
if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_ACCEPTING, RTTCPSERVERSTATE_CREATED))
{
Assert(!pServer->pfnServe);
Assert(!pServer->pvUser);
Assert(pServer->Thread == NIL_RTTHREAD);
Assert(pServer->hClientSocket == NIL_RTSOCKET);
pServer->pfnServe = pfnServe;
pServer->pvUser = pvUser;
pServer->Thread = RTThreadSelf();
Assert(pServer->Thread != NIL_RTTHREAD);
rc = rtTcpServerListen(pServer);
}
else
{
AssertMsgFailed(("enmState=%d\n", pServer->enmState));
rc = VERR_INVALID_STATE;
}
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return rc;
}
/**
* Internal worker common for RTTcpServerListen and the thread created by
* RTTcpServerCreate().
*
* The caller makes sure it has its own memory reference and releases it upon
* return.
*/
static int rtTcpServerListen(PRTTCPSERVER pServer)
{
/*
* Accept connection loop.
*/
for (;;)
{
/*
* Change state, getting an extra reference to the socket so we can
* allow others to close it while we're stuck in rtSocketAccept.
*/
RTTCPSERVERSTATE enmState = pServer->enmState;
RTSOCKET hServerSocket;
ASMAtomicXchgHandle(&pServer->hServerSocket, NIL_RTSOCKET, &hServerSocket);
if (hServerSocket != NIL_RTSOCKET)
{
RTSocketRetain(hServerSocket);
ASMAtomicWriteHandle(&pServer->hServerSocket, hServerSocket);
}
if ( enmState != RTTCPSERVERSTATE_ACCEPTING
&& enmState != RTTCPSERVERSTATE_SERVING)
{
RTSocketRelease(hServerSocket);
return rtTcpServerListenCleanup(pServer);
}
if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_ACCEPTING, enmState))
{
RTSocketRelease(hServerSocket);
continue;
}
/*
* Accept connection.
*/
struct sockaddr_in RemoteAddr;
size_t cbRemoteAddr = sizeof(RemoteAddr);
RTSOCKET hClientSocket;
RT_ZERO(RemoteAddr);
int rc = rtSocketAccept(hServerSocket, &hClientSocket, (struct sockaddr *)&RemoteAddr, &cbRemoteAddr);
RTSocketRelease(hServerSocket);
if (RT_FAILURE(rc))
{
/* These are typical for what can happen during destruction. */
if ( rc == VERR_INVALID_HANDLE
|| rc == VERR_INVALID_PARAMETER
|| rc == VERR_NET_NOT_SOCKET)
return rtTcpServerListenCleanup(pServer);
continue;
}
RTSocketSetInheritance(hClientSocket, false /*fInheritable*/);
/*
* Run a pfnServe callback.
*/
if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_SERVING, RTTCPSERVERSTATE_ACCEPTING))
{
rtTcpClose(hClientSocket, "rtTcpServerListen", true /*fTryGracefulShutdown*/);
return rtTcpServerListenCleanup(pServer);
}
RTSocketRetain(hClientSocket);
rtTcpAtomicXchgSock(&pServer->hClientSocket, hClientSocket);
rc = pServer->pfnServe(hClientSocket, pServer->pvUser);
rtTcpServerDestroySocket(&pServer->hClientSocket, "Listener: client (secondary)", true /*fTryGracefulShutdown*/);
RTSocketRelease(hClientSocket);
/*
* Stop the server?
*/
if (rc == VERR_TCP_SERVER_STOP)
{
if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_STOPPING, RTTCPSERVERSTATE_SERVING))
{
/*
* Reset the server socket and change the state to stopped. After that state change
* we cannot safely access the handle so we'll have to return here.
*/
hServerSocket = rtTcpAtomicXchgSock(&pServer->hServerSocket, NIL_RTSOCKET);
rtTcpServerSetState(pServer, RTTCPSERVERSTATE_STOPPED, RTTCPSERVERSTATE_STOPPING);
rtTcpClose(hServerSocket, "Listener: server stopped", false /*fTryGracefulShutdown*/);
}
else
rtTcpServerListenCleanup(pServer); /* ignore rc */
return rc;
}
}
}
/**
* Clean up after listener.
*/
static int rtTcpServerListenCleanup(PRTTCPSERVER pServer)
{
/*
* Close the server socket, the client one shouldn't be set.
*/
rtTcpServerDestroySocket(&pServer->hServerSocket, "ListenCleanup", false /*fTryGracefulShutdown*/);
Assert(pServer->hClientSocket == NIL_RTSOCKET);
/*
* Figure the return code and make sure the state is OK.
*/
RTTCPSERVERSTATE enmState = pServer->enmState;
switch (enmState)
{
case RTTCPSERVERSTATE_STOPPING:
case RTTCPSERVERSTATE_STOPPED:
return VERR_TCP_SERVER_SHUTDOWN;
case RTTCPSERVERSTATE_ACCEPTING:
rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_STOPPED, enmState);
return VERR_TCP_SERVER_DESTROYED;
case RTTCPSERVERSTATE_DESTROYING:
return VERR_TCP_SERVER_DESTROYED;
case RTTCPSERVERSTATE_STARTING:
case RTTCPSERVERSTATE_SERVING:
default:
AssertMsgFailedReturn(("pServer=%p enmState=%d\n", pServer, enmState), VERR_INTERNAL_ERROR_4);
}
}
/**
* Listen and accept one incoming connection.
*
* This is an alternative to RTTcpServerListen for the use the callbacks are not
* possible.
*
* @returns IPRT status code.
* @retval VERR_TCP_SERVER_SHUTDOWN if shut down by RTTcpServerShutdown.
* @retval VERR_INTERRUPTED if the listening was interrupted.
*
* @param pServer The server handle as returned from RTTcpServerCreateEx().
* @param phClientSocket Where to return the socket handle to the client
* connection (on success only). This must be closed
* by calling RTTcpServerDisconnectClient2().
*/
RTR3DECL(int) RTTcpServerListen2(PRTTCPSERVER pServer, PRTSOCKET phClientSocket)
{
/*
* Validate input and retain the instance.
*/
AssertPtrReturn(phClientSocket, VERR_INVALID_HANDLE);
*phClientSocket = NIL_RTSOCKET;
AssertReturn(pServer->u32Magic == RTTCPSERVER_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
int rc = VERR_INVALID_STATE;
for (;;)
{
/*
* Change state, getting an extra reference to the socket so we can
* allow others to close it while we're stuck in rtSocketAccept.
*/
RTTCPSERVERSTATE enmState = pServer->enmState;
RTSOCKET hServerSocket;
ASMAtomicXchgHandle(&pServer->hServerSocket, NIL_RTSOCKET, &hServerSocket);
if (hServerSocket != NIL_RTSOCKET)
{
RTSocketRetain(hServerSocket);
ASMAtomicWriteHandle(&pServer->hServerSocket, hServerSocket);
}
if ( enmState != RTTCPSERVERSTATE_SERVING
&& enmState != RTTCPSERVERSTATE_CREATED)
{
RTSocketRelease(hServerSocket);
return rtTcpServerListenCleanup(pServer);
}
if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_ACCEPTING, enmState))
{
RTSocketRelease(hServerSocket);
continue;
}
Assert(!pServer->pfnServe);
Assert(!pServer->pvUser);
Assert(pServer->Thread == NIL_RTTHREAD);
Assert(pServer->hClientSocket == NIL_RTSOCKET);
/*
* Accept connection.
*/
struct sockaddr_in RemoteAddr;
size_t cbRemoteAddr = sizeof(RemoteAddr);
RTSOCKET hClientSocket;
RT_ZERO(RemoteAddr);
rc = rtSocketAccept(hServerSocket, &hClientSocket, (struct sockaddr *)&RemoteAddr, &cbRemoteAddr);
RTSocketRelease(hServerSocket);
if (RT_FAILURE(rc))
{
if (!rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_CREATED, RTTCPSERVERSTATE_ACCEPTING))
rc = rtTcpServerListenCleanup(pServer);
if (RT_FAILURE(rc))
break;
continue;
}
RTSocketSetInheritance(hClientSocket, false /*fInheritable*/);
/*
* Chance to the 'serving' state and return the socket.
*/
if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_SERVING, RTTCPSERVERSTATE_ACCEPTING))
{
*phClientSocket = hClientSocket;
rc = VINF_SUCCESS;
}
else
{
rtTcpClose(hClientSocket, "RTTcpServerListen2", true /*fTryGracefulShutdown*/);
rc = rtTcpServerListenCleanup(pServer);
}
break;
}
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return rc;
}
/**
* Terminate the open connection to the server.
*
* @returns iprt status code.
* @param pServer Handle to the server.
*/
RTR3DECL(int) RTTcpServerDisconnectClient(PRTTCPSERVER pServer)
{
/*
* Validate input and retain the instance.
*/
AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
AssertReturn(pServer->u32Magic == RTTCPSERVER_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
int rc = rtTcpServerDestroySocket(&pServer->hClientSocket, "DisconnectClient: client", true /*fTryGracefulShutdown*/);
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return rc;
}
/**
* Terminates an open client connect when using RTTcpListen2
*
* @returns IPRT status code.
* @param hClientSocket The client socket handle. This will be invalid upon
* return, whether successful or not. NIL is quietly
* ignored (VINF_SUCCESS).
*/
RTR3DECL(int) RTTcpServerDisconnectClient2(RTSOCKET hClientSocket)
{
return rtTcpClose(hClientSocket, "RTTcpServerDisconnectClient2", true /*fTryGracefulShutdown*/);
}
/**
* Shuts down the server, leaving client connections open.
*
* @returns IPRT status code.
* @param pServer Handle to the server.
*/
RTR3DECL(int) RTTcpServerShutdown(PRTTCPSERVER pServer)
{
/*
* Validate input and retain the instance.
*/
AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
AssertReturn(pServer->u32Magic == RTTCPSERVER_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE);
/*
* Try change the state to stopping, then replace and destroy the server socket.
*/
for (;;)
{
RTTCPSERVERSTATE enmState = pServer->enmState;
if ( enmState != RTTCPSERVERSTATE_ACCEPTING
&& enmState != RTTCPSERVERSTATE_SERVING)
{
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
switch (enmState)
{
case RTTCPSERVERSTATE_CREATED:
case RTTCPSERVERSTATE_STARTING:
default:
AssertMsgFailed(("%d\n", enmState));
return VERR_INVALID_STATE;
case RTTCPSERVERSTATE_STOPPING:
case RTTCPSERVERSTATE_STOPPED:
return VINF_SUCCESS;
case RTTCPSERVERSTATE_DESTROYING:
return VERR_TCP_SERVER_DESTROYED;
}
}
if (rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_STOPPING, enmState))
{
rtTcpServerDestroySocket(&pServer->hServerSocket, "RTTcpServerShutdown", false /*fTryGracefulShutdown*/);
rtTcpServerSetState(pServer, RTTCPSERVERSTATE_STOPPED, RTTCPSERVERSTATE_STOPPING);
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return VINF_SUCCESS;
}
}
}
/**
* Closes down and frees a TCP Server.
* This will also terminate any open connections to the server.
*
* @returns iprt status code.
* @param pServer Handle to the server.
*/
RTR3DECL(int) RTTcpServerDestroy(PRTTCPSERVER pServer)
{
/*
* Validate input and retain the instance.
*/
AssertPtrReturn(pServer, VERR_INVALID_HANDLE);
AssertReturn(pServer->u32Magic == RTTCPSERVER_MAGIC, VERR_INVALID_HANDLE);
AssertReturn(RTMemPoolRetain(pServer) != UINT32_MAX, VERR_INVALID_HANDLE); /* paranoia */
/*
* Move the state along so the listener can figure out what's going on.
*/
for (;;)
{
bool fDestroyable;
RTTCPSERVERSTATE enmState = pServer->enmState;
switch (enmState)
{
case RTTCPSERVERSTATE_STARTING:
case RTTCPSERVERSTATE_ACCEPTING:
case RTTCPSERVERSTATE_SERVING:
case RTTCPSERVERSTATE_CREATED:
case RTTCPSERVERSTATE_STOPPED:
fDestroyable = rtTcpServerTrySetState(pServer, RTTCPSERVERSTATE_DESTROYING, enmState);
break;
/* destroyable states */
case RTTCPSERVERSTATE_STOPPING:
fDestroyable = true;
break;
/*
* Everything else means user or internal misbehavior.
*/
default:
AssertMsgFailed(("pServer=%p enmState=%d\n", pServer, enmState));
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return VERR_INTERNAL_ERROR;
}
if (fDestroyable)
break;
}
/*
* Destroy it.
*/
ASMAtomicWriteU32(&pServer->u32Magic, ~RTTCPSERVER_MAGIC);
rtTcpServerDestroySocket(&pServer->hServerSocket, "Destroyer: server", false /*fTryGracefulShutdown*/);
rtTcpServerDestroySocket(&pServer->hClientSocket, "Destroyer: client", true /*fTryGracefulShutdown*/);
/*
* Release it.
*/
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
RTMemPoolRelease(RTMEMPOOL_DEFAULT, pServer);
return VINF_SUCCESS;
}
RTR3DECL(int) RTTcpClientConnect(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock)
{
return RTTcpClientConnectEx(pszAddress, uPort, pSock, RT_SOCKETCONNECT_DEFAULT_WAIT, NULL);
}
RTR3DECL(int) RTTcpClientConnectEx(const char *pszAddress, uint32_t uPort, PRTSOCKET pSock,
RTMSINTERVAL cMillies, PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie)
{
/*
* Validate input.
*/
AssertReturn(uPort > 0, VERR_INVALID_PARAMETER);
AssertPtrReturn(pszAddress, VERR_INVALID_POINTER);
AssertPtrNullReturn(ppCancelCookie, VERR_INVALID_POINTER);
/*
* Resolve the address.
*/
RTNETADDR Addr;
int rc = RTSocketParseInetAddress(pszAddress, uPort, &Addr);
if (RT_FAILURE(rc))
return rc;
/*
* Create the socket and connect.
*/
RTSOCKET Sock;
rc = rtSocketCreate(&Sock, PF_INET, SOCK_STREAM, 0);
if (RT_SUCCESS(rc))
{
RTSocketSetInheritance(Sock, false /*fInheritable*/);
if (!ppCancelCookie)
rc = rtSocketConnect(Sock, &Addr, cMillies);
else
{
RTSocketRetain(Sock);
if (ASMAtomicCmpXchgPtr(ppCancelCookie, (PRTTCPCLIENTCONNECTCANCEL)Sock, NULL))
{
rc = rtSocketConnect(Sock, &Addr, cMillies);
if (ASMAtomicCmpXchgPtr(ppCancelCookie, NULL, (PRTTCPCLIENTCONNECTCANCEL)Sock))
RTSocketRelease(Sock);
else
rc = VERR_CANCELLED;
}
else
{
RTSocketRelease(Sock);
rc = VERR_CANCELLED;
}
}
if (RT_SUCCESS(rc))
{
*pSock = Sock;
return VINF_SUCCESS;
}
rtTcpClose(Sock, "RTTcpClientConnect", false /*fTryGracefulShutdown*/);
}
if (ppCancelCookie)
*ppCancelCookie = NULL;
return rc;
}
RTR3DECL(int) RTTcpClientCancelConnect(PRTTCPCLIENTCONNECTCANCEL volatile *ppCancelCookie)
{
AssertPtrReturn(ppCancelCookie, VERR_INVALID_POINTER);
RTSOCKET const hSockCancelled = (RTSOCKET)(uintptr_t)0xdead9999;
AssertCompile(NIL_RTSOCKET == NULL);
RTSOCKET hSock = (RTSOCKET)ASMAtomicXchgPtr((void * volatile *)ppCancelCookie, hSockCancelled);
if (hSock != NIL_RTSOCKET && hSock != hSockCancelled)
{
int rc = rtTcpClose(hSock, "RTTcpClientCancelConnect", false /*fTryGracefulShutdown*/);
AssertRCReturn(rc, rc);
}
return VINF_SUCCESS;
}
RTR3DECL(int) RTTcpClientClose(RTSOCKET Sock)
{
return rtTcpClose(Sock, "RTTcpClientClose", true /*fTryGracefulShutdown*/);
}
RTR3DECL(int) RTTcpClientCloseEx(RTSOCKET Sock, bool fGracefulShutdown)
{
return rtTcpClose(Sock, "RTTcpClientCloseEx", fGracefulShutdown);
}
#ifdef FIX_FOR_3_2
/**
* Changes the blocking mode of the socket.
*
* @returns 0 on success, -1 on failure.
* @param hSocket The socket to work on.
* @param fBlocking The desired mode of operation.
*/
static int rtTcpSetBlockingMode(RTHCUINTPTR hSocket, bool fBlocking)
{
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
u_long uBlocking = fBlocking ? 0 : 1;
if (ioctlsocket(hSocket, FIONBIO, &uBlocking))
return -1;
#else
int fFlags = fcntl(hSocket, F_GETFL, 0);
if (fFlags == -1)
return -1;
if (fBlocking)
fFlags &= ~O_NONBLOCK;
else
fFlags |= O_NONBLOCK;
if (fcntl(hSocket, F_SETFL, fFlags) == -1)
return -1;
#endif
return 0;
}
#endif
/**
* Internal close function which does all the proper bitching.
*/
static int rtTcpClose(RTSOCKET Sock, const char *pszMsg, bool fTryGracefulShutdown)
{
NOREF(pszMsg); /** @todo drop this parameter? */
/* ignore nil handles. */
if (Sock == NIL_RTSOCKET)
return VINF_SUCCESS;
/*
* Try to gracefully shut it down.
*/
int rc;
if (fTryGracefulShutdown)
{
rc = RTSocketShutdown(Sock, false /*fRead*/, true /*fWrite*/);
#ifdef FIX_FOR_3_2
RTHCUINTPTR hNative = RTSocketToNative(Sock);
if (RT_SUCCESS(rc) && rtTcpSetBlockingMode(hNative, false /*fBlocking*/) == 0)
#else
if (RT_SUCCESS(rc))
#endif
{
size_t cbReceived = 0;
uint64_t u64Start = RTTimeMilliTS();
while ( cbReceived < _1G
&& RTTimeMilliTS() - u64Start < 30000)
{
#ifdef FIX_FOR_3_2
fd_set FdSetR;
FD_ZERO(&FdSetR);
FD_SET(hNative, &FdSetR);
fd_set FdSetE;
FD_ZERO(&FdSetE);
FD_SET(hNative, &FdSetE);
struct timeval TvTimeout;
TvTimeout.tv_sec = 1;
TvTimeout.tv_usec = 0;
rc = select(hNative + 1, &FdSetR, NULL, &FdSetE, &TvTimeout);
if (rc == 0)
continue;
if (rc < 0)
break;
if (FD_ISSET(hNative, &FdSetE))
break;
#else
uint32_t fEvents;
rc = RTSocketSelectOneEx(Sock, RTSOCKET_EVT_READ | RTSOCKET_EVT_ERROR, &fEvents, 1000);
if (rc == VERR_TIMEOUT)
continue;
if (RT_FAILURE(rc))
break;
if (fEvents & RTSOCKET_EVT_ERROR)
break;
#endif
char abBitBucket[16*_1K];
#ifdef FIX_FOR_3_2
ssize_t cbRead = recv(hNative, &abBitBucket[0], sizeof(abBitBucket), MSG_NOSIGNAL);
if (cbRead == 0)
break; /* orderly shutdown in progress */
if (cbRead < 0 && errno != EAGAIN)
break; /* some kind of error, never mind which... */
#else
size_t cbRead;
rc = RTSocketReadNB(Sock, &abBitBucket[0], sizeof(abBitBucket), &cbRead);
if (RT_FAILURE(rc))
break; /* some kind of error, never mind which... */
if (rc != VINF_TRY_AGAIN && !cbRead)
break; /* orderly shutdown in progress */
#endif
cbReceived += cbRead;
}
}
}
/*
* Close the socket handle (drops our reference to it).
*/
return RTSocketClose(Sock);
}
/**
* Creates connected pair of TCP sockets.
*
* @returns IPRT status code.
* @param phServer Where to return the "server" side of the pair.
* @param phClient Where to return the "client" side of the pair.
*
* @note There is no server or client side, but we gotta call it something.
*/
RTR3DECL(int) RTTcpCreatePair(PRTSOCKET phServer, PRTSOCKET phClient, uint32_t fFlags)
{
/*
* Validate input.
*/
AssertPtrReturn(phServer, VERR_INVALID_PARAMETER);
AssertPtrReturn(phClient, VERR_INVALID_PARAMETER);
AssertReturn(!fFlags, VERR_INVALID_PARAMETER);
/*
* Do the job.
*/
return rtSocketCreateTcpPair(phServer, phClient);
}
RTR3DECL(int) RTTcpRead(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
{
return RTSocketRead(Sock, pvBuffer, cbBuffer, pcbRead);
}
RTR3DECL(int) RTTcpWrite(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer)
{
return RTSocketWrite(Sock, pvBuffer, cbBuffer);
}
RTR3DECL(int) RTTcpFlush(RTSOCKET Sock)
{
int fFlag = 1;
int rc = rtSocketSetOpt(Sock, IPPROTO_TCP, TCP_NODELAY, &fFlag, sizeof(fFlag));
if (RT_SUCCESS(rc))
{
fFlag = 0;
rc = rtSocketSetOpt(Sock, IPPROTO_TCP, TCP_NODELAY, &fFlag, sizeof(fFlag));
}
return rc;
}
RTR3DECL(int) RTTcpSetSendCoalescing(RTSOCKET Sock, bool fEnable)
{
int fFlag = fEnable ? 0 : 1;
return rtSocketSetOpt(Sock, IPPROTO_TCP, TCP_NODELAY, &fFlag, sizeof(fFlag));
}
RTR3DECL(int) RTTcpSetBufferSize(RTSOCKET hSocket, uint32_t cbSize)
{
int cbIntSize = (int)cbSize;
AssertReturn(cbIntSize >= 0, VERR_OUT_OF_RANGE);
int rc = rtSocketSetOpt(hSocket, SOL_SOCKET, SO_SNDBUF, &cbIntSize, sizeof(cbIntSize));
if (RT_SUCCESS(rc))
rc = rtSocketSetOpt(hSocket, SOL_SOCKET, SO_RCVBUF, &cbIntSize, sizeof(cbIntSize));
return rc;
}
RTR3DECL(int) RTTcpSelectOne(RTSOCKET Sock, RTMSINTERVAL cMillies)
{
return RTSocketSelectOne(Sock, cMillies);
}
RTR3DECL(int) RTTcpSelectOneEx(RTSOCKET Sock, uint32_t fEvents, uint32_t *pfEvents,
RTMSINTERVAL cMillies)
{
return RTSocketSelectOneEx(Sock, fEvents, pfEvents, cMillies);
}
RTR3DECL(int) RTTcpGetLocalAddress(RTSOCKET Sock, PRTNETADDR pAddr)
{
return RTSocketGetLocalAddress(Sock, pAddr);
}
RTR3DECL(int) RTTcpGetPeerAddress(RTSOCKET Sock, PRTNETADDR pAddr)
{
return RTSocketGetPeerAddress(Sock, pAddr);
}
RTR3DECL(int) RTTcpSgWrite(RTSOCKET Sock, PCRTSGBUF pSgBuf)
{
return RTSocketSgWrite(Sock, pSgBuf);
}
RTR3DECL(int) RTTcpSgWriteL(RTSOCKET hSocket, size_t cSegs, ...)
{
va_list va;
va_start(va, cSegs);
int rc = RTSocketSgWriteLV(hSocket, cSegs, va);
va_end(va);
return rc;
}
RTR3DECL(int) RTTcpSgWriteLV(RTSOCKET hSocket, size_t cSegs, va_list va)
{
return RTSocketSgWriteLV(hSocket, cSegs, va);
}
RTR3DECL(int) RTTcpReadNB(RTSOCKET Sock, void *pvBuffer, size_t cbBuffer, size_t *pcbRead)
{
return RTSocketReadNB(Sock, pvBuffer, cbBuffer, pcbRead);
}
RTR3DECL(int) RTTcpWriteNB(RTSOCKET Sock, const void *pvBuffer, size_t cbBuffer, size_t *pcbWritten)
{
return RTSocketWriteNB(Sock, pvBuffer, cbBuffer, pcbWritten);
}
RTR3DECL(int) RTTcpSgWriteNB(RTSOCKET Sock, PCRTSGBUF pSgBuf, size_t *pcbWritten)
{
return RTSocketSgWriteNB(Sock, pSgBuf, pcbWritten);
}
RTR3DECL(int) RTTcpSgWriteLNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, ...)
{
va_list va;
va_start(va, pcbWritten);
int rc = RTSocketSgWriteLVNB(hSocket, cSegs, pcbWritten, va);
va_end(va);
return rc;
}
RTR3DECL(int) RTTcpSgWriteLVNB(RTSOCKET hSocket, size_t cSegs, size_t *pcbWritten, va_list va)
{
return RTSocketSgWriteLVNB(hSocket, cSegs, pcbWritten, va);
}
|