Updater.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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_UPDATER_HPP
  28. #define _ZT_UPDATER_HPP
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <map>
  33. #include <vector>
  34. #include <algorithm>
  35. #include <iterator>
  36. #include <stdexcept>
  37. #include <string>
  38. #include <list>
  39. #include "Constants.hpp"
  40. #include "Packet.hpp"
  41. #include "Mutex.hpp"
  42. #include "Address.hpp"
  43. #include "C25519.hpp"
  44. #include "Array.hpp"
  45. #include "Dictionary.hpp"
  46. // Chunk size-- this can be changed, picked to always fit in one packet each.
  47. #define ZT_UPDATER_CHUNK_SIZE 1350
  48. // Sanity check value for constraining max size since right now we buffer
  49. // in RAM.
  50. #define ZT_UPDATER_MAX_SUPPORTED_SIZE (1024 * 1024 * 16)
  51. // Retry timeout in ms.
  52. #define ZT_UPDATER_RETRY_TIMEOUT 15000
  53. // After this long, look for a new peer to download from
  54. #define ZT_UPDATER_PEER_TIMEOUT 65000
  55. namespace ZeroTier {
  56. class RuntimeEnvironment;
  57. /**
  58. * Software update downloader and executer
  59. *
  60. * Downloads occur via the ZT1 protocol rather than out of band via http so
  61. * that ZeroTier One can be run in secure jailed environments where it is the
  62. * only protocol permitted over the "real" Internet. This is wanted for a
  63. * number of potentially popular use cases, like private LANs that connect
  64. * nodes in hostile environments or playing attack/defend on the future CTF
  65. * network.
  66. *
  67. * The protocol is a simple chunk-pulling "trivial FTP" like thing that should
  68. * be suitable for core engine software updates. Software updates themselves
  69. * are platform-specific executables that ZeroTier One then exits and runs.
  70. *
  71. * Updaters are cached one-deep and can be replicated peer to peer in addition
  72. * to coming from supernodes. This makes it just a little bit BitTorrent-like
  73. * and helps things scale, and is also ready for further protocol
  74. * decentralization that may occur in the future.
  75. */
  76. class Updater
  77. {
  78. public:
  79. /**
  80. * Contains information about a shared update available to other peers
  81. */
  82. struct SharedUpdate
  83. {
  84. std::string fullPath;
  85. std::string filename;
  86. unsigned char sha512[64];
  87. C25519::Signature sig;
  88. Address signedBy;
  89. unsigned long size;
  90. };
  91. Updater(const RuntimeEnvironment *renv);
  92. ~Updater();
  93. /**
  94. * Rescan home path for shareable updates
  95. *
  96. * This happens automatically on construction.
  97. */
  98. void refreshShared();
  99. /**
  100. * Attempt to find an update if this version is newer than ours
  101. *
  102. * This is called whenever a peer notifies us of its version. It does nothing
  103. * if that version is not newer, otherwise it looks around for an update.
  104. *
  105. * @param vMajor Major version
  106. * @param vMinor Minor version
  107. * @param revision Revision
  108. */
  109. void getUpdateIfThisIsNewer(unsigned int vMajor,unsigned int vMinor,unsigned int revision);
  110. /**
  111. * Called periodically from main loop
  112. *
  113. * This retries downloads if they're stalled and performs other cleanup.
  114. */
  115. void retryIfNeeded();
  116. /**
  117. * Called when a chunk is received
  118. *
  119. * If the chunk is a final chunk and we now have an update, this may result
  120. * in the commencement of the update process and the shutdown of ZT1.
  121. *
  122. * @param from Originating peer
  123. * @param sha512 Up to 64 bytes of hash to match
  124. * @param shalen Length of sha512[]
  125. * @param at Position of chunk
  126. * @param chunk Chunk data
  127. * @param len Length of chunk
  128. */
  129. void handleChunk(const Address &from,const void *sha512,unsigned int shalen,unsigned long at,const void *chunk,unsigned long len);
  130. /**
  131. * Called when a reply to a search for an update is received
  132. *
  133. * This checks SHA-512 hash signature and version as parsed from filename
  134. * before starting the transfer.
  135. *
  136. * @param from Node that sent reply saying it has the file
  137. * @param filename Name of file (can be parsed for version info)
  138. * @param sha512 64-byte SHA-512 hash of file's contents
  139. * @param filesize Size of file in bytes
  140. * @param signedBy Address of signer of filename+hash
  141. * @param signature Signature (currently must be Ed25519)
  142. * @param siglen Length of signature in bytes
  143. */
  144. void handlePeerHasFile(const Address &from,const char *filename,const void *sha512,unsigned long filesize,const Address &signedBy,const void *signature,unsigned int siglen);
  145. /**
  146. * Get data about a shared update if found
  147. *
  148. * @param filename File name
  149. * @param update Empty structure to be filled with update info
  150. * @return True if found (if false, 'update' is unmodified)
  151. */
  152. bool findSharedUpdate(const char *filename,SharedUpdate &update) const;
  153. /**
  154. * Get data about a shared update if found
  155. *
  156. * @param sha512 Up to 64 bytes of hash to match
  157. * @param shalen Length of sha512[]
  158. * @param update Empty structure to be filled with update info
  159. * @return True if found (if false, 'update' is unmodified)
  160. */
  161. bool findSharedUpdate(const void *sha512,unsigned int shalen,SharedUpdate &update) const;
  162. /**
  163. * Get a chunk of a shared update
  164. *
  165. * @param sha512 Up to 64 bytes of hash to match
  166. * @param shalen Length of sha512[]
  167. * @param at Position in file
  168. * @param chunk Buffer to store data
  169. * @param chunklen Number of bytes to get
  170. * @return True if chunk[] was successfully filled, false if not found or other error
  171. */
  172. bool getSharedChunk(const void *sha512,unsigned int shalen,unsigned long at,void *chunk,unsigned long chunklen) const;
  173. /**
  174. * @return Canonical update filename for this platform or empty string if unsupported
  175. */
  176. static std::string generateUpdateFilename(unsigned int vMajor,unsigned int vMinor,unsigned int revision);
  177. /**
  178. * Parse an updater filename and extract version info
  179. *
  180. * @param filename Filename to parse
  181. * @return True if info was extracted and value-result parameters set
  182. */
  183. static bool parseUpdateFilename(const char *filename,unsigned int &vMajor,unsigned int &vMinor,unsigned int &revision);
  184. /**
  185. * Compare major, minor, and revision components of a version
  186. *
  187. * @return True if the first set is greater than the second
  188. */
  189. static inline bool compareVersions(unsigned int vmaj1,unsigned int vmin1,unsigned int rev1,unsigned int vmaj2,unsigned int vmin2,unsigned int rev2)
  190. throw()
  191. {
  192. return ( ((((uint64_t)(vmaj1 & 0xffff)) << 32) | (((uint64_t)(vmin1 & 0xffff)) << 16) | (((uint64_t)(rev1 & 0xffff)))) > ((((uint64_t)(vmaj2 & 0xffff)) << 32) | (((uint64_t)(vmin2 & 0xffff)) << 16) | (((uint64_t)(rev2 & 0xffff)))) );
  193. }
  194. private:
  195. void _requestNextChunk();
  196. struct _Download
  197. {
  198. std::string data;
  199. std::vector<bool> haveChunks;
  200. std::list<Address> peersThatHave; // excluding current
  201. std::string filename;
  202. unsigned char sha512[64];
  203. Address currentlyReceivingFrom;
  204. uint64_t lastChunkReceivedAt;
  205. unsigned long lastChunkSize;
  206. unsigned int versionMajor,versionMinor,revision;
  207. };
  208. const RuntimeEnvironment *_r;
  209. _Download *_download;
  210. std::list<SharedUpdate> _sharedUpdates; // usually not more than 1 or 2 of these
  211. Mutex _lock;
  212. };
  213. } // namespace ZeroTier
  214. #endif