Demarc.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_DEMARC_HPP
  28. #define _ZT_DEMARC_HPP
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <map>
  32. #include <string>
  33. #include "Mutex.hpp"
  34. #include "InetAddress.hpp"
  35. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. class UdpSocket;
  38. /**
  39. * Local demarcation point
  40. *
  41. * This holds and provides unique identifiers for all local communication
  42. * endpoints, such as UDP sockets, raw Ethernet sockets, tunnels to a relay
  43. * server, etc. It permits other code to refer to these via Port and forget
  44. * about what they actually are.
  45. *
  46. * All ports are closed when this class is destroyed.
  47. */
  48. class Demarc
  49. {
  50. public:
  51. /**
  52. * Local demarcation port
  53. */
  54. typedef uint64_t Port;
  55. /**
  56. * Port identifier used to refer to any port
  57. */
  58. static const Port ANY_PORT = (Port)0xffffffffffffffffULL;
  59. /**
  60. * Port identifier used to refer to null port / port not found
  61. */
  62. static const Port NULL_PORT = (Port)0;
  63. Demarc(const RuntimeEnvironment *renv);
  64. ~Demarc();
  65. /**
  66. * Describe a port
  67. *
  68. * This can describe even ports that are not bound, e.g. from serialized
  69. * data.
  70. *
  71. * @param p Port
  72. * @return Human-readable description of port
  73. */
  74. static std::string describe(Port p)
  75. throw();
  76. /**
  77. * @param p Port to check
  78. * @return True if this port is bound/connected/etc.
  79. */
  80. bool has(Port p) const
  81. throw();
  82. /**
  83. * Bind local UDP port for both IPv4 and IPv6 traffic
  84. *
  85. * @param localPort Local IP port
  86. * @return True if successfully bound, or if already bound
  87. */
  88. bool bindLocalUdp(unsigned int localPort)
  89. throw();
  90. /**
  91. * Pick a port to send to an address of a given type
  92. *
  93. * @param to Destination address
  94. * @return Port or NULL_PORT if none
  95. */
  96. Port pick(const InetAddress &to) const
  97. throw();
  98. /**
  99. * Send a packet
  100. *
  101. * If fromPort is ANY_PORT or if the port is not found, a random port is
  102. * chosen from those available matching the characteristics of the address
  103. * in 'to'.
  104. *
  105. * @param fromPort Port to send from
  106. * @param to Destination IP/port
  107. * @param data Data to send
  108. * @param len Length of data in bytes
  109. * @param hopLimit IP hop limit for UDP packets or -1 for max/unlimited
  110. * @return Port actually sent from or NULL_PORT on failure
  111. */
  112. Port send(Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
  113. throw();
  114. /**
  115. * @param p Port
  116. * @return 64-bit integer suitable for serialization
  117. */
  118. static inline uint64_t portToInt(const Port p) throw() { return (uint64_t)p; }
  119. /**
  120. * @param p 64-bit integer from serialized representation
  121. * @return Port suitable for use in code
  122. */
  123. static inline Port intToPort(const uint64_t p) throw() { return (Port)p; }
  124. private:
  125. const RuntimeEnvironment *_r;
  126. static void _CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len);
  127. enum DemarcPortType
  128. {
  129. PORT_TYPE_UDP_SOCKET_V4 = 1,
  130. PORT_TYPE_UDP_SOCKET_V6 = 2,
  131. PORT_TYPE_LOCAL_ETHERNET = 3,
  132. PORT_TYPE_RELAY_TUNNEL = 4
  133. };
  134. // Variant holding instances of UdpSocket, etc.
  135. struct DemarcPortObj
  136. {
  137. Demarc::Port port;
  138. Demarc *parent;
  139. void *obj;
  140. DemarcPortType type;
  141. };
  142. std::map< Port,DemarcPortObj > _ports;
  143. Mutex _ports_m;
  144. };
  145. } // namespace ZeroTier
  146. #endif