/* * ZeroTier One - Network Virtualization Everywhere * Copyright (C) 2011-2015 ZeroTier, Inc. * * 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 . */ #ifndef ZT_SQLITENETWORKCONTROLLER_HPP #define ZT_SQLITENETWORKCONTROLLER_HPP #include #include #include #include #include #include #include #include #include "../node/Constants.hpp" #include "../node/NetworkController.hpp" #include "../node/Mutex.hpp" #include "../node/Utils.hpp" #include "../node/Address.hpp" #include "../node/InetAddress.hpp" #include "../node/NonCopyable.hpp" #include "../osdep/OSUtils.hpp" #include "../osdep/Thread.hpp" #include "../osdep/BlockingQueue.hpp" #include "../ext/json/json.hpp" #include "JSONDB.hpp" namespace ZeroTier { class Node; class EmbeddedNetworkController : public NetworkController,NonCopyable { public: /** * @param node Parent node * @param dbPath Path to store data */ EmbeddedNetworkController(Node *node,const char *dbPath); virtual ~EmbeddedNetworkController(); virtual void init(const Identity &signingId,Sender *sender); virtual void request( uint64_t nwid, const InetAddress &fromAddr, uint64_t requestPacketId, const Identity &identity, const Dictionary &metaData); unsigned int handleControlPlaneHttpGET( const std::vector &path, const std::map &urlArgs, const std::map &headers, const std::string &body, std::string &responseBody, std::string &responseContentType); unsigned int handleControlPlaneHttpPOST( const std::vector &path, const std::map &urlArgs, const std::map &headers, const std::string &body, std::string &responseBody, std::string &responseContentType); unsigned int handleControlPlaneHttpDELETE( const std::vector &path, const std::map &urlArgs, const std::map &headers, const std::string &body, std::string &responseBody, std::string &responseContentType); void handleRemoteTrace(const ZT_RemoteTrace &rt); void threadMain() throw(); private: struct _RQEntry { uint64_t nwid; uint64_t requestPacketId; InetAddress fromAddr; Identity identity; Dictionary metaData; enum { RQENTRY_TYPE_REQUEST = 0, RQENTRY_TYPE_PING = 1 } type; }; void _request(uint64_t nwid,const InetAddress &fromAddr,uint64_t requestPacketId,const Identity &identity,const Dictionary &metaData); inline void _startThreads() { Mutex::Lock _l(_threads_m); if (_threads.size() == 0) { long hwc = (long)std::thread::hardware_concurrency(); if (hwc < 1) hwc = 1; else if (hwc > 16) hwc = 16; for(long i=0;i _queue; std::vector _threads; Mutex _threads_m; JSONDB _db; Node *const _node; std::string _path; NetworkController::Sender *_sender; Identity _signingId; std::string _signingIdAddressString; struct _MemberStatusKey { _MemberStatusKey() : networkId(0),nodeId(0) {} _MemberStatusKey(const uint64_t nwid,const uint64_t nid) : networkId(nwid),nodeId(nid) {} uint64_t networkId; uint64_t nodeId; inline bool operator==(const _MemberStatusKey &k) const { return ((k.networkId == networkId)&&(k.nodeId == nodeId)); } }; struct _MemberStatus { _MemberStatus() : lastRequestTime(0),vMajor(-1),vMinor(-1),vRev(-1),vProto(-1) {} uint64_t lastRequestTime; int vMajor,vMinor,vRev,vProto; Dictionary lastRequestMetaData; Identity identity; InetAddress physicalAddr; // last known physical address inline bool online(const uint64_t now) const { return ((now - lastRequestTime) < (ZT_NETWORK_AUTOCONF_DELAY * 2)); } }; struct _MemberStatusHash { inline std::size_t operator()(const _MemberStatusKey &networkIdNodeId) const { return (std::size_t)(networkIdNodeId.networkId + networkIdNodeId.nodeId); } }; std::unordered_map< _MemberStatusKey,_MemberStatus,_MemberStatusHash > _memberStatus; Mutex _memberStatus_m; }; } // namespace ZeroTier #endif