From 9f107dbd4eefdb4e5427f15e844aefbd573a6465 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 18 Oct 2013 09:48:02 -0400 Subject: [PATCH] Work in progress on cleaning up netconf mess in node code... --- netconf-service/netconf.cpp | 55 ++++++++------ node/NetworkConfig.cpp | 50 +++++++++++++ node/NetworkConfig.hpp | 140 ++++++++++++++++++++++++++++++++++++ node/PacketDecoder.cpp | 3 +- 4 files changed, 225 insertions(+), 23 deletions(-) create mode 100644 node/NetworkConfig.cpp create mode 100644 node/NetworkConfig.hpp diff --git a/netconf-service/netconf.cpp b/netconf-service/netconf.cpp index c061c98d2..695e0795e 100644 --- a/netconf-service/netconf.cpp +++ b/netconf-service/netconf.cpp @@ -73,6 +73,7 @@ #include "../node/Identity.hpp" #include "../node/Utils.hpp" #include "../node/Mutex.hpp" +#include "../node/NetworkConfig.hpp" using namespace ZeroTier; using namespace mysqlpp; @@ -198,7 +199,7 @@ int main(int argc,char **argv) // Deserialize querying peer identity and network ID Identity peerIdentity(request.get("peerId")); uint64_t nwid = strtoull(request.get("nwid").c_str(),(char **)0,16); - std::string fromAddr(request.get("from")); + std::string fromAddr(request.get("from","")); // Meta-information from node, such as (future) geo-location stuff Dictionary meta; @@ -412,41 +413,51 @@ int main(int argc,char **argv) // Update activity table for this network to indicate peer's participation { - Query q = dbCon->query(); - q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime,lastActivityFrom) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << "," << quote << fromAddr << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime),lastActivityFrom = VALUES(lastActivityFrom)"; - q.exec(); + if (fromAddr.length()) { + Query q = dbCon->query(); + q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime,lastActivityFrom) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << "," << quote << fromAddr << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime),lastActivityFrom = VALUES(lastActivityFrom)"; + q.exec(); + } else { + Query q = dbCon->query(); + q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime)"; + q.exec(); + } } // Assemble response dictionary to send to peer Dictionary netconf; sprintf(buf,"%.16llx",(unsigned long long)nwid); - netconf["nwid"] = buf; - netconf["peer"] = peerIdentity.address().toString(); - netconf["name"] = name; - netconf["desc"] = desc; - netconf["o"] = (isOpen ? "1" : "0"); - netconf["et"] = etherTypeWhitelist; - netconf["mr"] = multicastRates.toString(); + netconf[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = buf; + netconf[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = peerIdentity.address().toString(); + netconf[ZT_NETWORKCONFIG_DICT_KEY_NAME] = name; + netconf[ZT_NETWORKCONFIG_DICT_KEY_DESC] = desc; + netconf[ZT_NETWORKCONFIG_DICT_KEY_IS_OPEN] = (isOpen ? "1" : "0"); + netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = etherTypeWhitelist; + netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = multicastRates.toString(); sprintf(buf,"%llx",(unsigned long long)Utils::now()); - netconf["ts"] = buf; - netconf["eARP"] = (emulateArp ? "1" : "0"); - netconf["eNDP"] = (emulateNdp ? "1" : "0"); - sprintf(buf,"%x",arpCacheTtl); - netconf["cARP"] = buf; - sprintf(buf,"%x",ndpCacheTtl); - netconf["cNDP"] = buf; + netconf[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = buf; + netconf[ZT_NETWORKCONFIG_DICT_KEY_EMULATE_ARP] = (emulateArp ? "1" : "0"); + netconf[ZT_NETWORKCONFIG_DICT_KEY_EMULATE_NDP] = (emulateNdp ? "1" : "0"); + if (arpCacheTtl) { + sprintf(buf,"%x",arpCacheTtl); + netconf[ZT_NETWORKCONFIG_DICT_KEY_ARP_CACHE_TTL] = buf; + } + if (ndpCachettl) { + sprintf(buf,"%x",ndpCacheTtl); + netconf[ZT_NETWORKCONFIG_DICT_KEY_NDP_CACHE_TTL] = buf; + } if (multicastPrefixBits) { sprintf(buf,"%x",multicastPrefixBits); - netconf["mpb"] = buf; + netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS] = buf; } if (multicastDepth) { sprintf(buf,"%x",multicastDepth); - netconf["md"] = buf; + netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH] = buf; } if (ipv4Static.length()) - netconf["v4s"] = ipv4Static; + netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = ipv4Static; if (ipv6Static.length()) - netconf["v6s"] = ipv6Static; + netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = ipv6Static; // Send netconf as service bus response { diff --git a/node/NetworkConfig.cpp b/node/NetworkConfig.cpp new file mode 100644 index 000000000..1536af88e --- /dev/null +++ b/node/NetworkConfig.cpp @@ -0,0 +1,50 @@ +/* + * 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 . + * + * -- + * + * 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/ + */ + +#include "NetworkConfig.hpp" + +namespace ZeroTier { + +std::set NetworkConfig::allowedEtherTypes() +{ + std::set ets; + for(unsigned int i=0;i>= 1; + ++et; + } + } + } + return ets; +} + +} // namespace ZeroTier diff --git a/node/NetworkConfig.hpp b/node/NetworkConfig.hpp new file mode 100644 index 000000000..317c66718 --- /dev/null +++ b/node/NetworkConfig.hpp @@ -0,0 +1,140 @@ +/* + * 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 . + * + * -- + * + * 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_NETWORKCONFIG_HPP +#define _ZT_NETWORKCONFIG_HPP + +#include +#include +#include + +#include "Dictionary.hpp" +#include "InetAddress.hpp" +#include "AtomicCounter.hpp" +#include "SharedPtr.hpp" + +namespace ZeroTier { + +// These are short to fit in packets with plenty of room to spare +#define ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES "et" +#define ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID "nwid" +#define ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP "ts" +#define ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO "id" +#define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS "mpb" +#define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH "md" +#define ZT_NETWORKCONFIG_DICT_KEY_ARP_CACHE_TTL "cARP" +#define ZT_NETWORKCONFIG_DICT_KEY_NDP_CACHE_TTL "cNDP" +#define ZT_NETWORKCONFIG_DICT_KEY_EMULATE_ARP "eARP" +#define ZT_NETWORKCONFIG_DICT_KEY_EMULATE_NDP "eNDP" +#define ZT_NETWORKCONFIG_DICT_KEY_IS_OPEN "o" +#define ZT_NETWORKCONFIG_DICT_KEY_NAME "name" +#define ZT_NETWORKCONFIG_DICT_KEY_DESC "desc" +#define ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC "v4s" +#define ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC "v6s" +#define ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES "mr" + +/** + * Network configuration received from netconf master nodes + * + * This is designed to work as an immutable value object held in a shared + * pointer so that it can be both updated and used without too much mutex + * boogie. + */ +class NetworkConfig +{ +public: + friend class SharedPtr; + + /** + * @param d Dictionary containing configuration + * @throws std::invalid_argument Invalid configuration + */ + NetworkConfig(const Dictionary &d) + throw(std::invalid_argument) + { + _fromDictionary(d); + } + + /** + * @param etherType Ethernet frame type to check + * @return True if allowed on this network + */ + inline bool allowsEtherType(unsigned int etherType) + throw() + { + if ((!etherType)||(etherType > 0xffff)) // sanity checks + return false; + else if ((_etWhitelist[0] & 1)) // prsence of 0 in set inverts sense: whitelist becomes blacklist + return (!(_etWhitelist[etherType >> 3] & (1 << (etherType & 7)))); + else return ((_etWhitelist[etherType >> 3] & (1 << (etherType & 7)))); + } + + std::set allowedEtherTypes() const; + inline uint64_t networkId() const throw() { return _nwid; } + inline uint64_t timestamp() const throw() { return _timestamp; } + inline const Address &issuedTo() const throw() { return _issuedTo; } + inline unsigned int multicastPrefixBits() const throw() { return _multicastPrefixBits; } + inline unsigned int multicastDepth() const throw() { return _multicastDepth; } + inline unsigned int arpCacheTtl() const throw() { return _arpCacheTtl; } + inline unsigned int ndpCacheTtl() const throw() { return _ndpCacheTtl; } + inline bool emulateArp() const throw() { return _emulateArp; } + inline bool emulateNdp() const throw() { return _emulateNdp; } + inline bool isOpen() const throw() { return _isOpen; } + inline const std::string &name() const throw() { return _name; } + inline const std::string &description() const throw() { return _description; } + inline const std::set &staticIps() const throw() { return _staticIps; } + inline const MulticastRateTable &multicastRates() const throw() { return _multicastRates; } + +private: + NetworkConfig() {} + ~NetworkConfig() {} + + void _fromDictionary(const Dictionary &d) + throw(std::invalid_argument); + + unsigned char _etWhitelist[65536 / 8]; + uint64_t _nwid; + uint64_t _timestamp; + Address _issuedTo; + unsigned int _multicastPrefixBits; + unsigned int _multicastDepth; + unsigned int _arpCacheTtl; + unsigned int _ndpCacheTtl; + bool _emulateArp; + bool _emulateNdp; + bool _isOpen; + std::string _name; + std::string _description; + std::set _staticIps; + MulticastRateTable _multicastRates; + + AtomicCounter __refCount; +}; + +} // namespace ZeroTier + +#endif diff --git a/node/PacketDecoder.cpp b/node/PacketDecoder.cpp index c4199732e..82f3b6afa 100644 --- a/node/PacketDecoder.cpp +++ b/node/PacketDecoder.cpp @@ -796,7 +796,8 @@ bool PacketDecoder::_doNETWORK_CONFIG_REQUEST(const RuntimeEnvironment *_r,const request["nwid"] = tmp; Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)packetId()); request["requestId"] = tmp; - request["from"] = _remoteAddress.toString(); + if (!hops()) + request["from"] = _remoteAddress.toString(); //TRACE("to netconf:\n%s",request.toString().c_str()); _r->netconfService->send(request); } else {