Phy.hpp 33 KB

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