UdpSocket.hpp 2.9 KB

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