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
|
/*
* ZeroTier One - Global Peer to Peer Ethernet
* Copyright (C) 2011-2014 ZeroTier Networks LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include "SocketManager.hpp"
#include "UdpSocket.hpp"
#include "TcpSocket.hpp"
#ifndef __WINDOWS__
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <signal.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
// Uncomment to turn off TCP Nagle
//#define ZT_TCP_NODELAY
// Allow us to use the same value on Windows and *nix
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif
#ifdef __WINDOWS__
#define CLOSE_SOCKET(s) ::closesocket(s)
#else
#define CLOSE_SOCKET(s) ::close(s)
#endif
namespace ZeroTier {
#ifdef __WINDOWS__
// hack copied from StackOverflow, behaves a bit like pipe() on *nix systems
static inline void winPipeHack(SOCKET fds[2])
{
struct sockaddr_in inaddr;
struct sockaddr addr;
SOCKET lst=::socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
memset(&inaddr, 0, sizeof(inaddr));
memset(&addr, 0, sizeof(addr));
inaddr.sin_family = AF_INET;
inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
inaddr.sin_port = 0;
int yes=1;
setsockopt(lst,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr));
listen(lst,1);
int len=sizeof(inaddr);
getsockname(lst, &addr,&len);
fds[0]=::socket(AF_INET, SOCK_STREAM,0);
connect(fds[0],&addr,len);
fds[1]=accept(lst,0,0);
closesocket(lst);
}
#endif
SocketManager::SocketManager(
int localUdpPort,
int localTcpPort,
void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
void *arg) :
_whackSendPipe(INVALID_SOCKET),
_whackReceivePipe(INVALID_SOCKET),
_tcpV4ListenSocket(INVALID_SOCKET),
_tcpV6ListenSocket(INVALID_SOCKET),
_nfds(0),
_packetHandler(packetHandler),
_arg(arg)
{
FD_ZERO(&_readfds);
FD_ZERO(&_writefds);
// Create a pipe or socket pair that can be used to interrupt select()
#ifdef __WINDOWS__
{
SOCKET tmps[2] = { INVALID_SOCKET,INVALID_SOCKET };
winPipeHack(tmps);
_whackSendPipe = tmps[0];
_whackReceivePipe = tmps[1];
u_long iMode=1;
ioctlsocket(tmps[1],FIONBIO,&iMode);
}
#else
{
int tmpfds[2];
if (::pipe(tmpfds))
throw std::runtime_error("pipe() failed");
_whackSendPipe = tmpfds[1];
_whackReceivePipe = tmpfds[0];
fcntl(_whackReceivePipe,F_SETFL,O_NONBLOCK);
}
#endif
FD_SET(_whackReceivePipe,&_readfds);
if (localTcpPort > 0) {
if (localTcpPort > 0xffff) {
_closeSockets();
throw std::runtime_error("invalid local TCP port number");
}
{ // bind TCP IPv6
_tcpV6ListenSocket = ::socket(AF_INET6,SOCK_STREAM,0);
#ifdef __WINDOWS__
if (_tcpV6ListenSocket == INVALID_SOCKET) {
#else
if (_tcpV6ListenSocket <= 0) {
#endif
_closeSockets();
throw std::runtime_error("unable to create IPv6 SOCK_STREAM socket");
}
#ifdef __WINDOWS__
{
BOOL f;
f = TRUE; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
f = TRUE; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
u_long iMode=1;
ioctlsocket(_tcpV6ListenSocket,FIONBIO,&iMode);
}
#else
{
int f;
f = 1; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
f = 1; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
fcntl(_tcpV6ListenSocket,F_SETFL,O_NONBLOCK);
}
#endif
struct sockaddr_in6 sin6;
memset(&sin6,0,sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(localTcpPort);
memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
if (::bind(_tcpV6ListenSocket,(const struct sockaddr *)&sin6,sizeof(sin6))) {
_closeSockets();
throw std::runtime_error("unable to bind to local TCP port");
}
if (::listen(_tcpV6ListenSocket,16)) {
_closeSockets();
throw std::runtime_error("listen() failed");
}
FD_SET(_tcpV6ListenSocket,&_readfds);
}
{ // bind TCP IPv4
_tcpV4ListenSocket = ::socket(AF_INET,SOCK_STREAM,0);
#ifdef __WINDOWS__
if (_tcpV4ListenSocket == INVALID_SOCKET) {
#else
if (_tcpV4ListenSocket <= 0) {
#endif
_closeSockets();
throw std::runtime_error("unable to create IPv4 SOCK_STREAM socket");
}
#ifdef __WINDOWS__
{
BOOL f = TRUE; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
u_long iMode=1;
ioctlsocket(_tcpV4ListenSocket,FIONBIO,&iMode);
}
#else
{
int f = 1; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
fcntl(_tcpV4ListenSocket,F_SETFL,O_NONBLOCK);
}
#endif
struct sockaddr_in sin4;
memset(&sin4,0,sizeof(sin4));
sin4.sin_family = AF_INET;
sin4.sin_port = htons(localTcpPort);
sin4.sin_addr.s_addr = INADDR_ANY;
if (::bind(_tcpV4ListenSocket,(const struct sockaddr *)&sin4,sizeof(sin4))) {
_closeSockets();
throw std::runtime_error("unable to bind to local TCP port");
}
if (::listen(_tcpV4ListenSocket,16)) {
_closeSockets();
throw std::runtime_error("listen() failed");
}
FD_SET(_tcpV4ListenSocket,&_readfds);
}
}
if (localUdpPort > 0) {
if (localUdpPort > 0xffff) {
_closeSockets();
throw std::runtime_error("invalid local UDP port number");
}
{ // bind UDP IPv6
#ifdef __WINDOWS__
SOCKET s = ::socket(AF_INET6,SOCK_DGRAM,0);
if (s == INVALID_SOCKET) {
_closeSockets();
throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
}
#else
int s = ::socket(AF_INET6,SOCK_DGRAM,0);
if (s <= 0) {
_closeSockets();
throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
}
#endif
{
#ifdef __WINDOWS__
BOOL f;
f = TRUE; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
f = FALSE; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&f,sizeof(f));
#else
int f;
f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
#ifdef IP_DONTFRAG
f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
#endif
#ifdef IP_MTU_DISCOVER
f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
#endif
#ifdef IPV6_MTU_DISCOVER
f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
#endif
#endif
}
struct sockaddr_in6 sin6;
memset(&sin6,0,sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(localUdpPort);
memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
if (::bind(s,(const struct sockaddr *)&sin6,sizeof(sin6))) {
CLOSE_SOCKET(s);
_closeSockets();
throw std::runtime_error("unable to bind to port");
}
_udpV6Socket = SharedPtr<Socket>(new UdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V6,s));
#ifdef __WINDOWS__
u_long iMode=1;
ioctlsocket(s,FIONBIO,&iMode);
#else
fcntl(s,F_SETFL,O_NONBLOCK);
#endif
FD_SET(s,&_readfds);
}
{ // bind UDP IPv4
#ifdef __WINDOWS__
SOCKET s = ::socket(AF_INET,SOCK_DGRAM,0);
if (s == INVALID_SOCKET) {
_closeSockets();
throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
}
#else
int s = ::socket(AF_INET,SOCK_DGRAM,0);
if (s <= 0) {
_closeSockets();
throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
}
#endif
{
#ifdef __WINDOWS__
BOOL f;
f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
f = FALSE; setsockopt(s,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&f,sizeof(f));
#else
int f;
f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
#ifdef IP_DONTFRAG
f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
#endif
#ifdef IP_MTU_DISCOVER
f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
#endif
#endif
}
struct sockaddr_in sin4;
memset(&sin4,0,sizeof(sin4));
sin4.sin_family = AF_INET;
sin4.sin_port = htons(localUdpPort);
sin4.sin_addr.s_addr = INADDR_ANY;
if (::bind(s,(const struct sockaddr *)&sin4,sizeof(sin4))) {
CLOSE_SOCKET(s);
_closeSockets();
throw std::runtime_error("unable to bind to port");
}
_udpV4Socket = SharedPtr<Socket>(new UdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V4,s));
#ifdef __WINDOWS__
u_long iMode=1;
ioctlsocket(s,FIONBIO,&iMode);
#else
fcntl(s,F_SETFL,O_NONBLOCK);
#endif
FD_SET(s,&_readfds);
}
}
_updateNfds();
}
SocketManager::~SocketManager()
{
Mutex::Lock _l(_pollLock);
_closeSockets();
}
bool SocketManager::send(const InetAddress &to,bool tcp,const void *msg,unsigned int msglen)
{
if (tcp) {
SharedPtr<Socket> ts;
{
Mutex::Lock _l(_tcpSockets_m);
std::map< InetAddress,SharedPtr<Socket> >::iterator opents(_tcpSockets.find(to));
if (opents != _tcpSockets.end())
ts = opents->second;
}
if (ts)
return ts->send(to,msg,msglen);
#ifdef __WINDOWS__
SOCKET s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
if (s == INVALID_SOCKET)
return false;
{ u_long iMode=1; ioctlsocket(s,FIONBIO,&iMode); }
#ifdef ZT_TCP_NODELAY
{ BOOL f = TRUE; setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
#endif
#else
int s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
if (s <= 0)
return false;
if (s >= FD_SETSIZE) {
::close(s);
return false;
}
fcntl(s,F_SETFL,O_NONBLOCK);
#ifdef ZT_TCP_NODELAY
{ int f = 1; setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
#endif
#endif
bool connecting = false;
if (::connect(s,to.saddr(),to.saddrLen())) {
#ifdef __WINDOWS__
if (WSAGetLastError() != WSAEWOULDBLOCK) {
#else
if (errno != EINPROGRESS) {
#endif
CLOSE_SOCKET(s);
return false;
} else connecting = true;
}
ts = SharedPtr<Socket>(new TcpSocket(this,s,connecting,to));
if (!ts->send(to,msg,msglen)) {
_fdSetLock.lock();
FD_CLR(s,&_readfds);
FD_CLR(s,&_writefds);
_fdSetLock.unlock();
return false;
}
{
Mutex::Lock _l(_tcpSockets_m);
_tcpSockets[to] = ts;
}
_fdSetLock.lock();
FD_SET(s,&_readfds);
if (connecting)
FD_SET(s,&_writefds);
_fdSetLock.unlock();
_updateNfds();
whack();
return true;
} else if (to.isV4()) {
if (_udpV4Socket)
return _udpV4Socket->send(to,msg,msglen);
} else if (to.isV6()) {
if (_udpV6Socket)
return _udpV6Socket->send(to,msg,msglen);
}
return false;
}
bool SocketManager::sendFirewallOpener(const InetAddress &to,int hopLimit)
{
if (to.isV4()) {
if (_udpV4Socket)
return ((UdpSocket *)_udpV4Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
} else if (to.isV6()) {
if (_udpV6Socket)
return ((UdpSocket *)_udpV6Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
}
return false;
}
void SocketManager::poll(unsigned long timeout)
{
fd_set rfds,wfds,efds;
struct timeval tv;
std::vector< SharedPtr<Socket> > ts;
#ifdef __WINDOWS__
SOCKET sockfd;
#else
int sockfd;
#endif
Mutex::Lock _l(_pollLock);
_fdSetLock.lock();
memcpy(&rfds,&_readfds,sizeof(rfds));
memcpy(&wfds,&_writefds,sizeof(wfds));
_fdSetLock.unlock();
FD_ZERO(&efds);
#ifdef __WINDOWS__
// Windows signals failed connects in exceptfds
{
Mutex::Lock _l2(_tcpSockets_m);
for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
if (((TcpSocket *)s->second.ptr())->_connecting)
FD_SET(s->second->_sock,&efds);
}
}
#endif
tv.tv_sec = (long)(timeout / 1000);
tv.tv_usec = (long)((timeout % 1000) * 1000);
select(_nfds + 1,&rfds,&wfds,&efds,(timeout > 0) ? &tv : (struct timeval *)0);
if (FD_ISSET(_whackReceivePipe,&rfds)) {
char tmp[16];
#ifdef __WINDOWS__
::recv(_whackReceivePipe,tmp,16,0);
#else
::read(_whackReceivePipe,tmp,16);
#endif
}
if ((_tcpV4ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV4ListenSocket,&rfds))) {
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
sockfd = accept(_tcpV4ListenSocket,(struct sockaddr *)&from,&fromlen);
#ifdef __WINDOWS__
if (sockfd != INVALID_SOCKET) {
#else
if (sockfd > 0) {
if (sockfd < FD_SETSIZE) {
#endif
InetAddress fromia((const struct sockaddr *)&from);
Mutex::Lock _l2(_tcpSockets_m);
try {
_tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(this,sockfd,false,fromia));
#ifdef __WINDOWS__
{ u_long iMode=1; ioctlsocket(sockfd,FIONBIO,&iMode); }
#ifdef ZT_TCP_NODELAY
{ BOOL f = TRUE; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
#endif
#else
fcntl(sockfd,F_SETFL,O_NONBLOCK);
#ifdef ZT_TCP_NODELAY
{ int f = 1; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
#endif
#endif
_fdSetLock.lock();
FD_SET(sockfd,&_readfds);
_fdSetLock.unlock();
if ((int)sockfd > (int)_nfds)
_nfds = (int)sockfd;
} catch ( ... ) {
CLOSE_SOCKET(sockfd);
}
#ifndef __WINDOWS__
} else {
CLOSE_SOCKET(sockfd);
}
#endif
}
}
if ((_tcpV6ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV6ListenSocket,&rfds))) {
struct sockaddr_in6 from;
socklen_t fromlen = sizeof(from);
sockfd = accept(_tcpV6ListenSocket,(struct sockaddr *)&from,&fromlen);
#ifdef __WINDOWS__
if (sockfd != INVALID_SOCKET) {
#else
if (sockfd > 0) {
if (sockfd < FD_SETSIZE) {
#endif
InetAddress fromia((const struct sockaddr *)&from);
Mutex::Lock _l2(_tcpSockets_m);
try {
_tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(this,sockfd,false,fromia));
#ifdef __WINDOWS__
{ u_long iMode=1; ioctlsocket(sockfd,FIONBIO,&iMode); }
#ifdef ZT_TCP_NODELAY
{ BOOL f = TRUE; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
#endif
#else
fcntl(sockfd,F_SETFL,O_NONBLOCK);
#ifdef ZT_TCP_NODELAY
{ int f = 1; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
#endif
#endif
_fdSetLock.lock();
FD_SET(sockfd,&_readfds);
_fdSetLock.unlock();
if ((int)sockfd > (int)_nfds)
_nfds = (int)sockfd;
} catch ( ... ) {
CLOSE_SOCKET(sockfd);
}
#ifndef __WINDOWS__
} else {
CLOSE_SOCKET(sockfd);
}
#endif
}
}
if ((_udpV4Socket)&&(FD_ISSET(_udpV4Socket->_sock,&rfds))) {
_udpV4Socket->notifyAvailableForRead(_udpV4Socket,this);
}
if ((_udpV6Socket)&&(FD_ISSET(_udpV6Socket->_sock,&rfds))) {
_udpV6Socket->notifyAvailableForRead(_udpV6Socket,this);
}
bool closedSockets = false;
{ // grab copy of TCP sockets list because _tcpSockets[] might be changed in a handler
Mutex::Lock _l2(_tcpSockets_m);
if (!_tcpSockets.empty()) {
ts.reserve(_tcpSockets.size());
uint64_t now = Utils::now();
for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();) {
#ifdef __WINDOWS__
if ( ((now - ((TcpSocket *)s->second.ptr())->_lastActivity) < ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT) && (! ((((TcpSocket *)s->second.ptr())->_connecting)&&(FD_ISSET(s->second->_sock,&efds))) ) ) {
#else
if ((now - ((TcpSocket *)s->second.ptr())->_lastActivity) < ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT) {
#endif
ts.push_back(s->second);
++s;
} else {
_fdSetLock.lock();
FD_CLR(s->second->_sock,&_readfds);
FD_CLR(s->second->_sock,&_writefds);
_fdSetLock.unlock();
_tcpSockets.erase(s++);
closedSockets = true;
}
}
}
}
for(std::vector< SharedPtr<Socket> >::iterator s(ts.begin());s!=ts.end();++s) {
if (FD_ISSET((*s)->_sock,&wfds)) {
if (!(*s)->notifyAvailableForWrite(*s,this)) {
{
Mutex::Lock _l2(_tcpSockets_m);
_tcpSockets.erase(((TcpSocket *)s->ptr())->_remote);
}
_fdSetLock.lock();
FD_CLR((*s)->_sock,&_readfds);
FD_CLR((*s)->_sock,&_writefds);
_fdSetLock.unlock();
closedSockets = true;
continue;
}
}
if (FD_ISSET((*s)->_sock,&rfds)) {
if (!(*s)->notifyAvailableForRead(*s,this)) {
{
Mutex::Lock _l2(_tcpSockets_m);
_tcpSockets.erase(((TcpSocket *)s->ptr())->_remote);
}
_fdSetLock.lock();
FD_CLR((*s)->_sock,&_readfds);
FD_CLR((*s)->_sock,&_writefds);
_fdSetLock.unlock();
closedSockets = true;
continue;
}
}
}
if (closedSockets)
_updateNfds();
}
void SocketManager::whack()
{
_whackSendPipe_m.lock();
#ifdef __WINDOWS__
::send(_whackSendPipe,(const char *)this,1,0);
#else
::write(_whackSendPipe,(const void *)this,1); // data is arbitrary, just send a byte
#endif
_whackSendPipe_m.unlock();
}
void SocketManager::closeTcpSockets()
{
{
Mutex::Lock _l2(_tcpSockets_m);
_fdSetLock.lock();
for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
FD_CLR(s->second->_sock,&_readfds);
FD_CLR(s->second->_sock,&_writefds);
}
_fdSetLock.unlock();
_tcpSockets.clear();
}
_updateNfds();
}
void SocketManager::_closeSockets()
throw()
{
#ifdef __WINDOWS__
if (_whackSendPipe != INVALID_SOCKET)
::closesocket(_whackSendPipe);
if (_whackReceivePipe != INVALID_SOCKET)
::closesocket(_whackReceivePipe);
if (_tcpV4ListenSocket != INVALID_SOCKET)
::closesocket(_tcpV4ListenSocket);
if (_tcpV6ListenSocket != INVALID_SOCKET)
::closesocket(_tcpV6ListenSocket);
#else
if (_whackSendPipe > 0)
::close(_whackSendPipe);
if (_whackReceivePipe > 0)
::close(_whackReceivePipe);
if (_tcpV4ListenSocket > 0)
::close(_tcpV4ListenSocket);
if (_tcpV4ListenSocket > 0)
::close(_tcpV6ListenSocket);
#endif
}
void SocketManager::_updateNfds()
{
#ifdef __WINDOWS__
SOCKET nfds = _whackSendPipe;
#else
int nfds = _whackSendPipe;
#endif
if (_whackReceivePipe > nfds)
nfds = _whackReceivePipe;
if (_tcpV4ListenSocket > nfds)
nfds = _tcpV4ListenSocket;
if (_tcpV6ListenSocket > nfds)
nfds = _tcpV6ListenSocket;
if ((_udpV4Socket)&&(_udpV4Socket->_sock > nfds))
nfds = _udpV4Socket->_sock;
if ((_udpV6Socket)&&(_udpV6Socket->_sock > nfds))
nfds = _udpV6Socket->_sock;
Mutex::Lock _l(_tcpSockets_m);
for(std::map< InetAddress,SharedPtr<Socket> >::const_iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
if (s->second->_sock > nfds)
nfds = s->second->_sock;
}
_nfds = (int)nfds;
}
} // namespace ZeroTier
|