Demarc.hpp 4.4 KB

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