2
0

Node.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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_NODE_HPP
  28. #define ZT_NODE_HPP
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <map>
  33. #include "Constants.hpp"
  34. #include "../include/ZeroTierOne.h"
  35. #include "InetAddress.hpp"
  36. #include "Mutex.hpp"
  37. #include "MAC.hpp"
  38. #include "Network.hpp"
  39. namespace ZeroTier {
  40. class RuntimeEnvironment;
  41. /**
  42. * Implementation of Node object as defined in CAPI
  43. *
  44. * The pointer returned by ZT1_Node_new() is an instance of this class.
  45. */
  46. class Node
  47. {
  48. public:
  49. Node(
  50. ZT1_DataStoreGetFunction *dataStoreGetFunction,
  51. ZT1_DataStorePutFunction *dataStorePutFunction,
  52. ZT1_WirePacketSendFunction *wirePacketSendFunction,
  53. ZT1_VirtualNetworkFrameFunction *virtualNetworkFrameFunction,
  54. ZT1_VirtualNetworkConfigCallback *virtualNetworkConfigCallback,
  55. ZT1_StatusCallback *statusCallback);
  56. ~Node();
  57. // Public API Functions ----------------------------------------------------
  58. ZT1_ResultCode processWirePacket(
  59. uint64_t now,
  60. const struct sockaddr_storage *remoteAddress,
  61. int linkDesperation,
  62. const void *packetData,
  63. unsigned int packetLength,
  64. uint64_t *nextCallDeadline);
  65. ZT1_ResultCode processVirtualNetworkFrame(
  66. uint64_t now,
  67. uint64_t nwid,
  68. uint64_t sourceMac,
  69. uint64_t destMac,
  70. unsigned int etherType,
  71. unsigned int vlanId,
  72. const void *frameData,
  73. unsigned int frameLength,
  74. uint64_t *nextCallDeadline);
  75. ZT1_Resultcode processNothing(uint64_t now,uint64_t *nextCallDeadline);
  76. ZT1_ResultCode join(uint64_t nwid);
  77. ZT1_ResultCode leave(uint64_t nwid);
  78. ZT1_ResultCode multicastSubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi = 0);
  79. ZT1_ResultCode multicastUnsubscribe(ZT1_Node *node,uint64_t nwid,uint64_t multicastGroup,unsigned long multicastAdi = 0);
  80. void status(ZT1_NodeStatus *status);
  81. ZT1_PeerList *peers();
  82. ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid);
  83. ZT1_VirtualNetworkList *listNetworks();
  84. void freeQueryResult(void *qr);
  85. void setNetconfMaster(void *networkConfigMasterInstance);
  86. // Internal functions ------------------------------------------------------
  87. /**
  88. * @return Time as of last call to run()
  89. */
  90. inline uint64_t now() const throw() { return _now; }
  91. /**
  92. * @return Current level of desperation
  93. */
  94. inline int desperation() const throw() { return (int)((_now - _timeOfLastPrivilgedPacket) / ZT_DESPERATION_INCREMENT); }
  95. /**
  96. * Called to update last packet receive time whenever a packet is received
  97. *
  98. * @param fromPrivilegedPeer If true, peer is a supernode or federated hub (a.k.a. an upstream link)
  99. */
  100. inline void packetReceived(bool fromPrivilegedPeer)
  101. throw()
  102. {
  103. const uint64_t n = _now;
  104. _timeOfLastPacketReceived = n;
  105. if (fromPrivilegedPeer)
  106. _timeOfLastPrivilgedPacket = n;
  107. }
  108. /**
  109. * @return Most recent time of any packet receipt
  110. */
  111. inline uint64_t timeOfLastPacketReceived() const throw() { return _timeOfLastPacketReceived; }
  112. /**
  113. * @return Timestamp of last packet received from a supernode or hub (upstream link)
  114. */
  115. inline uint64_t timeOfLastPrivilgedPacket() const throw() { return _timeOfLastPrivilgedPacket; }
  116. /**
  117. * Enqueue a ZeroTier message to be sent
  118. *
  119. * @param addr Destination address
  120. * @param data Packet data
  121. * @param len Packet length
  122. */
  123. inline void putPacket(const InetAddress &addr,const void *data,unsigned int len)
  124. {
  125. _wirePacketSendFunction(
  126. reinterpret_cast<ZT1_Node *>(this),
  127. reinterpret_cast<const struct sockaddr_storage *>(&addr),
  128. this->desperation(),
  129. (int)((++_spamCounter % ZT_DESPERATION_SPAM_EVERY) == 0),
  130. data,
  131. len);
  132. }
  133. /**
  134. * Enqueue a frame to be injected into a tap device (port)
  135. *
  136. * @param nwid Network ID
  137. * @param source Source MAC
  138. * @param dest Destination MAC
  139. * @param etherType 16-bit ethernet type
  140. * @param vlanId VLAN ID or 0 if none
  141. * @param data Frame data
  142. * @param len Frame length
  143. */
  144. inline void putFrame(uint64_t nwid,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  145. {
  146. _virtualNetworkFrameFunction(
  147. reinterpret_cast<ZT1_Node *>(this),
  148. nwid,
  149. source.toInt(),
  150. dest.toInt(),
  151. etherType,
  152. vlanId,
  153. data,
  154. len);
  155. }
  156. /**
  157. * @param nwid Network ID
  158. * @return Network instance
  159. */
  160. inline SharedPtr<Network> network(uint64_t nwid)
  161. {
  162. Mutex::Lock _l(_networks_m);
  163. std::map< uint64_t,SharedPtr<Network> >::iterator nw(_networks.find(nwid));
  164. return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
  165. }
  166. private:
  167. RuntimeEnvironment *RR;
  168. ZT1_DataStoreGetFunction *_dataStoreGetFunction;
  169. ZT1_DataStorePutFunction *_dataStorePutFunction;
  170. ZT1_WirePacketSendFunction *_wirePacketSendFunction;
  171. ZT1_VirtualNetworkFrameFunction *_virtualNetworkFrameFunction;
  172. ZT1_VirtualNetworkConfigCallback *_virtualNetworkConfigCallback;
  173. ZT1_StatusCallback *_statusCallback;
  174. //Dictionary _localConfig; // persisted as local.conf
  175. //Mutex _localConfig_m;
  176. std::map< uint64_t,SharedPtr<Network> > _networks;
  177. Mutex _networks_m;
  178. volatile uint64_t _now; // time of last run()
  179. volatile uint64_t _timeOfLastPacketReceived;
  180. volatile _timeOfLastPrivilgedPacket;
  181. volatile unsigned int _spamCounter; // used to "spam" every Nth packet
  182. };
  183. } // namespace ZeroTier
  184. #endif