Phy.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. #ifndef ZT_PHY_HPP
  28. #define ZT_PHY_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <list>
  33. #include <stdexcept>
  34. #if defined(_WIN32) || defined(_WIN64)
  35. #include <WinSock2.h>
  36. #include <WS2tcpip.h>
  37. #include <Windows.h>
  38. #define ZT_PHY_SOCKFD_TYPE SOCKET
  39. #define ZT_PHY_SOCKFD_NULL (INVALID_SOCKET)
  40. #define ZT_PHY_SOCKFD_VALID(s) ((s) != INVALID_SOCKET)
  41. #define ZT_PHY_CLOSE_SOCKET(s) ::closesocket(s)
  42. #define ZT_PHY_MAX_SOCKETS (FD_SETSIZE)
  43. #define ZT_PHY_SOCKADDR_STORAGE_TYPE struct sockaddr_storage
  44. #else // not Windows
  45. #include <errno.h>
  46. #include <signal.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <sys/time.h>
  50. #include <sys/types.h>
  51. #include <sys/select.h>
  52. #include <sys/socket.h>
  53. #include <arpa/inet.h>
  54. #include <netinet/in.h>
  55. #include <netinet/tcp.h>
  56. #define ZT_PHY_SOCKFD_TYPE int
  57. #define ZT_PHY_SOCKFD_NULL (-1)
  58. #define ZT_PHY_SOCKFD_VALID(s) ((s) > -1)
  59. #define ZT_PHY_CLOSE_SOCKET(s) ::close(s)
  60. #define ZT_PHY_MAX_SOCKETS (FD_SETSIZE)
  61. #define ZT_PHY_SOCKADDR_STORAGE_TYPE struct sockaddr_storage
  62. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  63. #define ZT_PHY_HAVE_EVENTFD 1
  64. #include <sys/eventfd.h>
  65. #endif
  66. #endif // Windows or not
  67. namespace ZeroTier {
  68. /**
  69. * Opaque socket type
  70. */
  71. typedef void PhySocket;
  72. /**
  73. * Simple templated non-blocking sockets implementation
  74. *
  75. * Yes there is boost::asio and libuv, but I like small binaries and I hate
  76. * build dependencies. Both drag in a whole bunch of pasta with them.
  77. *
  78. * This class is templated on a pointer to a handler class which must
  79. * implement the following functions:
  80. *
  81. * phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *from,void *data,unsigned long len)
  82. * phyOnTcpConnect(PhySocket *sock,void **uptr,bool success)
  83. * phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from)
  84. * phyOnTcpClose(PhySocket *sock,void **uptr)
  85. * phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  86. * phyOnTcpWritable(PhySocket *sock,void **uptr)
  87. * phyOnUnixAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN)
  88. * phyOnUnixClose(PhySocket *sock,void **uptr)
  89. * phyOnUnixData(PhySocket *sock,void **uptr,void *data,unsigned long len)
  90. * phyOnUnixWritable(PhySocket *sock,void **uptr)
  91. *
  92. * These templates typically refer to function objects. Templates are used to
  93. * avoid the call overhead of indirection, which is surprisingly high for high
  94. * bandwidth applications pushing a lot of packets.
  95. *
  96. * The 'sock' pointer above is an opaque pointer to a socket. Each socket
  97. * has a 'uptr' user-settable/modifiable pointer associated with it, which
  98. * can be set on bind/connect calls and is passed as a void ** to permit
  99. * resetting at any time. The ACCEPT handler takes two sets of sock and
  100. * uptr: sockL and uptrL for the listen socket, and sockN and uptrN for
  101. * the new TCP connection socket that has just been created.
  102. *
  103. * Note that phyOnUnix*() are only required and will only be used on systems
  104. * that support Unix domain sockets.
  105. *
  106. * Handlers are always called. On outgoing TCP connection, CONNECT is always
  107. * called on either success or failure followed by DATA and/or WRITABLE as
  108. * indicated. On socket close, handlers are called unless close() is told
  109. * explicitly not to call handlers. It is safe to close a socket within a
  110. * handler, and in that case close() can be told not to call handlers to
  111. * prevent recursion.
  112. *
  113. * This isn't thread-safe with the exception of whack(), which is safe to
  114. * call from another thread to abort poll().
  115. */
  116. template <typename HANDLER_PTR_TYPE>
  117. class Phy
  118. {
  119. private:
  120. HANDLER_PTR_TYPE _handler;
  121. enum PhySocketType
  122. {
  123. ZT_PHY_SOCKET_CLOSED = 0x00, // socket is closed, will be removed on next poll()
  124. ZT_PHY_SOCKET_TCP_OUT_PENDING = 0x01,
  125. ZT_PHY_SOCKET_TCP_OUT_CONNECTED = 0x02,
  126. ZT_PHY_SOCKET_TCP_IN = 0x03,
  127. ZT_PHY_SOCKET_TCP_LISTEN = 0x04,
  128. ZT_PHY_SOCKET_RAW = 0x05,
  129. ZT_PHY_SOCKET_UDP = 0x06,
  130. ZT_PHY_SOCKET_UNIX_IN = 0x07,
  131. ZT_PHY_SOCKET_UNIX_LISTEN = 0x08
  132. };
  133. struct PhySocketImpl
  134. {
  135. PhySocketType type;
  136. ZT_PHY_SOCKFD_TYPE sock;
  137. void *uptr; // user-settable pointer
  138. ZT_PHY_SOCKADDR_STORAGE_TYPE saddr; // remote for TCP_OUT and TCP_IN, local for TCP_LISTEN, RAW, and UDP
  139. };
  140. std::list<PhySocketImpl> _socks;
  141. fd_set _readfds;
  142. fd_set _writefds;
  143. #if defined(_WIN32) || defined(_WIN64)
  144. fd_set _exceptfds;
  145. #endif
  146. long _nfds;
  147. ZT_PHY_SOCKFD_TYPE _whackReceiveSocket;
  148. ZT_PHY_SOCKFD_TYPE _whackSendSocket;
  149. bool _noDelay;
  150. bool _noCheck;
  151. public:
  152. /**
  153. * @param handler Pointer of type HANDLER_PTR_TYPE to handler
  154. * @param noDelay If true, disable TCP NAGLE algorithm on TCP sockets
  155. * @param noCheck If true, attempt to set UDP SO_NO_CHECK option to disable sending checksums
  156. */
  157. Phy(HANDLER_PTR_TYPE handler,bool noDelay,bool noCheck) :
  158. _handler(handler)
  159. {
  160. FD_ZERO(&_readfds);
  161. FD_ZERO(&_writefds);
  162. #if defined(_WIN32) || defined(_WIN64)
  163. FD_ZERO(&_exceptfds);
  164. SOCKET pipes[2];
  165. { // hack copied from StackOverflow, behaves a bit like pipe() on *nix systems
  166. struct sockaddr_in inaddr;
  167. struct sockaddr addr;
  168. SOCKET lst=::socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
  169. if (lst == INVALID_SOCKET)
  170. throw std::runtime_error("unable to create pipes for select() abort");
  171. memset(&inaddr, 0, sizeof(inaddr));
  172. memset(&addr, 0, sizeof(addr));
  173. inaddr.sin_family = AF_INET;
  174. inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  175. inaddr.sin_port = 0;
  176. int yes=1;
  177. setsockopt(lst,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes));
  178. bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr));
  179. listen(lst,1);
  180. int len=sizeof(inaddr);
  181. getsockname(lst, &addr,&len);
  182. pipes[0]=::socket(AF_INET, SOCK_STREAM,0);
  183. if (pipes[0] == INVALID_SOCKET)
  184. throw std::runtime_error("unable to create pipes for select() abort");
  185. connect(pipes[0],&addr,len);
  186. pipes[1]=accept(lst,0,0);
  187. closesocket(lst);
  188. }
  189. #else // not Windows
  190. int pipes[2];
  191. if (::pipe(pipes))
  192. throw std::runtime_error("unable to create pipes for select() abort");
  193. #endif // Windows or not
  194. _nfds = (pipes[0] > pipes[1]) ? (long)pipes[0] : (long)pipes[1];
  195. _whackReceiveSocket = pipes[0];
  196. _whackSendSocket = pipes[1];
  197. _noDelay = noDelay;
  198. _noCheck = noCheck;
  199. }
  200. ~Phy()
  201. {
  202. for(typename std::list<PhySocketImpl>::const_iterator s(_socks.begin());s!=_socks.end();++s) {
  203. if (s->type != ZT_PHY_SOCKET_CLOSED)
  204. this->close((PhySocket *)&(*s),true);
  205. }
  206. ZT_PHY_CLOSE_SOCKET(_whackReceiveSocket);
  207. ZT_PHY_CLOSE_SOCKET(_whackSendSocket);
  208. }
  209. /**
  210. * Cause poll() to stop waiting immediately
  211. */
  212. inline void whack()
  213. {
  214. #if defined(_WIN32) || defined(_WIN64)
  215. ::send(_whackSendSocket,(const char *)this,1,0);
  216. #else
  217. ::write(_whackSendSocket,(PhySocket *)this,1);
  218. #endif
  219. }
  220. /**
  221. * @return Number of open sockets
  222. */
  223. inline unsigned long count() const throw() { return _socks.size(); }
  224. /**
  225. * @return Maximum number of sockets allowed
  226. */
  227. inline unsigned long maxCount() const throw() { return ZT_PHY_MAX_SOCKETS; }
  228. /**
  229. * Bind a UDP socket
  230. *
  231. * @param localAddress Local endpoint address and port
  232. * @param uptr Initial value of user pointer associated with this socket (default: NULL)
  233. * @param bufferSize Desired socket receive/send buffer size -- will set as close to this as possible (default: 0, leave alone)
  234. * @return Socket or NULL on failure to bind
  235. */
  236. inline PhySocket *udpBind(const struct sockaddr *localAddress,void *uptr = (void *)0,int bufferSize = 0)
  237. {
  238. if (_socks.size() >= ZT_PHY_MAX_SOCKETS)
  239. return (PhySocket *)0;
  240. ZT_PHY_SOCKFD_TYPE s = ::socket(localAddress->sa_family,SOCK_DGRAM,0);
  241. if (!ZT_PHY_SOCKFD_VALID(s))
  242. return (PhySocket *)0;
  243. if (bufferSize > 0) {
  244. int bs = bufferSize;
  245. while (bs >= 65536) {
  246. int tmpbs = bs;
  247. if (setsockopt(s,SOL_SOCKET,SO_RCVBUF,(const char *)&tmpbs,sizeof(tmpbs)) == 0)
  248. break;
  249. bs -= 16384;
  250. }
  251. bs = bufferSize;
  252. while (bs >= 65536) {
  253. int tmpbs = bs;
  254. if (setsockopt(s,SOL_SOCKET,SO_SNDBUF,(const char *)&tmpbs,sizeof(tmpbs)) == 0)
  255. break;
  256. bs -= 16384;
  257. }
  258. }
  259. #if defined(_WIN32) || defined(_WIN64)
  260. {
  261. BOOL f;
  262. if (localAddress->sa_family == AF_INET6) {
  263. f = TRUE; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  264. f = FALSE; setsockopt(s,IPPROTO_IPV6,IPV6_DONTFRAG,(const char *)&f,sizeof(f));
  265. }
  266. f = FALSE; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  267. f = TRUE; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(const char *)&f,sizeof(f));
  268. }
  269. #else // not Windows
  270. {
  271. int f;
  272. if (localAddress->sa_family == AF_INET6) {
  273. f = 1; setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  274. #ifdef IPV6_MTU_DISCOVER
  275. f = 0; setsockopt(s,IPPROTO_IPV6,IPV6_MTU_DISCOVER,&f,sizeof(f));
  276. #endif
  277. }
  278. f = 0; setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  279. f = 1; setsockopt(s,SOL_SOCKET,SO_BROADCAST,(void *)&f,sizeof(f));
  280. #ifdef IP_DONTFRAG
  281. f = 0; setsockopt(s,IPPROTO_IP,IP_DONTFRAG,&f,sizeof(f));
  282. #endif
  283. #ifdef IP_MTU_DISCOVER
  284. f = 0; setsockopt(s,IPPROTO_IP,IP_MTU_DISCOVER,&f,sizeof(f));
  285. #endif
  286. #ifdef SO_NO_CHECK
  287. if (_noCheck) {
  288. f = 1; setsockopt(s,SOL_SOCKET,SO_NO_CHECK,(void *)&f,sizeof(f));
  289. }
  290. #endif
  291. }
  292. #endif // Windows or not
  293. if (::bind(s,localAddress,(localAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))) {
  294. ZT_PHY_CLOSE_SOCKET(s);
  295. return (PhySocket *)0;
  296. }
  297. #if defined(_WIN32) || defined(_WIN64)
  298. { u_long iMode=1; ioctlsocket(s,FIONBIO,&iMode); }
  299. #else
  300. fcntl(s,F_SETFL,O_NONBLOCK);
  301. #endif
  302. try {
  303. _socks.push_back(PhySocketImpl());
  304. } catch ( ... ) {
  305. ZT_PHY_CLOSE_SOCKET(s);
  306. return (PhySocket *)0;
  307. }
  308. PhySocketImpl &sws = _socks.back();
  309. if ((long)s > _nfds)
  310. _nfds = (long)s;
  311. FD_SET(s,&_readfds);
  312. sws.type = ZT_PHY_SOCKET_UDP;
  313. sws.sock = s;
  314. sws.uptr = uptr;
  315. memset(&(sws.saddr),0,sizeof(struct sockaddr_storage));
  316. memcpy(&(sws.saddr),localAddress,(localAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
  317. return (PhySocket *)&sws;
  318. }
  319. /**
  320. * Send a UDP packet
  321. *
  322. * @param sock UDP socket
  323. * @param remoteAddress Destination address (must be correct type for socket)
  324. * @param data Data to send
  325. * @param len Length of packet
  326. * @return True if packet appears to have been sent successfully
  327. */
  328. inline bool udpSend(PhySocket *sock,const struct sockaddr *remoteAddress,const void *data,unsigned long len)
  329. {
  330. PhySocketImpl &sws = *(reinterpret_cast<PhySocketImpl *>(sock));
  331. #if defined(_WIN32) || defined(_WIN64)
  332. return ((long)::sendto(sws.sock,reinterpret_cast<const char *>(data),len,0,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len);
  333. #else
  334. return ((long)::sendto(sws.sock,data,len,0,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) == (long)len);
  335. #endif
  336. }
  337. #ifdef __UNIX_LIKE__
  338. /**
  339. * Listen for connections on a Unix domain socket
  340. *
  341. * @param path Path to Unix domain socket
  342. * @param uptr Arbitrary pointer to associate
  343. * @return PhySocket or NULL if cannot bind
  344. */
  345. inline PhySocket *unixListen(const char *path,void *uptr = (void *)0)
  346. {
  347. struct sockaddr_un sun;
  348. if (_socks.size() >= ZT_PHY_MAX_SOCKETS)
  349. return (PhySocket *)0;
  350. memset(&sun,0,sizeof(sun));
  351. sun.sun_family = AF_UNIX;
  352. if (strlen(path) >= sizeof(sun.sun_path))
  353. return (PhySocket *)0;
  354. strcpy(sun.sun_path,path);
  355. ZT_PHY_SOCKFD_TYPE s = ::socket(PF_UNIX,SOCK_STREAM,0);
  356. if (!ZT_PHY_SOCKFD_VALID(s))
  357. return (PhySocket *)0;
  358. ::fcntl(s,F_SETFL,O_NONBLOCK);
  359. ::unlink(path);
  360. if (::bind(s,(struct sockaddr *)&sun,sizeof(struct sockaddr_un)) != 0) {
  361. ZT_PHY_CLOSE_SOCKET(s);
  362. return (PhySocket *)0;
  363. }
  364. if (::listen(s,128) != 0) {
  365. ZT_PHY_CLOSE_SOCKET(s);
  366. return (PhySocket *)0;
  367. }
  368. try {
  369. _socks.push_back(PhySocketImpl());
  370. } catch ( ... ) {
  371. ZT_PHY_CLOSE_SOCKET(s);
  372. return (PhySocket *)0;
  373. }
  374. PhySocketImpl &sws = _socks.back();
  375. if ((long)s > _nfds)
  376. _nfds = (long)s;
  377. FD_SET(s,&_readfds);
  378. sws.type = ZT_PHY_SOCKET_UNIX_LISTEN;
  379. sws.sock = s;
  380. sws.uptr = uptr;
  381. memset(&(sws.saddr),0,sizeof(struct sockaddr_storage));
  382. memcpy(&(sws.saddr),&sun,sizeof(struct sockaddr_un));
  383. return (PhySocket *)&sws;
  384. }
  385. #endif // __UNIX_LIKE__
  386. /**
  387. * Bind a local listen socket to listen for new TCP connections
  388. *
  389. * @param localAddress Local address and port
  390. * @param uptr Initial value of uptr for new socket (default: NULL)
  391. * @return Socket or NULL on failure to bind
  392. */
  393. inline PhySocket *tcpListen(const struct sockaddr *localAddress,void *uptr = (void *)0)
  394. {
  395. if (_socks.size() >= ZT_PHY_MAX_SOCKETS)
  396. return (PhySocket *)0;
  397. ZT_PHY_SOCKFD_TYPE s = ::socket(localAddress->sa_family,SOCK_STREAM,0);
  398. if (!ZT_PHY_SOCKFD_VALID(s))
  399. return (PhySocket *)0;
  400. #if defined(_WIN32) || defined(_WIN64)
  401. {
  402. BOOL f;
  403. f = TRUE; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f));
  404. f = TRUE; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  405. f = (_noDelay ? TRUE : FALSE); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f));
  406. u_long iMode=1;
  407. ioctlsocket(s,FIONBIO,&iMode);
  408. }
  409. #else
  410. {
  411. int f;
  412. f = 1; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f));
  413. f = 1; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  414. f = (_noDelay ? 1 : 0); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f));
  415. fcntl(s,F_SETFL,O_NONBLOCK);
  416. }
  417. #endif
  418. if (::bind(s,localAddress,(localAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))) {
  419. ZT_PHY_CLOSE_SOCKET(s);
  420. return (PhySocket *)0;
  421. }
  422. if (::listen(s,1024)) {
  423. ZT_PHY_CLOSE_SOCKET(s);
  424. return (PhySocket *)0;
  425. }
  426. try {
  427. _socks.push_back(PhySocketImpl());
  428. } catch ( ... ) {
  429. ZT_PHY_CLOSE_SOCKET(s);
  430. return (PhySocket *)0;
  431. }
  432. PhySocketImpl &sws = _socks.back();
  433. if ((long)s > _nfds)
  434. _nfds = (long)s;
  435. FD_SET(s,&_readfds);
  436. sws.type = ZT_PHY_SOCKET_TCP_LISTEN;
  437. sws.sock = s;
  438. sws.uptr = uptr;
  439. memset(&(sws.saddr),0,sizeof(struct sockaddr_storage));
  440. memcpy(&(sws.saddr),localAddress,(localAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
  441. return (PhySocket *)&sws;
  442. }
  443. /**
  444. * Start a non-blocking connect; CONNECT handler is called on success or failure
  445. *
  446. * A return value of NULL indicates a synchronous failure such as a
  447. * failure to open a socket. The TCP connection handler is not called
  448. * in this case.
  449. *
  450. * It is possible on some platforms for an "instant connect" to occur,
  451. * such as when connecting to a loopback address. In this case, the
  452. * 'connected' result parameter will be set to 'true' and if the
  453. * 'callConnectHandler' flag is true (the default) the TCP connect
  454. * handler will be called before the function returns.
  455. *
  456. * These semantics can be a bit confusing, but they're less so than
  457. * the underlying semantics of asynchronous TCP connect.
  458. *
  459. * @param remoteAddress Remote address
  460. * @param connected Result parameter: set to whether an "instant connect" has occurred (true if yes)
  461. * @param uptr Initial value of uptr for new socket (default: NULL)
  462. * @param callConnectHandler If true, call TCP connect handler even if result is known before function exit (default: true)
  463. * @return New socket or NULL on failure
  464. */
  465. inline PhySocket *tcpConnect(const struct sockaddr *remoteAddress,bool &connected,void *uptr = (void *)0,bool callConnectHandler = true)
  466. {
  467. if (_socks.size() >= ZT_PHY_MAX_SOCKETS)
  468. return (PhySocket *)0;
  469. ZT_PHY_SOCKFD_TYPE s = ::socket(remoteAddress->sa_family,SOCK_STREAM,0);
  470. if (!ZT_PHY_SOCKFD_VALID(s)) {
  471. connected = false;
  472. return (PhySocket *)0;
  473. }
  474. #if defined(_WIN32) || defined(_WIN64)
  475. {
  476. BOOL f;
  477. if (remoteAddress->sa_family == AF_INET6) { f = TRUE; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f)); }
  478. f = TRUE; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f));
  479. f = (_noDelay ? TRUE : FALSE); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f));
  480. u_long iMode=1;
  481. ioctlsocket(s,FIONBIO,&iMode);
  482. }
  483. #else
  484. {
  485. int f;
  486. if (remoteAddress->sa_family == AF_INET6) { f = 1; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f)); }
  487. f = 1; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f));
  488. f = (_noDelay ? 1 : 0); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f));
  489. fcntl(s,F_SETFL,O_NONBLOCK);
  490. }
  491. #endif
  492. connected = true;
  493. if (::connect(s,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))) {
  494. connected = false;
  495. #if defined(_WIN32) || defined(_WIN64)
  496. if (WSAGetLastError() != WSAEWOULDBLOCK) {
  497. #else
  498. if (errno != EINPROGRESS) {
  499. #endif
  500. ZT_PHY_CLOSE_SOCKET(s);
  501. return (PhySocket *)0;
  502. } // else connection is proceeding asynchronously...
  503. }
  504. try {
  505. _socks.push_back(PhySocketImpl());
  506. } catch ( ... ) {
  507. ZT_PHY_CLOSE_SOCKET(s);
  508. return (PhySocket *)0;
  509. }
  510. PhySocketImpl &sws = _socks.back();
  511. if ((long)s > _nfds)
  512. _nfds = (long)s;
  513. if (connected) {
  514. FD_SET(s,&_readfds);
  515. sws.type = ZT_PHY_SOCKET_TCP_OUT_CONNECTED;
  516. } else {
  517. FD_SET(s,&_writefds);
  518. #if defined(_WIN32) || defined(_WIN64)
  519. FD_SET(s,&_exceptfds);
  520. #endif
  521. sws.type = ZT_PHY_SOCKET_TCP_OUT_PENDING;
  522. }
  523. sws.sock = s;
  524. sws.uptr = uptr;
  525. memset(&(sws.saddr),0,sizeof(struct sockaddr_storage));
  526. memcpy(&(sws.saddr),remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
  527. if ((callConnectHandler)&&(connected)) {
  528. try {
  529. _handler->phyOnTcpConnect((PhySocket *)&sws,&(sws.uptr),true);
  530. } catch ( ... ) {}
  531. }
  532. return (PhySocket *)&sws;
  533. }
  534. /**
  535. * Attempt to send data to a TCP connection (non-blocking)
  536. *
  537. * If -1 is returned, the socket should no longer be used as it is now
  538. * destroyed. If callCloseHandler is true, the close handler will be
  539. * called before the function returns.
  540. *
  541. * @param sock An open TCP socket (other socket types will fail)
  542. * @param data Data to send
  543. * @param len Length of data
  544. * @param callCloseHandler If true, call close handler on socket closing failure condition (default: true)
  545. * @return Number of bytes actually sent or -1 on fatal error (socket closure)
  546. */
  547. inline long tcpSend(PhySocket *sock,const void *data,unsigned long len,bool callCloseHandler = true)
  548. {
  549. PhySocketImpl &sws = *(reinterpret_cast<PhySocketImpl *>(sock));
  550. #if defined(_WIN32) || defined(_WIN64)
  551. long n = (long)::send(sws.sock,reinterpret_cast<const char *>(data),len,0);
  552. if (n == SOCKET_ERROR) {
  553. switch(WSAGetLastError()) {
  554. case WSAEINTR:
  555. case WSAEWOULDBLOCK:
  556. return 0;
  557. default:
  558. this->close(sock,callCloseHandler);
  559. return -1;
  560. }
  561. }
  562. #else // not Windows
  563. long n = (long)::send(sws.sock,data,len,0);
  564. if (n < 0) {
  565. switch(errno) {
  566. #ifdef EAGAIN
  567. case EAGAIN:
  568. #endif
  569. #if defined(EWOULDBLOCK) && ( !defined(EAGAIN) || (EWOULDBLOCK != EAGAIN) )
  570. case EWOULDBLOCK:
  571. #endif
  572. #ifdef EINTR
  573. case EINTR:
  574. #endif
  575. return 0;
  576. default:
  577. this->close(sock,callCloseHandler);
  578. return -1;
  579. }
  580. }
  581. #endif // Windows or not
  582. return n;
  583. }
  584. #ifdef __UNIX_LIKE__
  585. /**
  586. * Attempt to send data to a Unix domain socket connection (non-blocking)
  587. *
  588. * If -1 is returned, the socket should no longer be used as it is now
  589. * destroyed. If callCloseHandler is true, the close handler will be
  590. * called before the function returns.
  591. *
  592. * @param sock An open Unix socket (other socket types will fail)
  593. * @param data Data to send
  594. * @param len Length of data
  595. * @param callCloseHandler If true, call close handler on socket closing failure condition (default: true)
  596. * @return Number of bytes actually sent or -1 on fatal error (socket closure)
  597. */
  598. inline long unixSend(PhySocket *sock,const void *data,unsigned long len,bool callCloseHandler = true)
  599. {
  600. PhySocketImpl &sws = *(reinterpret_cast<PhySocketImpl *>(sock));
  601. long n = (long)::write(sws.sock,data,len);
  602. if (n < 0) {
  603. switch(errno) {
  604. #ifdef EAGAIN
  605. case EAGAIN:
  606. #endif
  607. #if defined(EWOULDBLOCK) && ( !defined(EAGAIN) || (EWOULDBLOCK != EAGAIN) )
  608. case EWOULDBLOCK:
  609. #endif
  610. #ifdef EINTR
  611. case EINTR:
  612. #endif
  613. return 0;
  614. default:
  615. this->close(sock,callCloseHandler);
  616. return -1;
  617. }
  618. }
  619. return n;
  620. }
  621. #endif // __UNIX_LIKE__
  622. /**
  623. * Set whether we want to be notified via the TCP writability handler when a socket is writable
  624. *
  625. * Call whack() if this is being done from another thread and you want
  626. * it to take effect immediately. Otherwise it is only guaranteed to
  627. * take effect on the next poll().
  628. *
  629. * @param sock TCP connection socket (other types are not valid)
  630. * @param notifyWritable Want writable notifications?
  631. */
  632. inline const void tcpSetNotifyWritable(PhySocket *sock,bool notifyWritable)
  633. {
  634. PhySocketImpl &sws = *(reinterpret_cast<PhySocketImpl *>(sock));
  635. if (notifyWritable) {
  636. FD_SET(sws.sock,&_writefds);
  637. } else {
  638. FD_CLR(sws.sock,&_writefds);
  639. }
  640. }
  641. /**
  642. * Wait for activity and handle one or more events
  643. *
  644. * Note that this is not guaranteed to wait up to 'timeout' even
  645. * if nothing happens, as whack() or other events such as signals
  646. * may cause premature termination.
  647. *
  648. * @param timeout Timeout in milliseconds or 0 for none (forever)
  649. */
  650. inline void poll(unsigned long timeout)
  651. {
  652. char buf[131072];
  653. struct sockaddr_storage ss;
  654. struct timeval tv;
  655. fd_set rfds,wfds,efds;
  656. memcpy(&rfds,&_readfds,sizeof(rfds));
  657. memcpy(&wfds,&_writefds,sizeof(wfds));
  658. #if defined(_WIN32) || defined(_WIN64)
  659. memcpy(&efds,&_exceptfds,sizeof(efds));
  660. #else
  661. FD_ZERO(&efds);
  662. #endif
  663. tv.tv_sec = (long)(timeout / 1000);
  664. tv.tv_usec = (long)((timeout % 1000) * 1000);
  665. if (::select((int)_nfds + 1,&rfds,&wfds,&efds,(timeout > 0) ? &tv : (struct timeval *)0) <= 0)
  666. return;
  667. if (FD_ISSET(_whackReceiveSocket,&rfds)) {
  668. char tmp[16];
  669. #if defined(_WIN32) || defined(_WIN64)
  670. ::recv(_whackReceiveSocket,tmp,16,0);
  671. #else
  672. ::read(_whackReceiveSocket,tmp,16);
  673. #endif
  674. }
  675. for(typename std::list<PhySocketImpl>::iterator s(_socks.begin());s!=_socks.end();) {
  676. switch (s->type) {
  677. case ZT_PHY_SOCKET_TCP_OUT_PENDING:
  678. #if defined(_WIN32) || defined(_WIN64)
  679. if (FD_ISSET(s->sock,&efds)) {
  680. this->close((PhySocket *)&(*s),true);
  681. } else // ... if
  682. #endif
  683. if (FD_ISSET(s->sock,&wfds)) {
  684. socklen_t slen = sizeof(ss);
  685. if (::getpeername(s->sock,(struct sockaddr *)&ss,&slen) != 0) {
  686. this->close((PhySocket *)&(*s),true);
  687. } else {
  688. s->type = ZT_PHY_SOCKET_TCP_OUT_CONNECTED;
  689. FD_SET(s->sock,&_readfds);
  690. FD_CLR(s->sock,&_writefds);
  691. #if defined(_WIN32) || defined(_WIN64)
  692. FD_CLR(s->sock,&_exceptfds);
  693. #endif
  694. try {
  695. _handler->phyOnTcpConnect((PhySocket *)&(*s),&(s->uptr),true);
  696. } catch ( ... ) {}
  697. }
  698. }
  699. break;
  700. case ZT_PHY_SOCKET_TCP_OUT_CONNECTED:
  701. case ZT_PHY_SOCKET_TCP_IN: {
  702. ZT_PHY_SOCKFD_TYPE sock = s->sock; // if closed, s->sock becomes invalid as s is no longer dereferencable
  703. if (FD_ISSET(sock,&rfds)) {
  704. long n = (long)::recv(sock,buf,sizeof(buf),0);
  705. if (n <= 0) {
  706. this->close((PhySocket *)&(*s),true);
  707. } else {
  708. try {
  709. _handler->phyOnTcpData((PhySocket *)&(*s),&(s->uptr),(void *)buf,(unsigned long)n);
  710. } catch ( ... ) {}
  711. }
  712. }
  713. if ((FD_ISSET(sock,&wfds))&&(FD_ISSET(sock,&_writefds))) {
  714. try {
  715. _handler->phyOnTcpWritable((PhySocket *)&(*s),&(s->uptr));
  716. } catch ( ... ) {}
  717. }
  718. } break;
  719. case ZT_PHY_SOCKET_TCP_LISTEN:
  720. if (FD_ISSET(s->sock,&rfds)) {
  721. memset(&ss,0,sizeof(ss));
  722. socklen_t slen = sizeof(ss);
  723. ZT_PHY_SOCKFD_TYPE newSock = ::accept(s->sock,(struct sockaddr *)&ss,&slen);
  724. if (ZT_PHY_SOCKFD_VALID(newSock)) {
  725. if (_socks.size() >= ZT_PHY_MAX_SOCKETS) {
  726. ZT_PHY_CLOSE_SOCKET(newSock);
  727. } else {
  728. #if defined(_WIN32) || defined(_WIN64)
  729. { BOOL f = (_noDelay ? TRUE : FALSE); setsockopt(newSock,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  730. { u_long iMode=1; ioctlsocket(newSock,FIONBIO,&iMode); }
  731. #else
  732. { int f = (_noDelay ? 1 : 0); setsockopt(newSock,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); }
  733. fcntl(newSock,F_SETFL,O_NONBLOCK);
  734. #endif
  735. _socks.push_back(PhySocketImpl());
  736. PhySocketImpl &sws = _socks.back();
  737. FD_SET(newSock,&_readfds);
  738. if ((long)newSock > _nfds)
  739. _nfds = (long)newSock;
  740. sws.type = ZT_PHY_SOCKET_TCP_IN;
  741. sws.sock = newSock;
  742. sws.uptr = (void *)0;
  743. memcpy(&(sws.saddr),&ss,sizeof(struct sockaddr_storage));
  744. try {
  745. _handler->phyOnTcpAccept((PhySocket *)&(*s),(PhySocket *)&(_socks.back()),&(s->uptr),&(sws.uptr),(const struct sockaddr *)&(sws.saddr));
  746. } catch ( ... ) {}
  747. }
  748. }
  749. }
  750. break;
  751. case ZT_PHY_SOCKET_UDP:
  752. if (FD_ISSET(s->sock,&rfds)) {
  753. for(;;) {
  754. memset(&ss,0,sizeof(ss));
  755. socklen_t slen = sizeof(ss);
  756. long n = (long)::recvfrom(s->sock,buf,sizeof(buf),0,(struct sockaddr *)&ss,&slen);
  757. if (n > 0) {
  758. try {
  759. _handler->phyOnDatagram((PhySocket *)&(*s),&(s->uptr),(const struct sockaddr *)&ss,(void *)buf,(unsigned long)n);
  760. } catch ( ... ) {}
  761. } else if (n < 0)
  762. break;
  763. }
  764. }
  765. break;
  766. case ZT_PHY_SOCKET_UNIX_IN: {
  767. #ifdef __UNIX_LIKE__
  768. ZT_PHY_SOCKFD_TYPE sock = s->sock; // if closed, s->sock becomes invalid as s is no longer dereferencable
  769. if (FD_ISSET(sock,&rfds)) {
  770. long n = (long)::read(sock,buf,sizeof(buf));
  771. if (n <= 0) {
  772. this->close((PhySocket *)&(*s),true);
  773. } else {
  774. try {
  775. _handler->phyOnUnixData((PhySocket *)&(*s),&(s->uptr),(void *)buf,(unsigned long)n);
  776. } catch ( ... ) {}
  777. }
  778. }
  779. if ((FD_ISSET(sock,&wfds))&&(FD_ISSET(sock,&_writefds))) {
  780. try {
  781. _handler->phyOnUnixWritable((PhySocket *)&(*s),&(s->uptr));
  782. } catch ( ... ) {}
  783. }
  784. #endif // __UNIX_LIKE__
  785. } break;
  786. case ZT_PHY_SOCKET_UNIX_LISTEN:
  787. #ifdef __UNIX_LIKE__
  788. if (FD_ISSET(s->sock,&rfds)) {
  789. memset(&ss,0,sizeof(ss));
  790. socklen_t slen = sizeof(ss);
  791. ZT_PHY_SOCKFD_TYPE newSock = ::accept(s->sock,(struct sockaddr *)&ss,&slen);
  792. if (ZT_PHY_SOCKFD_VALID(newSock)) {
  793. if (_socks.size() >= ZT_PHY_MAX_SOCKETS) {
  794. ZT_PHY_CLOSE_SOCKET(newSock);
  795. } else {
  796. fcntl(newSock,F_SETFL,O_NONBLOCK);
  797. _socks.push_back(PhySocketImpl());
  798. PhySocketImpl &sws = _socks.back();
  799. FD_SET(newSock,&_readfds);
  800. if ((long)newSock > _nfds)
  801. _nfds = (long)newSock;
  802. sws.type = ZT_PHY_SOCKET_UNIX_IN;
  803. sws.sock = newSock;
  804. sws.uptr = (void *)0;
  805. memcpy(&(sws.saddr),&ss,sizeof(struct sockaddr_storage));
  806. try {
  807. _handler->phyOnUnixAccept((PhySocket *)&(*s),(PhySocket *)&(_socks.back()),&(s->uptr),&(sws.uptr));
  808. } catch ( ... ) {}
  809. }
  810. }
  811. }
  812. #endif // __UNIX_LIKE__
  813. break;
  814. default:
  815. break;
  816. }
  817. if (s->type == ZT_PHY_SOCKET_CLOSED)
  818. _socks.erase(s++);
  819. else ++s;
  820. }
  821. }
  822. /**
  823. * @param sock Socket to close
  824. * @param callHandlers If true, call handlers for TCP connect (success: false) or close (default: true)
  825. */
  826. inline void close(PhySocket *sock,bool callHandlers = true)
  827. {
  828. if (!sock)
  829. return;
  830. PhySocketImpl &sws = *(reinterpret_cast<PhySocketImpl *>(sock));
  831. if (sws.type == ZT_PHY_SOCKET_CLOSED)
  832. return;
  833. FD_CLR(sws.sock,&_readfds);
  834. FD_CLR(sws.sock,&_writefds);
  835. #if defined(_WIN32) || defined(_WIN64)
  836. FD_CLR(sws.sock,&_exceptfds);
  837. #endif
  838. ZT_PHY_CLOSE_SOCKET(sws.sock);
  839. if (callHandlers) {
  840. switch(sws.type) {
  841. case ZT_PHY_SOCKET_TCP_OUT_PENDING:
  842. try {
  843. _handler->phyOnTcpConnect(sock,&(sws.uptr),false);
  844. } catch ( ... ) {}
  845. break;
  846. case ZT_PHY_SOCKET_TCP_OUT_CONNECTED:
  847. case ZT_PHY_SOCKET_TCP_IN:
  848. try {
  849. _handler->phyOnTcpClose(sock,&(sws.uptr));
  850. } catch ( ... ) {}
  851. break;
  852. case ZT_PHY_SOCKET_UNIX_IN:
  853. #ifdef __UNIX_LIKE__
  854. try {
  855. _handler->phyOnUnixClose(sock,&(sws.uptr));
  856. } catch ( ... ) {}
  857. #endif // __UNIX_LIKE__
  858. break;
  859. default:
  860. break;
  861. }
  862. }
  863. // Causes entry to be deleted from list in poll(), ignored elsewhere
  864. sws.type = ZT_PHY_SOCKET_CLOSED;
  865. if ((long)sws.sock >= (long)_nfds) {
  866. long nfds = (long)_whackSendSocket;
  867. if ((long)_whackReceiveSocket > nfds)
  868. nfds = (long)_whackReceiveSocket;
  869. for(typename std::list<PhySocketImpl>::iterator s(_socks.begin());s!=_socks.end();++s) {
  870. if ((s->type != ZT_PHY_SOCKET_CLOSED)&&((long)s->sock > nfds))
  871. nfds = (long)s->sock;
  872. }
  873. _nfds = nfds;
  874. }
  875. }
  876. };
  877. } // namespace ZeroTier
  878. #endif