SoftwareUpdater.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_SOFTWAREUPDATER_HPP
  28. #define ZT_SOFTWAREUPDATER_HPP
  29. #include <stdint.h>
  30. #include "Constants.hpp"
  31. #include "Mutex.hpp"
  32. #include "Utils.hpp"
  33. #include "HttpClient.hpp"
  34. #include "Defaults.hpp"
  35. namespace ZeroTier {
  36. class RuntimeEnvironment;
  37. /**
  38. * Software updater
  39. */
  40. class SoftwareUpdater
  41. {
  42. public:
  43. SoftwareUpdater(const RuntimeEnvironment *renv);
  44. ~SoftwareUpdater();
  45. /**
  46. * Called on each version message from a peer
  47. *
  48. * If a peer has a newer version, that causes an update to be started.
  49. *
  50. * @param vmaj Peer's major version
  51. * @param vmin Peer's minor version
  52. * @param rev Peer's revision
  53. */
  54. inline void sawRemoteVersion(unsigned int vmaj,unsigned int vmin,unsigned int rev)
  55. {
  56. const uint64_t tmp = packVersion(vmaj,vmin,rev);
  57. if (tmp > _myVersion) {
  58. Mutex::Lock _l(_lock);
  59. if ((_status == UPDATE_STATUS_IDLE)&&(!_die)&&(ZT_DEFAULTS.updateLatestNfoURL.length())) {
  60. const uint64_t now = Utils::now();
  61. if ((now - _lastUpdateAttempt) >= ZT_UPDATE_MIN_INTERVAL) {
  62. _lastUpdateAttempt = now;
  63. _status = UPDATE_STATUS_GETTING_NFO;
  64. HttpClient::GET(ZT_DEFAULTS.updateLatestNfoURL,HttpClient::NO_HEADERS,ZT_UPDATE_HTTP_TIMEOUT,&_cbHandleGetLatestVersionInfo,this);
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * Check for updates now regardless of last check time or version
  71. */
  72. inline void checkNow()
  73. {
  74. Mutex::Lock _l(_lock);
  75. if (_status == UPDATE_STATUS_IDLE) {
  76. _lastUpdateAttempt = Utils::now();
  77. _status = UPDATE_STATUS_GETTING_NFO;
  78. HttpClient::GET(ZT_DEFAULTS.updateLatestNfoURL,HttpClient::NO_HEADERS,ZT_UPDATE_HTTP_TIMEOUT,&_cbHandleGetLatestVersionInfo,this);
  79. }
  80. }
  81. /**
  82. * Pack three-component version into a 64-bit integer
  83. *
  84. * @param vmaj Major version (0..65535)
  85. * @param vmin Minor version (0..65535)
  86. * @param rev Revision (0..65535)
  87. * @return Version packed into an easily comparable 64-bit integer
  88. */
  89. static inline uint64_t packVersion(unsigned int vmaj,unsigned int vmin,unsigned int rev)
  90. throw()
  91. {
  92. return ( ((uint64_t)(vmaj & 0xffff) << 32) | ((uint64_t)(vmin & 0xffff) << 16) | (uint64_t)(rev & 0xffff) );
  93. }
  94. private:
  95. static void _cbHandleGetLatestVersionInfo(void *arg,int code,const std::string &url,bool onDisk,const std::string &body);
  96. static void _cbHandleGetLatestVersionBinary(void *arg,int code,const std::string &url,bool onDisk,const std::string &body);
  97. const RuntimeEnvironment *_r;
  98. const uint64_t _myVersion;
  99. volatile uint64_t _lastUpdateAttempt;
  100. volatile enum {
  101. UPDATE_STATUS_IDLE,
  102. UPDATE_STATUS_GETTING_NFO,
  103. UPDATE_STATUS_GETTING_FILE
  104. } _status;
  105. volatile bool _die;
  106. Address _signedBy;
  107. std::string _signature;
  108. Mutex _lock;
  109. };
  110. } // namespace ZeroTier
  111. #endif