RuntimeEnvironment.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_RUNTIMEENVIRONMENT_HPP
  28. #define ZT_RUNTIMEENVIRONMENT_HPP
  29. #include <string>
  30. #include "Constants.hpp"
  31. #include "Identity.hpp"
  32. namespace ZeroTier {
  33. class NodeConfig;
  34. class Logger;
  35. class Switch;
  36. class Topology;
  37. class CMWC4096;
  38. class Service;
  39. class Node;
  40. class SoftwareUpdater;
  41. class SocketManager;
  42. class Multicaster;
  43. class AntiRecursion;
  44. class EthernetTapFactory;
  45. class RoutingTable;
  46. class HttpClient;
  47. /**
  48. * Holds global state for an instance of ZeroTier::Node
  49. *
  50. * I do not believe in mutable static variables, period, or in global static
  51. * instances of objects that don't basically represent constants. It makes
  52. * unit testing, embedding, threading, and other things hard and is poor
  53. * practice.
  54. *
  55. * So we put everything that we would want to be global, like Logger, here
  56. * and we give everybody this as _r. The Node creates and initializes this
  57. * on startup and deletes things on shutdown.
  58. */
  59. class RuntimeEnvironment
  60. {
  61. public:
  62. RuntimeEnvironment() :
  63. homePath(),
  64. identity(),
  65. initialized(false),
  66. shutdownInProgress(false),
  67. tcpTunnelingEnabled(false),
  68. timeOfLastResynchronize(0),
  69. timeOfLastPacketReceived(0),
  70. tapFactory((EthernetTapFactory *)0),
  71. routingTable((RoutingTable *)0),
  72. sm((SocketManager *)0),
  73. log((Logger *)0),
  74. prng((CMWC4096 *)0),
  75. http((HttpClient *)0),
  76. antiRec((AntiRecursion *)0),
  77. mc((Multicaster *)0),
  78. sw((Switch *)0),
  79. topology((Topology *)0),
  80. nc((NodeConfig *)0),
  81. node((Node *)0),
  82. updater((SoftwareUpdater *)0)
  83. #ifndef __WINDOWS__
  84. ,netconfService((Service *)0)
  85. #endif
  86. {
  87. }
  88. // Full path to home folder
  89. std::string homePath;
  90. // This node's identity
  91. Identity identity;
  92. // Are we initialized?
  93. volatile bool initialized;
  94. // Indicates that we are shutting down -- this is hacky, want to factor out
  95. volatile bool shutdownInProgress;
  96. // Are we in outgoing TCP failover mode?
  97. volatile bool tcpTunnelingEnabled;
  98. // Time network environment (e.g. fingerprint) last changed -- used to determine online-ness
  99. volatile uint64_t timeOfLastResynchronize;
  100. // Time last packet was received -- from anywhere. This is updated in Peer::receive()
  101. // via an ugly const_cast<>.
  102. volatile uint64_t timeOfLastPacketReceived;
  103. // These are passed in from outside and are not created or deleted by the ZeroTier node core
  104. EthernetTapFactory *tapFactory;
  105. RoutingTable *routingTable;
  106. SocketManager *sm;
  107. /*
  108. * Order matters a bit here. These are constructed in this order
  109. * and then deleted in the opposite order on Node exit. The order ensures
  110. * that things that are needed are there before they're needed.
  111. *
  112. * These are constant and never null after startup unless indicated.
  113. */
  114. Logger *log; // null if logging is disabled
  115. CMWC4096 *prng;
  116. HttpClient *http;
  117. AntiRecursion *antiRec;
  118. Multicaster *mc;
  119. Switch *sw;
  120. Topology *topology;
  121. NodeConfig *nc;
  122. Node *node;
  123. SoftwareUpdater *updater; // null if software updates are not enabled
  124. #ifndef __WINDOWS__
  125. Service *netconfService; // null if no netconf service running
  126. #endif
  127. };
  128. } // namespace ZeroTier
  129. #endif