UdpSocket.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_UDPSOCKET_HPP
  28. #define _ZT_UDPSOCKET_HPP
  29. #include <stdexcept>
  30. #include "Constants.hpp"
  31. #include "Thread.hpp"
  32. #include "InetAddress.hpp"
  33. #include "Mutex.hpp"
  34. #ifdef __WINDOWS__
  35. #include <WinSock2.h>
  36. #endif
  37. namespace ZeroTier {
  38. /**
  39. * A local UDP socket
  40. *
  41. * The socket listens in a background thread and sends packets to Switch.
  42. */
  43. class UdpSocket
  44. {
  45. public:
  46. /**
  47. * Create and bind a local UDP socket
  48. *
  49. * @param localOnly If true, bind to loopback address only
  50. * @param localPort Local port to listen to
  51. * @param ipv6 If true, bind this as an IPv6 socket, otherwise IPv4
  52. * @param packetHandler Function to call when packets are read
  53. * @param arg First argument (after self) to function
  54. * @throws std::runtime_error Unable to bind
  55. */
  56. UdpSocket(
  57. bool localOnly,
  58. int localPort,
  59. bool ipv6,
  60. void (*packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int),
  61. void *arg)
  62. throw(std::runtime_error);
  63. ~UdpSocket();
  64. /**
  65. * @return Locally bound port
  66. */
  67. inline int localPort() const throw() { return _localPort; }
  68. /**
  69. * @return True if this is an IPv6 socket
  70. */
  71. inline bool v6() const throw() { return _v6; }
  72. /**
  73. * Send a packet
  74. *
  75. * Attempt to send V6 on a V4 or V4 on a V6 socket will return false.
  76. *
  77. * @param to Destination IP/port
  78. * @param data Data to send
  79. * @param len Length of data in bytes
  80. * @param hopLimit IP hop limit for UDP packet or -1 for max (max: 255)
  81. * @return True if packet successfully sent to link layer
  82. */
  83. bool send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
  84. throw();
  85. /**
  86. * Thread main method; do not call elsewhere
  87. */
  88. void threadMain()
  89. throw();
  90. private:
  91. Thread _thread;
  92. void (*_packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int);
  93. void *_arg;
  94. int _localPort;
  95. #ifdef __WINDOWS__
  96. volatile SOCKET _sock;
  97. #else
  98. volatile int _sock;
  99. #endif
  100. bool _v6;
  101. Mutex _sendLock;
  102. };
  103. } // namespace ZeroTier
  104. #endif