diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-11-01 12:38:38 -0400 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-11-01 12:38:38 -0400 |
commit | ae138566a94ed407c71dbc7a155c810b2389cc4b (patch) | |
tree | ee6b79392d767b9d727041f4ab87d20ca73bbadc /node/Updater.hpp | |
parent | e4044eeb709c09f5577f7e77260ccf997a298156 (diff) | |
download | infinitytier-ae138566a94ed407c71dbc7a155c810b2389cc4b.tar.gz infinitytier-ae138566a94ed407c71dbc7a155c810b2389cc4b.zip |
Updater code, work in progress...
Diffstat (limited to 'node/Updater.hpp')
-rw-r--r-- | node/Updater.hpp | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/node/Updater.hpp b/node/Updater.hpp new file mode 100644 index 00000000..66d45ee4 --- /dev/null +++ b/node/Updater.hpp @@ -0,0 +1,182 @@ +/* + * ZeroTier One - Global Peer to Peer Ethernet + * Copyright (C) 2012-2013 ZeroTier Networks LLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + * -- + * + * ZeroTier may be used and distributed under the terms of the GPLv3, which + * are available at: http://www.gnu.org/licenses/gpl-3.0.html + * + * If you would like to embed ZeroTier into a commercial application or + * redistribute it in a modified binary form, please contact ZeroTier Networks + * LLC. Start here: http://www.zerotier.com/ + */ + +#ifndef _ZT_UPDATER_HPP +#define _ZT_UPDATER_HPP + +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include <map> +#include <vector> +#include <algorithm> +#include <iterator> +#include <stdexcept> +#include <string> + +#include "Constants.hpp" +#include "Packet.hpp" +#include "Mutex.hpp" +#include "Address.hpp" +#include "C25519.hpp" +#include "Array.hpp" +#include "Dictionary.hpp" + +// Chunk size-- this can be changed, picked to always fit in one packet each. +#define ZT_UPDATER_CHUNK_SIZE 1350 + +// Sanity check value for constraining max size since right now we buffer +// in RAM. +#define ZT_UPDATER_MAX_SUPPORTED_SIZE (1024 * 1024 * 16) + +// Retry timeout in ms. +#define ZT_UPDATER_RETRY_TIMEOUT 30000 + +// After this long, look for a new set of peers that have the download shared. +#define ZT_UPDATER_REPOLL_TIMEOUT 60000 + +namespace ZeroTier { + +class RuntimeEnvironment; + +/** + * Software update downloader and executer + * + * FYI: downloads occur via the protocol rather than out of band via http so + * that ZeroTier One can be run in secure jailed environments where it is the + * only protocol permitted over the "real" Internet. This is required for a + * number of potentially popular use cases. + * + * The protocol is a simple chunk-pulling "trivial FTP" like thing that should + * be suitable for core engine software updates. Software updates themselves + * are platform-specific executables that ZeroTier One then exits and runs. + * + * Updaters are cached one-deep and can be replicated peer to peer in addition + * to coming from supernodes. This makes it just a little bit BitTorrent-like + * and helps things scale, and is also ready for further protocol + * decentralization that may occur in the future. + */ +class Updater +{ +public: + Updater(const RuntimeEnvironment *renv); + ~Updater(); + + /** + * Rescan home path for shareable updates + * + * This happens automatically on construction. + */ + void refreshShared(); + + /** + * Attempt to find an update if this version is newer than ours + * + * This is called whenever a peer notifies us of its version. It does nothing + * if that version is not newer, otherwise it looks around for an update. + * + * @param vMajor Major version + * @param vMinor Minor version + * @param revision Revision + */ + void getUpdateIfThisIsNewer(unsigned int vMajor,unsigned int vMinor,unsigned int revision); + + /** + * Called periodically from main loop + */ + void retryIfNeeded(); + +private: + struct _Download + { + _Download(const void *s512,const std::string &fn,unsigned long len,unsigned int vMajor,unsigned int vMinor,unsigned int rev) + { + data.resize(len); + haveChunks.resize((len / ZT_UPDATER_CHUNK_SIZE) + 1,false); + filename = fn; + memcpy(sha512,s512,64); + lastChunkSize = len % ZT_UPDATER_CHUNK_SIZE; + versionMajor = vMajor; + versionMinor = vMinor; + revision = rev; + } + + long nextChunk() const + { + std::vector<bool>::const_iterator ptr(std::find(haveChunks.begin(),haveChunks.end(),false)); + if (ptr != haveChunks.end()) + return std::distance(haveChunks.begin(),ptr); + else return -1; + } + + bool gotChunk(unsigned long at,const void *chunk,unsigned long len) + { + unsigned long whichChunk = at / ZT_UPDATER_CHUNK_SIZE; + if (at != (ZT_UPDATER_CHUNK_SIZE * whichChunk)) + return false; // not at chunk boundary + if (whichChunk >= haveChunks.size()) + return false; // overflow + if ((whichChunk == (haveChunks.size() - 1))&&(len != lastChunkSize)) + return false; // last chunk, size wrong + else if (len != ZT_UPDATER_CHUNK_SIZE) + return false; // chunk size wrong + for(unsigned long i=0;i<len;++i) + data[at + i] = ((const char *)chunk)[i]; + haveChunks[whichChunk] = true; + return true; + } + + std::vector<char> data; + std::vector<bool> haveChunks; + std::vector<Address> peersThatHave; + std::string filename; + unsigned char sha512[64]; + Address currentlyReceivingFrom; + uint64_t lastChunkReceivedAt; + unsigned long lastChunkSize; + unsigned int versionMajor,versionMinor,revision; + }; + + struct _Shared + { + std::string filename; + unsigned char sha512[64]; + C25519::Signature sig; + Address signedBy; + unsigned long size; + }; + + const RuntimeEnvironment *_r; + _Download *_download; + std::map< Array<unsigned char,16>,_Shared > _sharedUpdates; + Mutex _lock; +}; + +} // namespace ZeroTier + +#endif |