Demarc.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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;
  62. /**
  63. * Port identifier used to refer to null port / port not found
  64. */
  65. static const Port NULL_PORT;
  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. /**
  79. * @param p Port to check
  80. * @return True if this port is bound/connected/etc.
  81. */
  82. bool has(Port p) const
  83. throw();
  84. /**
  85. * Bind local UDP port for both IPv4 and IPv6 traffic
  86. *
  87. * @param localPort Local IP port
  88. * @return True if successfully bound, or if already bound
  89. */
  90. bool bindLocalUdp(unsigned int localPort)
  91. throw();
  92. /**
  93. * Pick a port to send to an address of a given type
  94. *
  95. * @param to Destination address
  96. * @return Port or NULL_PORT if none
  97. */
  98. Port pick(const InetAddress &to) const
  99. throw();
  100. /**
  101. * Send a packet
  102. *
  103. * If fromPort is ANY_PORT or if the port is not found, a random port is
  104. * chosen from those available matching the characteristics of the address
  105. * in 'to'.
  106. *
  107. * @param fromPort Port to send from
  108. * @param to Destination IP/port
  109. * @param data Data to send
  110. * @param len Length of data in bytes
  111. * @param hopLimit IP hop limit for UDP packets or -1 for max/unlimited
  112. * @return Port actually sent from or NULL_PORT on failure
  113. */
  114. Port send(Port fromPort,const InetAddress &to,const void *data,unsigned int len,int hopLimit) const
  115. throw();
  116. /**
  117. * @param p Port
  118. * @return 64-bit integer suitable for serialization
  119. */
  120. static inline uint64_t portToInt(const Port p) throw() { return (uint64_t)p; }
  121. /**
  122. * @param p 64-bit integer from serialized representation
  123. * @return Port suitable for use in code
  124. */
  125. static inline Port intToPort(const uint64_t p) throw() { return (Port)p; }
  126. private:
  127. const RuntimeEnvironment *_r;
  128. static void _CBudpSocketPacketHandler(UdpSocket *sock,void *arg,const InetAddress &from,const void *data,unsigned int len);
  129. enum DemarcPortType
  130. {
  131. PORT_TYPE_UDP_SOCKET_V4 = 1,
  132. PORT_TYPE_UDP_SOCKET_V6 = 2,
  133. PORT_TYPE_LOCAL_ETHERNET = 3,
  134. PORT_TYPE_RELAY_TUNNEL = 4
  135. };
  136. // Variant holding instances of UdpSocket, etc.
  137. struct DemarcPortObj
  138. {
  139. Demarc::Port port;
  140. Demarc *parent;
  141. void *obj;
  142. DemarcPortType type;
  143. };
  144. std::map< Port,DemarcPortObj > _ports;
  145. Mutex _ports_m;
  146. };
  147. } // namespace ZeroTier
  148. #endif