Phy.hpp 33 KB

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