UdpSocket.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. namespace ZeroTier {
  35. /**
  36. * A local UDP socket
  37. *
  38. * The socket listens in a background thread and sends packets to Switch.
  39. */
  40. class UdpSocket
  41. {
  42. public:
  43. /**
  44. * Create and bind a local UDP socket
  45. *
  46. * @param localOnly If true, bind to loopback address only
  47. * @param localPort Local port to listen to
  48. * @param ipv6 If true, bind this as an IPv6 socket, otherwise IPv4
  49. * @param packetHandler Function to call when packets are read
  50. * @param arg First argument (after self) to function
  51. * @throws std::runtime_error Unable to bind
  52. */
  53. UdpSocket(
  54. bool localOnly,
  55. int localPort,
  56. bool ipv6,
  57. void (*packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int),
  58. void *arg)
  59. throw(std::runtime_error);
  60. ~UdpSocket();
  61. /**
  62. * @return Locally bound port
  63. */
  64. inline int localPort() const throw() { return _localPort; }
  65. /**
  66. * @return True if this is an IPv6 socket
  67. */
  68. inline bool v6() const throw() { return _v6; }
  69. /**
  70. * Send a packet
  71. *
  72. * Attempt to send V6 on a V4 or V4 on a V6 socket will return false.
  73. *
  74. * @param to Destination IP/port
  75. * @param data Data to send
  76. * @param len Length of data in bytes
  77. * @param hopLimit IP hop limit for UDP packet or -1 for max (max: 255)
  78. * @return True if packet successfully sent to link layer
  79. */
  80. bool send(const InetAddress &to,const void *data,unsigned int len,int hopLimit)
  81. throw();
  82. /**
  83. * Thread main method; do not call elsewhere
  84. */
  85. void threadMain()
  86. throw();
  87. private:
  88. Thread _thread;
  89. void (*_packetHandler)(UdpSocket *,void *,const InetAddress &,const void *,unsigned int);
  90. void *_arg;
  91. int _localPort;
  92. volatile int _sock;
  93. bool _v6;
  94. Mutex _sendLock;
  95. };
  96. } // namespace ZeroTier
  97. #endif