SocketManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <fcntl.h>
  31. #include <time.h>
  32. #include <sys/types.h>
  33. #include "SocketManager.hpp"
  34. #include "UdpSocket.hpp"
  35. #include "TcpSocket.hpp"
  36. #ifndef __WINDOWS__
  37. #include <errno.h>
  38. #include <unistd.h>
  39. #include <sys/socket.h>
  40. #include <arpa/inet.h>
  41. #include <signal.h>
  42. #endif
  43. // Allow us to use the same value on Windows and *nix
  44. #ifndef INVALID_SOCKET
  45. #define INVALID_SOCKET (-1)
  46. #endif
  47. #ifdef __WINDOWS__
  48. #define CLOSE_SOCKET(s) ::closesocket(s)
  49. #else
  50. #define CLOSE_SOCKET(s) ::close(s)
  51. #endif
  52. namespace ZeroTier {
  53. #ifdef __WINDOWS__
  54. // hack from StackOverflow, behaves a bit like pipe() on *nix systems
  55. static inline void __winpipe(SOCKET fds[2])
  56. {
  57. struct sockaddr_in inaddr;
  58. struct sockaddr addr;
  59. SOCKET lst=::socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  60. memset(&inaddr, 0, sizeof(inaddr));
  61. memset(&addr, 0, sizeof(addr));
  62. inaddr.sin_family = AF_INET;
  63. inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  64. inaddr.sin_port = 0;
  65. int yes=1;
  66. setsockopt(lst,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
  67. bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr));
  68. listen(lst,1);
  69. int len=sizeof(inaddr);
  70. getsockname(lst, &addr,&len);
  71. fds[0]=::socket(AF_INET, SOCK_STREAM,0);
  72. connect(fds[0],&addr,len);
  73. fds[1]=accept(lst,0,0);
  74. closesocket(lst);
  75. }
  76. #endif
  77. SocketManager::SocketManager(
  78. int localUdpPort,
  79. int localTcpPort,
  80. void (*packetHandler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),
  81. void *arg) :
  82. _whackSendPipe(INVALID_SOCKET),
  83. _whackReceivePipe(INVALID_SOCKET),
  84. _tcpV4ListenSocket(INVALID_SOCKET),
  85. _tcpV6ListenSocket(INVALID_SOCKET),
  86. _nfds(0),
  87. _packetHandler(packetHandler),
  88. _arg(arg)
  89. {
  90. FD_ZERO(&_readfds);
  91. FD_ZERO(&_writefds);
  92. #ifdef __WINDOWS__
  93. {
  94. SOCKET tmps[2] = { INVALID_SOCKET,INVALID_SOCKET };
  95. __winpipe(tmps);
  96. _whackSendPipe = tmps[0];
  97. _whackReceivePipe = tmps[1];
  98. }
  99. #else
  100. {
  101. int tmpfds[2];
  102. if (::pipe(tmpfds))
  103. throw std::runtime_error("pipe() failed");
  104. _whackSendPipe = tmpfds[1];
  105. _whackReceivePipe = tmpfds[0];
  106. }
  107. #endif
  108. fcntl(_whackReceivePipe,F_SETFL,O_NONBLOCK);
  109. FD_SET(_whackReceivePipe,&_readfds);
  110. if (localTcpPort > 0) {
  111. if (localTcpPort > 0xffff) {
  112. _closeSockets();
  113. throw std::runtime_error("invalid local TCP port number");
  114. }
  115. { // bind TCP IPv6
  116. _tcpV6ListenSocket = ::socket(AF_INET6,SOCK_STREAM,0);
  117. #ifdef __WINDOWS__
  118. if (_tcpV6ListenSocket == INVALID_SOCKET) {
  119. _closeSockets();
  120. throw std::runtime_error("unable to create IPv6 SOCK_STREAM socket");
  121. }
  122. #else
  123. if (_tcpV6ListenSocket <= 0) {
  124. _closeSockets();
  125. throw std::runtime_error("unable to create IPv6 SOCK_STREAM socket");
  126. }
  127. #endif
  128. #ifdef __WINDOWS__
  129. {
  130. BOOL f;
  131. f = TRUE; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  132. f = TRUE; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  133. }
  134. #else
  135. {
  136. int f;
  137. f = 1; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  138. f = 1; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  139. }
  140. #endif
  141. fcntl(_tcpV6ListenSocket,F_SETFL,O_NONBLOCK);
  142. struct sockaddr_in6 sin6;
  143. memset(&sin6,0,sizeof(sin6));
  144. sin6.sin6_family = AF_INET6;
  145. sin6.sin6_port = htons(localTcpPort);
  146. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  147. if (::bind(_tcpV6ListenSocket,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  148. _closeSockets();
  149. throw std::runtime_error("unable to bind to local TCP port");
  150. }
  151. if (::listen(_tcpV6ListenSocket,16)) {
  152. _closeSockets();
  153. throw std::runtime_error("listen() failed");
  154. }
  155. FD_SET(_tcpV6ListenSocket,&_readfds);
  156. }
  157. { // bind TCP IPv4
  158. _tcpV4ListenSocket = ::socket(AF_INET,SOCK_STREAM,0);
  159. #ifdef __WINDOWS__
  160. if (_tcpV4ListenSocket == INVALID_SOCKET) {
  161. _closeSockets();
  162. throw std::runtime_error("unable to create IPv4 SOCK_STREAM socket");
  163. }
  164. #else
  165. if (_tcpV4ListenSocket <= 0) {
  166. _closeSockets();
  167. throw std::runtime_error("unable to create IPv4 SOCK_STREAM socket");
  168. }
  169. #endif
  170. #ifdef __WINDOWS__
  171. {
  172. BOOL f = TRUE; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  173. }
  174. #else
  175. {
  176. int f = 1; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  177. }
  178. #endif
  179. fcntl(_tcpV4ListenSocket,F_SETFL,O_NONBLOCK);
  180. struct sockaddr_in sin4;
  181. memset(&sin4,0,sizeof(sin4));
  182. sin4.sin_family = AF_INET;
  183. sin4.sin_port = htons(localTcpPort);
  184. sin4.sin_addr.s_addr = INADDR_ANY;
  185. if (::bind(_tcpV4ListenSocket,(const struct sockaddr *)&sin4,sizeof(sin4))) {
  186. _closeSockets();
  187. throw std::runtime_error("unable to bind to local TCP port");
  188. }
  189. if (::listen(_tcpV4ListenSocket,16)) {
  190. _closeSockets();
  191. throw std::runtime_error("listen() failed");
  192. }
  193. FD_SET(_tcpV4ListenSocket,&_readfds);
  194. }
  195. }
  196. if (localUdpPort > 0) {
  197. if (localUdpPort > 0xffff) {
  198. _closeSockets();
  199. throw std::runtime_error("invalid local UDP port number");
  200. }
  201. { // bind UDP IPv6
  202. #ifdef __WINDOWS__
  203. SOCKET s = ::socket(AF_INET6,SOCK_DGRAM,0);
  204. if (s == INVALID_SOCKET) {
  205. _closeSockets();
  206. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  207. }
  208. #else
  209. int s = ::socket(AF_INET6,SOCK_DGRAM,0);
  210. if (s <= 0) {
  211. _closeSockets();
  212. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  213. }
  214. #endif
  215. {
  216. #ifdef __WINDOWS__
  217. BOOL f;
  218. f = TRUE; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  219. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  220. f = FALSE; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&f,sizeof(f));
  221. #else
  222. int f;
  223. f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  224. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  225. #ifdef IP_DONTFRAG
  226. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  227. #endif
  228. #ifdef IP_MTU_DISCOVER
  229. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  230. #endif
  231. #ifdef IPV6_MTU_DISCOVER
  232. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
  233. #endif
  234. #endif
  235. }
  236. struct sockaddr_in6 sin6;
  237. memset(&sin6,0,sizeof(sin6));
  238. sin6.sin6_family = AF_INET6;
  239. sin6.sin6_port = htons(localUdpPort);
  240. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  241. if (::bind(s,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  242. CLOSE_SOCKET(s);
  243. _closeSockets();
  244. throw std::runtime_error("unable to bind to port");
  245. }
  246. _udpV6Socket = SharedPtr<Socket>(new UdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V6,s));
  247. fcntl(s,F_SETFL,O_NONBLOCK);
  248. FD_SET(s,&_readfds);
  249. }
  250. { // bind UDP IPv4
  251. #ifdef __WINDOWS__
  252. SOCKET s = ::socket(AF_INET,SOCK_DGRAM,0);
  253. if (s == INVALID_SOCKET) {
  254. _closeSockets();
  255. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  256. }
  257. #else
  258. int s = ::socket(AF_INET,SOCK_DGRAM,0);
  259. if (s <= 0) {
  260. _closeSockets();
  261. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  262. }
  263. #endif
  264. {
  265. #ifdef __WINDOWS__
  266. BOOL f;
  267. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  268. f = FALSE; setsockopt(s,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&f,sizeof(f));
  269. #else
  270. int f;
  271. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  272. #ifdef IP_DONTFRAG
  273. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  274. #endif
  275. #ifdef IP_MTU_DISCOVER
  276. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  277. #endif
  278. #endif
  279. }
  280. struct sockaddr_in sin4;
  281. memset(&sin4,0,sizeof(sin4));
  282. sin4.sin_family = AF_INET;
  283. sin4.sin_port = htons(localUdpPort);
  284. sin4.sin_addr.s_addr = INADDR_ANY;
  285. if (::bind(s,(const struct sockaddr *)&sin4,sizeof(sin4))) {
  286. CLOSE_SOCKET(s);
  287. _closeSockets();
  288. throw std::runtime_error("unable to bind to port");
  289. }
  290. _udpV4Socket = SharedPtr<Socket>(new UdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V4,s));
  291. fcntl(s,F_SETFL,O_NONBLOCK);
  292. FD_SET(s,&_readfds);
  293. }
  294. }
  295. _updateNfds();
  296. }
  297. SocketManager::~SocketManager()
  298. {
  299. Mutex::Lock _l(_pollLock);
  300. _closeSockets();
  301. }
  302. bool SocketManager::send(const InetAddress &to,bool tcp,const void *msg,unsigned int msglen)
  303. {
  304. if (tcp) {
  305. SharedPtr<Socket> ts;
  306. {
  307. Mutex::Lock _l(_tcpSockets_m);
  308. std::map< InetAddress,SharedPtr<Socket> >::iterator opents(_tcpSockets.find(to));
  309. if (opents != _tcpSockets.end())
  310. ts = opents->second;
  311. }
  312. if (ts)
  313. return ts->send(to,msg,msglen);
  314. #ifdef __WINDOWS__
  315. SOCKET s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
  316. if (s == INVALID_SOCKET)
  317. return false;
  318. if (s >= FD_SETSIZE) {
  319. ::closesocket(s);
  320. return false;
  321. }
  322. #else
  323. int s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
  324. if (s <= 0)
  325. return false;
  326. if (s >= FD_SETSIZE) {
  327. ::close(s);
  328. return false;
  329. }
  330. #endif
  331. fcntl(s,F_SETFL,O_NONBLOCK);
  332. bool connecting = false;
  333. if (connect(s,to.saddr(),to.saddrLen())) {
  334. if (errno != EINPROGRESS) {
  335. CLOSE_SOCKET(s);
  336. return false;
  337. } else connecting = true;
  338. }
  339. ts = SharedPtr<Socket>(new TcpSocket(this,s,connecting,to));
  340. if (!ts->send(to,msg,msglen))
  341. return false;
  342. _fdSetLock.lock();
  343. FD_SET(s,&_readfds);
  344. if (connecting)
  345. FD_SET(s,&_writefds);
  346. _fdSetLock.unlock();
  347. {
  348. Mutex::Lock _l(_tcpSockets_m);
  349. _tcpSockets[to] = ts;
  350. }
  351. return true;
  352. } else if (to.isV4()) {
  353. if (_udpV4Socket)
  354. return _udpV4Socket->send(to,msg,msglen);
  355. } else if (to.isV6()) {
  356. if (_udpV6Socket)
  357. return _udpV6Socket->send(to,msg,msglen);
  358. }
  359. return false;
  360. }
  361. bool SocketManager::sendFirewallOpener(const InetAddress &to,int hopLimit)
  362. {
  363. if (to.isV4()) {
  364. if (_udpV4Socket)
  365. return ((UdpSocket *)_udpV4Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
  366. } else if (to.isV6()) {
  367. if (_udpV6Socket)
  368. return ((UdpSocket *)_udpV6Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
  369. }
  370. return false;
  371. }
  372. void SocketManager::poll(unsigned long timeout)
  373. {
  374. fd_set rfds,wfds,efds;
  375. struct timeval tv;
  376. std::vector< SharedPtr<Socket> > ts;
  377. #ifdef __WINDOWS__
  378. SOCKET sockfd;
  379. #else
  380. int sockfd;
  381. #endif
  382. Mutex::Lock _l(_pollLock);
  383. _fdSetLock.lock();
  384. memcpy(&rfds,&_readfds,sizeof(rfds));
  385. memcpy(&wfds,&_writefds,sizeof(wfds));
  386. _fdSetLock.unlock();
  387. FD_ZERO(&efds);
  388. tv.tv_sec = (long)(timeout / 1000);
  389. tv.tv_usec = (long)((timeout % 1000) * 1000);
  390. select(_nfds + 1,&rfds,&wfds,&efds,(timeout > 0) ? &tv : (struct timeval *)0);
  391. if (FD_ISSET(_whackReceivePipe,&rfds)) {
  392. char tmp;
  393. #ifdef __WINDOWS__
  394. ::recv(_whackReceivePipe,&tmp,1,0);
  395. #else
  396. ::read(_whackReceivePipe,&tmp,1);
  397. #endif
  398. }
  399. if ((_tcpV4ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV4ListenSocket,&rfds))) {
  400. struct sockaddr_in from;
  401. socklen_t fromlen = sizeof(from);
  402. sockfd = accept(_tcpV4ListenSocket,(struct sockaddr *)&from,&fromlen);
  403. #ifdef __WINDOWS__
  404. if (sockfd != INVALID_SOCKET) {
  405. #else
  406. if (sockfd > 0) {
  407. #endif
  408. if (sockfd < FD_SETSIZE) {
  409. InetAddress fromia((const struct sockaddr *)&from);
  410. Mutex::Lock _l2(_tcpSockets_m);
  411. try {
  412. _tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(this,sockfd,false,fromia));
  413. fcntl(sockfd,F_SETFL,O_NONBLOCK);
  414. _fdSetLock.lock();
  415. FD_SET(sockfd,&_readfds);
  416. _fdSetLock.unlock();
  417. if (sockfd > _nfds)
  418. _nfds = sockfd;
  419. } catch ( ... ) {
  420. CLOSE_SOCKET(sockfd);
  421. }
  422. } else {
  423. CLOSE_SOCKET(sockfd);
  424. }
  425. }
  426. }
  427. if ((_tcpV6ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV6ListenSocket,&rfds))) {
  428. struct sockaddr_in6 from;
  429. socklen_t fromlen = sizeof(from);
  430. sockfd = accept(_tcpV6ListenSocket,(struct sockaddr *)&from,&fromlen);
  431. #ifdef __WINDOWS__
  432. if (sockfd != INVALID_SOCKET) {
  433. #else
  434. if (sockfd > 0) {
  435. #endif
  436. if (sockfd < FD_SETSIZE) {
  437. InetAddress fromia((const struct sockaddr *)&from);
  438. Mutex::Lock _l2(_tcpSockets_m);
  439. try {
  440. _tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(this,sockfd,false,fromia));
  441. fcntl(sockfd,F_SETFL,O_NONBLOCK);
  442. _fdSetLock.lock();
  443. FD_SET(sockfd,&_readfds);
  444. _fdSetLock.unlock();
  445. if (sockfd > _nfds)
  446. _nfds = sockfd;
  447. } catch ( ... ) {
  448. CLOSE_SOCKET(sockfd);
  449. }
  450. } else {
  451. CLOSE_SOCKET(sockfd);
  452. }
  453. }
  454. }
  455. if ((_udpV4Socket)&&(FD_ISSET(_udpV4Socket->_sock,&rfds))) {
  456. _udpV4Socket->notifyAvailableForRead(_udpV4Socket,this);
  457. }
  458. if ((_udpV6Socket)&&(FD_ISSET(_udpV6Socket->_sock,&rfds))) {
  459. _udpV6Socket->notifyAvailableForRead(_udpV6Socket,this);
  460. }
  461. bool closedSockets = false;
  462. { // grab copy of TCP sockets list because _tcpSockets[] might be changed in a handler
  463. Mutex::Lock _l2(_tcpSockets_m);
  464. if (_tcpSockets.size()) {
  465. ts.reserve(_tcpSockets.size());
  466. uint64_t now = Utils::now();
  467. for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();) {
  468. if ((now - ((TcpSocket *)s->second.ptr())->_lastActivity) < ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT) {
  469. ts.push_back(s->second);
  470. ++s;
  471. } else {
  472. _fdSetLock.lock();
  473. FD_CLR(s->second->_sock,&_readfds);
  474. FD_CLR(s->second->_sock,&_writefds);
  475. _fdSetLock.unlock();
  476. _tcpSockets.erase(s++);
  477. closedSockets = true;
  478. }
  479. }
  480. }
  481. }
  482. for(std::vector< SharedPtr<Socket> >::iterator s(ts.begin());s!=ts.end();++s) {
  483. if (FD_ISSET((*s)->_sock,&wfds)) {
  484. if (!(*s)->notifyAvailableForWrite(*s,this)) {
  485. {
  486. Mutex::Lock _l2(_tcpSockets_m);
  487. _tcpSockets.erase(((TcpSocket *)s->ptr())->_remote);
  488. }
  489. _fdSetLock.lock();
  490. FD_CLR((*s)->_sock,&_readfds);
  491. FD_CLR((*s)->_sock,&_writefds);
  492. _fdSetLock.unlock();
  493. closedSockets = true;
  494. continue;
  495. }
  496. }
  497. if (FD_ISSET((*s)->_sock,&rfds)) {
  498. if (!(*s)->notifyAvailableForRead(*s,this)) {
  499. {
  500. Mutex::Lock _l2(_tcpSockets_m);
  501. _tcpSockets.erase(((TcpSocket *)s->ptr())->_remote);
  502. }
  503. _fdSetLock.lock();
  504. FD_CLR((*s)->_sock,&_readfds);
  505. FD_CLR((*s)->_sock,&_writefds);
  506. _fdSetLock.unlock();
  507. closedSockets = true;
  508. continue;
  509. }
  510. }
  511. }
  512. if (closedSockets)
  513. _updateNfds();
  514. }
  515. void SocketManager::whack()
  516. {
  517. _whackSendPipe_m.lock();
  518. #ifdef __WINDOWS__
  519. ::send(_whackSendPipe,(const void *)this,1,0);
  520. #else
  521. ::write(_whackSendPipe,(const void *)this,1); // data is arbitrary, just send a byte
  522. #endif
  523. _whackSendPipe_m.unlock();
  524. }
  525. } // namespace ZeroTier