UdpSocket.hpp 2.8 KB

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