NativeSocketManager.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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_NATIVESOCKETMANAGER_HPP
  28. #define ZT_NATIVESOCKETMANAGER_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <map>
  32. #include <stdexcept>
  33. #include "../node/Constants.hpp"
  34. #include "../node/SharedPtr.hpp"
  35. #include "../node/Mutex.hpp"
  36. #include "../node/SocketManager.hpp"
  37. #include "../node/Socket.hpp"
  38. #ifdef __WINDOWS__
  39. #include <WinSock2.h>
  40. #include <WS2tcpip.h>
  41. #include <Windows.h>
  42. #else
  43. #include <unistd.h>
  44. #include <sys/time.h>
  45. #include <sys/types.h>
  46. #include <sys/select.h>
  47. #endif
  48. namespace ZeroTier {
  49. class NativeSocket;
  50. class NativeUdpSocket;
  51. class NativeTcpSocket;
  52. /**
  53. * Native socket manager for Unix and Windows
  54. */
  55. class NativeSocketManager : public SocketManager
  56. {
  57. friend class NativeUdpSocket;
  58. friend class NativeTcpSocket;
  59. public:
  60. NativeSocketManager(int localUdpPort,int localTcpPort);
  61. virtual ~NativeSocketManager();
  62. virtual bool send(const InetAddress &to,bool tcp,bool autoConnectTcp,const void *msg,unsigned int msglen);
  63. virtual void poll(unsigned long timeout,void (*handler)(const SharedPtr<Socket> &,void *,const InetAddress &,Buffer<ZT_SOCKET_MAX_MESSAGE_LEN> &),void *arg);
  64. virtual void whack();
  65. virtual void closeTcpSockets();
  66. private:
  67. // Used by TcpSocket to register/unregister for write availability notification
  68. void _startNotifyWrite(const NativeSocket *sock);
  69. void _stopNotifyWrite(const NativeSocket *sock);
  70. // Called in SocketManager destructor or in constructor cleanup before exception throwing
  71. void _closeSockets();
  72. // Called in SocketManager to recompute _nfds for select() based implementation
  73. void _updateNfds();
  74. #ifdef __WINDOWS__
  75. SOCKET _whackSendPipe;
  76. SOCKET _whackReceivePipe;
  77. SOCKET _tcpV4ListenSocket;
  78. SOCKET _tcpV6ListenSocket;
  79. #else
  80. int _whackSendPipe;
  81. int _whackReceivePipe;
  82. int _tcpV4ListenSocket;
  83. int _tcpV6ListenSocket;
  84. #endif
  85. Mutex _whackSendPipe_m;
  86. SharedPtr<Socket> _udpV4Socket;
  87. SharedPtr<Socket> _udpV6Socket;
  88. fd_set _readfds;
  89. fd_set _writefds;
  90. volatile int _nfds;
  91. Mutex _fdSetLock;
  92. std::map< InetAddress,SharedPtr<Socket> > _tcpSockets;
  93. Mutex _tcpSockets_m;
  94. Mutex _pollLock;
  95. };
  96. } // namespace ZeroTier
  97. #endif