Updater.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "Constants.hpp"
  39. #include "Packet.hpp"
  40. #include "Mutex.hpp"
  41. #include "Address.hpp"
  42. #include "C25519.hpp"
  43. #include "Array.hpp"
  44. #include "Dictionary.hpp"
  45. // Chunk size-- this can be changed, picked to always fit in one packet each.
  46. #define ZT_UPDATER_CHUNK_SIZE 1350
  47. // Sanity check value for constraining max size since right now we buffer
  48. // in RAM.
  49. #define ZT_UPDATER_MAX_SUPPORTED_SIZE (1024 * 1024 * 16)
  50. // Retry timeout in ms.
  51. #define ZT_UPDATER_RETRY_TIMEOUT 30000
  52. // After this long, look for a new set of peers that have the download shared.
  53. #define ZT_UPDATER_REPOLL_TIMEOUT 60000
  54. namespace ZeroTier {
  55. class RuntimeEnvironment;
  56. /**
  57. * Software update downloader and executer
  58. *
  59. * FYI: downloads occur via the protocol rather than out of band via http so
  60. * that ZeroTier One can be run in secure jailed environments where it is the
  61. * only protocol permitted over the "real" Internet. This is required for a
  62. * number of potentially popular use cases.
  63. *
  64. * The protocol is a simple chunk-pulling "trivial FTP" like thing that should
  65. * be suitable for core engine software updates. Software updates themselves
  66. * are platform-specific executables that ZeroTier One then exits and runs.
  67. *
  68. * Updaters are cached one-deep and can be replicated peer to peer in addition
  69. * to coming from supernodes. This makes it just a little bit BitTorrent-like
  70. * and helps things scale, and is also ready for further protocol
  71. * decentralization that may occur in the future.
  72. */
  73. class Updater
  74. {
  75. public:
  76. Updater(const RuntimeEnvironment *renv);
  77. ~Updater();
  78. /**
  79. * Rescan home path for shareable updates
  80. *
  81. * This happens automatically on construction.
  82. */
  83. void refreshShared();
  84. /**
  85. * Attempt to find an update if this version is newer than ours
  86. *
  87. * This is called whenever a peer notifies us of its version. It does nothing
  88. * if that version is not newer, otherwise it looks around for an update.
  89. *
  90. * @param vMajor Major version
  91. * @param vMinor Minor version
  92. * @param revision Revision
  93. */
  94. void getUpdateIfThisIsNewer(unsigned int vMajor,unsigned int vMinor,unsigned int revision);
  95. /**
  96. * Called periodically from main loop
  97. */
  98. void retryIfNeeded();
  99. /**
  100. * Called when a chunk is received
  101. *
  102. * @param sha512First16 First 16 bytes of SHA-512 hash
  103. * @param at Position of chunk
  104. * @param chunk Chunk data
  105. * @param len Length of chunk
  106. */
  107. void handleChunk(const void *sha512First16,unsigned long at,const void *chunk,unsigned long len);
  108. /**
  109. * @return Canonical update filename for this platform or empty string if unsupported
  110. */
  111. static std::string generateUpdateFilename(unsigned int vMajor,unsigned int vMinor,unsigned int revision);
  112. /**
  113. * Parse an updater filename and extract version info
  114. *
  115. * @param filename Filename to parse
  116. * @return True if info was extracted and value-result parameters set
  117. */
  118. static bool parseUpdateFilename(const char *filename,unsigned int &vMajor,unsigned int &vMinor,unsigned int &revision);
  119. private:
  120. struct _Download
  121. {
  122. _Download(const void *s512,const std::string &fn,unsigned long len,unsigned int vMajor,unsigned int vMinor,unsigned int rev)
  123. {
  124. data.resize(len);
  125. haveChunks.resize((len / ZT_UPDATER_CHUNK_SIZE) + 1,false);
  126. filename = fn;
  127. memcpy(sha512,s512,64);
  128. lastChunkSize = len % ZT_UPDATER_CHUNK_SIZE;
  129. versionMajor = vMajor;
  130. versionMinor = vMinor;
  131. revision = rev;
  132. }
  133. long nextChunk() const
  134. {
  135. std::vector<bool>::const_iterator ptr(std::find(haveChunks.begin(),haveChunks.end(),false));
  136. if (ptr != haveChunks.end())
  137. return std::distance(haveChunks.begin(),ptr);
  138. else return -1;
  139. }
  140. bool gotChunk(unsigned long at,const void *chunk,unsigned long len)
  141. {
  142. unsigned long whichChunk = at / ZT_UPDATER_CHUNK_SIZE;
  143. if (at != (ZT_UPDATER_CHUNK_SIZE * whichChunk))
  144. return false; // not at chunk boundary
  145. if (whichChunk >= haveChunks.size())
  146. return false; // overflow
  147. if ((whichChunk == (haveChunks.size() - 1))&&(len != lastChunkSize))
  148. return false; // last chunk, size wrong
  149. else if (len != ZT_UPDATER_CHUNK_SIZE)
  150. return false; // chunk size wrong
  151. for(unsigned long i=0;i<len;++i)
  152. data[at + i] = ((const char *)chunk)[i];
  153. haveChunks[whichChunk] = true;
  154. return true;
  155. }
  156. std::string data;
  157. std::vector<bool> haveChunks;
  158. std::vector<Address> peersThatHave;
  159. std::string filename;
  160. unsigned char sha512[64];
  161. Address currentlyReceivingFrom;
  162. uint64_t lastChunkReceivedAt;
  163. unsigned long lastChunkSize;
  164. unsigned int versionMajor,versionMinor,revision;
  165. };
  166. struct _Shared
  167. {
  168. std::string filename;
  169. unsigned char sha512[64];
  170. C25519::Signature sig;
  171. Address signedBy;
  172. unsigned long size;
  173. };
  174. const RuntimeEnvironment *_r;
  175. _Download *_download;
  176. std::map< Array<unsigned char,16>,_Shared > _sharedUpdates;
  177. Mutex _lock;
  178. };
  179. } // namespace ZeroTier
  180. #endif