SoftwareUpdater.hpp 4.5 KB

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