2
0

Demarc.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. * Its name "demarcation point" comes from the telco/cable terminology for
  49. * the box where wires terminate at a customer's property.
  50. */
  51. class Demarc
  52. {
  53. public:
  54. /**
  55. * Local demarcation port
  56. */
  57. typedef uint64_t Port;
  58. /**
  59. * Port identifier used to refer to any port
  60. */
  61. static const Port ANY_PORT = (Port)0xffffffffffffffffULL;
  62. /**
  63. * Port identifier used to refer to null port / port not found
  64. */
  65. static const Port NULL_PORT = (Port)0;
  66. Demarc(const RuntimeEnvironment *renv);
  67. ~Demarc();
  68. /**
  69. * Describe a port
  70. *
  71. * This can describe even ports that are not bound, e.g. from serialized
  72. * data.
  73. *
  74. * @param p Port
  75. * @return Human-readable description of port
  76. */
  77. static std::string describe(Port p)
  78. throw();
  79. /**
  80. * @param p Port to check
  81. * @return True if this port is bound/connected/etc.
  82. */
  83. bool has(Port p) const
  84. throw();
  85. /**
  86. * Bind local UDP port for both IPv4 and IPv6 traffic
  87. *
  88. * @param localPort Local IP port
  89. * @return True if successfully bound, or if already bound
  90. */
  91. bool bindLocalUdp(unsigned int localPort)
  92. throw();
  93. /**
  94. * Pick a port to send to an address of a given type
  95. *
  96. * @param to Destination address
  97. * @return Port or NULL_PORT if none
  98. */
  99. Port pick(const InetAddress &to) const
  100. throw();
  101. /**
  102. * Send a packet
  103. *
  104. * If fromPort is ANY_PORT or if the port is not found, a random port is
  105. * chosen from those available matching the characteristics of the address
  106. * in 'to'.
  107. *
  108. * @param fromPort Port to send from
  109. * @param to Destination IP/port
  110. * @param data Data to send
  111. * @param len Length of data in bytes
  112. * @param hopLimit IP hop limit for UDP packets or -1 for max/unlimited
  113. * @return Port actually sent from or NULL_PORT on failure
  114. */
  115. Port send(Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
  116. throw();
  117. /**
  118. * @param p Port
  119. * @return 64-bit integer suitable for serialization
  120. */
  121. static inline uint64_t portToInt(const Port p) throw() { return (uint64_t)p; }
  122. /**
  123. * @param p 64-bit integer from serialized representation
  124. * @return Port suitable for use in code
  125. */
  126. static inline Port intToPort(const uint64_t p) throw() { return (Port)p; }
  127. private:
  128. const RuntimeEnvironment *_r;
  129. static void _CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len);
  130. enum DemarcPortType
  131. {
  132. PORT_TYPE_UDP_SOCKET_V4 = 1,
  133. PORT_TYPE_UDP_SOCKET_V6 = 2,
  134. PORT_TYPE_LOCAL_ETHERNET = 3,
  135. PORT_TYPE_RELAY_TUNNEL = 4
  136. };
  137. // Variant holding instances of UdpSocket, etc.
  138. struct DemarcPortObj
  139. {
  140. Demarc::Port port;
  141. Demarc *parent;
  142. void *obj;
  143. DemarcPortType type;
  144. };
  145. std::map< Port,DemarcPortObj > _ports;
  146. Mutex _ports_m;
  147. };
  148. } // namespace ZeroTier
  149. #endif