SocketManager.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. u_long iMode=1;
  99. ioctlsocket(tmps[1],FIONBIO,&iMode);
  100. }
  101. #else
  102. {
  103. int tmpfds[2];
  104. if (::pipe(tmpfds))
  105. throw std::runtime_error("pipe() failed");
  106. _whackSendPipe = tmpfds[1];
  107. _whackReceivePipe = tmpfds[0];
  108. fcntl(_whackReceivePipe,F_SETFL,O_NONBLOCK);
  109. }
  110. #endif
  111. FD_SET(_whackReceivePipe,&_readfds);
  112. if (localTcpPort > 0) {
  113. if (localTcpPort > 0xffff) {
  114. _closeSockets();
  115. throw std::runtime_error("invalid local TCP port number");
  116. }
  117. { // bind TCP IPv6
  118. _tcpV6ListenSocket = ::socket(AF_INET6,SOCK_STREAM,0);
  119. #ifdef __WINDOWS__
  120. if (_tcpV6ListenSocket == INVALID_SOCKET) {
  121. _closeSockets();
  122. throw std::runtime_error("unable to create IPv6 SOCK_STREAM socket");
  123. }
  124. #else
  125. if (_tcpV6ListenSocket <= 0) {
  126. _closeSockets();
  127. throw std::runtime_error("unable to create IPv6 SOCK_STREAM socket");
  128. }
  129. #endif
  130. #ifdef __WINDOWS__
  131. {
  132. BOOL f;
  133. f = TRUE; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  134. f = TRUE; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  135. u_long iMode=1;
  136. ioctlsocket(_tcpV6ListenSocket,FIONBIO,&iMode);
  137. }
  138. #else
  139. {
  140. int f;
  141. f = 1; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  142. f = 1; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  143. fcntl(_tcpV6ListenSocket,F_SETFL,O_NONBLOCK);
  144. }
  145. #endif
  146. struct sockaddr_in6 sin6;
  147. memset(&sin6,0,sizeof(sin6));
  148. sin6.sin6_family = AF_INET6;
  149. sin6.sin6_port = htons(localTcpPort);
  150. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  151. if (::bind(_tcpV6ListenSocket,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  152. _closeSockets();
  153. throw std::runtime_error("unable to bind to local TCP port");
  154. }
  155. if (::listen(_tcpV6ListenSocket,16)) {
  156. _closeSockets();
  157. throw std::runtime_error("listen() failed");
  158. }
  159. FD_SET(_tcpV6ListenSocket,&_readfds);
  160. }
  161. { // bind TCP IPv4
  162. _tcpV4ListenSocket = ::socket(AF_INET,SOCK_STREAM,0);
  163. #ifdef __WINDOWS__
  164. if (_tcpV4ListenSocket == INVALID_SOCKET) {
  165. _closeSockets();
  166. throw std::runtime_error("unable to create IPv4 SOCK_STREAM socket");
  167. }
  168. #else
  169. if (_tcpV4ListenSocket <= 0) {
  170. _closeSockets();
  171. throw std::runtime_error("unable to create IPv4 SOCK_STREAM socket");
  172. }
  173. #endif
  174. #ifdef __WINDOWS__
  175. {
  176. BOOL f = TRUE; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  177. u_long iMode=1;
  178. ioctlsocket(_tcpV4ListenSocket,FIONBIO,&iMode);
  179. }
  180. #else
  181. {
  182. int f = 1; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  183. fcntl(_tcpV4ListenSocket,F_SETFL,O_NONBLOCK);
  184. }
  185. #endif
  186. struct sockaddr_in sin4;
  187. memset(&sin4,0,sizeof(sin4));
  188. sin4.sin_family = AF_INET;
  189. sin4.sin_port = htons(localTcpPort);
  190. sin4.sin_addr.s_addr = INADDR_ANY;
  191. if (::bind(_tcpV4ListenSocket,(const struct sockaddr *)&sin4,sizeof(sin4))) {
  192. _closeSockets();
  193. throw std::runtime_error("unable to bind to local TCP port");
  194. }
  195. if (::listen(_tcpV4ListenSocket,16)) {
  196. _closeSockets();
  197. throw std::runtime_error("listen() failed");
  198. }
  199. FD_SET(_tcpV4ListenSocket,&_readfds);
  200. }
  201. }
  202. if (localUdpPort > 0) {
  203. if (localUdpPort > 0xffff) {
  204. _closeSockets();
  205. throw std::runtime_error("invalid local UDP port number");
  206. }
  207. { // bind UDP IPv6
  208. #ifdef __WINDOWS__
  209. SOCKET s = ::socket(AF_INET6,SOCK_DGRAM,0);
  210. if (s == INVALID_SOCKET) {
  211. _closeSockets();
  212. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  213. }
  214. #else
  215. int s = ::socket(AF_INET6,SOCK_DGRAM,0);
  216. if (s <= 0) {
  217. _closeSockets();
  218. throw std::runtime_error("unable to create IPv6 SOCK_DGRAM socket");
  219. }
  220. #endif
  221. {
  222. #ifdef __WINDOWS__
  223. BOOL f;
  224. f = TRUE; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  225. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  226. f = FALSE; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&f,sizeof(f));
  227. #else
  228. int f;
  229. f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  230. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  231. #ifdef IP_DONTFRAG
  232. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  233. #endif
  234. #ifdef IP_MTU_DISCOVER
  235. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  236. #endif
  237. #ifdef IPV6_MTU_DISCOVER
  238. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
  239. #endif
  240. #endif
  241. }
  242. struct sockaddr_in6 sin6;
  243. memset(&sin6,0,sizeof(sin6));
  244. sin6.sin6_family = AF_INET6;
  245. sin6.sin6_port = htons(localUdpPort);
  246. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  247. if (::bind(s,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  248. CLOSE_SOCKET(s);
  249. _closeSockets();
  250. throw std::runtime_error("unable to bind to port");
  251. }
  252. _udpV6Socket = SharedPtr<Socket>(new UdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V6,s));
  253. #ifdef __WINDOWS__
  254. u_long iMode=1;
  255. ioctlsocket(s,FIONBIO,&iMode);
  256. #else
  257. fcntl(s,F_SETFL,O_NONBLOCK);
  258. #endif
  259. FD_SET(s,&_readfds);
  260. }
  261. { // bind UDP IPv4
  262. #ifdef __WINDOWS__
  263. SOCKET s = ::socket(AF_INET,SOCK_DGRAM,0);
  264. if (s == INVALID_SOCKET) {
  265. _closeSockets();
  266. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  267. }
  268. #else
  269. int s = ::socket(AF_INET,SOCK_DGRAM,0);
  270. if (s <= 0) {
  271. _closeSockets();
  272. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  273. }
  274. #endif
  275. {
  276. #ifdef __WINDOWS__
  277. BOOL f;
  278. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  279. f = FALSE; setsockopt(s,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&f,sizeof(f));
  280. #else
  281. int f;
  282. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  283. #ifdef IP_DONTFRAG
  284. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  285. #endif
  286. #ifdef IP_MTU_DISCOVER
  287. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  288. #endif
  289. #endif
  290. }
  291. struct sockaddr_in sin4;
  292. memset(&sin4,0,sizeof(sin4));
  293. sin4.sin_family = AF_INET;
  294. sin4.sin_port = htons(localUdpPort);
  295. sin4.sin_addr.s_addr = INADDR_ANY;
  296. if (::bind(s,(const struct sockaddr *)&sin4,sizeof(sin4))) {
  297. CLOSE_SOCKET(s);
  298. _closeSockets();
  299. throw std::runtime_error("unable to bind to port");
  300. }
  301. _udpV4Socket = SharedPtr<Socket>(new UdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V4,s));
  302. #ifdef __WINDOWS__
  303. u_long iMode=1;
  304. ioctlsocket(s,FIONBIO,&iMode);
  305. #else
  306. fcntl(s,F_SETFL,O_NONBLOCK);
  307. #endif
  308. FD_SET(s,&_readfds);
  309. }
  310. }
  311. _updateNfds();
  312. }
  313. SocketManager::~SocketManager()
  314. {
  315. Mutex::Lock _l(_pollLock);
  316. _closeSockets();
  317. }
  318. bool SocketManager::send(const InetAddress &to,bool tcp,const void *msg,unsigned int msglen)
  319. {
  320. if (tcp) {
  321. SharedPtr<Socket> ts;
  322. {
  323. Mutex::Lock _l(_tcpSockets_m);
  324. std::map< InetAddress,SharedPtr<Socket> >::iterator opents(_tcpSockets.find(to));
  325. if (opents != _tcpSockets.end())
  326. ts = opents->second;
  327. }
  328. if (ts)
  329. return ts->send(to,msg,msglen);
  330. #ifdef __WINDOWS__
  331. SOCKET s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
  332. if (s == INVALID_SOCKET)
  333. return false;
  334. if (s >= FD_SETSIZE) {
  335. ::closesocket(s);
  336. return false;
  337. }
  338. {
  339. u_long iMode=1;
  340. ioctlsocket(s,FIONBIO,&iMode);
  341. }
  342. #else
  343. int s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
  344. if (s <= 0)
  345. return false;
  346. if (s >= FD_SETSIZE) {
  347. ::close(s);
  348. return false;
  349. }
  350. fcntl(s,F_SETFL,O_NONBLOCK);
  351. #endif
  352. bool connecting = false;
  353. if (connect(s,to.saddr(),to.saddrLen())) {
  354. if (errno != EINPROGRESS) {
  355. CLOSE_SOCKET(s);
  356. return false;
  357. } else connecting = true;
  358. }
  359. ts = SharedPtr<Socket>(new TcpSocket(this,s,connecting,to));
  360. if (!ts->send(to,msg,msglen))
  361. return false;
  362. {
  363. Mutex::Lock _l(_tcpSockets_m);
  364. _tcpSockets[to] = ts;
  365. }
  366. _fdSetLock.lock();
  367. FD_SET(s,&_readfds);
  368. if (connecting)
  369. FD_SET(s,&_writefds);
  370. _fdSetLock.unlock();
  371. return true;
  372. } else if (to.isV4()) {
  373. if (_udpV4Socket)
  374. return _udpV4Socket->send(to,msg,msglen);
  375. } else if (to.isV6()) {
  376. if (_udpV6Socket)
  377. return _udpV6Socket->send(to,msg,msglen);
  378. }
  379. return false;
  380. }
  381. bool SocketManager::sendFirewallOpener(const InetAddress &to,int hopLimit)
  382. {
  383. if (to.isV4()) {
  384. if (_udpV4Socket)
  385. return ((UdpSocket *)_udpV4Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
  386. } else if (to.isV6()) {
  387. if (_udpV6Socket)
  388. return ((UdpSocket *)_udpV6Socket.ptr())->sendWithHopLimit(to,"",1,hopLimit);
  389. }
  390. return false;
  391. }
  392. void SocketManager::poll(unsigned long timeout)
  393. {
  394. fd_set rfds,wfds,efds;
  395. struct timeval tv;
  396. std::vector< SharedPtr<Socket> > ts;
  397. #ifdef __WINDOWS__
  398. SOCKET sockfd;
  399. #else
  400. int sockfd;
  401. #endif
  402. Mutex::Lock _l(_pollLock);
  403. _fdSetLock.lock();
  404. memcpy(&rfds,&_readfds,sizeof(rfds));
  405. memcpy(&wfds,&_writefds,sizeof(wfds));
  406. _fdSetLock.unlock();
  407. FD_ZERO(&efds);
  408. tv.tv_sec = (long)(timeout / 1000);
  409. tv.tv_usec = (long)((timeout % 1000) * 1000);
  410. select(_nfds + 1,&rfds,&wfds,&efds,(timeout > 0) ? &tv : (struct timeval *)0);
  411. if (FD_ISSET(_whackReceivePipe,&rfds)) {
  412. char tmp;
  413. #ifdef __WINDOWS__
  414. ::recv(_whackReceivePipe,&tmp,1,0);
  415. #else
  416. ::read(_whackReceivePipe,&tmp,1);
  417. #endif
  418. }
  419. if ((_tcpV4ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV4ListenSocket,&rfds))) {
  420. struct sockaddr_in from;
  421. socklen_t fromlen = sizeof(from);
  422. sockfd = accept(_tcpV4ListenSocket,(struct sockaddr *)&from,&fromlen);
  423. #ifdef __WINDOWS__
  424. if (sockfd != INVALID_SOCKET) {
  425. #else
  426. if (sockfd > 0) {
  427. #endif
  428. if (sockfd < FD_SETSIZE) {
  429. InetAddress fromia((const struct sockaddr *)&from);
  430. Mutex::Lock _l2(_tcpSockets_m);
  431. try {
  432. _tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(this,sockfd,false,fromia));
  433. #ifdef __WINDOWS__
  434. u_long iMode=1;
  435. ioctlsocket(sockfd,FIONBIO,&iMode);
  436. #else
  437. fcntl(sockfd,F_SETFL,O_NONBLOCK);
  438. #endif
  439. _fdSetLock.lock();
  440. FD_SET(sockfd,&_readfds);
  441. _fdSetLock.unlock();
  442. if ((int)sockfd > (int)_nfds)
  443. _nfds = (int)sockfd;
  444. } catch ( ... ) {
  445. CLOSE_SOCKET(sockfd);
  446. }
  447. } else {
  448. CLOSE_SOCKET(sockfd);
  449. }
  450. }
  451. }
  452. if ((_tcpV6ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV6ListenSocket,&rfds))) {
  453. struct sockaddr_in6 from;
  454. socklen_t fromlen = sizeof(from);
  455. sockfd = accept(_tcpV6ListenSocket,(struct sockaddr *)&from,&fromlen);
  456. #ifdef __WINDOWS__
  457. if (sockfd != INVALID_SOCKET) {
  458. #else
  459. if (sockfd > 0) {
  460. #endif
  461. if (sockfd < FD_SETSIZE) {
  462. InetAddress fromia((const struct sockaddr *)&from);
  463. Mutex::Lock _l2(_tcpSockets_m);
  464. try {
  465. _tcpSockets[fromia] = SharedPtr<Socket>(new TcpSocket(this,sockfd,false,fromia));
  466. #ifdef __WINDOWS__
  467. u_long iMode=1;
  468. ioctlsocket(sockfd,FIONBIO,&iMode);
  469. #else
  470. fcntl(sockfd,F_SETFL,O_NONBLOCK);
  471. #endif
  472. _fdSetLock.lock();
  473. FD_SET(sockfd,&_readfds);
  474. _fdSetLock.unlock();
  475. if ((int)sockfd > (int)_nfds)
  476. _nfds = (int)sockfd;
  477. } catch ( ... ) {
  478. CLOSE_SOCKET(sockfd);
  479. }
  480. } else {
  481. CLOSE_SOCKET(sockfd);
  482. }
  483. }
  484. }
  485. if ((_udpV4Socket)&&(FD_ISSET(_udpV4Socket->_sock,&rfds))) {
  486. _udpV4Socket->notifyAvailableForRead(_udpV4Socket,this);
  487. }
  488. if ((_udpV6Socket)&&(FD_ISSET(_udpV6Socket->_sock,&rfds))) {
  489. _udpV6Socket->notifyAvailableForRead(_udpV6Socket,this);
  490. }
  491. bool closedSockets = false;
  492. { // grab copy of TCP sockets list because _tcpSockets[] might be changed in a handler
  493. Mutex::Lock _l2(_tcpSockets_m);
  494. if (_tcpSockets.size()) {
  495. ts.reserve(_tcpSockets.size());
  496. uint64_t now = Utils::now();
  497. for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();) {
  498. if ((now - ((TcpSocket *)s->second.ptr())->_lastActivity) < ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT) {
  499. ts.push_back(s->second);
  500. ++s;
  501. } else {
  502. _fdSetLock.lock();
  503. FD_CLR(s->second->_sock,&_readfds);
  504. FD_CLR(s->second->_sock,&_writefds);
  505. _fdSetLock.unlock();
  506. _tcpSockets.erase(s++);
  507. closedSockets = true;
  508. }
  509. }
  510. }
  511. }
  512. for(std::vector< SharedPtr<Socket> >::iterator s(ts.begin());s!=ts.end();++s) {
  513. if (FD_ISSET((*s)->_sock,&wfds)) {
  514. if (!(*s)->notifyAvailableForWrite(*s,this)) {
  515. {
  516. Mutex::Lock _l2(_tcpSockets_m);
  517. _tcpSockets.erase(((TcpSocket *)s->ptr())->_remote);
  518. }
  519. _fdSetLock.lock();
  520. FD_CLR((*s)->_sock,&_readfds);
  521. FD_CLR((*s)->_sock,&_writefds);
  522. _fdSetLock.unlock();
  523. closedSockets = true;
  524. continue;
  525. }
  526. }
  527. if (FD_ISSET((*s)->_sock,&rfds)) {
  528. if (!(*s)->notifyAvailableForRead(*s,this)) {
  529. {
  530. Mutex::Lock _l2(_tcpSockets_m);
  531. _tcpSockets.erase(((TcpSocket *)s->ptr())->_remote);
  532. }
  533. _fdSetLock.lock();
  534. FD_CLR((*s)->_sock,&_readfds);
  535. FD_CLR((*s)->_sock,&_writefds);
  536. _fdSetLock.unlock();
  537. closedSockets = true;
  538. continue;
  539. }
  540. }
  541. }
  542. if (closedSockets)
  543. _updateNfds();
  544. }
  545. void SocketManager::whack()
  546. {
  547. _whackSendPipe_m.lock();
  548. #ifdef __WINDOWS__
  549. ::send(_whackSendPipe,(const char *)this,1,0);
  550. #else
  551. ::write(_whackSendPipe,(const void *)this,1); // data is arbitrary, just send a byte
  552. #endif
  553. _whackSendPipe_m.unlock();
  554. }
  555. void SocketManager::closeTcpSockets()
  556. {
  557. {
  558. Mutex::Lock _l2(_tcpSockets_m);
  559. _fdSetLock.lock();
  560. for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
  561. FD_CLR(s->second->_sock,&_readfds);
  562. FD_CLR(s->second->_sock,&_writefds);
  563. }
  564. _fdSetLock.unlock();
  565. _tcpSockets.clear();
  566. }
  567. _updateNfds();
  568. }
  569. void SocketManager::_closeSockets()
  570. throw()
  571. {
  572. #ifdef __WINDOWS__
  573. if (_whackSendPipe != INVALID_SOCKET)
  574. ::closesocket(_whackSendPipe);
  575. if (_whackReceivePipe != INVALID_SOCKET)
  576. ::closesocket(_whackReceivePipe);
  577. if (_tcpV4ListenSocket != INVALID_SOCKET)
  578. ::closesocket(_tcpV4ListenSocket);
  579. if (_tcpV6ListenSocket != INVALID_SOCKET)
  580. ::closesocket(_tcpV6ListenSocket);
  581. #else
  582. if (_whackSendPipe > 0)
  583. ::close(_whackSendPipe);
  584. if (_whackReceivePipe > 0)
  585. ::close(_whackReceivePipe);
  586. if (_tcpV4ListenSocket > 0)
  587. ::close(_tcpV4ListenSocket);
  588. if (_tcpV4ListenSocket > 0)
  589. ::close(_tcpV6ListenSocket);
  590. #endif
  591. }
  592. void SocketManager::_updateNfds()
  593. {
  594. #ifdef __WINDOWS__
  595. SOCKET nfds = _whackSendPipe;
  596. #else
  597. int nfds = _whackSendPipe;
  598. #endif
  599. if (_whackReceivePipe > nfds)
  600. nfds = _whackReceivePipe;
  601. if (_tcpV4ListenSocket > nfds)
  602. nfds = _tcpV4ListenSocket;
  603. if (_tcpV6ListenSocket > nfds)
  604. nfds = _tcpV6ListenSocket;
  605. if ((_udpV4Socket)&&(_udpV4Socket->_sock > nfds))
  606. nfds = _udpV4Socket->_sock;
  607. if ((_udpV6Socket)&&(_udpV6Socket->_sock > nfds))
  608. nfds = _udpV6Socket->_sock;
  609. Mutex::Lock _l(_tcpSockets_m);
  610. for(std::map< InetAddress,SharedPtr<Socket> >::const_iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
  611. if (s->second->_sock > nfds)
  612. nfds = s->second->_sock;
  613. }
  614. _nfds = (int)nfds;
  615. }
  616. } // namespace ZeroTier