From ca93b4a1ac9d07ea150572801fd47f9a0818abff Mon Sep 17 00:00:00 2001 From: Adam Ierymenko <adam.ierymenko@gmail.com> Date: Fri, 18 Oct 2013 14:16:53 -0400 Subject: Clean up some stuff, including a few spots where exceptions were not being handled correctly. --- attic/Range.hpp | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ node/Address.hpp | 50 ++++++-------------- node/Buffer.hpp | 19 ++++++++ node/Defaults.cpp | 4 +- node/Defaults.hpp | 4 +- node/Demarc.cpp | 1 - node/Demarc.hpp | 3 +- node/NodeConfig.cpp | 4 +- node/NodeConfig.hpp | 6 +-- node/PacketDecoder.cpp | 1 - node/PacketDecoder.hpp | 3 +- node/Range.hpp | 123 ------------------------------------------------- node/SysEnv.cpp | 3 -- node/SysEnv.hpp | 3 +- node/Topology.cpp | 3 +- node/Topology.hpp | 4 +- 16 files changed, 165 insertions(+), 189 deletions(-) create mode 100644 attic/Range.hpp delete mode 100644 node/Range.hpp diff --git a/attic/Range.hpp b/attic/Range.hpp new file mode 100644 index 00000000..7e3785b5 --- /dev/null +++ b/attic/Range.hpp @@ -0,0 +1,123 @@ +/* + * 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_RANGE_HPP +#define _ZT_RANGE_HPP + +namespace ZeroTier { + +/** + * A range of numeric values + * + * @tparam T Type, can be any numeric value (int, float, double, etc.) + */ +template<typename T> +class Range +{ +public: + /** + * Construct an empty range + */ + Range() + throw() : + start(0), + end(0) + { + } + + /** + * @param s Starting value (inclusive) + * @param e Ending value (exclusive) + */ + Range(T s,T e) + throw() : + start(s), + end(e) + { + } + + /** + * Construct a range containing from n to n+1 (thus only n for integers) + * + * @param n Number to contain + */ + Range(T n) + throw() : + start(n), + end(n+1) + { + } + + /** + * @return end - start + */ + inline T magnitude() const + throw() + { + return (end - start); + } + + /** + * @return True if range contains something (magnitude is nonzero) + */ + inline operator bool() const + throw() + { + return (end > start); + } + + /** + * @param v Value to test + * @return True if value is between start (inclusive) and end (exclusive) + */ + inline bool operator()(const T &v) const + throw() + { + return ((v >= start)&&(v < end)); + } + + inline bool operator==(const Range &r) const throw() { return ((start == r.start)&&(end == r.end)); } + inline bool operator!=(const Range &r) const throw() { return (!(*this == r)); } + inline bool operator<(const Range &r) const throw() { return ((start < r.start) ? true : ((start == r.start) ? (end < r.end) : false)); } + inline bool operator>(const Range &r) const throw() { return (r < *this); } + inline bool operator<=(const Range &r) const throw() { return !(r < *this); } + inline bool operator>=(const Range &r) const throw() { return !(*this < r); } + + /** + * Start of range (may be modified directly) + */ + T start; + + /** + * End of range (may be modified directly) + */ + T end; +}; + +} // namespace ZeroTier + +#endif diff --git a/node/Address.hpp b/node/Address.hpp index 250e95f3..71c85f01 100644 --- a/node/Address.hpp +++ b/node/Address.hpp @@ -150,25 +150,12 @@ public: inline void appendTo(Buffer<C> &b) const throw(std::out_of_range) { - b.append((unsigned char)((_a >> 32) & 0xff)); - b.append((unsigned char)((_a >> 24) & 0xff)); - b.append((unsigned char)((_a >> 16) & 0xff)); - b.append((unsigned char)((_a >> 8) & 0xff)); - b.append((unsigned char)(_a & 0xff)); - } - - /** - * @return String containing address as 5 binary bytes - */ - inline std::string toBinaryString() const - { - std::string b; - b.push_back((char)((_a >> 32) & 0xff)); - b.push_back((char)((_a >> 24) & 0xff)); - b.push_back((char)((_a >> 16) & 0xff)); - b.push_back((char)((_a >> 8) & 0xff)); - b.push_back((char)(_a & 0xff)); - return b; + unsigned char *p = (unsigned char *)b.appendField(ZT_ADDRESS_LENGTH); + *(p++) = (unsigned char)((_a >> 32) & 0xff); + *(p++) = (unsigned char)((_a >> 24) & 0xff); + *(p++) = (unsigned char)((_a >> 16) & 0xff); + *(p++) = (unsigned char)((_a >> 8) & 0xff); + *p = (unsigned char)(_a & 0xff); } /** @@ -201,19 +188,12 @@ public: inline bool wouldHaveMac(const MAC &mac) const throw() { - if (mac.data[0] != ZT_MAC_FIRST_OCTET) - return false; - if (mac.data[1] != (unsigned char)((_a >> 32) & 0xff)) - return false; - if (mac.data[2] != (unsigned char)((_a >> 24) & 0xff)) - return false; - if (mac.data[3] != (unsigned char)((_a >> 16) & 0xff)) - return false; - if (mac.data[4] != (unsigned char)((_a >> 8) & 0xff)) - return false; - if (mac.data[5] != (unsigned char)(_a & 0xff)) - return false; - return true; + return ((mac.data[0] != ZT_MAC_FIRST_OCTET)|| + (mac.data[1] != (unsigned char)((_a >> 32) & 0xff))|| + (mac.data[2] != (unsigned char)((_a >> 24) & 0xff))|| + (mac.data[3] != (unsigned char)((_a >> 16) & 0xff))|| + (mac.data[4] != (unsigned char)((_a >> 8) & 0xff))|| + (mac.data[5] != (unsigned char)(_a & 0xff))); } /** @@ -234,11 +214,7 @@ public: /** * Set to null/zero */ - inline void zero() - throw() - { - _a = 0; - } + inline void zero() throw() { _a = 0; } /** * Check if this address is reserved diff --git a/node/Buffer.hpp b/node/Buffer.hpp index 73d0e5de..1767ae04 100644 --- a/node/Buffer.hpp +++ b/node/Buffer.hpp @@ -301,6 +301,25 @@ public: append(b._b,b._l); } + /** + * Increment size and return pointer to field of specified size + * + * The memory isn't actually written, so this is a shortcut for a multi-step + * process involving getting the current pointer and adding size. + * + * @param l Length of field to append + * @return Pointer to beginning of appended field of length 'l' + */ + inline char *appendField(unsigned int l) + throw(std::out_of_range) + { + if ((_l + l) > C) + throw std::out_of_range("Buffer: append beyond capacity"); + char *r = _b + _l; + _l += l; + return r; + } + /** * Increment size by a given number of bytes * diff --git a/node/Defaults.cpp b/node/Defaults.cpp index 8e3a8c9f..0a0003b0 100644 --- a/node/Defaults.cpp +++ b/node/Defaults.cpp @@ -43,7 +43,6 @@ namespace ZeroTier { const Defaults ZT_DEFAULTS; static inline std::map< Identity,std::vector<InetAddress> > _mkSupernodeMap() - throw(std::runtime_error) { std::map< Identity,std::vector<InetAddress> > sn; Identity id; @@ -99,8 +98,7 @@ static inline std::string _mkDefaultHomePath() #endif } -Defaults::Defaults() - throw(std::runtime_error) : +Defaults::Defaults() : #ifdef ZT_TRACE_MULTICAST multicastTraceWatcher(ZT_TRACE_MULTICAST), #endif diff --git a/node/Defaults.hpp b/node/Defaults.hpp index 4937ec03..5f870194 100644 --- a/node/Defaults.hpp +++ b/node/Defaults.hpp @@ -49,9 +49,7 @@ namespace ZeroTier { class Defaults { public: - Defaults() - throw(std::runtime_error); - ~Defaults() {} + Defaults(); #ifdef ZT_TRACE_MULTICAST /** diff --git a/node/Demarc.cpp b/node/Demarc.cpp index e607abda..71606074 100644 --- a/node/Demarc.cpp +++ b/node/Demarc.cpp @@ -94,7 +94,6 @@ bool Demarc::has(Port p) const } bool Demarc::bindLocalUdp(unsigned int localPort) - throw() { Mutex::Lock _l(_ports_m); diff --git a/node/Demarc.hpp b/node/Demarc.hpp index 767cc864..fc283fef 100644 --- a/node/Demarc.hpp +++ b/node/Demarc.hpp @@ -100,8 +100,7 @@ public: * @param localPort Local IP port * @return True if successfully bound, or if already bound */ - bool bindLocalUdp(unsigned int localPort) - throw(); + bool bindLocalUdp(unsigned int localPort); /** * Pick a port to send to an address of a given type diff --git a/node/NodeConfig.cpp b/node/NodeConfig.cpp index afdf75d9..0dda8da7 100644 --- a/node/NodeConfig.cpp +++ b/node/NodeConfig.cpp @@ -58,8 +58,7 @@ namespace ZeroTier { -NodeConfig::NodeConfig(const RuntimeEnvironment *renv,const char *authToken,unsigned int controlPort) - throw(std::runtime_error) : +NodeConfig::NodeConfig(const RuntimeEnvironment *renv,const char *authToken,unsigned int controlPort) : _r(renv), _controlSocket(true,controlPort,false,&_CBcontrolPacketHandler,this) { @@ -266,7 +265,6 @@ std::vector<std::string> NodeConfig::execute(const char *command) } std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > NodeConfig::encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload) - throw(std::out_of_range) { char poly1305tag[ZT_POLY1305_MAC_LEN]; char iv[8]; diff --git a/node/NodeConfig.hpp b/node/NodeConfig.hpp index 12bc32b3..0e7e4c98 100644 --- a/node/NodeConfig.hpp +++ b/node/NodeConfig.hpp @@ -63,8 +63,7 @@ public: * @param controlPort Control port for local control packet I/O * @throws std::runtime_error Unable to bind to local control port */ - NodeConfig(const RuntimeEnvironment *renv,const char *authToken,unsigned int controlPort) - throw(std::runtime_error); + NodeConfig(const RuntimeEnvironment *renv,const char *authToken,unsigned int controlPort); ~NodeConfig(); @@ -143,8 +142,7 @@ public: * @return One or more transport armored packets (if payload too big) * @throws std::out_of_range An element of payload is too big */ - static std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload) - throw(std::out_of_range); + static std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > encodeControlMessage(const void *key,unsigned long conversationId,const std::vector<std::string> &payload); /** * Decode a packet from the control bus diff --git a/node/PacketDecoder.cpp b/node/PacketDecoder.cpp index 37b46a67..c23fab1d 100644 --- a/node/PacketDecoder.cpp +++ b/node/PacketDecoder.cpp @@ -45,7 +45,6 @@ namespace ZeroTier { bool PacketDecoder::tryDecode(const RuntimeEnvironment *_r) - throw(std::out_of_range,std::runtime_error) { if ((!encrypted())&&(verb() == Packet::VERB_HELLO)) { // Unencrypted HELLOs are handled here since they are used to diff --git a/node/PacketDecoder.hpp b/node/PacketDecoder.hpp index e5035317..cb3522ff 100644 --- a/node/PacketDecoder.hpp +++ b/node/PacketDecoder.hpp @@ -100,8 +100,7 @@ public: * @throws std::out_of_range Range error processing packet (should be discarded) * @throws std::runtime_error Other error processing packet (should be discarded) */ - bool tryDecode(const RuntimeEnvironment *_r) - throw(std::out_of_range,std::runtime_error); + bool tryDecode(const RuntimeEnvironment *_r); /** * @return Time of packet receipt / start of decode diff --git a/node/Range.hpp b/node/Range.hpp deleted file mode 100644 index 7e3785b5..00000000 --- a/node/Range.hpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * 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_RANGE_HPP -#define _ZT_RANGE_HPP - -namespace ZeroTier { - -/** - * A range of numeric values - * - * @tparam T Type, can be any numeric value (int, float, double, etc.) - */ -template<typename T> -class Range -{ -public: - /** - * Construct an empty range - */ - Range() - throw() : - start(0), - end(0) - { - } - - /** - * @param s Starting value (inclusive) - * @param e Ending value (exclusive) - */ - Range(T s,T e) - throw() : - start(s), - end(e) - { - } - - /** - * Construct a range containing from n to n+1 (thus only n for integers) - * - * @param n Number to contain - */ - Range(T n) - throw() : - start(n), - end(n+1) - { - } - - /** - * @return end - start - */ - inline T magnitude() const - throw() - { - return (end - start); - } - - /** - * @return True if range contains something (magnitude is nonzero) - */ - inline operator bool() const - throw() - { - return (end > start); - } - - /** - * @param v Value to test - * @return True if value is between start (inclusive) and end (exclusive) - */ - inline bool operator()(const T &v) const - throw() - { - return ((v >= start)&&(v < end)); - } - - inline bool operator==(const Range &r) const throw() { return ((start == r.start)&&(end == r.end)); } - inline bool operator!=(const Range &r) const throw() { return (!(*this == r)); } - inline bool operator<(const Range &r) const throw() { return ((start < r.start) ? true : ((start == r.start) ? (end < r.end) : false)); } - inline bool operator>(const Range &r) const throw() { return (r < *this); } - inline bool operator<=(const Range &r) const throw() { return !(r < *this); } - inline bool operator>=(const Range &r) const throw() { return !(*this < r); } - - /** - * Start of range (may be modified directly) - */ - T start; - - /** - * End of range (may be modified directly) - */ - T end; -}; - -} // namespace ZeroTier - -#endif diff --git a/node/SysEnv.cpp b/node/SysEnv.cpp index 150b26a8..3fb2c872 100644 --- a/node/SysEnv.cpp +++ b/node/SysEnv.cpp @@ -74,7 +74,6 @@ SysEnv::~SysEnv() #ifdef __APPLE__ uint64_t SysEnv::getNetworkConfigurationFingerprint() - throw() { int mib[6]; size_t needed; @@ -141,7 +140,6 @@ uint64_t SysEnv::getNetworkConfigurationFingerprint() #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux) uint64_t SysEnv::getNetworkConfigurationFingerprint() - throw() { char buf[16384]; uint64_t fingerprint = 5381; // djb2 hash algorithm is used below @@ -218,7 +216,6 @@ uint64_t SysEnv::getNetworkConfigurationFingerprint() #ifdef __WINDOWS__ uint64_t SysEnv::getNetworkConfigurationFingerprint() - throw() { // TODO: windows version return 1; diff --git a/node/SysEnv.hpp b/node/SysEnv.hpp index 2db275c3..21c25713 100644 --- a/node/SysEnv.hpp +++ b/node/SysEnv.hpp @@ -48,8 +48,7 @@ public: /** * @return Fingerprint of currently running network environment */ - uint64_t getNetworkConfigurationFingerprint() - throw(); + uint64_t getNetworkConfigurationFingerprint(); private: const RuntimeEnvironment *_r; diff --git a/node/Topology.cpp b/node/Topology.cpp index 8acbb027..3414812a 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -37,8 +37,7 @@ namespace ZeroTier { #define ZT_KISSDB_KEY_SIZE ZT_ADDRESS_LENGTH #define ZT_KISSDB_VALUE_SIZE ZT_PEER_MAX_SERIALIZED_LENGTH -Topology::Topology(const RuntimeEnvironment *renv,const char *dbpath) - throw(std::runtime_error) : +Topology::Topology(const RuntimeEnvironment *renv,const char *dbpath) : _r(renv), _amSupernode(false) { diff --git a/node/Topology.hpp b/node/Topology.hpp index 637cbb36..acae83d6 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -55,9 +55,7 @@ class RuntimeEnvironment; class Topology { public: - Topology(const RuntimeEnvironment *renv,const char *dbpath) - throw(std::runtime_error); - + Topology(const RuntimeEnvironment *renv,const char *dbpath); ~Topology(); /** -- cgit v1.2.3