forked from KunYi/external_iperf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listener.cpp
716 lines (631 loc) · 26.8 KB
/
Listener.cpp
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
/*---------------------------------------------------------------
* Copyright (c) 1999,2000,2001,2002,2003
* The Board of Trustees of the University of Illinois
* All Rights Reserved.
*---------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software (Iperf) 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:
*
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and
* the following disclaimers.
*
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimers in the documentation and/or other materials
* provided with the distribution.
*
*
* Neither the names of the University of Illinois, NCSA,
* nor the names of its contributors may be used to endorse
* or promote products derived from this Software without
* specific prior written permission.
*
* 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 CONTIBUTORS 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.
* ________________________________________________________________
* National Laboratory for Applied Network Research
* National Center for Supercomputing Applications
* University of Illinois at Urbana-Champaign
* http://www.ncsa.uiuc.edu
* ________________________________________________________________
*
* Listener.cpp
* by Mark Gates <[email protected]>
* & Ajay Tirumala <[email protected]>
* -------------------------------------------------------------------
* Listener sets up a socket listening on the server host. For each
* connected socket that accept() returns, this creates a Server
* socket and spawns a thread for it.
*
* Changes to the latest version. Listener will run as a daemon
* Multicast Server is now Multi-threaded
* -------------------------------------------------------------------
* headers
* uses
* <stdlib.h>
* <stdio.h>
* <string.h>
* <errno.h>
*
* <sys/types.h>
* <unistd.h>
*
* <netdb.h>
* <netinet/in.h>
* <sys/socket.h>
* ------------------------------------------------------------------- */
#define HEADERS()
#include "headers.h"
#include "Listener.hpp"
#include "SocketAddr.h"
#include "PerfSocket.hpp"
#include "List.h"
#include "util.h"
/* -------------------------------------------------------------------
* Stores local hostname and socket info.
* ------------------------------------------------------------------- */
Listener::Listener( thread_Settings *inSettings ) {
mClients = inSettings->mThreads;
mBuf = NULL;
mSettings = inSettings;
// initialize buffer
mBuf = new char[ mSettings->mBufLen ];
// open listening socket
Listen( );
ReportSettings( inSettings );
} // end Listener
/* -------------------------------------------------------------------
* Delete memory (buffer).
* ------------------------------------------------------------------- */
Listener::~Listener() {
if ( mSettings->mSock != INVALID_SOCKET ) {
int rc = close( mSettings->mSock );
WARN_errno( rc == SOCKET_ERROR, "close" );
mSettings->mSock = INVALID_SOCKET;
}
DELETE_ARRAY( mBuf );
} // end ~Listener
/* -------------------------------------------------------------------
* Listens for connections and starts Servers to handle data.
* For TCP, each accepted connection spawns a Server thread.
* For UDP, handle all data in this thread for Win32 Only, otherwise
* spawn a new Server thread.
* ------------------------------------------------------------------- */
void Listener::Run( void ) {
#ifdef WIN32
if ( isUDP( mSettings ) && !isSingleUDP( mSettings ) ) {
UDPSingleServer();
} else
#else
#ifdef sun
if ( ( isUDP( mSettings ) &&
isMulticast( mSettings ) &&
!isSingleUDP( mSettings ) ) ||
isSingleUDP( mSettings ) ) {
UDPSingleServer();
} else
#else
if ( isSingleUDP( mSettings ) ) {
UDPSingleServer();
} else
#endif
#endif
{
bool client = false, UDP = isUDP( mSettings ), mCount = (mSettings->mThreads != 0);
thread_Settings *tempSettings = NULL;
Iperf_ListEntry *exist, *listtemp;
client_hdr* hdr = ( UDP ? (client_hdr*) (((UDP_datagram*)mBuf) + 1) :
(client_hdr*) mBuf);
if ( mSettings->mHost != NULL ) {
client = true;
SockAddr_remoteAddr( mSettings );
}
Settings_Copy( mSettings, &server );
server->mThreadMode = kMode_Server;
// Accept each packet,
// If there is no existing client, then start
// a new thread to service the new client
// The listener runs in a single thread
// Thread per client model is followed
do {
// Get a new socket
Accept( server );
if ( server->mSock == INVALID_SOCKET ) {
break;
}
if ( sInterupted != 0 ) {
close( server->mSock );
break;
}
// Reset Single Client Stuff
if ( isSingleClient( mSettings ) && clients == NULL ) {
mSettings->peer = server->peer;
mClients--;
client = true;
// Once all the server threads exit then quit
// Must keep going in case this client sends
// more streams
if ( mClients == 0 ) {
thread_release_nonterm( 0 );
mClients = 1;
}
}
// Verify that it is allowed
if ( client ) {
if ( !SockAddr_Hostare_Equal( (sockaddr*) &mSettings->peer,
(sockaddr*) &server->peer ) ) {
// Not allowed try again
close( server->mSock );
if ( isUDP( mSettings ) ) {
mSettings->mSock = -1;
Listen();
}
continue;
}
}
// Create an entry for the connection list
listtemp = new Iperf_ListEntry;
memcpy(listtemp, &server->peer, sizeof(iperf_sockaddr));
listtemp->next = NULL;
// See if we need to do summing
Mutex_Lock( &clients_mutex );
exist = Iperf_hostpresent( &server->peer, clients);
if ( exist != NULL ) {
// Copy group ID
listtemp->holder = exist->holder;
server->multihdr = exist->holder;
} else {
server->mThreads = 0;
Mutex_Lock( &groupCond );
groupID--;
listtemp->holder = InitMulti( server, groupID );
server->multihdr = listtemp->holder;
Mutex_Unlock( &groupCond );
}
// Store entry in connection list
Iperf_pushback( listtemp, &clients );
Mutex_Unlock( &clients_mutex );
tempSettings = NULL;
if ( !isCompat( mSettings ) && !isMulticast( mSettings ) ) {
if ( !UDP ) {
// TCP does not have the info yet
if ( recv( server->mSock, (char*)hdr, sizeof(client_hdr), 0) > 0 ) {
Settings_GenerateClientSettings( server, &tempSettings,
hdr );
}
} else {
Settings_GenerateClientSettings( server, &tempSettings,
hdr );
}
}
if ( tempSettings != NULL ) {
client_init( tempSettings );
if ( tempSettings->mMode == kTest_DualTest ) {
#ifdef HAVE_THREAD
server->runNow = tempSettings;
#else
server->runNext = tempSettings;
#endif
} else {
server->runNext = tempSettings;
}
}
// Start the server
#if defined(WIN32) && defined(HAVE_THREAD)
if ( UDP ) {
// WIN32 does bad UDP handling so run single threaded
if ( server->runNow != NULL ) {
thread_start( server->runNow );
}
server_spawn( server );
if ( server->runNext != NULL ) {
thread_start( server->runNext );
}
} else
#endif
thread_start( server );
// create a new socket
if ( UDP ) {
mSettings->mSock = -1;
Listen( );
}
// Prep for next connection
if ( !isSingleClient( mSettings ) ) {
mClients--;
}
Settings_Copy( mSettings, &server );
server->mThreadMode = kMode_Server;
} while ( !sInterupted && (!mCount || ( mCount && mClients > 0 )) );
Settings_Destroy( server );
}
} // end Run
/* -------------------------------------------------------------------
* Setup a socket listening on a port.
* For TCP, this calls bind() and listen().
* For UDP, this just calls bind().
* If inLocalhost is not null, bind to that address rather than the
* wildcard server address, specifying what incoming interface to
* accept connections on.
* ------------------------------------------------------------------- */
void Listener::Listen( ) {
int rc;
SockAddr_localAddr( mSettings );
// create an internet TCP socket
int type = (isUDP( mSettings ) ? SOCK_DGRAM : SOCK_STREAM);
int domain = (SockAddr_isIPv6( &mSettings->local ) ?
#ifdef HAVE_IPV6
AF_INET6
#else
AF_INET
#endif
: AF_INET);
#ifdef WIN32
if ( SockAddr_isMulticast( &mSettings->local ) ) {
// Multicast on Win32 requires special handling
mSettings->mSock = WSASocket( domain, type, 0, 0, 0, WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF );
WARN_errno( mSettings->mSock == INVALID_SOCKET, "socket" );
} else
#endif
{
mSettings->mSock = socket( domain, type, 0 );
WARN_errno( mSettings->mSock == INVALID_SOCKET, "socket" );
}
SetSocketOptions( mSettings );
// reuse the address, so we can run if a former server was killed off
int boolean = 1;
Socklen_t len = sizeof(boolean);
setsockopt( mSettings->mSock, SOL_SOCKET, SO_REUSEADDR, (char*) &boolean, len );
// bind socket to server address
#ifdef WIN32
if ( SockAddr_isMulticast( &mSettings->local ) ) {
// Multicast on Win32 requires special handling
rc = WSAJoinLeaf( mSettings->mSock, (sockaddr*) &mSettings->local, mSettings->size_local,0,0,0,0,JL_BOTH);
WARN_errno( rc == SOCKET_ERROR, "WSAJoinLeaf (aka bind)" );
} else
#endif
{
rc = bind( mSettings->mSock, (sockaddr*) &mSettings->local, mSettings->size_local );
WARN_errno( rc == SOCKET_ERROR, "bind" );
}
// listen for connections (TCP only).
// default backlog traditionally 5
if ( !isUDP( mSettings ) ) {
rc = listen( mSettings->mSock, 5 );
WARN_errno( rc == SOCKET_ERROR, "listen" );
}
#ifndef WIN32
// if multicast, join the group
if ( SockAddr_isMulticast( &mSettings->local ) ) {
McastJoin( );
}
#endif
} // end Listen
/* -------------------------------------------------------------------
* Joins the multicast group, with the default interface.
* ------------------------------------------------------------------- */
void Listener::McastJoin( ) {
#ifdef HAVE_MULTICAST
if ( !SockAddr_isIPv6( &mSettings->local ) ) {
struct ip_mreq mreq;
memcpy( &mreq.imr_multiaddr, SockAddr_get_in_addr( &mSettings->local ),
sizeof(mreq.imr_multiaddr));
mreq.imr_interface.s_addr = htonl( INADDR_ANY );
int rc = setsockopt( mSettings->mSock, IPPROTO_IP, IP_ADD_MEMBERSHIP,
(char*) &mreq, sizeof(mreq));
WARN_errno( rc == SOCKET_ERROR, "multicast join" );
}
#ifdef HAVE_IPV6_MULTICAST
else {
struct ipv6_mreq mreq;
memcpy( &mreq.ipv6mr_multiaddr, SockAddr_get_in6_addr( &mSettings->local ),
sizeof(mreq.ipv6mr_multiaddr));
mreq.ipv6mr_interface = 0;
int rc = setsockopt( mSettings->mSock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
(char*) &mreq, sizeof(mreq));
WARN_errno( rc == SOCKET_ERROR, "multicast join" );
}
#endif
#endif
}
// end McastJoin
/* -------------------------------------------------------------------
* Sets the Multicast TTL for outgoing packets.
* ------------------------------------------------------------------- */
void Listener::McastSetTTL( int val ) {
#ifdef HAVE_MULTICAST
if ( !SockAddr_isIPv6( &mSettings->local ) ) {
int rc = setsockopt( mSettings->mSock, IPPROTO_IP, IP_MULTICAST_TTL,
(char*) &val, sizeof(val));
WARN_errno( rc == SOCKET_ERROR, "multicast ttl" );
}
#ifdef HAVE_IPV6_MULTICAST
else {
int rc = setsockopt( mSettings->mSock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
(char*) &val, sizeof(val));
WARN_errno( rc == SOCKET_ERROR, "multicast ttl" );
}
#endif
#endif
}
// end McastSetTTL
/* -------------------------------------------------------------------
* After Listen() has setup mSock, this will block
* until a new connection arrives.
* ------------------------------------------------------------------- */
void Listener::Accept( thread_Settings *server ) {
server->size_peer = sizeof(iperf_sockaddr);
if ( isUDP( server ) ) {
/* -------------------------------------------------------------------
* Do the equivalent of an accept() call for UDP sockets. This waits
* on a listening UDP socket until we get a datagram.
* ------------------------------------------------------------------- */
int rc;
Iperf_ListEntry *exist;
int32_t datagramID;
server->mSock = INVALID_SOCKET;
while ( server->mSock == INVALID_SOCKET ) {
rc = recvfrom( mSettings->mSock, mBuf, mSettings->mBufLen, 0,
(struct sockaddr*) &server->peer, &server->size_peer );
FAIL_errno( rc == SOCKET_ERROR, "recvfrom", mSettings );
Mutex_Lock( &clients_mutex );
// Handle connection for UDP sockets.
exist = Iperf_present( &server->peer, clients);
datagramID = ntohl( ((UDP_datagram*) mBuf)->id );
if ( exist == NULL && datagramID >= 0 ) {
server->mSock = mSettings->mSock;
int rc = connect( server->mSock, (struct sockaddr*) &server->peer,
server->size_peer );
FAIL_errno( rc == SOCKET_ERROR, "connect UDP", mSettings );
} else {
server->mSock = INVALID_SOCKET;
}
Mutex_Unlock( &clients_mutex );
}
} else {
// Handles interupted accepts. Returns the newly connected socket.
server->mSock = INVALID_SOCKET;
while ( server->mSock == INVALID_SOCKET ) {
// accept a connection
server->mSock = accept( mSettings->mSock,
(sockaddr*) &server->peer, &server->size_peer );
if ( server->mSock == INVALID_SOCKET && errno == EINTR ) {
continue;
}
}
}
server->size_local = sizeof(iperf_sockaddr);
getsockname( server->mSock, (sockaddr*) &server->local,
&server->size_local );
} // end Accept
void Listener::UDPSingleServer( ) {
bool client = false, UDP = isUDP( mSettings ), mCount = (mSettings->mThreads != 0);
thread_Settings *tempSettings = NULL;
Iperf_ListEntry *exist, *listtemp;
int rc;
int32_t datagramID;
client_hdr* hdr = ( UDP ? (client_hdr*) (((UDP_datagram*)mBuf) + 1) :
(client_hdr*) mBuf);
ReportStruct *reportstruct = new ReportStruct;
if ( mSettings->mHost != NULL ) {
client = true;
SockAddr_remoteAddr( mSettings );
}
Settings_Copy( mSettings, &server );
server->mThreadMode = kMode_Server;
// Accept each packet,
// If there is no existing client, then start
// a new report to service the new client
// The listener runs in a single thread
Mutex_Lock( &clients_mutex );
do {
// Get next packet
while ( sInterupted == 0) {
server->size_peer = sizeof( iperf_sockaddr );
rc = recvfrom( mSettings->mSock, mBuf, mSettings->mBufLen, 0,
(struct sockaddr*) &server->peer, &server->size_peer );
WARN_errno( rc == SOCKET_ERROR, "recvfrom" );
if ( rc == SOCKET_ERROR ) {
return;
}
// Handle connection for UDP sockets.
exist = Iperf_present( &server->peer, clients);
datagramID = ntohl( ((UDP_datagram*) mBuf)->id );
if ( datagramID >= 0 ) {
if ( exist != NULL ) {
// read the datagram ID and sentTime out of the buffer
reportstruct->packetID = datagramID;
reportstruct->sentTime.tv_sec = ntohl( ((UDP_datagram*) mBuf)->tv_sec );
reportstruct->sentTime.tv_usec = ntohl( ((UDP_datagram*) mBuf)->tv_usec );
reportstruct->packetLen = rc;
gettimeofday( &(reportstruct->packetTime), NULL );
ReportPacket( exist->server->reporthdr, reportstruct );
} else {
Mutex_Lock( &groupCond );
groupID--;
server->mSock = -groupID;
Mutex_Unlock( &groupCond );
server->size_local = sizeof(iperf_sockaddr);
getsockname( mSettings->mSock, (sockaddr*) &server->local,
&server->size_local );
break;
}
} else {
if ( exist != NULL ) {
// read the datagram ID and sentTime out of the buffer
reportstruct->packetID = -datagramID;
reportstruct->sentTime.tv_sec = ntohl( ((UDP_datagram*) mBuf)->tv_sec );
reportstruct->sentTime.tv_usec = ntohl( ((UDP_datagram*) mBuf)->tv_usec );
reportstruct->packetLen = rc;
gettimeofday( &(reportstruct->packetTime), NULL );
ReportPacket( exist->server->reporthdr, reportstruct );
// stop timing
gettimeofday( &(reportstruct->packetTime), NULL );
CloseReport( exist->server->reporthdr, reportstruct );
if ( rc > (int) ( sizeof( UDP_datagram )
+ sizeof( server_hdr ) ) ) {
UDP_datagram *UDP_Hdr;
server_hdr *hdr;
UDP_Hdr = (UDP_datagram*) mBuf;
Transfer_Info *stats = GetReport( exist->server->reporthdr );
hdr = (server_hdr*) (UDP_Hdr+1);
hdr->flags = htonl( HEADER_VERSION1 );
hdr->total_len1 = htonl( (long) (stats->TotalLen >> 32) );
hdr->total_len2 = htonl( (long) (stats->TotalLen & 0xFFFFFFFF) );
hdr->stop_sec = htonl( (long) stats->endTime );
hdr->stop_usec = htonl( (long)((stats->endTime - (long)stats->endTime)
* rMillion));
hdr->error_cnt = htonl( stats->cntError );
hdr->outorder_cnt = htonl( stats->cntOutofOrder );
hdr->datagrams = htonl( stats->cntDatagrams );
hdr->jitter1 = htonl( (long) stats->jitter );
hdr->jitter2 = htonl( (long) ((stats->jitter - (long)stats->jitter)
* rMillion) );
}
EndReport( exist->server->reporthdr );
exist->server->reporthdr = NULL;
Iperf_delete( &(exist->server->peer), &clients );
} else if ( rc > (int) ( sizeof( UDP_datagram )
+ sizeof( server_hdr ) ) ) {
UDP_datagram *UDP_Hdr;
server_hdr *hdr;
UDP_Hdr = (UDP_datagram*) mBuf;
hdr = (server_hdr*) (UDP_Hdr+1);
hdr->flags = htonl( 0 );
}
sendto( mSettings->mSock, mBuf, mSettings->mBufLen, 0,
(struct sockaddr*) &server->peer, server->size_peer);
}
}
if ( server->mSock == INVALID_SOCKET ) {
break;
}
if ( sInterupted != 0 ) {
close( server->mSock );
break;
}
// Reset Single Client Stuff
if ( isSingleClient( mSettings ) && clients == NULL ) {
mSettings->peer = server->peer;
mClients--;
client = true;
// Once all the server threads exit then quit
// Must keep going in case this client sends
// more streams
if ( mClients == 0 ) {
thread_release_nonterm( 0 );
mClients = 1;
}
}
// Verify that it is allowed
if ( client ) {
if ( !SockAddr_Hostare_Equal( (sockaddr*) &mSettings->peer,
(sockaddr*) &server->peer ) ) {
// Not allowed try again
connect( mSettings->mSock,
(sockaddr*) &server->peer,
server->size_peer );
close( mSettings->mSock );
mSettings->mSock = -1;
Listen( );
continue;
}
}
// Create an entry for the connection list
listtemp = new Iperf_ListEntry;
memcpy(listtemp, &server->peer, sizeof(iperf_sockaddr));
listtemp->server = server;
listtemp->next = NULL;
// See if we need to do summing
exist = Iperf_hostpresent( &server->peer, clients);
if ( exist != NULL ) {
// Copy group ID
listtemp->holder = exist->holder;
server->multihdr = exist->holder;
} else {
server->mThreads = 0;
Mutex_Lock( &groupCond );
groupID--;
listtemp->holder = InitMulti( server, groupID );
server->multihdr = listtemp->holder;
Mutex_Unlock( &groupCond );
}
// Store entry in connection list
Iperf_pushback( listtemp, &clients );
tempSettings = NULL;
if ( !isCompat( mSettings ) && !isMulticast( mSettings ) ) {
Settings_GenerateClientSettings( server, &tempSettings,
hdr );
}
if ( tempSettings != NULL ) {
client_init( tempSettings );
if ( tempSettings->mMode == kTest_DualTest ) {
#ifdef HAVE_THREAD
thread_start( tempSettings );
#else
server->runNext = tempSettings;
#endif
} else {
server->runNext = tempSettings;
}
}
server->reporthdr = InitReport( server );
// Prep for next connection
if ( !isSingleClient( mSettings ) ) {
mClients--;
}
Settings_Copy( mSettings, &server );
server->mThreadMode = kMode_Server;
} while ( !sInterupted && (!mCount || ( mCount && mClients > 0 )) );
Mutex_Unlock( &clients_mutex );
Settings_Destroy( server );
}
/* --------------------------------------------------------------------
* Run the server as a daemon
* --------------------------------------------------------------------*/
void Listener::runAsDaemon(const char *pname, int facility) {
#ifndef WIN32
pid_t pid;
/* Create a child process & if successful, exit from the parent process */
if ( (pid = fork()) == -1 ) {
fprintf( stderr, "error in first child create\n");
exit(0);
} else if ( pid != 0 ) {
exit(0);
}
/* Try becoming the session leader, once the parent exits */
if ( setsid() == -1 ) { /* Become the session leader */
fprintf( stderr, "Cannot change the session group leader\n");
} else {
}
signal(SIGHUP,SIG_IGN);
/* Now fork() and get released from the terminal */
if ( (pid = fork()) == -1 ) {
fprintf( stderr, "error\n");
exit(0);
} else if ( pid != 0 ) {
exit(0);
}
chdir(".");
fprintf( stderr, "Running Iperf Server as a daemon\n");
fprintf( stderr, "The Iperf daemon process ID : %d\n",((int)getpid()));
fflush(stderr);
fclose(stdin);
#else
fprintf( stderr, "Use the precompiled windows version for service (daemon) option\n");
#endif
}