SoftwareUpdater.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  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. #ifndef ZT_SOFTWAREUPDATER_HPP
  19. #define ZT_SOFTWAREUPDATER_HPP
  20. #include <stdint.h>
  21. #include <vector>
  22. #include <map>
  23. #include <string>
  24. #include "../include/ZeroTierOne.h"
  25. #include "../node/Identity.hpp"
  26. #include "../node/Array.hpp"
  27. #include "../node/Packet.hpp"
  28. #include "../ext/json/json.hpp"
  29. /**
  30. * VERB_USER_MESSAGE type ID for software update messages
  31. */
  32. #define ZT_SOFTWARE_UPDATE_USER_MESSAGE_TYPE 100
  33. /**
  34. * ZeroTier address of node that provides software updates
  35. */
  36. #define ZT_SOFTWARE_UPDATE_SERVICE 0xb1d366e81fULL
  37. /**
  38. * ZeroTier identity that must be used to sign software updates
  39. */
  40. #define ZT_SOFTWARE_UPDATE_SIGNING_AUTHORITY "a60ed7a9eb:0:740fd482e02eabd28f69c83ad4353d44c9514627bc0656b5d194e2f34715745b62297120d47d7f0c728424c33297ea499dea4ce006ee3d7c0d802a6fb199d909"
  41. /**
  42. * Chunk size for in-band downloads (can be changed, designed to always fit in one UDP packet easily)
  43. */
  44. #define ZT_SOFTWARE_UPDATE_CHUNK_SIZE (ZT_PROTO_MAX_PACKET_LENGTH - 128)
  45. /**
  46. * Sanity limit for the size of an update binary image
  47. */
  48. #define ZT_SOFTWARE_UPDATE_MAX_SIZE (1024 * 1024 * 256)
  49. /**
  50. * How often (ms) do we check?
  51. */
  52. //#define ZT_SOFTWARE_UPDATE_CHECK_PERIOD (60 * 60 * 1000)
  53. #define ZT_SOFTWARE_UPDATE_CHECK_PERIOD 5000
  54. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR "versionMajor"
  55. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR "versionMinor"
  56. #define ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION "versionRev"
  57. #define ZT_SOFTWARE_UPDATE_JSON_EXPECT_SIGNED_BY "expectedSigner"
  58. #define ZT_SOFTWARE_UPDATE_JSON_PLATFORM "platform"
  59. #define ZT_SOFTWARE_UPDATE_JSON_ARCHITECTURE "arch"
  60. #define ZT_SOFTWARE_UPDATE_JSON_VENDOR "vendor"
  61. #define ZT_SOFTWARE_UPDATE_JSON_CHANNEL "channel"
  62. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNED_BY "updateSigner"
  63. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIGNATURE "updateSig"
  64. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_HASH "updateHash"
  65. #define ZT_SOFTWARE_UPDATE_JSON_UPDATE_SIZE "updateSize"
  66. #define ZT_SOFTWARE_UPDATE_JSON_EXEC_ARGS "updateExecArgs"
  67. namespace ZeroTier {
  68. class Node;
  69. /**
  70. * This class handles retrieving and executing updates, or serving them
  71. */
  72. class SoftwareUpdater
  73. {
  74. public:
  75. /**
  76. * Each message begins with an 8-bit message verb
  77. */
  78. enum MessageVerb
  79. {
  80. /**
  81. * Payload: JSON containing current system platform, version, etc.
  82. */
  83. VERB_GET_LATEST = 1,
  84. /**
  85. * Payload: JSON describing latest update for this target. (No response is sent if there is none.)
  86. */
  87. VERB_LATEST = 2,
  88. /**
  89. * Payload:
  90. * <[16] first 128 bits of hash of data object>
  91. * <[4] 32-bit index of chunk to get>
  92. */
  93. VERB_GET_DATA = 3,
  94. /**
  95. * Payload:
  96. * <[16] first 128 bits of hash of data object>
  97. * <[4] 32-bit index of chunk>
  98. * <[...] chunk data>
  99. */
  100. VERB_DATA = 4
  101. };
  102. SoftwareUpdater(Node &node,const std::string &homePath);
  103. ~SoftwareUpdater();
  104. /**
  105. * Load update-dist.d if it exists
  106. */
  107. void loadUpdatesToDistribute();
  108. /**
  109. * Handle a software update user message
  110. *
  111. * @param origin ZeroTier address of message origin
  112. * @param data Message payload
  113. * @param len Length of message
  114. */
  115. void handleSoftwareUpdateUserMessage(uint64_t origin,const void *data,unsigned int len);
  116. /**
  117. * Check for updates and do other update-related housekeeping
  118. *
  119. * It should be called about every 10 seconds.
  120. *
  121. * @return Null JSON object or update information if there is an update downloaded and ready
  122. */
  123. nlohmann::json check(const uint64_t now);
  124. /**
  125. * Apply any ready update now
  126. *
  127. * Depending on the platform this function may never return and may forcibly
  128. * exit the process. It does nothing if no update is ready.
  129. */
  130. void apply();
  131. private:
  132. Node &_node;
  133. uint64_t _lastCheckTime;
  134. std::string _homePath;
  135. // Offered software updates if we are an update host (we have update-dist.d and update hosting is enabled)
  136. struct _D
  137. {
  138. nlohmann::json meta;
  139. std::string bin;
  140. };
  141. std::map< Array<uint8_t,16>,_D > _dist; // key is first 16 bytes of hash
  142. nlohmann::json _latestMeta;
  143. std::string _latestBin;
  144. Array<uint8_t,16> _latestBinHashPrefix;
  145. unsigned long _latestBinLength;
  146. bool _latestBinValid;
  147. };
  148. } // namespace ZeroTier
  149. #endif