Node.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. namespace ZeroTier {
  39. class RuntimeEnvironment;
  40. /**
  41. * Implementation of Node object as defined in CAPI
  42. *
  43. * The pointer returned by ZT1_Node_new() is an instance of this class.
  44. */
  45. class Node
  46. {
  47. public:
  48. Node(
  49. ZT1_DataStoreGetFunction *dataStoreGetFunction,
  50. ZT1_DataStorePutFunction *dataStorePutFunction,
  51. ZT1_VirtualNetworkConfigCallback *networkConfigCallback,
  52. ZT1_StatusCallback *statusCallback);
  53. ~Node();
  54. // Public API Functions ----------------------------------------------------
  55. ZT1_ResultCode run(
  56. uint64_t now,
  57. const ZT1_WireMessage *inputWireMessages,
  58. unsigned int inputWireMessageCount,
  59. const ZT1_VirtualLanFrame *inputLanFrames,
  60. unsigned int inputLanFrameCount,
  61. const ZT1_WireMessage **outputWireMessages,
  62. unsigned int *outputWireMessageCount,
  63. const ZT1_VirtualNetworkFrame **outputFrames,
  64. unsigned int *outputLanFrameCount,
  65. unsigned long *maxNextInterval);
  66. ZT1_ResultCode join(uint64_t nwid);
  67. ZT1_ResultCode leave(uint64_t nwid);
  68. void status(ZT1_NodeStatus *status);
  69. ZT1_PeerList *peers();
  70. ZT1_VirtualNetworkConfig *networkConfig(uint64_t nwid);
  71. ZT1_VirtualNetworkList *listNetworks();
  72. void freeQueryResult(void *qr);
  73. ZT1_ResultCode setNetconfMaster(
  74. ZT1_Node *node,
  75. void *networkConfigMasterInstance);
  76. // Internal functions ------------------------------------------------------
  77. /**
  78. * @return Time as of last call to run()
  79. */
  80. inline uint64_t now() const throw() { return _now; }
  81. /**
  82. * @return Current level of desperation
  83. */
  84. inline int desperation() const throw() { return (int)((_now - _timeOfLastPrivilgedPacket) / ZT_DESPERATION_INCREMENT); }
  85. /**
  86. * Enqueue a ZeroTier message to be sent
  87. *
  88. * @param addr Destination address
  89. * @param data Packet data
  90. * @param len Packet length
  91. */
  92. inline void putPacket(const InetAddress &addr,const void *data,unsigned int len)
  93. {
  94. Mutex::Lock _l(_outputWireMessages_m);
  95. if (_outputWireMessageCount >= _outputWireMessageCapacity) {
  96. ZT1_WireMessage *old = _outputWireMessages;
  97. _outputWireMessages = new ZT1_WireMessage[_outputWireMessageCapacity *= 2];
  98. memcpy(_outputWireMessages,old,sizeof(ZT1_WireMessage) * _outputWireMessageCount);
  99. delete [] old;
  100. }
  101. ZT1_WireMessage &wm = _outputWireMessages[_outputWireMessageCount++];
  102. memcpy(&(wm.address),&addr,sizeof(ZT_SOCKADDR_STORAGE));
  103. wm.desperation = this->desperation();
  104. wm.spam = (int)((++_spamCounter % ZT_DESPERATION_SPAM_EVERY) == 0);
  105. memcpy(wm.packetData,data,len);
  106. wm.packetLength = len;
  107. }
  108. /**
  109. * Enqueue a frame to be injected into a tap device (port)
  110. *
  111. * @param nwid Network ID
  112. * @param source Source MAC
  113. * @param dest Destination MAC
  114. * @param etherType 16-bit ethernet type
  115. * @param vlanId VLAN ID or 0 if none
  116. * @param data Frame data
  117. * @param len Frame length
  118. */
  119. inline void putFrame(uint64_t nwid,const MAC &source,const MAC &dest,unsigned int etherType,unsigned int vlanId,const void *data,unsigned int len)
  120. {
  121. Mutex::Lock _l(_outputFrames_m);
  122. if (_outputFrameCount >= _outputFrameCapacity) {
  123. ZT1_VirtualNetworkFrame *old = _outputFrames;
  124. _outputFrames = new ZT1_VirtualNetworkFrame[_outputFrameCapacity *= 2];
  125. memcpy(_outputFrames,old,sizeof(ZT1_VirtualNetworkFrame) * _outputFrameCount);
  126. delete [] old;
  127. }
  128. ZT1_VirtualNetworkFrame &f = _outputFrames[_outputFrameCount++];
  129. f.nwid = nwid;
  130. f.sourceMac = source.toInt();
  131. f.destMac = dest.toInt();
  132. f.etherType = etherType;
  133. f.vlanId = vlanId;
  134. memcpy(f.frameData,data,len);
  135. f.frameLength = len;
  136. }
  137. /**
  138. * @param nwid Network ID
  139. * @return Network instance
  140. */
  141. inline SharedPtr<Network> network(uint64_t nwid)
  142. {
  143. Mutex::Lock _l(_networks_m);
  144. std::map< uint64_t,Network >::iterator nw(_networks.find(nwid));
  145. return ((nw == _networks.end()) ? SharedPtr<Network>() : nw->second);
  146. }
  147. private:
  148. RuntimeEnvironment *RR;
  149. ZT1_WireMessage *_outputWireMessages;
  150. unsigned long _outputWireMessageCount;
  151. unsigned long _outputWireMessageCapacity;
  152. Mutex _outputWireMessages_m;
  153. ZT1_VirtualNetworkFrame *_outputFrames;
  154. unsigned long _outputFrameCount;
  155. unsigned long _outputFrameCapacity;
  156. Mutex _outputFrames_m;
  157. ZT1_DataStoreGetFunction *_dataStoreGetFunction,
  158. ZT1_DataStorePutFunction *_dataStorePutFunction,
  159. ZT1_VirtualPortConfigCallback *_portConfigCallback,
  160. ZT1_StatusCallback *_statusCallback);
  161. //Dictionary _localConfig; // persisted as local.conf
  162. //Mutex _localConfig_m;
  163. std::map< uint64_t,SharedPtr<Network> > _networks;
  164. Mutex _networks_m;
  165. uint64_t _now; // time of last run()
  166. uint64_t _timeOfLastPacketReceived;
  167. uint64_t _timeOfLastPrivilgedPacket;
  168. unsigned int _spamCounter; // used to "spam" every Nth packet
  169. };
  170. } // namespace ZeroTier
  171. #endif