SoftwareUpdater.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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_SOFTWAREUPDATER_HPP
  28. #define ZT_SOFTWAREUPDATER_HPP
  29. #include <stdint.h>
  30. #include <string>
  31. #include "Constants.hpp"
  32. #include "Mutex.hpp"
  33. #include "Utils.hpp"
  34. #include "Defaults.hpp"
  35. #include "Address.hpp"
  36. /**
  37. * Delay between fetches of the root topology update URL
  38. *
  39. * 86400000 = check once every 24 hours (this doesn't change often)
  40. */
  41. #define ZT_UPDATE_ROOT_TOPOLOGY_CHECK_INTERVAL 86400000
  42. /**
  43. * Minimum interval between attempts to do a software update
  44. */
  45. #define ZT_UPDATE_MIN_INTERVAL 120000
  46. /**
  47. * Maximum interval between checks for new versions
  48. */
  49. #define ZT_UPDATE_MAX_INTERVAL 7200000
  50. /**
  51. * Software update HTTP timeout in seconds
  52. */
  53. #define ZT_UPDATE_HTTP_TIMEOUT 120
  54. namespace ZeroTier {
  55. class RuntimeEnvironment;
  56. /**
  57. * Software updater
  58. */
  59. class SoftwareUpdater
  60. {
  61. public:
  62. SoftwareUpdater(const RuntimeEnvironment *renv);
  63. ~SoftwareUpdater();
  64. /**
  65. * Remove old updates in updates.d
  66. */
  67. void cleanOldUpdates();
  68. /**
  69. * Called on each version message from a peer
  70. *
  71. * If a peer has a newer version, that causes an update to be started.
  72. *
  73. * @param vmaj Peer's major version
  74. * @param vmin Peer's minor version
  75. * @param rev Peer's revision
  76. */
  77. void sawRemoteVersion(unsigned int vmaj,unsigned int vmin,unsigned int rev);
  78. /**
  79. * Check for updates now regardless of last check time or version
  80. *
  81. * This only starts a check if one is not in progress. Otherwise it does
  82. * nothing.
  83. */
  84. void checkNow();
  85. /**
  86. * Check for updates now if it's been longer than ZT_UPDATE_MAX_INTERVAL
  87. *
  88. * This is called periodically from the main loop.
  89. */
  90. inline void checkIfMaxIntervalExceeded(uint64_t now)
  91. {
  92. if ((now - _lastUpdateAttempt) >= ZT_UPDATE_MAX_INTERVAL)
  93. checkNow();
  94. }
  95. /**
  96. * Pack three-component version into a 64-bit integer
  97. *
  98. * @param vmaj Major version (0..65535)
  99. * @param vmin Minor version (0..65535)
  100. * @param rev Revision (0..65535)
  101. * @return Version packed into an easily comparable 64-bit integer
  102. */
  103. static inline uint64_t packVersion(unsigned int vmaj,unsigned int vmin,unsigned int rev)
  104. throw()
  105. {
  106. return ( ((uint64_t)(vmaj & 0xffff) << 32) | ((uint64_t)(vmin & 0xffff) << 16) | (uint64_t)(rev & 0xffff) );
  107. }
  108. /**
  109. * Parse NFO data from .nfo file on software update site
  110. *
  111. * The first argument is the NFO data, and all the remaining arguments are
  112. * result parameters to be filled with results. If an error is returned the
  113. * results in the parameters should be considered undefined.
  114. *
  115. * @param nfo NFO data
  116. * @param vMajor Result: major version
  117. * @param vMinor Result: minor version
  118. * @param vRevision Result: revision number
  119. * @param signedBy Result: signing identity
  120. * @param signature Result: Ed25519 signature data
  121. * @param url Result: URL of update binary
  122. * @return NULL on success or error message on failure
  123. */
  124. static const char *parseNfo(
  125. const char *nfoText,
  126. unsigned int &vMajor,
  127. unsigned int &vMinor,
  128. unsigned int &vRevision,
  129. Address &signedBy,
  130. std::string &signature,
  131. std::string &url);
  132. /**
  133. * Validate an update once downloaded
  134. *
  135. * This obtains the identity corresponding to the address from the compiled-in
  136. * list of valid signing identities.
  137. *
  138. * @param data Update data
  139. * @param len Length of update data
  140. * @param signedBy Signing authority address
  141. * @param signature Signing authority signature
  142. * @return True on validation success, false if rejected
  143. */
  144. static bool validateUpdate(
  145. const void *data,
  146. unsigned int len,
  147. const Address &signedBy,
  148. const std::string &signature);
  149. private:
  150. static void _cbHandleGetLatestVersionInfo(void *arg,int code,const std::string &url,const std::string &body);
  151. static void _cbHandleGetLatestVersionBinary(void *arg,int code,const std::string &url,const std::string &body);
  152. const RuntimeEnvironment *RR;
  153. const uint64_t _myVersion;
  154. volatile uint64_t _lastUpdateAttempt;
  155. volatile enum {
  156. UPDATE_STATUS_IDLE,
  157. UPDATE_STATUS_GETTING_NFO,
  158. UPDATE_STATUS_GETTING_FILE
  159. } _status;
  160. volatile bool _die;
  161. Address _signedBy;
  162. std::string _signature;
  163. Mutex _lock;
  164. };
  165. } // namespace ZeroTier
  166. #endif