NativeSocketManager.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. /* Native SocketManager for Windows and Unix */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <fcntl.h>
  32. #include <time.h>
  33. #include <sys/types.h>
  34. #include <algorithm>
  35. #include "../node/Constants.hpp"
  36. #include "NativeSocketManager.hpp"
  37. #ifndef __WINDOWS__
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <sys/socket.h>
  41. #include <arpa/inet.h>
  42. #include <signal.h>
  43. #include <netinet/in.h>
  44. #include <netinet/tcp.h>
  45. #endif // !__WINDOWS__
  46. // Uncomment to turn off TCP Nagle
  47. //#define ZT_TCP_NODELAY
  48. // Allow us to use the same value on Windows and *nix
  49. #ifndef INVALID_SOCKET
  50. #define INVALID_SOCKET (-1)
  51. #endif
  52. #ifdef __WINDOWS__
  53. #define CLOSE_SOCKET(s) ::closesocket(s)
  54. #else
  55. #define CLOSE_SOCKET(s) ::close(s)
  56. #endif
  57. namespace ZeroTier {
  58. //////////////////////////////////////////////////////////////////////////////
  59. // Socket implementations
  60. //////////////////////////////////////////////////////////////////////////////
  61. class NativeSocket : public Socket
  62. {
  63. public:
  64. #ifdef __WINDOWS__
  65. NativeSocket(const Type &t,SOCKET s) : Socket(t),_sock(s) {}
  66. SOCKET _sock;
  67. #else
  68. NativeSocket(const Type &t,int s) : Socket(t),_sock(s) {}
  69. int _sock;
  70. #endif
  71. };
  72. /**
  73. * Native UDP socket
  74. */
  75. class NativeUdpSocket : public NativeSocket
  76. {
  77. public:
  78. #ifdef __WINDOWS__
  79. NativeUdpSocket(Type t,SOCKET s) : NativeSocket(t,s) {}
  80. #else
  81. NativeUdpSocket(Type t,int s) : NativeSocket(t,s) {}
  82. #endif
  83. virtual ~NativeUdpSocket()
  84. {
  85. #ifdef __WINDOWS__
  86. ::closesocket(_sock);
  87. #else
  88. ::close(_sock);
  89. #endif
  90. }
  91. virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen)
  92. {
  93. if (to.isV6()) {
  94. #ifdef __WINDOWS__
  95. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  96. #else
  97. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  98. #endif
  99. } else {
  100. #ifdef __WINDOWS__
  101. return ((int)sendto(_sock,(const char *)msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  102. #else
  103. return ((int)sendto(_sock,msg,msglen,0,to.saddr(),to.saddrLen()) == (int)msglen);
  104. #endif
  105. }
  106. }
  107. inline bool notifyAvailableForRead(const SharedPtr<Socket> &self,NativeSocketManager *sm,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg)
  108. {
  109. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> buf;
  110. InetAddress from;
  111. socklen_t salen = from.saddrSpaceLen();
  112. int n = (int)recvfrom(_sock,(char *)(buf.data()),ZT_SOCKET_MAX_MESSAGE_LEN,0,from.saddr(),&salen);
  113. if (n > 0) {
  114. buf.setSize((unsigned int)n);
  115. try {
  116. handler(self,arg,from,buf);
  117. } catch ( ... ) {} // handlers should not throw
  118. }
  119. return true;
  120. }
  121. inline bool notifyAvailableForWrite(const SharedPtr<Socket> &self,NativeSocketManager *sm)
  122. {
  123. return true;
  124. }
  125. };
  126. /**
  127. * A TCP socket encapsulating ZeroTier packets over a TCP stream connection
  128. *
  129. * This implements a simple packet encapsulation that is designed to look like
  130. * a TLS connection. It's not a TLS connection, but it sends TLS format record
  131. * headers. It could be extended in the future to implement a fake TLS
  132. * handshake.
  133. *
  134. * At the moment, each packet is just made to look like TLS application data:
  135. * <[1] TLS content type> - currently 0x17 for "application data"
  136. * <[1] TLS major version> - currently 0x03 for TLS 1.2
  137. * <[1] TLS minor version> - currently 0x03 for TLS 1.2
  138. * <[2] payload length> - 16-bit length of payload in bytes
  139. * <[...] payload> - Message payload
  140. *
  141. * The primary purpose of TCP sockets is to work over ports like HTTPS(443),
  142. * allowing users behind particularly fascist firewalls to at least reach
  143. * ZeroTier's supernodes. UDP is the preferred method of communication as
  144. * encapsulating L2 and L3 protocols over TCP is inherently inefficient
  145. * due to double-ACKs. So TCP is only used as a fallback.
  146. */
  147. class NativeTcpSocket : public NativeSocket
  148. {
  149. public:
  150. #ifdef __WINDOWS__
  151. NativeTcpSocket(NativeSocketManager *sm,SOCKET s,Socket::Type t,bool c,const InetAddress &r) :
  152. #else
  153. NativeTcpSocket(NativeSocketManager *sm,int s,Socket::Type t,bool c,const InetAddress &r) :
  154. #endif
  155. NativeSocket(t,s),
  156. _lastActivity(Utils::now()),
  157. _sm(sm),
  158. _inptr(0),
  159. _outptr(0),
  160. _connecting(c),
  161. _remote(r) {}
  162. virtual ~NativeTcpSocket()
  163. {
  164. #ifdef __WINDOWS__
  165. ::closesocket(_sock);
  166. #else
  167. ::close(_sock);
  168. #endif
  169. }
  170. virtual bool send(const InetAddress &to,const void *msg,unsigned int msglen)
  171. {
  172. if (msglen > ZT_SOCKET_MAX_MESSAGE_LEN)
  173. return false; // message too big
  174. if (!msglen)
  175. return true; // sanity check
  176. Mutex::Lock _l(_writeLock);
  177. bool writeInProgress = ((_outptr != 0)||(_connecting));
  178. if ((_outptr + 5 + msglen) > (unsigned int)sizeof(_outbuf))
  179. return false;
  180. _outbuf[_outptr++] = 0x17; // look like TLS data
  181. _outbuf[_outptr++] = 0x03;
  182. _outbuf[_outptr++] = 0x03; // look like TLS 1.2
  183. _outbuf[_outptr++] = (unsigned char)((msglen >> 8) & 0xff);
  184. _outbuf[_outptr++] = (unsigned char)(msglen & 0xff);
  185. for(unsigned int i=0;i<msglen;++i)
  186. _outbuf[_outptr++] = ((const unsigned char *)msg)[i];
  187. if (!writeInProgress) {
  188. // If no output was enqueued before this, try to send() it and then
  189. // start a queued write if any remains after that.
  190. int n = (int)::send(_sock,(const char *)_outbuf,_outptr,0);
  191. if (n > 0)
  192. memmove(_outbuf,_outbuf + (unsigned int)n,_outptr -= (unsigned int)n);
  193. if (_outptr) {
  194. _sm->_startNotifyWrite(this);
  195. _sm->whack();
  196. }
  197. } // else just leave in _outbuf[] to get written when stream is available for write
  198. return true;
  199. }
  200. inline bool notifyAvailableForRead(const SharedPtr<Socket> &self,NativeSocketManager *sm,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg)
  201. {
  202. unsigned char buf[65536];
  203. int n = (int)::recv(_sock,(char *)buf,sizeof(buf),0);
  204. if (n <= 0)
  205. return false; // read error, stream probably closed
  206. unsigned int p = _inptr,pl = 0;
  207. for(int k=0;k<n;++k) {
  208. _inbuf[p++] = buf[k];
  209. if (p >= (int)sizeof(_inbuf))
  210. return false; // read overrun, packet too large or invalid
  211. if ((!pl)&&(p >= 5)) {
  212. if (_inbuf[0] == 0x17) {
  213. // fake TLS data frame, next two bytes are TLS version and are ignored
  214. pl = (((unsigned int)_inbuf[3] << 8) | (unsigned int)_inbuf[4]) + 5;
  215. } else return false; // in the future we may support fake TLS handshakes
  216. }
  217. if ((pl)&&(p >= pl)) {
  218. Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> data(_inbuf + 5,pl - 5);
  219. memmove(_inbuf,_inbuf + pl,p -= pl);
  220. try {
  221. handler(self,arg,_remote,data);
  222. } catch ( ... ) {} // handlers should not throw
  223. pl = 0;
  224. }
  225. }
  226. _inptr = p;
  227. return true;
  228. }
  229. inline bool notifyAvailableForWrite(const SharedPtr<Socket> &self,NativeSocketManager *sm)
  230. {
  231. Mutex::Lock _l(_writeLock);
  232. if (_connecting)
  233. _connecting = false;
  234. if (_outptr) {
  235. int n = (int)::send(_sock,(const char *)_outbuf,_outptr,0);
  236. #ifdef __WINDOWS__
  237. if (n == SOCKET_ERROR) {
  238. switch(WSAGetLastError()) {
  239. case WSAEINTR:
  240. case WSAEWOULDBLOCK:
  241. break;
  242. default:
  243. return false;
  244. }
  245. #else
  246. if (n <= 0) {
  247. switch(errno) {
  248. #ifdef EAGAIN
  249. case EAGAIN:
  250. #endif
  251. #if defined(EWOULDBLOCK) && ( !defined(EAGAIN) || (EWOULDBLOCK != EAGAIN) )
  252. case EWOULDBLOCK:
  253. #endif
  254. #ifdef EINTR
  255. case EINTR:
  256. #endif
  257. break;
  258. default:
  259. return false;
  260. }
  261. #endif
  262. } else memmove(_outbuf,_outbuf + (unsigned int)n,_outptr -= (unsigned int)n);
  263. }
  264. if (!_outptr)
  265. sm->_stopNotifyWrite(this);
  266. return true;
  267. }
  268. unsigned char _inbuf[ZT_SOCKET_MAX_MESSAGE_LEN];
  269. unsigned char _outbuf[ZT_SOCKET_MAX_MESSAGE_LEN * 4];
  270. uint64_t _lastActivity; // updated whenever data is received, checked directly by SocketManager for stale TCP cleanup
  271. NativeSocketManager *_sm;
  272. unsigned int _inptr;
  273. unsigned int _outptr;
  274. bool _connecting; // manipulated directly by SocketManager, true if connect() is in progress
  275. InetAddress _remote;
  276. Mutex _writeLock;
  277. };
  278. //////////////////////////////////////////////////////////////////////////////
  279. #ifdef __WINDOWS__
  280. // hack copied from StackOverflow, behaves a bit like pipe() on *nix systems
  281. static inline void winPipeHack(SOCKET fds[2])
  282. {
  283. struct sockaddr_in inaddr;
  284. struct sockaddr addr;
  285. SOCKET lst=::socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  286. memset(&inaddr, 0, sizeof(inaddr));
  287. memset(&addr, 0, sizeof(addr));
  288. inaddr.sin_family = AF_INET;
  289. inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  290. inaddr.sin_port = 0;
  291. int yes=1;
  292. setsockopt(lst,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
  293. bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr));
  294. listen(lst,1);
  295. int len=sizeof(inaddr);
  296. getsockname(lst, &addr,&len);
  297. fds[0]=::socket(AF_INET, SOCK_STREAM,0);
  298. connect(fds[0],&addr,len);
  299. fds[1]=accept(lst,0,0);
  300. closesocket(lst);
  301. }
  302. #endif
  303. NativeSocketManager::NativeSocketManager(int localUdpPort,int localTcpPort) :
  304. SocketManager(),
  305. _whackSendPipe(INVALID_SOCKET),
  306. _whackReceivePipe(INVALID_SOCKET),
  307. _tcpV4ListenSocket(INVALID_SOCKET),
  308. _tcpV6ListenSocket(INVALID_SOCKET),
  309. _nfds(0)
  310. {
  311. FD_ZERO(&_readfds);
  312. FD_ZERO(&_writefds);
  313. // Create a pipe or socket pair that can be used to interrupt select()
  314. #ifdef __WINDOWS__
  315. {
  316. SOCKET tmps[2] = { INVALID_SOCKET,INVALID_SOCKET };
  317. winPipeHack(tmps);
  318. _whackSendPipe = tmps[0];
  319. _whackReceivePipe = tmps[1];
  320. u_long iMode=1;
  321. ioctlsocket(tmps[1],FIONBIO,&iMode);
  322. }
  323. #else
  324. {
  325. int tmpfds[2];
  326. if (::pipe(tmpfds))
  327. throw std::runtime_error("pipe() failed");
  328. _whackSendPipe = tmpfds[1];
  329. _whackReceivePipe = tmpfds[0];
  330. fcntl(_whackReceivePipe,F_SETFL,O_NONBLOCK);
  331. }
  332. #endif
  333. FD_SET(_whackReceivePipe,&_readfds);
  334. if (localTcpPort > 0) {
  335. if (localTcpPort > 0xffff) {
  336. _closeSockets();
  337. throw std::runtime_error("invalid local TCP port number");
  338. }
  339. { // bind TCP IPv6
  340. _tcpV6ListenSocket = ::socket(AF_INET6,SOCK_STREAM,0);
  341. #ifdef __WINDOWS__
  342. if (_tcpV6ListenSocket != INVALID_SOCKET) {
  343. {
  344. BOOL f;
  345. f = TRUE; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  346. f = TRUE; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  347. u_long iMode=1;
  348. ioctlsocket(_tcpV6ListenSocket,FIONBIO,&iMode);
  349. }
  350. #else
  351. if (_tcpV6ListenSocket > 0) {
  352. {
  353. int f;
  354. f = 1; ::setsockopt(_tcpV6ListenSocket,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  355. f = 1; ::setsockopt(_tcpV6ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  356. fcntl(_tcpV6ListenSocket,F_SETFL,O_NONBLOCK);
  357. }
  358. #endif // __WINDOWS__ / not __WINDOWS__
  359. struct sockaddr_in6 sin6;
  360. memset(&sin6,0,sizeof(sin6));
  361. sin6.sin6_family = AF_INET6;
  362. sin6.sin6_port = htons(localTcpPort);
  363. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  364. if (::bind(_tcpV6ListenSocket,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  365. _closeSockets();
  366. throw std::runtime_error("unable to bind to local TCP port");
  367. }
  368. if (::listen(_tcpV6ListenSocket,16)) {
  369. _closeSockets();
  370. throw std::runtime_error("listen() failed");
  371. }
  372. FD_SET(_tcpV6ListenSocket,&_readfds);
  373. }
  374. }
  375. { // bind TCP IPv4
  376. _tcpV4ListenSocket = ::socket(AF_INET,SOCK_STREAM,0);
  377. #ifdef __WINDOWS__
  378. if (_tcpV4ListenSocket == INVALID_SOCKET) {
  379. #else
  380. if (_tcpV4ListenSocket <= 0) {
  381. #endif
  382. _closeSockets();
  383. throw std::runtime_error("unable to create IPv4 SOCK_STREAM socket");
  384. }
  385. #ifdef __WINDOWS__
  386. {
  387. BOOL f = TRUE; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  388. u_long iMode=1;
  389. ioctlsocket(_tcpV4ListenSocket,FIONBIO,&iMode);
  390. }
  391. #else
  392. {
  393. int f = 1; ::setsockopt(_tcpV4ListenSocket,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  394. fcntl(_tcpV4ListenSocket,F_SETFL,O_NONBLOCK);
  395. }
  396. #endif
  397. struct sockaddr_in sin4;
  398. memset(&sin4,0,sizeof(sin4));
  399. sin4.sin_family = AF_INET;
  400. sin4.sin_port = htons(localTcpPort);
  401. sin4.sin_addr.s_addr = INADDR_ANY;
  402. if (::bind(_tcpV4ListenSocket,(const struct sockaddr *)&sin4,sizeof(sin4))) {
  403. _closeSockets();
  404. throw std::runtime_error("unable to bind to local TCP port");
  405. }
  406. if (::listen(_tcpV4ListenSocket,16)) {
  407. _closeSockets();
  408. throw std::runtime_error("listen() failed");
  409. }
  410. FD_SET(_tcpV4ListenSocket,&_readfds);
  411. }
  412. }
  413. if (localUdpPort > 0) {
  414. if (localUdpPort > 0xffff) {
  415. _closeSockets();
  416. throw std::runtime_error("invalid local UDP port number");
  417. }
  418. { // bind UDP IPv6
  419. #ifdef __WINDOWS__
  420. SOCKET s = ::socket(AF_INET6,SOCK_DGRAM,0);
  421. if (s != INVALID_SOCKET) {
  422. #else
  423. int s = ::socket(AF_INET6,SOCK_DGRAM,0);
  424. if (s > 0) {
  425. #endif
  426. {
  427. int bs = 1048576;
  428. while (bs >= 65536) {
  429. int tmpbs = bs;
  430. if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)&tmpbs,sizeof(tmpbs)) == 0)
  431. break;
  432. bs -= 16384;
  433. }
  434. bs = 1048576;
  435. while (bs >= 65536) {
  436. int tmpbs = bs;
  437. if (setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)&tmpbs,sizeof(tmpbs)) == 0)
  438. break;
  439. bs -= 16384;
  440. }
  441. #ifdef __WINDOWS__
  442. BOOL f;
  443. f = TRUE; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  444. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  445. f = FALSE; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&f,sizeof(f));
  446. f = TRUE; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(const char *)&f,sizeof(f));
  447. #else
  448. int f;
  449. f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  450. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  451. f = 1; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(void *)&f,sizeof(f));
  452. #ifdef IP_DONTFRAG
  453. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  454. #endif
  455. #ifdef IP_MTU_DISCOVER
  456. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  457. #endif
  458. #ifdef IPV6_MTU_DISCOVER
  459. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
  460. #endif
  461. #endif
  462. }
  463. struct sockaddr_in6 sin6;
  464. memset(&sin6,0,sizeof(sin6));
  465. sin6.sin6_family = AF_INET6;
  466. sin6.sin6_port = htons(localUdpPort);
  467. memcpy(&(sin6.sin6_addr),&in6addr_any,sizeof(struct in6_addr));
  468. if (::bind(s,(const struct sockaddr *)&sin6,sizeof(sin6))) {
  469. CLOSE_SOCKET(s);
  470. _closeSockets();
  471. throw std::runtime_error("unable to bind to port");
  472. }
  473. _udpV6Socket = SharedPtr<Socket>(new NativeUdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V6,s));
  474. #ifdef __WINDOWS__
  475. u_long iMode=1;
  476. ioctlsocket(s,FIONBIO,&iMode);
  477. #else
  478. fcntl(s,F_SETFL,O_NONBLOCK);
  479. #endif
  480. FD_SET(s,&_readfds);
  481. }
  482. }
  483. { // bind UDP IPv4
  484. #ifdef __WINDOWS__
  485. SOCKET s = ::socket(AF_INET,SOCK_DGRAM,0);
  486. if (s == INVALID_SOCKET) {
  487. _closeSockets();
  488. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  489. }
  490. #else
  491. int s = ::socket(AF_INET,SOCK_DGRAM,0);
  492. if (s <= 0) {
  493. _closeSockets();
  494. throw std::runtime_error("unable to create IPv4 SOCK_DGRAM socket");
  495. }
  496. #endif
  497. {
  498. int bs = 1048576;
  499. while (bs >= 65536) {
  500. int tmpbs = bs;
  501. if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)&tmpbs,sizeof(tmpbs)) == 0)
  502. break;
  503. bs -= 16384;
  504. }
  505. bs = 1048576;
  506. while (bs >= 65536) {
  507. int tmpbs = bs;
  508. if (setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)&tmpbs,sizeof(tmpbs)) == 0)
  509. break;
  510. bs -= 16384;
  511. }
  512. #ifdef __WINDOWS__
  513. BOOL f;
  514. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  515. f = FALSE; setsockopt(s,IPPROTO_IP,IP_DONTFRAGMENT,(const char *)&f,sizeof(f));
  516. f = TRUE; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(const char *)&f,sizeof(f));
  517. #else
  518. int f;
  519. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  520. f = 1; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(void *)&f,sizeof(f));
  521. #ifdef IP_DONTFRAG
  522. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  523. #endif
  524. #ifdef IP_MTU_DISCOVER
  525. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  526. #endif
  527. #endif
  528. }
  529. struct sockaddr_in sin4;
  530. memset(&sin4,0,sizeof(sin4));
  531. sin4.sin_family = AF_INET;
  532. sin4.sin_port = htons(localUdpPort);
  533. sin4.sin_addr.s_addr = INADDR_ANY;
  534. if (::bind(s,(const struct sockaddr *)&sin4,sizeof(sin4))) {
  535. CLOSE_SOCKET(s);
  536. _closeSockets();
  537. throw std::runtime_error("unable to bind to port");
  538. }
  539. _udpV4Socket = SharedPtr<Socket>(new NativeUdpSocket(Socket::ZT_SOCKET_TYPE_UDP_V4,s));
  540. #ifdef __WINDOWS__
  541. u_long iMode=1;
  542. ioctlsocket(s,FIONBIO,&iMode);
  543. #else
  544. fcntl(s,F_SETFL,O_NONBLOCK);
  545. #endif
  546. FD_SET(s,&_readfds);
  547. }
  548. }
  549. _updateNfds();
  550. }
  551. NativeSocketManager::~NativeSocketManager()
  552. {
  553. Mutex::Lock _l(_pollLock);
  554. _closeSockets();
  555. }
  556. bool NativeSocketManager::send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen)
  557. {
  558. if (tcp) {
  559. SharedPtr<Socket> ts;
  560. {
  561. Mutex::Lock _l(_tcpSockets_m);
  562. std::map< InetAddress,SharedPtr<Socket> >::iterator opents(_tcpSockets.find(to));
  563. if (opents != _tcpSockets.end())
  564. ts = opents->second;
  565. }
  566. if (ts)
  567. return ts->send(to,msg,msglen);
  568. if (!autoConnectTcp)
  569. return false;
  570. #ifdef __WINDOWS__
  571. SOCKET s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
  572. if (s == INVALID_SOCKET)
  573. return false;
  574. { u_long iMode=1; ioctlsocket(s,FIONBIO,&iMode); }
  575. #ifdef ZT_TCP_NODELAY
  576. { BOOL f = TRUE; setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  577. #endif
  578. #else
  579. int s = ::socket(to.isV4() ? AF_INET : AF_INET6,SOCK_STREAM,0);
  580. if (s <= 0)
  581. return false;
  582. if (s >= FD_SETSIZE) {
  583. ::close(s);
  584. return false;
  585. }
  586. fcntl(s,F_SETFL,O_NONBLOCK);
  587. #ifdef ZT_TCP_NODELAY
  588. { int f = 1; setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  589. #endif
  590. #endif
  591. bool connecting = false;
  592. if (::connect(s,to.saddr(),to.saddrLen())) {
  593. #ifdef __WINDOWS__
  594. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  595. #else
  596. if (errno != EINPROGRESS) {
  597. #endif
  598. CLOSE_SOCKET(s);
  599. return false;
  600. } else connecting = true;
  601. }
  602. ts = SharedPtr<Socket>(new NativeTcpSocket(this,s,Socket::ZT_SOCKET_TYPE_TCP_OUT,connecting,to));
  603. if (!ts->send(to,msg,msglen)) {
  604. _fdSetLock.lock();
  605. FD_CLR(s,&_readfds);
  606. FD_CLR(s,&_writefds);
  607. _fdSetLock.unlock();
  608. return false;
  609. }
  610. {
  611. Mutex::Lock _l(_tcpSockets_m);
  612. _tcpSockets[to] = ts;
  613. }
  614. _fdSetLock.lock();
  615. FD_SET(s,&_readfds);
  616. if (connecting)
  617. FD_SET(s,&_writefds);
  618. _fdSetLock.unlock();
  619. _updateNfds();
  620. whack();
  621. return true;
  622. } else if (to.isV4()) {
  623. if (_udpV4Socket)
  624. return _udpV4Socket->send(to,msg,msglen);
  625. } else if (to.isV6()) {
  626. if (_udpV6Socket)
  627. return _udpV6Socket->send(to,msg,msglen);
  628. }
  629. return false;
  630. }
  631. void NativeSocketManager::poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg)
  632. {
  633. fd_set rfds,wfds,efds;
  634. struct timeval tv;
  635. std::vector< SharedPtr<Socket> > ts;
  636. #ifdef __WINDOWS__
  637. SOCKET sockfd;
  638. #else
  639. int sockfd;
  640. #endif
  641. Mutex::Lock _l(_pollLock);
  642. _fdSetLock.lock();
  643. memcpy(&rfds,&_readfds,sizeof(rfds));
  644. memcpy(&wfds,&_writefds,sizeof(wfds));
  645. _fdSetLock.unlock();
  646. FD_ZERO(&efds);
  647. #ifdef __WINDOWS__
  648. // Windows signals failed connects in exceptfds
  649. {
  650. Mutex::Lock _l2(_tcpSockets_m);
  651. for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
  652. if (((TcpSocket *)s->second.ptr())->_connecting)
  653. FD_SET(s->second->_sock,&efds);
  654. }
  655. }
  656. #endif
  657. tv.tv_sec = (long)(timeout / 1000);
  658. tv.tv_usec = (long)((timeout % 1000) * 1000);
  659. select(_nfds + 1,&rfds,&wfds,&efds,(timeout > 0) ? &tv : (struct timeval *)0);
  660. if (FD_ISSET(_whackReceivePipe,&rfds)) {
  661. char tmp[16];
  662. #ifdef __WINDOWS__
  663. ::recv(_whackReceivePipe,tmp,16,0);
  664. #else
  665. ::read(_whackReceivePipe,tmp,16);
  666. #endif
  667. }
  668. if ((_tcpV4ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV4ListenSocket,&rfds))) {
  669. struct sockaddr_in from;
  670. socklen_t fromlen = sizeof(from);
  671. sockfd = accept(_tcpV4ListenSocket,(struct sockaddr *)&from,&fromlen);
  672. #ifdef __WINDOWS__
  673. if (sockfd != INVALID_SOCKET) {
  674. #else
  675. if (sockfd > 0) {
  676. if (sockfd < FD_SETSIZE) {
  677. #endif
  678. InetAddress fromia((const struct sockaddr *)&from);
  679. Mutex::Lock _l2(_tcpSockets_m);
  680. try {
  681. _tcpSockets[fromia] = SharedPtr<Socket>(new NativeTcpSocket(this,sockfd,Socket::ZT_SOCKET_TYPE_TCP_IN,false,fromia));
  682. #ifdef __WINDOWS__
  683. { u_long iMode=1; ioctlsocket(sockfd,FIONBIO,&iMode); }
  684. #ifdef ZT_TCP_NODELAY
  685. { BOOL f = TRUE; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  686. #endif
  687. #else
  688. fcntl(sockfd,F_SETFL,O_NONBLOCK);
  689. #ifdef ZT_TCP_NODELAY
  690. { int f = 1; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  691. #endif
  692. #endif
  693. _fdSetLock.lock();
  694. FD_SET(sockfd,&_readfds);
  695. _fdSetLock.unlock();
  696. if ((int)sockfd > (int)_nfds)
  697. _nfds = (int)sockfd;
  698. } catch ( ... ) {
  699. CLOSE_SOCKET(sockfd);
  700. }
  701. #ifndef __WINDOWS__
  702. } else {
  703. CLOSE_SOCKET(sockfd);
  704. }
  705. #endif
  706. }
  707. }
  708. if ((_tcpV6ListenSocket != INVALID_SOCKET)&&(FD_ISSET(_tcpV6ListenSocket,&rfds))) {
  709. struct sockaddr_in6 from;
  710. socklen_t fromlen = sizeof(from);
  711. sockfd = accept(_tcpV6ListenSocket,(struct sockaddr *)&from,&fromlen);
  712. #ifdef __WINDOWS__
  713. if (sockfd != INVALID_SOCKET) {
  714. #else
  715. if (sockfd > 0) {
  716. if (sockfd < FD_SETSIZE) {
  717. #endif
  718. InetAddress fromia((const struct sockaddr *)&from);
  719. Mutex::Lock _l2(_tcpSockets_m);
  720. try {
  721. _tcpSockets[fromia] = SharedPtr<Socket>(new NativeTcpSocket(this,sockfd,Socket::ZT_SOCKET_TYPE_TCP_IN,false,fromia));
  722. #ifdef __WINDOWS__
  723. { u_long iMode=1; ioctlsocket(sockfd,FIONBIO,&iMode); }
  724. #ifdef ZT_TCP_NODELAY
  725. { BOOL f = TRUE; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  726. #endif
  727. #else
  728. fcntl(sockfd,F_SETFL,O_NONBLOCK);
  729. #ifdef ZT_TCP_NODELAY
  730. { int f = 1; setsockopt(sockfd,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  731. #endif
  732. #endif
  733. _fdSetLock.lock();
  734. FD_SET(sockfd,&_readfds);
  735. _fdSetLock.unlock();
  736. if ((int)sockfd > (int)_nfds)
  737. _nfds = (int)sockfd;
  738. } catch ( ... ) {
  739. CLOSE_SOCKET(sockfd);
  740. }
  741. #ifndef __WINDOWS__
  742. } else {
  743. CLOSE_SOCKET(sockfd);
  744. }
  745. #endif
  746. }
  747. }
  748. {
  749. NativeUdpSocket *usock = (NativeUdpSocket *)_udpV4Socket.ptr();
  750. if ((usock)&&(FD_ISSET(usock->_sock,&rfds))) {
  751. usock->notifyAvailableForRead(_udpV4Socket,this,handler,arg);
  752. }
  753. usock = (NativeUdpSocket *)_udpV6Socket.ptr();
  754. if ((usock)&&(FD_ISSET(usock->_sock,&rfds))) {
  755. usock->notifyAvailableForRead(_udpV6Socket,this,handler,arg);
  756. }
  757. }
  758. bool closedSockets = false;
  759. { // grab copy of TCP sockets list because _tcpSockets[] might be changed in a handler
  760. Mutex::Lock _l2(_tcpSockets_m);
  761. if (!_tcpSockets.empty()) {
  762. ts.reserve(_tcpSockets.size());
  763. uint64_t now = Utils::now();
  764. for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();) {
  765. NativeTcpSocket *tsock = (NativeTcpSocket *)s->second.ptr();
  766. #ifdef __WINDOWS__
  767. if ( ((now - tsock->_lastActivity) < ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT) && (! ((tsock->_connecting)&&(FD_ISSET(tsock->_sock,&efds))) ) ) {
  768. #else
  769. if ((now - tsock->_lastActivity) < ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT) {
  770. #endif
  771. ts.push_back(s->second);
  772. ++s;
  773. } else {
  774. _fdSetLock.lock();
  775. FD_CLR(tsock->_sock,&_readfds);
  776. FD_CLR(tsock->_sock,&_writefds);
  777. _fdSetLock.unlock();
  778. _tcpSockets.erase(s++);
  779. closedSockets = true;
  780. }
  781. }
  782. }
  783. }
  784. for(std::vector< SharedPtr<Socket> >::iterator s(ts.begin());s!=ts.end();++s) {
  785. NativeTcpSocket *tsock = (NativeTcpSocket *)s->ptr();
  786. if (FD_ISSET(tsock->_sock,&wfds)) {
  787. if (!tsock->notifyAvailableForWrite(*s,this)) {
  788. {
  789. Mutex::Lock _l2(_tcpSockets_m);
  790. _tcpSockets.erase(tsock->_remote);
  791. }
  792. _fdSetLock.lock();
  793. FD_CLR(tsock->_sock,&_readfds);
  794. FD_CLR(tsock->_sock,&_writefds);
  795. _fdSetLock.unlock();
  796. closedSockets = true;
  797. continue;
  798. }
  799. }
  800. if (FD_ISSET(tsock->_sock,&rfds)) {
  801. if (!tsock->notifyAvailableForRead(*s,this,handler,arg)) {
  802. {
  803. Mutex::Lock _l2(_tcpSockets_m);
  804. _tcpSockets.erase(tsock->_remote);
  805. }
  806. _fdSetLock.lock();
  807. FD_CLR(tsock->_sock,&_readfds);
  808. FD_CLR(tsock->_sock,&_writefds);
  809. _fdSetLock.unlock();
  810. closedSockets = true;
  811. continue;
  812. }
  813. }
  814. }
  815. if (closedSockets)
  816. _updateNfds();
  817. }
  818. void NativeSocketManager::whack()
  819. {
  820. _whackSendPipe_m.lock();
  821. #ifdef __WINDOWS__
  822. ::send(_whackSendPipe,(const char *)this,1,0);
  823. #else
  824. ::write(_whackSendPipe,(const void *)this,1); // data is arbitrary, just send a byte
  825. #endif
  826. _whackSendPipe_m.unlock();
  827. }
  828. void NativeSocketManager::closeTcpSockets()
  829. {
  830. {
  831. Mutex::Lock _l2(_tcpSockets_m);
  832. _fdSetLock.lock();
  833. for(std::map< InetAddress,SharedPtr<Socket> >::iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
  834. FD_CLR(((NativeTcpSocket *)s->second.ptr())->_sock,&_readfds);
  835. FD_CLR(((NativeTcpSocket *)s->second.ptr())->_sock,&_writefds);
  836. }
  837. _fdSetLock.unlock();
  838. _tcpSockets.clear();
  839. }
  840. _updateNfds();
  841. }
  842. void NativeSocketManager::_startNotifyWrite(const NativeSocket *sock)
  843. {
  844. _fdSetLock.lock();
  845. FD_SET(sock->_sock,&_writefds);
  846. _fdSetLock.unlock();
  847. }
  848. void NativeSocketManager::_stopNotifyWrite(const NativeSocket *sock)
  849. {
  850. _fdSetLock.lock();
  851. FD_CLR(sock->_sock,&_writefds);
  852. _fdSetLock.unlock();
  853. }
  854. void NativeSocketManager::_closeSockets()
  855. {
  856. #ifdef __WINDOWS__
  857. if (_whackSendPipe != INVALID_SOCKET)
  858. ::closesocket(_whackSendPipe);
  859. if (_whackReceivePipe != INVALID_SOCKET)
  860. ::closesocket(_whackReceivePipe);
  861. if (_tcpV4ListenSocket != INVALID_SOCKET)
  862. ::closesocket(_tcpV4ListenSocket);
  863. if (_tcpV6ListenSocket != INVALID_SOCKET)
  864. ::closesocket(_tcpV6ListenSocket);
  865. #else
  866. if (_whackSendPipe > 0)
  867. ::close(_whackSendPipe);
  868. if (_whackReceivePipe > 0)
  869. ::close(_whackReceivePipe);
  870. if (_tcpV4ListenSocket > 0)
  871. ::close(_tcpV4ListenSocket);
  872. if (_tcpV4ListenSocket > 0)
  873. ::close(_tcpV6ListenSocket);
  874. #endif
  875. }
  876. void NativeSocketManager::_updateNfds()
  877. {
  878. #ifdef __WINDOWS__
  879. SOCKET nfds = _whackSendPipe;
  880. #else
  881. int nfds = _whackSendPipe;
  882. #endif
  883. if (_whackReceivePipe > nfds)
  884. nfds = _whackReceivePipe;
  885. if (_tcpV4ListenSocket > nfds)
  886. nfds = _tcpV4ListenSocket;
  887. if (_tcpV6ListenSocket > nfds)
  888. nfds = _tcpV6ListenSocket;
  889. if ((_udpV4Socket)&&(((NativeUdpSocket *)_udpV4Socket.ptr())->_sock > nfds))
  890. nfds = ((NativeUdpSocket *)_udpV4Socket.ptr())->_sock;
  891. if ((_udpV6Socket)&&(((NativeUdpSocket *)_udpV6Socket.ptr())->_sock > nfds))
  892. nfds = ((NativeUdpSocket *)_udpV6Socket.ptr())->_sock;
  893. Mutex::Lock _l(_tcpSockets_m);
  894. for(std::map< InetAddress,SharedPtr<Socket> >::const_iterator s(_tcpSockets.begin());s!=_tcpSockets.end();++s) {
  895. if (((NativeTcpSocket *)s->second.ptr())->_sock > nfds)
  896. nfds = ((NativeTcpSocket *)s->second.ptr())->_sock;
  897. }
  898. _nfds = (int)nfds;
  899. }
  900. } // namespace ZeroTier