From e2f783ebbd39466bc03bf115b20064d222b91944 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 5 Aug 2016 15:02:01 -0700 Subject: . --- attic/LockingPtr.hpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 attic/LockingPtr.hpp (limited to 'attic') diff --git a/attic/LockingPtr.hpp b/attic/LockingPtr.hpp new file mode 100644 index 00000000..c373129a --- /dev/null +++ b/attic/LockingPtr.hpp @@ -0,0 +1,99 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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_LOCKINGPTR_HPP +#define ZT_LOCKINGPTR_HPP + +#include "Mutex.hpp" + +namespace ZeroTier { + +/** + * A simple pointer that locks and holds a mutex until destroyed + * + * Care must be taken when using this. It's not very sophisticated and does + * not handle being copied except for the simple return use case. When it is + * copied it hands off the mutex to the copy and clears it in the original, + * meaning that the mutex is unlocked when the last LockingPtr<> in a chain + * of such handoffs is destroyed. If this chain of handoffs "forks" (more than + * one copy is made) then non-determinism may ensue. + * + * This does not delete or do anything else with the pointer. It also does not + * take care of locking the lock. That must be done beforehand. + */ +template +class LockingPtr +{ +public: + LockingPtr() : + _ptr((T *)0), + _lock((Mutex *)0) + { + } + + LockingPtr(T *obj,Mutex *lock) : + _ptr(obj), + _lock(lock) + { + } + + LockingPtr(const LockingPtr &p) : + _ptr(p._ptr), + _lock(p._lock) + { + const_cast(&p)->_lock = (Mutex *)0; + } + + ~LockingPtr() + { + if (_lock) + _lock->unlock(); + } + + inline LockingPtr &operator=(const LockingPtr &p) + { + _ptr = p._ptr; + _lock = p._lock; + const_cast(&p)->_lock = (Mutex *)0; + return *this; + } + + inline operator bool() const throw() { return (_ptr != (T *)0); } + inline T &operator*() const throw() { return *_ptr; } + inline T *operator->() const throw() { return _ptr; } + + /** + * @return Raw pointer to held object + */ + inline T *ptr() const throw() { return _ptr; } + + inline bool operator==(const LockingPtr &sp) const throw() { return (_ptr == sp._ptr); } + inline bool operator!=(const LockingPtr &sp) const throw() { return (_ptr != sp._ptr); } + inline bool operator>(const LockingPtr &sp) const throw() { return (_ptr > sp._ptr); } + inline bool operator<(const LockingPtr &sp) const throw() { return (_ptr < sp._ptr); } + inline bool operator>=(const LockingPtr &sp) const throw() { return (_ptr >= sp._ptr); } + inline bool operator<=(const LockingPtr &sp) const throw() { return (_ptr <= sp._ptr); } + +private: + T *_ptr; + Mutex *_lock; +}; + +} // namespace ZeroTier + +#endif -- cgit v1.2.3 From 51cf49a24fa5e953de0192006c624675090483f5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 8 Aug 2016 17:40:22 -0700 Subject: cleanup --- attic/BinarySemaphore.hpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++ node/BinarySemaphore.hpp | 97 ----------------------------------------------- 2 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 attic/BinarySemaphore.hpp delete mode 100644 node/BinarySemaphore.hpp (limited to 'attic') diff --git a/attic/BinarySemaphore.hpp b/attic/BinarySemaphore.hpp new file mode 100644 index 00000000..315d2b00 --- /dev/null +++ b/attic/BinarySemaphore.hpp @@ -0,0 +1,97 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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_BINARYSEMAPHORE_HPP +#define ZT_BINARYSEMAPHORE_HPP + +#include +#include +#include + +#include "Constants.hpp" +#include "NonCopyable.hpp" + +#ifdef __WINDOWS__ + +#include + +namespace ZeroTier { + +class BinarySemaphore : NonCopyable +{ +public: + BinarySemaphore() throw() { _sem = CreateSemaphore(NULL,0,1,NULL); } + ~BinarySemaphore() { CloseHandle(_sem); } + inline void wait() { WaitForSingleObject(_sem,INFINITE); } + inline void post() { ReleaseSemaphore(_sem,1,NULL); } +private: + HANDLE _sem; +}; + +} // namespace ZeroTier + +#else // !__WINDOWS__ + +#include + +namespace ZeroTier { + +class BinarySemaphore : NonCopyable +{ +public: + BinarySemaphore() + { + pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); + pthread_cond_init(&_cond,(const pthread_condattr_t *)0); + _f = false; + } + + ~BinarySemaphore() + { + pthread_cond_destroy(&_cond); + pthread_mutex_destroy(&_mh); + } + + inline void wait() + { + pthread_mutex_lock(const_cast (&_mh)); + while (!_f) + pthread_cond_wait(const_cast (&_cond),const_cast (&_mh)); + _f = false; + pthread_mutex_unlock(const_cast (&_mh)); + } + + inline void post() + { + pthread_mutex_lock(const_cast (&_mh)); + _f = true; + pthread_mutex_unlock(const_cast (&_mh)); + pthread_cond_signal(const_cast (&_cond)); + } + +private: + pthread_cond_t _cond; + pthread_mutex_t _mh; + volatile bool _f; +}; + +} // namespace ZeroTier + +#endif // !__WINDOWS__ + +#endif diff --git a/node/BinarySemaphore.hpp b/node/BinarySemaphore.hpp deleted file mode 100644 index 315d2b00..00000000 --- a/node/BinarySemaphore.hpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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_BINARYSEMAPHORE_HPP -#define ZT_BINARYSEMAPHORE_HPP - -#include -#include -#include - -#include "Constants.hpp" -#include "NonCopyable.hpp" - -#ifdef __WINDOWS__ - -#include - -namespace ZeroTier { - -class BinarySemaphore : NonCopyable -{ -public: - BinarySemaphore() throw() { _sem = CreateSemaphore(NULL,0,1,NULL); } - ~BinarySemaphore() { CloseHandle(_sem); } - inline void wait() { WaitForSingleObject(_sem,INFINITE); } - inline void post() { ReleaseSemaphore(_sem,1,NULL); } -private: - HANDLE _sem; -}; - -} // namespace ZeroTier - -#else // !__WINDOWS__ - -#include - -namespace ZeroTier { - -class BinarySemaphore : NonCopyable -{ -public: - BinarySemaphore() - { - pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); - pthread_cond_init(&_cond,(const pthread_condattr_t *)0); - _f = false; - } - - ~BinarySemaphore() - { - pthread_cond_destroy(&_cond); - pthread_mutex_destroy(&_mh); - } - - inline void wait() - { - pthread_mutex_lock(const_cast (&_mh)); - while (!_f) - pthread_cond_wait(const_cast (&_cond),const_cast (&_mh)); - _f = false; - pthread_mutex_unlock(const_cast (&_mh)); - } - - inline void post() - { - pthread_mutex_lock(const_cast (&_mh)); - _f = true; - pthread_mutex_unlock(const_cast (&_mh)); - pthread_cond_signal(const_cast (&_cond)); - } - -private: - pthread_cond_t _cond; - pthread_mutex_t _mh; - volatile bool _f; -}; - -} // namespace ZeroTier - -#endif // !__WINDOWS__ - -#endif -- cgit v1.2.3 From 58701c1ca8adc774dd1a6d179d8431579a59f85b Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 16 Aug 2016 14:08:08 -0700 Subject: . --- attic/old-controller-schema.sql | 112 +++++++++++++++++++++++++++++++++ controller/SqliteNetworkController.cpp | 7 --- controller/schema.sql | 112 --------------------------------- controller/schema.sql.c | 112 --------------------------------- controller/schema2c.sh | 8 --- 5 files changed, 112 insertions(+), 239 deletions(-) create mode 100644 attic/old-controller-schema.sql delete mode 100644 controller/schema.sql delete mode 100644 controller/schema.sql.c delete mode 100755 controller/schema2c.sh (limited to 'attic') diff --git a/attic/old-controller-schema.sql b/attic/old-controller-schema.sql new file mode 100644 index 00000000..d1daf8d0 --- /dev/null +++ b/attic/old-controller-schema.sql @@ -0,0 +1,112 @@ +CREATE TABLE Config ( + k varchar(16) PRIMARY KEY NOT NULL, + v varchar(1024) NOT NULL +); + +CREATE TABLE Network ( + id char(16) PRIMARY KEY NOT NULL, + name varchar(128) NOT NULL, + private integer NOT NULL DEFAULT(1), + enableBroadcast integer NOT NULL DEFAULT(1), + allowPassiveBridging integer NOT NULL DEFAULT(0), + multicastLimit integer NOT NULL DEFAULT(32), + creationTime integer NOT NULL DEFAULT(0), + revision integer NOT NULL DEFAULT(1), + memberRevisionCounter integer NOT NULL DEFAULT(1), + flags integer NOT NULL DEFAULT(0) +); + +CREATE TABLE AuthToken ( + id integer PRIMARY KEY NOT NULL, + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + authMode integer NOT NULL DEFAULT(1), + useCount integer NOT NULL DEFAULT(0), + maxUses integer NOT NULL DEFAULT(0), + expiresAt integer NOT NULL DEFAULT(0), + token varchar(256) NOT NULL +); + +CREATE INDEX AuthToken_networkId_token ON AuthToken(networkId,token); + +CREATE TABLE Node ( + id char(10) PRIMARY KEY NOT NULL, + identity varchar(4096) NOT NULL +); + +CREATE TABLE IpAssignment ( + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + nodeId char(10) REFERENCES Node(id) ON DELETE CASCADE, + type integer NOT NULL DEFAULT(0), + ip blob(16) NOT NULL, + ipNetmaskBits integer NOT NULL DEFAULT(0), + ipVersion integer NOT NULL DEFAULT(4) +); + +CREATE UNIQUE INDEX IpAssignment_networkId_ip ON IpAssignment (networkId, ip); + +CREATE INDEX IpAssignment_networkId_nodeId ON IpAssignment (networkId, nodeId); + +CREATE TABLE IpAssignmentPool ( + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + ipRangeStart blob(16) NOT NULL, + ipRangeEnd blob(16) NOT NULL, + ipVersion integer NOT NULL DEFAULT(4) +); + +CREATE UNIQUE INDEX IpAssignmentPool_networkId_ipRangeStart ON IpAssignmentPool (networkId,ipRangeStart); + +CREATE TABLE Member ( + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE, + authorized integer NOT NULL DEFAULT(0), + activeBridge integer NOT NULL DEFAULT(0), + memberRevision integer NOT NULL DEFAULT(0), + flags integer NOT NULL DEFAULT(0), + lastRequestTime integer NOT NULL DEFAULT(0), + lastPowDifficulty integer NOT NULL DEFAULT(0), + lastPowTime integer NOT NULL DEFAULT(0), + recentHistory blob, + PRIMARY KEY (networkId, nodeId) +); + +CREATE INDEX Member_networkId_nodeId ON Member(networkId,nodeId); +CREATE INDEX Member_networkId_activeBridge ON Member(networkId, activeBridge); +CREATE INDEX Member_networkId_memberRevision ON Member(networkId, memberRevision); +CREATE INDEX Member_networkId_lastRequestTime ON Member(networkId, lastRequestTime); + +CREATE TABLE Route ( + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + target blob(16) NOT NULL, + via blob(16), + targetNetmaskBits integer NOT NULL, + ipVersion integer NOT NULL, + flags integer NOT NULL, + metric integer NOT NULL +); + +CREATE INDEX Route_networkId ON Route (networkId); + +CREATE TABLE Rule ( + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + capId integer, + ruleNo integer NOT NULL, + ruleType integer NOT NULL DEFAULT(0), + "addr" blob(16), + "int1" integer, + "int2" integer, + "int3" integer, + "int4" integer +); + +CREATE INDEX Rule_networkId_capId ON Rule (networkId,capId); + +CREATE TABLE MemberTC ( + networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, + nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE, + tagId integer, + tagValue integer, + capId integer, + capMaxCustodyChainLength integer NOT NULL DEFAULT(1) +); + +CREATE INDEX MemberTC_networkId_nodeId ON MemberTC (networkId,nodeId); diff --git a/controller/SqliteNetworkController.cpp b/controller/SqliteNetworkController.cpp index 94d2e2e6..cbfa8891 100644 --- a/controller/SqliteNetworkController.cpp +++ b/controller/SqliteNetworkController.cpp @@ -53,15 +53,8 @@ #include "../node/MAC.hpp" #include "../node/Address.hpp" -// offbase includes and builds upon nlohmann::json using json = nlohmann::json; -// Stored in database as schemaVersion key in Config. -// If not present, database is assumed to be empty and at the current schema version -// and this key/value is added automatically. -//#define ZT_NETCONF_SQLITE_SCHEMA_VERSION 5 -//#define ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR "5" - // API version reported via JSON control plane #define ZT_NETCONF_CONTROLLER_API_VERSION 3 diff --git a/controller/schema.sql b/controller/schema.sql deleted file mode 100644 index d1daf8d0..00000000 --- a/controller/schema.sql +++ /dev/null @@ -1,112 +0,0 @@ -CREATE TABLE Config ( - k varchar(16) PRIMARY KEY NOT NULL, - v varchar(1024) NOT NULL -); - -CREATE TABLE Network ( - id char(16) PRIMARY KEY NOT NULL, - name varchar(128) NOT NULL, - private integer NOT NULL DEFAULT(1), - enableBroadcast integer NOT NULL DEFAULT(1), - allowPassiveBridging integer NOT NULL DEFAULT(0), - multicastLimit integer NOT NULL DEFAULT(32), - creationTime integer NOT NULL DEFAULT(0), - revision integer NOT NULL DEFAULT(1), - memberRevisionCounter integer NOT NULL DEFAULT(1), - flags integer NOT NULL DEFAULT(0) -); - -CREATE TABLE AuthToken ( - id integer PRIMARY KEY NOT NULL, - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - authMode integer NOT NULL DEFAULT(1), - useCount integer NOT NULL DEFAULT(0), - maxUses integer NOT NULL DEFAULT(0), - expiresAt integer NOT NULL DEFAULT(0), - token varchar(256) NOT NULL -); - -CREATE INDEX AuthToken_networkId_token ON AuthToken(networkId,token); - -CREATE TABLE Node ( - id char(10) PRIMARY KEY NOT NULL, - identity varchar(4096) NOT NULL -); - -CREATE TABLE IpAssignment ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - nodeId char(10) REFERENCES Node(id) ON DELETE CASCADE, - type integer NOT NULL DEFAULT(0), - ip blob(16) NOT NULL, - ipNetmaskBits integer NOT NULL DEFAULT(0), - ipVersion integer NOT NULL DEFAULT(4) -); - -CREATE UNIQUE INDEX IpAssignment_networkId_ip ON IpAssignment (networkId, ip); - -CREATE INDEX IpAssignment_networkId_nodeId ON IpAssignment (networkId, nodeId); - -CREATE TABLE IpAssignmentPool ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - ipRangeStart blob(16) NOT NULL, - ipRangeEnd blob(16) NOT NULL, - ipVersion integer NOT NULL DEFAULT(4) -); - -CREATE UNIQUE INDEX IpAssignmentPool_networkId_ipRangeStart ON IpAssignmentPool (networkId,ipRangeStart); - -CREATE TABLE Member ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE, - authorized integer NOT NULL DEFAULT(0), - activeBridge integer NOT NULL DEFAULT(0), - memberRevision integer NOT NULL DEFAULT(0), - flags integer NOT NULL DEFAULT(0), - lastRequestTime integer NOT NULL DEFAULT(0), - lastPowDifficulty integer NOT NULL DEFAULT(0), - lastPowTime integer NOT NULL DEFAULT(0), - recentHistory blob, - PRIMARY KEY (networkId, nodeId) -); - -CREATE INDEX Member_networkId_nodeId ON Member(networkId,nodeId); -CREATE INDEX Member_networkId_activeBridge ON Member(networkId, activeBridge); -CREATE INDEX Member_networkId_memberRevision ON Member(networkId, memberRevision); -CREATE INDEX Member_networkId_lastRequestTime ON Member(networkId, lastRequestTime); - -CREATE TABLE Route ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - target blob(16) NOT NULL, - via blob(16), - targetNetmaskBits integer NOT NULL, - ipVersion integer NOT NULL, - flags integer NOT NULL, - metric integer NOT NULL -); - -CREATE INDEX Route_networkId ON Route (networkId); - -CREATE TABLE Rule ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - capId integer, - ruleNo integer NOT NULL, - ruleType integer NOT NULL DEFAULT(0), - "addr" blob(16), - "int1" integer, - "int2" integer, - "int3" integer, - "int4" integer -); - -CREATE INDEX Rule_networkId_capId ON Rule (networkId,capId); - -CREATE TABLE MemberTC ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE, - tagId integer, - tagValue integer, - capId integer, - capMaxCustodyChainLength integer NOT NULL DEFAULT(1) -); - -CREATE INDEX MemberTC_networkId_nodeId ON MemberTC (networkId,nodeId); diff --git a/controller/schema.sql.c b/controller/schema.sql.c deleted file mode 100644 index e84ee766..00000000 --- a/controller/schema.sql.c +++ /dev/null @@ -1,112 +0,0 @@ -#define ZT_NETCONF_SCHEMA_SQL \ -"CREATE TABLE Config (\n"\ -" k varchar(16) PRIMARY KEY NOT NULL,\n"\ -" v varchar(1024) NOT NULL\n"\ -");\n"\ -"\n"\ -"CREATE TABLE Network (\n"\ -" id char(16) PRIMARY KEY NOT NULL,\n"\ -" name varchar(128) NOT NULL,\n"\ -" private integer NOT NULL DEFAULT(1),\n"\ -" enableBroadcast integer NOT NULL DEFAULT(1),\n"\ -" allowPassiveBridging integer NOT NULL DEFAULT(0),\n"\ -" multicastLimit integer NOT NULL DEFAULT(32),\n"\ -" creationTime integer NOT NULL DEFAULT(0),\n"\ -" revision integer NOT NULL DEFAULT(1),\n"\ -" memberRevisionCounter integer NOT NULL DEFAULT(1),\n"\ -" flags integer NOT NULL DEFAULT(0)\n"\ -");\n"\ -"\n"\ -"CREATE TABLE AuthToken (\n"\ -" id integer PRIMARY KEY NOT NULL,\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" authMode integer NOT NULL DEFAULT(1),\n"\ -" useCount integer NOT NULL DEFAULT(0),\n"\ -" maxUses integer NOT NULL DEFAULT(0),\n"\ -" expiresAt integer NOT NULL DEFAULT(0),\n"\ -" token varchar(256) NOT NULL\n"\ -");\n"\ -"\n"\ -"CREATE INDEX AuthToken_networkId_token ON AuthToken(networkId,token);\n"\ -"\n"\ -"CREATE TABLE Node (\n"\ -" id char(10) PRIMARY KEY NOT NULL,\n"\ -" identity varchar(4096) NOT NULL\n"\ -");\n"\ -"\n"\ -"CREATE TABLE IpAssignment (\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" nodeId char(10) REFERENCES Node(id) ON DELETE CASCADE,\n"\ -" type integer NOT NULL DEFAULT(0),\n"\ -" ip blob(16) NOT NULL,\n"\ -" ipNetmaskBits integer NOT NULL DEFAULT(0),\n"\ -" ipVersion integer NOT NULL DEFAULT(4)\n"\ -");\n"\ -"\n"\ -"CREATE UNIQUE INDEX IpAssignment_networkId_ip ON IpAssignment (networkId, ip);\n"\ -"\n"\ -"CREATE INDEX IpAssignment_networkId_nodeId ON IpAssignment (networkId, nodeId);\n"\ -"\n"\ -"CREATE TABLE IpAssignmentPool (\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" ipRangeStart blob(16) NOT NULL,\n"\ -" ipRangeEnd blob(16) NOT NULL,\n"\ -" ipVersion integer NOT NULL DEFAULT(4)\n"\ -");\n"\ -"\n"\ -"CREATE UNIQUE INDEX IpAssignmentPool_networkId_ipRangeStart ON IpAssignmentPool (networkId,ipRangeStart);\n"\ -"\n"\ -"CREATE TABLE Member (\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE,\n"\ -" authorized integer NOT NULL DEFAULT(0),\n"\ -" activeBridge integer NOT NULL DEFAULT(0),\n"\ -" memberRevision integer NOT NULL DEFAULT(0),\n"\ -" flags integer NOT NULL DEFAULT(0),\n"\ -" lastRequestTime integer NOT NULL DEFAULT(0),\n"\ -" lastPowDifficulty integer NOT NULL DEFAULT(0),\n"\ -" lastPowTime integer NOT NULL DEFAULT(0),\n"\ -" recentHistory blob,\n"\ -" PRIMARY KEY (networkId, nodeId)\n"\ -");\n"\ -"\n"\ -"CREATE INDEX Member_networkId_nodeId ON Member(networkId,nodeId);\n"\ -"CREATE INDEX Member_networkId_activeBridge ON Member(networkId, activeBridge);\n"\ -"CREATE INDEX Member_networkId_memberRevision ON Member(networkId, memberRevision);\n"\ -"CREATE INDEX Member_networkId_lastRequestTime ON Member(networkId, lastRequestTime);\n"\ -"\n"\ -"CREATE TABLE Route (\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" target blob(16) NOT NULL,\n"\ -" via blob(16),\n"\ -" targetNetmaskBits integer NOT NULL,\n"\ -" ipVersion integer NOT NULL,\n"\ -" flags integer NOT NULL,\n"\ -" metric integer NOT NULL\n"\ -");\n"\ -"\n"\ -"CREATE INDEX Route_networkId ON Route (networkId);\n"\ -"\n"\ -"CREATE TABLE Relay (\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" address char(10) NOT NULL,\n"\ -" phyAddress varchar(64) NOT NULL\n"\ -");\n"\ -"\n"\ -"CREATE UNIQUE INDEX Relay_networkId_address ON Relay (networkId,address);\n"\ -"\n"\ -"CREATE TABLE Rule (\n"\ -" networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE,\n"\ -" policyId varchar(32),\n"\ -" ruleNo integer NOT NULL,\n"\ -" ruleType integer NOT NULL DEFAULT(0),\n"\ -" \"addr\" blob(16),\n"\ -" \"int1\" integer,\n"\ -" \"int2\" integer,\n"\ -" \"int3\" integer,\n"\ -" \"int4\" integer\n"\ -");\n"\ -"\n"\ -"CREATE INDEX Rule_networkId_ruleNo ON Rule (networkId, ruleNo);\n"\ -"CREATE INDEX Rule_networkId_policyId ON Rule (networkId, policyId);\n"\ -"" diff --git a/controller/schema2c.sh b/controller/schema2c.sh deleted file mode 100755 index 4f4f1647..00000000 --- a/controller/schema2c.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -# Run this file to package the .sql file into a .c file whenever the SQL changes. - -rm -f schema.sql.c -echo '#define ZT_NETCONF_SCHEMA_SQL \' >schema.sql.c -cat schema.sql | sed 's/"/\\"/g' | sed 's/^/"/' | sed 's/$/\\n"\\/' >>schema.sql.c -echo '""' >>schema.sql.c -- cgit v1.2.3 From 236fdb450c4576dcb114a4671090d7b00a283503 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 27 Sep 2016 07:02:16 -0700 Subject: cleanup attic --- attic/Filter.cpp | 408 ------------------------------------------------------ attic/Filter.hpp | 284 ------------------------------------- attic/SECURITY.md | 84 ----------- 3 files changed, 776 deletions(-) delete mode 100644 attic/Filter.cpp delete mode 100644 attic/Filter.hpp delete mode 100644 attic/SECURITY.md (limited to 'attic') diff --git a/attic/Filter.cpp b/attic/Filter.cpp deleted file mode 100644 index a701e8b7..00000000 --- a/attic/Filter.cpp +++ /dev/null @@ -1,408 +0,0 @@ -/* - * 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 . - * - * -- - * - * 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 -#include -#include -#include - -#include - -#include "RuntimeEnvironment.hpp" -#include "Logger.hpp" -#include "Filter.hpp" -#include "Utils.hpp" - -namespace ZeroTier { - -const char *const Filter::UNKNOWN_NAME = "(unknown)"; -const Range Filter::ANY; - -static inline Range __parseRange(char *r) - throw(std::invalid_argument) -{ - char *saveptr = (char *)0; - unsigned int a = 0; - unsigned int b = 0; - unsigned int fn = 0; - for(char *f=Utils::stok(r,"-",&saveptr);(f);f=Utils::stok((char *)0,"-",&saveptr)) { - if (*f) { - switch(fn++) { - case 0: - if (*f != '*') - a = b = (unsigned int)strtoul(f,(char **)0,10); - break; - case 1: - if (*f != '*') - b = (unsigned int)strtoul(f,(char **)0,10); - break; - default: - throw std::invalid_argument("rule range must be , -, or *"); - } - } - } - return Range(a,b); -} - -Filter::Rule::Rule(const char *s) - throw(std::invalid_argument) -{ - char *saveptr = (char *)0; - char tmp[256]; - if (!Utils::scopy(tmp,sizeof(tmp),s)) - throw std::invalid_argument("rule string too long"); - unsigned int fn = 0; - for(char *f=Utils::stok(tmp,";",&saveptr);(f);f=Utils::stok((char *)0,";",&saveptr)) { - if (*f) { - switch(fn++) { - case 0: - _etherType = __parseRange(f); - break; - case 1: - _protocol = __parseRange(f); - break; - case 2: - _port = __parseRange(f); - break; - default: - throw std::invalid_argument("rule string has unknown extra fields"); - } - } - } - if (fn != 3) - throw std::invalid_argument("rule string must contain 3 fields"); -} - -bool Filter::Rule::operator()(unsigned int etype,const void *data,unsigned int len) const - throw(std::invalid_argument) -{ - if ((!_etherType)||(_etherType(etype))) { // ethertype is ANY, or matches - // Ethertype determines meaning of protocol and port - switch(etype) { - case ZT_ETHERTYPE_IPV4: - if (len > 20) { - if ((!_protocol)||(_protocol(((const uint8_t *)data)[9]))) { // protocol is ANY or match - if (!_port) // port is ANY - return true; - - // Don't match on fragments beyond fragment 0. If we've blocked - // fragment 0, further fragments will fall on deaf ears anyway. - if ((Utils::ntoh(((const uint16_t *)data)[3]) & 0x1fff)) - return false; - - // Internet header length determines where data begins, in multiples of 32 bits - unsigned int ihl = 4 * (((const uint8_t *)data)[0] & 0x0f); - - switch(((const uint8_t *)data)[9]) { // port's meaning depends on IP protocol - case ZT_IPPROTO_ICMP: - // For ICMP, port is ICMP type - return _port(((const uint8_t *)data)[ihl]); - case ZT_IPPROTO_TCP: - case ZT_IPPROTO_UDP: - case ZT_IPPROTO_SCTP: - case ZT_IPPROTO_UDPLITE: - // For these, port is destination port. Protocol designers were - // nice enough to put the field in the same place. - return _port(((const uint16_t *)data)[(ihl / 2) + 1]); - default: - // port has no meaning for other IP types, so ignore it - return true; - } - - return false; // no match on port - } - } else throw std::invalid_argument("undersized IPv4 packet"); - break; - - case ZT_ETHERTYPE_IPV6: - if (len > 40) { - int nextHeader = ((const uint8_t *)data)[6]; - unsigned int pos = 40; - while ((pos < len)&&(nextHeader >= 0)&&(nextHeader != 59)) { // 59 == no next header - fprintf(stderr,"[rule] V6: start header parse, header %.2x pos %d\n",nextHeader,pos); - - switch(nextHeader) { - case 0: // hop-by-hop options - case 60: // destination options - case 43: // routing - case 135: // mobility (mobile IPv6 options) - if (_protocol((unsigned int)nextHeader)) - return true; // match if our goal was to match any of these - nextHeader = ((const uint8_t *)data)[pos]; - pos += 8 + (8 * ((const uint8_t *)data)[pos + 1]); - break; - case 44: // fragment - if (_protocol(44)) - return true; // match if our goal was to match fragments - nextHeader = ((const uint8_t *)data)[pos]; - pos += 8; - break; - case ZT_IPPROTO_AH: // AH - return _protocol(ZT_IPPROTO_AH); // true if AH is matched protocol, otherwise false since packet will be IPsec - case ZT_IPPROTO_ESP: // ESP - return _protocol(ZT_IPPROTO_ESP); // true if ESP is matched protocol, otherwise false since packet will be IPsec - case ZT_IPPROTO_ICMPV6: - // Only match ICMPv6 if we've selected it specifically - if (_protocol(ZT_IPPROTO_ICMPV6)) { - // Port is interpreted as ICMPv6 type - if ((!_port)||(_port(((const uint8_t *)data)[pos]))) - return true; - } - break; - case ZT_IPPROTO_TCP: - case ZT_IPPROTO_UDP: - case ZT_IPPROTO_SCTP: - case ZT_IPPROTO_UDPLITE: - // If we encounter any of these, match if protocol matches or is wildcard as - // we'll consider these the "real payload" if present. - if ((!_protocol)||(_protocol(nextHeader))) { - if ((!_port)||(_port(((const uint16_t *)data)[(pos / 2) + 1]))) - return true; // protocol matches or is ANY, port is ANY or matches - } - break; - default: { - char foo[128]; - Utils::snprintf(foo,sizeof(foo),"unrecognized IPv6 header type %d",(int)nextHeader); - throw std::invalid_argument(foo); - } - } - - fprintf(stderr,"[rule] V6: end header parse, next header %.2x, new pos %d\n",nextHeader,pos); - } - } else throw std::invalid_argument("undersized IPv6 packet"); - break; - - default: - // For other ethertypes, protocol and port are ignored. What would they mean? - return true; - } - } - - return false; -} - -std::string Filter::Rule::toString() const -{ - char buf[128]; - std::string s; - - switch(_etherType.magnitude()) { - case 0: - s.push_back('*'); - break; - case 1: - Utils::snprintf(buf,sizeof(buf),"%u",_etherType.start); - s.append(buf); - break; - default: - Utils::snprintf(buf,sizeof(buf),"%u-%u",_etherType.start,_etherType.end); - s.append(buf); - break; - } - s.push_back(';'); - switch(_protocol.magnitude()) { - case 0: - s.push_back('*'); - break; - case 1: - Utils::snprintf(buf,sizeof(buf),"%u",_protocol.start); - s.append(buf); - break; - default: - Utils::snprintf(buf,sizeof(buf),"%u-%u",_protocol.start,_protocol.end); - s.append(buf); - break; - } - s.push_back(';'); - switch(_port.magnitude()) { - case 0: - s.push_back('*'); - break; - case 1: - Utils::snprintf(buf,sizeof(buf),"%u",_port.start); - s.append(buf); - break; - default: - Utils::snprintf(buf,sizeof(buf),"%u-%u",_port.start,_port.end); - s.append(buf); - break; - } - - return s; -} - -Filter::Filter(const char *s) - throw(std::invalid_argument) -{ - char tmp[16384]; - if (!Utils::scopy(tmp,sizeof(tmp),s)) - throw std::invalid_argument("filter string too long"); - char *saveptr = (char *)0; - unsigned int fn = 0; - for(char *f=Utils::stok(tmp,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) { - try { - _rules.push_back(Rule(f)); - ++fn; - } catch (std::invalid_argument &exc) { - char tmp[256]; - Utils::snprintf(tmp,sizeof(tmp),"invalid rule at index %u: %s",fn,exc.what()); - throw std::invalid_argument(tmp); - } - } - std::sort(_rules.begin(),_rules.end()); -} - -std::string Filter::toString() const -{ - std::string s; - - for(std::vector::const_iterator r(_rules.begin());r!=_rules.end();++r) { - if (s.length() > 0) - s.push_back(','); - s.append(r->toString()); - } - - return s; -} - -void Filter::add(const Rule &r) -{ - for(std::vector::iterator rr(_rules.begin());rr!=_rules.end();++rr) { - if (r == *rr) - return; - } - _rules.push_back(r); - std::sort(_rules.begin(),_rules.end()); -} - -const char *Filter::etherTypeName(const unsigned int etherType) - throw() -{ - switch(etherType) { - case ZT_ETHERTYPE_IPV4: return "ETHERTYPE_IPV4"; - case ZT_ETHERTYPE_ARP: return "ETHERTYPE_ARP"; - case ZT_ETHERTYPE_RARP: return "ETHERTYPE_RARP"; - case ZT_ETHERTYPE_ATALK: return "ETHERTYPE_ATALK"; - case ZT_ETHERTYPE_AARP: return "ETHERTYPE_AARP"; - case ZT_ETHERTYPE_IPX_A: return "ETHERTYPE_IPX_A"; - case ZT_ETHERTYPE_IPX_B: return "ETHERTYPE_IPX_B"; - case ZT_ETHERTYPE_IPV6: return "ETHERTYPE_IPV6"; - } - return UNKNOWN_NAME; -} - -const char *Filter::ipProtocolName(const unsigned int ipp) - throw() -{ - switch(ipp) { - case ZT_IPPROTO_ICMP: return "IPPROTO_ICMP"; - case ZT_IPPROTO_IGMP: return "IPPROTO_IGMP"; - case ZT_IPPROTO_TCP: return "IPPROTO_TCP"; - case ZT_IPPROTO_UDP: return "IPPROTO_UDP"; - case ZT_IPPROTO_GRE: return "IPPROTO_GRE"; - case ZT_IPPROTO_ESP: return "IPPROTO_ESP"; - case ZT_IPPROTO_AH: return "IPPROTO_AH"; - case ZT_IPPROTO_ICMPV6: return "IPPROTO_ICMPV6"; - case ZT_IPPROTO_OSPF: return "IPPROTO_OSPF"; - case ZT_IPPROTO_IPIP: return "IPPROTO_IPIP"; - case ZT_IPPROTO_IPCOMP: return "IPPROTO_IPCOMP"; - case ZT_IPPROTO_L2TP: return "IPPROTO_L2TP"; - case ZT_IPPROTO_SCTP: return "IPPROTO_SCTP"; - case ZT_IPPROTO_FC: return "IPPROTO_FC"; - case ZT_IPPROTO_UDPLITE: return "IPPROTO_UDPLITE"; - case ZT_IPPROTO_HIP: return "IPPROTO_HIP"; - } - return UNKNOWN_NAME; -} - -const char *Filter::icmpTypeName(const unsigned int icmpType) - throw() -{ - switch(icmpType) { - case ZT_ICMP_ECHO_REPLY: return "ICMP_ECHO_REPLY"; - case ZT_ICMP_DESTINATION_UNREACHABLE: return "ICMP_DESTINATION_UNREACHABLE"; - case ZT_ICMP_SOURCE_QUENCH: return "ICMP_SOURCE_QUENCH"; - case ZT_ICMP_REDIRECT: return "ICMP_REDIRECT"; - case ZT_ICMP_ALTERNATE_HOST_ADDRESS: return "ICMP_ALTERNATE_HOST_ADDRESS"; - case ZT_ICMP_ECHO_REQUEST: return "ICMP_ECHO_REQUEST"; - case ZT_ICMP_ROUTER_ADVERTISEMENT: return "ICMP_ROUTER_ADVERTISEMENT"; - case ZT_ICMP_ROUTER_SOLICITATION: return "ICMP_ROUTER_SOLICITATION"; - case ZT_ICMP_TIME_EXCEEDED: return "ICMP_TIME_EXCEEDED"; - case ZT_ICMP_BAD_IP_HEADER: return "ICMP_BAD_IP_HEADER"; - case ZT_ICMP_TIMESTAMP: return "ICMP_TIMESTAMP"; - case ZT_ICMP_TIMESTAMP_REPLY: return "ICMP_TIMESTAMP_REPLY"; - case ZT_ICMP_INFORMATION_REQUEST: return "ICMP_INFORMATION_REQUEST"; - case ZT_ICMP_INFORMATION_REPLY: return "ICMP_INFORMATION_REPLY"; - case ZT_ICMP_ADDRESS_MASK_REQUEST: return "ICMP_ADDRESS_MASK_REQUEST"; - case ZT_ICMP_ADDRESS_MASK_REPLY: return "ICMP_ADDRESS_MASK_REPLY"; - case ZT_ICMP_TRACEROUTE: return "ICMP_TRACEROUTE"; - case ZT_ICMP_MOBILE_HOST_REDIRECT: return "ICMP_MOBILE_HOST_REDIRECT"; - case ZT_ICMP_MOBILE_REGISTRATION_REQUEST: return "ICMP_MOBILE_REGISTRATION_REQUEST"; - case ZT_ICMP_MOBILE_REGISTRATION_REPLY: return "ICMP_MOBILE_REGISTRATION_REPLY"; - } - return UNKNOWN_NAME; -} - -const char *Filter::icmp6TypeName(const unsigned int icmp6Type) - throw() -{ - switch(icmp6Type) { - case ZT_ICMP6_DESTINATION_UNREACHABLE: return "ICMP6_DESTINATION_UNREACHABLE"; - case ZT_ICMP6_PACKET_TOO_BIG: return "ICMP6_PACKET_TOO_BIG"; - case ZT_ICMP6_TIME_EXCEEDED: return "ICMP6_TIME_EXCEEDED"; - case ZT_ICMP6_PARAMETER_PROBLEM: return "ICMP6_PARAMETER_PROBLEM"; - case ZT_ICMP6_ECHO_REQUEST: return "ICMP6_ECHO_REQUEST"; - case ZT_ICMP6_ECHO_REPLY: return "ICMP6_ECHO_REPLY"; - case ZT_ICMP6_MULTICAST_LISTENER_QUERY: return "ICMP6_MULTICAST_LISTENER_QUERY"; - case ZT_ICMP6_MULTICAST_LISTENER_REPORT: return "ICMP6_MULTICAST_LISTENER_REPORT"; - case ZT_ICMP6_MULTICAST_LISTENER_DONE: return "ICMP6_MULTICAST_LISTENER_DONE"; - case ZT_ICMP6_ROUTER_SOLICITATION: return "ICMP6_ROUTER_SOLICITATION"; - case ZT_ICMP6_ROUTER_ADVERTISEMENT: return "ICMP6_ROUTER_ADVERTISEMENT"; - case ZT_ICMP6_NEIGHBOR_SOLICITATION: return "ICMP6_NEIGHBOR_SOLICITATION"; - case ZT_ICMP6_NEIGHBOR_ADVERTISEMENT: return "ICMP6_NEIGHBOR_ADVERTISEMENT"; - case ZT_ICMP6_REDIRECT_MESSAGE: return "ICMP6_REDIRECT_MESSAGE"; - case ZT_ICMP6_ROUTER_RENUMBERING: return "ICMP6_ROUTER_RENUMBERING"; - case ZT_ICMP6_NODE_INFORMATION_QUERY: return "ICMP6_NODE_INFORMATION_QUERY"; - case ZT_ICMP6_NODE_INFORMATION_RESPONSE: return "ICMP6_NODE_INFORMATION_RESPONSE"; - case ZT_ICMP6_INV_NEIGHBOR_SOLICITATION: return "ICMP6_INV_NEIGHBOR_SOLICITATION"; - case ZT_ICMP6_INV_NEIGHBOR_ADVERTISEMENT: return "ICMP6_INV_NEIGHBOR_ADVERTISEMENT"; - case ZT_ICMP6_MLDV2: return "ICMP6_MLDV2"; - case ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST: return "ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST"; - case ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY: return "ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY"; - case ZT_ICMP6_MOBILE_PREFIX_SOLICITATION: return "ICMP6_MOBILE_PREFIX_SOLICITATION"; - case ZT_ICMP6_MOBILE_PREFIX_ADVERTISEMENT: return "ICMP6_MOBILE_PREFIX_ADVERTISEMENT"; - case ZT_ICMP6_CERTIFICATION_PATH_SOLICITATION: return "ICMP6_CERTIFICATION_PATH_SOLICITATION"; - case ZT_ICMP6_CERTIFICATION_PATH_ADVERTISEMENT: return "ICMP6_CERTIFICATION_PATH_ADVERTISEMENT"; - case ZT_ICMP6_MULTICAST_ROUTER_ADVERTISEMENT: return "ICMP6_MULTICAST_ROUTER_ADVERTISEMENT"; - case ZT_ICMP6_MULTICAST_ROUTER_SOLICITATION: return "ICMP6_MULTICAST_ROUTER_SOLICITATION"; - case ZT_ICMP6_MULTICAST_ROUTER_TERMINATION: return "ICMP6_MULTICAST_ROUTER_TERMINATION"; - case ZT_ICMP6_RPL_CONTROL_MESSAGE: return "ICMP6_RPL_CONTROL_MESSAGE"; - } - return UNKNOWN_NAME; -} - -} // namespace ZeroTier diff --git a/attic/Filter.hpp b/attic/Filter.hpp deleted file mode 100644 index 4bea3715..00000000 --- a/attic/Filter.hpp +++ /dev/null @@ -1,284 +0,0 @@ -/* - * 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 . - * - * -- - * - * 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_FILTER_HPP -#define _ZT_FILTER_HPP - -#include -#include -#include - -#include -#include -#include -#include - -#include "Range.hpp" - -/* Ethernet frame types that might be relevant to us */ -#define ZT_ETHERTYPE_IPV4 0x0800 -#define ZT_ETHERTYPE_ARP 0x0806 -#define ZT_ETHERTYPE_RARP 0x8035 -#define ZT_ETHERTYPE_ATALK 0x809b -#define ZT_ETHERTYPE_AARP 0x80f3 -#define ZT_ETHERTYPE_IPX_A 0x8137 -#define ZT_ETHERTYPE_IPX_B 0x8138 -#define ZT_ETHERTYPE_IPV6 0x86dd - -/* IP protocols we might care about */ -#define ZT_IPPROTO_ICMP 0x01 -#define ZT_IPPROTO_IGMP 0x02 -#define ZT_IPPROTO_TCP 0x06 -#define ZT_IPPROTO_UDP 0x11 -#define ZT_IPPROTO_GRE 0x2f -#define ZT_IPPROTO_ESP 0x32 -#define ZT_IPPROTO_AH 0x33 -#define ZT_IPPROTO_ICMPV6 0x3a -#define ZT_IPPROTO_OSPF 0x59 -#define ZT_IPPROTO_IPIP 0x5e -#define ZT_IPPROTO_IPCOMP 0x6c -#define ZT_IPPROTO_L2TP 0x73 -#define ZT_IPPROTO_SCTP 0x84 -#define ZT_IPPROTO_FC 0x85 -#define ZT_IPPROTO_UDPLITE 0x88 -#define ZT_IPPROTO_HIP 0x8b - -/* IPv4 ICMP types */ -#define ZT_ICMP_ECHO_REPLY 0 -#define ZT_ICMP_DESTINATION_UNREACHABLE 3 -#define ZT_ICMP_SOURCE_QUENCH 4 -#define ZT_ICMP_REDIRECT 5 -#define ZT_ICMP_ALTERNATE_HOST_ADDRESS 6 -#define ZT_ICMP_ECHO_REQUEST 8 -#define ZT_ICMP_ROUTER_ADVERTISEMENT 9 -#define ZT_ICMP_ROUTER_SOLICITATION 10 -#define ZT_ICMP_TIME_EXCEEDED 11 -#define ZT_ICMP_BAD_IP_HEADER 12 -#define ZT_ICMP_TIMESTAMP 13 -#define ZT_ICMP_TIMESTAMP_REPLY 14 -#define ZT_ICMP_INFORMATION_REQUEST 15 -#define ZT_ICMP_INFORMATION_REPLY 16 -#define ZT_ICMP_ADDRESS_MASK_REQUEST 17 -#define ZT_ICMP_ADDRESS_MASK_REPLY 18 -#define ZT_ICMP_TRACEROUTE 30 -#define ZT_ICMP_MOBILE_HOST_REDIRECT 32 -#define ZT_ICMP_MOBILE_REGISTRATION_REQUEST 35 -#define ZT_ICMP_MOBILE_REGISTRATION_REPLY 36 - -/* IPv6 ICMP types */ -#define ZT_ICMP6_DESTINATION_UNREACHABLE 1 -#define ZT_ICMP6_PACKET_TOO_BIG 2 -#define ZT_ICMP6_TIME_EXCEEDED 3 -#define ZT_ICMP6_PARAMETER_PROBLEM 4 -#define ZT_ICMP6_ECHO_REQUEST 128 -#define ZT_ICMP6_ECHO_REPLY 129 -#define ZT_ICMP6_MULTICAST_LISTENER_QUERY 130 -#define ZT_ICMP6_MULTICAST_LISTENER_REPORT 131 -#define ZT_ICMP6_MULTICAST_LISTENER_DONE 132 -#define ZT_ICMP6_ROUTER_SOLICITATION 133 -#define ZT_ICMP6_ROUTER_ADVERTISEMENT 134 -#define ZT_ICMP6_NEIGHBOR_SOLICITATION 135 -#define ZT_ICMP6_NEIGHBOR_ADVERTISEMENT 136 -#define ZT_ICMP6_REDIRECT_MESSAGE 137 -#define ZT_ICMP6_ROUTER_RENUMBERING 138 -#define ZT_ICMP6_NODE_INFORMATION_QUERY 139 -#define ZT_ICMP6_NODE_INFORMATION_RESPONSE 140 -#define ZT_ICMP6_INV_NEIGHBOR_SOLICITATION 141 -#define ZT_ICMP6_INV_NEIGHBOR_ADVERTISEMENT 142 -#define ZT_ICMP6_MLDV2 143 -#define ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REQUEST 144 -#define ZT_ICMP6_HOME_AGENT_ADDRESS_DISCOVERY_REPLY 145 -#define ZT_ICMP6_MOBILE_PREFIX_SOLICITATION 146 -#define ZT_ICMP6_MOBILE_PREFIX_ADVERTISEMENT 147 -#define ZT_ICMP6_CERTIFICATION_PATH_SOLICITATION 148 -#define ZT_ICMP6_CERTIFICATION_PATH_ADVERTISEMENT 149 -#define ZT_ICMP6_MULTICAST_ROUTER_ADVERTISEMENT 151 -#define ZT_ICMP6_MULTICAST_ROUTER_SOLICITATION 152 -#define ZT_ICMP6_MULTICAST_ROUTER_TERMINATION 153 -#define ZT_ICMP6_RPL_CONTROL_MESSAGE 155 - -namespace ZeroTier { - -class RuntimeEnvironment; - -/** - * A simple Ethernet frame level filter - * - * This doesn't specify actions, since it's used as a deny filter. The rule - * in ZT1 is "that which is not explicitly prohibited is allowed." (Except for - * ethertypes, which are handled by a whitelist.) - */ -class Filter -{ -public: - /** - * Value returned by etherTypeName, etc. on unknown - * - * These static methods return precisely this, so a pointer equality - * check will work. - */ - static const char *const UNKNOWN_NAME; - - /** - * An empty range as a more idiomatic way of specifying a wildcard match - */ - static const Range ANY; - - /** - * A filter rule - */ - class Rule - { - public: - Rule() - throw() : - _etherType(), - _protocol(), - _port() - { - } - - /** - * Construct a rule from a string-serialized value - * - * @param s String formatted rule, such as returned by toString() - * @throws std::invalid_argument String formatted rule is not valid - */ - Rule(const char *s) - throw(std::invalid_argument); - - /** - * Construct a new rule - * - * @param etype Ethernet type or empty range for ANY - * @param prot Protocol or empty range for ANY (meaning depends on ethertype, e.g. IP protocol numbers) - * @param prt Port or empty range for ANY (only applies to some protocols) - */ - Rule(const Range &etype,const Range &prot,const Range &prt) - throw() : - _etherType(etype), - _protocol(prot), - _port(prt) - { - } - - inline const Range ðerType() const throw() { return _etherType; } - inline const Range &protocol() const throw() { return _protocol; } - inline const Range &port() const throw() { return _port; } - - /** - * Test this rule against a frame - * - * @param etype Type of ethernet frame - * @param data Ethernet frame data - * @param len Length of ethernet frame - * @return True if rule matches - * @throws std::invalid_argument Frame invalid or not parseable - */ - bool operator()(unsigned int etype,const void *data,unsigned int len) const - throw(std::invalid_argument); - - /** - * Serialize rule as string - * - * @return Human readable representation of rule - */ - std::string toString() const; - - inline bool operator==(const Rule &r) const throw() { return ((_etherType == r._etherType)&&(_protocol == r._protocol)&&(_port == r._port)); } - inline bool operator!=(const Rule &r) const throw() { return !(*this == r); } - inline bool operator<(const Rule &r) const - throw() - { - if (_etherType < r._etherType) - return true; - else if (_etherType == r._etherType) { - if (_protocol < r._protocol) - return true; - else if (_protocol == r._protocol) { - if (_port < r._port) - return true; - } - } - return false; - } - inline bool operator>(const Rule &r) const throw() { return (r < *this); } - inline bool operator<=(const Rule &r) const throw() { return !(r < *this); } - inline bool operator>=(const Rule &r) const throw() { return !(*this < r); } - - private: - Range _etherType; - Range _protocol; - Range _port; - }; - - Filter() {} - - /** - * @param s String-serialized filter representation - */ - Filter(const char *s) - throw(std::invalid_argument); - - /** - * @return Comma-delimited list of string-format rules - */ - std::string toString() const; - - /** - * Add a rule to this filter - * - * @param r Rule to add to filter - */ - void add(const Rule &r); - - inline bool operator()(unsigned int etype,const void *data,unsigned int len) const - throw(std::invalid_argument) - { - for(std::vector::const_iterator r(_rules.begin());r!=_rules.end();++r) { - if ((*r)(etype,data,len)) - return true; - } - return false; - } - - static const char *etherTypeName(const unsigned int etherType) - throw(); - static const char *ipProtocolName(const unsigned int ipp) - throw(); - static const char *icmpTypeName(const unsigned int icmpType) - throw(); - static const char *icmp6TypeName(const unsigned int icmp6Type) - throw(); - -private: - std::vector _rules; -}; - -} // namespace ZeroTier - -#endif diff --git a/attic/SECURITY.md b/attic/SECURITY.md deleted file mode 100644 index 5ca125e9..00000000 --- a/attic/SECURITY.md +++ /dev/null @@ -1,84 +0,0 @@ -ZeroTier Security -====== - -## Summary - - -## Using ZeroTier Securely - -### Overall Recommendations - -*TL;DR: same as anything else: defense in depth defense in depth defense in depth.* - -We encourage our users to treat private ZeroTier networks as being rougly equivalent in security to WPA2-enterprise securied WiFi or on-premise wired Ethernet. (Public networks on the other hand are open by design.) That means they're networks with perimeters, but like all networks the compromise of any participating device or network controller allows an attacker to breach this perimeter. - -**Never trust the network.** Many modern security professionals discourage reliance on network perimeters as major components in any security strategy, and we strongly agree regardless of whether your network is physical or virtual. - -As part of a defense in depth approach **we specifically encourage the use of other secure protocols and authentication systems over ZeroTier networks**. While the use of secure encrypted protocols like SSH and SSL over ZeroTier adds a bit more overhead, it greatly reduces the chance of total compromise. - -Imagine that the per-day probability of a major "0-day" security flaw in ZeroTier and OpenSSH are both roughly 0.001 or one per thousand days. Using both at the same time gives you a cumulative 0-day risk of roughly 0.000001 or one per one million days. - -Those are made-up numbers. In reality these probabilities can't be known ahead of time. History shows that a 0-day could be found in anything tomorrow, next week, or never. But layers of security give you an overall posture that is the product -- more than the sum -- of its parts. That's how defense in depth works. - -### ZeroTier Specifics - -#### Protect Your Identity - -Each ZeroTier device has an identity. The secret portion of this identity is stored in a file called "identity.secret." *Protect this file.* If it's stolen your device's identity (as represented by its 10-digit ZeroTier address) can easily be stolen or impersonated and your traffic can be decrypted or man-in-the-middle'd. - -#### Protect Your Controller - -The second major component of ZeroTier network security is the network controller. It's responsible for issuing certificates and configuration information to all network members. That makes it a certificate authority. Compromise of the controller allows an attacker to join or disrupt any network the controller controls. It does *not*, however, allow an attacker to decrypt peer to peer unicast traffic. - -If you are using our controller-as-a-service at [my.zerotier.com](https://my.zerotier.com), you are delegating this responsibility to us. - -## Security Priorities - -These are our security "must-haves." If the system fails in any of these objectives it is broken. - -* ZeroTier must be secure against remote vulnerabilities. This includes things like unauthorized remote control, remote penetration of the device using ZeroTier as a vector, or remote injection of malware. - -* The content (but not meta-data) of communication must be secure against eavesdropping on the wire by any known means. (We can't warrant against secret vulnerabilities against ciphers, etc., or anything else we don't know about.) - -* Communication must be secure against man-in-the-middle attacks and remote device impersonation. - -## Security Non-Priorities - -There are a few aspects of security we knowingly do not address, since doing so would be beyond scope or would conflict too greatly with other priorities. - -* ZeroTier makes no effort to conceal communication meta-data such as source and destination addresses and the amount of information transferred between peers. To do this more or less requires onion routing or other "heavy" approaches to anonymity, and this is beyond scope. - -* ZeroTier does not implement complex certificate chains, X.509, or other feature-rich (some would say feature-laden) cryptographic stuff. We only implement the crypto we need to get the job done. - -* We don't take extraordinary measures to preserve security under conditions in which an endpoint device has been penetrated by other means (e.g. "rooted" by third party malware) or physicall compromised. If someone steals your keys they've stolen your keys, and if they've "pwned" your device they can easily eavesdrop on everything directly. - -## Insecurities and Areas for Improvement - -The only perfectly secure system is one that is off. All real world systems have potential security weaknesses. If possible, we like to know what these are and acknowledge their existence. - -In some cases we plan to improve these. In other cases we have deliberately decided to "punt" on them in favor of some other priority (see philosophy). We may or may not revisit this decision in the future. - -* We don't implement forward secrecy / ephemeral keys. A [discussion of this can be found at the closed GitHub issue for this feature](https://github.com/zerotier/ZeroTierOne/issues/204). In short: we've decided to "punt" on this feature because it introduces complexity and state negotiation. One of the design goals of ZeroTier is "reliability convergence" -- the reliability of ZeroTier virtual networks should rapidly converge with that of the underlying physical wire. Any state that must be negotiated prior to communication multiplies the probability of delay or failure due to packet loss. We *may* revisit this decision at a later date. - -## Secure Coding Practices - -The first line of defense employed against remote vulnerabilities and other major security flaws is the use of secure coding practices. These are, in no particular order: - -* All parsing of remote messages is performed via higher level safe bounds-checked data structures and interfaces. See node/Buffer.hpp for one of the core elements of this. - -* C++ exceptions are used to ensure that any unhandled failure or error condition (such as a bounds checking violation) results in the safe and complete termination of message processing. Invalid messages are dropped and ignored. - -* Minimalism is a secure coding practice. There is an exponential relationship between complexity and the probability of bugs, and complex designs are much harder to audit and reason about. - -* Our build scripts try to enable any OS and compiler level security features such as ASLR and "stack canaries" on non-debug builds. - -## Cryptographic Security Practices - -* We use [boring crypto](https://cr.yp.to/talks/2015.10.05/slides-djb-20151005-a4.pdf). A single symmetric algorithm (Salsa20/12), a single asymmetric algorithm (Curve25519 ECDH-256), and a single MAC (Poly1305). The way these algorithms are used is identical to how they're used in the NaCl reference implementation. The protocol supports selection of alternative algorithms but only for "future proofing" in the case that a serious flaw is discovered in any of these. Avoding algorithm bloat and cryptographic state negotiation helps guard against down-grade, "oracle," and other protocol level attacks. - -* Authenticated encryption is employed with authentication being performed prior to any other operations on received messages. See also: [the cryptographic doom principle](https://moxie.org/blog/the-cryptographic-doom-principle/). - -* "Never branch on anything secret" -- deterministic-time comparisons and other operations are used in cryptographic operations. See Utils::secureEq() in node/Utils.hpp. - -* OS-derived crypographic random numbers (/dev/urandom or Windows CryptGenRandom) are further randomized using encryption by a secondary key with a secondary source of entropy to guard against CSPRNG bugs. Such OS-level CSPRNG bugs have been found in the past. See Utils::getSecureRandom() in node/Utils.hpp. - -- cgit v1.2.3 From bf8d71e82c27eae1e47bde411054f5258df29146 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 17 Nov 2016 16:20:41 -0800 Subject: Add notion of upstream that is separate from root in Topology, etc. --- attic/CertificateOfTrust.cpp | 67 +++++++++++++++++++ attic/CertificateOfTrust.hpp | 155 +++++++++++++++++++++++++++++++++++++++++++ node/IncomingPacket.cpp | 10 +-- node/Packet.hpp | 19 ++++-- node/Topology.cpp | 87 ++++++++++++++++-------- node/Topology.hpp | 37 ++++++----- objects.mk | 1 + 7 files changed, 321 insertions(+), 55 deletions(-) create mode 100644 attic/CertificateOfTrust.cpp create mode 100644 attic/CertificateOfTrust.hpp (limited to 'attic') diff --git a/attic/CertificateOfTrust.cpp b/attic/CertificateOfTrust.cpp new file mode 100644 index 00000000..e85a91df --- /dev/null +++ b/attic/CertificateOfTrust.cpp @@ -0,0 +1,67 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + */ + +#include "CertificateOfTrust.hpp" + +#include "RuntimeEnvironment.hpp" +#include "Topology.hpp" +#include "Switch.hpp" + +namespace ZeroTier { + +bool CertificateOfTrust::create(uint64_t ts,uint64_t rls,const Identity &iss,const Identity &tgt,Level l) +{ + if ((!iss)||(!iss.hasPrivate())) + return false; + + _timestamp = ts; + _roles = rls; + _issuer = iss.address(); + _target = tgt; + _level = l; + + Buffer tmp; + tmp.append(_timestamp); + tmp.append(_roles); + _issuer.appendTo(tmp); + _target.serialize(tmp,false); + tmp.append((uint16_t)_level); + _signature = iss.sign(tmp.data(),tmp.size()); + + return true; +} + +int CertificateOfTrust::verify(const RuntimeEnvironment *RR) const +{ + const Identity id(RR->topology->getIdentity(_issuer)); + if (!id) { + RR->sw->requestWhois(_issuer); + return 1; + } + + Buffer tmp; + tmp.append(_timestamp); + tmp.append(_roles); + _issuer.appendTo(tmp); + _target.serialize(tmp,false); + tmp.append((uint16_t)_level); + + return (id.verify(tmp.data(),tmp.size(),_signature) ? 0 : -1); +} + +} // namespace ZeroTier diff --git a/attic/CertificateOfTrust.hpp b/attic/CertificateOfTrust.hpp new file mode 100644 index 00000000..6e3c8743 --- /dev/null +++ b/attic/CertificateOfTrust.hpp @@ -0,0 +1,155 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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_CERTIFICATEOFTRUST_HPP +#define ZT_CERTIFICATEOFTRUST_HPP + +#include "Constants.hpp" +#include "Identity.hpp" +#include "C25519.hpp" +#include "Buffer.hpp" + +namespace ZeroTier { + +class RuntimeEnvironment; + +/** + * Certificate of peer to peer trust + */ +class CertificateOfTrust +{ +public: + /** + * Trust levels, with 0 indicating anti-trust + */ + enum Level + { + /** + * Negative trust is reserved for informing peers that another peer is misbehaving, etc. Not currently used. + */ + LEVEL_NEGATIVE = 0, + + /** + * Default trust -- for most peers + */ + LEVEL_DEFAULT = 1, + + /** + * Above normal trust, e.g. common network membership + */ + LEVEL_MEDIUM = 25, + + /** + * High trust -- e.g. an upstream or a controller + */ + LEVEL_HIGH = 50, + + /** + * Right now ultimate is only for roots + */ + LEVEL_ULTIMATE = 100 + }; + + /** + * Role bit masks + */ + enum Role + { + /** + * Target is permitted to represent issuer on the network as a federated root / relay + */ + ROLE_UPSTREAM = 0x00000001 + }; + + CertificateOfTrust() : + _timestamp(0), + _roles(0), + _issuer(), + _target(), + _level(LEVEL_DEFAULT), + _signature() {} + + /** + * Create and sign this certificate of trust + * + * @param ts Cert timestamp + * @param rls Roles bitmap + * @param iss Issuer identity (must have secret key!) + * @param tgt Target identity + * @param l Trust level + * @return True on successful signature + */ + bool create(uint64_t ts,uint64_t rls,const Identity &iss,const Identity &tgt,Level l); + + /** + * Verify this COT and its signature + * + * @param RR Runtime environment for looking up peers + * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential + */ + int verify(const RuntimeEnvironment *RR) const; + + inline bool roleUpstream() const { return ((_roles & (uint64_t)ROLE_UPSTREAM) != 0); } + + inline uint64_t timestamp() const { return _timestamp; } + inline uint64_t roles() const { return _roles; } + inline const Address &issuer() const { return _issuer; } + inline const Identity &target() const { return _target; } + inline Level level() const { return _level; } + + inline operator bool() const { return (_issuer); } + + template + inline void serialize(Buffer &b) const + { + b.append(_timestamp); + b.append(_roles); + _issuer.appendTo(b); + _target.serialize(b); + b.append((uint16_t)_level); + b.append((uint8_t)1); // 1 == ed25519 signature + b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); + b.append(_signature.data,ZT_C25519_SIGNATURE_LEN); + b.append((uint16_t)0); // length of additional fields + } + + template + inline unsigned int deserialize(const Buffer &b,unsigned int startAt = 0) + { + unsigned int p = startAt; + _timestamp = b.template at(p); p += 8; + _roles = b.template at(p); p += 8; + _issuer.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH; + p += _target.deserialize(b,p); + _level = b.template at(p); p += 2; + p += b.template at(p); p += 2; + return (p - startAt); + } + +private: + uint64_t _timestamp; + uint64_t _roles; + Address _issuer; + Identity _target; + Level _level; + C25519::Signature _signature; +}; + +} // namespace ZeroTier + +#endif diff --git a/node/IncomingPacket.cpp b/node/IncomingPacket.cpp index bde5df71..c6346346 100644 --- a/node/IncomingPacket.cpp +++ b/node/IncomingPacket.cpp @@ -160,7 +160,7 @@ bool IncomingPacket::_doERROR(const RuntimeEnvironment *RR,const SharedPtr case Packet::ERROR_IDENTITY_COLLISION: // FIXME: for federation this will need a payload with a signature or something. - if (RR->topology->isRoot(peer->identity())) + if (RR->topology->isUpstream(peer->identity())) RR->node->postEvent(ZT_EVENT_FATAL_ERROR_IDENTITY_COLLISION); break; @@ -508,11 +508,7 @@ bool IncomingPacket::_doWHOIS(const RuntimeEnvironment *RR,const SharedPtr id.serialize(outp,false); ++count; } else { - // If I am not the root and don't know this identity, ask upstream. Downstream - // peer may re-request in the future and if so we will be able to provide it. - if (!RR->topology->amRoot()) - RR->sw->requestWhois(addr); - + RR->sw->requestWhois(addr); #ifdef ZT_ENABLE_CLUSTER // Distribute WHOIS queries across a cluster if we do not know the ID. // This may result in duplicate OKs to the querying peer, which is fine. @@ -666,7 +662,7 @@ bool IncomingPacket::_doEXT_FRAME(const RuntimeEnvironment *RR,const SharedPtr

address(),RR->identity.address(),Packet::VERB_OK); outp.append((uint8_t)Packet::VERB_EXT_FRAME); outp.append((uint64_t)packetId()); diff --git a/node/Packet.hpp b/node/Packet.hpp index a8738884..7a742aad 100644 --- a/node/Packet.hpp +++ b/node/Packet.hpp @@ -617,10 +617,8 @@ public: * <[1] protocol address length (4 for IPv4, 16 for IPv6)> * <[...] protocol address (network byte order)> * - * This is sent by a relaying node to initiate NAT traversal between two - * peers that are communicating by way of indirect relay. The relay will - * send this to both peers at the same time on a periodic basis, telling - * each where it might find the other on the network. + * An upstream node can send this to inform both sides of a relay of + * information they might use to establish a direct connection. * * Upon receipt a peer sends HELLO to establish a direct link. * @@ -1051,7 +1049,18 @@ public: * OK or ERROR and has no special semantics outside of whatever the user * (via the ZeroTier core API) chooses to give it. */ - VERB_USER_MESSAGE = 0x14 + VERB_USER_MESSAGE = 0x14, + + /** + * Information related to federation and mesh-like behavior: + * <[2] 16-bit length of Dictionary> + * <[...] topology definition info Dictionary> + * + * This message can carry information that can be used to define topology + * and implement "mesh-like" behavior. It can optionally generate OK or + * ERROR, and these carry the same payload. + */ + VERB_TOPOLOGY_HINT = 0x15 }; /** diff --git a/node/Topology.cpp b/node/Topology.cpp index 12a7cc0b..48ced7c5 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -111,9 +111,8 @@ SharedPtr Topology::getPeer(const Address &zta) { Mutex::Lock _l(_lock); const SharedPtr *const ap = _peers.get(zta); - if (ap) { + if (ap) return *ap; - } } try { @@ -158,7 +157,7 @@ void Topology::saveIdentity(const Identity &id) } } -SharedPtr Topology::getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid) +SharedPtr Topology::getUpstreamPeer(const Address *avoid,unsigned int avoidCount,bool strictAvoid) { const uint64_t now = RR->node->now(); Mutex::Lock _l(_lock); @@ -189,22 +188,25 @@ SharedPtr Topology::getBestRoot(const Address *avoid,unsigned int avoidCou const SharedPtr *bestOverall = (const SharedPtr *)0; const SharedPtr *bestNotAvoid = (const SharedPtr *)0; - for(std::vector< SharedPtr >::const_iterator r(_rootPeers.begin());r!=_rootPeers.end();++r) { - bool avoiding = false; - for(unsigned int i=0;iaddress()) { - avoiding = true; - break; + for(std::vector

::const_iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) { + const SharedPtr *const p = _peers.get(*a); + if (p) { + bool avoiding = false; + for(unsigned int i=0;iaddress()) { + avoiding = true; + break; + } + } + const unsigned int q = (*p)->relayQuality(now); + if (q <= bestQualityOverall) { + bestQualityOverall = q; + bestOverall = &(*p); + } + if ((!avoiding)&&(q <= bestQualityNotAvoid)) { + bestQualityNotAvoid = q; + bestNotAvoid = &(*p); } - } - const unsigned int q = (*r)->relayQuality(now); - if (q <= bestQualityOverall) { - bestQualityOverall = q; - bestOverall = &(*r); - } - if ((!avoiding)&&(q <= bestQualityNotAvoid)) { - bestQualityNotAvoid = q; - bestNotAvoid = &(*r); } } @@ -219,9 +221,34 @@ SharedPtr Topology::getBestRoot(const Address *avoid,unsigned int avoidCou return SharedPtr(); } +bool Topology::isRoot(const Identity &id) const +{ + Mutex::Lock _l(_lock); + return (std::find(_rootAddresses.begin(),_rootAddresses.end(),id.address()) != _rootAddresses.end()); +} + bool Topology::isUpstream(const Identity &id) const { - return isRoot(id); + Mutex::Lock _l(_lock); + return (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),id.address()) != _upstreamAddresses.end()); +} + +void Topology::setUpstream(const Address &a,bool upstream) +{ + Mutex::Lock _l(_lock); + if (std::find(_rootAddresses.begin(),_rootAddresses.end(),a) == _rootAddresses.end()) { + if (upstream) { + if (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),a) == _upstreamAddresses.end()) + _upstreamAddresses.push_back(a); + } else { + std::vector
ua; + for(std::vector
::iterator i(_upstreamAddresses.begin());i!=_upstreamAddresses.end();++i) { + if (a != *i) + ua.push_back(*i); + } + _upstreamAddresses.swap(ua); + } + } } bool Topology::worldUpdateIfValid(const World &newWorld) @@ -249,7 +276,7 @@ void Topology::clean(uint64_t now) Address *a = (Address *)0; SharedPtr *p = (SharedPtr *)0; while (i.next(a,p)) { - if ( (!(*p)->isAlive(now)) && (std::find(_rootAddresses.begin(),_rootAddresses.end(),*a) == _rootAddresses.end()) ) + if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) _peers.erase(*a); } } @@ -280,25 +307,33 @@ Identity Topology::_getIdentity(const Address &zta) void Topology::_setWorld(const World &newWorld) { // assumed _lock is locked (or in constructor) + + std::vector
ua; + for(std::vector
::iterator a(_upstreamAddresses.begin());a!=_upstreamAddresses.end();++a) { + if (std::find(_rootAddresses.begin(),_rootAddresses.end(),*a) == _rootAddresses.end()) + ua.push_back(*a); + } + _world = newWorld; - _amRoot = false; _rootAddresses.clear(); - _rootPeers.clear(); + _amRoot = false; + for(std::vector::const_iterator r(_world.roots().begin());r!=_world.roots().end();++r) { _rootAddresses.push_back(r->identity.address()); + if (std::find(ua.begin(),ua.end(),r->identity.address()) == ua.end()) + ua.push_back(r->identity.address()); if (r->identity.address() == RR->identity.address()) { _amRoot = true; } else { SharedPtr *rp = _peers.get(r->identity.address()); - if (rp) { - _rootPeers.push_back(*rp); - } else { + if (!rp) { SharedPtr newrp(new Peer(RR,RR->identity,r->identity)); _peers.set(r->identity.address(),newrp); - _rootPeers.push_back(newrp); } } } + + _upstreamAddresses.swap(ua); } } // namespace ZeroTier diff --git a/node/Topology.hpp b/node/Topology.hpp index e63766cb..573d5ca2 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -125,35 +125,27 @@ public: void saveIdentity(const Identity &id); /** - * Get the current favorite root server + * Get the current best upstream peer * * @return Root server with lowest latency or NULL if none */ - inline SharedPtr getBestRoot() { return getBestRoot((const Address *)0,0,false); } + inline SharedPtr getUpstreamPeer() { return getUpstreamPeer((const Address *)0,0,false); } /** - * Get the best root server, avoiding root servers listed in an array - * - * This will get the best root server (lowest latency, etc.) but will - * try to avoid the listed root servers, only using them if no others - * are available. + * Get the current best upstream peer, avoiding those in the supplied avoid list * * @param avoid Nodes to avoid * @param avoidCount Number of nodes to avoid * @param strictAvoid If false, consider avoided root servers anyway if no non-avoid root servers are available * @return Root server or NULL if none available */ - SharedPtr getBestRoot(const Address *avoid,unsigned int avoidCount,bool strictAvoid); + SharedPtr getUpstreamPeer(const Address *avoid,unsigned int avoidCount,bool strictAvoid); /** * @param id Identity to check * @return True if this is a designated root server in this world */ - inline bool isRoot(const Identity &id) const - { - Mutex::Lock _l(_lock); - return (std::find(_rootAddresses.begin(),_rootAddresses.end(),id.address()) != _rootAddresses.end()); - } + bool isRoot(const Identity &id) const; /** * @param id Identity to check @@ -161,6 +153,16 @@ public: */ bool isUpstream(const Identity &id) const; + /** + * Set whether or not an address is upstream + * + * If the address is a root this does nothing, since roots are fixed. + * + * @param a Target address + * @param upstream New upstream status + */ + void setUpstream(const Address &a,bool upstream); + /** * @return Vector of root server addresses */ @@ -175,7 +177,8 @@ public: */ inline std::vector
upstreamAddresses() const { - return rootAddresses(); + Mutex::Lock _l(_lock); + return _upstreamAddresses; } /** @@ -342,9 +345,9 @@ private: Hashtable< Address,SharedPtr > _peers; Hashtable< Path::HashKey,SharedPtr > _paths; - std::vector< Address > _rootAddresses; - std::vector< SharedPtr > _rootPeers; - bool _amRoot; + std::vector< Address > _upstreamAddresses; // includes roots + std::vector< Address > _rootAddresses; // only roots + bool _amRoot; // am I a root? Mutex _lock; }; diff --git a/objects.mk b/objects.mk index 078a92a7..16858ef3 100644 --- a/objects.mk +++ b/objects.mk @@ -4,6 +4,7 @@ OBJS=\ node/C25519.o \ node/Capability.o \ node/CertificateOfMembership.o \ + node/CertificateOfTrust.o \ node/Cluster.o \ node/Identity.o \ node/IncomingPacket.o \ -- cgit v1.2.3 From 6b5d6efe6c9d597c49b3fe57d6c40342b226556f Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 23 Dec 2016 14:33:04 -0800 Subject: Retire old build farm (something new is coming) and update makefile for linux to not auto-build doc. --- attic/linux-build-farm/README.md | 8 +++ .../linux-build-farm/amazon-2016.03/x64/Dockerfile | 13 ++++ attic/linux-build-farm/build.sh | 69 ++++++++++++++++++++++ attic/linux-build-farm/centos-6/x64/Dockerfile | 13 ++++ attic/linux-build-farm/centos-6/x86/Dockerfile | 13 ++++ attic/linux-build-farm/centos-7/x64/Dockerfile | 10 ++++ attic/linux-build-farm/centos-7/x86/Dockerfile | 22 +++++++ .../linux-build-farm/debian-jessie/x64/Dockerfile | 12 ++++ .../linux-build-farm/debian-jessie/x86/Dockerfile | 12 ++++ .../linux-build-farm/debian-stretch/x64/Dockerfile | 12 ++++ .../linux-build-farm/debian-stretch/x86/Dockerfile | 12 ++++ .../linux-build-farm/debian-wheezy/x64/Dockerfile | 12 ++++ .../linux-build-farm/debian-wheezy/x86/Dockerfile | 15 +++++ attic/linux-build-farm/fedora-22/x64/Dockerfile | 10 ++++ attic/linux-build-farm/fedora-22/x86/Dockerfile | 19 ++++++ attic/linux-build-farm/make-apt-repos.sh | 16 +++++ attic/linux-build-farm/make-rpm-repos.sh | 64 ++++++++++++++++++++ .../other/zerotier-containerized/Dockerfile | 20 +++++++ .../other/zerotier-containerized/main.sh | 10 ++++ .../linux-build-farm/ubuntu-trusty/x64/Dockerfile | 12 ++++ .../linux-build-farm/ubuntu-trusty/x86/Dockerfile | 12 ++++ attic/linux-build-farm/ubuntu-wily/x64/Dockerfile | 12 ++++ attic/linux-build-farm/ubuntu-wily/x86/Dockerfile | 12 ++++ .../linux-build-farm/ubuntu-xenial/x64/Dockerfile | 14 +++++ .../linux-build-farm/ubuntu-xenial/x86/Dockerfile | 14 +++++ linux-build-farm/README.md | 8 --- linux-build-farm/amazon-2016.03/x64/Dockerfile | 13 ---- linux-build-farm/build.sh | 69 ---------------------- linux-build-farm/centos-6/x64/Dockerfile | 13 ---- linux-build-farm/centos-6/x86/Dockerfile | 13 ---- linux-build-farm/centos-7/x64/Dockerfile | 10 ---- linux-build-farm/centos-7/x86/Dockerfile | 22 ------- linux-build-farm/debian-jessie/x64/Dockerfile | 12 ---- linux-build-farm/debian-jessie/x86/Dockerfile | 12 ---- linux-build-farm/debian-stretch/x64/Dockerfile | 12 ---- linux-build-farm/debian-stretch/x86/Dockerfile | 12 ---- linux-build-farm/debian-wheezy/x64/Dockerfile | 12 ---- linux-build-farm/debian-wheezy/x86/Dockerfile | 15 ----- linux-build-farm/fedora-22/x64/Dockerfile | 10 ---- linux-build-farm/fedora-22/x86/Dockerfile | 19 ------ linux-build-farm/make-apt-repos.sh | 16 ----- linux-build-farm/make-rpm-repos.sh | 64 -------------------- .../other/zerotier-containerized/Dockerfile | 20 ------- .../other/zerotier-containerized/main.sh | 10 ---- linux-build-farm/ubuntu-trusty/x64/Dockerfile | 12 ---- linux-build-farm/ubuntu-trusty/x86/Dockerfile | 12 ---- linux-build-farm/ubuntu-wily/x64/Dockerfile | 12 ---- linux-build-farm/ubuntu-wily/x86/Dockerfile | 12 ---- linux-build-farm/ubuntu-xenial/x64/Dockerfile | 14 ----- linux-build-farm/ubuntu-xenial/x86/Dockerfile | 14 ----- make-linux.mk | 8 +-- 51 files changed, 440 insertions(+), 444 deletions(-) create mode 100644 attic/linux-build-farm/README.md create mode 100644 attic/linux-build-farm/amazon-2016.03/x64/Dockerfile create mode 100755 attic/linux-build-farm/build.sh create mode 100644 attic/linux-build-farm/centos-6/x64/Dockerfile create mode 100644 attic/linux-build-farm/centos-6/x86/Dockerfile create mode 100644 attic/linux-build-farm/centos-7/x64/Dockerfile create mode 100644 attic/linux-build-farm/centos-7/x86/Dockerfile create mode 100644 attic/linux-build-farm/debian-jessie/x64/Dockerfile create mode 100644 attic/linux-build-farm/debian-jessie/x86/Dockerfile create mode 100644 attic/linux-build-farm/debian-stretch/x64/Dockerfile create mode 100644 attic/linux-build-farm/debian-stretch/x86/Dockerfile create mode 100644 attic/linux-build-farm/debian-wheezy/x64/Dockerfile create mode 100644 attic/linux-build-farm/debian-wheezy/x86/Dockerfile create mode 100644 attic/linux-build-farm/fedora-22/x64/Dockerfile create mode 100644 attic/linux-build-farm/fedora-22/x86/Dockerfile create mode 100755 attic/linux-build-farm/make-apt-repos.sh create mode 100755 attic/linux-build-farm/make-rpm-repos.sh create mode 100644 attic/linux-build-farm/other/zerotier-containerized/Dockerfile create mode 100755 attic/linux-build-farm/other/zerotier-containerized/main.sh create mode 100644 attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile create mode 100644 attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile create mode 100644 attic/linux-build-farm/ubuntu-wily/x64/Dockerfile create mode 100644 attic/linux-build-farm/ubuntu-wily/x86/Dockerfile create mode 100644 attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile create mode 100644 attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile delete mode 100644 linux-build-farm/README.md delete mode 100644 linux-build-farm/amazon-2016.03/x64/Dockerfile delete mode 100755 linux-build-farm/build.sh delete mode 100644 linux-build-farm/centos-6/x64/Dockerfile delete mode 100644 linux-build-farm/centos-6/x86/Dockerfile delete mode 100644 linux-build-farm/centos-7/x64/Dockerfile delete mode 100644 linux-build-farm/centos-7/x86/Dockerfile delete mode 100644 linux-build-farm/debian-jessie/x64/Dockerfile delete mode 100644 linux-build-farm/debian-jessie/x86/Dockerfile delete mode 100644 linux-build-farm/debian-stretch/x64/Dockerfile delete mode 100644 linux-build-farm/debian-stretch/x86/Dockerfile delete mode 100644 linux-build-farm/debian-wheezy/x64/Dockerfile delete mode 100644 linux-build-farm/debian-wheezy/x86/Dockerfile delete mode 100644 linux-build-farm/fedora-22/x64/Dockerfile delete mode 100644 linux-build-farm/fedora-22/x86/Dockerfile delete mode 100755 linux-build-farm/make-apt-repos.sh delete mode 100755 linux-build-farm/make-rpm-repos.sh delete mode 100644 linux-build-farm/other/zerotier-containerized/Dockerfile delete mode 100755 linux-build-farm/other/zerotier-containerized/main.sh delete mode 100644 linux-build-farm/ubuntu-trusty/x64/Dockerfile delete mode 100644 linux-build-farm/ubuntu-trusty/x86/Dockerfile delete mode 100644 linux-build-farm/ubuntu-wily/x64/Dockerfile delete mode 100644 linux-build-farm/ubuntu-wily/x86/Dockerfile delete mode 100644 linux-build-farm/ubuntu-xenial/x64/Dockerfile delete mode 100644 linux-build-farm/ubuntu-xenial/x86/Dockerfile (limited to 'attic') diff --git a/attic/linux-build-farm/README.md b/attic/linux-build-farm/README.md new file mode 100644 index 00000000..8055eb0b --- /dev/null +++ b/attic/linux-build-farm/README.md @@ -0,0 +1,8 @@ +Dockerized Linux Build Farm +====== + +This subfolder contains Dockerfiles and a script to build Linux packages for a variety of Linux distributions. It's also an excellent way to test your CPU fans and stress test your disk. + +Running `build.sh` with no arguments builds everything. You can run `build.sh` with the name of a distro (e.g. centos-7) to only build that. Both 32 and 64 bit packages are built except where no 32-bit version of the distribution exists. + +The `make-apt-repos.sh` and `make-rpm-repos.sh` scripts build repositories. They may require some editing for outside-of-ZeroTier use, and be careful with the apt one if you have an existing *aptly* configuration. diff --git a/attic/linux-build-farm/amazon-2016.03/x64/Dockerfile b/attic/linux-build-farm/amazon-2016.03/x64/Dockerfile new file mode 100644 index 00000000..bd1a246a --- /dev/null +++ b/attic/linux-build-farm/amazon-2016.03/x64/Dockerfile @@ -0,0 +1,13 @@ +#FROM ambakshi/amazon-linux:2016.03 +#MAINTAINER Adam Ierymenko + +#RUN yum update -y +#RUN yum install -y epel-release +#RUN yum install -y make development-tools rpmdevtools clang gcc-c++ ruby ruby-devel + +#RUN gem install ronn + +FROM zerotier/zt1-build-amazon-2016.03-x64-base +MAINTAINER Adam Ierymenko + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/build.sh b/attic/linux-build-farm/build.sh new file mode 100755 index 00000000..0eb7c5d2 --- /dev/null +++ b/attic/linux-build-farm/build.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +export PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin + +subdirs=$* +if [ ! -n "$subdirs" ]; then + subdirs=`find . -type d -name '*-*' -printf '%f '` +fi + +if [ ! -d ./ubuntu-trusty ]; then + echo 'Must run from linux-build-farm subfolder.' + exit 1 +fi + +rm -f zt1-src.tar.gz +cd .. +git archive --format=tar.gz --prefix=ZeroTierOne/ -o linux-build-farm/zt1-src.tar.gz HEAD +cd linux-build-farm + +# Note that --privileged is used so we can bind mount VM shares when building in a VM. +# It has no other impact or purpose, but probably doesn't matter here in any case. + +for distro in $subdirs; do + echo + echo "--- BUILDING FOR $distro ---" + echo + + cd $distro + + if [ -d x64 ]; then + cd x64 + mv ../../zt1-src.tar.gz . + docker build -t zt1-build-${distro}-x64 . + mv zt1-src.tar.gz ../.. + cd .. + fi + + if [ -d x86 ]; then + cd x86 + mv ../../zt1-src.tar.gz . + docker build -t zt1-build-${distro}-x86 . + mv zt1-src.tar.gz ../.. + cd .. + fi + + rm -f *.deb *.rpm + +# exit 0 + + if [ ! -n "`echo $distro | grep -F debian`" -a ! -n "`echo $distro | grep -F ubuntu`" ]; then + if [ -d x64 ]; then + docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x64 /bin/bash -c 'cd /ZeroTierOne ; make redhat ; cd .. ; cp `find /root/rpmbuild -type f -name *.rpm` /artifacts ; ls -l /artifacts' + fi + if [ -d x86 ]; then + docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x86 /bin/bash -c 'cd /ZeroTierOne ; make redhat ; cd .. ; cp `find /root/rpmbuild -type f -name *.rpm` /artifacts ; ls -l /artifacts' + fi + else + if [ -d x64 ]; then + docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x64 /bin/bash -c 'cd /ZeroTierOne ; make debian ; cd .. ; cp *.deb /artifacts ; ls -l /artifacts' + fi + if [ -d x86 ]; then + docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x86 /bin/bash -c 'cd /ZeroTierOne ; make debian ; cd .. ; cp *.deb /artifacts ; ls -l /artifacts' + fi + fi + + cd .. +done + +rm -f zt1-src.tar.gz diff --git a/attic/linux-build-farm/centos-6/x64/Dockerfile b/attic/linux-build-farm/centos-6/x64/Dockerfile new file mode 100644 index 00000000..2796e422 --- /dev/null +++ b/attic/linux-build-farm/centos-6/x64/Dockerfile @@ -0,0 +1,13 @@ +FROM centos:6 +MAINTAINER Adam Ierymenko + +RUN yum update -y +RUN yum install -y epel-release +RUN yum install -y make development-tools rpmdevtools clang gcc-c++ tar + +RUN yum install -y nodejs npm + +# Stop use of http-parser-devel which is installed by nodejs/npm +RUN rm -f /usr/include/http_parser.h + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/centos-6/x86/Dockerfile b/attic/linux-build-farm/centos-6/x86/Dockerfile new file mode 100644 index 00000000..8192d139 --- /dev/null +++ b/attic/linux-build-farm/centos-6/x86/Dockerfile @@ -0,0 +1,13 @@ +FROM toopher/centos-i386:centos6 +MAINTAINER Adam Ierymenko + +RUN yum update -y +RUN yum install -y epel-release +RUN yum install -y make development-tools rpmdevtools clang gcc-c++ tar + +RUN yum install -y nodejs npm + +# Stop use of http-parser-devel which is installed by nodejs/npm +RUN rm -f /usr/include/http_parser.h + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/centos-7/x64/Dockerfile b/attic/linux-build-farm/centos-7/x64/Dockerfile new file mode 100644 index 00000000..10b58402 --- /dev/null +++ b/attic/linux-build-farm/centos-7/x64/Dockerfile @@ -0,0 +1,10 @@ +FROM centos:7 +MAINTAINER Adam Ierymenko + +RUN yum update -y +RUN yum install -y epel-release +RUN yum install -y make development-tools rpmdevtools clang gcc-c++ ruby ruby-devel + +RUN gem install ronn + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/centos-7/x86/Dockerfile b/attic/linux-build-farm/centos-7/x86/Dockerfile new file mode 100644 index 00000000..a637a8d3 --- /dev/null +++ b/attic/linux-build-farm/centos-7/x86/Dockerfile @@ -0,0 +1,22 @@ +#FROM zerotier/centos7-32bit +#MAINTAINER Adam Ierymenko + +#RUN echo 'i686-redhat-linux' >/etc/rpm/platform + +#RUN yum update -y +#RUN yum install -y make development-tools rpmdevtools http-parser-devel lz4-devel libnatpmp-devel + +#RUN yum install -y gcc-c++ +#RUN rpm --install --force https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm +#RUN rpm --install --force ftp://rpmfind.net/linux/centos/6.8/os/i386/Packages/libffi-3.0.5-3.2.el6.i686.rpm +#RUN yum install -y clang + +FROM zerotier/zt1-build-centos-7-x86-base +MAINTAINER Adam Ierymenko + +RUN yum install -y ruby ruby-devel +RUN gem install ronn + +#RUN rpm --erase http-parser-devel lz4-devel libnatpmp-devel + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-jessie/x64/Dockerfile b/attic/linux-build-farm/debian-jessie/x64/Dockerfile new file mode 100644 index 00000000..316c1d83 --- /dev/null +++ b/attic/linux-build-farm/debian-jessie/x64/Dockerfile @@ -0,0 +1,12 @@ +FROM debian:jessie +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.5 + +RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ +RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-jessie/x86/Dockerfile b/attic/linux-build-farm/debian-jessie/x86/Dockerfile new file mode 100644 index 00000000..3ad83329 --- /dev/null +++ b/attic/linux-build-farm/debian-jessie/x86/Dockerfile @@ -0,0 +1,12 @@ +FROM 32bit/debian:jessie +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.5 + +RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ +RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-stretch/x64/Dockerfile b/attic/linux-build-farm/debian-stretch/x64/Dockerfile new file mode 100644 index 00000000..c973c2b7 --- /dev/null +++ b/attic/linux-build-farm/debian-stretch/x64/Dockerfile @@ -0,0 +1,12 @@ +FROM debian:stretch +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang + +#RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ +#RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-stretch/x86/Dockerfile b/attic/linux-build-farm/debian-stretch/x86/Dockerfile new file mode 100644 index 00000000..bfc7a86f --- /dev/null +++ b/attic/linux-build-farm/debian-stretch/x86/Dockerfile @@ -0,0 +1,12 @@ +FROM mcandre/docker-debian-32bit:stretch +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang + +#RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ +#RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-wheezy/x64/Dockerfile b/attic/linux-build-farm/debian-wheezy/x64/Dockerfile new file mode 100644 index 00000000..77e1c325 --- /dev/null +++ b/attic/linux-build-farm/debian-wheezy/x64/Dockerfile @@ -0,0 +1,12 @@ +FROM debian:wheezy +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper ruby-ronn g++ make devscripts + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / + +RUN mv -f /ZeroTierOne/debian/control.wheezy /ZeroTierOne/debian/control +RUN mv -f /ZeroTierOne/debian/rules.wheezy /ZeroTierOne/debian/rules diff --git a/attic/linux-build-farm/debian-wheezy/x86/Dockerfile b/attic/linux-build-farm/debian-wheezy/x86/Dockerfile new file mode 100644 index 00000000..1f0117d2 --- /dev/null +++ b/attic/linux-build-farm/debian-wheezy/x86/Dockerfile @@ -0,0 +1,15 @@ +#FROM tubia/debian:wheezy +#MAINTAINER Adam Ierymenko + +#RUN apt-get update +#RUN apt-get install -y build-essential debhelper ruby-ronn g++ make devscripts + +FROM zerotier/zt1-build-debian-wheezy-x86-base +MAINTAINER Adam Ierymenko + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / + +RUN mv -f /ZeroTierOne/debian/control.wheezy /ZeroTierOne/debian/control +RUN mv -f /ZeroTierOne/debian/rules.wheezy /ZeroTierOne/debian/rules diff --git a/attic/linux-build-farm/fedora-22/x64/Dockerfile b/attic/linux-build-farm/fedora-22/x64/Dockerfile new file mode 100644 index 00000000..6da0a921 --- /dev/null +++ b/attic/linux-build-farm/fedora-22/x64/Dockerfile @@ -0,0 +1,10 @@ +FROM fedora:22 +MAINTAINER Adam Ierymenko + +RUN yum update -y +RUN yum install -y make rpmdevtools gcc-c++ rubygem-ronn json-parser-devel lz4-devel http-parser-devel libnatpmp-devel + +RUN rpm --erase http-parser-devel +RUN yum install -y rubygem-ronn ruby + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/fedora-22/x86/Dockerfile b/attic/linux-build-farm/fedora-22/x86/Dockerfile new file mode 100644 index 00000000..3c24b844 --- /dev/null +++ b/attic/linux-build-farm/fedora-22/x86/Dockerfile @@ -0,0 +1,19 @@ +#FROM nickcis/fedora-32:22 +#MAINTAINER Adam Ierymenko + +#RUN mkdir -p /etc/dnf/vars +#RUN echo 'i386' >/etc/dnf/vars/basearch +#RUN echo 'i386' >/etc/dnf/vars/arch + +#RUN yum update -y +#RUN yum install -y make rpmdevtools gcc-c++ rubygem-ronn json-parser-devel lz4-devel http-parser-devel libnatpmp-devel + +FROM zerotier/zt1-build-fedora-22-x86-base +MAINTAINER Adam Ierymenko + +RUN echo 'i686-redhat-linux' >/etc/rpm/platform + +RUN rpm --erase http-parser-devel +RUN yum install -y rubygem-ronn ruby + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/make-apt-repos.sh b/attic/linux-build-farm/make-apt-repos.sh new file mode 100755 index 00000000..7a81cc5c --- /dev/null +++ b/attic/linux-build-farm/make-apt-repos.sh @@ -0,0 +1,16 @@ +#!/bin/bash + +# This builds a series of Debian repositories for each distribution. + +export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin + +for distro in debian-* ubuntu-*; do + if [ -n "`find ${distro} -name '*.deb' -type f`" ]; then + arches=`ls ${distro}/*.deb | cut -d _ -f 3 | cut -d . -f 1 | xargs | sed 's/ /,/g'` + distro_name=`echo $distro | cut -d '-' -f 2` + echo '---' $distro / $distro_name / $arches + aptly repo create -architectures=${arches} -comment="ZeroTier, Inc. Debian Packages" -component="main" -distribution=${distro_name} zt-release-${distro_name} + aptly repo add zt-release-${distro_name} ${distro}/*.deb + aptly publish repo zt-release-${distro_name} $distro_name + fi +done diff --git a/attic/linux-build-farm/make-rpm-repos.sh b/attic/linux-build-farm/make-rpm-repos.sh new file mode 100755 index 00000000..0ed1cfe4 --- /dev/null +++ b/attic/linux-build-farm/make-rpm-repos.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin + +GPG_KEY=contact@zerotier.com + +rm -rf /tmp/zt-rpm-repo +mkdir /tmp/zt-rpm-repo + +for distro in centos-* fedora-* amazon-*; do + dname=`echo $distro | cut -d '-' -f 1` + if [ "$dname" = "centos" ]; then + dname=el + fi + if [ "$dname" = "fedora" ]; then + dname=fc + fi + if [ "$dname" = "amazon" ]; then + dname=amzn1 + fi + dvers=`echo $distro | cut -d '-' -f 2` + + mkdir -p /tmp/zt-rpm-repo/$dname/$dvers + + cp -v $distro/*.rpm /tmp/zt-rpm-repo/$dname/$dvers +done + +rpmsign --resign --key-id=$GPG_KEY --digest-algo=sha256 `find /tmp/zt-rpm-repo -type f -name '*.rpm'` + +for db in `find /tmp/zt-rpm-repo -mindepth 2 -maxdepth 2 -type d`; do + createrepo --database $db +done + +# Stupid RHEL stuff +cd /tmp/zt-rpm-repo/el +ln -sf 6 6Client +ln -sf 6 6Workstation +ln -sf 6 6Server +ln -sf 6 6.0 +ln -sf 6 6.1 +ln -sf 6 6.2 +ln -sf 6 6.3 +ln -sf 6 6.4 +ln -sf 6 6.5 +ln -sf 6 6.6 +ln -sf 6 6.7 +ln -sf 6 6.8 +ln -sf 6 6.9 +ln -sf 7 7Client +ln -sf 7 7Workstation +ln -sf 7 7Server +ln -sf 7 7.0 +ln -sf 7 7.1 +ln -sf 7 7.2 +ln -sf 7 7.3 +ln -sf 7 7.4 +ln -sf 7 7.5 +ln -sf 7 7.6 +ln -sf 7 7.7 +ln -sf 7 7.8 +ln -sf 7 7.9 + +echo +echo Repo created in /tmp/zt-rpm-repo diff --git a/attic/linux-build-farm/other/zerotier-containerized/Dockerfile b/attic/linux-build-farm/other/zerotier-containerized/Dockerfile new file mode 100644 index 00000000..678216da --- /dev/null +++ b/attic/linux-build-farm/other/zerotier-containerized/Dockerfile @@ -0,0 +1,20 @@ +FROM alpine:latest +MAINTAINER Adam Ierymenko + +LABEL version="1.1.14" +LABEL description="Containerized ZeroTier One for use on CoreOS or other Docker-only Linux hosts." + +# Uncomment to build in container +#RUN apk add --update alpine-sdk linux-headers + +RUN apk add --update libgcc libstdc++ + +ADD zerotier-one / +RUN chmod 0755 /zerotier-one +RUN ln -sf /zerotier-one /zerotier-cli +RUN mkdir -p /var/lib/zerotier-one + +ADD main.sh / +RUN chmod 0755 /main.sh + +ENTRYPOINT /main.sh diff --git a/attic/linux-build-farm/other/zerotier-containerized/main.sh b/attic/linux-build-farm/other/zerotier-containerized/main.sh new file mode 100755 index 00000000..685a6891 --- /dev/null +++ b/attic/linux-build-farm/other/zerotier-containerized/main.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin + +if [ ! -e /dev/net/tun ]; then + echo 'FATAL: cannot start ZeroTier One in container: /dev/net/tun not present.' + exit 1 +fi + +exec /zerotier-one diff --git a/attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile b/attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile new file mode 100644 index 00000000..f84cc6e3 --- /dev/null +++ b/attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:14.04 +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.6 + +RUN ln -sf /usr/bin/clang++-3.6 /usr/bin/clang++ +RUN ln -sf /usr/bin/clang-3.6 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile b/attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile new file mode 100644 index 00000000..6be3ae87 --- /dev/null +++ b/attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile @@ -0,0 +1,12 @@ +FROM 32bit/ubuntu:14.04 +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.6 + +RUN ln -sf /usr/bin/clang++-3.6 /usr/bin/clang++ +RUN ln -sf /usr/bin/clang-3.6 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-wily/x64/Dockerfile b/attic/linux-build-farm/ubuntu-wily/x64/Dockerfile new file mode 100644 index 00000000..99b8d34c --- /dev/null +++ b/attic/linux-build-farm/ubuntu-wily/x64/Dockerfile @@ -0,0 +1,12 @@ +FROM ubuntu:wily +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.7 + +RUN ln -sf /usr/bin/clang++-3.7 /usr/bin/clang++ +RUN ln -sf /usr/bin/clang-3.7 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-wily/x86/Dockerfile b/attic/linux-build-farm/ubuntu-wily/x86/Dockerfile new file mode 100644 index 00000000..86ad14f2 --- /dev/null +++ b/attic/linux-build-farm/ubuntu-wily/x86/Dockerfile @@ -0,0 +1,12 @@ +FROM daald/ubuntu32:wily +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.7 + +RUN ln -sf /usr/bin/clang++-3.7 /usr/bin/clang++ +RUN ln -sf /usr/bin/clang-3.7 /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile b/attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile new file mode 100644 index 00000000..fa665a0a --- /dev/null +++ b/attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile @@ -0,0 +1,14 @@ +FROM ubuntu:xenial +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.8 + +#RUN ln -sf /usr/bin/clang++-3.8 /usr/bin/clang++ +#RUN ln -sf /usr/bin/clang-3.8 /usr/bin/clang + +RUN rm -f /usr/bin/clang++ /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile b/attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile new file mode 100644 index 00000000..d01eec9b --- /dev/null +++ b/attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile @@ -0,0 +1,14 @@ +FROM f69m/ubuntu32:xenial +MAINTAINER Adam Ierymenko + +RUN apt-get update +RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.8 + +#RUN ln -sf /usr/bin/clang++-3.8 /usr/bin/clang++ +#RUN ln -sf /usr/bin/clang-3.8 /usr/bin/clang + +RUN rm -f /usr/bin/clang++ /usr/bin/clang + +RUN dpkg --purge libhttp-parser-dev + +ADD zt1-src.tar.gz / diff --git a/linux-build-farm/README.md b/linux-build-farm/README.md deleted file mode 100644 index 8055eb0b..00000000 --- a/linux-build-farm/README.md +++ /dev/null @@ -1,8 +0,0 @@ -Dockerized Linux Build Farm -====== - -This subfolder contains Dockerfiles and a script to build Linux packages for a variety of Linux distributions. It's also an excellent way to test your CPU fans and stress test your disk. - -Running `build.sh` with no arguments builds everything. You can run `build.sh` with the name of a distro (e.g. centos-7) to only build that. Both 32 and 64 bit packages are built except where no 32-bit version of the distribution exists. - -The `make-apt-repos.sh` and `make-rpm-repos.sh` scripts build repositories. They may require some editing for outside-of-ZeroTier use, and be careful with the apt one if you have an existing *aptly* configuration. diff --git a/linux-build-farm/amazon-2016.03/x64/Dockerfile b/linux-build-farm/amazon-2016.03/x64/Dockerfile deleted file mode 100644 index bd1a246a..00000000 --- a/linux-build-farm/amazon-2016.03/x64/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -#FROM ambakshi/amazon-linux:2016.03 -#MAINTAINER Adam Ierymenko - -#RUN yum update -y -#RUN yum install -y epel-release -#RUN yum install -y make development-tools rpmdevtools clang gcc-c++ ruby ruby-devel - -#RUN gem install ronn - -FROM zerotier/zt1-build-amazon-2016.03-x64-base -MAINTAINER Adam Ierymenko - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/build.sh b/linux-build-farm/build.sh deleted file mode 100755 index 0eb7c5d2..00000000 --- a/linux-build-farm/build.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin - -subdirs=$* -if [ ! -n "$subdirs" ]; then - subdirs=`find . -type d -name '*-*' -printf '%f '` -fi - -if [ ! -d ./ubuntu-trusty ]; then - echo 'Must run from linux-build-farm subfolder.' - exit 1 -fi - -rm -f zt1-src.tar.gz -cd .. -git archive --format=tar.gz --prefix=ZeroTierOne/ -o linux-build-farm/zt1-src.tar.gz HEAD -cd linux-build-farm - -# Note that --privileged is used so we can bind mount VM shares when building in a VM. -# It has no other impact or purpose, but probably doesn't matter here in any case. - -for distro in $subdirs; do - echo - echo "--- BUILDING FOR $distro ---" - echo - - cd $distro - - if [ -d x64 ]; then - cd x64 - mv ../../zt1-src.tar.gz . - docker build -t zt1-build-${distro}-x64 . - mv zt1-src.tar.gz ../.. - cd .. - fi - - if [ -d x86 ]; then - cd x86 - mv ../../zt1-src.tar.gz . - docker build -t zt1-build-${distro}-x86 . - mv zt1-src.tar.gz ../.. - cd .. - fi - - rm -f *.deb *.rpm - -# exit 0 - - if [ ! -n "`echo $distro | grep -F debian`" -a ! -n "`echo $distro | grep -F ubuntu`" ]; then - if [ -d x64 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x64 /bin/bash -c 'cd /ZeroTierOne ; make redhat ; cd .. ; cp `find /root/rpmbuild -type f -name *.rpm` /artifacts ; ls -l /artifacts' - fi - if [ -d x86 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x86 /bin/bash -c 'cd /ZeroTierOne ; make redhat ; cd .. ; cp `find /root/rpmbuild -type f -name *.rpm` /artifacts ; ls -l /artifacts' - fi - else - if [ -d x64 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x64 /bin/bash -c 'cd /ZeroTierOne ; make debian ; cd .. ; cp *.deb /artifacts ; ls -l /artifacts' - fi - if [ -d x86 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x86 /bin/bash -c 'cd /ZeroTierOne ; make debian ; cd .. ; cp *.deb /artifacts ; ls -l /artifacts' - fi - fi - - cd .. -done - -rm -f zt1-src.tar.gz diff --git a/linux-build-farm/centos-6/x64/Dockerfile b/linux-build-farm/centos-6/x64/Dockerfile deleted file mode 100644 index 2796e422..00000000 --- a/linux-build-farm/centos-6/x64/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM centos:6 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y epel-release -RUN yum install -y make development-tools rpmdevtools clang gcc-c++ tar - -RUN yum install -y nodejs npm - -# Stop use of http-parser-devel which is installed by nodejs/npm -RUN rm -f /usr/include/http_parser.h - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/centos-6/x86/Dockerfile b/linux-build-farm/centos-6/x86/Dockerfile deleted file mode 100644 index 8192d139..00000000 --- a/linux-build-farm/centos-6/x86/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM toopher/centos-i386:centos6 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y epel-release -RUN yum install -y make development-tools rpmdevtools clang gcc-c++ tar - -RUN yum install -y nodejs npm - -# Stop use of http-parser-devel which is installed by nodejs/npm -RUN rm -f /usr/include/http_parser.h - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/centos-7/x64/Dockerfile b/linux-build-farm/centos-7/x64/Dockerfile deleted file mode 100644 index 10b58402..00000000 --- a/linux-build-farm/centos-7/x64/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM centos:7 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y epel-release -RUN yum install -y make development-tools rpmdevtools clang gcc-c++ ruby ruby-devel - -RUN gem install ronn - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/centos-7/x86/Dockerfile b/linux-build-farm/centos-7/x86/Dockerfile deleted file mode 100644 index a637a8d3..00000000 --- a/linux-build-farm/centos-7/x86/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -#FROM zerotier/centos7-32bit -#MAINTAINER Adam Ierymenko - -#RUN echo 'i686-redhat-linux' >/etc/rpm/platform - -#RUN yum update -y -#RUN yum install -y make development-tools rpmdevtools http-parser-devel lz4-devel libnatpmp-devel - -#RUN yum install -y gcc-c++ -#RUN rpm --install --force https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm -#RUN rpm --install --force ftp://rpmfind.net/linux/centos/6.8/os/i386/Packages/libffi-3.0.5-3.2.el6.i686.rpm -#RUN yum install -y clang - -FROM zerotier/zt1-build-centos-7-x86-base -MAINTAINER Adam Ierymenko - -RUN yum install -y ruby ruby-devel -RUN gem install ronn - -#RUN rpm --erase http-parser-devel lz4-devel libnatpmp-devel - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/debian-jessie/x64/Dockerfile b/linux-build-farm/debian-jessie/x64/Dockerfile deleted file mode 100644 index 316c1d83..00000000 --- a/linux-build-farm/debian-jessie/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM debian:jessie -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.5 - -RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/debian-jessie/x86/Dockerfile b/linux-build-farm/debian-jessie/x86/Dockerfile deleted file mode 100644 index 3ad83329..00000000 --- a/linux-build-farm/debian-jessie/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM 32bit/debian:jessie -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.5 - -RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/debian-stretch/x64/Dockerfile b/linux-build-farm/debian-stretch/x64/Dockerfile deleted file mode 100644 index c973c2b7..00000000 --- a/linux-build-farm/debian-stretch/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM debian:stretch -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang - -#RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/debian-stretch/x86/Dockerfile b/linux-build-farm/debian-stretch/x86/Dockerfile deleted file mode 100644 index bfc7a86f..00000000 --- a/linux-build-farm/debian-stretch/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM mcandre/docker-debian-32bit:stretch -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang - -#RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/debian-wheezy/x64/Dockerfile b/linux-build-farm/debian-wheezy/x64/Dockerfile deleted file mode 100644 index 77e1c325..00000000 --- a/linux-build-farm/debian-wheezy/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM debian:wheezy -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper ruby-ronn g++ make devscripts - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / - -RUN mv -f /ZeroTierOne/debian/control.wheezy /ZeroTierOne/debian/control -RUN mv -f /ZeroTierOne/debian/rules.wheezy /ZeroTierOne/debian/rules diff --git a/linux-build-farm/debian-wheezy/x86/Dockerfile b/linux-build-farm/debian-wheezy/x86/Dockerfile deleted file mode 100644 index 1f0117d2..00000000 --- a/linux-build-farm/debian-wheezy/x86/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -#FROM tubia/debian:wheezy -#MAINTAINER Adam Ierymenko - -#RUN apt-get update -#RUN apt-get install -y build-essential debhelper ruby-ronn g++ make devscripts - -FROM zerotier/zt1-build-debian-wheezy-x86-base -MAINTAINER Adam Ierymenko - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / - -RUN mv -f /ZeroTierOne/debian/control.wheezy /ZeroTierOne/debian/control -RUN mv -f /ZeroTierOne/debian/rules.wheezy /ZeroTierOne/debian/rules diff --git a/linux-build-farm/fedora-22/x64/Dockerfile b/linux-build-farm/fedora-22/x64/Dockerfile deleted file mode 100644 index 6da0a921..00000000 --- a/linux-build-farm/fedora-22/x64/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM fedora:22 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y make rpmdevtools gcc-c++ rubygem-ronn json-parser-devel lz4-devel http-parser-devel libnatpmp-devel - -RUN rpm --erase http-parser-devel -RUN yum install -y rubygem-ronn ruby - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/fedora-22/x86/Dockerfile b/linux-build-farm/fedora-22/x86/Dockerfile deleted file mode 100644 index 3c24b844..00000000 --- a/linux-build-farm/fedora-22/x86/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -#FROM nickcis/fedora-32:22 -#MAINTAINER Adam Ierymenko - -#RUN mkdir -p /etc/dnf/vars -#RUN echo 'i386' >/etc/dnf/vars/basearch -#RUN echo 'i386' >/etc/dnf/vars/arch - -#RUN yum update -y -#RUN yum install -y make rpmdevtools gcc-c++ rubygem-ronn json-parser-devel lz4-devel http-parser-devel libnatpmp-devel - -FROM zerotier/zt1-build-fedora-22-x86-base -MAINTAINER Adam Ierymenko - -RUN echo 'i686-redhat-linux' >/etc/rpm/platform - -RUN rpm --erase http-parser-devel -RUN yum install -y rubygem-ronn ruby - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/make-apt-repos.sh b/linux-build-farm/make-apt-repos.sh deleted file mode 100755 index 7a81cc5c..00000000 --- a/linux-build-farm/make-apt-repos.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -# This builds a series of Debian repositories for each distribution. - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -for distro in debian-* ubuntu-*; do - if [ -n "`find ${distro} -name '*.deb' -type f`" ]; then - arches=`ls ${distro}/*.deb | cut -d _ -f 3 | cut -d . -f 1 | xargs | sed 's/ /,/g'` - distro_name=`echo $distro | cut -d '-' -f 2` - echo '---' $distro / $distro_name / $arches - aptly repo create -architectures=${arches} -comment="ZeroTier, Inc. Debian Packages" -component="main" -distribution=${distro_name} zt-release-${distro_name} - aptly repo add zt-release-${distro_name} ${distro}/*.deb - aptly publish repo zt-release-${distro_name} $distro_name - fi -done diff --git a/linux-build-farm/make-rpm-repos.sh b/linux-build-farm/make-rpm-repos.sh deleted file mode 100755 index 0ed1cfe4..00000000 --- a/linux-build-farm/make-rpm-repos.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -GPG_KEY=contact@zerotier.com - -rm -rf /tmp/zt-rpm-repo -mkdir /tmp/zt-rpm-repo - -for distro in centos-* fedora-* amazon-*; do - dname=`echo $distro | cut -d '-' -f 1` - if [ "$dname" = "centos" ]; then - dname=el - fi - if [ "$dname" = "fedora" ]; then - dname=fc - fi - if [ "$dname" = "amazon" ]; then - dname=amzn1 - fi - dvers=`echo $distro | cut -d '-' -f 2` - - mkdir -p /tmp/zt-rpm-repo/$dname/$dvers - - cp -v $distro/*.rpm /tmp/zt-rpm-repo/$dname/$dvers -done - -rpmsign --resign --key-id=$GPG_KEY --digest-algo=sha256 `find /tmp/zt-rpm-repo -type f -name '*.rpm'` - -for db in `find /tmp/zt-rpm-repo -mindepth 2 -maxdepth 2 -type d`; do - createrepo --database $db -done - -# Stupid RHEL stuff -cd /tmp/zt-rpm-repo/el -ln -sf 6 6Client -ln -sf 6 6Workstation -ln -sf 6 6Server -ln -sf 6 6.0 -ln -sf 6 6.1 -ln -sf 6 6.2 -ln -sf 6 6.3 -ln -sf 6 6.4 -ln -sf 6 6.5 -ln -sf 6 6.6 -ln -sf 6 6.7 -ln -sf 6 6.8 -ln -sf 6 6.9 -ln -sf 7 7Client -ln -sf 7 7Workstation -ln -sf 7 7Server -ln -sf 7 7.0 -ln -sf 7 7.1 -ln -sf 7 7.2 -ln -sf 7 7.3 -ln -sf 7 7.4 -ln -sf 7 7.5 -ln -sf 7 7.6 -ln -sf 7 7.7 -ln -sf 7 7.8 -ln -sf 7 7.9 - -echo -echo Repo created in /tmp/zt-rpm-repo diff --git a/linux-build-farm/other/zerotier-containerized/Dockerfile b/linux-build-farm/other/zerotier-containerized/Dockerfile deleted file mode 100644 index 678216da..00000000 --- a/linux-build-farm/other/zerotier-containerized/Dockerfile +++ /dev/null @@ -1,20 +0,0 @@ -FROM alpine:latest -MAINTAINER Adam Ierymenko - -LABEL version="1.1.14" -LABEL description="Containerized ZeroTier One for use on CoreOS or other Docker-only Linux hosts." - -# Uncomment to build in container -#RUN apk add --update alpine-sdk linux-headers - -RUN apk add --update libgcc libstdc++ - -ADD zerotier-one / -RUN chmod 0755 /zerotier-one -RUN ln -sf /zerotier-one /zerotier-cli -RUN mkdir -p /var/lib/zerotier-one - -ADD main.sh / -RUN chmod 0755 /main.sh - -ENTRYPOINT /main.sh diff --git a/linux-build-farm/other/zerotier-containerized/main.sh b/linux-build-farm/other/zerotier-containerized/main.sh deleted file mode 100755 index 685a6891..00000000 --- a/linux-build-farm/other/zerotier-containerized/main.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -if [ ! -e /dev/net/tun ]; then - echo 'FATAL: cannot start ZeroTier One in container: /dev/net/tun not present.' - exit 1 -fi - -exec /zerotier-one diff --git a/linux-build-farm/ubuntu-trusty/x64/Dockerfile b/linux-build-farm/ubuntu-trusty/x64/Dockerfile deleted file mode 100644 index f84cc6e3..00000000 --- a/linux-build-farm/ubuntu-trusty/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM ubuntu:14.04 -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.6 - -RUN ln -sf /usr/bin/clang++-3.6 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.6 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/ubuntu-trusty/x86/Dockerfile b/linux-build-farm/ubuntu-trusty/x86/Dockerfile deleted file mode 100644 index 6be3ae87..00000000 --- a/linux-build-farm/ubuntu-trusty/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM 32bit/ubuntu:14.04 -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.6 - -RUN ln -sf /usr/bin/clang++-3.6 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.6 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/ubuntu-wily/x64/Dockerfile b/linux-build-farm/ubuntu-wily/x64/Dockerfile deleted file mode 100644 index 99b8d34c..00000000 --- a/linux-build-farm/ubuntu-wily/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM ubuntu:wily -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.7 - -RUN ln -sf /usr/bin/clang++-3.7 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.7 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/ubuntu-wily/x86/Dockerfile b/linux-build-farm/ubuntu-wily/x86/Dockerfile deleted file mode 100644 index 86ad14f2..00000000 --- a/linux-build-farm/ubuntu-wily/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM daald/ubuntu32:wily -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.7 - -RUN ln -sf /usr/bin/clang++-3.7 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.7 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/ubuntu-xenial/x64/Dockerfile b/linux-build-farm/ubuntu-xenial/x64/Dockerfile deleted file mode 100644 index fa665a0a..00000000 --- a/linux-build-farm/ubuntu-xenial/x64/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM ubuntu:xenial -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.8 - -#RUN ln -sf /usr/bin/clang++-3.8 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.8 /usr/bin/clang - -RUN rm -f /usr/bin/clang++ /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/linux-build-farm/ubuntu-xenial/x86/Dockerfile b/linux-build-farm/ubuntu-xenial/x86/Dockerfile deleted file mode 100644 index d01eec9b..00000000 --- a/linux-build-farm/ubuntu-xenial/x86/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM f69m/ubuntu32:xenial -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.8 - -#RUN ln -sf /usr/bin/clang++-3.8 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.8 /usr/bin/clang - -RUN rm -f /usr/bin/clang++ /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/make-linux.mk b/make-linux.mk index ceb97a8a..ed8ec44b 100644 --- a/make-linux.mk +++ b/make-linux.mk @@ -113,7 +113,7 @@ endif #LDFLAGS= #STRIP=echo -all: one manpages +all: one one: $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o osdep/LinuxDropPrivileges.o $(CXX) $(CXXFLAGS) $(LDFLAGS) -o zerotier-one $(OBJS) service/OneService.o one.o osdep/LinuxEthernetTap.o osdep/LinuxDropPrivileges.o $(LDLIBS) @@ -131,13 +131,9 @@ manpages: FORCE doc: manpages clean: FORCE - rm -rf *.so *.o node/*.o controller/*.o osdep/*.o service/*.o ext/http-parser/*.o ext/lz4/*.o ext/json-parser/*.o ext/miniupnpc/*.o ext/libnatpmp/*.o $(OBJS) zerotier-one zerotier-idtool zerotier-cli zerotier-selftest build-* ZeroTierOneInstaller-* *.deb *.rpm .depend doc/*.1 doc/*.2 doc/*.8 debian/files debian/zerotier-one*.debhelper debian/zerotier-one.substvars debian/*.log debian/zerotier-one + rm -rf *.so *.o node/*.o controller/*.o osdep/*.o service/*.o ext/http-parser/*.o ext/lz4/*.o ext/json-parser/*.o ext/miniupnpc/*.o ext/libnatpmp/*.o $(OBJS) zerotier-one zerotier-idtool zerotier-cli zerotier-selftest build-* ZeroTierOneInstaller-* *.deb *.rpm .depend debian/files debian/zerotier-one*.debhelper debian/zerotier-one.substvars debian/*.log debian/zerotier-one doc/node_modules distclean: clean - rm -rf doc/node_modules - find linux-build-farm -type f -name '*.deb' -print0 | xargs -0 rm -fv - find linux-build-farm -type f -name '*.rpm' -print0 | xargs -0 rm -fv - find linux-build-farm -type f -name 'zt1-src.tar.gz' | xargs rm -fv realclean: distclean -- cgit v1.2.3 From 4a7c76a11bb799442e5f57954fd75d57a197b3d9 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 20 Jan 2017 10:51:55 -0800 Subject: docs, cleanup, temporarily put cli in attic since it is not done. --- README.md | 73 ++-- attic/cli/README.md | 57 +++ attic/cli/zerotier.cpp | 957 +++++++++++++++++++++++++++++++++++++++++++++++++ cli/README.md | 57 --- cli/zerotier.cpp | 957 ------------------------------------------------- 5 files changed, 1060 insertions(+), 1041 deletions(-) create mode 100644 attic/cli/README.md create mode 100644 attic/cli/zerotier.cpp delete mode 100644 cli/README.md delete mode 100644 cli/zerotier.cpp (limited to 'attic') diff --git a/README.md b/README.md index 5c6ff381..5be0c0ce 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,7 @@ ZeroTier - A Planetary Ethernet Switch ====== -ZeroTier is a software-based managed Ethernet switch for planet Earth. - -It erases the LAN/WAN distinction and makes VPNs, tunnels, proxies, and other kludges arising from the inflexible nature of physical networks obsolete. Everything is encrypted end-to-end and traffic takes the most direct (peer to peer) path available. - -This repository contains ZeroTier One, a service that provides ZeroTier network connectivity to devices running Windows, Mac, Linux, iOS, Android, and FreeBSD and makes joining virtual networks as easy as joining IRC or Slack channels. It also contains the OS-independent core ZeroTier protocol implementation in [node/](node/). +ZeroTier is an advanced SDN Ethernet switch for planet Earth. It erases the LAN/WAN distinction and makes VPNs, tunnels, proxies, and other kludges arising from the inflexible nature of physical networks obsolete. Everything is encrypted end-to-end and traffic takes the most direct (peer to peer) path available. Visit [ZeroTier's site](https://www.zerotier.com/) for more information and [pre-built binary packages](https://www.zerotier.com/download.shtml). Apps for Android and iOS are available for free in the Google Play and Apple app stores. @@ -13,36 +9,59 @@ Visit [ZeroTier's site](https://www.zerotier.com/) for more information and [pre ZeroTier's basic operation is easy to understand. Devices have 10-digit *ZeroTier addresses* like `89e92ceee5` and networks have 16-digit network IDs like `8056c2e21c000001`. All it takes for a device to join a network is its 16-digit ID, and all it takes for a network to authorize a device is its 10-digit address. Everything else is automatic. -A "device" can be anything really: desktops, laptops, phones, servers, VMs/VPSes, containers, and even (soon) apps. +A "device" in our terminology is any "unit of compute" capable of talking to a network: desktops, laptops, phones, servers, VMs/VPSes, containers, and even user-space applications via our [SDK](https://github.com/zerotier/ZeroTierSDK). -For testing we provide a public virtual network called *Earth* with network ID `8056c2e21c000001`. On Linux and Mac you can do this with: +For testing purposes we provide a public virtual network called *Earth* with network ID `8056c2e21c000001`. You can join it with: sudo zerotier-cli join 8056c2e21c000001 -Now wait about 30 seconds and check your system with `ip addr list` or `ifconfig`. You'll see a new interface whose name starts with *zt* and it should quickly get an IPv4 and an IPv6 address. Once you see it get an IP, try pinging `earth.zerotier.net` at `29.209.112.93`. If you've joined Earth from more than one system, try pinging your other machine. - -*(IPv4 addresses for Earth are assigned from the block 28.0.0.0/7, which is not a part of the public Internet but is non-standard for private networks. It's used to avoid IP conflicts during testing. Your networks can run any IP addressing scheme you want.)* - -If you don't want to belong to a giant Ethernet party line anymore, just type: +Now wait about 30 seconds and check your system with `ip addr list` or `ifconfig`. You'll see a new interface whose name starts with *zt* and it should quickly get an IPv4 and an IPv6 address. Once you see it get an IP, try pinging `earth.zerotier.net` at `29.209.112.93`. If you've joined Earth from more than one system, try pinging your other machine. If you don't want to belong to a giant Ethernet party line anymore, just type: sudo zerotier-cli leave 8056c2e21c000001 The *zt* interface will disappear. You're no longer on the network. -To create networks of your own you'll need a network controller. You can use [our hosted controller at my.zerotier.com](https://my.zerotier.com) which is free for up to 100 devices on an unlimited number of networks, or you can build your own controller and run it through its local JSON API. See [README.md in controller/](controller/) for more information. - -### Building from Source - -For Mac, Linux, and BSD, just type `make` (or `gmake` on BSD). Platform requirements are: - - - **Mac**: Xcode command line tools for OSX 10.7 or newer. - - **Linux**: GCC/G++ 4.9 or newer or CLANG 3.4 or newer, and GNU make and standard shell tools of course. - - The Linux make file will auto-detect and prefer CLANG if present since in our experience it does a better job optimizing C++ code. You can specify CC= and CXX= on the make command line to override this behavior. - - Several distributions including CentOS 7 ship with G++ 4.8 which has broken C++11 support. If you are running CentOS 7 type `sudo yum install clang` to install CLANG 3.4, which will work fine. There is no C++11 in the ZeroTier core but we have started using it in the service and network controller code. - - If any of the following have development headers on the system they are auto-detected and the build will link against system versions: `libnatpmp`, and `miniupnpc`. If these are missing (or too old) the versions in `ext/` are used and are statically linked into the binary. Please be aware of this since if you build with these present the resulting binary will require them on other systems too. - - **FreeBSD**: GCC/G++ 4.9 or newer and `gmake` since our make files use GNU extensions. - -Each supported platform has its own *make-XXX.mk* file that contains the actual make rules for the platform. The right .mk file is included by the main Makefile based on the GNU make *OSTYPE* variable. Take a look at the .mk file for your platform for other targets, debug build rules, etc. +To create networks of your own, you'll need a network controller. ZeroTier One (for desktops and servers) includes controller functionality in its default build that can be configured via its JSON API (see [README.md in controller/](controller/)). ZeroTier provides a hosted solution with a nice web UI and SaaS add-ons at [my.zerotier.com](https://my.zerotier.com/). Basic controller functionality is free for up to 100 devices. + +### Project Layout + + - `artwork/`: icons, logos, etc. + - `attic/`: old stuff and experimental code that we want to keep around for reference. + - `controller/`: the reference network controller implementation, which is built and included by default on desktop and server build targets. + - `debian/`: files for building Debian packages on Linux. + - `doc/`: manual pages and other documentation. + - `ext/`: third party libraries, binaries that we ship for convenience on some platforms (Mac and Windows), and installation support files. + - `include/`: include files for the ZeroTier core. + - `java/`: a JNI wrapper used with our Android mobile app. (The whole Android app is not open source but may be made so in the future.) + - `macui/`: a Macintosh menu-bar app for controlling ZeroTier One, written in Objective C. + - `node/`: the ZeroTier virtual Ethernet switch core, which is designed to be entirely separate from the rest of the code and able to be built as a stand-alone OS-independent library. Note to developers: do not use C++11 features in here, since we want this to build on old embedded platforms that lack C++11 support. C++11 can be used elsewhere. + - `osdep/`: code to support and integrate with OSes, including platform-specific stuff only built for certain targets. + - `service/`: the ZeroTier One service, which wraps the ZeroTier core and provides VPN-like connectivity to virtual networks for desktops, laptops, servers, VMs, and containers. + - `tcp-proxy/`: TCP proxy code run by ZeroTier, Inc. to provide TCP fallback (this will die soon!). + - `windows/`: Visual Studio solution files, Windows service code for ZeroTier One, and the Windows task bar app UI. + - `world/`: ZeroTier "world" definition and source for building and signing it -- normally only of use to ZeroTier, Inc. A "world" is one virtual switch. We've defined one for planet Earth and surrounding space and operate a network of free anchor points (root servers) for it. + +The base path contains the ZeroTier One service main entry point (`one.cpp`), self test code, makefiles, etc. + +### Build and Platform Notes + +To build on Mac and Linux just type `make`. On FreeBSD and OpenBSD `gmake` (GNU make) is required and can be installed from packages or ports. For Windows there is a Visual Studio solution in `windows/'. + + - **Mac** + - Xcode command line tools for OSX 10.7 or newer are required. + - Tap device driver kext source is in `ext/tap-mac` and a signed pre-built binary can be found in `ext/bin/tap-mac`. You should not need to build it yourself. It's a fork of [tuntaposx](http://tuntaposx.sourceforge.net) with device names changed to `zt#`, support for a larger MTU, and tun functionality removed. + - **Linux** + - The minimum compiler versions required are GCC/G++ 4.9.3 or CLANG/CLANG++ 3.4.2. + - Linux makefiles automatically detect and prefer clang/clang++ if present as it produces smaller and slightly faster binaries in most cases. You can override by supplying CC and CXX variables on the make command line. + - CentOS 7 ships with a version of GCC/G++ that is too old, but a new enough version of CLANG can be found in the *epel* repositories. Type `yum install epel-release` and then `yum install clang` to build there. + - **FreeBSD** + - Tested most recently on FreeBSD-11. Older versions may work but we're not sure. + - GCC/G++ 4.9 and gmake are required. These can be installed from packages or ports. Type `gmake` to build. + - **OpenBSD** + - There is a limit of four network memberships on OpenBSD as there are only four tap devices (`/dev/tap0` through `/dev/tap3`). We're not sure if this can be increased. + - OpenBSD lacks `getifmaddrs` (or any equivalent method) to get interface multicast memberships. As a result multicast will only work on OpenBSD for ARP and NDP (IP/MAC lookup) and not for other purposes. + - Only tested on OpenBSD 6.0. Older versions may not work. + - GCC/G++ 4.9 and gmake are required and can be installed using `pkg_add` or from ports. They get installed in `/usr/local/bin` as `egcc` and `eg++` and our makefile is pre-configured to use them on OpenBSD. Typing `make selftest` will build a *zerotier-selftest* binary which unit tests various internals and reports on a few aspects of the build environment. It's a good idea to try this on novel platforms or architectures. @@ -65,7 +84,7 @@ The service is controlled via the JSON API, which by default is available at 127 Here's where home folders live (by default) on each OS: * **Linux**: `/var/lib/zerotier-one` - * **FreeBSD**: `/var/db/zerotier-one` + * **FreeBSD** / **OpenBSD**: `/var/db/zerotier-one` * **Mac**: `/Library/Application Support/ZeroTier/One` * **Windows**: `\ProgramData\ZeroTier\One` (That's for Windows 7. The base 'shared app data' folder might be different on different Windows versions.) diff --git a/attic/cli/README.md b/attic/cli/README.md new file mode 100644 index 00000000..595df07e --- /dev/null +++ b/attic/cli/README.md @@ -0,0 +1,57 @@ +The new ZeroTier CLI! +==== + +With this update we've expanded upon the previous CLI's functionality, so things should seem pretty familiar. Here are some of the new features we've introduced: + + - Create and administer networks on ZeroTier Central directly from the console. + - Service configurations, allows you to control local/remote instances of ZeroTier One + - Identity generation and management is now part of the same CLI tool + +*** +## Configurations + +Configurations are a way for you to nickname and logically organize the control of ZeroTier services running locally or remotely (this includes ZeroTier Central!). They're merely groupings of service API url's and auth tokens. The CLI's settings data is contained within `.zerotierCliSettings`. + +For instance, you can control your local instance of ZeroTier One via the `@local` config. By default it is represented as follows: + +``` +"local": { + "auth": "7tyqRoFytajf21j2l2t9QPm5", + "type": "one", + "url": "http://127.0.0.1:9993/" +} +``` + +As an example, if you issue the command `zerotier ls` is it implicitly stating `zerotier @local ls`. + +With the same line of thinking, you could create a `@my.zerotier.com` which would allow for something like `zerotier @my.zerotier.com net-create` which talks to our hosted ZeroTier Central to create a new network. + + + +## Command families + +- `cli-` is for configuring the settings data for the CLI itself, such as adding/removing `@thing` configurations, variables, etc. +- `net-` is for operating on a *ZeroTier Central* service such as `https://my.zerotier.com` +- `id-` is for handling ZeroTier identities. + +And those commands with no prefix are there to allow you to operate ZeroTier One instances either local or remote. + +*** +## Useful command examples + +*Add a ZeroTier One configuration:* + + - `zerotier cli-add-zt MyLocalConfigName https://127.0.0.1:9993/ ` + +*Add a ZeroTier Central configuration:* + + - `zerotier cli-add-central MyZTCentralConfigName https://my.zerotier.com/ ` + +*Set a default ZeroTier One instance:* + + - `zerotier cli-set defaultOne MyLocalConfigName` + +*Set a default ZeroTier Central:* + + - `zerotier cli-set defaultCentral MyZTCentralConfigName` + diff --git a/attic/cli/zerotier.cpp b/attic/cli/zerotier.cpp new file mode 100644 index 00000000..e75268d1 --- /dev/null +++ b/attic/cli/zerotier.cpp @@ -0,0 +1,957 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + */ + +// Note: unlike the rest of ZT's code base, this requires C++11 due to +// the JSON library it uses and other things. + +#include +#include +#include +#include + +#include "../node/Constants.hpp" +#include "../node/Identity.hpp" +#include "../version.h" +#include "../osdep/OSUtils.hpp" +#include "../ext/offbase/json/json.hpp" + +#ifdef __WINDOWS__ +#include +#include +#include +#include +#else +#include +#include +#endif + +#include +#include +#include +#include +#include +#include + +#include + +using json = nlohmann::json; +using namespace ZeroTier; + +#define ZT_CLI_FLAG_VERBOSE 'v' +#define ZT_CLI_FLAG_UNSAFE_SSL 'X' + +#define REQ_GET 0 +#define REQ_POST 1 +#define REQ_DEL 2 + +#define OK_STR "[OK ]: " +#define FAIL_STR "[FAIL]: " +#define WARN_STR "[WARN]: " +#define INVALID_ARGS_STR "Invalid args. Usage: " + +struct CLIState +{ + std::string atname; + std::string command; + std::string url; + std::map reqHeaders; + std::vector args; + std::map opts; + json settings; +}; + +namespace { + +static Identity getIdFromArg(char *arg) +{ + Identity id; + if ((strlen(arg) > 32)&&(arg[10] == ':')) { // identity is a literal on the command line + if (id.fromString(arg)) + return id; + } else { // identity is to be read from a file + std::string idser; + if (OSUtils::readFile(arg,idser)) { + if (id.fromString(idser)) + return id; + } + } + return Identity(); +} + +static std::string trimString(const std::string &s) +{ + unsigned long end = (unsigned long)s.length(); + while (end) { + char c = s[end - 1]; + if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t')) + --end; + else break; + } + unsigned long start = 0; + while (start < end) { + char c = s[start]; + if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t')) + ++start; + else break; + } + return s.substr(start,end - start); +} + +static inline std::string getSettingsFilePath() +{ +#ifdef __WINDOWS__ +#else + const char *home = getenv("HOME"); + if (!home) + home = "/"; + return (std::string(home) + "/.zerotierCliSettings"); +#endif +} + +static bool saveSettingsBackup(CLIState &state) +{ + std::string sfp(getSettingsFilePath().c_str()); + if(state.settings.find("generateBackupConfig") != state.settings.end() + && state.settings["generateBackupConfig"].get() == "true") { + std::string backup_file = getSettingsFilePath() + ".bak"; + if(!OSUtils::writeFile(sfp.c_str(), state.settings.dump(2))) { + OSUtils::lockDownFile(sfp.c_str(),false); + std::cout << WARN_STR << "unable to write backup config file" << std::endl; + return false; + } + return true; + } + return false; +} + +static bool saveSettings(CLIState &state) +{ + std::string sfp(getSettingsFilePath().c_str()); + if(OSUtils::writeFile(sfp.c_str(), state.settings.dump(2))) { + OSUtils::lockDownFile(sfp.c_str(),false); + std::cout << OK_STR << "changes saved." << std::endl; + return true; + } + std::cout << FAIL_STR << "unable to write to " << sfp << std::endl; + return false; +} + +static void dumpHelp() +{ + std::cout << "ZeroTier Newer-Spiffier CLI " << ZEROTIER_ONE_VERSION_MAJOR << "." << ZEROTIER_ONE_VERSION_MINOR << "." << ZEROTIER_ONE_VERSION_REVISION << std::endl; + std::cout << "(c)2016 ZeroTier, Inc. / Licensed under the GNU GPL v3" << std::endl; + std::cout << std::endl; + std::cout << "Configuration path: " << getSettingsFilePath() << std::endl; + std::cout << std::endl; + std::cout << "Usage: zerotier [-option] [@name] []" << std::endl; + std::cout << std::endl; + std::cout << "Options:" << std::endl; + std::cout << " -verbose - Verbose JSON output" << std::endl; + std::cout << " -X - Do not check SSL certs (CAUTION!)" << std::endl; + std::cout << std::endl; + std::cout << "CLI Configuration Commands:" << std::endl; + std::cout << " cli-set - Set a CLI option ('cli-set help')" << std::endl; + std::cout << " cli-unset - Un-sets a CLI option ('cli-unset help')" << std::endl; + std::cout << " cli-ls - List configured @things" << std::endl; + std::cout << " cli-rm @name - Remove a configured @thing" << std::endl; + std::cout << " cli-add-zt @name - Add a ZeroTier service" << std::endl; + std::cout << " cli-add-central @name - Add ZeroTier Central instance" << std::endl; + std::cout << std::endl; + std::cout << "ZeroTier One Service Commands:" << std::endl; + std::cout << " -v / -version - Displays default local instance's version'" << std::endl; + std::cout << " ls - List currently joined networks" << std::endl; + std::cout << " join [opt=value ...] - Join a network" << std::endl; + std::cout << " leave - Leave a network" << std::endl; + std::cout << " peers - List ZeroTier VL1 peers" << std::endl; + std::cout << " show [] - Get info about self or object" << std::endl; + std::cout << std::endl; + std::cout << "Network Controller Commands:" << std::endl; + std::cout << " net-create - Create a new network" << std::endl; + std::cout << " net-rm - Delete a network (CAUTION!)" << std::endl; + std::cout << " net-ls - List administered networks" << std::endl; + std::cout << " net-members - List members of a network" << std::endl; + std::cout << " net-show [
] - Get network or member info" << std::endl; + std::cout << " net-auth
- Authorize a member" << std::endl; + std::cout << " net-unauth
- De-authorize a member" << std::endl; + std::cout << " net-set - See 'net-set help'" << std::endl; + std::cout << std::endl; + std::cout << "Identity Commands:" << std::endl; + std::cout << " id-generate [] - Generate a ZeroTier identity" << std::endl; + std::cout << " id-validate - Locally validate an identity" << std::endl; + std::cout << " id-sign - Sign a file" << std::endl; + std::cout << " id-verify - Verify a file's signature" << std::endl; + std::cout << " id-getpublic - Get full identity's public portion" << std::endl; + std::cout << std::endl; +} + +static size_t _curlStringAppendCallback(void *contents,size_t size,size_t nmemb,void *stdstring) +{ + size_t totalSize = size * nmemb; + reinterpret_cast(stdstring)->append((const char *)contents,totalSize); + return totalSize; +} + +static std::tuple REQUEST(int requestType, CLIState &state, const std::map &headers, const std::string &postfield, const std::string &url) +{ + std::string body; + char errbuf[CURL_ERROR_SIZE]; + char urlbuf[4096]; + + CURL *curl; + curl = curl_easy_init(); + if (!curl) { + std::cerr << "FATAL: curl_easy_init() failed" << std::endl; + exit(-1); + } + + Utils::scopy(urlbuf,sizeof(urlbuf),url.c_str()); + curl_easy_setopt(curl,CURLOPT_URL,urlbuf); + + struct curl_slist *hdrs = (struct curl_slist *)0; + for(std::map::const_iterator i(headers.begin());i!=headers.end();++i) { + std::string htmp(i->first); + htmp.append(": "); + htmp.append(i->second); + hdrs = curl_slist_append(hdrs,htmp.c_str()); + } + if (hdrs) + curl_easy_setopt(curl,CURLOPT_HTTPHEADER,hdrs); + + //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); + curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&body); + curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curlStringAppendCallback); + + if(std::find(state.args.begin(), state.args.end(), "-X") == state.args.end()) + curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,(state.opts.count(ZT_CLI_FLAG_UNSAFE_SSL) > 0) ? 0L : 1L); + + if(requestType == REQ_POST) { + curl_easy_setopt(curl, CURLOPT_POST, 1); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfield.c_str()); + } + if(requestType == REQ_DEL) + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + if(requestType == REQ_GET) { + curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errbuf); + curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,0L); + } + + curl_easy_setopt(curl,CURLOPT_USERAGENT,"ZeroTier-CLI"); + CURLcode res = curl_easy_perform(curl); + + errbuf[CURL_ERROR_SIZE-1] = (char)0; // sanity check + + if (res != CURLE_OK) + return std::make_tuple(-1,std::string(errbuf)); + + long response_code; + int rc = (int)curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &response_code); + + if(response_code == 401) { std::cout << FAIL_STR << response_code << "Unauthorized." << std::endl; exit(0); } + else if(response_code == 403) { std::cout << FAIL_STR << response_code << "Forbidden." << std::endl; exit(0); } + else if(response_code == 404) { std::cout << FAIL_STR << response_code << "Not found." << std::endl; exit(0); } + else if(response_code == 408) { std::cout << FAIL_STR << response_code << "Request timed out." << std::endl; exit(0); } + else if(response_code != 200) { std::cout << FAIL_STR << response_code << "Unable to process request." << std::endl; exit(0); } + + curl_easy_cleanup(curl); + if (hdrs) + curl_slist_free_all(hdrs); + return std::make_tuple(response_code,body); +} + +} // anonymous namespace + +////////////////////////////////////////////////////////////////////////////// + +// Check for user-specified @thing config +// Make sure it @thing makes sense +// Apply appropriate request headers +static void checkForThing(CLIState &state, std::string thingType, bool warnNoThingProvided) +{ + std::string configName; + if(state.atname.length()) { + configName = state.atname.erase(0,1); + // make sure specified @thing makes sense in the context of the command + if(thingType == "one" && state.settings["things"][configName]["type"].get() != "one") { + std::cout << FAIL_STR << "A ZeroTier Central config was specified for a ZeroTier One command." << std::endl; + exit(0); + } + if(thingType == "central" && state.settings["things"][configName]["type"].get() != "central") { + std::cout << FAIL_STR << "A ZeroTier One config was specified for a ZeroTier Central command." << std::endl; + exit(0); + } + } + else { // no @thing specified, check for defaults depending on type + if(thingType == "one") { + if(state.settings.find("defaultOne") != state.settings.end()) { + if(warnNoThingProvided) + std::cout << WARN_STR << "No @thing specified, assuming default for ZeroTier One command: " << state.settings["defaultOne"].get().c_str() << std::endl; + configName = state.settings["defaultOne"].get().erase(0,1); // get default + } + else { + std::cout << WARN_STR << "No @thing specified, and no default is known." << std::endl; + std::cout << "HELP: To set a default: zerotier cli-set defaultOne @my_default_thing" << std::endl; + exit(0); + } + } + if(thingType == "central") { + if(state.settings.find("defaultCentral") != state.settings.end()) { + if(warnNoThingProvided) + std::cout << WARN_STR << "No @thing specified, assuming default for ZeroTier Central command: " << state.settings["defaultCentral"].get().c_str() << std::endl; + configName = state.settings["defaultCentral"].get().erase(0,1); // get default + } + else { + std::cout << WARN_STR << "No @thing specified, and no default is known." << std::endl; + std::cout << "HELP: To set a default: zerotier cli-set defaultCentral @my_default_thing" << std::endl; + exit(0); + } + } + } + // Apply headers + if(thingType == "one") { + state.reqHeaders["X-ZT1-Auth"] = state.settings["things"][configName]["auth"]; + } + if(thingType == "central"){ + state.reqHeaders["Content-Type"] = "application/json"; + state.reqHeaders["Authorization"] = "Bearer " + state.settings["things"][configName]["auth"].get(); + state.reqHeaders["Accept"] = "application/json"; + } + state.url = state.settings["things"][configName]["url"]; +} + +static bool checkURL(std::string url) +{ + // TODO + return true; +} + +static std::string getLocalVersion(CLIState &state) +{ + json result; + std::tuple res; + checkForThing(state,"one",false); + res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "/status"); + if(std::get<0>(res) == 200) { + result = json::parse(std::get<1>(res)); + return result["version"].get(); + } + return "---"; +} + +#ifdef __WINDOWS__ +int _tmain(int argc, _TCHAR* argv[]) +#else +int main(int argc,char **argv) +#endif +{ +#ifdef __WINDOWS__ + { + WSADATA wsaData; + WSAStartup(MAKEWORD(2,2),&wsaData); + } +#endif + + curl_global_init(CURL_GLOBAL_DEFAULT); + CLIState state; + std::string arg1, arg2, authToken; + + for(int i=1;i 0)&&(port < 65536))&&(authToken.length() > 0)) { + state.settings["things"]["local"]["url"] = (std::string("http://127.0.0.1:") + portStr + "/"); + state.settings["things"]["local"]["auth"] = authToken; + initSuccess = true; + } + } + + if (!saveSettings(state)) { + std::cerr << "FATAL: unable to write " << getSettingsFilePath() << std::endl; + exit(-1); + } + + if (initSuccess) { + std::cerr << "INFO: initialized new config at " << getSettingsFilePath() << std::endl; + } else { + std::cerr << "INFO: initialized new config at " << getSettingsFilePath() << " but could not auto-init local ZeroTier One service config from " << oneHome << " -- you will need to set local service URL and port manually if you want to control a local instance of ZeroTier One. (This happens if you are not root/administrator.)" << std::endl; + } + } + } + + // PRE-REQUEST SETUP + + json result; + std::tuple res; + std::string url = ""; + + // META + + if ((state.command.length() == 0)||(state.command == "help")) { + dumpHelp(); + return -1; + } + + // zerotier version + else if (state.command == "v" || state.command == "version") { + std::cout << getLocalVersion(state) << std::endl; + return 1; + } + + // zerotier cli-set + else if (state.command == "cli-set") { + if(argc != 4) { + std::cerr << INVALID_ARGS_STR << "zerotier cli-set " << std::endl; + return 1; + } + std::string settingName, settingValue; + if(state.atname.length()) { // User provided @thing erroneously, we will ignore it and adjust argument indices + settingName = argv[3]; + settingValue = argv[4]; + } + else { + settingName = argv[2]; + settingValue = argv[3]; + } + saveSettingsBackup(state); + state.settings[settingName] = settingValue; // changes + saveSettings(state); + } + + // zerotier cli-unset + else if (state.command == "cli-unset") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier cli-unset " << std::endl; + return 1; + } + std::string settingName; + if(state.atname.length()) // User provided @thing erroneously, we will ignore it and adjust argument indices + settingName = argv[3]; + else + settingName = argv[2]; + saveSettingsBackup(state); + state.settings.erase(settingName); // changes + saveSettings(state); + } + + // zerotier @thing_to_remove cli-rm --- removes the configuration + else if (state.command == "cli-rm") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier cli-rm <@thing>" << std::endl; + return 1; + } + if(state.settings["things"].find(state.atname) != state.settings["things"].end()) { + if(state.settings["defaultOne"] == state.atname) { + std::cout << "WARNING: The config you're trying to remove is currently set as your default. Set a new default first!" << std::endl; + std::cout << " | Usage: zerotier set defaultOne @your_other_thing" << std::endl; + } + else { + state.settings["things"].erase(state.atname.c_str()); + saveSettings(state); + } + } + } + + // zerotier cli-add-zt + // TODO: Check for malformed urls/auth + else if (state.command == "cli-add-zt") { + if(argc != 5) { + std::cerr << INVALID_ARGS_STR << "zerotier cli-add-zt " << std::endl; + return 1; + } + std::string thing_name = argv[2], url = argv[3], auth = argv[4]; + if(!checkURL(url)) { + std::cout << FAIL_STR << "Malformed URL" << std::endl; + return 1; + } + if(state.settings.find(thing_name) != state.settings.end()) { + std::cout << "WARNING: A @thing with the shortname " << thing_name.c_str() + << " already exists. Choose another name or rename the old @thing" << std::endl; + std::cout << " | Usage: To rename a @thing: zerotier cli-rename @old_thing_name @new_thing_name" << std::endl; + } + else { + result = json::parse("{ \"auth\": \"" + auth + "\", \"type\": \"" + "one" + "\", \"url\": \"" + url + "\" }"); + saveSettingsBackup(state); + // TODO: Handle cases where user may or may not prepend an @ + state.settings["things"][thing_name] = result; // changes + saveSettings(state); + } + } + + // zerotier cli-add-central + // TODO: Check for malformed urls/auth + else if (state.command == "cli-add-central") { + if(argc != 5) { + std::cerr << INVALID_ARGS_STR << "zerotier cli-add-central " << std::endl; + return 1; + } + std::string thing_name = argv[2], url = argv[3], auth = argv[4]; + if(!checkURL(url)) { + std::cout << FAIL_STR << "Malformed URL" << std::endl; + return 1; + } + if(state.settings.find(thing_name) != state.settings.end()) { + std::cout << "WARNING: A @thing with the shortname " << thing_name.c_str() + << " already exists. Choose another name or rename the old @thing" << std::endl; + std::cout << " | Usage: To rename a @thing: zerotier cli-rename @old_thing_name @new_thing_name" << std::endl; + } + else { + result = json::parse("{ \"auth\": \"" + auth + "\", \"type\": \"" + "central" + "\", \"url\": \"" + url + "\" }"); + saveSettingsBackup(state); + // TODO: Handle cases where user may or may not prepend an @ + state.settings["things"]["@" + thing_name] = result; // changes + saveSettings(state); + } + } + + // ONE SERVICE + + // zerotier ls --- display all networks currently joined + else if (state.command == "ls" || state.command == "listnetworks") { + if(argc != 2) { + std::cerr << INVALID_ARGS_STR << "zerotier ls" << std::endl; + return 1; + } + checkForThing(state,"one",true); + url = state.url + "network"; + res = REQUEST(REQ_GET,state,state.reqHeaders,"",(const std::string)url); + if(std::get<0>(res) == 200) { + std::cout << "listnetworks " << std::endl; + auto j = json::parse(std::get<1>(res).c_str()); + if (j.type() == json::value_t::array) { + for(int i=0;i(); + std::string name = j[i]["name"].get(); + std::string mac = j[i]["mac"].get(); + std::string status = j[i]["status"].get(); + std::string type = j[i]["type"].get(); + std::string addrs; + for(int m=0; m() + " "; + } + std::string dev = j[i]["portDeviceName"].get(); + std::cout << "listnetworks " << nwid << " " << name << " " << mac << " " << status << " " << type << " " << dev << " " << addrs << std::endl; + } + } + } + } + + // zerotier join --- joins a network + else if (state.command == "join") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier join " << std::endl; + return 1; + } + checkForThing(state,"one",true); + res = REQUEST(REQ_POST,state,state.reqHeaders,"{}",state.url + "/network/" + state.args[0]); + if(std::get<0>(res) == 200) { + std::cout << OK_STR << "connected to " << state.args[0] << std::endl; + } + } + + // zerotier leave --- leaves a network + else if (state.command == "leave") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier leave " << std::endl; + return 1; + } + checkForThing(state,"one",true); + res = REQUEST(REQ_DEL,state,state.reqHeaders,"{}",state.url + "/network/" + state.args[0]); + if(std::get<0>(res) == 200) { + std::cout << OK_STR << "disconnected from " << state.args[0] << std::endl; + } + } + + // zerotier peers --- display address and role of all peers + else if (state.command == "peers") { + if(argc != 2) { + std::cerr << INVALID_ARGS_STR << "zerotier peers" << std::endl; + return 1; + } + checkForThing(state,"one",true); + res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "/peer"); + if(std::get<0>(res) == 200) { + json result = json::parse(std::get<1>(res)); + for(int i=0; i(res) == 200) { + result = json::parse(std::get<1>(res)); + std::string status_str = result["online"].get() ? "ONLINE" : "OFFLINE"; + std::cout << "info " << result["address"].get() + << " " << status_str << " " << result["version"].get() << std::endl; + } + } + + // REMOTE + + // zerotier @thing net-create --- creates a new network + else if (state.command == "net-create") { + if(argc > 3 || (argc == 3 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-create" << std::endl; + return 1; + } + checkForThing(state,"central",true); + res = REQUEST(REQ_POST,state,state.reqHeaders,"",state.url + "api/network"); + if(std::get<0>(res) == 200) { + json result = json::parse(std::get<1>(res)); + std::cout << OK_STR << "created network " << result["config"]["nwid"].get() << std::endl; + } + } + + // zerotier @thing net-rm --- deletes a network + else if (state.command == "net-rm") { + if(argc > 4 || (argc == 4 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-rm " << std::endl; + return 1; + } + checkForThing(state,"central",true); + if(!state.args.size()) { + std::cout << "Argument error: No network specified." << std::endl; + std::cout << " | Usage: zerotier net-rm " << std::endl; + } + else { + std::string nwid = state.args[0]; + res = REQUEST(REQ_DEL,state,state.reqHeaders,"",state.url + "api/network/" + nwid); + if(std::get<0>(res) == 200) { + std::cout << "deleted network " << nwid << std::endl; + } + } + } + + // zerotier @thing net-ls --- lists all networks + else if (state.command == "net-ls") { + if(argc > 3 || (argc == 3 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-ls" << std::endl; + return 1; + } + checkForThing(state,"central",true); + res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network"); + if(std::get<0>(res) == 200) { + json result = json::parse(std::get<1>(res)); + for(int m=0;m() << std::endl; + } + } + } + + // zerotier @thing net-members --- show all members of a network + else if (state.command == "net-members") { + if(argc > 4 || (argc == 4 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-members " << std::endl; + return 1; + } + checkForThing(state,"central",true); + if(!state.args.size()) { + std::cout << FAIL_STR << "Argument error: No network specified." << std::endl; + std::cout << " | Usage: zerotier net-members " << std::endl; + } + else { + std::string nwid = state.args[0]; + res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member"); + json result = json::parse(std::get<1>(res)); + std::cout << "Members of " << nwid << ":" << std::endl; + for (json::iterator it = result.begin(); it != result.end(); ++it) { + std::cout << it.key() << std::endl; + } + } + } + + // zerotier @thing net-show --- show info about a device on a specific network + else if (state.command == "net-show") { + if(argc > 5 || (argc == 5 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-show " << std::endl; + return 1; + } + checkForThing(state,"central",true); + if(state.args.size() < 2) { + std::cout << FAIL_STR << "Argument error: Too few arguments." << std::endl; + std::cout << " | Usage: zerotier net-show " << std::endl; + } + else { + std::string nwid = state.args[0]; + std::string devid = state.args[1]; + res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member/" + devid); + // TODO: More info, what would we like to show exactly? + if(std::get<0>(res) == 200) { + json result = json::parse(std::get<1>(res)); + std::cout << "Assigned IP: " << std::endl; + for(int m=0; m() << std::endl; + } + } + } + } + + // zerotier @thing net-auth --- authorize a device on a network + else if (state.command == "net-auth") { + if(argc > 5 || (argc == 5 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-auth " << std::endl; + return 1; + } + checkForThing(state,"central",true); + if(state.args.size() != 2) { + std::cout << FAIL_STR << "Argument error: Network and/or device ID not specified." << std::endl; + std::cout << " | Usage: zerotier net-auth " << std::endl; + } + std::string nwid = state.args[0]; + std::string devid = state.args[1]; + url = state.url + "api/network/" + nwid + "/member/" + devid; + // Add device to network + res = REQUEST(REQ_POST,state,state.reqHeaders,"",(const std::string)url); + if(std::get<0>(res) == 200) { + result = json::parse(std::get<1>(res)); + res = REQUEST(REQ_GET,state,state.reqHeaders,"",(const std::string)url); + result = json::parse(std::get<1>(res)); + result["config"]["authorized"] = "true"; + std::string newconfig = result.dump(); + res = REQUEST(REQ_POST,state,state.reqHeaders,newconfig,(const std::string)url); + if(std::get<0>(res) == 200) + std::cout << OK_STR << devid << " authorized on " << nwid << std::endl; + else + std::cout << FAIL_STR << "There was a problem authorizing that device." << std::endl; + } + } + + // zerotier @thing net-unauth + else if (state.command == "net-unauth") { + if(argc > 5 || (argc == 5 && !state.atname.length())) { + std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-unauth " << std::endl; + return 1; + } + checkForThing(state,"central",true); + if(state.args.size() != 2) { + std::cout << FAIL_STR << "Bad argument. No network and/or device ID specified." << std::endl; + std::cout << " | Usage: zerotier net-unauth " << std::endl; + } + std::string nwid = state.args[0]; + std::string devid = state.args[1]; + // If successful, get member config + res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member/" + devid); + result = json::parse(std::get<1>(res)); + // modify auth field and re-POST + result["config"]["authorized"] = "false"; + std::string newconfig = result.dump(); + res = REQUEST(REQ_POST,state,state.reqHeaders,newconfig,state.url + "api/network/" + nwid + "/member/" + devid); + if(std::get<0>(res) == 200) + std::cout << OK_STR << devid << " de-authorized from " << nwid << std::endl; + else + std::cout << FAIL_STR << "There was a problem de-authorizing that device." << std::endl; + } + + // zerotier @thing net-set + else if (state.command == "net-set") { + } + + // ID + + // zerotier id-generate [] + else if (state.command == "id-generate") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier id-generate []" << std::endl; + return 1; + } + uint64_t vanity = 0; + int vanityBits = 0; + if (argc >= 5) { + vanity = Utils::hexStrToU64(argv[4]) & 0xffffffffffULL; + vanityBits = 4 * strlen(argv[4]); + if (vanityBits > 40) + vanityBits = 40; + } + + ZeroTier::Identity id; + for(;;) { + id.generate(); + if ((id.address().toInt() >> (40 - vanityBits)) == vanity) { + if (vanityBits > 0) { + fprintf(stderr,"vanity address: found %.10llx !\n",(unsigned long long)id.address().toInt()); + } + break; + } else { + fprintf(stderr,"vanity address: tried %.10llx looking for first %d bits of %.10llx\n",(unsigned long long)id.address().toInt(),vanityBits,(unsigned long long)(vanity << (40 - vanityBits))); + } + } + + std::string idser = id.toString(true); + if (argc >= 3) { + if (!OSUtils::writeFile(argv[2],idser)) { + std::cerr << "Error writing to " << argv[2] << std::endl; + return 1; + } else std::cout << argv[2] << " written" << std::endl; + if (argc >= 4) { + idser = id.toString(false); + if (!OSUtils::writeFile(argv[3],idser)) { + std::cerr << "Error writing to " << argv[3] << std::endl; + return 1; + } else std::cout << argv[3] << " written" << std::endl; + } + } else std::cout << idser << std::endl; + } + + // zerotier id-validate + else if (state.command == "id-validate") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier id-validate " << std::endl; + return 1; + } + Identity id = getIdFromArg(argv[2]); + if (!id) { + std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; + return 1; + } + if (!id.locallyValidate()) { + std::cerr << argv[2] << " FAILED validation." << std::endl; + return 1; + } else std::cout << argv[2] << "is a valid identity" << std::endl; + } + + // zerotier id-sign + else if (state.command == "id-sign") { + if(argc != 4) { + std::cerr << INVALID_ARGS_STR << "zerotier id-sign " << std::endl; + return 1; + } + Identity id = getIdFromArg(argv[2]); + if (!id) { + std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; + return 1; + } + if (!id.hasPrivate()) { + std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl; + return 1; + } + std::string inf; + if (!OSUtils::readFile(argv[3],inf)) { + std::cerr << argv[3] << " is not readable" << std::endl; + return 1; + } + C25519::Signature signature = id.sign(inf.data(),(unsigned int)inf.length()); + std::cout << Utils::hex(signature.data,(unsigned int)signature.size()) << std::endl; + } + + // zerotier id-verify + else if (state.command == "id-verify") { + if(argc != 4) { + std::cerr << INVALID_ARGS_STR << "zerotier id-verify " << std::endl; + return 1; + } + Identity id = getIdFromArg(argv[2]); + if (!id) { + std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; + return 1; + } + std::string inf; + if (!OSUtils::readFile(argv[3],inf)) { + std::cerr << argv[3] << " is not readable" << std::endl; + return 1; + } + std::string signature(Utils::unhex(argv[4])); + if ((signature.length() > ZT_ADDRESS_LENGTH)&&(id.verify(inf.data(),(unsigned int)inf.length(),signature.data(),(unsigned int)signature.length()))) { + std::cout << argv[3] << " signature valid" << std::endl; + } else { + std::cerr << argv[3] << " signature check FAILED" << std::endl; + return 1; + } + } + + // zerotier id-getpublic + else if (state.command == "id-getpublic") { + if(argc != 3) { + std::cerr << INVALID_ARGS_STR << "zerotier id-getpublic " << std::endl; + return 1; + } + Identity id = getIdFromArg(argv[2]); + if (!id) { + std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; + return 1; + } + std::cerr << id.toString(false) << std::endl; + } + // + else { + dumpHelp(); + return -1; + } + if(std::find(state.args.begin(), state.args.end(), "-verbose") != state.args.end()) + std::cout << "\n\nAPI response = " << std::get<1>(res) << std::endl; + curl_global_cleanup(); + return 0; +} diff --git a/cli/README.md b/cli/README.md deleted file mode 100644 index 595df07e..00000000 --- a/cli/README.md +++ /dev/null @@ -1,57 +0,0 @@ -The new ZeroTier CLI! -==== - -With this update we've expanded upon the previous CLI's functionality, so things should seem pretty familiar. Here are some of the new features we've introduced: - - - Create and administer networks on ZeroTier Central directly from the console. - - Service configurations, allows you to control local/remote instances of ZeroTier One - - Identity generation and management is now part of the same CLI tool - -*** -## Configurations - -Configurations are a way for you to nickname and logically organize the control of ZeroTier services running locally or remotely (this includes ZeroTier Central!). They're merely groupings of service API url's and auth tokens. The CLI's settings data is contained within `.zerotierCliSettings`. - -For instance, you can control your local instance of ZeroTier One via the `@local` config. By default it is represented as follows: - -``` -"local": { - "auth": "7tyqRoFytajf21j2l2t9QPm5", - "type": "one", - "url": "http://127.0.0.1:9993/" -} -``` - -As an example, if you issue the command `zerotier ls` is it implicitly stating `zerotier @local ls`. - -With the same line of thinking, you could create a `@my.zerotier.com` which would allow for something like `zerotier @my.zerotier.com net-create` which talks to our hosted ZeroTier Central to create a new network. - - - -## Command families - -- `cli-` is for configuring the settings data for the CLI itself, such as adding/removing `@thing` configurations, variables, etc. -- `net-` is for operating on a *ZeroTier Central* service such as `https://my.zerotier.com` -- `id-` is for handling ZeroTier identities. - -And those commands with no prefix are there to allow you to operate ZeroTier One instances either local or remote. - -*** -## Useful command examples - -*Add a ZeroTier One configuration:* - - - `zerotier cli-add-zt MyLocalConfigName https://127.0.0.1:9993/ ` - -*Add a ZeroTier Central configuration:* - - - `zerotier cli-add-central MyZTCentralConfigName https://my.zerotier.com/ ` - -*Set a default ZeroTier One instance:* - - - `zerotier cli-set defaultOne MyLocalConfigName` - -*Set a default ZeroTier Central:* - - - `zerotier cli-set defaultCentral MyZTCentralConfigName` - diff --git a/cli/zerotier.cpp b/cli/zerotier.cpp deleted file mode 100644 index e75268d1..00000000 --- a/cli/zerotier.cpp +++ /dev/null @@ -1,957 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -// Note: unlike the rest of ZT's code base, this requires C++11 due to -// the JSON library it uses and other things. - -#include -#include -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Identity.hpp" -#include "../version.h" -#include "../osdep/OSUtils.hpp" -#include "../ext/offbase/json/json.hpp" - -#ifdef __WINDOWS__ -#include -#include -#include -#include -#else -#include -#include -#endif - -#include -#include -#include -#include -#include -#include - -#include - -using json = nlohmann::json; -using namespace ZeroTier; - -#define ZT_CLI_FLAG_VERBOSE 'v' -#define ZT_CLI_FLAG_UNSAFE_SSL 'X' - -#define REQ_GET 0 -#define REQ_POST 1 -#define REQ_DEL 2 - -#define OK_STR "[OK ]: " -#define FAIL_STR "[FAIL]: " -#define WARN_STR "[WARN]: " -#define INVALID_ARGS_STR "Invalid args. Usage: " - -struct CLIState -{ - std::string atname; - std::string command; - std::string url; - std::map reqHeaders; - std::vector args; - std::map opts; - json settings; -}; - -namespace { - -static Identity getIdFromArg(char *arg) -{ - Identity id; - if ((strlen(arg) > 32)&&(arg[10] == ':')) { // identity is a literal on the command line - if (id.fromString(arg)) - return id; - } else { // identity is to be read from a file - std::string idser; - if (OSUtils::readFile(arg,idser)) { - if (id.fromString(idser)) - return id; - } - } - return Identity(); -} - -static std::string trimString(const std::string &s) -{ - unsigned long end = (unsigned long)s.length(); - while (end) { - char c = s[end - 1]; - if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t')) - --end; - else break; - } - unsigned long start = 0; - while (start < end) { - char c = s[start]; - if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t')) - ++start; - else break; - } - return s.substr(start,end - start); -} - -static inline std::string getSettingsFilePath() -{ -#ifdef __WINDOWS__ -#else - const char *home = getenv("HOME"); - if (!home) - home = "/"; - return (std::string(home) + "/.zerotierCliSettings"); -#endif -} - -static bool saveSettingsBackup(CLIState &state) -{ - std::string sfp(getSettingsFilePath().c_str()); - if(state.settings.find("generateBackupConfig") != state.settings.end() - && state.settings["generateBackupConfig"].get() == "true") { - std::string backup_file = getSettingsFilePath() + ".bak"; - if(!OSUtils::writeFile(sfp.c_str(), state.settings.dump(2))) { - OSUtils::lockDownFile(sfp.c_str(),false); - std::cout << WARN_STR << "unable to write backup config file" << std::endl; - return false; - } - return true; - } - return false; -} - -static bool saveSettings(CLIState &state) -{ - std::string sfp(getSettingsFilePath().c_str()); - if(OSUtils::writeFile(sfp.c_str(), state.settings.dump(2))) { - OSUtils::lockDownFile(sfp.c_str(),false); - std::cout << OK_STR << "changes saved." << std::endl; - return true; - } - std::cout << FAIL_STR << "unable to write to " << sfp << std::endl; - return false; -} - -static void dumpHelp() -{ - std::cout << "ZeroTier Newer-Spiffier CLI " << ZEROTIER_ONE_VERSION_MAJOR << "." << ZEROTIER_ONE_VERSION_MINOR << "." << ZEROTIER_ONE_VERSION_REVISION << std::endl; - std::cout << "(c)2016 ZeroTier, Inc. / Licensed under the GNU GPL v3" << std::endl; - std::cout << std::endl; - std::cout << "Configuration path: " << getSettingsFilePath() << std::endl; - std::cout << std::endl; - std::cout << "Usage: zerotier [-option] [@name] []" << std::endl; - std::cout << std::endl; - std::cout << "Options:" << std::endl; - std::cout << " -verbose - Verbose JSON output" << std::endl; - std::cout << " -X - Do not check SSL certs (CAUTION!)" << std::endl; - std::cout << std::endl; - std::cout << "CLI Configuration Commands:" << std::endl; - std::cout << " cli-set - Set a CLI option ('cli-set help')" << std::endl; - std::cout << " cli-unset - Un-sets a CLI option ('cli-unset help')" << std::endl; - std::cout << " cli-ls - List configured @things" << std::endl; - std::cout << " cli-rm @name - Remove a configured @thing" << std::endl; - std::cout << " cli-add-zt @name - Add a ZeroTier service" << std::endl; - std::cout << " cli-add-central @name - Add ZeroTier Central instance" << std::endl; - std::cout << std::endl; - std::cout << "ZeroTier One Service Commands:" << std::endl; - std::cout << " -v / -version - Displays default local instance's version'" << std::endl; - std::cout << " ls - List currently joined networks" << std::endl; - std::cout << " join [opt=value ...] - Join a network" << std::endl; - std::cout << " leave - Leave a network" << std::endl; - std::cout << " peers - List ZeroTier VL1 peers" << std::endl; - std::cout << " show [] - Get info about self or object" << std::endl; - std::cout << std::endl; - std::cout << "Network Controller Commands:" << std::endl; - std::cout << " net-create - Create a new network" << std::endl; - std::cout << " net-rm - Delete a network (CAUTION!)" << std::endl; - std::cout << " net-ls - List administered networks" << std::endl; - std::cout << " net-members - List members of a network" << std::endl; - std::cout << " net-show [
] - Get network or member info" << std::endl; - std::cout << " net-auth
- Authorize a member" << std::endl; - std::cout << " net-unauth
- De-authorize a member" << std::endl; - std::cout << " net-set - See 'net-set help'" << std::endl; - std::cout << std::endl; - std::cout << "Identity Commands:" << std::endl; - std::cout << " id-generate [] - Generate a ZeroTier identity" << std::endl; - std::cout << " id-validate - Locally validate an identity" << std::endl; - std::cout << " id-sign - Sign a file" << std::endl; - std::cout << " id-verify - Verify a file's signature" << std::endl; - std::cout << " id-getpublic - Get full identity's public portion" << std::endl; - std::cout << std::endl; -} - -static size_t _curlStringAppendCallback(void *contents,size_t size,size_t nmemb,void *stdstring) -{ - size_t totalSize = size * nmemb; - reinterpret_cast(stdstring)->append((const char *)contents,totalSize); - return totalSize; -} - -static std::tuple REQUEST(int requestType, CLIState &state, const std::map &headers, const std::string &postfield, const std::string &url) -{ - std::string body; - char errbuf[CURL_ERROR_SIZE]; - char urlbuf[4096]; - - CURL *curl; - curl = curl_easy_init(); - if (!curl) { - std::cerr << "FATAL: curl_easy_init() failed" << std::endl; - exit(-1); - } - - Utils::scopy(urlbuf,sizeof(urlbuf),url.c_str()); - curl_easy_setopt(curl,CURLOPT_URL,urlbuf); - - struct curl_slist *hdrs = (struct curl_slist *)0; - for(std::map::const_iterator i(headers.begin());i!=headers.end();++i) { - std::string htmp(i->first); - htmp.append(": "); - htmp.append(i->second); - hdrs = curl_slist_append(hdrs,htmp.c_str()); - } - if (hdrs) - curl_easy_setopt(curl,CURLOPT_HTTPHEADER,hdrs); - - //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); - curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&body); - curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curlStringAppendCallback); - - if(std::find(state.args.begin(), state.args.end(), "-X") == state.args.end()) - curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,(state.opts.count(ZT_CLI_FLAG_UNSAFE_SSL) > 0) ? 0L : 1L); - - if(requestType == REQ_POST) { - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfield.c_str()); - } - if(requestType == REQ_DEL) - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - if(requestType == REQ_GET) { - curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errbuf); - curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,0L); - } - - curl_easy_setopt(curl,CURLOPT_USERAGENT,"ZeroTier-CLI"); - CURLcode res = curl_easy_perform(curl); - - errbuf[CURL_ERROR_SIZE-1] = (char)0; // sanity check - - if (res != CURLE_OK) - return std::make_tuple(-1,std::string(errbuf)); - - long response_code; - int rc = (int)curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &response_code); - - if(response_code == 401) { std::cout << FAIL_STR << response_code << "Unauthorized." << std::endl; exit(0); } - else if(response_code == 403) { std::cout << FAIL_STR << response_code << "Forbidden." << std::endl; exit(0); } - else if(response_code == 404) { std::cout << FAIL_STR << response_code << "Not found." << std::endl; exit(0); } - else if(response_code == 408) { std::cout << FAIL_STR << response_code << "Request timed out." << std::endl; exit(0); } - else if(response_code != 200) { std::cout << FAIL_STR << response_code << "Unable to process request." << std::endl; exit(0); } - - curl_easy_cleanup(curl); - if (hdrs) - curl_slist_free_all(hdrs); - return std::make_tuple(response_code,body); -} - -} // anonymous namespace - -////////////////////////////////////////////////////////////////////////////// - -// Check for user-specified @thing config -// Make sure it @thing makes sense -// Apply appropriate request headers -static void checkForThing(CLIState &state, std::string thingType, bool warnNoThingProvided) -{ - std::string configName; - if(state.atname.length()) { - configName = state.atname.erase(0,1); - // make sure specified @thing makes sense in the context of the command - if(thingType == "one" && state.settings["things"][configName]["type"].get() != "one") { - std::cout << FAIL_STR << "A ZeroTier Central config was specified for a ZeroTier One command." << std::endl; - exit(0); - } - if(thingType == "central" && state.settings["things"][configName]["type"].get() != "central") { - std::cout << FAIL_STR << "A ZeroTier One config was specified for a ZeroTier Central command." << std::endl; - exit(0); - } - } - else { // no @thing specified, check for defaults depending on type - if(thingType == "one") { - if(state.settings.find("defaultOne") != state.settings.end()) { - if(warnNoThingProvided) - std::cout << WARN_STR << "No @thing specified, assuming default for ZeroTier One command: " << state.settings["defaultOne"].get().c_str() << std::endl; - configName = state.settings["defaultOne"].get().erase(0,1); // get default - } - else { - std::cout << WARN_STR << "No @thing specified, and no default is known." << std::endl; - std::cout << "HELP: To set a default: zerotier cli-set defaultOne @my_default_thing" << std::endl; - exit(0); - } - } - if(thingType == "central") { - if(state.settings.find("defaultCentral") != state.settings.end()) { - if(warnNoThingProvided) - std::cout << WARN_STR << "No @thing specified, assuming default for ZeroTier Central command: " << state.settings["defaultCentral"].get().c_str() << std::endl; - configName = state.settings["defaultCentral"].get().erase(0,1); // get default - } - else { - std::cout << WARN_STR << "No @thing specified, and no default is known." << std::endl; - std::cout << "HELP: To set a default: zerotier cli-set defaultCentral @my_default_thing" << std::endl; - exit(0); - } - } - } - // Apply headers - if(thingType == "one") { - state.reqHeaders["X-ZT1-Auth"] = state.settings["things"][configName]["auth"]; - } - if(thingType == "central"){ - state.reqHeaders["Content-Type"] = "application/json"; - state.reqHeaders["Authorization"] = "Bearer " + state.settings["things"][configName]["auth"].get(); - state.reqHeaders["Accept"] = "application/json"; - } - state.url = state.settings["things"][configName]["url"]; -} - -static bool checkURL(std::string url) -{ - // TODO - return true; -} - -static std::string getLocalVersion(CLIState &state) -{ - json result; - std::tuple res; - checkForThing(state,"one",false); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "/status"); - if(std::get<0>(res) == 200) { - result = json::parse(std::get<1>(res)); - return result["version"].get(); - } - return "---"; -} - -#ifdef __WINDOWS__ -int _tmain(int argc, _TCHAR* argv[]) -#else -int main(int argc,char **argv) -#endif -{ -#ifdef __WINDOWS__ - { - WSADATA wsaData; - WSAStartup(MAKEWORD(2,2),&wsaData); - } -#endif - - curl_global_init(CURL_GLOBAL_DEFAULT); - CLIState state; - std::string arg1, arg2, authToken; - - for(int i=1;i 0)&&(port < 65536))&&(authToken.length() > 0)) { - state.settings["things"]["local"]["url"] = (std::string("http://127.0.0.1:") + portStr + "/"); - state.settings["things"]["local"]["auth"] = authToken; - initSuccess = true; - } - } - - if (!saveSettings(state)) { - std::cerr << "FATAL: unable to write " << getSettingsFilePath() << std::endl; - exit(-1); - } - - if (initSuccess) { - std::cerr << "INFO: initialized new config at " << getSettingsFilePath() << std::endl; - } else { - std::cerr << "INFO: initialized new config at " << getSettingsFilePath() << " but could not auto-init local ZeroTier One service config from " << oneHome << " -- you will need to set local service URL and port manually if you want to control a local instance of ZeroTier One. (This happens if you are not root/administrator.)" << std::endl; - } - } - } - - // PRE-REQUEST SETUP - - json result; - std::tuple res; - std::string url = ""; - - // META - - if ((state.command.length() == 0)||(state.command == "help")) { - dumpHelp(); - return -1; - } - - // zerotier version - else if (state.command == "v" || state.command == "version") { - std::cout << getLocalVersion(state) << std::endl; - return 1; - } - - // zerotier cli-set - else if (state.command == "cli-set") { - if(argc != 4) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-set " << std::endl; - return 1; - } - std::string settingName, settingValue; - if(state.atname.length()) { // User provided @thing erroneously, we will ignore it and adjust argument indices - settingName = argv[3]; - settingValue = argv[4]; - } - else { - settingName = argv[2]; - settingValue = argv[3]; - } - saveSettingsBackup(state); - state.settings[settingName] = settingValue; // changes - saveSettings(state); - } - - // zerotier cli-unset - else if (state.command == "cli-unset") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-unset " << std::endl; - return 1; - } - std::string settingName; - if(state.atname.length()) // User provided @thing erroneously, we will ignore it and adjust argument indices - settingName = argv[3]; - else - settingName = argv[2]; - saveSettingsBackup(state); - state.settings.erase(settingName); // changes - saveSettings(state); - } - - // zerotier @thing_to_remove cli-rm --- removes the configuration - else if (state.command == "cli-rm") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-rm <@thing>" << std::endl; - return 1; - } - if(state.settings["things"].find(state.atname) != state.settings["things"].end()) { - if(state.settings["defaultOne"] == state.atname) { - std::cout << "WARNING: The config you're trying to remove is currently set as your default. Set a new default first!" << std::endl; - std::cout << " | Usage: zerotier set defaultOne @your_other_thing" << std::endl; - } - else { - state.settings["things"].erase(state.atname.c_str()); - saveSettings(state); - } - } - } - - // zerotier cli-add-zt - // TODO: Check for malformed urls/auth - else if (state.command == "cli-add-zt") { - if(argc != 5) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-add-zt " << std::endl; - return 1; - } - std::string thing_name = argv[2], url = argv[3], auth = argv[4]; - if(!checkURL(url)) { - std::cout << FAIL_STR << "Malformed URL" << std::endl; - return 1; - } - if(state.settings.find(thing_name) != state.settings.end()) { - std::cout << "WARNING: A @thing with the shortname " << thing_name.c_str() - << " already exists. Choose another name or rename the old @thing" << std::endl; - std::cout << " | Usage: To rename a @thing: zerotier cli-rename @old_thing_name @new_thing_name" << std::endl; - } - else { - result = json::parse("{ \"auth\": \"" + auth + "\", \"type\": \"" + "one" + "\", \"url\": \"" + url + "\" }"); - saveSettingsBackup(state); - // TODO: Handle cases where user may or may not prepend an @ - state.settings["things"][thing_name] = result; // changes - saveSettings(state); - } - } - - // zerotier cli-add-central - // TODO: Check for malformed urls/auth - else if (state.command == "cli-add-central") { - if(argc != 5) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-add-central " << std::endl; - return 1; - } - std::string thing_name = argv[2], url = argv[3], auth = argv[4]; - if(!checkURL(url)) { - std::cout << FAIL_STR << "Malformed URL" << std::endl; - return 1; - } - if(state.settings.find(thing_name) != state.settings.end()) { - std::cout << "WARNING: A @thing with the shortname " << thing_name.c_str() - << " already exists. Choose another name or rename the old @thing" << std::endl; - std::cout << " | Usage: To rename a @thing: zerotier cli-rename @old_thing_name @new_thing_name" << std::endl; - } - else { - result = json::parse("{ \"auth\": \"" + auth + "\", \"type\": \"" + "central" + "\", \"url\": \"" + url + "\" }"); - saveSettingsBackup(state); - // TODO: Handle cases where user may or may not prepend an @ - state.settings["things"]["@" + thing_name] = result; // changes - saveSettings(state); - } - } - - // ONE SERVICE - - // zerotier ls --- display all networks currently joined - else if (state.command == "ls" || state.command == "listnetworks") { - if(argc != 2) { - std::cerr << INVALID_ARGS_STR << "zerotier ls" << std::endl; - return 1; - } - checkForThing(state,"one",true); - url = state.url + "network"; - res = REQUEST(REQ_GET,state,state.reqHeaders,"",(const std::string)url); - if(std::get<0>(res) == 200) { - std::cout << "listnetworks " << std::endl; - auto j = json::parse(std::get<1>(res).c_str()); - if (j.type() == json::value_t::array) { - for(int i=0;i(); - std::string name = j[i]["name"].get(); - std::string mac = j[i]["mac"].get(); - std::string status = j[i]["status"].get(); - std::string type = j[i]["type"].get(); - std::string addrs; - for(int m=0; m() + " "; - } - std::string dev = j[i]["portDeviceName"].get(); - std::cout << "listnetworks " << nwid << " " << name << " " << mac << " " << status << " " << type << " " << dev << " " << addrs << std::endl; - } - } - } - } - - // zerotier join --- joins a network - else if (state.command == "join") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier join " << std::endl; - return 1; - } - checkForThing(state,"one",true); - res = REQUEST(REQ_POST,state,state.reqHeaders,"{}",state.url + "/network/" + state.args[0]); - if(std::get<0>(res) == 200) { - std::cout << OK_STR << "connected to " << state.args[0] << std::endl; - } - } - - // zerotier leave --- leaves a network - else if (state.command == "leave") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier leave " << std::endl; - return 1; - } - checkForThing(state,"one",true); - res = REQUEST(REQ_DEL,state,state.reqHeaders,"{}",state.url + "/network/" + state.args[0]); - if(std::get<0>(res) == 200) { - std::cout << OK_STR << "disconnected from " << state.args[0] << std::endl; - } - } - - // zerotier peers --- display address and role of all peers - else if (state.command == "peers") { - if(argc != 2) { - std::cerr << INVALID_ARGS_STR << "zerotier peers" << std::endl; - return 1; - } - checkForThing(state,"one",true); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "/peer"); - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - for(int i=0; i(res) == 200) { - result = json::parse(std::get<1>(res)); - std::string status_str = result["online"].get() ? "ONLINE" : "OFFLINE"; - std::cout << "info " << result["address"].get() - << " " << status_str << " " << result["version"].get() << std::endl; - } - } - - // REMOTE - - // zerotier @thing net-create --- creates a new network - else if (state.command == "net-create") { - if(argc > 3 || (argc == 3 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-create" << std::endl; - return 1; - } - checkForThing(state,"central",true); - res = REQUEST(REQ_POST,state,state.reqHeaders,"",state.url + "api/network"); - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - std::cout << OK_STR << "created network " << result["config"]["nwid"].get() << std::endl; - } - } - - // zerotier @thing net-rm --- deletes a network - else if (state.command == "net-rm") { - if(argc > 4 || (argc == 4 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-rm " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(!state.args.size()) { - std::cout << "Argument error: No network specified." << std::endl; - std::cout << " | Usage: zerotier net-rm " << std::endl; - } - else { - std::string nwid = state.args[0]; - res = REQUEST(REQ_DEL,state,state.reqHeaders,"",state.url + "api/network/" + nwid); - if(std::get<0>(res) == 200) { - std::cout << "deleted network " << nwid << std::endl; - } - } - } - - // zerotier @thing net-ls --- lists all networks - else if (state.command == "net-ls") { - if(argc > 3 || (argc == 3 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-ls" << std::endl; - return 1; - } - checkForThing(state,"central",true); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network"); - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - for(int m=0;m() << std::endl; - } - } - } - - // zerotier @thing net-members --- show all members of a network - else if (state.command == "net-members") { - if(argc > 4 || (argc == 4 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-members " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(!state.args.size()) { - std::cout << FAIL_STR << "Argument error: No network specified." << std::endl; - std::cout << " | Usage: zerotier net-members " << std::endl; - } - else { - std::string nwid = state.args[0]; - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member"); - json result = json::parse(std::get<1>(res)); - std::cout << "Members of " << nwid << ":" << std::endl; - for (json::iterator it = result.begin(); it != result.end(); ++it) { - std::cout << it.key() << std::endl; - } - } - } - - // zerotier @thing net-show --- show info about a device on a specific network - else if (state.command == "net-show") { - if(argc > 5 || (argc == 5 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-show " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(state.args.size() < 2) { - std::cout << FAIL_STR << "Argument error: Too few arguments." << std::endl; - std::cout << " | Usage: zerotier net-show " << std::endl; - } - else { - std::string nwid = state.args[0]; - std::string devid = state.args[1]; - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member/" + devid); - // TODO: More info, what would we like to show exactly? - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - std::cout << "Assigned IP: " << std::endl; - for(int m=0; m() << std::endl; - } - } - } - } - - // zerotier @thing net-auth --- authorize a device on a network - else if (state.command == "net-auth") { - if(argc > 5 || (argc == 5 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-auth " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(state.args.size() != 2) { - std::cout << FAIL_STR << "Argument error: Network and/or device ID not specified." << std::endl; - std::cout << " | Usage: zerotier net-auth " << std::endl; - } - std::string nwid = state.args[0]; - std::string devid = state.args[1]; - url = state.url + "api/network/" + nwid + "/member/" + devid; - // Add device to network - res = REQUEST(REQ_POST,state,state.reqHeaders,"",(const std::string)url); - if(std::get<0>(res) == 200) { - result = json::parse(std::get<1>(res)); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",(const std::string)url); - result = json::parse(std::get<1>(res)); - result["config"]["authorized"] = "true"; - std::string newconfig = result.dump(); - res = REQUEST(REQ_POST,state,state.reqHeaders,newconfig,(const std::string)url); - if(std::get<0>(res) == 200) - std::cout << OK_STR << devid << " authorized on " << nwid << std::endl; - else - std::cout << FAIL_STR << "There was a problem authorizing that device." << std::endl; - } - } - - // zerotier @thing net-unauth - else if (state.command == "net-unauth") { - if(argc > 5 || (argc == 5 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-unauth " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(state.args.size() != 2) { - std::cout << FAIL_STR << "Bad argument. No network and/or device ID specified." << std::endl; - std::cout << " | Usage: zerotier net-unauth " << std::endl; - } - std::string nwid = state.args[0]; - std::string devid = state.args[1]; - // If successful, get member config - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member/" + devid); - result = json::parse(std::get<1>(res)); - // modify auth field and re-POST - result["config"]["authorized"] = "false"; - std::string newconfig = result.dump(); - res = REQUEST(REQ_POST,state,state.reqHeaders,newconfig,state.url + "api/network/" + nwid + "/member/" + devid); - if(std::get<0>(res) == 200) - std::cout << OK_STR << devid << " de-authorized from " << nwid << std::endl; - else - std::cout << FAIL_STR << "There was a problem de-authorizing that device." << std::endl; - } - - // zerotier @thing net-set - else if (state.command == "net-set") { - } - - // ID - - // zerotier id-generate [] - else if (state.command == "id-generate") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier id-generate []" << std::endl; - return 1; - } - uint64_t vanity = 0; - int vanityBits = 0; - if (argc >= 5) { - vanity = Utils::hexStrToU64(argv[4]) & 0xffffffffffULL; - vanityBits = 4 * strlen(argv[4]); - if (vanityBits > 40) - vanityBits = 40; - } - - ZeroTier::Identity id; - for(;;) { - id.generate(); - if ((id.address().toInt() >> (40 - vanityBits)) == vanity) { - if (vanityBits > 0) { - fprintf(stderr,"vanity address: found %.10llx !\n",(unsigned long long)id.address().toInt()); - } - break; - } else { - fprintf(stderr,"vanity address: tried %.10llx looking for first %d bits of %.10llx\n",(unsigned long long)id.address().toInt(),vanityBits,(unsigned long long)(vanity << (40 - vanityBits))); - } - } - - std::string idser = id.toString(true); - if (argc >= 3) { - if (!OSUtils::writeFile(argv[2],idser)) { - std::cerr << "Error writing to " << argv[2] << std::endl; - return 1; - } else std::cout << argv[2] << " written" << std::endl; - if (argc >= 4) { - idser = id.toString(false); - if (!OSUtils::writeFile(argv[3],idser)) { - std::cerr << "Error writing to " << argv[3] << std::endl; - return 1; - } else std::cout << argv[3] << " written" << std::endl; - } - } else std::cout << idser << std::endl; - } - - // zerotier id-validate - else if (state.command == "id-validate") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier id-validate " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - if (!id.locallyValidate()) { - std::cerr << argv[2] << " FAILED validation." << std::endl; - return 1; - } else std::cout << argv[2] << "is a valid identity" << std::endl; - } - - // zerotier id-sign - else if (state.command == "id-sign") { - if(argc != 4) { - std::cerr << INVALID_ARGS_STR << "zerotier id-sign " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - if (!id.hasPrivate()) { - std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl; - return 1; - } - std::string inf; - if (!OSUtils::readFile(argv[3],inf)) { - std::cerr << argv[3] << " is not readable" << std::endl; - return 1; - } - C25519::Signature signature = id.sign(inf.data(),(unsigned int)inf.length()); - std::cout << Utils::hex(signature.data,(unsigned int)signature.size()) << std::endl; - } - - // zerotier id-verify - else if (state.command == "id-verify") { - if(argc != 4) { - std::cerr << INVALID_ARGS_STR << "zerotier id-verify " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - std::string inf; - if (!OSUtils::readFile(argv[3],inf)) { - std::cerr << argv[3] << " is not readable" << std::endl; - return 1; - } - std::string signature(Utils::unhex(argv[4])); - if ((signature.length() > ZT_ADDRESS_LENGTH)&&(id.verify(inf.data(),(unsigned int)inf.length(),signature.data(),(unsigned int)signature.length()))) { - std::cout << argv[3] << " signature valid" << std::endl; - } else { - std::cerr << argv[3] << " signature check FAILED" << std::endl; - return 1; - } - } - - // zerotier id-getpublic - else if (state.command == "id-getpublic") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier id-getpublic " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - std::cerr << id.toString(false) << std::endl; - } - // - else { - dumpHelp(); - return -1; - } - if(std::find(state.args.begin(), state.args.end(), "-verbose") != state.args.end()) - std::cout << "\n\nAPI response = " << std::get<1>(res) << std::endl; - curl_global_cleanup(); - return 0; -} -- cgit v1.2.3 From 5fa1d9796cbed473bdac1f1a4b84d5f6e5a1c69c Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 27 Jan 2017 17:34:39 -0800 Subject: zerotier-idtool commands to init and generate moons --- attic/world/README.md | 7 ++ attic/world/build.sh | 1 + attic/world/earth-2016-01-13.bin | Bin 0 -> 634 bytes attic/world/mkworld.cpp | 149 ++++++++++++++++++++++++++++++ attic/world/old/earth-2015-11-16.bin | Bin 0 -> 494 bytes attic/world/old/earth-2015-11-20.bin | Bin 0 -> 792 bytes attic/world/old/earth-2015-12-17.bin | Bin 0 -> 732 bytes node/World.hpp | 27 ++++++ one.cpp | 87 ++++++++++++++++++ world/README.md | 7 -- world/build.sh | 1 - world/earth-2016-01-13.bin | Bin 634 -> 0 bytes world/mkworld.cpp | 169 ----------------------------------- world/old/earth-2015-11-16.bin | Bin 494 -> 0 bytes world/old/earth-2015-11-20.bin | Bin 792 -> 0 bytes world/old/earth-2015-12-17.bin | Bin 732 -> 0 bytes 16 files changed, 271 insertions(+), 177 deletions(-) create mode 100644 attic/world/README.md create mode 100755 attic/world/build.sh create mode 100644 attic/world/earth-2016-01-13.bin create mode 100644 attic/world/mkworld.cpp create mode 100644 attic/world/old/earth-2015-11-16.bin create mode 100644 attic/world/old/earth-2015-11-20.bin create mode 100644 attic/world/old/earth-2015-12-17.bin delete mode 100644 world/README.md delete mode 100755 world/build.sh delete mode 100644 world/earth-2016-01-13.bin delete mode 100644 world/mkworld.cpp delete mode 100644 world/old/earth-2015-11-16.bin delete mode 100644 world/old/earth-2015-11-20.bin delete mode 100644 world/old/earth-2015-12-17.bin (limited to 'attic') diff --git a/attic/world/README.md b/attic/world/README.md new file mode 100644 index 00000000..dda4920a --- /dev/null +++ b/attic/world/README.md @@ -0,0 +1,7 @@ +World Definitions and Generator Code +====== + +This little bit of code is used to generate world updates. Ordinary users probably will never need this unless they want to test or experiment. + +See mkworld.cpp for documentation. To build from this directory use 'source ./build.sh'. + diff --git a/attic/world/build.sh b/attic/world/build.sh new file mode 100755 index 00000000..b783702c --- /dev/null +++ b/attic/world/build.sh @@ -0,0 +1 @@ +c++ -I.. -o mkworld ../node/C25519.cpp ../node/Salsa20.cpp ../node/SHA512.cpp ../node/Identity.cpp ../node/Utils.cpp ../node/InetAddress.cpp ../osdep/OSUtils.cpp mkworld.cpp diff --git a/attic/world/earth-2016-01-13.bin b/attic/world/earth-2016-01-13.bin new file mode 100644 index 00000000..5dea4d21 Binary files /dev/null and b/attic/world/earth-2016-01-13.bin differ diff --git a/attic/world/mkworld.cpp b/attic/world/mkworld.cpp new file mode 100644 index 00000000..e0f477b3 --- /dev/null +++ b/attic/world/mkworld.cpp @@ -0,0 +1,149 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + */ + +/* + * This utility makes the World from the configuration specified below. + * It probably won't be much use to anyone outside ZeroTier, Inc. except + * for testing and experimentation purposes. + * + * If you want to make your own World you must edit this file. + * + * When run, it expects two files in the current directory: + * + * previous.c25519 - key pair to sign this world (key from previous world) + * current.c25519 - key pair whose public key should be embedded in this world + * + * If these files do not exist, they are both created with the same key pair + * and a self-signed initial World is born. + */ + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace ZeroTier; + +int main(int argc,char **argv) +{ + std::string previous,current; + if ((!OSUtils::readFile("previous.c25519",previous))||(!OSUtils::readFile("current.c25519",current))) { + C25519::Pair np(C25519::generate()); + previous = std::string(); + previous.append((const char *)np.pub.data,ZT_C25519_PUBLIC_KEY_LEN); + previous.append((const char *)np.priv.data,ZT_C25519_PRIVATE_KEY_LEN); + current = previous; + OSUtils::writeFile("previous.c25519",previous); + OSUtils::writeFile("current.c25519",current); + fprintf(stderr,"INFO: created initial world keys: previous.c25519 and current.c25519 (both initially the same)"ZT_EOL_S); + } + + if ((previous.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))||(current.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))) { + fprintf(stderr,"FATAL: previous.c25519 or current.c25519 empty or invalid"ZT_EOL_S); + return 1; + } + C25519::Pair previousKP; + memcpy(previousKP.pub.data,previous.data(),ZT_C25519_PUBLIC_KEY_LEN); + memcpy(previousKP.priv.data,previous.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN); + C25519::Pair currentKP; + memcpy(currentKP.pub.data,current.data(),ZT_C25519_PUBLIC_KEY_LEN); + memcpy(currentKP.priv.data,current.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN); + + // ========================================================================= + // EDIT BELOW HERE + + std::vector roots; + + const uint64_t id = ZT_WORLD_ID_EARTH; + const uint64_t ts = 1452708876314ULL; // January 13th, 2016 + + // Alice + roots.push_back(World::Root()); + roots.back().identity = Identity("9d219039f3:0:01f0922a98e3b34ebcbff333269dc265d7a020aab69d72be4d4acc9c8c9294785771256cd1d942a90d1bd1d2dca3ea84ef7d85afe6611fb43ff0b74126d90a6e"); + roots.back().stableEndpoints.push_back(InetAddress("188.166.94.177/9993")); // Amsterdam + roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:2:d0::7d:1/9993")); // Amsterdam + roots.back().stableEndpoints.push_back(InetAddress("154.66.197.33/9993")); // Johannesburg + roots.back().stableEndpoints.push_back(InetAddress("2c0f:f850:154:197::33/9993")); // Johannesburg + roots.back().stableEndpoints.push_back(InetAddress("159.203.97.171/9993")); // New York + roots.back().stableEndpoints.push_back(InetAddress("2604:a880:800:a1::54:6001/9993")); // New York + roots.back().stableEndpoints.push_back(InetAddress("169.57.143.104/9993")); // Sao Paolo + roots.back().stableEndpoints.push_back(InetAddress("2607:f0d0:1d01:57::2/9993")); // Sao Paolo + roots.back().stableEndpoints.push_back(InetAddress("107.170.197.14/9993")); // San Francisco + roots.back().stableEndpoints.push_back(InetAddress("2604:a880:1:20::200:e001/9993")); // San Francisco + roots.back().stableEndpoints.push_back(InetAddress("128.199.197.217/9993")); // Singapore + roots.back().stableEndpoints.push_back(InetAddress("2400:6180:0:d0::b7:4001/9993")); // Singapore + + // Bob + roots.push_back(World::Root()); + roots.back().identity = Identity("8841408a2e:0:bb1d31f2c323e264e9e64172c1a74f77899555ed10751cd56e86405cde118d02dffe555d462ccf6a85b5631c12350c8d5dc409ba10b9025d0f445cf449d92b1c"); + roots.back().stableEndpoints.push_back(InetAddress("45.32.198.130/9993")); // Dallas + roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6400:81c3:5400:00ff:fe18:1d61/9993")); // Dallas + roots.back().stableEndpoints.push_back(InetAddress("46.101.160.249/9993")); // Frankfurt + roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:3:d0::6a:3001/9993")); // Frankfurt + roots.back().stableEndpoints.push_back(InetAddress("107.191.46.210/9993")); // Paris + roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6800:83a4::64/9993")); // Paris + roots.back().stableEndpoints.push_back(InetAddress("45.32.246.179/9993")); // Sydney + roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:5800:8bf8:5400:ff:fe15:b39a/9993")); // Sydney + roots.back().stableEndpoints.push_back(InetAddress("45.32.248.87/9993")); // Tokyo + roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:7000:9bc9:5400:00ff:fe15:c4f5/9993")); // Tokyo + roots.back().stableEndpoints.push_back(InetAddress("159.203.2.154/9993")); // Toronto + roots.back().stableEndpoints.push_back(InetAddress("2604:a880:cad:d0::26:7001/9993")); // Toronto + + // END WORLD DEFINITION + // ========================================================================= + + fprintf(stderr,"INFO: generating and signing id==%llu ts==%llu"ZT_EOL_S,(unsigned long long)id,(unsigned long long)ts); + + World nw = World::make(World::TYPE_PLANET,id,ts,currentKP.pub,roots,previousKP); + + Buffer outtmp; + nw.serialize(outtmp,false); + World testw; + testw.deserialize(outtmp,0); + if (testw != nw) { + fprintf(stderr,"FATAL: serialization test failed!"ZT_EOL_S); + return 1; + } + + OSUtils::writeFile("world.bin",std::string((const char *)outtmp.data(),outtmp.size())); + fprintf(stderr,"INFO: world.bin written with %u bytes of binary world data."ZT_EOL_S,outtmp.size()); + + fprintf(stdout,ZT_EOL_S); + fprintf(stdout,"#define ZT_DEFAULT_WORLD_LENGTH %u"ZT_EOL_S,outtmp.size()); + fprintf(stdout,"static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {"); + for(unsigned int i=0;i 0) + fprintf(stdout,","); + fprintf(stdout,"0x%.2x",(unsigned int)d[i]); + } + fprintf(stdout,"};"ZT_EOL_S); + + return 0; +} diff --git a/attic/world/old/earth-2015-11-16.bin b/attic/world/old/earth-2015-11-16.bin new file mode 100644 index 00000000..910ff144 Binary files /dev/null and b/attic/world/old/earth-2015-11-16.bin differ diff --git a/attic/world/old/earth-2015-11-20.bin b/attic/world/old/earth-2015-11-20.bin new file mode 100644 index 00000000..198682e5 Binary files /dev/null and b/attic/world/old/earth-2015-11-20.bin differ diff --git a/attic/world/old/earth-2015-12-17.bin b/attic/world/old/earth-2015-12-17.bin new file mode 100644 index 00000000..20fadb56 Binary files /dev/null and b/attic/world/old/earth-2015-12-17.bin differ diff --git a/node/World.hpp b/node/World.hpp index 06dcb981..8fe6dd2e 100644 --- a/node/World.hpp +++ b/node/World.hpp @@ -227,6 +227,33 @@ public: inline bool operator<(const World &w) const { return (((int)_type < (int)w._type) ? true : ((_type == w._type) ? (_id < w._id) : false)); } + /** + * Create a World object signed with a key pair + * + * @param t World type + * @param id World ID + * @param ts World timestamp / revision + * @param sk Key that must be used to sign the next future update to this world + * @param roots Roots and their stable endpoints + * @param signWith Key to sign this World with (can have the same public as the next-update signing key, but doesn't have to) + * @return Signed World object + */ + static inline World make(World::Type t,uint64_t id,uint64_t ts,const C25519::Public &sk,const std::vector &roots,const C25519::Pair &signWith) + { + World w; + w._id = id; + w._ts = ts; + w._type = t; + w._updatesMustBeSignedBy = sk; + w._roots = roots; + + Buffer tmp; + w.serialize(tmp,true); + w._signature = C25519::sign(signWith,tmp.data(),tmp.size()); + + return w; + } + protected: uint64_t _id; uint64_t _ts; diff --git a/one.cpp b/one.cpp index 016aab74..37ea13d3 100644 --- a/one.cpp +++ b/one.cpp @@ -62,6 +62,8 @@ #include "node/CertificateOfMembership.hpp" #include "node/Utils.hpp" #include "node/NetworkController.hpp" +#include "node/Buffer.hpp" +#include "node/World.hpp" #include "osdep/OSUtils.hpp" #include "osdep/Http.hpp" @@ -545,6 +547,8 @@ static void idtoolPrintHelp(FILE *out,const char *pn) fprintf(out," getpublic " ZT_EOL_S); fprintf(out," sign " ZT_EOL_S); fprintf(out," verify " ZT_EOL_S); + fprintf(out," initmoon " ZT_EOL_S); + fprintf(out," genmoon " ZT_EOL_S); } static Identity getIdFromArg(char *arg) @@ -689,6 +693,89 @@ static int idtool(int argc,char **argv) fprintf(stderr,"%s signature check FAILED" ZT_EOL_S,argv[3]); return 1; } + } else if (!strcmp(argv[1],"initmoon")) { + if (argc < 3) { + idtoolPrintHelp(stdout,argv[0]); + } else { + const Identity id = getIdFromArg(argv[2]); + if (!id) { + fprintf(stderr,"%s is not a valid identity" ZT_EOL_S,argv[2]); + return 1; + } + + C25519::Pair kp(C25519::generate()); + + nlohmann::json mj; + mj["objtype"] = "world"; + mj["worldType"] = "moon"; + mj["updatesMustBeSignedBy"] = mj["signingKey"] = Utils::hex(kp.pub.data,(unsigned int)kp.pub.size()); + mj["signingKeySECRET"] = Utils::hex(kp.priv.data,(unsigned int)kp.priv.size()); + mj["id"] = (id.address().toString() + "000000"); + nlohmann::json seedj; + seedj["identity"] = id.toString(false); + seedj["stableEndpoints"] = nlohmann::json::array(); + (mj["roots"] = nlohmann::json::array()).push_back(seedj); + std::string mjd(OSUtils::jsonDump(mj)); + + printf("%s" ZT_EOL_S,mjd.c_str()); + } + } else if (!strcmp(argv[1],"genmoon")) { + if (argc < 3) { + idtoolPrintHelp(stdout,argv[0]); + } else { + std::string buf; + if (!OSUtils::readFile(argv[2],buf)) { + fprintf(stderr,"cannot read %s" ZT_EOL_S,argv[2]); + return 1; + } + nlohmann::json mj(OSUtils::jsonParse(buf)); + + uint64_t id = Utils::hexStrToU64(OSUtils::jsonString(mj["id"],"").c_str()); + + World::Type t; + if (mj["worldType"] == "moon") { + t = World::TYPE_MOON; + } else if (mj["worldType"] == "planet") { + t = World::TYPE_PLANET; + } else { + fprintf(stderr,"invalid worldType" ZT_EOL_S); + return 1; + } + + C25519::Pair signingKey; + C25519::Public updatesMustBeSignedBy; + Utils::unhex(OSUtils::jsonString(mj["singingKey"],""),signingKey.pub.data,(unsigned int)signingKey.pub.size()); + Utils::unhex(OSUtils::jsonString(mj["singingKeySECRET"],""),signingKey.priv.data,(unsigned int)signingKey.priv.size()); + Utils::unhex(OSUtils::jsonString(mj["updatesMustBeSignedBy"],""),updatesMustBeSignedBy.data,(unsigned int)updatesMustBeSignedBy.size()); + + std::vector roots; + nlohmann::json &rootsj = mj["roots"]; + if (rootsj.is_array()) { + for(unsigned long i=0;i<(unsigned long)rootsj.size();++i) { + nlohmann::json &r = rootsj[i]; + if (r.is_object()) { + roots.push_back(World::Root()); + roots.back().identity = Identity(OSUtils::jsonString(r["identity"],"")); + nlohmann::json &stableEndpointsj = r["stableEndpoints"]; + if (stableEndpointsj.is_array()) { + for(unsigned long k=0;k<(unsigned long)stableEndpointsj.size();++k) + roots.back().stableEndpoints.push_back(InetAddress(OSUtils::jsonString(stableEndpointsj[k],""))); + std::sort(roots.back().stableEndpoints.begin(),roots.back().stableEndpoints.end()); + } + } + } + } + std::sort(roots.begin(),roots.end()); + + const uint64_t now = OSUtils::now(); + World w(World::make(t,id,now,updatesMustBeSignedBy,roots,signingKey)); + Buffer wbuf; + w.serialize(wbuf); + char fn[128]; + Utils::snprintf(fn,sizeof(fn),"%.16llx_%.16llx.moon",w.id(),now); + OSUtils::writeFile(fn,wbuf.data(),wbuf.size()); + printf("wrote %s (signed world with timestamp %llu)" ZT_EOL_S,fn,now); + } } else { idtoolPrintHelp(stdout,argv[0]); return 1; diff --git a/world/README.md b/world/README.md deleted file mode 100644 index dda4920a..00000000 --- a/world/README.md +++ /dev/null @@ -1,7 +0,0 @@ -World Definitions and Generator Code -====== - -This little bit of code is used to generate world updates. Ordinary users probably will never need this unless they want to test or experiment. - -See mkworld.cpp for documentation. To build from this directory use 'source ./build.sh'. - diff --git a/world/build.sh b/world/build.sh deleted file mode 100755 index b783702c..00000000 --- a/world/build.sh +++ /dev/null @@ -1 +0,0 @@ -c++ -I.. -o mkworld ../node/C25519.cpp ../node/Salsa20.cpp ../node/SHA512.cpp ../node/Identity.cpp ../node/Utils.cpp ../node/InetAddress.cpp ../osdep/OSUtils.cpp mkworld.cpp diff --git a/world/earth-2016-01-13.bin b/world/earth-2016-01-13.bin deleted file mode 100644 index 5dea4d21..00000000 Binary files a/world/earth-2016-01-13.bin and /dev/null differ diff --git a/world/mkworld.cpp b/world/mkworld.cpp deleted file mode 100644 index 2e9e621f..00000000 --- a/world/mkworld.cpp +++ /dev/null @@ -1,169 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -/* - * This utility makes the World from the configuration specified below. - * It probably won't be much use to anyone outside ZeroTier, Inc. except - * for testing and experimentation purposes. - * - * If you want to make your own World you must edit this file. - * - * When run, it expects two files in the current directory: - * - * previous.c25519 - key pair to sign this world (key from previous world) - * current.c25519 - key pair whose public key should be embedded in this world - * - * If these files do not exist, they are both created with the same key pair - * and a self-signed initial World is born. - */ - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -using namespace ZeroTier; - -class WorldMaker : public World -{ -public: - static inline World make(World::Type t,uint64_t id,uint64_t ts,const C25519::Public &sk,const std::vector &roots,const C25519::Pair &signWith) - { - WorldMaker w; - w._id = id; - w._ts = ts; - w._type = t; - w._updateSigningKey = sk; - w._roots = roots; - - Buffer tmp; - w.serialize(tmp,true); - w._signature = C25519::sign(signWith,tmp.data(),tmp.size()); - - return w; - } -}; - -int main(int argc,char **argv) -{ - std::string previous,current; - if ((!OSUtils::readFile("previous.c25519",previous))||(!OSUtils::readFile("current.c25519",current))) { - C25519::Pair np(C25519::generate()); - previous = std::string(); - previous.append((const char *)np.pub.data,ZT_C25519_PUBLIC_KEY_LEN); - previous.append((const char *)np.priv.data,ZT_C25519_PRIVATE_KEY_LEN); - current = previous; - OSUtils::writeFile("previous.c25519",previous); - OSUtils::writeFile("current.c25519",current); - fprintf(stderr,"INFO: created initial world keys: previous.c25519 and current.c25519 (both initially the same)"ZT_EOL_S); - } - - if ((previous.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))||(current.length() != (ZT_C25519_PUBLIC_KEY_LEN + ZT_C25519_PRIVATE_KEY_LEN))) { - fprintf(stderr,"FATAL: previous.c25519 or current.c25519 empty or invalid"ZT_EOL_S); - return 1; - } - C25519::Pair previousKP; - memcpy(previousKP.pub.data,previous.data(),ZT_C25519_PUBLIC_KEY_LEN); - memcpy(previousKP.priv.data,previous.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN); - C25519::Pair currentKP; - memcpy(currentKP.pub.data,current.data(),ZT_C25519_PUBLIC_KEY_LEN); - memcpy(currentKP.priv.data,current.data() + ZT_C25519_PUBLIC_KEY_LEN,ZT_C25519_PRIVATE_KEY_LEN); - - // ========================================================================= - // EDIT BELOW HERE - - std::vector roots; - - const uint64_t id = ZT_WORLD_ID_EARTH; - const uint64_t ts = 1452708876314ULL; // January 13th, 2016 - - // Alice - roots.push_back(World::Root()); - roots.back().identity = Identity("9d219039f3:0:01f0922a98e3b34ebcbff333269dc265d7a020aab69d72be4d4acc9c8c9294785771256cd1d942a90d1bd1d2dca3ea84ef7d85afe6611fb43ff0b74126d90a6e"); - roots.back().stableEndpoints.push_back(InetAddress("188.166.94.177/9993")); // Amsterdam - roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:2:d0::7d:1/9993")); // Amsterdam - roots.back().stableEndpoints.push_back(InetAddress("154.66.197.33/9993")); // Johannesburg - roots.back().stableEndpoints.push_back(InetAddress("2c0f:f850:154:197::33/9993")); // Johannesburg - roots.back().stableEndpoints.push_back(InetAddress("159.203.97.171/9993")); // New York - roots.back().stableEndpoints.push_back(InetAddress("2604:a880:800:a1::54:6001/9993")); // New York - roots.back().stableEndpoints.push_back(InetAddress("169.57.143.104/9993")); // Sao Paolo - roots.back().stableEndpoints.push_back(InetAddress("2607:f0d0:1d01:57::2/9993")); // Sao Paolo - roots.back().stableEndpoints.push_back(InetAddress("107.170.197.14/9993")); // San Francisco - roots.back().stableEndpoints.push_back(InetAddress("2604:a880:1:20::200:e001/9993")); // San Francisco - roots.back().stableEndpoints.push_back(InetAddress("128.199.197.217/9993")); // Singapore - roots.back().stableEndpoints.push_back(InetAddress("2400:6180:0:d0::b7:4001/9993")); // Singapore - - // Bob - roots.push_back(World::Root()); - roots.back().identity = Identity("8841408a2e:0:bb1d31f2c323e264e9e64172c1a74f77899555ed10751cd56e86405cde118d02dffe555d462ccf6a85b5631c12350c8d5dc409ba10b9025d0f445cf449d92b1c"); - roots.back().stableEndpoints.push_back(InetAddress("45.32.198.130/9993")); // Dallas - roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6400:81c3:5400:00ff:fe18:1d61/9993")); // Dallas - roots.back().stableEndpoints.push_back(InetAddress("46.101.160.249/9993")); // Frankfurt - roots.back().stableEndpoints.push_back(InetAddress("2a03:b0c0:3:d0::6a:3001/9993")); // Frankfurt - roots.back().stableEndpoints.push_back(InetAddress("107.191.46.210/9993")); // Paris - roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:6800:83a4::64/9993")); // Paris - roots.back().stableEndpoints.push_back(InetAddress("45.32.246.179/9993")); // Sydney - roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:5800:8bf8:5400:ff:fe15:b39a/9993")); // Sydney - roots.back().stableEndpoints.push_back(InetAddress("45.32.248.87/9993")); // Tokyo - roots.back().stableEndpoints.push_back(InetAddress("2001:19f0:7000:9bc9:5400:00ff:fe15:c4f5/9993")); // Tokyo - roots.back().stableEndpoints.push_back(InetAddress("159.203.2.154/9993")); // Toronto - roots.back().stableEndpoints.push_back(InetAddress("2604:a880:cad:d0::26:7001/9993")); // Toronto - - // END WORLD DEFINITION - // ========================================================================= - - fprintf(stderr,"INFO: generating and signing id==%llu ts==%llu"ZT_EOL_S,(unsigned long long)id,(unsigned long long)ts); - - World nw = WorldMaker::make(World::TYPE_PLANET,id,ts,currentKP.pub,roots,previousKP); - - Buffer outtmp; - nw.serialize(outtmp,false); - World testw; - testw.deserialize(outtmp,0); - if (testw != nw) { - fprintf(stderr,"FATAL: serialization test failed!"ZT_EOL_S); - return 1; - } - - OSUtils::writeFile("world.bin",std::string((const char *)outtmp.data(),outtmp.size())); - fprintf(stderr,"INFO: world.bin written with %u bytes of binary world data."ZT_EOL_S,outtmp.size()); - - fprintf(stdout,ZT_EOL_S); - fprintf(stdout,"#define ZT_DEFAULT_WORLD_LENGTH %u"ZT_EOL_S,outtmp.size()); - fprintf(stdout,"static const unsigned char ZT_DEFAULT_WORLD[ZT_DEFAULT_WORLD_LENGTH] = {"); - for(unsigned int i=0;i 0) - fprintf(stdout,","); - fprintf(stdout,"0x%.2x",(unsigned int)d[i]); - } - fprintf(stdout,"};"ZT_EOL_S); - - return 0; -} diff --git a/world/old/earth-2015-11-16.bin b/world/old/earth-2015-11-16.bin deleted file mode 100644 index 910ff144..00000000 Binary files a/world/old/earth-2015-11-16.bin and /dev/null differ diff --git a/world/old/earth-2015-11-20.bin b/world/old/earth-2015-11-20.bin deleted file mode 100644 index 198682e5..00000000 Binary files a/world/old/earth-2015-11-20.bin and /dev/null differ diff --git a/world/old/earth-2015-12-17.bin b/world/old/earth-2015-12-17.bin deleted file mode 100644 index 20fadb56..00000000 Binary files a/world/old/earth-2015-12-17.bin and /dev/null differ -- cgit v1.2.3 From 04c7adea07d3df6684f7776999150631226f19a5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 8 Mar 2017 08:58:07 -0800 Subject: cleanup --- attic/BinarySemaphore.hpp | 97 --------------------------- attic/CertificateOfTrust.cpp | 67 ------------------- attic/CertificateOfTrust.hpp | 155 ------------------------------------------- attic/LockingPtr.hpp | 99 --------------------------- 4 files changed, 418 deletions(-) delete mode 100644 attic/BinarySemaphore.hpp delete mode 100644 attic/CertificateOfTrust.cpp delete mode 100644 attic/CertificateOfTrust.hpp delete mode 100644 attic/LockingPtr.hpp (limited to 'attic') diff --git a/attic/BinarySemaphore.hpp b/attic/BinarySemaphore.hpp deleted file mode 100644 index 315d2b00..00000000 --- a/attic/BinarySemaphore.hpp +++ /dev/null @@ -1,97 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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_BINARYSEMAPHORE_HPP -#define ZT_BINARYSEMAPHORE_HPP - -#include -#include -#include - -#include "Constants.hpp" -#include "NonCopyable.hpp" - -#ifdef __WINDOWS__ - -#include - -namespace ZeroTier { - -class BinarySemaphore : NonCopyable -{ -public: - BinarySemaphore() throw() { _sem = CreateSemaphore(NULL,0,1,NULL); } - ~BinarySemaphore() { CloseHandle(_sem); } - inline void wait() { WaitForSingleObject(_sem,INFINITE); } - inline void post() { ReleaseSemaphore(_sem,1,NULL); } -private: - HANDLE _sem; -}; - -} // namespace ZeroTier - -#else // !__WINDOWS__ - -#include - -namespace ZeroTier { - -class BinarySemaphore : NonCopyable -{ -public: - BinarySemaphore() - { - pthread_mutex_init(&_mh,(const pthread_mutexattr_t *)0); - pthread_cond_init(&_cond,(const pthread_condattr_t *)0); - _f = false; - } - - ~BinarySemaphore() - { - pthread_cond_destroy(&_cond); - pthread_mutex_destroy(&_mh); - } - - inline void wait() - { - pthread_mutex_lock(const_cast (&_mh)); - while (!_f) - pthread_cond_wait(const_cast (&_cond),const_cast (&_mh)); - _f = false; - pthread_mutex_unlock(const_cast (&_mh)); - } - - inline void post() - { - pthread_mutex_lock(const_cast (&_mh)); - _f = true; - pthread_mutex_unlock(const_cast (&_mh)); - pthread_cond_signal(const_cast (&_cond)); - } - -private: - pthread_cond_t _cond; - pthread_mutex_t _mh; - volatile bool _f; -}; - -} // namespace ZeroTier - -#endif // !__WINDOWS__ - -#endif diff --git a/attic/CertificateOfTrust.cpp b/attic/CertificateOfTrust.cpp deleted file mode 100644 index e85a91df..00000000 --- a/attic/CertificateOfTrust.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -#include "CertificateOfTrust.hpp" - -#include "RuntimeEnvironment.hpp" -#include "Topology.hpp" -#include "Switch.hpp" - -namespace ZeroTier { - -bool CertificateOfTrust::create(uint64_t ts,uint64_t rls,const Identity &iss,const Identity &tgt,Level l) -{ - if ((!iss)||(!iss.hasPrivate())) - return false; - - _timestamp = ts; - _roles = rls; - _issuer = iss.address(); - _target = tgt; - _level = l; - - Buffer tmp; - tmp.append(_timestamp); - tmp.append(_roles); - _issuer.appendTo(tmp); - _target.serialize(tmp,false); - tmp.append((uint16_t)_level); - _signature = iss.sign(tmp.data(),tmp.size()); - - return true; -} - -int CertificateOfTrust::verify(const RuntimeEnvironment *RR) const -{ - const Identity id(RR->topology->getIdentity(_issuer)); - if (!id) { - RR->sw->requestWhois(_issuer); - return 1; - } - - Buffer tmp; - tmp.append(_timestamp); - tmp.append(_roles); - _issuer.appendTo(tmp); - _target.serialize(tmp,false); - tmp.append((uint16_t)_level); - - return (id.verify(tmp.data(),tmp.size(),_signature) ? 0 : -1); -} - -} // namespace ZeroTier diff --git a/attic/CertificateOfTrust.hpp b/attic/CertificateOfTrust.hpp deleted file mode 100644 index 6e3c8743..00000000 --- a/attic/CertificateOfTrust.hpp +++ /dev/null @@ -1,155 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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_CERTIFICATEOFTRUST_HPP -#define ZT_CERTIFICATEOFTRUST_HPP - -#include "Constants.hpp" -#include "Identity.hpp" -#include "C25519.hpp" -#include "Buffer.hpp" - -namespace ZeroTier { - -class RuntimeEnvironment; - -/** - * Certificate of peer to peer trust - */ -class CertificateOfTrust -{ -public: - /** - * Trust levels, with 0 indicating anti-trust - */ - enum Level - { - /** - * Negative trust is reserved for informing peers that another peer is misbehaving, etc. Not currently used. - */ - LEVEL_NEGATIVE = 0, - - /** - * Default trust -- for most peers - */ - LEVEL_DEFAULT = 1, - - /** - * Above normal trust, e.g. common network membership - */ - LEVEL_MEDIUM = 25, - - /** - * High trust -- e.g. an upstream or a controller - */ - LEVEL_HIGH = 50, - - /** - * Right now ultimate is only for roots - */ - LEVEL_ULTIMATE = 100 - }; - - /** - * Role bit masks - */ - enum Role - { - /** - * Target is permitted to represent issuer on the network as a federated root / relay - */ - ROLE_UPSTREAM = 0x00000001 - }; - - CertificateOfTrust() : - _timestamp(0), - _roles(0), - _issuer(), - _target(), - _level(LEVEL_DEFAULT), - _signature() {} - - /** - * Create and sign this certificate of trust - * - * @param ts Cert timestamp - * @param rls Roles bitmap - * @param iss Issuer identity (must have secret key!) - * @param tgt Target identity - * @param l Trust level - * @return True on successful signature - */ - bool create(uint64_t ts,uint64_t rls,const Identity &iss,const Identity &tgt,Level l); - - /** - * Verify this COT and its signature - * - * @param RR Runtime environment for looking up peers - * @return 0 == OK, 1 == waiting for WHOIS, -1 == BAD signature or credential - */ - int verify(const RuntimeEnvironment *RR) const; - - inline bool roleUpstream() const { return ((_roles & (uint64_t)ROLE_UPSTREAM) != 0); } - - inline uint64_t timestamp() const { return _timestamp; } - inline uint64_t roles() const { return _roles; } - inline const Address &issuer() const { return _issuer; } - inline const Identity &target() const { return _target; } - inline Level level() const { return _level; } - - inline operator bool() const { return (_issuer); } - - template - inline void serialize(Buffer &b) const - { - b.append(_timestamp); - b.append(_roles); - _issuer.appendTo(b); - _target.serialize(b); - b.append((uint16_t)_level); - b.append((uint8_t)1); // 1 == ed25519 signature - b.append((uint16_t)ZT_C25519_SIGNATURE_LEN); - b.append(_signature.data,ZT_C25519_SIGNATURE_LEN); - b.append((uint16_t)0); // length of additional fields - } - - template - inline unsigned int deserialize(const Buffer &b,unsigned int startAt = 0) - { - unsigned int p = startAt; - _timestamp = b.template at(p); p += 8; - _roles = b.template at(p); p += 8; - _issuer.setTo(b.field(p,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); p += ZT_ADDRESS_LENGTH; - p += _target.deserialize(b,p); - _level = b.template at(p); p += 2; - p += b.template at(p); p += 2; - return (p - startAt); - } - -private: - uint64_t _timestamp; - uint64_t _roles; - Address _issuer; - Identity _target; - Level _level; - C25519::Signature _signature; -}; - -} // namespace ZeroTier - -#endif diff --git a/attic/LockingPtr.hpp b/attic/LockingPtr.hpp deleted file mode 100644 index c373129a..00000000 --- a/attic/LockingPtr.hpp +++ /dev/null @@ -1,99 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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_LOCKINGPTR_HPP -#define ZT_LOCKINGPTR_HPP - -#include "Mutex.hpp" - -namespace ZeroTier { - -/** - * A simple pointer that locks and holds a mutex until destroyed - * - * Care must be taken when using this. It's not very sophisticated and does - * not handle being copied except for the simple return use case. When it is - * copied it hands off the mutex to the copy and clears it in the original, - * meaning that the mutex is unlocked when the last LockingPtr<> in a chain - * of such handoffs is destroyed. If this chain of handoffs "forks" (more than - * one copy is made) then non-determinism may ensue. - * - * This does not delete or do anything else with the pointer. It also does not - * take care of locking the lock. That must be done beforehand. - */ -template -class LockingPtr -{ -public: - LockingPtr() : - _ptr((T *)0), - _lock((Mutex *)0) - { - } - - LockingPtr(T *obj,Mutex *lock) : - _ptr(obj), - _lock(lock) - { - } - - LockingPtr(const LockingPtr &p) : - _ptr(p._ptr), - _lock(p._lock) - { - const_cast(&p)->_lock = (Mutex *)0; - } - - ~LockingPtr() - { - if (_lock) - _lock->unlock(); - } - - inline LockingPtr &operator=(const LockingPtr &p) - { - _ptr = p._ptr; - _lock = p._lock; - const_cast(&p)->_lock = (Mutex *)0; - return *this; - } - - inline operator bool() const throw() { return (_ptr != (T *)0); } - inline T &operator*() const throw() { return *_ptr; } - inline T *operator->() const throw() { return _ptr; } - - /** - * @return Raw pointer to held object - */ - inline T *ptr() const throw() { return _ptr; } - - inline bool operator==(const LockingPtr &sp) const throw() { return (_ptr == sp._ptr); } - inline bool operator!=(const LockingPtr &sp) const throw() { return (_ptr != sp._ptr); } - inline bool operator>(const LockingPtr &sp) const throw() { return (_ptr > sp._ptr); } - inline bool operator<(const LockingPtr &sp) const throw() { return (_ptr < sp._ptr); } - inline bool operator>=(const LockingPtr &sp) const throw() { return (_ptr >= sp._ptr); } - inline bool operator<=(const LockingPtr &sp) const throw() { return (_ptr <= sp._ptr); } - -private: - T *_ptr; - Mutex *_lock; -}; - -} // namespace ZeroTier - -#endif -- cgit v1.2.3 From 645bf4a764e6240bb7f36683aa739505f40b9cce Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 14 Apr 2017 13:30:12 -0700 Subject: Resurrect zerotier-containerized. --- .../other/zerotier-containerized/Dockerfile | 20 -------------------- .../other/zerotier-containerized/main.sh | 10 ---------- .../linux/zerotier-containerized/Dockerfile | 20 ++++++++++++++++++++ .../linux/zerotier-containerized/main.sh | 10 ++++++++++ 4 files changed, 30 insertions(+), 30 deletions(-) delete mode 100644 attic/linux-build-farm/other/zerotier-containerized/Dockerfile delete mode 100755 attic/linux-build-farm/other/zerotier-containerized/main.sh create mode 100644 ext/installfiles/linux/zerotier-containerized/Dockerfile create mode 100755 ext/installfiles/linux/zerotier-containerized/main.sh (limited to 'attic') diff --git a/attic/linux-build-farm/other/zerotier-containerized/Dockerfile b/attic/linux-build-farm/other/zerotier-containerized/Dockerfile deleted file mode 100644 index 678216da..00000000 --- a/attic/linux-build-farm/other/zerotier-containerized/Dockerfile +++ /dev/null @@ -1,20 +0,0 @@ -FROM alpine:latest -MAINTAINER Adam Ierymenko - -LABEL version="1.1.14" -LABEL description="Containerized ZeroTier One for use on CoreOS or other Docker-only Linux hosts." - -# Uncomment to build in container -#RUN apk add --update alpine-sdk linux-headers - -RUN apk add --update libgcc libstdc++ - -ADD zerotier-one / -RUN chmod 0755 /zerotier-one -RUN ln -sf /zerotier-one /zerotier-cli -RUN mkdir -p /var/lib/zerotier-one - -ADD main.sh / -RUN chmod 0755 /main.sh - -ENTRYPOINT /main.sh diff --git a/attic/linux-build-farm/other/zerotier-containerized/main.sh b/attic/linux-build-farm/other/zerotier-containerized/main.sh deleted file mode 100755 index 685a6891..00000000 --- a/attic/linux-build-farm/other/zerotier-containerized/main.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -if [ ! -e /dev/net/tun ]; then - echo 'FATAL: cannot start ZeroTier One in container: /dev/net/tun not present.' - exit 1 -fi - -exec /zerotier-one diff --git a/ext/installfiles/linux/zerotier-containerized/Dockerfile b/ext/installfiles/linux/zerotier-containerized/Dockerfile new file mode 100644 index 00000000..678216da --- /dev/null +++ b/ext/installfiles/linux/zerotier-containerized/Dockerfile @@ -0,0 +1,20 @@ +FROM alpine:latest +MAINTAINER Adam Ierymenko + +LABEL version="1.1.14" +LABEL description="Containerized ZeroTier One for use on CoreOS or other Docker-only Linux hosts." + +# Uncomment to build in container +#RUN apk add --update alpine-sdk linux-headers + +RUN apk add --update libgcc libstdc++ + +ADD zerotier-one / +RUN chmod 0755 /zerotier-one +RUN ln -sf /zerotier-one /zerotier-cli +RUN mkdir -p /var/lib/zerotier-one + +ADD main.sh / +RUN chmod 0755 /main.sh + +ENTRYPOINT /main.sh diff --git a/ext/installfiles/linux/zerotier-containerized/main.sh b/ext/installfiles/linux/zerotier-containerized/main.sh new file mode 100755 index 00000000..685a6891 --- /dev/null +++ b/ext/installfiles/linux/zerotier-containerized/main.sh @@ -0,0 +1,10 @@ +#!/bin/sh + +export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin + +if [ ! -e /dev/net/tun ]; then + echo 'FATAL: cannot start ZeroTier One in container: /dev/net/tun not present.' + exit 1 +fi + +exec /zerotier-one -- cgit v1.2.3 From 54c47a1e03b5a7446315fc2cff64fb93fd85c5b7 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 4 May 2017 10:42:22 -0700 Subject: Add some historic code just for the heck of it. --- attic/historic/anode/LICENSE.txt | 674 +++++++++++++++ attic/historic/anode/config.mk.Darwin | 17 + attic/historic/anode/config.mk.Linux | 17 + attic/historic/anode/docs/anode_protocol.txt | 764 +++++++++++++++++ attic/historic/anode/libanode/Makefile | 33 + attic/historic/anode/libanode/address.c | 98 +++ attic/historic/anode/libanode/aes_digest.c | 85 ++ attic/historic/anode/libanode/anode.h | 795 +++++++++++++++++ attic/historic/anode/libanode/errors.c | 52 ++ attic/historic/anode/libanode/identity.c | 110 +++ attic/historic/anode/libanode/impl/aes.c | 72 ++ attic/historic/anode/libanode/impl/aes.h | 64 ++ attic/historic/anode/libanode/impl/dictionary.c | 239 ++++++ attic/historic/anode/libanode/impl/dictionary.h | 126 +++ attic/historic/anode/libanode/impl/dns_txt.c | 93 ++ attic/historic/anode/libanode/impl/dns_txt.h | 37 + attic/historic/anode/libanode/impl/ec.c | 219 +++++ attic/historic/anode/libanode/impl/ec.h | 61 ++ attic/historic/anode/libanode/impl/environment.c | 118 +++ attic/historic/anode/libanode/impl/environment.h | 30 + attic/historic/anode/libanode/impl/http_client.c | 558 ++++++++++++ attic/historic/anode/libanode/impl/http_client.h | 200 +++++ attic/historic/anode/libanode/impl/misc.c | 190 +++++ attic/historic/anode/libanode/impl/misc.h | 193 +++++ attic/historic/anode/libanode/impl/mutex.h | 34 + attic/historic/anode/libanode/impl/thread.c | 58 ++ attic/historic/anode/libanode/impl/thread.h | 65 ++ attic/historic/anode/libanode/impl/types.h | 25 + attic/historic/anode/libanode/network_address.c | 136 +++ attic/historic/anode/libanode/secure_random.c | 88 ++ attic/historic/anode/libanode/system_transport.c | 948 +++++++++++++++++++++ attic/historic/anode/libanode/tests/Makefile | 25 + attic/historic/anode/libanode/tests/aes-test.c | 191 +++++ .../libanode/tests/anode-secure_random-test.c | 38 + .../anode/libanode/tests/anode-utils-test.c | 75 ++ .../anode/libanode/tests/anode-zone-test.c | 47 + .../anode/libanode/tests/dictionary-test.c | 149 ++++ attic/historic/anode/libanode/tests/ec-test.c | 97 +++ .../anode/libanode/tests/environment-test.c | 28 + .../anode/libanode/tests/http_client-test.c | 233 +++++ attic/historic/anode/libanode/tests/misc-test.c | 137 +++ .../anode/libanode/tests/system_transport-test.c | 70 ++ attic/historic/anode/libanode/uri.c | 185 ++++ .../anode/libanode/utils/anode-make-identity.c | 50 ++ attic/historic/anode/libanode/zone.c | 184 ++++ attic/historic/anode/libspark/Makefile | 16 + .../experiments/FindGoodSegmentDelimiters.cpp | 161 ++++ attic/historic/anode/libspark/experiments/Makefile | 5 + attic/historic/anode/libspark/streamencoder.h | 108 +++ attic/historic/anode/libspark/wrapper.h | 66 ++ 50 files changed, 8064 insertions(+) create mode 100644 attic/historic/anode/LICENSE.txt create mode 100644 attic/historic/anode/config.mk.Darwin create mode 100644 attic/historic/anode/config.mk.Linux create mode 100644 attic/historic/anode/docs/anode_protocol.txt create mode 100644 attic/historic/anode/libanode/Makefile create mode 100644 attic/historic/anode/libanode/address.c create mode 100644 attic/historic/anode/libanode/aes_digest.c create mode 100644 attic/historic/anode/libanode/anode.h create mode 100644 attic/historic/anode/libanode/errors.c create mode 100644 attic/historic/anode/libanode/identity.c create mode 100644 attic/historic/anode/libanode/impl/aes.c create mode 100644 attic/historic/anode/libanode/impl/aes.h create mode 100644 attic/historic/anode/libanode/impl/dictionary.c create mode 100644 attic/historic/anode/libanode/impl/dictionary.h create mode 100644 attic/historic/anode/libanode/impl/dns_txt.c create mode 100644 attic/historic/anode/libanode/impl/dns_txt.h create mode 100644 attic/historic/anode/libanode/impl/ec.c create mode 100644 attic/historic/anode/libanode/impl/ec.h create mode 100644 attic/historic/anode/libanode/impl/environment.c create mode 100644 attic/historic/anode/libanode/impl/environment.h create mode 100644 attic/historic/anode/libanode/impl/http_client.c create mode 100644 attic/historic/anode/libanode/impl/http_client.h create mode 100644 attic/historic/anode/libanode/impl/misc.c create mode 100644 attic/historic/anode/libanode/impl/misc.h create mode 100644 attic/historic/anode/libanode/impl/mutex.h create mode 100644 attic/historic/anode/libanode/impl/thread.c create mode 100644 attic/historic/anode/libanode/impl/thread.h create mode 100644 attic/historic/anode/libanode/impl/types.h create mode 100644 attic/historic/anode/libanode/network_address.c create mode 100644 attic/historic/anode/libanode/secure_random.c create mode 100644 attic/historic/anode/libanode/system_transport.c create mode 100644 attic/historic/anode/libanode/tests/Makefile create mode 100644 attic/historic/anode/libanode/tests/aes-test.c create mode 100644 attic/historic/anode/libanode/tests/anode-secure_random-test.c create mode 100644 attic/historic/anode/libanode/tests/anode-utils-test.c create mode 100644 attic/historic/anode/libanode/tests/anode-zone-test.c create mode 100644 attic/historic/anode/libanode/tests/dictionary-test.c create mode 100644 attic/historic/anode/libanode/tests/ec-test.c create mode 100644 attic/historic/anode/libanode/tests/environment-test.c create mode 100644 attic/historic/anode/libanode/tests/http_client-test.c create mode 100644 attic/historic/anode/libanode/tests/misc-test.c create mode 100644 attic/historic/anode/libanode/tests/system_transport-test.c create mode 100644 attic/historic/anode/libanode/uri.c create mode 100644 attic/historic/anode/libanode/utils/anode-make-identity.c create mode 100644 attic/historic/anode/libanode/zone.c create mode 100644 attic/historic/anode/libspark/Makefile create mode 100644 attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp create mode 100644 attic/historic/anode/libspark/experiments/Makefile create mode 100644 attic/historic/anode/libspark/streamencoder.h create mode 100644 attic/historic/anode/libspark/wrapper.h (limited to 'attic') diff --git a/attic/historic/anode/LICENSE.txt b/attic/historic/anode/LICENSE.txt new file mode 100644 index 00000000..94a9ed02 --- /dev/null +++ b/attic/historic/anode/LICENSE.txt @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + 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 . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/attic/historic/anode/config.mk.Darwin b/attic/historic/anode/config.mk.Darwin new file mode 100644 index 00000000..a5cbeeec --- /dev/null +++ b/attic/historic/anode/config.mk.Darwin @@ -0,0 +1,17 @@ +CC=gcc +CXX=g++ + +#ARCH_FLAGS=-arch x86_64 -arch i386 -arch ppc + +DEFS=-DHAS_DEV_URANDOM +CXXDEFS=-DBOOST_DISABLE_ASSERTS -DBOOST_NO_TYPEID -DNDEBUG + +CFLAGS=-mmacosx-version-min=10.4 -std=c99 -O6 -ftree-vectorize -Wall $(DEFS) $(ARCH_FLAGS) +CXXFLAGS=-mmacosx-version-min=10.4 -Drestrict=__restrict__ -O6 -ftree-vectorize -Wall $(DEFS) $(CXXDEFS) $(ARCH_FLAGS) + +LDFLAGS=-mmacosx-version-min=10.4 $(ARCH_FLAGS) +DLLFLAGS=$(ARCH_FLAGS) -shared +DLLEXT=dylib + +LIBANODE_LIBS=-lcrypto -lpthread -lresolv +LIBSPARK_LIBS=-lz diff --git a/attic/historic/anode/config.mk.Linux b/attic/historic/anode/config.mk.Linux new file mode 100644 index 00000000..072fa4ce --- /dev/null +++ b/attic/historic/anode/config.mk.Linux @@ -0,0 +1,17 @@ +CC=gcc +CXX=g++ + +DEFS=-DHAS_DEV_URANDOM + +CFLAGS=-std=c99 -O6 -fPIC -Wall $(DEFS) +CXXFLAGS=-Drestrict=__restrict__ -O6 -Wall $(DEFS) -I.. + +#CFLAGS=-g -Wall $(DEFS) +#CXXFLAGS=-g -Wall $(DEFS) + +LDFLAGS= +DLLFLAGS=-shared +DLLEXT=so + +LIBANODE_LIBS=-lcrypto -lresolv -pthread +LIBSPARK_LIBS=-lz diff --git a/attic/historic/anode/docs/anode_protocol.txt b/attic/historic/anode/docs/anode_protocol.txt new file mode 100644 index 00000000..0adb9236 --- /dev/null +++ b/attic/historic/anode/docs/anode_protocol.txt @@ -0,0 +1,764 @@ +***************************************************************************** +Anode Protocol Specification Draft +Version 0.8 + +(c)2009-2010 Adam Ierymenko +***************************************************************************** + +Table of Contents + +***************************************************************************** + +1. Introduction + +Anode provides three components that work together to provide a global, +secure, and mobile addressing system for computer networks: + +1) An addressing system based on public key cryptography enabling network + devices or applications to assign themselves secure, unique, and globally + reachable network addresses in a flat address space. + +2) A system enabling network participants holding global addresses to locate + one another on local or global networks with "zero configuration." + +3) A communications protocol for communication between addressed network + participants that requires no special operating system support and no + changes to existing network infrastructure. + +Using Anode, both fixed and mobile applications and devices can communicate +directly as if they were all connected to the same VPN. Anode restores the +original vision of the Internet as a "flat" network where anything can talk +to anything, and adds the added benefits of address mobility and strong +protection against address spoofing and other protocol level attacks. + +1.1. Design Philosophy + +Anode's design philosophy is the classical "KISS" principle: "Keep It Simple +Stupid." Anode's design principles are: + +#1: Do not try to solve too many problems at once, and stay in scope. + +Anode does not attempt to solve too many problems at once. It attempts to +solve the problems of mobile addressing, address portability, and "flat" +addressing in the presence of NAT or other barriers. + +It does not attempt to duplicate the full functionality of SSL, X.509, SSH, +XMPP, an enterprise service bus, a pub/sub architecture, BitTorrent, etc. All +of those protocols and services can be used over Anode if their functionality +is desired. + +#2: Avoid state management. + +State multiplies the complexity and failure modes of network protocols. State +also tends to get in the way of the achievement of new features implicitly +(see principle #4). Avoid state whenever possible. + +#3: Avoid algorithm and dependency bloat. + +Anode uses only elliptic curve Diffie-Hellman (EC-DH) and AES-256. No other +cryptographic algorithms or hash functions are presently necessary. This +yields implementations compact enough for embedded devices. + +Anode also requires few or no dependencies, depending on whether the two +needed cryptographic algorithms are obtained through a library or included. +No other protocols or libraries are required in an implementation. + +#4: Achieve features implicitly. + +Use a simple stateless design that allows features to be achieved implicitly +rather than specified explicitly. For example, Anode can do multi-homing and +could be used to build a mesh network, but neither of these features is +explicitly specified. + +***************************************************************************** + +2. Core Concepts and Algorithms + +This section describes addresses, zones, common algorithms, and other core +concepts. + +2.1. Zones + +A zone is a 32-bit integer encoded into every Anode address. Zones serve to +assist in the location of peers by address on global IP networks. They are +not presently significant for local communications, though they could be +used to partition addresses into groups or link them with configuration +options. + +Each zone has a corresponding zone file which can be fetched in a number of +ways (see below). A zone file is a flat text format dictionary of the format +"key=value" separated by carriage returns. Line feeds are ignored, and any +character may be escaped with a backslash (\) character. Blank lines are +ignored. + +The following entries must appear in a zone file: + +n= +d= +c= +r= +ttl= + +Additional fields may appear as well, including fields specific to special +applications or protocols supported within the zone. Some of these are +defined in this document. + +Zone file fetching mechanisms are described below. Multiple mechanisms are +specified to enable fallback in the event that one mechanism is not available. + +2.1.1. Zone File Retrieval + +Zone files are retrieved via HTTP, with the HTTP address being formed in one +of two ways. + +The preferred DNS method: + +To fetch a zone file via DNS, use the zone ID to generate a host name and URI +of the form: + + http://a--XXXXXXXX.net/z + +The XXXXXXXX field is the zone ID in hexadecimal. + +The fallback IP method: + +For fallback in the absence of DNS, the zone ID can be used directly as an +IPv4 or IPv4-mapped-to-IPv6 IP address. A URI is generated of the form: + + http://ip_address/z + +Support for this method requires that a zone ID be chosen to correspond to a +permanent IPv4 (preferably mappable to IPv6 space as well) IP address. + +2.1.2. Zone ID Reservation + +By convention, a zone ID is considered reserved when a domain of the form +"a--XXXXXXXX.net" (where XXXXXXXX is the ID in hex) is registered. + +It is recommended that this be done even for zone IDs not used for global +address location in order to globally reserve them. + +2.2. Addresses + +Anode addresses are binary strings containing a 32-bit zone ID, a public key, +and possibly other fields. Only one address type is presently defined: + +|---------------------------------------------------------------------------| +| Name | Type ID | Elliptic Curve Parameters | Total Length | +|---------------------------------------------------------------------------| +| ANODE-256-40 | 1 | NIST-P-256 | 40 | +|---------------------------------------------------------------------------| + +|---------------------------------------------------------------------------| +| Name | Binary Layout | +|---------------------------------------------------------------------------| +| ANODE-256-40 | | +|---------------------------------------------------------------------------| + +The public key is a "compressed" form elliptic curve public key as described +in RFC5480. + +The unused section of the address must be zero. These bytes are reserved for +future use. + +2.2.1. ASCII Format For Addresses + +Addresses are encoded in ASCII using base-32, which provides a quotable and +printable encoding that is of manageable length and is case-insensitive. For +example, an ANODE-256-40 address is 64 characters long in base-32 encoding. + +2.3. Relaying + +An Anode peer may optionally relay packets to any other reachable peer. +Relaying is accomplished by sending a packet to a peer with the recipient set +to the final recipient. The receiving peer will, if relaying is allowed and if +it knows of or can reach the recipient, forward the packet. + +No error is returned if relaying fails, so relay paths are treated as possible +paths for communication until a return is received in the same way as direct +paths. + +Relaying can be used by peers to send messages indirectly, locate one +another, and determine network location information to facilitate the +establishment of direct communications. + +Peers may refuse to relay or may limit the transmission rate at which packets +can be relayed. + +2.3.1. Zone Relays + +If a zone's addresses are globally reachable on global IP networks, it must +have one or more zone relays. These must have globally reachable public +static IP addresses. + +Zone relays are specified in the zone file in the following format: + + zr.
=[,]::: + +The address checksum is the sum of the bytes in the Anode address modulus +the number of "zr" entries, in hexadecimal. For example, if a zone had four +global relays its zone file could contain the lines: + + zr.0=1.2.3.4:4343:4344:klj4j3... + zr.1=2.3.4.5:4343:4344:00194j... + zr.2=3.4.5.6:4343:4344:1j42zz... + zr.3=4.5.6.7:4343:4344:z94j1q... + +The relay would be chosen by taking the sum of the bytes in the address +modulo 4. For example, if the bytes of an address sum to 5081 then relay +zr.1 would be used to communicate with that address. + +If more than one IP address is listed for a given relay, the peer must choose +at random from among the addresses of the desired type (IPv4 or IPv6). + +Each relay must have one Anode address for every address type supported within +the zone. (At present there is only one address type defined.) + +Peers should prefer UDP and fall back to TCP only if UDP is not available. + +To make itself available, a peer must make itself known to its designated zone +relay. This is accomplished by sending a PING message. + +2.4. Key Agreement and Derivation + +Key agreement is performed using elliptic curve Diffie-Hellman. This yields +a raw key whose size depends on the elliptic curve parameters in use. + +The following algorithm is used to derive a key of any length from a raw +key generated through key agreement: + +1) Zero the derived key buffer. +2) Determine the largest of the original raw key or the derived key. +3) Loop from 0 to the largest length determined in step 2, XOR each byte of + the derived key buffer with the corresponding byte of the original key + buffer with each index being modulus the length of the respective buffer. + +2.5. Message Authentication + +For message authentication, CMAC-AES (with AES-256) is used. This is also +known in some literature as OMAC1-AES. The key is derived from key agreement +between the key pair of the sending peer and the address of the recipient. + +2.6. AES-DIGEST + +To maintain cryptographic algorithm frugality, a cryptographic hash function +is constructed from the AES-256 cipher. This hash function uses the common +Davis-Meyer construction with Merkle-Damgård length padding. + +It is described by the following pseudocode: + + byte previous_digest[16] + byte digest[16] = { 0,0,... } + byte block[32] = { 0,0,... } + integer block_counter = 0 + + ; digest message + for each byte b of message + block[block_counter] = b + block_counter = block_counter + 1 + if block_counter == 32 then + block_counter = 0 + save digest[] in previous_digest[] + encrypt digest[] with aes-256 using block[] as 256-bit aes-256 key + xor digest[] with previous_digest[] + end if + next + + ; append end marker, do final block + block[block_counter] = 0x80 + block_counter = block_counter + 1 + zero rest of block[] from block_counter to 15 + save digest[] in previous_digest[] + encrypt digest[] with aes-256 using block[] as 256-bit aes-256 key + xor digest[] with previous_digest[] + + ; Merkle-Damgård length padding + zero first 8 bytes of block[] + fill last 8 bytes of block[] w/64-bit length in big-endian order + save digest[] in previous_digest[] + encrypt digest[] with aes-256 using block[] as 256-bit aes-128 key + xor digest[] with previous_digest[] + + ; digest[] now contains 128-bit message digest + +2.7. Short Address Identifiers (Address IDs) + +A short 8-byte version of the Anode address is used in the protocol to reduce +transmission overhead when both sides are already aware of the other's full +address. + +The short address identifier is formed by computing the AES-DIGEST of the +full address and then XORing the first 8 bytes of the digest with the last +8 bytes to yield an 8-byte shortened digest. + +2.8. DNS Resolution of Anode Addresses + +Anode addresses can be saved in DNS TXT records in the following format: + +anode:
+ +This permits Anode addresses to be resolved from normal DNS host name. + +2.9. Packet Transmission Mechanisms + +2.9.1. UDP Transmission + +The recommended method of sending Anode packets is UDP. Each packet is simply +sent as a UDP packet. + +2.9.2. TCP Transmission + +To send packets over TCP, each packet is prefixed by its size as a 16-bit +integer. + +2.9.3. HTTP Transmission + +Anode packets may be submitted in HTTP POST transactions for transport over +networks where HTTP is the only available protocol. + +Anode packets are simply prefixed with a 16-byte packet size and concatenated +together just as they are in a TCP stream. One or more packets may be sent +with each HTTP POST transaction for improved performance. + +Since this method is intended for use in "hostile" or highly restricted +circumstances, no additional details such as special headers or MIME types +are specified to allow maximum flexibility. Peers should ignore anything +other than the payload. + +2.10. Endpoints + +An endpoint indicates a place where Anode packets may be sent. The following +endpoint types are specified: + +|---------------------------------------------------------------------------| +| Endpoint Type | Description | Address Format | +|---------------------------------------------------------------------------| +| 0x00 | Unspecified | (none) | +| 0x01 | Ethernet | | +| 0x02 | UDP/IPv4 | | +| 0x03 | TCP/IPv4 | | +| 0x04 | UDP/IPv6 | | +| 0x05 | TCP/IPv6 | | +| 0x06 | HTTP | | +|---------------------------------------------------------------------------| + +Endpoints are encoded by beginning with a single byte indicating the endpoint +type followed by the address information required for the given type. + +Note that IP ports bear no relationship to Anode protocol ports. + +2.11. Notes + +All integers in the protocol are transmitted in network (big endian) byte +order. + +***************************************************************************** + +3. Common Packet Format + +A common header is used for all Anode packets: + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Hop Count | 1 | 8-bit hop count (not included in MAC) | +| Flags | 1 | 8-bit flags | +| MAC | 8 | 8 byte shortened CMAC-AES of packet | +| Sender Address | ? | Full address or short ID of sender | +| Recipient Address | ? | Full address or short ID of recipient | +| Peer IDs | 1 | Two 4-bit peer IDs: sender, recipient | +| Message Type | 1 | 8-bit message type | +| Message | ? | Message payload | +|---------------------------------------------------------------------------| + +3.1. Hop Count + +The hop count begins at zero and must be incremented by each peer that relays +the packet to another peer. The hop count must not wrap to zero at 255. + +Because the hop count is modified in transit, it is not included in MAC +calculation or authentication. + +The hop count is used to prioritize endpoints that are direct over endpoints +that involve relaying, or to prioritize closer routes over more distant +ones. + +3.2. Flags and Flag Behavior + +|---------------------------------------------------------------------------| +| Flag | Description | +|---------------------------------------------------------------------------| +| 0x01 | Sender address fully specified | +| 0x02 | Recipient address fully specified | +| 0x04 | Authentication error response | +|---------------------------------------------------------------------------| + +If flag 0x01 is set, then the sender address will be the full address rather +than a short address identifier. The length of the address can be determined +from the first byte of the address, which always specifies the address type. +Flag 0x02 has the same meaning for the recipient address. + +A peer must send fully specified sender addresses until it receives a response +from the recipient. At this point the sender may assume that the recipient +knows its address and use short a short sender address instead. This +assumption should time out, with a recommended timeout of 60 seconds. + +There is presently no need to send fully specified recipient addresses, but +the flag is present in case it is needed and must be honored. + +Flag 0x04 indicates that this is an error response containing a failed +authentication error. Since authentication failed, this packet may not have +a valid MAC. Packets with this flag must never have any effect other than +to inform of an error. This error, since it is unauthenticated, must never +have any side effects such as terminating a connection. + +3.3. MAC + +The MAC is calculated as follows: + +1) Temporarily set the 64-bit/8-byte MAC field in the packet to the packet's + size as a 64-bit big-endian integer. +2) Calculate the MAC for the entire packet (excluding the first byte) using + the key agreed upon between the sender and the recipient, resulting in a + 16 byte full CMAC-AES MAC. +3) Derive the 8 byte packet MAC by XORing the first 8 bytes of the full 16 + byte CMAC-AES MAC with the last 8 bytes. Place this into the packet's MAC + field. + +3.4. Peer IDs + +Peer IDs provide a method for up to 15 different peers to share an address, +each with a unique ID allowing packets to be routed to them individually. + +A peer ID of zero indicates "any" or "unspecified." Real peers must have a +nonzero peer ID. In the normal single peer per address case, any peer ID may +be used. If multiple peers are to share an address, some implementation- +dependent method must be used to ensure that each peer has a unique peer ID. + +Relaying peers must follow these rules based on the recipient peer ID when +relaying messages: + + - IF the peer ID is zero or if the peer ID is not known, the message must + be forwarded to a random endpoint for the given recipient address. + - IF the peer ID is nonzero and matches one or more known endpoints for the + given recipient address and peer ID, the message must only be sent to + a matching endpoint. + +A receiving peer should process any message that it receives regardless of +whether its recipient peer ID is correct. The peer ID is primarily for relays. + +Peers should typically send messages with a nonzero recipient peer ID when +responding to or involved in a conversation with a specific peer (e.g. a +streaming connection), and send zero recipient peer IDs otherwise. + +3.5. Short Address Conflict Disambiguation + +In the unlikely event of two Anode addresses with the same short identifier, +the recipient should use MAC validation to disambiguate. The peer ID must not +be relied upon for this purpose. + +***************************************************************************** + +4. Basic Signaling and Transport Protocol + +4.1. Message Types + +|---------------------------------------------------------------------------| +| Type | ID | Description | +|---------------------------------------------------------------------------| +| ERROR | 0x00 | Error response | +| PING | 0x01 | Echo request | +| PONG | 0x02 | Echo response | +| EPC_REQ | 0x03 | Endpoint check request | +| EPC | 0x04 | Endpoint check response | +| EPI | 0x05 | Endpoint information | +| NAT_T | 0x06 | NAT traversal message | +| NETID_REQ | 0x07 | Request network address identification and/or test | +| NETID | 0x08 | Response to network address identification request | +| DGRAM | 0x09 | Simple UDP-like datagram | +|---------------------------------------------------------------------------| + +4.2. Message Details + +4.2.1. ERROR + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Error Code | 2 | 16-bit error code | +| Error Arguments | ? | Error arguments, depending on error type | +|---------------------------------------------------------------------------| + +Error arguments are empty unless otherwise stated below. + +Error codes: + +|---------------------------------------------------------------------------| +| Error Code | Description | +|---------------------------------------------------------------------------| +| 0x01 | Message not valid | +| 0x02 | Message authentication or decryption failed | +| 0x03 | Relaying and related features not authorized | +| 0x04 | Relay recipient not reachable | +|---------------------------------------------------------------------------| + +Generation of errors is optional. A peer may choose to ignore invalid +messages or to throttle the sending of errors. + +4.2.2. PING + +(Payload unspecified.) + +Request echo of payload as PONG message. + +4.2.3. PONG + +(Payload unspecified.) + +Echoed payload of received PING message. + +4.2.4. EPC_REQ + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Request ID | 4 | 32-bit request ID | +|---------------------------------------------------------------------------| + +Request echo of request ID in EPC message, used to check and learn endpoints. + +To learn a network endpoint for a peer, CHECK_REQ is sent. If CHECK is +returned with a valid request ID, the endpoint is considered valid. + +4.2.5. EPC + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Request ID | 4 | 32-bit request ID echoed back | +|---------------------------------------------------------------------------| + +Response to EPC_REQ containing request ID. + +4.2.6. EPI + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Flags | 1 | 8-bit flags | +| Endpoint | ? | Endpoint type and address | +| NAT-T mode | 1 | 8-bit NAT traversal mode | +| NAT-T options | ? | Options related to specified NAT-T mode | +|---------------------------------------------------------------------------| + +EPI stands for EndPoint Identification, and is sent to notify another peer of +a network endpoint where the sending peer is reachable. + +If the receiving peer is interested in communicating with the sending peer, +the receiving peer must send EPC_REQ to the sending peer at the specified +endpoint to check the validity of that endpoint. The endpoint is learned if a +valid EPC is returned. + +If the endpoint in EPI is unspecified, the actual source of the EPI message +is the endpoint. This allows EPI messages to be broadcast on a local LAN +segment to advertise the presence of an address on a local network. EPI +broadcasts on local IP networks must be made to UDP port 8737. + +Usually EPI is sent via relays (usually zone relays) to inform a peer of an +endpoint for direct communication. + +There are presently no flags, so flags must be zero. + +4.2.7. NAT_T + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| NAT-T mode | 1 | 8-bit NAT traversal mode | +| NAT-T options | ? | Options related to specified NAT-T mode | +|---------------------------------------------------------------------------| + +NAT_T is used to send messages specific to certain NAT traversal modes. + +4.2.8. NETID_REQ + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Request ID | 4 | 32-bit request ID | +| Endpoint | ? | Endpoint type and address information | +|---------------------------------------------------------------------------| + +When a NETID_REQ message is received, the recipient attempts to echo it back +as a NETID message to the specified endpoint address. If the endpoint is +unspecified, the recipient must fill it in with the actual origin of the +NETID_REQ message. This allows a peer to cooperate with another peer (usually +a zone relay) to empirically determine its externally visible network +address information. + +A peer may ignore NETID_REQ or respond with an error if it does not allow +relaying. + +4.2.9. NETID + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Request ID | 4 | 32-bit request ID echoed back | +| Endpoint Type | 1 | 8-bit endpoint type | +| Endpoint Address | ? | Endpoint Address (size depends on type) | +|---------------------------------------------------------------------------| + +NETID is sent in response to NETID_REQ to the specified endpoint address. It +always contains the endpoint address to which it was sent. + +4.2.10. DGRAM + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Source Port | 2 | 16-bit source port | +| Destination Port | 2 | 16-bit destination port | +| Payload | ? | Datagram packet payload | +|---------------------------------------------------------------------------| + +A datagram is a UDP-like message without flow control or delivery assurance. + +***************************************************************************** + +5. Stream Protocol + +The stream protocol is very similar to TCP, though it omits some features +that are not required since they are taken care of by the encapsulating +protocol. SCTP was also an inspiration in the design. + +5.1. Message Types + +|---------------------------------------------------------------------------| +| Type | ID | Description | +|---------------------------------------------------------------------------| +| S_OPEN | 20 | Initiate a streaming connection (like TCP SYN) | +| S_CLOSE | 21 | Terminate a streaming connection (like TCP RST/FIN) | +| S_DATA | 22 | Data packet | +| S_ACK | 23 | Acknowedge receipt of one or more data packets | +| S_DACK | 24 | Combination of DATA and ACK | +|---------------------------------------------------------------------------| + +5.2. Message Details + +5.2.1. S_OPEN + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Sender Link ID | 2 | 16-bit sender link ID | +| Destination Port | 2 | 16-bit destination port | +| Window Size | 2 | 16-bit window size in 1024-byte increments | +| Init. Seq. Number | 4 | 32-bit initial sequence number | +| Flags | 1 | 8-bit flags | +|---------------------------------------------------------------------------| + +The OPEN message corresponds to TCP SYN, and initiates a connection. It +specifies the initial window size for the sender and the sender's initial +sequence number, which should be randomly chosen to prevent replay attacks. + +If OPEN is successful, the recipient sends its own OPEN to establish the +connetion. If OPEN is unsuccessful, CLOSE is sent with its initial and current +sequence numbers equal and an appropriate reason such as "connection refused." + +The sender link ID must be unique for a given recipient. + +If flag 01 is set, the sender link ID is actually a source port where the +sender might be listening for connections as well. This exactly duplicates +the behavior of standard TCP. Otherwise, the sender link ID is simply an +arbitrary number that the sender uses to identify the connection with this +recipient and there is no port of origin. Ports of origin are optional for +Anode streaming connections to permit greater scalability. + +5.2.2. S_CLOSE + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Sender Link ID | 2 | 16-bit sender link ID | +| Destination Port | 2 | 16-bit destination port | +| Flags | 1 | 8-bit flags | +| Reason | 1 | 8-bit close reason | +| Init. Seq. Number | 4 | 32-bit initial sequence number | +| Sequence Number | 4 | 32-bit current sequence number | +|---------------------------------------------------------------------------| + +The CLOSE message serves a function similar to TCP FIN. The initial sequence +number is the original starting sequence number sent with S_OPEN, while the +current sequence number is the sequence number corresponding to the close +and must be ACKed to complete the close operation. The use of the initial +sequence number helps to serve as a key to prevent replay attacks. + +CLOSE is also used to indicate a failed OPEN attempt. In this case the current +sequence number will be equal to the initial sequence number and no ACK will +be expected. + +There are currently no flags, so flags must be zero. + +The reason field describes the reason for the close: + +|---------------------------------------------------------------------------| +| Reason Code | Description | +|---------------------------------------------------------------------------| +| 00 | Application closed connection | +| 01 | Connection refused | +| 02 | Protocol error | +| 03 | Timed out | +|---------------------------------------------------------------------------| + +Established connections will usually be closed with reason 00, while reason +01 is usually provided if an OPEN is received but the port is not bound. + +5.2.3. S_DATA + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Sender Link ID | 2 | 16-bit sender link ID | +| Destination Port | 2 | 16-bit destination port | +| Sequence Number | 4 | 32-bit sequence number | +| Payload | ? | Data payload | +|---------------------------------------------------------------------------| + +The DATA message carries a packet of data, with the sequence number +determining order. The sequence number is monotonically incremented with +each data packet, and wraps at the maximum value of an unsigned 32-bit +integer. + +5.2.4. S_ACK + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Sender Link ID | 2 | 16-bit sender link ID | +| Destination Port | 2 | 16-bit destination port | +| Window Size | 2 | 16-bit window size in 1024-byte increments | +| Acknowledgements | ? | One or more acknowledgements (see below) | +|---------------------------------------------------------------------------| + +Each acknowledgement is a 32-bit integer followed by an 8-bit integer (5 bytes +total). The 32-bit integer is the first sequence number to acknowledge, and +the 8-bit integer is the number of sequential following sequence numbers to +acknowledge. For example "1, 4" would acknowledge sequence numbers 1, 2, 3, +and 4. + +5.2.5. S_DACK + +|---------------------------------------------------------------------------| +| Field | Length | Description | +|---------------------------------------------------------------------------| +| Sender Link ID | 2 | 16-bit sender link ID | +| Destination Port | 2 | 16-bit destination port | +| Window Size | 2 | 16-bit window size in 1024-byte increments | +| Num. Acks | 1 | 8-bit number of acknowledgements | +| Acknowledgements | ? | One or more acknowledgements | +| Payload | ? | Data payload | +|---------------------------------------------------------------------------| + +The DACK message combines ACK and DATA, allowing two peers that are both +transmitting data to efficiently ACK without a separate packet. diff --git a/attic/historic/anode/libanode/Makefile b/attic/historic/anode/libanode/Makefile new file mode 100644 index 00000000..088587bd --- /dev/null +++ b/attic/historic/anode/libanode/Makefile @@ -0,0 +1,33 @@ +SYSNAME:=${shell uname} +SYSNAME!=uname +include ../config.mk.${SYSNAME} + +LIBANODE_OBJS= \ + impl/aes.o \ + impl/dictionary.o \ + impl/dns_txt.o \ + impl/ec.o \ + impl/environment.o \ + impl/misc.o \ + impl/thread.o \ + address.o \ + aes_digest.o \ + errors.o \ + identity.o \ + network_address.o \ + secure_random.o \ + system_transport.o \ + uri.o +# zone.o + +all: $(LIBANODE_OBJS) + ar rcs libanode.a $(LIBANODE_OBJS) + ranlib libanode.a + $(CC) $(CFLAGS) -o utils/anode-make-identity utils/anode-make-identity.c $(LIBANODE_OBJS) $(LIBANODE_LIBS) + +clean: force + rm -f $(LIBANODE_OBJS) + rm -f libanode.$(DLLEXT) libanode.a + rm -f utils/anode-make-identity + +force: ; diff --git a/attic/historic/anode/libanode/address.c b/attic/historic/anode/libanode/address.c new file mode 100644 index 00000000..e7ab7837 --- /dev/null +++ b/attic/historic/anode/libanode/address.c @@ -0,0 +1,98 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include "impl/aes.h" +#include "impl/ec.h" +#include "impl/misc.h" +#include "impl/types.h" +#include "anode.h" + +int AnodeAddress_calc_short_id( + const AnodeAddress *address, + AnodeAddressId *short_address_id) +{ + unsigned char digest[16]; + + switch(AnodeAddress_get_type(address)) { + case ANODE_ADDRESS_ANODE_256_40: + Anode_aes_digest(address->bits,ANODE_ADDRESS_LENGTH_ANODE_256_40,digest); + break; + default: + return ANODE_ERR_ADDRESS_INVALID; + } + + *((uint64_t *)short_address_id->bits) = ((uint64_t *)digest)[0] ^ ((uint64_t *)digest)[1]; + + return 0; +} + +int AnodeAddress_get_zone(const AnodeAddress *address,AnodeZone *zone) +{ + switch(AnodeAddress_get_type(address)) { + case ANODE_ADDRESS_ANODE_256_40: + *((uint32_t *)&(zone->bits[0])) = *((uint32_t *)&(address->bits[1])); + return 0; + } + return ANODE_ERR_ADDRESS_INVALID; +} + +int AnodeAddress_to_string(const AnodeAddress *address,char *buf,int len) +{ + const unsigned char *inptr; + char *outptr; + unsigned int i; + + switch(AnodeAddress_get_type(address)) { + case ANODE_ADDRESS_ANODE_256_40: + if (len < (((ANODE_ADDRESS_LENGTH_ANODE_256_40 / 5) * 8) + 1)) + return ANODE_ERR_BUFFER_TOO_SMALL; + inptr = (const unsigned char *)address->bits; + outptr = buf; + for(i=0;i<(ANODE_ADDRESS_LENGTH_ANODE_256_40 / 5);++i) { + Anode_base32_5_to_8(inptr,outptr); + inptr += 5; + outptr += 8; + } + *outptr = (char)0; + return ((ANODE_ADDRESS_LENGTH_ANODE_256_40 / 5) * 8); + } + return ANODE_ERR_ADDRESS_INVALID; +} + +int AnodeAddress_from_string(const char *str,AnodeAddress *address) +{ + const char *blk_start = str; + const char *ptr = str; + unsigned int address_len = 0; + + while (*ptr) { + if ((unsigned long)(ptr - blk_start) == 8) { + if ((address_len + 5) > sizeof(address->bits)) + return ANODE_ERR_ADDRESS_INVALID; + Anode_base32_8_to_5(blk_start,(unsigned char *)&(address->bits[address_len])); + address_len += 5; + blk_start = ptr; + } + ++ptr; + } + + if (ptr != blk_start) + return ANODE_ERR_ADDRESS_INVALID; + if (AnodeAddress_get_type(address) != ANODE_ADDRESS_ANODE_256_40) + return ANODE_ERR_ADDRESS_INVALID; + + return 0; +} diff --git a/attic/historic/anode/libanode/aes_digest.c b/attic/historic/anode/libanode/aes_digest.c new file mode 100644 index 00000000..07b0fc7a --- /dev/null +++ b/attic/historic/anode/libanode/aes_digest.c @@ -0,0 +1,85 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include "anode.h" +#include "impl/aes.h" +#include "impl/misc.h" +#include "impl/types.h" + +void Anode_aes_digest(const void *const message,unsigned long message_len,void *const hash) +{ + unsigned char previous_digest[16]; + unsigned char digest[16]; + unsigned char block[32]; + const unsigned char *in = (const unsigned char *)message; + const unsigned char *end = in + message_len; + unsigned long block_counter; + AnodeAesExpandedKey expkey; + + ((uint64_t *)digest)[0] = 0ULL; + ((uint64_t *)digest)[1] = 0ULL; + ((uint64_t *)block)[0] = 0ULL; + ((uint64_t *)block)[1] = 0ULL; + ((uint64_t *)block)[2] = 0ULL; + ((uint64_t *)block)[3] = 0ULL; + + /* Davis-Meyer hash function built from block cipher */ + block_counter = 0; + while (in != end) { + block[block_counter++] = *(in++); + if (block_counter == 32) { + block_counter = 0; + ((uint64_t *)previous_digest)[0] = ((uint64_t *)digest)[0]; + ((uint64_t *)previous_digest)[1] = ((uint64_t *)digest)[1]; + Anode_aes256_expand_key(block,&expkey); + Anode_aes256_encrypt(&expkey,digest,digest); + ((uint64_t *)digest)[0] ^= ((uint64_t *)previous_digest)[0]; + ((uint64_t *)digest)[1] ^= ((uint64_t *)previous_digest)[1]; + } + } + + /* Davis-Meyer end marker */ + block[block_counter++] = 0x80; + while (block_counter != 32) block[block_counter++] = 0; + ((uint64_t *)previous_digest)[0] = ((uint64_t *)digest)[0]; + ((uint64_t *)previous_digest)[1] = ((uint64_t *)digest)[1]; + Anode_aes256_expand_key(block,&expkey); + Anode_aes256_encrypt(&expkey,digest,digest); + ((uint64_t *)digest)[0] ^= ((uint64_t *)previous_digest)[0]; + ((uint64_t *)digest)[1] ^= ((uint64_t *)previous_digest)[1]; + + /* Merkle-Damgård length padding */ + ((uint64_t *)block)[0] = 0ULL; + if (sizeof(message_len) >= 8) { /* 32/64 bit? this will get optimized out */ + block[8] = (uint8_t)((uint64_t)message_len >> 56); + block[9] = (uint8_t)((uint64_t)message_len >> 48); + block[10] = (uint8_t)((uint64_t)message_len >> 40); + block[11] = (uint8_t)((uint64_t)message_len >> 32); + } else ((uint32_t *)block)[2] = 0; + block[12] = (uint8_t)(message_len >> 24); + block[13] = (uint8_t)(message_len >> 16); + block[14] = (uint8_t)(message_len >> 8); + block[15] = (uint8_t)message_len; + ((uint64_t *)previous_digest)[0] = ((uint64_t *)digest)[0]; + ((uint64_t *)previous_digest)[1] = ((uint64_t *)digest)[1]; + Anode_aes256_expand_key(block,&expkey); + Anode_aes256_encrypt(&expkey,digest,digest); + ((uint64_t *)digest)[0] ^= ((uint64_t *)previous_digest)[0]; + ((uint64_t *)digest)[1] ^= ((uint64_t *)previous_digest)[1]; + + ((uint64_t *)hash)[0] = ((uint64_t *)digest)[0]; + ((uint64_t *)hash)[1] = ((uint64_t *)digest)[1]; +} diff --git a/attic/historic/anode/libanode/anode.h b/attic/historic/anode/libanode/anode.h new file mode 100644 index 00000000..e0c51e2e --- /dev/null +++ b/attic/historic/anode/libanode/anode.h @@ -0,0 +1,795 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 _ANODE_ANODE_H +#define _ANODE_ANODE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef NULL +#define NULL ((void *)0) +#endif + +#define ANODE_ADDRESS_LENGTH_ANODE_256_40 40 +#define ANODE_ADDRESS_MAX_LENGTH 40 +#define ANODE_ADDRESS_SECRET_LENGTH_ANODE_256_40 32 +#define ANODE_ADDRESS_MAX_SECRET_LENGTH 32 + +#define ANODE_ADDRESS_ID_LENGTH 8 +#define ANODE_ZONE_LENGTH 4 + +#define ANODE_ERR_NONE 0 +#define ANODE_ERR_INVALID_ARGUMENT (-10000) +#define ANODE_ERR_OUT_OF_MEMORY (-10001) +#define ANODE_ERR_INVALID_URI (-10002) +#define ANODE_ERR_BUFFER_TOO_SMALL (-10003) +#define ANODE_ERR_ADDRESS_INVALID (-10010) +#define ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED (-10011) +#define ANODE_ERR_CONNECTION_CLOSED (-10012) +#define ANODE_ERR_CONNECTION_CLOSED_BY_REMOTE (-10013) +#define ANODE_ERR_CONNECT_FAILED (-10014) +#define ANODE_ERR_UNABLE_TO_BIND (-10015) +#define ANODE_ERR_TOO_MANY_OPEN_SOCKETS (-10016) +#define ANODE_ERR_DNS_NAME_NOT_FOUND_OR_TIMED_OUT (-10017) + +/** + * Get a human-readable error description for an error code + * + * The value of 'err' can be either negative or positive. + * + * @param err Error code + * @return Human-readable description + */ +extern const char *Anode_strerror(int err); + +/* ----------------------------------------------------------------------- */ +/* Secure random source */ +/* ----------------------------------------------------------------------- */ + +/** + * Opaque secure random instance + */ +typedef void AnodeSecureRandom; + +/** + * Initialize a secure random source + * + * No cleanup/destructor is necessary. + * + * @param srng Random structure to initialize + */ +extern AnodeSecureRandom *AnodeSecureRandom_new(); + +/** + * Generate random bytes + * + * @param srng Secure random source + * @param buf Buffer to fill + * @param count Number of bytes to generate + */ +extern void AnodeSecureRandom_gen_bytes(AnodeSecureRandom *srng,void *buf,long count); + +/** + * Destroy and free a secure random instance + * + * @param srng Secure random source + */ +extern void AnodeSecureRandom_delete(AnodeSecureRandom *srng); + +/* ----------------------------------------------------------------------- */ +/* AES-256 derived Davis-Meyer hash function */ +/* ----------------------------------------------------------------------- */ + +/** + * Digest a message using AES-DIGEST to yield a 16-byte hash code + * + * @param message Message to digest + * @param message_len Length of message in bytes + * @param hash Buffer to store 16 byte hash code + */ +extern void Anode_aes_digest( + const void *const message, + unsigned long message_len, + void *const hash); + +/* ----------------------------------------------------------------------- */ +/* Address Types and Components */ +/* ----------------------------------------------------------------------- */ + +/** + * Anode address + * + * The first byte always identifies the address type, which right now can + * only be type 1 (ANODE-256-40). + */ +typedef struct +{ + char bits[ANODE_ADDRESS_MAX_LENGTH]; +} AnodeAddress; + +/** + * 8-byte short Anode address ID + */ +typedef struct +{ + char bits[ANODE_ADDRESS_ID_LENGTH]; +} AnodeAddressId; + +/** + * 4-byte Anode zone ID + */ +typedef struct +{ + char bits[ANODE_ZONE_LENGTH]; +} AnodeZone; + +/** + * Anode address types + */ +enum AnodeAddressType +{ + ANODE_ADDRESS_ANODE_256_40 = 1 +}; + +/** + * Get the type of an Anode address + * + * This is a shortcut macro for just looking at the first byte and casting + * it to the AnodeAddressType enum. + * + * @param a Pointer to address + * @return Type as enum AnodeAddressType + */ +#define AnodeAddress_get_type(a) ((enum AnodeAddressType)((a)->bits[0])) + +/** + * Calculate the short 8 byte address ID from an address + * + * @param address Binary address + * @param short_address_id Buffer to store 8-byte short address ID + * @return 0 on success or error code on failure + */ +extern int AnodeAddress_calc_short_id( + const AnodeAddress *address, + AnodeAddressId *short_address_id); + +/** + * Extract the zone from an anode address + * + * @param address Binary address + * @param zone Zone value-result parameter to fill on success + * @return 0 on success or error code on failure + */ +extern int AnodeAddress_get_zone(const AnodeAddress *address,AnodeZone *zone); + +/** + * Convert an address to an ASCII string + * + * Anode addresses are 64 characters in ASCII form, so the buffer should + * have 65 bytes of space. + * + * @param address Address to convert + * @param buf Buffer to receive address in string form (should have 65 bytes of space) + * @param len Length of buffer + * @return Length of resulting string or a negative error code on error + */ +extern int AnodeAddress_to_string(const AnodeAddress *address,char *buf,int len); + +/** + * Convert a string into an address + * + * @param str Address in string form + * @param address Address buffer to receive result + * @return Zero on sucess or error code on error + */ +extern int AnodeAddress_from_string(const char *str,AnodeAddress *address); + +/** + * Supported network address types + */ +enum AnodeNetworkAddressType +{ + ANODE_NETWORK_ADDRESS_IPV4 = 0, + ANODE_NETWORK_ADDRESS_IPV6 = 1, + ANODE_NETWORK_ADDRESS_ETHERNET = 2, /* reserved but unused */ + ANODE_NETWORK_ADDRESS_USB = 3, /* reserved but unused */ + ANODE_NETWORK_ADDRESS_BLUETOOTH = 4, /* reserved but unused */ + ANODE_NETWORK_ADDRESS_IPC = 5, /* reserved but unused */ + ANODE_NETWORK_ADDRESS_80211S = 6, /* reserved but unused */ + ANODE_NETWORK_ADDRESS_SERIAL = 7, /* reserved but unused */ + ANODE_NETWORK_ADDRESS_ANODE_256_40 = 8 +}; + +/** + * Anode network address + * + * This can contain an address of any type: IPv4, IPv6, or Anode, and is used + * with the common transport API. + * + * The length of the address stored in bits[] is determined by the type. + */ +typedef struct +{ + enum AnodeNetworkAddressType type; + char bits[ANODE_ADDRESS_MAX_LENGTH]; +} AnodeNetworkAddress; + +/** + * An endpoint with an address and a port + */ +typedef struct +{ + AnodeNetworkAddress address; + int port; +} AnodeNetworkEndpoint; + +/* Constants for binding to any address (v4 or v6) */ +extern const AnodeNetworkAddress AnodeNetworkAddress_IP_ANY_V4; +extern const AnodeNetworkAddress AnodeNetworkAddress_IP_ANY_V6; + +/* Local host address in v4 and v6 */ +extern const AnodeNetworkAddress AnodeNetworkAddress_IP_LOCAL_V4; +extern const AnodeNetworkAddress AnodeNetworkAddress_IP_LOCAL_V6; + +/** + * Convert a network address to an ASCII string + * + * The buffer must have room for a 15 character string for IPv4, a 40 byte + * string for IPv6, and a 64 byte string for Anode addresses. This does not + * include the trailing null. + * + * @param address Address to convert + * @param buf Buffer to receive address in string form + * @param len Length of buffer + * @return Length of resulting string or a negative error code on error + */ +extern int AnodeNetworkAddress_to_string(const AnodeNetworkAddress *address,char *buf,int len); + +/** + * Convert a string into a network address of the correct type + * + * @param str Address in string form + * @param address Address buffer to receive result + * @return Zero on sucess or error code on error + */ +extern int AnodeNetworkAddress_from_string(const char *str,AnodeNetworkAddress *address); + +/** + * Fill a network endpoint from a C-API sockaddr structure + * + * The argument must be struct sockaddr_in for IPv4 or sockaddr_in6 for IPv6. + * The common sin_family field will be used to differentiate. + * + * @param sockaddr Pointer to proper sockaddr structure + * @param endpoint Endpoint structure to fill + * @return Zero on success or error on failure + */ +extern int AnodeNetworkEndpoint_from_sockaddr(const void *sockaddr,AnodeNetworkEndpoint *endpoint); + +/** + * Fill a sockaddr from a network endpoint + * + * To support either IPv4 or IPv6 addresses, there is a sockaddr_storage + * structure in most C APIs. If you supply anything other than an IP address + * such as an Anode address, this will return an error. + * + * @param endpoint Endpoint structure to convert + * @param sockaddr Sockaddr structure storage + * @param sockaddr_len Length of sockaddr structure storage in bytes + * @return Zero on success or error on failure + */ +extern int AnodeNetworkEndpoint_to_sockaddr(const AnodeNetworkEndpoint *endpoint,void *sockaddr,int sockaddr_len); + +/* ----------------------------------------------------------------------- */ +/* Identity Generation and Management */ +/* ----------------------------------------------------------------------- */ + +/** + * Anode identity structure containing address and secret key + * + * This structure is memcpy-safe, and its members are accessible. + */ +typedef struct +{ + /* The public Anode address */ + AnodeAddress address; + + /* Short address ID */ + AnodeAddressId address_id; + + /* The secret key corresponding with the public address */ + /* Secret length is determined by address type */ + char secret[ANODE_ADDRESS_MAX_SECRET_LENGTH]; +} AnodeIdentity; + +/** + * Generate a new identity + * + * This generates a public/private key pair and from that generates an + * identity containing an address and a secret key. + * + * @param identity Destination structure to store new identity + * @param zone Zone ID + * @param type Type of identity to generate + * @return Zero on success, error on failure + */ +extern int AnodeIdentity_generate( + AnodeIdentity *identity, + const AnodeZone *zone, + enum AnodeAddressType type); + +/** + * Convert an Anode identity to a string representation + * + * @param identity Identity to convert + * @param dest String buffer + * @param dest_len Length of string buffer + * @return Length of string created or negative error code on failure + */ +extern int AnodeIdentity_to_string( + const AnodeIdentity *identity, + char *dest, + int dest_len); + +/** + * Convert a string representation to an Anode identity structure + * + * @param identity Destination structure to fill + * @param str C-string containing string representation + * @return Zero on success or negative error code on failure + */ +extern int AnodeIdentity_from_string( + AnodeIdentity *identity, + const char *str); + +/* ----------------------------------------------------------------------- */ +/* Transport API */ +/* ----------------------------------------------------------------------- */ + +struct _AnodeTransport; +typedef struct _AnodeTransport AnodeTransport; +struct _AnodeEvent; +typedef struct _AnodeEvent AnodeEvent; + +/** + * Anode socket + */ +typedef struct +{ + /* Type of socket (read-only) */ + enum { + ANODE_SOCKET_DATAGRAM = 1, + ANODE_SOCKET_STREAM_LISTEN = 2, + ANODE_SOCKET_STREAM_CONNECTION = 3 + } type; + + /* Socket state */ + enum { + ANODE_SOCKET_CLOSED = 0, + ANODE_SOCKET_OPEN = 1, + ANODE_SOCKET_CONNECTING = 2, + } state; + + /* Local address or remote address for stream connections (read-only) */ + AnodeNetworkEndpoint endpoint; + + /* Name of owning class (read-only) */ + const char *class_name; + + /* Pointers for end user use (writable) */ + void *user_ptr[2]; + + /* Special handler to receive events or null for default (writable) */ + void (*event_handler)(const AnodeEvent *event); +} AnodeSocket; + +/** + * Anode transport I/O event + */ +struct _AnodeEvent +{ + enum { + ANODE_TRANSPORT_EVENT_DATAGRAM_RECEIVED = 1, + ANODE_TRANSPORT_EVENT_STREAM_INCOMING_CONNECT = 2, + ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_ESTABLISHED = 3, + ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_FAILED = 4, + ANODE_TRANSPORT_EVENT_STREAM_CLOSED = 5, + ANODE_TRANSPORT_EVENT_STREAM_DATA_RECEIVED = 6, + ANODE_TRANSPORT_EVENT_STREAM_AVAILABLE_FOR_WRITE = 7, + ANODE_TRANSPORT_EVENT_DNS_RESULT = 8 + } type; + + AnodeTransport *transport; + + /* Anode socket corresponding to this event */ + AnodeSocket *sock; + + /* Originating endpoint for incoming datagrams */ + AnodeNetworkEndpoint *datagram_from; + + /* DNS lookup results */ + const char *dns_name; + AnodeNetworkAddress *dns_addresses; + int dns_address_count; + + /* Error code or 0 for none */ + int error_code; + + /* Data for incoming datagrams and stream received events */ + int data_length; + char *data; +}; + +/** + * Enum used for dns_resolve method in transport to specify query rules + * + * This can be specified for ipv4, ipv6, and Anode address types to tell the + * DNS resolver when to bother querying for addresses of the given type. + * NEVER means to never query for this type, and ALWAYS means to always + * query. IF_NO_PREVIOUS means to query for this type if no addresses were + * found in previous queries. Addresses are queried in the order of ipv4, + * ipv6, then Anode, so if you specify IF_NO_PREVIOUS for all three you will + * get addresses in that order of priority. + */ +enum AnodeTransportDnsIncludeMode +{ + ANODE_TRANSPORT_DNS_QUERY_NEVER = 0, + ANODE_TRANSPORT_DNS_QUERY_ALWAYS = 1, + ANODE_TRANSPORT_DNS_QUERY_IF_NO_PREVIOUS = 2 +}; + +struct _AnodeTransport +{ + /** + * Set the default event handler + * + * @param transport Transport engine + * @param event_handler Default event handler + */ + void (*set_default_event_handler)(AnodeTransport *transport, + void (*event_handler)(const AnodeEvent *event)); + + /** + * Enqueue a function to be executed during a subsequent call to poll() + * + * This can be called from other threads, so it can be used to pass a + * message to the I/O thread in multithreaded applications. + * + * If it is called from the same thread, the function is still queued to be + * run later rather than being run instantly. + * + * The order in which invoked functions are called is undefined. + * + * @param transport Transport engine + * @param ptr Arbitrary pointer to pass to function to be called + * @param func Function to be called + */ + void (*invoke)(AnodeTransport *transport, + void *ptr, + void (*func)(void *)); + + /** + * Initiate a forward DNS query + * + * @param transport Transport instance + * @param name DNS name to query + * @param event_handler Event handler or null for default event path + * @param ipv4_include_mode Inclusion mode for IPv4 addresses + * @param ipv6_include_mode Inclusion mode for IPv6 addresses + * @param anode_include_mode Inclusion mode for Anode addresses + */ + void (*dns_resolve)(AnodeTransport *transport, + const char *name, + void (*event_handler)(const AnodeEvent *), + enum AnodeTransportDnsIncludeMode ipv4_include_mode, + enum AnodeTransportDnsIncludeMode ipv6_include_mode, + enum AnodeTransportDnsIncludeMode anode_include_mode); + + /** + * Open a datagram socket + * + * @param transport Transport instance + * @param local_address Local address to bind + * @param local_port Local port to bind + * @param error_code Value-result parameter to receive error code on error + * @return Listen socket or null if error (check error_code in error case) + */ + AnodeSocket *(*datagram_listen)(AnodeTransport *transport, + const AnodeNetworkAddress *local_address, + int local_port, + int *error_code); + + /** + * Open a socket to listen for incoming stream connections + * + * @param transport Transport instance + * @param local_address Local address to bind + * @param local_port Local port to bind + * @param error_code Value-result parameter to receive error code on error + * @return Listen socket or null if error (check error_code in error case) + */ + AnodeSocket *(*stream_listen)(AnodeTransport *transport, + const AnodeNetworkAddress *local_address, + int local_port, + int *error_code); + + /** + * Send a datagram to a network endpoint + * + * @param transport Transport instance + * @param socket Originating datagram socket + * @param data Data to send + * @param data_len Length of data to send + * @param to_endpoint Destination endpoint + * @return Zero on success or error code on error + */ + int (*datagram_send)(AnodeTransport *transport, + AnodeSocket *sock, + const void *data, + int data_len, + const AnodeNetworkEndpoint *to_endpoint); + + /** + * Initiate an outgoing stream connection attempt + * + * For IPv4 and IPv6 addresses, this will initiate a TCP connection. For + * Anode addresses, Anode's internal streaming protocol will be used. + * + * @param transport Transport instance + * @param to_endpoint Destination endpoint + * @param error_code Error code value-result parameter, filled on error + * @return Stream socket object or null on error (check error_code) + */ + AnodeSocket *(*stream_connect)(AnodeTransport *transport, + const AnodeNetworkEndpoint *to_endpoint, + int *error_code); + + /** + * Indicate that you are interested in writing to a stream + * + * This does nothing if the socket is not a stream connection or is not + * connected. + * + * @param transport Transport instance + * @param sock Stream connection + */ + void (*stream_start_writing)(AnodeTransport *transport, + AnodeSocket *sock); + + /** + * Indicate that you are no longer interested in writing to a stream + * + * This does nothing if the socket is not a stream connection or is not + * connected. + * + * @param transport Transport instance + * @param sock Stream connection + */ + void (*stream_stop_writing)(AnodeTransport *transport, + AnodeSocket *sock); + + /** + * Send data to a stream connection + * + * This must be called after a stream is indicated to be ready for writing. + * It returns the number of bytes actually written, or a negative error + * code on failure. + * + * A return value of zero can occur here, and simply indicates that nothing + * was sent. This may occur with certain network stacks on certain + * platforms. + * + * @param transport Transport engine + * @param sock Stream socket + * @param data Data to send + * @param data_len Maximum data to send in bytes + * @return Actual data sent or negative error code on error + */ + int (*stream_send)(AnodeTransport *transport, + AnodeSocket *sock, + const void *data, + int data_len); + + /** + * Close a socket + * + * If the socket is a stream connection in the connected state, this + * will generate a stream closed event with a zero error_code to indicate + * a normal close. + * + * @param transport Transport engine + * @param sock Socket object + */ + void (*close)(AnodeTransport *transport, + AnodeSocket *sock); + + /** + * Run main polling loop + * + * This should be called repeatedly from the I/O thread of your main + * process. It blocks until one or more events occur, and then returns + * the number of events. Error returns here are fatal and indicate + * serious problems such as build or platform issues or a lack of any + * network interface. + * + * Functions queued with invoke() are also called inside here. + * + * @param transport Transport engine + * @return Number of events handled or negative on (fatal) error + */ + int (*poll)(AnodeTransport *transport); + + /** + * Check whether transport supports an address type + * + * Inheriting classes should call their base if they do not natively + * speak the specified type. + * + * @param transport Transport engine + * @param at Address type + * @return Nonzero if true + */ + int (*supports_address_type)(const AnodeTransport *transport, + enum AnodeNetworkAddressType at); + + /** + * Get the instance of AnodeTransport under this one (if any) + * + * @param transport Transport engine + * @return Base instance or null if none + */ + AnodeTransport *(*base_instance)(const AnodeTransport *transport); + + /** + * @param transport Transport engine + * @return Class name of this instance + */ + const char *(*class_name)(AnodeTransport *transport); + + /** + * Delete this transport and its base transports + * + * The 'transport' pointer and any streams or sockets it owns are no longer + * valid after this call. + * + * @param transport Transport engine + */ + void (*delete)(AnodeTransport *transport); +}; + +/** + * Construct a new system transport + * + * This is the default base for AnodeTransport, and it is constructed + * automatically if 'base' is null in AnodeTransport_new(). However, it also + * exposed to the user so that specialized transports (such as those that use + * proxy servers) can be developed on top of it. These in turn can be supplied + * as 'base' to AnodeTransport_new() to talk Anode over these transports. + * + * The system transport supports IP protocols and possibly others. + * + * @param base Base class or null for none (usually null) + * @return Base transport engine instance + */ +extern AnodeTransport *AnodeSystemTransport_new(AnodeTransport *base); + +/** + * Construct a new Anode core transport + * + * This is the transport that talks Anode using the specified base transport. + * Requests for other address types are passed through to the base. If the + * base is null, an instance of AnodeSystemTransport is used. + * + * Since transport engines inherit their functionality, this transport + * will also do standard IP and everything else that the system transport + * supports. Most users will just want to construct this with a null base. + * + * @param base Base transport to use, or null to use SystemTransport + * @return Anode transport engine or null on error + */ +extern AnodeTransport *AnodeCoreTransport_new(AnodeTransport *base); + +/* ----------------------------------------------------------------------- */ +/* URI Parser */ +/* ----------------------------------------------------------------------- */ + +/** + * URI broken down by component + */ +typedef struct +{ + char scheme[8]; + char username[64]; + char password[64]; + char host[128]; + char path[256]; + char query[256]; + char fragment[64]; + int port; +} AnodeURI; + +/** + * URI parser + * + * A buffer too small error will occur if any field is too large for the + * AnodeURI structure. + * + * @param parsed_uri Structure to fill with parsed URI data + * @param uri_string URI in string format + * @return Zero on success or error on failure + */ +extern int AnodeURI_parse(AnodeURI *parsed_uri,const char *uri_string); + +/** + * Output a URI in string format + * + * @param uri URI to output as string + * @param buf Buffer to store URI string + * @param len Length of buffer + * @return Buffer or null on error + */ +extern char *AnodeURI_to_string(const AnodeURI *uri,char *buf,int len); + +/* ----------------------------------------------------------------------- */ +/* Zone File Lookup and Dictionary */ +/* ----------------------------------------------------------------------- */ + +/** + * Zone file dictionary + */ +typedef void AnodeZoneFile; + +/** + * Start asynchronous zone fetch + * + * When the zone is retrieved, the lookup handler is called. If zone lookup + * failed, the zone file argument to the handler will be null. + * + * @param transport Transport engine + * @param zone Zone ID + * @param user_ptr User pointer + * @param zone_lookup_handler Handler for Anode zone lookup + */ +extern void AnodeZoneFile_lookup( + AnodeTransport *transport, + const AnodeZone *zone, + void *ptr, + void (*zone_lookup_handler)(const AnodeZone *,AnodeZoneFile *,void *)); + +/** + * Look up a key in a zone file + * + * @param zone Zone file object + * @param key Key to get in zone file + */ +extern const char *AnodeZoneFile_get(const AnodeZoneFile *zone,const char *key); + +/** + * Free a zone file + * + * @param zone Zone to free + */ +extern void AnodeZoneFile_free(AnodeZoneFile *zone); + +/* ----------------------------------------------------------------------- */ + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/attic/historic/anode/libanode/errors.c b/attic/historic/anode/libanode/errors.c new file mode 100644 index 00000000..6836bdc4 --- /dev/null +++ b/attic/historic/anode/libanode/errors.c @@ -0,0 +1,52 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include "anode.h" + +struct AnodeErrDesc +{ + int code; + const char *desc; +}; + +#define TOTAL_ERRORS 12 +static const struct AnodeErrDesc ANODE_ERRORS[TOTAL_ERRORS] = { + { ANODE_ERR_NONE, "No error (success)" }, + { ANODE_ERR_INVALID_ARGUMENT, "Invalid argument" }, + { ANODE_ERR_OUT_OF_MEMORY, "Out of memory" }, + { ANODE_ERR_INVALID_URI, "Invalid URI" }, + { ANODE_ERR_BUFFER_TOO_SMALL, "Supplied buffer too small" }, + { ANODE_ERR_ADDRESS_INVALID, "Address invalid" }, + { ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED, "Address type not supported"}, + { ANODE_ERR_CONNECTION_CLOSED, "Connection closed"}, + { ANODE_ERR_CONNECT_FAILED, "Connect failed"}, + { ANODE_ERR_UNABLE_TO_BIND, "Unable to bind to address"}, + { ANODE_ERR_TOO_MANY_OPEN_SOCKETS, "Too many open sockets"}, + { ANODE_ERR_DNS_NAME_NOT_FOUND_OR_TIMED_OUT, "DNS name not found or timed out"} +}; + +extern const char *Anode_strerror(int err) +{ + int i; + int negerr = -err; + + for(i=0;i + * + * 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 . */ + +#include +#include +#include "impl/types.h" +#include "impl/ec.h" +#include "impl/misc.h" +#include "anode.h" + +int AnodeIdentity_generate(AnodeIdentity *identity,const AnodeZone *zone,enum AnodeAddressType type) +{ + struct AnodeECKeyPair kp; + + switch(type) { + case ANODE_ADDRESS_ANODE_256_40: + if (!AnodeECKeyPair_generate(&kp)) + return ANODE_ERR_OUT_OF_MEMORY; + + identity->address.bits[0] = (unsigned char)ANODE_ADDRESS_ANODE_256_40; + + identity->address.bits[1] = zone->bits[0]; + identity->address.bits[2] = zone->bits[1]; + identity->address.bits[3] = zone->bits[2]; + identity->address.bits[4] = zone->bits[3]; + + identity->address.bits[5] = 0; + identity->address.bits[6] = 0; + + Anode_memcpy((void *)&(identity->address.bits[7]),(const void *)kp.pub.key,ANODE_EC_PUBLIC_KEY_BYTES); + Anode_memcpy((void *)identity->secret,(const void *)kp.priv.key,kp.priv.bytes); + + AnodeAddress_calc_short_id(&identity->address,&identity->address_id); + + AnodeECKeyPair_destroy(&kp); + + return 0; + } + + return ANODE_ERR_INVALID_ARGUMENT; +} + +int AnodeIdentity_to_string(const AnodeIdentity *identity,char *dest,int dest_len) +{ + char hexbuf[128]; + char strbuf[128]; + int n; + + if ((n = AnodeAddress_to_string(&identity->address,strbuf,sizeof(strbuf))) <= 0) + return n; + + switch(AnodeAddress_get_type(&identity->address)) { + case ANODE_ADDRESS_ANODE_256_40: + Anode_to_hex((const unsigned char *)identity->secret,ANODE_ADDRESS_SECRET_LENGTH_ANODE_256_40,hexbuf,sizeof(hexbuf)); + n = snprintf(dest,dest_len,"ANODE-256-40:%s:%s",strbuf,hexbuf); + if (n >= dest_len) + return ANODE_ERR_BUFFER_TOO_SMALL; + return n; + } + + return ANODE_ERR_INVALID_ARGUMENT; +} + +int AnodeIdentity_from_string(AnodeIdentity *identity,const char *str) +{ + char buf[1024]; + char *id_name; + char *address; + char *secret; + int ec; + + Anode_str_copy(buf,str,sizeof(buf)); + + id_name = buf; + if (!id_name) return 0; + if (!*id_name) return 0; + address = (char *)Anode_strchr(id_name,':'); + if (!address) return 0; + if (!*address) return 0; + *(address++) = (char)0; + secret = (char *)Anode_strchr(address,':'); + if (!secret) return 0; + if (!*secret) return 0; + *(secret++) = (char)0; + + if (Anode_strcaseeq("ANODE-256-40",id_name)) { + if ((ec = AnodeAddress_from_string(address,&identity->address))) + return ec; + if (Anode_strlen(secret) != (ANODE_ADDRESS_SECRET_LENGTH_ANODE_256_40 * 2)) + return ANODE_ERR_INVALID_ARGUMENT; + Anode_from_hex(secret,(unsigned char *)identity->secret,sizeof(identity->secret)); + AnodeAddress_calc_short_id(&identity->address,&identity->address_id); + return 0; + } + + return ANODE_ERR_INVALID_ARGUMENT; +} diff --git a/attic/historic/anode/libanode/impl/aes.c b/attic/historic/anode/libanode/impl/aes.c new file mode 100644 index 00000000..90e5e23b --- /dev/null +++ b/attic/historic/anode/libanode/impl/aes.c @@ -0,0 +1,72 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include "aes.h" + +void Anode_cmac_aes256( + const AnodeAesExpandedKey *expkey, + const unsigned char *restrict data, + unsigned long data_len, + unsigned char *restrict mac) +{ + unsigned char cbc[16]; + unsigned char pad[16]; + const unsigned char *restrict pos = data; + unsigned long i; + unsigned long remaining = data_len; + unsigned char c; + + ((uint64_t *)((void *)cbc))[0] = 0ULL; + ((uint64_t *)((void *)cbc))[1] = 0ULL; + + while (remaining >= 16) { + ((uint64_t *)((void *)cbc))[0] ^= ((uint64_t *)((void *)pos))[0]; + ((uint64_t *)((void *)cbc))[1] ^= ((uint64_t *)((void *)pos))[1]; + pos += 16; + if (remaining > 16) + Anode_aes256_encrypt(expkey,cbc,cbc); + remaining -= 16; + } + + ((uint64_t *)((void *)pad))[0] = 0ULL; + ((uint64_t *)((void *)pad))[1] = 0ULL; + Anode_aes256_encrypt(expkey,pad,pad); + + c = pad[0] & 0x80; + for(i=0;i<15;++i) + pad[i] = (pad[i] << 1) | (pad[i + 1] >> 7); + pad[15] <<= 1; + if (c) + pad[15] ^= 0x87; + + if (remaining||(!data_len)) { + for(i=0;i> 7); + pad[15] <<= 1; + if (c) + pad[15] ^= 0x87; + } + + ((uint64_t *)((void *)mac))[0] = ((uint64_t *)((void *)pad))[0] ^ ((uint64_t *)((void *)cbc))[0]; + ((uint64_t *)((void *)mac))[1] = ((uint64_t *)((void *)pad))[1] ^ ((uint64_t *)((void *)cbc))[1]; + + Anode_aes256_encrypt(expkey,mac,mac); +} diff --git a/attic/historic/anode/libanode/impl/aes.h b/attic/historic/anode/libanode/impl/aes.h new file mode 100644 index 00000000..25e33933 --- /dev/null +++ b/attic/historic/anode/libanode/impl/aes.h @@ -0,0 +1,64 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 _ANODE_AES_H +#define _ANODE_AES_H + +#include +#include "types.h" + +/* This just glues us to OpenSSL's built-in AES-256 implementation */ + +#define ANODE_AES_BLOCK_SIZE 16 +#define ANODE_AES_KEY_SIZE 32 + +typedef AES_KEY AnodeAesExpandedKey; + +#define Anode_aes256_expand_key(k,ek) AES_set_encrypt_key((const unsigned char *)(k),256,(AES_KEY *)(ek)) + +/* Note: in and out can be the same thing */ +#define Anode_aes256_encrypt(ek,in,out) AES_encrypt((const unsigned char *)(in),(unsigned char *)(out),(const AES_KEY *)(ek)) + +/* Note: iv is modified */ +static inline void Anode_aes256_cfb_encrypt( + const AnodeAesExpandedKey *expkey, + const unsigned char *in, + unsigned char *out, + unsigned char *iv, + unsigned long len) +{ + int tmp = 0; + AES_cfb128_encrypt(in,out,len,(const AES_KEY *)expkey,iv,&tmp,AES_ENCRYPT); +} +static inline void Anode_aes256_cfb_decrypt( + const AnodeAesExpandedKey *expkey, + const unsigned char *in, + unsigned char *out, + unsigned char *iv, + unsigned long len) +{ + int tmp = 0; + AES_cfb128_encrypt(in,out,len,(const AES_KEY *)expkey,iv,&tmp,AES_DECRYPT); +} + +/* CMAC message authentication code */ +void Anode_cmac_aes256( + const AnodeAesExpandedKey *expkey, + const unsigned char *restrict data, + unsigned long data_len, + unsigned char *restrict mac); + +#endif diff --git a/attic/historic/anode/libanode/impl/dictionary.c b/attic/historic/anode/libanode/impl/dictionary.c new file mode 100644 index 00000000..060c3815 --- /dev/null +++ b/attic/historic/anode/libanode/impl/dictionary.c @@ -0,0 +1,239 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include "dictionary.h" + +static const char *EMPTY_STR = ""; + +void AnodeDictionary_clear(struct AnodeDictionary *d) +{ + struct AnodeDictionaryEntry *e,*ne; + int oldcs; + unsigned int i; + + oldcs = d->case_sensitive; + + for(i=0;iht[i]; + while (e) { + ne = e->next; + if ((e->key)&&(e->key != EMPTY_STR)) free((void *)e->key); + if ((e->value)&&(e->value != EMPTY_STR)) free((void *)e->value); + free((void *)e); + e = ne; + } + } + + Anode_zero((void *)d,sizeof(struct AnodeDictionary)); + + d->case_sensitive = oldcs; +} + +void AnodeDictionary_put(struct AnodeDictionary *d,const char *key,const char *value) +{ + struct AnodeDictionaryEntry *e; + char *p1; + const char *p2; + unsigned int bucket = (d->case_sensitive) ? AnodeDictionary__get_bucket(key) : AnodeDictionary__get_bucket_ci(key); + unsigned int len,i; + + e = d->ht[bucket]; + while (e) { + if (((d->case_sensitive) ? Anode_streq(key,e->key) : Anode_strcaseeq(key,e->key))) { + if (!d->case_sensitive) { + p1 = e->key; + p2 = key; + while (*p2) *(p1++) = *(p2++); + } + + len = 0; + while (value[len]) ++len; + if (len) { + if ((e->value)&&(e->value != EMPTY_STR)) + e->value = (char *)realloc((void *)e->value,len + 1); + else e->value = (char *)malloc(len + 1); + for(i=0;ivalue[i] = value[i]; + e->value[i] = (char)0; + } else { + if ((e->value)&&(e->value != EMPTY_STR)) free((void *)e->value); + e->value = (char *)EMPTY_STR; + } + return; + } + e = e->next; + } + + e = (struct AnodeDictionaryEntry *)malloc(sizeof(struct AnodeDictionaryEntry)); + + len = 0; + while (key[len]) ++len; + if (len) { + e->key = (char *)malloc(len + 1); + for(i=0;ikey[i] = key[i]; + e->key[i] = (char)0; + } else e->key = (char *)EMPTY_STR; + + len = 0; + while (value[len]) ++len; + if (len) { + e->value = (char *)malloc(len + 1); + for(i=0;ivalue[i] = value[i]; + e->value[i] = (char)0; + } else e->value = (char *)EMPTY_STR; + + e->next = d->ht[bucket]; + d->ht[bucket] = e; + + ++d->size; +} + +void AnodeDictionary_read( + struct AnodeDictionary *d, + char *in, + const char *line_breaks, + const char *kv_breaks, + const char *comment_chars, + char escape_char, + int trim_whitespace_from_keys, + int trim_whitespace_from_values) +{ + char *line = in; + char *key; + char *value; + char *p1,*p2,*p3; + char last = ~escape_char; + int eof_state = 0; + + for(;;) { + if ((!*in)||((Anode_strchr(line_breaks,*in))&&((last != escape_char)||(!escape_char)))) { + if (!*in) + eof_state = 1; + else *in = (char)0; + + if ((*line)&&((comment_chars)&&(!Anode_strchr(comment_chars,*line)))) { + key = line; + + while (*line) { + if ((Anode_strchr(kv_breaks,*line))&&((last != escape_char)||(!escape_char))) { + *(line++) = (char)0; + break; + } else last = *(line++); + } + while ((*line)&&(Anode_strchr(kv_breaks,*line))&&((last != escape_char)||(!escape_char))) + last = *(line++); + value = line; + + if (escape_char) { + p1 = key; + while (*p1) { + if (*p1 == escape_char) { + p2 = p1; + p3 = p1 + 1; + while (*p3) + *(p2++) = *(p3++); + *p2 = (char)0; + } + ++p1; + } + p1 = value; + while (*p1) { + if (*p1 == escape_char) { + p2 = p1; + p3 = p1 + 1; + while (*p3) + *(p2++) = *(p3++); + *p2 = (char)0; + } + ++p1; + } + } + + if (trim_whitespace_from_keys) + Anode_trim(key); + if (trim_whitespace_from_values) + Anode_trim(value); + + AnodeDictionary_put(d,key,value); + } + + if (eof_state) + break; + else line = in + 1; + } + last = *(in++); + } +} + +long AnodeDictionary_write( + struct AnodeDictionary *d, + char *out, + long out_size, + const char *line_break, + const char *kv_break) +{ + struct AnodeDictionaryEntry *e; + const char *tmp; + long ptr = 0; + unsigned int bucket; + + if (out_size <= 0) + return -1; + + for(bucket=0;bucketht[bucket]; + while (e) { + tmp = e->key; + if (tmp) { + while (*tmp) { + out[ptr++] = *tmp++; + if (ptr >= (out_size - 1)) return -1; + } + } + + tmp = kv_break; + if (tmp) { + while (*tmp) { + out[ptr++] = *tmp++; + if (ptr >= (out_size - 1)) return -1; + } + } + + tmp = e->value; + if (tmp) { + while (*tmp) { + out[ptr++] = *tmp++; + if (ptr >= (out_size - 1)) return -1; + } + } + + tmp = line_break; + if (tmp) { + while (*tmp) { + out[ptr++] = *tmp++; + if (ptr >= (out_size - 1)) return -1; + } + } + + e = e->next; + } + } + + out[ptr] = (char)0; + + return ptr; +} diff --git a/attic/historic/anode/libanode/impl/dictionary.h b/attic/historic/anode/libanode/impl/dictionary.h new file mode 100644 index 00000000..48e1642a --- /dev/null +++ b/attic/historic/anode/libanode/impl/dictionary.h @@ -0,0 +1,126 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +/* This is a simple string hash table suitable for small tables such as zone + * files or HTTP header lists. */ + +#ifndef _ANODE_DICTIONARY_H +#define _ANODE_DICTIONARY_H + +#include "misc.h" + +/* This is a fixed hash table and is designed for relatively small numbers + * of keys for things like zone files. */ +#define ANODE_DICTIONARY_FIXED_HASH_TABLE_SIZE 16 +#define ANODE_DICTIONARY_FIXED_HASH_TABLE_MASK 15 + +/* Computes a hash code for a string and returns the hash bucket */ +static inline unsigned int AnodeDictionary__get_bucket(const char *s) +{ + unsigned int hc = 3; + while (*s) + hc = ((hc << 4) + hc) + (unsigned int)*(s++); + return ((hc ^ (hc >> 4)) & ANODE_DICTIONARY_FIXED_HASH_TABLE_MASK); +} +/* Case insensitive version of get_bucket */ +static inline unsigned int AnodeDictionary__get_bucket_ci(const char *s) +{ + unsigned int hc = 3; + while (*s) + hc = ((hc << 4) + hc) + (unsigned int)Anode_tolower(*(s++)); + return ((hc ^ (hc >> 4)) & ANODE_DICTIONARY_FIXED_HASH_TABLE_MASK); +} + +struct AnodeDictionaryEntry +{ + char *key; + char *value; + struct AnodeDictionaryEntry *next; +}; + +struct AnodeDictionary +{ + struct AnodeDictionaryEntry *ht[ANODE_DICTIONARY_FIXED_HASH_TABLE_SIZE]; + unsigned int size; + int case_sensitive; +}; + +static inline void AnodeDictionary_init(struct AnodeDictionary *d,int case_sensitive) +{ + Anode_zero((void *)d,sizeof(struct AnodeDictionary)); + d->case_sensitive = case_sensitive; +} + +void AnodeDictionary_clear(struct AnodeDictionary *d); + +static inline void AnodeDictionary_destroy(struct AnodeDictionary *d) +{ + AnodeDictionary_clear(d); +} + +void AnodeDictionary_put(struct AnodeDictionary *d,const char *key,const char *value); + +static inline const char *AnodeDictionary_get(struct AnodeDictionary *d,const char *key) +{ + struct AnodeDictionaryEntry *e; + unsigned int bucket = (d->case_sensitive) ? AnodeDictionary__get_bucket(key) : AnodeDictionary__get_bucket_ci(key); + + e = d->ht[bucket]; + while (e) { + if ((d->case_sensitive ? Anode_streq(key,e->key) : Anode_strcaseeq(key,e->key))) + return e->value; + e = e->next; + } + + return (const char *)0; +} + +static inline void AnodeDictionary_iterate( + struct AnodeDictionary *d, + void *arg, + int (*func)(void *,const char *,const char *)) +{ + struct AnodeDictionaryEntry *e; + unsigned int bucket; + + for(bucket=0;bucketht[bucket]; + while (e) { + if (!func(arg,e->key,e->value)) + return; + e = e->next; + } + } +} + +void AnodeDictionary_read( + struct AnodeDictionary *d, + char *in, + const char *line_breaks, + const char *kv_breaks, + const char *comment_chars, + char escape_char, + int trim_whitespace_from_keys, + int trim_whitespace_from_values); + +long AnodeDictionary_write( + struct AnodeDictionary *d, + char *out, + long out_size, + const char *line_break, + const char *kv_break); + +#endif diff --git a/attic/historic/anode/libanode/impl/dns_txt.c b/attic/historic/anode/libanode/impl/dns_txt.c new file mode 100644 index 00000000..b5cf1318 --- /dev/null +++ b/attic/historic/anode/libanode/impl/dns_txt.c @@ -0,0 +1,93 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "dns_txt.h" + +#ifndef C_IN +#define C_IN ns_c_in +#endif +#ifndef T_TXT +#define T_TXT ns_t_txt +#endif + +static volatile int Anode_resolver_initialized = 0; + +int Anode_sync_resolve_txt(const char *host,char *txt,unsigned int txt_len) +{ + unsigned char answer[16384],*pptr,*end; + char name[16384]; + int len,explen,i; + + if (!Anode_resolver_initialized) { + Anode_resolver_initialized = 1; + res_init(); + } + + /* Do not taunt happy fun ball. */ + + len = res_search(host,C_IN,T_TXT,answer,sizeof(answer)); + if (len > 12) { + pptr = answer + 12; + end = answer + len; + + explen = dn_expand(answer,end,pptr,name,sizeof(name)); + if (explen > 0) { + pptr += explen; + + if ((pptr + 2) >= end) return 2; + if (ntohs(*((uint16_t *)pptr)) == T_TXT) { + pptr += 4; + if (pptr >= end) return 2; + + explen = dn_expand(answer,end,pptr,name,sizeof(name)); + if (explen > 0) { + pptr += explen; + + if ((pptr + 2) >= end) return 2; + if (ntohs(*((uint16_t *)pptr)) == T_TXT) { + pptr += 10; + if (pptr >= end) return 2; + + len = *(pptr++); + if (len <= 0) return 2; + if ((pptr + len) > end) return 2; + + if (txt_len < (len + 1)) + return 4; + else { + for(i=0;i + * + * 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 _ANODE_DNS_TXT_H +#define _ANODE_DNS_TXT_H + +/** + * Synchronous TXT resolver routine + * + * Error codes: + * 1 - I/O error + * 2 - Invalid response + * 3 - TXT record not found + * 4 - Destination buffer too small for result + * + * @param host Host name + * @param txt Buffer to store TXT result + * @param txt_len Size of buffer + * @return Zero on success, special error code on failure + */ +int Anode_sync_resolve_txt(const char *host,char *txt,unsigned int txt_len); + +#endif + diff --git a/attic/historic/anode/libanode/impl/ec.c b/attic/historic/anode/libanode/impl/ec.c new file mode 100644 index 00000000..2604b4a9 --- /dev/null +++ b/attic/historic/anode/libanode/impl/ec.c @@ -0,0 +1,219 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "types.h" +#include "misc.h" +#include "ec.h" + +static EC_GROUP *AnodeEC_group = (EC_GROUP *)0; + +static void *AnodeEC_KDF(const void *in,size_t inlen,void *out,size_t *outlen) +{ + unsigned long i,longest_length; + + if (!*outlen) + return out; + + for(i=0;i<(unsigned long)*outlen;++i) + ((unsigned char *)out)[i] = (unsigned char)0; + + longest_length = inlen; + if (longest_length < *outlen) + longest_length = *outlen; + for(i=0;i ANODE_EC_PRIME_BYTES)||(len < 0)) { + EC_KEY_free(key); + return 0; + } + BN_bn2bin(EC_KEY_get0_private_key(key),&(pair->priv.key[ANODE_EC_PRIME_BYTES - len])); + pair->priv.bytes = ANODE_EC_PRIME_BYTES; + + len = EC_POINT_point2oct(AnodeEC_group,EC_KEY_get0_public_key(key),POINT_CONVERSION_COMPRESSED,pair->pub.key,sizeof(pair->pub.key),0); + if (len != ANODE_EC_PUBLIC_KEY_BYTES) { + EC_KEY_free(key); + return 0; + } + pair->pub.bytes = ANODE_EC_PUBLIC_KEY_BYTES; + + /* Keep a copy of OpenSSL's structure around so we don't have to re-init + * it every time we use our key pair structure. */ + pair->internal_key = key; + + return 1; +} + +int AnodeECKeyPair_init(struct AnodeECKeyPair *pair,const struct AnodeECKey *pub,const struct AnodeECKey *priv) +{ + EC_KEY *key; + EC_POINT *kxy; + BIGNUM *pn; + + if (!AnodeEC_group) { + AnodeEC_group = EC_GROUP_new_by_curve_name(ANODE_EC_GROUP); + if (!AnodeEC_group) return 0; + } + + key = EC_KEY_new(); + if (!key) + return 0; + + if (!EC_KEY_set_group(key,AnodeEC_group)) { + EC_KEY_free(key); + return 0; + } + + /* Grab the private key */ + if (priv->bytes != ANODE_EC_PRIME_BYTES) { + EC_KEY_free(key); + return 0; + } + pn = BN_new(); + if (!pn) { + EC_KEY_free(key); + return 0; + } + if (!BN_bin2bn(priv->key,ANODE_EC_PRIME_BYTES,pn)) { + BN_free(pn); + EC_KEY_free(key); + return 0; + } + if (!EC_KEY_set_private_key(key,pn)) { + BN_free(pn); + EC_KEY_free(key); + return 0; + } + BN_free(pn); + + /* Set the public key */ + if (pub->bytes != ANODE_EC_PUBLIC_KEY_BYTES) { + EC_KEY_free(key); + return 0; + } + kxy = EC_POINT_new(AnodeEC_group); + if (!kxy) { + EC_KEY_free(key); + return 0; + } + EC_POINT_oct2point(AnodeEC_group,kxy,pub->key,ANODE_EC_PUBLIC_KEY_BYTES,0); + if (!EC_KEY_set_public_key(key,kxy)) { + EC_POINT_free(kxy); + EC_KEY_free(key); + return 0; + } + EC_POINT_free(kxy); + + Anode_zero(pair,sizeof(struct AnodeECKeyPair)); + Anode_memcpy((void *)&(pair->pub),(const void *)pub,sizeof(struct AnodeECKey)); + Anode_memcpy((void *)&(pair->priv),(const void *)priv,sizeof(struct AnodeECKey)); + pair->internal_key = key; + + return 1; +} + +void AnodeECKeyPair_destroy(struct AnodeECKeyPair *pair) +{ + if (pair) { + if (pair->internal_key) + EC_KEY_free((EC_KEY *)pair->internal_key); + } +} + +int AnodeECKeyPair_agree(const struct AnodeECKeyPair *my_key_pair,const struct AnodeECKey *their_pub_key,unsigned char *key_buf,unsigned int key_len) +{ + EC_POINT *pub; + int i; + + if (!AnodeEC_group) { + AnodeEC_group = EC_GROUP_new_by_curve_name(ANODE_EC_GROUP); + if (!AnodeEC_group) return 0; + } + + if (!my_key_pair->internal_key) + return 0; + + if (their_pub_key->bytes != ANODE_EC_PUBLIC_KEY_BYTES) + return 0; + pub = EC_POINT_new(AnodeEC_group); + if (!pub) + return 0; + EC_POINT_oct2point(AnodeEC_group,pub,their_pub_key->key,ANODE_EC_PUBLIC_KEY_BYTES,0); + + i = ECDH_compute_key(key_buf,key_len,pub,(EC_KEY *)my_key_pair->internal_key,&AnodeEC_KDF); + if (i != (int)key_len) { + EC_POINT_free(pub); + return 0; + } + + EC_POINT_free(pub); + + return 1; +} + +void AnodeEC_random(unsigned char *buf,unsigned int len) +{ + RAND_pseudo_bytes(buf,len); +} diff --git a/attic/historic/anode/libanode/impl/ec.h b/attic/historic/anode/libanode/impl/ec.h new file mode 100644 index 00000000..f1262664 --- /dev/null +++ b/attic/historic/anode/libanode/impl/ec.h @@ -0,0 +1,61 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +/* Elliptic curve glue -- hides OpenSSL code behind this source module */ + +#ifndef _ANODE_EC_H +#define _ANODE_EC_H + +#include "misc.h" + +/* Right now, only one mode is supported: NIST-P-256. This is the only mode + * supported in the spec as well, and should be good for quite some time. + * If other modes are needed this code will need to be refactored. */ + +/* NIST-P-256 prime size in bytes */ +#define ANODE_EC_PRIME_BYTES 32 + +/* Sizes of key fields */ +#define ANODE_EC_GROUP NID_X9_62_prime256v1 +#define ANODE_EC_PUBLIC_KEY_BYTES (ANODE_EC_PRIME_BYTES + 1) +#define ANODE_EC_PRIVATE_KEY_BYTES ANODE_EC_PRIME_BYTES + +/* Larger of public or private key bytes, used for buffers */ +#define ANODE_EC_MAX_BYTES ANODE_EC_PUBLIC_KEY_BYTES + +struct AnodeECKey +{ + unsigned char key[ANODE_EC_MAX_BYTES]; + unsigned int bytes; +}; + +struct AnodeECKeyPair +{ + struct AnodeECKey pub; + struct AnodeECKey priv; + void *internal_key; +}; + +/* Key management functions */ +int AnodeECKeyPair_generate(struct AnodeECKeyPair *pair); +int AnodeECKeyPair_init(struct AnodeECKeyPair *pair,const struct AnodeECKey *pub,const struct AnodeECKey *priv); +void AnodeECKeyPair_destroy(struct AnodeECKeyPair *pair); +int AnodeECKeyPair_agree(const struct AnodeECKeyPair *my_key_pair,const struct AnodeECKey *their_pub_key,unsigned char *key_buf,unsigned int key_len); + +/* Provides access to the secure PRNG used to generate keys */ +void AnodeEC_random(unsigned char *buf,unsigned int len); + +#endif diff --git a/attic/historic/anode/libanode/impl/environment.c b/attic/historic/anode/libanode/impl/environment.c new file mode 100644 index 00000000..16e8ebe3 --- /dev/null +++ b/attic/historic/anode/libanode/impl/environment.c @@ -0,0 +1,118 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include "environment.h" + +#ifdef WINDOWS +#include +#else +#include +#include +#endif + +static char Anode_cache_base[1024] = { 0 }; + +const char *Anode_get_cache() +{ + if (Anode_cache_base[0]) + return Anode_cache_base; + +#ifdef WINDOWS +#else + char tmp[1024]; + char home[1024]; + unsigned int i; + struct stat st; + const char *_home = getenv("HOME"); + + if (!_home) + return (const char *)0; + for(i=0;i + * + * 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 _ANODE_ENVIRONMENT_H +#define _ANODE_ENVIRONMENT_H + +#ifdef WINDOWS +#define ANODE_PATH_SEPARATOR '\\' +#else +#define ANODE_PATH_SEPARATOR '/' +#endif + +const char *Anode_get_cache(); +char *Anode_get_cache_sub(const char *cache_subdir,char *buf,unsigned int len); + +#endif + diff --git a/attic/historic/anode/libanode/impl/http_client.c b/attic/historic/anode/libanode/impl/http_client.c new file mode 100644 index 00000000..a398a585 --- /dev/null +++ b/attic/historic/anode/libanode/impl/http_client.c @@ -0,0 +1,558 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include "http_client.h" +#include "misc.h" +#include "types.h" + +/* How much to increment read buffer at each capacity top? */ +#define ANODE_HTTP_CAPACITY_INCREMENT 4096 + +static void AnodeHttpClient_close_and_fail(struct AnodeHttpClient *client) +{ + if (client->impl.tcp_connection) { + client->impl.transport_engine->tcp_close(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + } + + client->response.data_length = 0; + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; + + if (client->handler) + client->handler(client); +} + +static void AnodeHttpClient_do_initiate_client(struct AnodeHttpClient *client) +{ + const char *method = ""; + long l,i; + + switch(client->method) { + case ANODE_HTTP_GET: method = "GET"; break; + case ANODE_HTTP_HEAD: method = "HEAD"; break; + case ANODE_HTTP_POST: method = "POST"; break; + } + client->impl.outbuf_len = snprintf((char *)client->impl.outbuf,sizeof(client->impl.outbuf), + "%s %s%s%s HTTP/1.1\r\nHost: %s:%d\r\n%s", + method, + client->uri.path, + ((client->uri.query[0]) ? "?" : ""), + client->uri.query, + client->uri.host, + ((client->uri.port > 0) ? client->uri.port : 80), + ((client->keepalive) ? "" : "Connection: close\r\n") + ); + if (client->impl.outbuf_len >= (sizeof(client->impl.outbuf) - 2)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE; + AnodeHttpClient_close_and_fail(client); + return; + } + + if (client->method == ANODE_HTTP_POST) { + if ((client->data)&&(client->data_length)) { + client->impl.outbuf_len += snprintf((char *)client->impl.outbuf + client->impl.outbuf_len,sizeof(client->impl.outbuf) - client->impl.outbuf_len, + "Content-Type: %s\r\n", + (client->data_content_type ? client->data_content_type : "application/x-www-form-urlencoded") + ); + if (client->impl.outbuf_len >= (sizeof(client->impl.outbuf) - 2)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE; + AnodeHttpClient_close_and_fail(client); + return; + } + client->impl.outbuf_len += snprintf((char *)client->impl.outbuf + client->impl.outbuf_len,sizeof(client->impl.outbuf) - client->impl.outbuf_len, + "Content-Length: %u\r\n", + client->data_length + ); + if (client->impl.outbuf_len >= (sizeof(client->impl.outbuf) - 2)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE; + AnodeHttpClient_close_and_fail(client); + return; + } + } else { + client->impl.outbuf_len += snprintf((char *)client->impl.outbuf + client->impl.outbuf_len,sizeof(client->impl.outbuf) - client->impl.outbuf_len, + "Content-Length: 0\r\n" + ); + if (client->impl.outbuf_len >= (sizeof(client->impl.outbuf) - 2)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE; + AnodeHttpClient_close_and_fail(client); + return; + } + } + } + + l = AnodeDictionary_write(&(client->headers),(char *)client->impl.outbuf + client->impl.outbuf_len,(long)(sizeof(client->impl.outbuf) - client->impl.outbuf_len - 2),"\r\n",": "); + if (l < 0) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE; + AnodeHttpClient_close_and_fail(client); + return; + } + + client->impl.outbuf_len += (unsigned int)l; + if (client->impl.outbuf_len >= (sizeof(client->impl.outbuf) - 2)) { /* sanity check */ + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE; + AnodeHttpClient_close_and_fail(client); + return; + } + + client->impl.outbuf[client->impl.outbuf_len++] = '\r'; + client->impl.outbuf[client->impl.outbuf_len++] = '\n'; + + if ((client->method == ANODE_HTTP_POST)&&(client->data)&&(client->data_length)) { + i = sizeof(client->impl.outbuf) - client->impl.outbuf_len; + if (i > client->data_length) + i = client->data_length; + Anode_memcpy((client->impl.outbuf + client->impl.outbuf_len),client->data,i); + client->impl.request_data_ptr += i; + client->impl.outbuf_len += i; + } + + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_SEND; + client->impl.transport_engine->tcp_start_writing(client->impl.transport_engine,client->impl.tcp_connection); +} + +static void AnodeHttpClient_tcp_outgoing_connect_handler( + AnodeTransportEngine *transport, + AnodeTransportTcpConnection *connection, + int error_code) +{ + struct AnodeHttpClient *client; + + if (!(client = (struct AnodeHttpClient *)(connection->ptr))) + return; + + if ((client->impl.phase == ANODE_HTTP_REQUEST_PHASE_CONNECT)&&(!client->impl.freed)) { + if (error_code) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_CONNECT_FAILED; + AnodeHttpClient_close_and_fail(client); + } else { + client->impl.tcp_connection = connection; + AnodeHttpClient_do_initiate_client(client); + } + } else transport->tcp_close(transport,connection); +} + +static void AnodeHttpClient_tcp_connection_terminated_handler( + AnodeTransportEngine *transport, + AnodeTransportTcpConnection *connection, + int error_code) +{ + struct AnodeHttpClient *client; + + if (!(client = (struct AnodeHttpClient *)(connection->ptr))) + return; + if (client->impl.freed) + return; + + client->response.data_length = 0; + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + if ((client->impl.phase != ANODE_HTTP_REQUEST_PHASE_KEEPALIVE)&&(client->impl.phase != ANODE_HTTP_REQUEST_PHASE_CLOSED)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_SERVER_CLOSED_CONNECTION; + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; + AnodeHttpClient_close_and_fail(client); + } else client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; +} + +static void AnodeHttpClient_tcp_receive_handler( + AnodeTransportEngine *transport, + AnodeTransportTcpConnection *connection, + void *data, + unsigned int data_length) +{ + struct AnodeHttpClient *client; + char *p1,*p2; + unsigned int i; + long l; + + if (!(client = (struct AnodeHttpClient *)(connection->ptr))) + return; + if (client->impl.freed) { + transport->tcp_close(transport,connection); + return; + } + + if (!client->response.data) + client->response.data = malloc(client->impl.response_data_capacity = ANODE_HTTP_CAPACITY_INCREMENT); + + i = 0; + while (i < data_length) { + switch(client->impl.read_mode) { + case ANODE_HTTP_READ_MODE_WAITING: + for(;iresponse.data)[client->response.data_length] = (char)0; + client->response.data_length = 0; + + p1 = (char *)Anode_strchr((char *)client->response.data,' '); + if (!p1) + p1 = (char *)Anode_strchr((char *)client->response.data,'\t'); + if (p1) { + while ((*p1 == ' ')||(*p1 == '\t')) ++p1; + if (!*p1) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_INVALID_RESPONSE; + AnodeHttpClient_close_and_fail(client); + return; + } + p2 = p1 + 1; + while (*p2) { + if ((*p2 == ' ')||(*p2 == '\t')||(*p2 == '\r')||(*p2 == '\n')) { + *p2 = (char)0; + break; + } else ++p2; + } + client->response.code = (int)strtol(p1,(char **)0,10); + client->impl.read_mode = ANODE_HTTP_READ_MODE_HEADERS; + ++i; break; /* Exit inner for() */ + } + } else { + ((char *)client->response.data)[client->response.data_length++] = ((const char *)data)[i]; + if (client->response.data_length >= client->impl.response_data_capacity) + client->response.data = realloc(client->response.data,client->impl.response_data_capacity += ANODE_HTTP_CAPACITY_INCREMENT); + } + } + break; + case ANODE_HTTP_READ_MODE_HEADERS: + case ANODE_HTTP_READ_MODE_CHUNKED_FOOTER: + for(;iimpl.header_line_buf[client->impl.header_line_buf_ptr] = (char)0; + client->impl.header_line_buf_ptr = 0; + + if ((!client->impl.header_line_buf[0])||((client->impl.header_line_buf[0] == '\r')&&(!client->impl.header_line_buf[1]))) { + /* If the line is empty (or is empty with \r\n as the + * line terminator), we're at the end. */ + if (client->impl.read_mode == ANODE_HTTP_READ_MODE_CHUNKED_FOOTER) { + /* If this is a chunked footer, we finally end the + * chunked response. */ + client->impl.read_mode = ANODE_HTTP_READ_MODE_WAITING; + if (client->keepalive) + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_KEEPALIVE; + else { + client->impl.transport_engine->tcp_close(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; + } + if (client->handler) + client->handler(client); + if (client->impl.freed) + return; + } else { + /* Otherwise, this is a regular header block */ + if (client->response.code == 100) { + /* Ignore 100 Continue messages */ + client->impl.read_mode = ANODE_HTTP_READ_MODE_WAITING; + ++i; break; /* Exit inner for() */ + } else if ((client->response.code == 200)&&(client->method != ANODE_HTTP_HEAD)) { + /* Other messages get their headers parsed to determine + * how to read them. */ + p1 = (char *)AnodeDictionary_get(&(client->response.headers),"transfer-encoding"); + if ((p1)&&(Anode_strcaseeq(p1,"chunked"))) { + /* Chunked encoding enters chunked mode */ + client->impl.header_line_buf_ptr = 0; + client->impl.read_mode = ANODE_HTTP_READ_MODE_CHUNKED_CHUNK_SIZE; + ++i; break; /* Exit inner for() */ + } else { + /* Else we must have a Content-Length header */ + p1 = (char *)AnodeDictionary_get(&(client->response.headers),"content-length"); + if (!p1) { + /* No chunked or content length is not supported */ + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_INVALID_RESPONSE; + AnodeHttpClient_close_and_fail(client); + return; + } else { + /* Enter block read mode with content length */ + l = strtol(p1,(char **)0,10); + if (l <= 0) { + /* Zero length data is all done... */ + client->impl.expecting_response_length = 0; + client->impl.read_mode = ANODE_HTTP_READ_MODE_WAITING; + if (client->keepalive) + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_KEEPALIVE; + else { + client->impl.transport_engine->tcp_close(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; + } + + if (client->handler) + client->handler(client); + if (client->impl.freed) + return; + + ++i; break; /* Exit inner for() */ + } else { + /* Else start reading... */ + client->impl.expecting_response_length = (unsigned int)l; + client->impl.read_mode = ANODE_HTTP_READ_MODE_BLOCK; + ++i; break; /* Exit inner for() */ + } + } + } + } else { + /* HEAD clients or non-200 codes get headers only */ + client->impl.expecting_response_length = 0; + client->impl.read_mode = ANODE_HTTP_READ_MODE_WAITING; + if (client->keepalive) + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_KEEPALIVE; + else { + client->impl.transport_engine->tcp_close(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; + } + + if (client->handler) + client->handler(client); + if (client->impl.freed) + return; + + ++i; break; /* Exit inner for() */ + } + } + } else { + /* Otherwise this is another header, add to dictionary */ + AnodeDictionary_read( + &(client->response.headers), + client->impl.header_line_buf, + "\r\n", + ": \t", + "", + (char)0, + 1, + 1 + ); + } + } else { + client->impl.header_line_buf[client->impl.header_line_buf_ptr++] = ((const char *)data)[i]; + if (client->impl.header_line_buf_ptr >= sizeof(client->impl.header_line_buf)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_INVALID_RESPONSE; + AnodeHttpClient_close_and_fail(client); + return; + } + } + } + break; + case ANODE_HTTP_READ_MODE_BLOCK: + if ((client->response.data_length + client->impl.expecting_response_length) > client->impl.response_data_capacity) + client->response.data = realloc(client->response.data,client->impl.response_data_capacity = (client->response.data_length + client->impl.expecting_response_length)); + + for(;((iimpl.expecting_response_length));++i) { + ((char *)client->response.data)[client->response.data_length++] = ((const char *)data)[i]; + --client->impl.expecting_response_length; + } + + if (!client->impl.expecting_response_length) { + client->impl.read_mode = ANODE_HTTP_READ_MODE_WAITING; + if (client->keepalive) + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_KEEPALIVE; + else { + client->impl.transport_engine->tcp_close(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CLOSED; + } + + if (client->handler) + client->handler(client); + if (client->impl.freed) + return; + } + break; + case ANODE_HTTP_READ_MODE_CHUNKED_CHUNK_SIZE: + for(;iimpl.header_line_buf[client->impl.header_line_buf_ptr] = (char)0; + client->impl.header_line_buf_ptr = 0; + + p1 = client->impl.header_line_buf; + while (*p1) { + if ((*p1 == ';')||(*p1 == ' ')||(*p1 == '\r')||(*p1 == '\n')||(*p1 == '\t')) { + *p1 = (char)0; + break; + } else ++p1; + } + + if (client->impl.header_line_buf[0]) { + l = strtol(client->impl.header_line_buf,(char **)0,16); + if (l <= 0) { + /* Zero length ends chunked and enters footer mode */ + client->impl.expecting_response_length = 0; + client->impl.read_mode = ANODE_HTTP_READ_MODE_CHUNKED_FOOTER; + } else { + /* Otherwise the next chunk is to be read */ + client->impl.expecting_response_length = (unsigned int)l; + client->impl.read_mode = ANODE_HTTP_READ_MODE_CHUNKED_DATA; + } + ++i; break; /* Exit inner for() */ + } + } else { + client->impl.header_line_buf[client->impl.header_line_buf_ptr++] = ((const char *)data)[i]; + if (client->impl.header_line_buf_ptr >= sizeof(client->impl.header_line_buf)) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_INVALID_RESPONSE; + AnodeHttpClient_close_and_fail(client); + return; + } + } + } + break; + case ANODE_HTTP_READ_MODE_CHUNKED_DATA: + if ((client->response.data_length + client->impl.expecting_response_length) > client->impl.response_data_capacity) + client->response.data = realloc(client->response.data,client->impl.response_data_capacity = (client->response.data_length + client->impl.expecting_response_length)); + + for(;((iimpl.expecting_response_length));++i) { + ((char *)client->response.data)[client->response.data_length++] = ((const char *)data)[i]; + --client->impl.expecting_response_length; + } + + if (!client->impl.expecting_response_length) + client->impl.read_mode = ANODE_HTTP_READ_MODE_CHUNKED_CHUNK_SIZE; + break; + } + } +} + +static void AnodeHttpClient_tcp_available_for_write_handler( + AnodeTransportEngine *transport, + AnodeTransportTcpConnection *connection) +{ + struct AnodeHttpClient *client; + unsigned int i,j; + int n; + + if (!(client = (struct AnodeHttpClient *)(connection->ptr))) + return; + if (client->impl.freed) { + transport->tcp_close(transport,connection); + return; + } + + if (client->impl.phase == ANODE_HTTP_REQUEST_PHASE_SEND) { + n = client->impl.transport_engine->tcp_send(client->impl.transport_engine,client->impl.tcp_connection,(const void *)client->impl.outbuf,(int)client->impl.outbuf_len); + if (n < 0) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_SERVER_CLOSED_CONNECTION; + AnodeHttpClient_close_and_fail(client); + } else if (n > 0) { + for(i=0,j=(client->impl.outbuf_len - (unsigned int)n);iimpl.outbuf[i] = client->impl.outbuf[i + (unsigned int)n]; + client->impl.outbuf_len -= (unsigned int)n; + + if ((client->method == ANODE_HTTP_POST)&&(client->data)&&(client->data_length)) { + i = sizeof(client->impl.outbuf) - client->impl.outbuf_len; + j = client->data_length - client->impl.request_data_ptr; + if (i > j) + i = j; + Anode_memcpy((client->impl.outbuf + client->impl.outbuf_len),client->data,i); + client->impl.request_data_ptr += i; + client->impl.outbuf_len += i; + } + + if (!client->impl.outbuf_len) { + client->impl.transport_engine->tcp_stop_writing(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_RECEIVE; + } + } + } else client->impl.transport_engine->tcp_stop_writing(client->impl.transport_engine,client->impl.tcp_connection); +} + +static void AnodeHttpClient_dns_result_handler( + AnodeTransportEngine *transport, + void *ptr, + int error_code, + const char *name, + const AnodeTransportIpAddress *ip_addresses, + unsigned int ip_address_count, + const AnodeAddress *anode_address) +{ + struct AnodeHttpClient *client; + AnodeTransportIpEndpoint to_endpoint; + + if (!(client = (struct AnodeHttpClient *)ptr)) + return; + if (client->impl.freed) + return; + + if ((error_code)||(!ip_address_count)) { + if (client->impl.phase == ANODE_HTTP_REQUEST_PHASE_RESOLVE) { + client->response.code = ANODE_HTTP_SPECIAL_RESPONSE_DNS_RESOLVE_FAILED; + AnodeHttpClient_close_and_fail(client); + } + } else { + client->impl.phase = ANODE_HTTP_REQUEST_PHASE_CONNECT; + Anode_memcpy(&to_endpoint.address,ip_addresses,sizeof(AnodeTransportIpAddress)); + to_endpoint.port = (client->uri.port > 0) ? client->uri.port : 80; + client->impl.transport_engine->tcp_connect( + client->impl.transport_engine, + client, + &AnodeHttpClient_tcp_outgoing_connect_handler, + &AnodeHttpClient_tcp_connection_terminated_handler, + &AnodeHttpClient_tcp_receive_handler, + &AnodeHttpClient_tcp_available_for_write_handler, + &to_endpoint); + } +} + +struct AnodeHttpClient *AnodeHttpClient_new(AnodeTransportEngine *transport_engine) +{ + struct AnodeHttpClient *req = malloc(sizeof(struct AnodeHttpClient)); + Anode_zero(req,sizeof(struct AnodeHttpClient)); + + AnodeDictionary_init(&(req->headers),0); + AnodeDictionary_init(&(req->response.headers),0); + + req->impl.transport_engine = transport_engine; + + return req; +} + +void AnodeHttpClient_send(struct AnodeHttpClient *client) +{ + client->response.code = 0; + client->response.data_length = 0; + AnodeDictionary_clear(&(client->response.headers)); + + client->impl.request_data_ptr = 0; + client->impl.expecting_response_length = 0; + client->impl.read_mode = ANODE_HTTP_READ_MODE_WAITING; + client->impl.outbuf_len = 0; + + if (!client->impl.tcp_connection) { + client->impl.transport_engine->dns_resolve( + client->impl.transport_engine, + &AnodeHttpClient_dns_result_handler, + client, + client->uri.host, + ANODE_TRANSPORT_DNS_QUERY_ALWAYS, + ANODE_TRANSPORT_DNS_QUERY_IF_NO_PREVIOUS, + ANODE_TRANSPORT_DNS_QUERY_NEVER); + } else AnodeHttpClient_do_initiate_client(client); +} + +void AnodeHttpClient_free(struct AnodeHttpClient *client) +{ + AnodeDictionary_destroy(&(client->headers)); + AnodeDictionary_destroy(&(client->response.headers)); + + if (client->impl.tcp_connection) { + client->impl.transport_engine->tcp_close(client->impl.transport_engine,client->impl.tcp_connection); + client->impl.tcp_connection = (AnodeTransportTcpConnection *)0; + } + + if (client->response.data) + free(client->response.data); + + client->impl.freed = 1; + client->impl.transport_engine->run_later(client->impl.transport_engine,client,&free); +} diff --git a/attic/historic/anode/libanode/impl/http_client.h b/attic/historic/anode/libanode/impl/http_client.h new file mode 100644 index 00000000..f1673097 --- /dev/null +++ b/attic/historic/anode/libanode/impl/http_client.h @@ -0,0 +1,200 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 _ANODE_HTTP_CLIENT_H +#define _ANODE_HTTP_CLIENT_H + +#include +#include +#include "dictionary.h" +#include "../anode.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * HTTP request type + */ +enum AnodeHttpClientRequestMethod +{ + ANODE_HTTP_GET = 0, + ANODE_HTTP_HEAD = 1, + ANODE_HTTP_POST = 2 +}; + +/* + * Special response codes to indicate I/O errors + */ +#define ANODE_HTTP_SPECIAL_RESPONSE_DNS_RESOLVE_FAILED -1 +#define ANODE_HTTP_SPECIAL_RESPONSE_CONNECT_FAILED -2 +#define ANODE_HTTP_SPECIAL_RESPONSE_HEADERS_TOO_LARGE -3 +#define ANODE_HTTP_SPECIAL_RESPONSE_SERVER_CLOSED_CONNECTION -4 +#define ANODE_HTTP_SPECIAL_RESPONSE_INVALID_RESPONSE -5 + +/** + * Simple HTTP client + */ +struct AnodeHttpClient +{ + /** + * Request URI + */ + AnodeURI uri; + + /** + * Request method: GET, PUT, HEAD, or POST + */ + enum AnodeHttpClientRequestMethod method; + + /** + * Data for POST requests + * + * It is your responsibility to manage and/or free this pointer. The HTTP + * client only reads from it. + */ + const void *data; + unsigned int data_length; + + /** + * Content type for data, or null for application/x-www-form-urlencoded + */ + const char *data_content_type; + + /** + * Set to non-zero to use HTTP connection keepalive + * + * If keepalive is enabled, this request can be modified and re-used and + * its associated connection will stay open (being reopened if needed) + * until it is freed. + * + * Note that this client is too dumb to pool connections and pick them on + * the basis of host. Keepalive mode should only be set if the next request + * will be from the same host and port, otherwise you will get a '404'. + */ + int keepalive; + + /** + * Function pointer to be called when request is complete (or fails) + */ + void (*handler)(struct AnodeHttpClient *); + + /** + * Two arbitrary pointers that can be stored here for use by the handler. + * These are not accessed or modified by the client. + */ + void *ptr[2]; + + /** + * Request headers + */ + struct AnodeDictionary headers; + + struct { + /** + * Response code, set on completion or failure before handler is called + * + * Also check for the special response codes defined in http_client.h as + * these negative codes indicate network or other errors. + */ + int code; + + /** + * Response data, for GET and POST requests + */ + void *data; + + /** + * Length of response data + */ + unsigned int data_length; + + /** + * Response headers + */ + struct AnodeDictionary headers; + } response; + + /** + * Internal fields used by implementation + */ + struct { + /* Transport engine being used by request */ + AnodeTransportEngine *transport_engine; + + /* Connection to which request has been sent, or null if none */ + struct AnodeHttpConnection *connection; + + /* Buffer for reading chunked mode chunk lines (can't use data buf) */ + char header_line_buf[256]; + unsigned int header_line_buf_ptr; + + /* Where are we in sending request data? */ + unsigned int request_data_ptr; + + /* Capacity of response_data buffer */ + unsigned int response_data_capacity; + + /* How much response data are we currently expecting? */ + /* This is content-length in block mode or chunk length in chunked mode */ + unsigned int expecting_response_length; + + /* Read mode */ + enum { + ANODE_HTTP_READ_MODE_WAITING = 0, + ANODE_HTTP_READ_MODE_HEADERS = 1, + ANODE_HTTP_READ_MODE_BLOCK = 2, + ANODE_HTTP_READ_MODE_CHUNKED_CHUNK_SIZE = 3, + ANODE_HTTP_READ_MODE_CHUNKED_DATA = 4, + ANODE_HTTP_READ_MODE_CHUNKED_FOOTER = 5 + } read_mode; + + /* Connection from transport engine */ + AnodeTransportTcpConnection *tcp_connection; + + /* Write buffer */ + unsigned char outbuf[16384]; + unsigned int outbuf_len; + + /* Phase of request state machine */ + enum { + ANODE_HTTP_REQUEST_PHASE_RESOLVE = 0, + ANODE_HTTP_REQUEST_PHASE_CONNECT = 1, + ANODE_HTTP_REQUEST_PHASE_SEND = 2, + ANODE_HTTP_REQUEST_PHASE_RECEIVE = 3, + ANODE_HTTP_REQUEST_PHASE_KEEPALIVE = 4, + ANODE_HTTP_REQUEST_PHASE_CLOSED = 5 + } phase; + + /* Has request object been freed? */ + int freed; + + /** + * Pointer used internally for putting requests into linked lists + */ + struct AnodeHttpClient *next; + } impl; +}; + +struct AnodeHttpClient *AnodeHttpClient_new(AnodeTransportEngine *transport_engine); +void AnodeHttpClient_send(struct AnodeHttpClient *client); +void AnodeHttpClient_free(struct AnodeHttpClient *client); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/attic/historic/anode/libanode/impl/misc.c b/attic/historic/anode/libanode/impl/misc.c new file mode 100644 index 00000000..edc73978 --- /dev/null +++ b/attic/historic/anode/libanode/impl/misc.c @@ -0,0 +1,190 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include "misc.h" +#include "types.h" + +static const char Anode_hex_chars[16] = { + '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' +}; + +static const char Anode_base32_chars[32] = { + 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q', + 'r','s','t','u','v','w','x','y','z','2','3','4','5','6','7' +}; +static const unsigned char Anode_base32_bits[256] = { + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5, + 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,0,1,2, + 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +}; + +/* Table for converting ASCII chars to lower case */ +const unsigned char Anode_ascii_tolower_table[256] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, + 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, + 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, + 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, + 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, + 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, + 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, + 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, + 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, + 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, + 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, + 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff +}; + +void Anode_trim(char *s) +{ + char *dest = s; + char *last; + while ((*s)&&((*s == ' ')||(*s == '\t')||(*s == '\r')||(*s == '\n'))) + ++s; + last = s; + while ((*dest = *s)) { + if ((*dest != ' ')&&(*dest != '\t')&&(*dest != '\r')&&(*dest != '\n')) + last = dest; + ++dest; + ++s; + } + if (*last) + *(++last) = (char)0; +} + +unsigned int Anode_rand() +{ + static volatile int need_seed = 1; + + if (need_seed) { + need_seed = 0; + srandom((unsigned long)Anode_time64()); + } + + return (unsigned int)random(); +} + +void Anode_to_hex(const unsigned char *b,unsigned int len,char *h,unsigned int hlen) +{ + unsigned int i; + + if ((len * 2) >= hlen) + len = (hlen - 1) / 2; + + for(i=0;i> 4]; + *(h++) = Anode_hex_chars[b[i] & 0xf]; + } + *h = (char)0; +} + +void Anode_from_hex(const char *h,unsigned char *b,unsigned int blen) +{ + unsigned char *end = b + blen; + unsigned char v = (unsigned char)0; + + while (b != end) { + switch(*(h++)) { + case '0': v = 0x00; break; + case '1': v = 0x10; break; + case '2': v = 0x20; break; + case '3': v = 0x30; break; + case '4': v = 0x40; break; + case '5': v = 0x50; break; + case '6': v = 0x60; break; + case '7': v = 0x70; break; + case '8': v = 0x80; break; + case '9': v = 0x90; break; + case 'a': v = 0xa0; break; + case 'b': v = 0xb0; break; + case 'c': v = 0xc0; break; + case 'd': v = 0xd0; break; + case 'e': v = 0xe0; break; + case 'f': v = 0xf0; break; + default: return; + } + + switch(*(h++)) { + case '0': v |= 0x00; break; + case '1': v |= 0x01; break; + case '2': v |= 0x02; break; + case '3': v |= 0x03; break; + case '4': v |= 0x04; break; + case '5': v |= 0x05; break; + case '6': v |= 0x06; break; + case '7': v |= 0x07; break; + case '8': v |= 0x08; break; + case '9': v |= 0x09; break; + case 'a': v |= 0x0a; break; + case 'b': v |= 0x0b; break; + case 'c': v |= 0x0c; break; + case 'd': v |= 0x0d; break; + case 'e': v |= 0x0e; break; + case 'f': v |= 0x0f; break; + default: return; + } + + *(b++) = v; + } +} + +void Anode_base32_5_to_8(const unsigned char *in,char *out) +{ + out[0] = Anode_base32_chars[(in[0]) >> 3]; + out[1] = Anode_base32_chars[(in[0] & 0x07) << 2 | (in[1] & 0xc0) >> 6]; + out[2] = Anode_base32_chars[(in[1] & 0x3e) >> 1]; + out[3] = Anode_base32_chars[(in[1] & 0x01) << 4 | (in[2] & 0xf0) >> 4]; + out[4] = Anode_base32_chars[(in[2] & 0x0f) << 1 | (in[3] & 0x80) >> 7]; + out[5] = Anode_base32_chars[(in[3] & 0x7c) >> 2]; + out[6] = Anode_base32_chars[(in[3] & 0x03) << 3 | (in[4] & 0xe0) >> 5]; + out[7] = Anode_base32_chars[(in[4] & 0x1f)]; +} + +void Anode_base32_8_to_5(const char *in,unsigned char *out) +{ + out[0] = ((Anode_base32_bits[(unsigned int)in[0]]) << 3) | (Anode_base32_bits[(unsigned int)in[1]] & 0x1C) >> 2; + out[1] = ((Anode_base32_bits[(unsigned int)in[1]] & 0x03) << 6) | (Anode_base32_bits[(unsigned int)in[2]]) << 1 | (Anode_base32_bits[(unsigned int)in[3]] & 0x10) >> 4; + out[2] = ((Anode_base32_bits[(unsigned int)in[3]] & 0x0F) << 4) | (Anode_base32_bits[(unsigned int)in[4]] & 0x1E) >> 1; + out[3] = ((Anode_base32_bits[(unsigned int)in[4]] & 0x01) << 7) | (Anode_base32_bits[(unsigned int)in[5]]) << 2 | (Anode_base32_bits[(unsigned int)in[6]] & 0x18) >> 3; + out[4] = ((Anode_base32_bits[(unsigned int)in[6]] & 0x07) << 5) | (Anode_base32_bits[(unsigned int)in[7]]); +} diff --git a/attic/historic/anode/libanode/impl/misc.h b/attic/historic/anode/libanode/impl/misc.h new file mode 100644 index 00000000..38ddea7c --- /dev/null +++ b/attic/historic/anode/libanode/impl/misc.h @@ -0,0 +1,193 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +/* This contains miscellaneous functions, including some re-implementations + * of some functions from string.h. This is to help us port to some platforms + * (cough Windows Mobile cough) that lack a lot of the basic C library. */ + +#ifndef _ANODE_MISC_H +#define _ANODE_MISC_H + +#include +#include +#include "types.h" + +#ifndef ANODE_NO_STRING_H +#include +#include +#endif + +/* Table mapping ASCII characters to themselves or their lower case */ +extern const unsigned char Anode_ascii_tolower_table[256]; + +/* Get the lower case version of an ASCII char */ +#define Anode_tolower(c) ((char)Anode_ascii_tolower_table[((unsigned long)((unsigned char)(c)))]) + +/* Test strings for equality, return nonzero if equal */ +static inline unsigned int Anode_streq(const char *restrict a,const char *restrict b) +{ + if ((!a)||(!b)) + return 0; + while (*a == *(b++)) { + if (!*(a++)) + return 1; + } + return 0; +} + +/* Equality test ignoring (ASCII) case */ +static inline unsigned int Anode_strcaseeq(const char *restrict a,const char *restrict b) +{ + if ((!a)||(!b)) + return 0; + while (Anode_tolower(*a) == Anode_tolower(*(b++))) { + if (!*(a++)) + return 1; + } + return 0; +} + +/* Safe c-string copy, ensuring that dest[] always ends with zero */ +static inline void Anode_str_copy(char *restrict dest,const char *restrict src,unsigned int dest_size) +{ + char *restrict dest_end = dest + (dest_size - 1); + while ((*src)&&(dest != dest_end)) + *(dest++) = *(src++); + *dest = (char)0; +} + +/* Simple memcpy() */ +#ifdef ANODE_NO_STRING_H +static inline void Anode_memcpy(void *restrict dest,const void *restrict src,unsigned int len) +{ + unsigned int i; + for(i=0;i 8 base32 characters and vice versa */ +void Anode_base32_5_to_8(const unsigned char *in,char *out); +void Anode_base32_8_to_5(const char *in,unsigned char *out); + +#endif diff --git a/attic/historic/anode/libanode/impl/mutex.h b/attic/historic/anode/libanode/impl/mutex.h new file mode 100644 index 00000000..b20eb82b --- /dev/null +++ b/attic/historic/anode/libanode/impl/mutex.h @@ -0,0 +1,34 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 _ANODE_MUTEX_H +#define _ANODE_MUTEX_H + +#ifdef WINDOWS + +#else /* WINDOWS */ + +#include + +#define AnodeMutex pthread_mutex_t +#define AnodeMutex_init(m) pthread_mutex_init((m),(const pthread_mutexattr_t *)0) +#define AnodeMutex_destroy(m) pthread_mutex_destroy((m)) +#define AnodeMutex_lock(m) pthread_mutex_lock((m)) +#define AnodeMutex_unlock(m) pthread_mutex_unlock((m)) + +#endif /* WINDOWS */ + +#endif diff --git a/attic/historic/anode/libanode/impl/thread.c b/attic/historic/anode/libanode/impl/thread.c new file mode 100644 index 00000000..c2070462 --- /dev/null +++ b/attic/historic/anode/libanode/impl/thread.c @@ -0,0 +1,58 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include "thread.h" +#include + +#ifdef WINDOWS + +#else /* not WINDOWS */ + +struct _AnodeThread +{ + void (*func)(void *); + void *arg; + int wait_for_join; + pthread_t thread; +}; + +static void *_AnodeThread_main(void *arg) +{ + ((struct _AnodeThread *)arg)->func(((struct _AnodeThread *)arg)->arg); + if (!((struct _AnodeThread *)arg)->wait_for_join) + free(arg); + return (void *)0; +} + +AnodeThread *AnodeThread_create(void (*func)(void *),void *arg,int wait_for_join) +{ + struct _AnodeThread *t = malloc(sizeof(struct _AnodeThread)); + t->func = func; + t->arg = arg; + t->wait_for_join = wait_for_join; + pthread_create(&t->thread,(const pthread_attr_t *)0,&_AnodeThread_main,(void *)t); + if (!wait_for_join) + pthread_detach(t->thread); + return (AnodeThread *)t; +} + +void AnodeThread_join(AnodeThread *thread) +{ + pthread_join(((struct _AnodeThread *)thread)->thread,(void **)0); + free((void *)thread); +} + +#endif /* WINDOWS / not WINDOWS */ diff --git a/attic/historic/anode/libanode/impl/thread.h b/attic/historic/anode/libanode/impl/thread.h new file mode 100644 index 00000000..accf173a --- /dev/null +++ b/attic/historic/anode/libanode/impl/thread.h @@ -0,0 +1,65 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 _ANODE_THREAD_H +#define _ANODE_THREAD_H + +#ifdef WINDOWS + +#include +#include +typedef DWORD AnodeThreadId; + +#else /* not WINDOWS */ + +#include +typedef pthread_t AnodeThreadId; + +#define AnodeThread_self() pthread_self() +#define AnodeThreadId_equal(a,b) pthread_equal((pthread_t)(a),(pthread_t)(b)) + +#endif + +typedef void AnodeThread; + +/** + * Create and launch a new thread + * + * If wait_for_join is true (nonzero), the thread can and must be joined. The + * thread object won't be freed until join is called and returns. If + * wait_for_join is false, the thread object frees itself automatically on + * termination. + * + * If wait_for_join is false (zero), there is really no need to keep track of + * the thread object. + * + * @param func Function to call as thread main + * @param arg Argument to pass to function + * @param wait_for_join If false, thread deletes itself when it terminates + */ +AnodeThread *AnodeThread_create(void (*func)(void *),void *arg,int wait_for_join); + +/** + * Wait for a thread to terminate and delete thread object + * + * This can only be used for threads created with wait_for_join set to true. + * The thread object is no longer valid after this call. + * + * @param thread Thread to wait for termination and delete + */ +void AnodeThread_join(AnodeThread *thread); + +#endif diff --git a/attic/historic/anode/libanode/impl/types.h b/attic/historic/anode/libanode/impl/types.h new file mode 100644 index 00000000..5f070e5a --- /dev/null +++ b/attic/historic/anode/libanode/impl/types.h @@ -0,0 +1,25 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 _ANODE_TYPES_H +#define _ANODE_TYPES_H + +#ifdef WINDOWS +#else +#include +#endif + +#endif diff --git a/attic/historic/anode/libanode/network_address.c b/attic/historic/anode/libanode/network_address.c new file mode 100644 index 00000000..86ec054f --- /dev/null +++ b/attic/historic/anode/libanode/network_address.c @@ -0,0 +1,136 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include "impl/misc.h" +#include "impl/types.h" +#include "anode.h" + +const AnodeNetworkAddress AnodeNetworkAddress_ANY4 = { + ANODE_NETWORK_ADDRESS_IPV4, + { 0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } +}; +const AnodeNetworkAddress AnodeNetworkAddress_ANY6 = { + ANODE_NETWORK_ADDRESS_IPV6, + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } +}; +const AnodeNetworkAddress AnodeNetworkAddress_LOCAL4 = { + ANODE_NETWORK_ADDRESS_IPV4, + { 127,0,0,1, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } +}; +const AnodeNetworkAddress AnodeNetworkAddress_LOCAL6 = { + ANODE_NETWORK_ADDRESS_IPV6, + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } +}; + +int AnodeNetworkAddress_to_string(const AnodeNetworkAddress *address,char *buf,int len) +{ + const char *s; + + switch(address->type) { + case ANODE_NETWORK_ADDRESS_IPV4: + s = inet_ntop(AF_INET,(const void *)address->bits,buf,len); + if (s) + return Anode_strlen(s); + else return ANODE_ERR_INVALID_ARGUMENT; + break; + case ANODE_NETWORK_ADDRESS_IPV6: + s = inet_ntop(AF_INET6,address->bits,buf,len); + if (s) + return Anode_strlen(s); + else return ANODE_ERR_INVALID_ARGUMENT; + /* + case ANODE_NETWORK_ADDRESS_ETHERNET: + break; + case ANODE_NETWORK_ADDRESS_USB: + break; + case ANODE_NETWORK_ADDRESS_BLUETOOTH: + break; + case ANODE_NETWORK_ADDRESS_IPC: + break; + case ANODE_NETWORK_ADDRESS_80211S: + break; + case ANODE_NETWORK_ADDRESS_SERIAL: + break; + */ + case ANODE_NETWORK_ADDRESS_ANODE_256_40: + return AnodeAddress_to_string((const AnodeAddress *)address->bits,buf,len); + default: + return ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + } +} + +int AnodeNetworkAddress_from_string(const char *str,AnodeNetworkAddress *address) +{ + unsigned int dots = Anode_count_char(str,'.'); + unsigned int colons = Anode_count_char(str,':'); + + if ((dots == 3)&&(!colons)) { + address->type = ANODE_NETWORK_ADDRESS_IPV4; + if (inet_pton(AF_INET,str,address->bits) > 0) + return 0; + else return ANODE_ERR_INVALID_ARGUMENT; + } else if ((colons)&&(!dots)) { + address->type = ANODE_NETWORK_ADDRESS_IPV6; + if (inet_pton(AF_INET6,str,address->bits) > 0) + return 0; + else return ANODE_ERR_INVALID_ARGUMENT; + } else { + address->type = ANODE_NETWORK_ADDRESS_ANODE_256_40; + return AnodeAddress_from_string(str,(AnodeAddress *)address->bits); + } +} + +int AnodeNetworkEndpoint_from_sockaddr(const void *sockaddr,AnodeNetworkEndpoint *endpoint) +{ + switch(((struct sockaddr_storage *)sockaddr)->ss_family) { + case AF_INET: + *((uint32_t *)endpoint->address.bits) = (uint32_t)(((struct sockaddr_in *)sockaddr)->sin_addr.s_addr); + endpoint->port = (int)ntohs(((struct sockaddr_in *)sockaddr)->sin_port); + return 0; + case AF_INET6: + Anode_memcpy(endpoint->address.bits,((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr,16); + endpoint->port = (int)ntohs(((struct sockaddr_in6 *)sockaddr)->sin6_port); + return 0; + default: + return ANODE_ERR_INVALID_ARGUMENT; + } +} + +int AnodeNetworkEndpoint_to_sockaddr(const AnodeNetworkEndpoint *endpoint,void *sockaddr,int sockaddr_len) +{ + switch(endpoint->address.type) { + case ANODE_NETWORK_ADDRESS_IPV4: + if (sockaddr_len < (int)sizeof(struct sockaddr_in)) + return ANODE_ERR_BUFFER_TOO_SMALL; + Anode_zero(sockaddr,sizeof(struct sockaddr_in)); + ((struct sockaddr_in *)sockaddr)->sin_family = AF_INET; + ((struct sockaddr_in *)sockaddr)->sin_port = htons((uint16_t)endpoint->port); + ((struct sockaddr_in *)sockaddr)->sin_addr.s_addr = *((uint32_t *)endpoint->address.bits); + return 0; + case ANODE_NETWORK_ADDRESS_IPV6: + if (sockaddr_len < (int)sizeof(struct sockaddr_in6)) + return ANODE_ERR_BUFFER_TOO_SMALL; + Anode_zero(sockaddr,sizeof(struct sockaddr_in6)); + ((struct sockaddr_in6 *)sockaddr)->sin6_family = AF_INET6; + ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons((uint16_t)endpoint->port); + Anode_memcpy(((struct sockaddr_in6 *)sockaddr)->sin6_addr.s6_addr,endpoint->address.bits,16); + return 0; + default: + return ANODE_ERR_INVALID_ARGUMENT; + } +} diff --git a/attic/historic/anode/libanode/secure_random.c b/attic/historic/anode/libanode/secure_random.c new file mode 100644 index 00000000..4322d7de --- /dev/null +++ b/attic/historic/anode/libanode/secure_random.c @@ -0,0 +1,88 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include "impl/aes.h" +#include "impl/misc.h" +#include "anode.h" + +#ifdef WINDOWS +#include +#include +#endif + +struct AnodeSecureRandomImpl +{ + AnodeAesExpandedKey key; + unsigned char state[ANODE_AES_BLOCK_SIZE]; + unsigned char block[ANODE_AES_BLOCK_SIZE]; + unsigned int ptr; +}; + +AnodeSecureRandom *AnodeSecureRandom_new() +{ + unsigned char keybuf[ANODE_AES_KEY_SIZE + ANODE_AES_BLOCK_SIZE + ANODE_AES_BLOCK_SIZE]; + unsigned int i; + struct AnodeSecureRandomImpl *srng; + +#ifdef WINDOWS + HCRYPTPROV hProv; + if (CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT|CRYPT_SILENT)) { + CryptGenRandom(hProv,sizeof(keybuf),keybuf); + CryptReleaseContext(hProv,0); + } +#else + FILE *urandf = fopen("/dev/urandom","rb"); + if (urandf) { + fread((void *)keybuf,sizeof(keybuf),1,urandf); + fclose(urandf); + } +#endif + + for(i=0;i> 5); + + srng = malloc(sizeof(struct AnodeSecureRandomImpl)); + Anode_aes256_expand_key(keybuf,&srng->key); + for(i=0;istate[i] = keybuf[ANODE_AES_KEY_SIZE + i]; + for(i=0;iblock[i] = keybuf[ANODE_AES_KEY_SIZE + ANODE_AES_KEY_SIZE + i]; + srng->ptr = ANODE_AES_BLOCK_SIZE; + + return (AnodeSecureRandom *)srng; +} + +void AnodeSecureRandom_gen_bytes(AnodeSecureRandom *srng,void *buf,long count) +{ + long i,j; + + for(i=0;iptr == ANODE_AES_BLOCK_SIZE) { + Anode_aes256_encrypt(&((struct AnodeSecureRandomImpl *)srng)->key,((struct AnodeSecureRandomImpl *)srng)->state,((struct AnodeSecureRandomImpl *)srng)->state); + for(j=0;jblock[j] ^= ((struct AnodeSecureRandomImpl *)srng)->state[j]; + ((struct AnodeSecureRandomImpl *)srng)->ptr = 0; + } + ((unsigned char *)buf)[i] = ((struct AnodeSecureRandomImpl *)srng)->block[((struct AnodeSecureRandomImpl *)srng)->ptr++]; + } +} + +void AnodeSecureRandom_delete(AnodeSecureRandom *srng) +{ + free(srng); +} diff --git a/attic/historic/anode/libanode/system_transport.c b/attic/historic/anode/libanode/system_transport.c new file mode 100644 index 00000000..4bfb143e --- /dev/null +++ b/attic/historic/anode/libanode/system_transport.c @@ -0,0 +1,948 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include +#include +#include +#include "anode.h" +#include "impl/mutex.h" +#include "impl/thread.h" +#include "impl/misc.h" +#include "impl/dns_txt.h" + +#ifdef WINDOWS +#include +#include +#define AnodeSystemTransport__close_socket(s) closesocket((s)) +#define ANODE_USE_SELECT 1 +#else +#include +#include +#define AnodeSystemTransport__close_socket(s) close((s)) +#endif + +static const char *AnodeSystemTransport_CLASS = "SystemTransport"; + +/* ======================================================================== */ + +struct AnodeSystemTransport; + +struct AnodeSystemTransport_AnodeSocket +{ + AnodeSocket base; /* must be first */ + unsigned int entry_idx; +}; + +#define ANODE_SYSTEM_TRANSPORT_DNS_MAX_RESULTS 16 +struct AnodeSystemTransport__dns_request +{ + struct AnodeSystemTransport__dns_request *next; + + AnodeThread *thread; + struct AnodeSystemTransport *owner; + + void (*event_handler)(const AnodeEvent *event); + + char name[256]; + enum AnodeTransportDnsIncludeMode ipv4_include_mode; + enum AnodeTransportDnsIncludeMode ipv6_include_mode; + enum AnodeTransportDnsIncludeMode anode_include_mode; + + AnodeNetworkAddress addresses[ANODE_SYSTEM_TRANSPORT_DNS_MAX_RESULTS]; + unsigned int address_count; + + int error_code; +}; + +#ifdef ANODE_USE_SELECT +typedef int AnodeSystemTransport__poll_fd; /* for select() */ +#else +typedef struct pollfd AnodeSystemTransport__poll_fd; /* for poll() */ +#endif + +struct AnodeSystemTransport +{ + AnodeTransport interface; /* must be first */ + + AnodeTransport *base; + +#ifdef ANODE_USE_SELECT + FD_SET readfds; + FD_SET writefds; +#endif + + void (*default_event_handler)(const AnodeEvent *event); + + AnodeSystemTransport__poll_fd *fds; + struct AnodeSystemTransport_AnodeSocket *sockets; + unsigned int fd_count; + unsigned int fd_capacity; + + struct AnodeSystemTransport__dns_request *pending_dns_requests; + + int invoke_pipe[2]; + AnodeMutex invoke_pipe_m; + void *invoke_pipe_buf[2]; + unsigned int invoke_pipe_buf_ptr; +}; + +/* ======================================================================== */ +/* Internal helper methods */ + +static unsigned int AnodeSystemTransport__add_entry(struct AnodeSystemTransport *transport) +{ + if ((transport->fd_count + 1) > transport->fd_capacity) { + transport->fd_capacity += 8; + transport->fds = realloc(transport->fds,sizeof(AnodeSystemTransport__poll_fd) * transport->fd_capacity); + transport->sockets = realloc(transport->sockets,sizeof(struct AnodeSystemTransport_AnodeSocket) * transport->fd_capacity); + } + return transport->fd_count++; +} + +static void AnodeSystemTransport__remove_entry(struct AnodeSystemTransport *transport,const unsigned int idx) +{ + unsigned int i; + + --transport->fd_count; + for(i=idx;ifd_count;++i) { + Anode_memcpy(&transport->fds[i],&transport->fds[i+1],sizeof(AnodeSystemTransport__poll_fd)); + Anode_memcpy(&transport->sockets[i],&transport->sockets[i+1],sizeof(struct AnodeSystemTransport_AnodeSocket)); + } + + if ((transport->fd_capacity - transport->fd_count) > 16) { + transport->fd_capacity -= 16; + transport->fds = realloc(transport->fds,sizeof(AnodeSystemTransport__poll_fd) * transport->fd_capacity); + transport->sockets = realloc(transport->sockets,sizeof(struct AnodeSystemTransport_AnodeSocket) * transport->fd_capacity); + } +} + +static void AnodeSystemTransport__dns_invoke_on_completion(void *_dreq) +{ + struct AnodeSystemTransport__dns_request *dreq = (struct AnodeSystemTransport__dns_request *)_dreq; + struct AnodeSystemTransport__dns_request *ptr,**lastnext; + + AnodeThread_join(dreq->thread); + + ptr = dreq->owner->pending_dns_requests; + lastnext = &dreq->owner->pending_dns_requests; + while (ptr) { + if (ptr == dreq) { + *lastnext = ptr->next; + break; + } else { + lastnext = &ptr->next; + ptr = ptr->next; + } + } + + free(dreq); +} + +static void AnodeSystemTransport__dns_thread_main(void *_dreq) +{ + struct AnodeSystemTransport__dns_request *dreq = (struct AnodeSystemTransport__dns_request *)_dreq; + + dreq->owner->interface.invoke((AnodeTransport *)dreq->owner,dreq,&AnodeSystemTransport__dns_invoke_on_completion); +} + +static void AnodeSystemTransport__do_close(struct AnodeSystemTransport *transport,struct AnodeSystemTransport_AnodeSocket *sock,const int error_code,const int generate_event) +{ + AnodeEvent evbuf; + int fd; + + if (sock->base.class_name == AnodeSystemTransport_CLASS) { +#ifdef ANODE_USE_SELECT + fd = (int)(transport->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx]); +#else + fd = transport->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx].fd; +#endif + + if ((sock->base.type == ANODE_SOCKET_STREAM_CONNECTION)&&(sock->base.state != ANODE_SOCKET_CLOSED)) { + sock->base.state = ANODE_SOCKET_CLOSED; + + if (generate_event) { + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_CLOSED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = error_code; + evbuf.data_length = 0; + evbuf.data = NULL; + + if (sock->base.event_handler) + sock->base.event_handler(&evbuf); + else if (transport->default_event_handler) + transport->default_event_handler(&evbuf); + } + } + + AnodeSystemTransport__close_socket(fd); + AnodeSystemTransport__remove_entry(transport,((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx); + +#ifdef ANODE_USE_SELECT + FD_CLR(sock,&THIS->readfds); + FD_CLR(sock,&THIS->writefds); +#endif + } else transport->base->close(transport->base,(AnodeSocket *)sock); +} + +static int AnodeSystemTransport__populate_network_endpoint(const struct sockaddr_storage *saddr,AnodeNetworkEndpoint *ep) +{ + switch(saddr->ss_family) { + case AF_INET: + ep->address.type = ANODE_NETWORK_ADDRESS_IPV4; + *((uint32_t *)ep->address.bits) = ((struct sockaddr_in *)saddr)->sin_addr.s_addr; + ep->port = ntohs(((struct sockaddr_in *)saddr)->sin_port); + return 1; + case AF_INET6: + ep->address.type = ANODE_NETWORK_ADDRESS_IPV6; + Anode_memcpy(ep->address.bits,((struct sockaddr_in6 *)saddr)->sin6_addr.s6_addr,16); + ep->port = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); + return 1; + } + return 0; +} + +/* ======================================================================== */ + +#ifdef THIS +#undef THIS +#endif +#define THIS ((struct AnodeSystemTransport *)transport) + +static void AnodeSystemTransport_invoke(AnodeTransport *transport, + void *ptr, + void (*func)(void *)) +{ + void *invoke_msg[2]; + + invoke_msg[0] = ptr; + invoke_msg[1] = (void *)func; + + AnodeMutex_lock(&THIS->invoke_pipe_m); + write(THIS->invoke_pipe[1],(void *)(&invoke_msg),sizeof(invoke_msg)); + AnodeMutex_unlock(&THIS->invoke_pipe_m); +} + +static void AnodeSystemTransport_dns_resolve(AnodeTransport *transport, + const char *name, + void (*event_handler)(const AnodeEvent *), + enum AnodeTransportDnsIncludeMode ipv4_include_mode, + enum AnodeTransportDnsIncludeMode ipv6_include_mode, + enum AnodeTransportDnsIncludeMode anode_include_mode) +{ + struct AnodeSystemTransport__dns_request *dreq = malloc(sizeof(struct AnodeSystemTransport__dns_request)); + + dreq->owner = THIS; + dreq->event_handler = event_handler; + Anode_str_copy(dreq->name,name,sizeof(dreq->name)); + dreq->ipv4_include_mode = ipv4_include_mode; + dreq->ipv6_include_mode = ipv6_include_mode; + dreq->anode_include_mode = anode_include_mode; + + dreq->address_count = 0; + dreq->error_code = 0; + + dreq->next = THIS->pending_dns_requests; + THIS->pending_dns_requests = dreq; + + dreq->thread = AnodeThread_create(&AnodeSystemTransport__dns_thread_main,dreq,0); +} + +static AnodeSocket *AnodeSystemTransport_datagram_listen(AnodeTransport *transport, + const AnodeNetworkAddress *local_address, + int local_port, + int *error_code) +{ + struct sockaddr_in sin4; + struct sockaddr_in6 sin6; + struct AnodeSystemTransport_AnodeSocket *sock; + unsigned int entry_idx; + int fd; + int tmp; + + switch(local_address->type) { + case ANODE_NETWORK_ADDRESS_IPV4: + fd = socket(AF_INET,SOCK_DGRAM,0); + if (fd <= 0) { + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + tmp = 1; + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&tmp,sizeof(tmp)); + fcntl(fd,F_SETFL,O_NONBLOCK); + + Anode_zero(&sin4,sizeof(struct sockaddr_in)); + sin4.sin_family = AF_INET; + sin4.sin_port = htons(local_port); + sin4.sin_addr.s_addr = *((uint32_t *)local_address->bits); + + if (bind(fd,(const struct sockaddr *)&sin4,sizeof(sin4))) { + AnodeSystemTransport__close_socket(fd); + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + break; + case ANODE_NETWORK_ADDRESS_IPV6: + fd = socket(AF_INET6,SOCK_DGRAM,0); + if (fd <= 0) { + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + tmp = 1; setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&tmp,sizeof(tmp)); + fcntl(fd,F_SETFL,O_NONBLOCK); +#ifdef IPV6_V6ONLY + tmp = 1; setsockopt(fd,IPPROTO_IPV6,IPV6_V6ONLY,&tmp,sizeof(tmp)); +#endif + + Anode_zero(&sin6,sizeof(struct sockaddr_in6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons(local_port); + Anode_memcpy(sin6.sin6_addr.s6_addr,local_address->bits,16); + + if (bind(fd,(const struct sockaddr *)&sin6,sizeof(sin6))) { + AnodeSystemTransport__close_socket(fd); + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + break; + default: + if (THIS->base) + return THIS->base->datagram_listen(THIS->base,local_address,local_port,error_code); + else { + *error_code = ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + return (AnodeSocket *)0; + } + } + + entry_idx = AnodeSystemTransport__add_entry(THIS); + sock = &(THIS->sockets[entry_idx]); + + sock->base.type = ANODE_SOCKET_DATAGRAM; + sock->base.state = ANODE_SOCKET_OPEN; + Anode_memcpy(&sock->base.endpoint.address,local_address,sizeof(AnodeNetworkAddress)); + sock->base.endpoint.port = local_port; + sock->base.class_name = AnodeSystemTransport_CLASS; + sock->base.user_ptr[0] = NULL; + sock->base.user_ptr[1] = NULL; + sock->base.event_handler = NULL; + sock->entry_idx = entry_idx; + + THIS->fds[entry_idx].fd = fd; + THIS->fds[entry_idx].events = POLLIN; + THIS->fds[entry_idx].revents = 0; + + *error_code = 0; + return (AnodeSocket *)sock; +} + +static AnodeSocket *AnodeSystemTransport_stream_listen(AnodeTransport *transport, + const AnodeNetworkAddress *local_address, + int local_port, + int *error_code) +{ + struct sockaddr_in sin4; + struct sockaddr_in6 sin6; + struct AnodeSystemTransport_AnodeSocket *sock; + unsigned int entry_idx; + int fd; + int tmp; + + switch(local_address->type) { + case ANODE_NETWORK_ADDRESS_IPV4: + fd = socket(AF_INET,SOCK_STREAM,0); + if (fd < 0) { + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + fcntl(fd,F_SETFL,O_NONBLOCK); + + Anode_zero(&sin4,sizeof(struct sockaddr_in)); + sin4.sin_family = AF_INET; + sin4.sin_port = htons(local_port); + sin4.sin_addr.s_addr = *((uint32_t *)local_address->bits); + + if (bind(fd,(const struct sockaddr *)&sin4,sizeof(sin4))) { + AnodeSystemTransport__close_socket(fd); + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + if (listen(fd,8)) { + AnodeSystemTransport__close_socket(fd); + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + break; + case ANODE_NETWORK_ADDRESS_IPV6: + fd = socket(AF_INET6,SOCK_STREAM,0); + if (fd < 0) { + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + fcntl(fd,F_SETFL,O_NONBLOCK); +#ifdef IPV6_V6ONLY + tmp = 1; setsockopt(fd,IPPROTO_IPV6,IPV6_V6ONLY,&tmp,sizeof(tmp)); +#endif + + Anode_zero(&sin6,sizeof(struct sockaddr_in6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons(local_port); + Anode_memcpy(sin6.sin6_addr.s6_addr,local_address->bits,16); + + if (bind(fd,(const struct sockaddr *)&sin6,sizeof(sin6))) { + AnodeSystemTransport__close_socket(fd); + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + if (listen(fd,8)) { + AnodeSystemTransport__close_socket(fd); + *error_code = ANODE_ERR_UNABLE_TO_BIND; + return (AnodeSocket *)0; + } + break; + default: + if (THIS->base) + return THIS->base->stream_listen(THIS->base,local_address,local_port,error_code); + else { + *error_code = ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + return (AnodeSocket *)0; + } + } + + entry_idx = AnodeSystemTransport__add_entry(THIS); + sock = &(THIS->sockets[entry_idx]); + + sock->base.type = ANODE_SOCKET_STREAM_LISTEN; + sock->base.state = ANODE_SOCKET_OPEN; + Anode_memcpy(&sock->base.endpoint.address,local_address,sizeof(AnodeNetworkAddress)); + sock->base.endpoint.port = local_port; + sock->base.class_name = AnodeSystemTransport_CLASS; + sock->base.user_ptr[0] = NULL; + sock->base.user_ptr[1] = NULL; + sock->base.event_handler = NULL; + sock->entry_idx = entry_idx; + + THIS->fds[entry_idx].fd = fd; + THIS->fds[entry_idx].events = POLLIN; + THIS->fds[entry_idx].revents = 0; + + *error_code = 0; + return (AnodeSocket *)sock; +} + +static int AnodeSystemTransport_datagram_send(AnodeTransport *transport, + AnodeSocket *sock, + const void *data, + int data_len, + const AnodeNetworkEndpoint *to_endpoint) +{ + struct sockaddr_in sin4; + struct sockaddr_in6 sin6; + +#ifdef ANODE_USE_SELECT + const int fd = (int)(THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx]); +#else + const int fd = THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx].fd; +#endif + + switch(to_endpoint->address.type) { + case ANODE_NETWORK_ADDRESS_IPV4: + Anode_zero(&sin4,sizeof(struct sockaddr_in)); + sin4.sin_family = AF_INET; + sin4.sin_port = htons((uint16_t)to_endpoint->port); + sin4.sin_addr.s_addr = *((uint32_t *)to_endpoint->address.bits); + sendto(fd,data,data_len,0,(struct sockaddr *)&sin4,sizeof(sin4)); + return 0; + case ANODE_NETWORK_ADDRESS_IPV6: + Anode_zero(&sin6,sizeof(struct sockaddr_in6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons((uint16_t)to_endpoint->port); + Anode_memcpy(sin6.sin6_addr.s6_addr,to_endpoint->address.bits,16); + sendto(fd,data,data_len,0,(struct sockaddr *)&sin6,sizeof(sin6)); + return 0; + default: + if (THIS->base) + return THIS->base->datagram_send(THIS->base,sock,data,data_len,to_endpoint); + else return ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + } +} + +static AnodeSocket *AnodeSystemTransport_stream_connect(AnodeTransport *transport, + const AnodeNetworkEndpoint *to_endpoint, + int *error_code) +{ + struct sockaddr_in sin4; + struct sockaddr_in6 sin6; + struct AnodeSystemTransport_AnodeSocket *sock; + unsigned int entry_idx; + int fd; + + switch(to_endpoint->address.type) { + case ANODE_NETWORK_ADDRESS_IPV4: + Anode_zero(&sin4,sizeof(struct sockaddr_in)); + sin4.sin_family = AF_INET; + sin4.sin_port = htons(to_endpoint->port); + sin4.sin_addr.s_addr = *((uint32_t *)to_endpoint->address.bits); + + fd = socket(AF_INET,SOCK_STREAM,0); + if (fd < 0) { + *error_code = ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + return (AnodeSocket *)0; + } + fcntl(fd,F_SETFL,O_NONBLOCK); + + if (connect(fd,(struct sockaddr *)&sin4,sizeof(sin4))) { + if (errno != EINPROGRESS) { + *error_code = ANODE_ERR_CONNECT_FAILED; + AnodeSystemTransport__close_socket(fd); + return (AnodeSocket *)0; + } + } + break; + case ANODE_NETWORK_ADDRESS_IPV6: + Anode_zero(&sin6,sizeof(struct sockaddr_in6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_port = htons(to_endpoint->port); + Anode_memcpy(sin6.sin6_addr.s6_addr,to_endpoint->address.bits,16); + + fd = socket(AF_INET6,SOCK_STREAM,0); + if (fd < 0) { + *error_code = ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + return (AnodeSocket *)0; + } + fcntl(fd,F_SETFL,O_NONBLOCK); + + if (connect(fd,(struct sockaddr *)&sin6,sizeof(sin6))) { + if (errno == EINPROGRESS) { + *error_code = ANODE_ERR_CONNECT_FAILED; + AnodeSystemTransport__close_socket(fd); + return (AnodeSocket *)0; + } + } + break; + default: + if (THIS->base) + return THIS->base->stream_connect(THIS->base,to_endpoint,error_code); + else { + *error_code = ANODE_ERR_ADDRESS_TYPE_NOT_SUPPORTED; + return (AnodeSocket *)0; + } + } + + entry_idx = AnodeSystemTransport__add_entry(THIS); + sock = &(THIS->sockets[entry_idx]); + + sock->base.type = ANODE_SOCKET_STREAM_CONNECTION; + sock->base.state = ANODE_SOCKET_CONNECTING; + Anode_memcpy(&sock->base.endpoint,to_endpoint,sizeof(AnodeNetworkEndpoint)); + sock->base.class_name = AnodeSystemTransport_CLASS; + sock->base.user_ptr[0] = NULL; + sock->base.user_ptr[1] = NULL; + sock->base.event_handler = NULL; + sock->entry_idx = entry_idx; + + THIS->fds[entry_idx].fd = fd; + THIS->fds[entry_idx].events = POLLIN|POLLOUT; + THIS->fds[entry_idx].revents = 0; + + return (AnodeSocket *)sock; +} + +static void AnodeSystemTransport_stream_start_writing(AnodeTransport *transport, + AnodeSocket *sock) +{ + if ((sock->type == ANODE_SOCKET_STREAM_CONNECTION)&&(((struct AnodeSystemTransport_AnodeSocket *)sock)->base.state == ANODE_SOCKET_OPEN)) { + if (sock->class_name == AnodeSystemTransport_CLASS) { +#ifdef ANODE_USE_SELECT + FD_SET((int)(THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx]),&THIS->writefds); +#else + THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx].events = (POLLIN|POLLOUT); +#endif + } else THIS->base->stream_start_writing(THIS->base,sock); + } +} + +static void AnodeSystemTransport_stream_stop_writing(AnodeTransport *transport, + AnodeSocket *sock) +{ + if ((sock->type == ANODE_SOCKET_STREAM_CONNECTION)&&(((struct AnodeSystemTransport_AnodeSocket *)sock)->base.state == ANODE_SOCKET_OPEN)) { + if (sock->class_name == AnodeSystemTransport_CLASS) { +#ifdef ANODE_USE_SELECT + FD_CLR((int)(THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx]),&THIS->writefds); +#else + THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx].events = POLLIN; +#endif + } else THIS->base->stream_stop_writing(THIS->base,sock); + } +} + +static int AnodeSystemTransport_stream_send(AnodeTransport *transport, + AnodeSocket *sock, + const void *data, + int data_len) +{ + int result; + + if (sock->type == ANODE_SOCKET_STREAM_CONNECTION) { + if (sock->class_name == AnodeSystemTransport_CLASS) { + if (((struct AnodeSystemTransport_AnodeSocket *)sock)->base.state != ANODE_SOCKET_OPEN) + return ANODE_ERR_CONNECTION_CLOSED; + +#ifdef ANODE_USE_SELECT + result = send((int)(THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx]),data,data_len,0); +#else + result = send(THIS->fds[((struct AnodeSystemTransport_AnodeSocket *)sock)->entry_idx].fd,data,data_len,0); +#endif + + if (result >= 0) + return result; + else { + AnodeSystemTransport__do_close(THIS,(struct AnodeSystemTransport_AnodeSocket *)sock,ANODE_ERR_CONNECTION_CLOSED_BY_REMOTE,1); + return ANODE_ERR_CONNECTION_CLOSED; + } + } else return THIS->base->stream_send(THIS->base,sock,data,data_len); + } else return ANODE_ERR_INVALID_ARGUMENT; +} + +static void AnodeSystemTransport_close(AnodeTransport *transport, + AnodeSocket *sock) +{ + AnodeSystemTransport__do_close(THIS,(struct AnodeSystemTransport_AnodeSocket *)sock,0,1); +} + +static void AnodeSystemTransport__poll_do_read_datagram(struct AnodeSystemTransport *transport,int fd,struct AnodeSystemTransport_AnodeSocket *sock) +{ + char buf[16384]; + struct sockaddr_storage fromaddr; + AnodeNetworkEndpoint tmp_ep; + AnodeEvent evbuf; + socklen_t addrlen; + int n; + + addrlen = sizeof(struct sockaddr_storage); + n = recvfrom(fd,buf,sizeof(buf),0,(struct sockaddr *)&fromaddr,&addrlen); + if ((n >= 0)&&(AnodeSystemTransport__populate_network_endpoint(&fromaddr,&tmp_ep))) { + evbuf.type = ANODE_TRANSPORT_EVENT_DATAGRAM_RECEIVED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = &tmp_ep; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = 0; + evbuf.data_length = n; + evbuf.data = buf; + + if (sock->base.event_handler) + sock->base.event_handler(&evbuf); + else if (transport->default_event_handler) + transport->default_event_handler(&evbuf); + } +} + +static void AnodeSystemTransport__poll_do_accept_incoming_connection(struct AnodeSystemTransport *transport,int fd,struct AnodeSystemTransport_AnodeSocket *sock) +{ + struct sockaddr_storage fromaddr; + AnodeNetworkEndpoint tmp_ep; + AnodeEvent evbuf; + struct AnodeSystemTransport_AnodeSocket *newsock; + socklen_t addrlen; + int n; + unsigned int entry_idx; + + addrlen = sizeof(struct sockaddr_storage); + n = accept(fd,(struct sockaddr *)&fromaddr,&addrlen); + if ((n >= 0)&&(AnodeSystemTransport__populate_network_endpoint(&fromaddr,&tmp_ep))) { + entry_idx = AnodeSystemTransport__add_entry(transport); + newsock = &(transport->sockets[entry_idx]); + + newsock->base.type = ANODE_SOCKET_STREAM_CONNECTION; + newsock->base.state = ANODE_SOCKET_OPEN; + Anode_memcpy(&newsock->base.endpoint,&tmp_ep,sizeof(AnodeNetworkEndpoint)); + newsock->base.class_name = AnodeSystemTransport_CLASS; + newsock->base.user_ptr[0] = NULL; + newsock->base.user_ptr[1] = NULL; + newsock->base.event_handler = NULL; + newsock->entry_idx = entry_idx; + + THIS->fds[entry_idx].fd = n; + THIS->fds[entry_idx].events = POLLIN; + THIS->fds[entry_idx].revents = 0; + + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_INCOMING_CONNECT; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)newsock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = 0; + evbuf.data_length = 0; + evbuf.data = NULL; + + if (sock->base.event_handler) + sock->base.event_handler(&evbuf); + else if (transport->default_event_handler) + transport->default_event_handler(&evbuf); + } +} + +static void AnodeSystemTransport__poll_do_read_stream(struct AnodeSystemTransport *transport,int fd,struct AnodeSystemTransport_AnodeSocket *sock) +{ + char buf[65536]; + AnodeEvent evbuf; + int n; + + n = recv(fd,buf,sizeof(buf),0); + if (n > 0) { + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_DATA_RECEIVED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = 0; + evbuf.data_length = n; + evbuf.data = buf; + + if (sock->base.event_handler) + sock->base.event_handler(&evbuf); + else if (transport->default_event_handler) + transport->default_event_handler(&evbuf); + } else AnodeSystemTransport__do_close(transport,sock,ANODE_ERR_CONNECTION_CLOSED_BY_REMOTE,1); +} + +static void AnodeSystemTransport__poll_do_stream_available_for_write(struct AnodeSystemTransport *transport,int fd,struct AnodeSystemTransport_AnodeSocket *sock) +{ + AnodeEvent evbuf; + + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_DATA_RECEIVED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = 0; + evbuf.data_length = 0; + evbuf.data = NULL; + + if (sock->base.event_handler) + sock->base.event_handler(&evbuf); + else if (transport->default_event_handler) + transport->default_event_handler(&evbuf); +} + +static void AnodeSystemTransport__poll_do_outgoing_connect(struct AnodeSystemTransport *transport,int fd,struct AnodeSystemTransport_AnodeSocket *sock) +{ + AnodeEvent evbuf; + int err_code; + socklen_t optlen; + + optlen = sizeof(err_code); + if (getsockopt(fd,SOL_SOCKET,SO_ERROR,(void *)&err_code,&optlen)) { + /* Error getting result, so we assume a failure */ + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_FAILED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = ANODE_ERR_CONNECT_FAILED; + evbuf.data_length = 0; + evbuf.data = NULL; + + AnodeSystemTransport__do_close(transport,sock,0,0); + } else if (err_code) { + /* Error code is nonzero, so connect failed */ + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_FAILED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = ANODE_ERR_CONNECT_FAILED; + evbuf.data_length = 0; + evbuf.data = NULL; + + AnodeSystemTransport__do_close(transport,sock,0,0); + } else { + /* Connect succeeded */ + evbuf.type = ANODE_TRANSPORT_EVENT_STREAM_OUTGOING_CONNECT_ESTABLISHED; + evbuf.transport = (AnodeTransport *)transport; + evbuf.sock = (AnodeSocket *)sock; + evbuf.datagram_from = NULL; + evbuf.dns_name = NULL; + evbuf.dns_addresses = NULL; + evbuf.dns_address_count = 0; + evbuf.error_code = 0; + evbuf.data_length = 0; + evbuf.data = NULL; + } + + if (sock->base.event_handler) + sock->base.event_handler(&evbuf); + else if (transport->default_event_handler) + transport->default_event_handler(&evbuf); +} + +static int AnodeSystemTransport_poll(AnodeTransport *transport) +{ + int timeout = -1; + unsigned int fd_idx; + int event_count = 0; + int n; + + if (poll((struct pollfd *)THIS->fds,THIS->fd_count,timeout) > 0) { + for(fd_idx=0;fd_idxfd_count;++fd_idx) { + if ((THIS->fds[fd_idx].revents & (POLLERR|POLLHUP|POLLNVAL))) { + if (THIS->sockets[fd_idx].base.type == ANODE_SOCKET_STREAM_CONNECTION) { + if (THIS->sockets[fd_idx].base.state == ANODE_SOCKET_CONNECTING) + AnodeSystemTransport__poll_do_outgoing_connect(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + else AnodeSystemTransport__do_close(THIS,&THIS->sockets[fd_idx],ANODE_ERR_CONNECTION_CLOSED_BY_REMOTE,1); + ++event_count; + } + } else { + if ((THIS->fds[fd_idx].revents & POLLIN)) { + if (THIS->fds[fd_idx].fd == THIS->invoke_pipe[0]) { + n = read(THIS->invoke_pipe[0],&(((unsigned char *)(&(THIS->invoke_pipe_buf)))[THIS->invoke_pipe_buf_ptr]),sizeof(THIS->invoke_pipe_buf) - THIS->invoke_pipe_buf_ptr); + if (n > 0) { + THIS->invoke_pipe_buf_ptr += (unsigned int)n; + if (THIS->invoke_pipe_buf_ptr >= sizeof(THIS->invoke_pipe_buf)) { + THIS->invoke_pipe_buf_ptr -= sizeof(THIS->invoke_pipe_buf); + ((void (*)(void *))(THIS->invoke_pipe_buf[1]))(THIS->invoke_pipe_buf[0]); + } + } + } else { + switch(THIS->sockets[fd_idx].base.type) { + case ANODE_SOCKET_DATAGRAM: + AnodeSystemTransport__poll_do_read_datagram(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + break; + case ANODE_SOCKET_STREAM_LISTEN: + AnodeSystemTransport__poll_do_accept_incoming_connection(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + break; + case ANODE_SOCKET_STREAM_CONNECTION: + if (THIS->sockets[fd_idx].base.state == ANODE_SOCKET_CONNECTING) + AnodeSystemTransport__poll_do_outgoing_connect(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + else AnodeSystemTransport__poll_do_read_stream(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + break; + } + ++event_count; + } + } + + if ((THIS->fds[fd_idx].revents & POLLOUT)) { + if (THIS->sockets[fd_idx].base.state == ANODE_SOCKET_CONNECTING) + AnodeSystemTransport__poll_do_outgoing_connect(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + else AnodeSystemTransport__poll_do_stream_available_for_write(THIS,THIS->fds[fd_idx].fd,&THIS->sockets[fd_idx]); + ++event_count; + } + } + } + } + + return event_count; +} + +static int AnodeSystemTransport_supports_address_type(const AnodeTransport *transport, + enum AnodeNetworkAddressType at) +{ + switch(at) { + case ANODE_NETWORK_ADDRESS_IPV4: + return 1; + case ANODE_NETWORK_ADDRESS_IPV6: + return 1; + default: + if (THIS->base) + return THIS->base->supports_address_type(THIS->base,at); + return 0; + } +} + +static AnodeTransport *AnodeSystemTransport_base_instance(const AnodeTransport *transport) +{ + return THIS->base; +} + +static const char *AnodeSystemTransport_class_name(AnodeTransport *transport) +{ + return AnodeSystemTransport_CLASS; +} + +static void AnodeSystemTransport_delete(AnodeTransport *transport) +{ + close(THIS->invoke_pipe[0]); + close(THIS->invoke_pipe[1]); + + AnodeMutex_destroy(&THIS->invoke_pipe_m); + + if (THIS->fds) free(THIS->fds); + if (THIS->sockets) free(THIS->sockets); + + if (THIS->base) THIS->base->delete(THIS->base); + + free(transport); +} + +/* ======================================================================== */ + +AnodeTransport *AnodeSystemTransport_new(AnodeTransport *base) +{ + struct AnodeSystemTransport *t; + unsigned int entry_idx; + + t = malloc(sizeof(struct AnodeSystemTransport)); + if (!t) return (AnodeTransport *)0; + Anode_zero(t,sizeof(struct AnodeSystemTransport)); + + t->interface.invoke = &AnodeSystemTransport_invoke; + t->interface.dns_resolve = &AnodeSystemTransport_dns_resolve; + t->interface.datagram_listen = &AnodeSystemTransport_datagram_listen; + t->interface.stream_listen = &AnodeSystemTransport_stream_listen; + t->interface.datagram_send = &AnodeSystemTransport_datagram_send; + t->interface.stream_connect = &AnodeSystemTransport_stream_connect; + t->interface.stream_start_writing = &AnodeSystemTransport_stream_start_writing; + t->interface.stream_stop_writing = &AnodeSystemTransport_stream_stop_writing; + t->interface.stream_send = &AnodeSystemTransport_stream_send; + t->interface.close = &AnodeSystemTransport_close; + t->interface.poll = &AnodeSystemTransport_poll; + t->interface.supports_address_type = &AnodeSystemTransport_supports_address_type; + t->interface.base_instance = &AnodeSystemTransport_base_instance; + t->interface.class_name = &AnodeSystemTransport_class_name; + t->interface.delete = &AnodeSystemTransport_delete; + + t->base = base; + + pipe(t->invoke_pipe); + fcntl(t->invoke_pipe[0],F_SETFL,O_NONBLOCK); + entry_idx = AnodeSystemTransport__add_entry(t); + t->fds[entry_idx].fd = t->invoke_pipe[0]; + t->fds[entry_idx].events = POLLIN; + t->fds[entry_idx].revents = 0; + AnodeMutex_init(&t->invoke_pipe_m); + + return (AnodeTransport *)t; +} diff --git a/attic/historic/anode/libanode/tests/Makefile b/attic/historic/anode/libanode/tests/Makefile new file mode 100644 index 00000000..a479092c --- /dev/null +++ b/attic/historic/anode/libanode/tests/Makefile @@ -0,0 +1,25 @@ +all: force clean anode-utils-test anode-zone-test aes-test ec-test + +aes-test: + gcc -Wall -O6 -ftree-vectorize -std=c99 -o aes-test aes-test.c ../aes_digest.c -lcrypto + +http_client-test: + gcc -O0 -g -std=c99 -o http_client-test http_client-test.c ../anode-utils.c ../misc.c ../http_client.c ../dictionary.c ../iptransport.c ../anode-transport.c -lcrypto + +anode-utils-test: + gcc -O0 -g -std=c99 -o anode-utils-test anode-utils-test.c ../anode-utils.c ../misc.c + +ec-test: + gcc -O0 -g -std=c99 -o ec-test ec-test.c ../impl/ec.c ../impl/misc.c -lcrypto + +anode-zone-test: + gcc -O0 -g -std=c99 -o anode-zone-test anode-zone-test.c ../anode-zone.c ../http_client.c ../dictionary.c ../misc.c ../anode-transport.c ../iptransport.c ../environment.c + +system_transport-test: + gcc -O0 -g -std=c99 -o system_transport-test system_transport-test.c ../system_transport.c ../network_address.c ../address.c ../aes_digest.c ../impl/misc.c ../impl/thread.c ../impl/dns_txt.c ../impl/aes.c -lresolv -lcrypto + +clean: force + rm -rf *.dSYM + rm -f http_client-test anode-utils-test anode-zone-test ec-test aes-test system_transport-test + +force: ; diff --git a/attic/historic/anode/libanode/tests/aes-test.c b/attic/historic/anode/libanode/tests/aes-test.c new file mode 100644 index 00000000..bca63b89 --- /dev/null +++ b/attic/historic/anode/libanode/tests/aes-test.c @@ -0,0 +1,191 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include +#include "../impl/aes.h" +#include "../anode.h" + +static const unsigned char AES_TEST_KEY[32] = { + 0x08,0x09,0x0A,0x0B,0x0D,0x0E,0x0F,0x10,0x12,0x13,0x14,0x15,0x17,0x18,0x19,0x1A, + 0x1C,0x1D,0x1E,0x1F,0x21,0x22,0x23,0x24,0x26,0x27,0x28,0x29,0x2B,0x2C,0x2D,0x2E +}; +static const unsigned char AES_TEST_IN[16] = { + 0x06,0x9A,0x00,0x7F,0xC7,0x6A,0x45,0x9F,0x98,0xBA,0xF9,0x17,0xFE,0xDF,0x95,0x21 +}; +static const unsigned char AES_TEST_OUT[16] = { + 0x08,0x0e,0x95,0x17,0xeb,0x16,0x77,0x71,0x9a,0xcf,0x72,0x80,0x86,0x04,0x0a,0xe3 +}; + +static const unsigned char CMAC_TEST_KEY[32] = { + 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81, + 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 +}; + +static const unsigned char CMAC_TEST1_OUT[16] = { + 0x02,0x89,0x62,0xf6,0x1b,0x7b,0xf8,0x9e,0xfc,0x6b,0x55,0x1f,0x46,0x67,0xd9,0x83 +}; + +static const unsigned char CMAC_TEST2_IN[16] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a +}; +static const unsigned char CMAC_TEST2_OUT[16] = { + 0x28,0xa7,0x02,0x3f,0x45,0x2e,0x8f,0x82,0xbd,0x4b,0xf2,0x8d,0x8c,0x37,0xc3,0x5c +}; + +static const unsigned char CMAC_TEST3_IN[40] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11 +}; +static const unsigned char CMAC_TEST3_OUT[16] = { + 0xaa,0xf3,0xd8,0xf1,0xde,0x56,0x40,0xc2,0x32,0xf5,0xb1,0x69,0xb9,0xc9,0x11,0xe6 +}; + +static const unsigned char CMAC_TEST4_IN[64] = { + 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a, + 0xae,0x2d,0x8a,0x57,0x1e,0x03,0xac,0x9c,0x9e,0xb7,0x6f,0xac,0x45,0xaf,0x8e,0x51, + 0x30,0xc8,0x1c,0x46,0xa3,0x5c,0xe4,0x11,0xe5,0xfb,0xc1,0x19,0x1a,0x0a,0x52,0xef, + 0xf6,0x9f,0x24,0x45,0xdf,0x4f,0x9b,0x17,0xad,0x2b,0x41,0x7b,0xe6,0x6c,0x37,0x10 +}; +static const unsigned char CMAC_TEST4_OUT[16] = { + 0xe1,0x99,0x21,0x90,0x54,0x9f,0x6e,0xd5,0x69,0x6a,0x2c,0x05,0x6c,0x31,0x54,0x10 +}; + +static void test_cmac(const AnodeAesExpandedKey *expkey,const unsigned char *in,unsigned int inlen,const unsigned char *expected) +{ + unsigned int i; + unsigned char out[16]; + + printf("Testing CMAC with %u byte input:\n",inlen); + printf(" IN: "); + for(i=0;i + * + * 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 . */ + +#include +#include +#include "../anode.h" +#include "../misc.h" + +int main(int argc,char **argv) +{ + unsigned char test[10005]; + unsigned int i; + AnodeSecureRandom srng; + + AnodeSecureRandom_init(&srng); + + AnodeSecureRandom_gen_bytes(&srng,test,sizeof(test)); + + for(i=0;i + * + * 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 . */ + +#include +#include +#include "../anode.h" +#include "../misc.h" + +static const char *testuris[22] = { + "http://www.test.com", + "http://www.test.com/", + "http://www.test.com/path/to/something", + "http://user@www.test.com", + "http://user@www.test.com/path/to/something", + "http://user:password@www.test.com/path/to/something", + "http://www.test.com/path/to/something?query=foo&bar=baz", + "http://www.test.com/path/to/something#fragment", + "http://www.test.com/path/to/something?query=foo&bar=baz#fragment", + "http://user:password@www.test.com/path/to/something#fragment", + "http://user:password@www.test.com/path/to/something?query=foo&bar=baz#fragment", + "http://@www.test.com/", + "http://:@www.test.com/", + "http://www.test.com:8080/path/to/something", + "http://user:password@www.test.com:8080/path/to/something?query=foo#fragment", + "http://", + "http://www.test.com/path/to/something?#", + "http://www.test.com/path/to/something?#fragment", + "http:", + "http", + "mailto:this_is_a_urn@somedomain.com", + "" +}; + +int main(int argc,char **argv) +{ + int i,r; + char reconstbuf[2048]; + char *reconst; + AnodeURI uri; + + for(i=0;i<22;++i) { + printf("\"%s\":\n",testuris[i]); + r = AnodeURI_parse(&uri,testuris[i]); + if (r) { + printf(" error: %d\n",r); + } else { + printf(" scheme: %s\n",uri.scheme); + printf(" username: %s\n",uri.username); + printf(" password: %s\n",uri.password); + printf(" host: %s\n",uri.host); + printf(" port: %d\n",uri.port); + printf(" path: %s\n",uri.path); + printf(" query: %s\n",uri.query); + printf(" fragment: %s\n",uri.fragment); + } + reconst = AnodeURI_to_string(&uri,reconstbuf,sizeof(reconstbuf)); + printf("Reconstituted URI: %s\n",reconst ? reconst : "(null)"); + printf("\n"); + } + + return 0; +} diff --git a/attic/historic/anode/libanode/tests/anode-zone-test.c b/attic/historic/anode/libanode/tests/anode-zone-test.c new file mode 100644 index 00000000..08396716 --- /dev/null +++ b/attic/historic/anode/libanode/tests/anode-zone-test.c @@ -0,0 +1,47 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include "../anode.h" +#include "../dictionary.h" + +static int got_it = 0; + +static void zone_lookup_handler(void *ptr,long zone_id,AnodeZone *zone) +{ + if (zone) + printf("got %.8lx: %d entries\n",(unsigned long)zone_id & 0xffffffff,((struct AnodeDictionary *)zone)->size); + else printf("failed.\n"); + got_it = 1; +} + +int main(int argc,char **argv) +{ + AnodeTransportEngine transport; + + Anode_init_ip_transport_engine(&transport); + + AnodeZone_lookup(&transport,0,0,&zone_lookup_handler); + + while (!got_it) + transport.poll(&transport); + + transport.destroy(&transport); + + return 0; +} diff --git a/attic/historic/anode/libanode/tests/dictionary-test.c b/attic/historic/anode/libanode/tests/dictionary-test.c new file mode 100644 index 00000000..12a5fb2f --- /dev/null +++ b/attic/historic/anode/libanode/tests/dictionary-test.c @@ -0,0 +1,149 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include +#include "../dictionary.h" + +static const char *HASH_TESTS[16] = { + "test", + "testt", + "", + "foo", + "fooo", + "1", + "2", + "3", + "4", + "11", + "22", + "33", + "44", + "adklfjklejrer", + "erngnetbekjrq", + "erklerqqqqre" +}; + +int diterate(void *arg,const char *key,const char *value) +{ + printf(" %s: %s\n",key ? key : "(null)",value ? value : "(null)"); + return 1; +} + +int main(int argc,char **argv) +{ + char tmp[1024]; + char fuzzparam1[16],fuzzparam2[16],fuzzparam3[16]; + struct AnodeDictionary d; + unsigned int i,j,k,cs; + + srandom(time(0)); + + printf("Trying out hash function a little...\n"); + for(i=0;i<16;++i) + printf(" %s: %u\n",HASH_TESTS[i],(unsigned int)AnodeDictionary__get_bucket(HASH_TESTS[i])); + + for(cs=0;cs<2;++cs) { + printf("\nTesting with case sensitivity = %d\n",cs); + AnodeDictionary_init(&d,cs); + + printf("\nTesting dictionary by adding and retrieving some keys...\n"); + AnodeDictionary_put(&d,"test1","This is the first test"); + AnodeDictionary_put(&d,"test2","This is the second test"); + AnodeDictionary_put(&d,"test3","This is the third test (lower case)"); + AnodeDictionary_put(&d,"TEST3","This is the third test (UPPER CASE)"); + AnodeDictionary_iterate(&d,(void *)0,&diterate); + if (d.size != (cs ? 4 : 3)) { + printf("Failed (size).\n"); + return 1; + } + + AnodeDictionary_clear(&d); + if (d.size||(AnodeDictionary_get(&d,"test1"))) { + printf("Failed (clear).\n"); + return 1; + } + + printf("\nTesting read, trial 1: simple key=value with unterminated line\n"); + strcpy(tmp,"foo=bar\nbar=baz\ntest1=Happy happy joyjoy!\ntest2=foobarbaz\nlinewithnocr=thisworked"); + AnodeDictionary_read(&d,tmp,"\r\n","=","",'\\',0,0); + printf("Results:\n"); + AnodeDictionary_iterate(&d,(void *)0,&diterate); + AnodeDictionary_clear(&d); + + printf("\nTesting read, trial 2: key=value with escape chars, escaped CRs\n"); + strcpy(tmp,"foo=bar\r\nbar==baz\nte\\=st1=\\=Happy happy joyjoy!\ntest2=foobarbaz\\\nfoobarbaz on next line\r\n"); + AnodeDictionary_read(&d,tmp,"\r\n","=","",'\\',0,0); + printf("Results:\n"); + AnodeDictionary_iterate(&d,(void *)0,&diterate); + AnodeDictionary_clear(&d); + + printf("\nTesting read, trial 3: HTTP header-like dictionary\n"); + strcpy(tmp,"Host: some.host.net\r\nX-Some-Header: foo bar\r\nX-Some-Other-Header: y0y0y0y0y0\r\n"); + AnodeDictionary_read(&d,tmp,"\r\n",": ","",0,0,0); + printf("Results:\n"); + AnodeDictionary_iterate(&d,(void *)0,&diterate); + AnodeDictionary_clear(&d); + + printf("\nTesting read, trial 4: single line key/value\n"); + strcpy(tmp,"Header: one line only"); + AnodeDictionary_read(&d,tmp,"\r\n",": ","",0,0,0); + printf("Results:\n"); + AnodeDictionary_iterate(&d,(void *)0,&diterate); + AnodeDictionary_clear(&d); + + printf("\nFuzzing dictionary reader...\n"); fflush(stdout); + for(i=0;i<200000;++i) { + j = random() % (sizeof(tmp) - 1); + for(k=0;k> 3); + if (!tmp[k]) tmp[k] = 1; + } + tmp[j] = (char)0; + + j = random() % (sizeof(fuzzparam1) - 1); + for(k=0;k> 3); + if (!fuzzparam1[k]) fuzzparam1[k] = 1; + } + fuzzparam1[j] = (char)0; + + j = random() % (sizeof(fuzzparam2) - 1); + for(k=0;k> 3); + if (!fuzzparam2[k]) fuzzparam2[k] = 1; + } + fuzzparam2[j] = (char)0; + + j = random() % (sizeof(fuzzparam3) - 1); + for(k=0;k> 3); + if (!fuzzparam3[k]) fuzzparam3[k] = 1; + } + fuzzparam3[j] = (char)0; + + AnodeDictionary_read(&d,tmp,fuzzparam1,fuzzparam2,fuzzparam3,random() & 3,random() & 1,random() & 1); + AnodeDictionary_clear(&d); + } + + AnodeDictionary_destroy(&d); + } + + return 0; +} diff --git a/attic/historic/anode/libanode/tests/ec-test.c b/attic/historic/anode/libanode/tests/ec-test.c new file mode 100644 index 00000000..49f04265 --- /dev/null +++ b/attic/historic/anode/libanode/tests/ec-test.c @@ -0,0 +1,97 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include "../impl/ec.h" +#include "../impl/misc.h" + +#define TEST_KEY_LEN 128 +#define AnodeEC_key_to_hex(k,b,l) Anode_to_hex((k)->key,(k)->bytes,(b),l) + +int main(int argc,char **argv) +{ + struct AnodeECKeyPair pair1; + struct AnodeECKeyPair pair2; + struct AnodeECKeyPair pair3; + unsigned char key[TEST_KEY_LEN]; + char str[16384]; + + printf("Creating key pair #1...\n"); + if (!AnodeECKeyPair_generate(&pair1)) { + printf("Could not create key pair.\n"); + return 1; + } + AnodeEC_key_to_hex(&pair1.pub,str,sizeof(str)); + printf("Public: %s\n",str); + AnodeEC_key_to_hex(&pair1.priv,str,sizeof(str)); + printf("Private: %s\n\n",str); + + printf("Creating key pair #2...\n"); + if (!AnodeECKeyPair_generate(&pair2)) { + printf("Could not create key pair.\n"); + return 1; + } + AnodeEC_key_to_hex(&pair2.pub,str,sizeof(str)); + printf("Public: %s\n",str); + AnodeEC_key_to_hex(&pair2.priv,str,sizeof(str)); + printf("Private: %s\n\n",str); + + printf("Key agreement between public #2 and private #1...\n"); + if (!AnodeECKeyPair_agree(&pair1,&pair2.pub,key,TEST_KEY_LEN)) { + printf("Agreement failed.\n"); + return 1; + } + Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str)); + printf("Agreed secret: %s\n\n",str); + + printf("Key agreement between public #1 and private #2...\n"); + if (!AnodeECKeyPair_agree(&pair2,&pair1.pub,key,TEST_KEY_LEN)) { + printf("Agreement failed.\n"); + return 1; + } + Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str)); + printf("Agreed secret: %s\n\n",str); + + printf("Testing key pair init function (init #3 from #2's parts)...\n"); + if (!AnodeECKeyPair_init(&pair3,&(pair2.pub),&(pair2.priv))) { + printf("Init failed.\n"); + return 1; + } + + printf("Key agreement between public #1 and private #3...\n"); + if (!AnodeECKeyPair_agree(&pair3,&pair1.pub,key,TEST_KEY_LEN)) { + printf("Agreement failed.\n"); + return 1; + } + Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str)); + printf("Agreed secret: %s\n\n",str); + + printf("Key agreement between public #1 and private #1...\n"); + if (!AnodeECKeyPair_agree(&pair1,&pair1.pub,key,TEST_KEY_LEN)) { + printf("Agreement failed.\n"); + return 1; + } + Anode_to_hex(key,TEST_KEY_LEN,str,sizeof(str)); + printf("Agreed secret (should not match): %s\n\n",str); + + AnodeECKeyPair_destroy(&pair1); + AnodeECKeyPair_destroy(&pair2); + AnodeECKeyPair_destroy(&pair3); + + return 0; +} diff --git a/attic/historic/anode/libanode/tests/environment-test.c b/attic/historic/anode/libanode/tests/environment-test.c new file mode 100644 index 00000000..c481a129 --- /dev/null +++ b/attic/historic/anode/libanode/tests/environment-test.c @@ -0,0 +1,28 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include "../environment.h" + +int main(int argc,char **argv) +{ + const char *cache = Anode_get_cache(); + + printf("Cache folder: %s\n",cache ? cache : "(null)"); + + return 0; +} diff --git a/attic/historic/anode/libanode/tests/http_client-test.c b/attic/historic/anode/libanode/tests/http_client-test.c new file mode 100644 index 00000000..e1f93967 --- /dev/null +++ b/attic/historic/anode/libanode/tests/http_client-test.c @@ -0,0 +1,233 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include "../anode.h" +#include "../misc.h" +#include "../http_client.h" +#include "../dictionary.h" + +struct TestCase +{ + int method; + AnodeURI uri; + const void *client_data; + unsigned int client_data_len; + const char *expected_sha1; + char actual_sha1[64]; + int got_it; + int keepalive; + struct TestCase *next; +}; + +#define NUM_TEST_CASES 7 +static struct TestCase test_cases[NUM_TEST_CASES]; + +static void init_test_cases(int keepalive) +{ + AnodeURI_parse(&(test_cases[0].uri),"http://zerotier.com/for_unit_tests/test1.txt"); + test_cases[0].method = ANODE_HTTP_GET; + test_cases[0].client_data_len = 0; + test_cases[0].expected_sha1 = "0828324174b10cc867b7255a84a8155cf89e1b8b"; + test_cases[0].actual_sha1[0] = (char)0; + test_cases[0].got_it = 0; + test_cases[0].keepalive = keepalive; + test_cases[0].next = &(test_cases[1]); + + AnodeURI_parse(&(test_cases[1].uri),"http://zerotier.com/for_unit_tests/test2.bin"); + test_cases[1].method = ANODE_HTTP_GET; + test_cases[1].client_data_len = 0; + test_cases[1].expected_sha1 = "6b67c635786ab52666211d02412c0d0f0372980d"; + test_cases[1].actual_sha1[0] = (char)0; + test_cases[1].got_it = 0; + test_cases[1].keepalive = keepalive; + test_cases[1].next = &(test_cases[2]); + + AnodeURI_parse(&(test_cases[2].uri),"http://zerotier.com/for_unit_tests/test3.bin"); + test_cases[2].method = ANODE_HTTP_GET; + test_cases[2].client_data_len = 0; + test_cases[2].expected_sha1 = "efa7722029fdbb6abd0e3ed32a0b44bfb982cff0"; + test_cases[2].actual_sha1[0] = (char)0; + test_cases[2].got_it = 0; + test_cases[2].keepalive = keepalive; + test_cases[2].next = &(test_cases[3]); + + AnodeURI_parse(&(test_cases[3].uri),"http://zerotier.com/for_unit_tests/test4.bin"); + test_cases[3].method = ANODE_HTTP_GET; + test_cases[3].client_data_len = 0; + test_cases[3].expected_sha1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709"; + test_cases[3].actual_sha1[0] = (char)0; + test_cases[3].got_it = 0; + test_cases[3].keepalive = keepalive; + test_cases[3].next = &(test_cases[4]); + + AnodeURI_parse(&(test_cases[4].uri),"http://zerotier.com/for_unit_tests/echo.php?echo=foobar"); + test_cases[4].method = ANODE_HTTP_GET; + test_cases[4].client_data_len = 0; + test_cases[4].expected_sha1 = "8843d7f92416211de9ebb963ff4ce28125932878"; + test_cases[4].actual_sha1[0] = (char)0; + test_cases[4].got_it = 0; + test_cases[4].keepalive = keepalive; + test_cases[4].next = &(test_cases[5]); + + AnodeURI_parse(&(test_cases[5].uri),"http://zerotier.com/for_unit_tests/echo.php"); + test_cases[5].method = ANODE_HTTP_POST; + test_cases[5].client_data = "echo=foobar"; + test_cases[5].client_data_len = strlen((char *)test_cases[5].client_data); + test_cases[5].expected_sha1 = "8843d7f92416211de9ebb963ff4ce28125932878"; + test_cases[5].actual_sha1[0] = (char)0; + test_cases[5].got_it = 0; + test_cases[5].keepalive = keepalive; + test_cases[5].next = &(test_cases[6]); + + AnodeURI_parse(&(test_cases[6].uri),"http://zerotier.com/for_unit_tests/test3.bin"); + test_cases[6].method = ANODE_HTTP_HEAD; + test_cases[6].client_data_len = 0; + test_cases[6].expected_sha1 = "da39a3ee5e6b4b0d3255bfef95601890afd80709"; + test_cases[6].actual_sha1[0] = (char)0; + test_cases[6].got_it = 0; + test_cases[6].keepalive = keepalive; + test_cases[6].next = 0; +} + +static int http_handler_dump_headers(void *arg,const char *key,const char *value) +{ + printf(" H %s: %s\n",key,value); + return 1; +} + +static void http_handler(struct AnodeHttpClient *client) +{ + const char *method = "???"; + char buf[1024]; + unsigned char sha[20]; + struct TestCase *test = (struct TestCase *)client->ptr[0]; + + switch(client->method) { + case ANODE_HTTP_GET: + method = "GET"; + break; + case ANODE_HTTP_HEAD: + method = "HEAD"; + break; + case ANODE_HTTP_POST: + method = "POST"; + break; + } + + if (client->response.code == 200) { + SHA1((unsigned char *)client->response.data,client->response.data_length,sha); + Anode_to_hex(sha,20,test->actual_sha1,sizeof(test->actual_sha1)); + printf("%s %s\n * SHA1: %s exp: %s\n",method,AnodeURI_to_string(&(test->uri),buf,sizeof(buf)),test->actual_sha1,test->expected_sha1); + if (strcmp(test->actual_sha1,test->expected_sha1)) + printf(" ! SHA1 MISMATCH!\n"); + AnodeDictionary_iterate(&(client->response.headers),0,&http_handler_dump_headers); + } else printf("%s %s: ERROR: %d\n",method,AnodeURI_to_string(&(test->uri),buf,sizeof(buf)),client->response.code); + + test->got_it = 1; + + if (!test->keepalive) + AnodeHttpClient_free(client); + else { + test = test->next; + if (test) { + memcpy((void *)&(client->uri),(const void *)&(test->uri),sizeof(AnodeURI)); + + client->data = test->client_data; + client->data_length = test->client_data_len; + client->ptr[0] = test; + client->keepalive = test->keepalive; + client->method = test->method; + client->handler = &http_handler; + + AnodeHttpClient_send(client); + } else { + AnodeHttpClient_free(client); + } + } +} + +int main(int argc,char **argv) +{ + struct AnodeHttpClient *client; + AnodeTransportEngine transport_engine; + int i; + + if (Anode_init_ip_transport_engine(&transport_engine)) { + printf("Failed (transport engine init)\n"); + return 1; + } + + printf("Testing without keepalive...\n\n"); + init_test_cases(0); + for(i=0;iuri),(const void *)&(test_cases[i].uri),sizeof(AnodeURI)); + client->data = test_cases[i].client_data; + client->data_length = test_cases[i].client_data_len; + client->ptr[0] = &test_cases[i]; + client->keepalive = test_cases[i].keepalive; + client->method = test_cases[i].method; + client->handler = &http_handler; + + AnodeHttpClient_send(client); + } + + for(;;) { + for(i=0;iuri),(const void *)&(test_cases[i].uri),sizeof(AnodeURI)); + client->data = test_cases[i].client_data; + client->data_length = test_cases[i].client_data_len; + client->ptr[0] = &test_cases[i]; + client->keepalive = test_cases[i].keepalive; + client->method = test_cases[i].method; + client->handler = &http_handler; + + AnodeHttpClient_send(client); + + for(;;) { + for(i=0;i + * + * 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 . */ + +#include +#include +#include +#include +#include +#include "../misc.h" + +int main(int argc,char **argv) +{ + const char *base32TestStr = "asdf"; + char *fields[16]; + char buf[1024]; + char buf2[1024]; + char buf3[4096]; + unsigned int i; + unsigned long tmpl,tmpl2; + unsigned long long tmp64; + + srand(time(0)); + + Anode_base32_5_to_8((const unsigned char *)base32TestStr,buf); + printf("Base32 from test string: %s\n",buf); + Anode_base32_8_to_5("MFZWIZQA",(unsigned char *)buf2); + printf("Test string from Base32 (upper case): %s\n",buf2); + Anode_base32_8_to_5("mfzwizqa",(unsigned char *)buf2); + printf("Test string from Base32 (lower case): %s\n",buf2); + printf("Testing variable length encoding/decoded with pad5 functions...\n"); + for(i=0;i<1024;++i) { + tmpl = rand() % (sizeof(buf) - 8); + if (!tmpl) + tmpl = 1; + for(tmpl2=0;tmpl2> 3)); + if (!Anode_base32_encode_pad5(buf2,tmpl,buf3,sizeof(buf3))) { + printf("Failed (encode failed).\n"); + return 1; + } + memset(buf2,0,sizeof(buf2)); + if (!Anode_base32_decode_pad5(buf3,buf2,sizeof(buf2))) { + printf("Failed (decode failed).\n"); + return 1; + } + if (memcmp(buf,buf2,tmpl)) { + printf("Failed (compare failed).\n"); + return 1; + } + } + + printf("Anode_htonll(0x0102030405060708) == 0x%.16llx\n",tmp64 = Anode_htonll(0x0102030405060708ULL)); + printf("Anode_ntohll(0x%.16llx) == 0x%.16llx\n",tmp64,Anode_ntohll(tmp64)); + if (Anode_ntohll(tmp64) != 0x0102030405060708ULL) { + printf("Failed.\n"); + return 1; + } + + strcpy(buf,"foo bar baz"); + Anode_trim(buf); + printf("Testing string trim: 'foo bar baz' -> '%s'\n",buf); + strcpy(buf,"foo bar baz "); + Anode_trim(buf); + printf("Testing string trim: 'foo bar baz ' -> '%s'\n",buf); + strcpy(buf," foo bar baz"); + Anode_trim(buf); + printf("Testing string trim: ' foo bar baz' -> '%s'\n",buf); + strcpy(buf," foo bar baz "); + Anode_trim(buf); + printf("Testing string trim: ' foo bar baz ' -> '%s'\n",buf); + strcpy(buf,""); + Anode_trim(buf); + printf("Testing string trim: '' -> '%s'\n",buf); + strcpy(buf," "); + Anode_trim(buf); + printf("Testing string trim: ' ' -> '%s'\n",buf); + + printf("Testing string split.\n"); + strcpy(buf,"66.246.138.121,5323,0"); + i = Anode_split(buf,';',fields,16); + if (i != 1) { + printf("Failed.\n"); + return 1; + } else printf("Fields: %s\n",fields[0]); + strcpy(buf,"a;b;c"); + i = Anode_split(buf,';',fields,16); + if (i != 3) { + printf("Failed.\n"); + return 1; + } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]); + strcpy(buf,";;"); + i = Anode_split(buf,';',fields,16); + if (i != 3) { + printf("Failed.\n"); + return 1; + } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]); + strcpy(buf,"a;b;"); + i = Anode_split(buf,';',fields,16); + if (i != 3) { + printf("Failed.\n"); + return 1; + } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]); + strcpy(buf,"a;;c"); + i = Anode_split(buf,';',fields,16); + if (i != 3) { + printf("Failed.\n"); + return 1; + } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]); + strcpy(buf,";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"); + i = Anode_split(buf,';',fields,16); + if (i != 16) { + printf("Failed.\n"); + return 1; + } + strcpy(buf,""); + i = Anode_split(buf,';',fields,16); + if (i != 0) { + printf("Failed.\n"); + return 1; + } + printf("Passed.\n"); + + return 0; +} diff --git a/attic/historic/anode/libanode/tests/system_transport-test.c b/attic/historic/anode/libanode/tests/system_transport-test.c new file mode 100644 index 00000000..bda575ed --- /dev/null +++ b/attic/historic/anode/libanode/tests/system_transport-test.c @@ -0,0 +1,70 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include "../anode.h" +#include "../impl/thread.h" + +static int do_client() +{ + AnodeTransport *st; + AnodeSocket *udp_sock; + int run = 1; + + st = AnodeSystemTransport_new(NULL); + if (!st) { + printf("FAILED: unable to construct AnodeSystemTransport.\n"); + return -1; + } + printf("Created AnodeSystemTransport.\n"); + + while (run) + st->poll(st); +} + +static int do_server() +{ + AnodeTransport *st; + AnodeSocket *udp_sock; + AnodeSocket *tcp_sock; + int run = 1; + + st = AnodeSystemTransport_new(NULL); + if (!st) { + printf("FAILED: unable to construct AnodeSystemTransport.\n"); + return -1; + } + printf("Created AnodeSystemTransport.\n"); + + while (run) + st->poll(st); +} + +int main(int argc,char **argv) +{ + if (argc == 2) { + if (!strcmp(argv[1],"client")) + return do_client(); + else if (!strcmp(argv[1],"server")) + return do_server(); + } + + printf("Usage: system_transport-test \n"); + return -1; +} diff --git a/attic/historic/anode/libanode/uri.c b/attic/historic/anode/libanode/uri.c new file mode 100644 index 00000000..ca644b6a --- /dev/null +++ b/attic/historic/anode/libanode/uri.c @@ -0,0 +1,185 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include "impl/misc.h" +#include "anode.h" + +int AnodeURI_parse(AnodeURI *parsed_uri,const char *uri_string) +{ + char buf[sizeof(AnodeURI)]; + unsigned long ptr = 0; + char c; + char *p1,*p2; + + Anode_zero((void *)parsed_uri,sizeof(AnodeURI)); + + /* Get the scheme */ + for(;;) { + c = *(uri_string++); + if (!c) { + parsed_uri->scheme[ptr] = (char)0; + return ANODE_ERR_INVALID_URI; + } else if (c == ':') { + parsed_uri->scheme[ptr] = (char)0; + break; + } else { + parsed_uri->scheme[ptr++] = c; + if (ptr == sizeof(parsed_uri->scheme)) + return ANODE_ERR_BUFFER_TOO_SMALL; + } + } + + if (*uri_string == '/') { + /* If it starts with /, it's a URL */ + + /* Skip double slash */ + if (!(*(++uri_string))) + return 0; /* Scheme with no path */ + if (*uri_string == '/') { + if (!(*(++uri_string))) + return 0; /* Scheme with no path */ + } + + /* Get the host section and put it in buf[] */ + ptr = 0; + while ((*uri_string)&&(*uri_string != '/')) { + buf[ptr++] = *(uri_string++); + if (ptr == sizeof(buf)) + return ANODE_ERR_BUFFER_TOO_SMALL; + } + buf[ptr] = (char)0; + + /* Parse host section for host, username, password, and port */ + if (buf[0]) { + p1 = (char *)Anode_strchr(buf,'@'); + if (p1) { + *(p1++) = (char)0; + if (*p1) { + p2 = (char *)Anode_strchr(buf,':'); + if (p2) { + *(p2++) = (char)0; + Anode_str_copy(parsed_uri->password,p2,sizeof(parsed_uri->password)); + } + Anode_str_copy(parsed_uri->username,buf,sizeof(parsed_uri->username)); + } else return ANODE_ERR_INVALID_URI; + } else p1 = buf; + + p2 = (char *)Anode_strchr(p1,':'); + if (p2) { + *(p2++) = (char)0; + if (*p2) + parsed_uri->port = (int)strtoul(p2,(char **)0,10); + } + Anode_str_copy(parsed_uri->host,p1,sizeof(parsed_uri->host)); + } + + /* Get the path, query, and fragment section and put it in buf[] */ + ptr = 0; + while ((buf[ptr++] = *(uri_string++))) { + if (ptr == sizeof(buf)) + return ANODE_ERR_BUFFER_TOO_SMALL; + } + + /* Parse path section for path, query, and fragment */ + if (buf[0]) { + p1 = (char *)Anode_strchr(buf,'?'); + if (p1) { + *(p1++) = (char)0; + p2 = (char *)Anode_strchr(p1,'#'); + if (p2) { + *(p2++) = (char)0; + Anode_str_copy(parsed_uri->fragment,p2,sizeof(parsed_uri->fragment)); + } + Anode_str_copy(parsed_uri->query,p1,sizeof(parsed_uri->query)); + } else { + p2 = (char *)Anode_strchr(buf,'#'); + if (p2) { + *(p2++) = (char)0; + Anode_str_copy(parsed_uri->fragment,p2,sizeof(parsed_uri->fragment)); + } + } + Anode_str_copy(parsed_uri->path,buf,sizeof(parsed_uri->path)); + } + } else { + /* Otherwise, it's a URN and what remains is all path */ + ptr = 0; + while ((parsed_uri->path[ptr++] = *(uri_string++))) { + if (ptr == sizeof(parsed_uri->path)) + return ANODE_ERR_BUFFER_TOO_SMALL; + } + } + + return 0; +} + +char *AnodeURI_to_string(const AnodeURI *uri,char *buf,int len) +{ + int i = 0; + char portbuf[16]; + const char *p; + + p = uri->scheme; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + + buf[i++] = ':'; if (i >= len) return (char *)0; + + if (uri->host[0]) { + buf[i++] = '/'; if (i >= len) return (char *)0; + buf[i++] = '/'; if (i >= len) return (char *)0; + + if (uri->username[0]) { + p = uri->username; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + if (uri->password[0]) { + buf[i++] = ':'; if (i >= len) return (char *)0; + p = uri->password; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + } + buf[i++] = '@'; if (i >= len) return (char *)0; + } + + p = uri->host; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + + if ((uri->port > 0)&&(uri->port <= 0xffff)) { + buf[i++] = ':'; if (i >= len) return (char *)0; + snprintf(portbuf,sizeof(portbuf),"%d",uri->port); + p = portbuf; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + } + } + + p = uri->path; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + + if (uri->query[0]) { + buf[i++] = '?'; if (i >= len) return (char *)0; + p = uri->query; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + } + + if (uri->fragment[0]) { + buf[i++] = '#'; if (i >= len) return (char *)0; + p = uri->fragment; + while (*p) { buf[i++] = *(p++); if (i >= len) return (char *)0; } + } + + buf[i] = (char)0; + + return buf; +} diff --git a/attic/historic/anode/libanode/utils/anode-make-identity.c b/attic/historic/anode/libanode/utils/anode-make-identity.c new file mode 100644 index 00000000..99a3897a --- /dev/null +++ b/attic/historic/anode/libanode/utils/anode-make-identity.c @@ -0,0 +1,50 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include "../anode.h" +#include "../impl/misc.h" +#include "../impl/types.h" + +int main(int argc,char **argv) +{ + char str[1024]; + AnodeZone zone; + AnodeIdentity identity; + + if (argc < 2) { + printf("Usage: anode-make-identity <32-bit zone ID hex>\n"); + return 0; + } + + *((uint32_t *)zone.bits) = htonl((uint32_t)strtoul(argv[1],(char **)0,16)); + + if (AnodeIdentity_generate(&identity,&zone,ANODE_ADDRESS_ANODE_256_40)) { + fprintf(stderr,"Error: identity key pair generation failed (check build settings).\n"); + return 1; + } + if (AnodeIdentity_to_string(&identity,str,sizeof(str)) <= 0) { + fprintf(stderr,"Error: internal error converting identity to string.\n"); + return -1; + } + + printf("%s\n",str); + + return 0; +} diff --git a/attic/historic/anode/libanode/zone.c b/attic/historic/anode/libanode/zone.c new file mode 100644 index 00000000..a6e397ae --- /dev/null +++ b/attic/historic/anode/libanode/zone.c @@ -0,0 +1,184 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009-2010 Adam Ierymenko + * + * 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 . */ + +#include +#include +#include +#include +#include +#include +#include "impl/types.h" +#include "impl/misc.h" +#include "impl/dictionary.h" +#include "impl/environment.h" +#include "impl/http_client.h" +#include "anode.h" + +static const char *_MONTHS[12] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" }; +static const char *_DAYS_OF_WEEK[7] = { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" }; +static inline unsigned long get_file_time_for_http(const char *path,char *buf,unsigned int len) +{ + struct stat st; + struct tm *gmt; + + if (!stat(path,(struct stat *)&st)) { + gmt = gmtime(&st.st_mtime); + if (gmt) { + snprintf(buf,len,"%s, %d %s %d %d:%d:%d GMT", + _DAYS_OF_WEEK[gmt->tm_wday], + gmt->tm_mday, + _MONTHS[gmt->tm_mon], + (1900 + gmt->tm_year), + gmt->tm_hour, + gmt->tm_min, + gmt->tm_sec); + buf[len - 1] = (char)0; + return (unsigned long)st.st_size; + } + } + + return 0; +} + +struct AnodeZoneLookupJob +{ + char cached_zone_file[2048]; + struct AnodeDictionary *zone_dict; + AnodeZone zone; + void *ptr; + void (*zone_lookup_handler)(void *,const AnodeZone *,AnodeZoneFile *); + int had_cached_zone; +}; + +static void AnodeZone_lookup_http_handler(struct AnodeHttpClient *client) +{ + char *data_tmp; + struct AnodeZoneLookupJob *job = (struct AnodeZoneLookupJob *)client->ptr[0]; + FILE *zf; + + if ((client->response.code == 200)&&(client->response.data_length > 0)) { + zf = fopen(job->cached_zone_file,"w"); + if (zf) { + fwrite(client->response.data,1,client->response.data_length,zf); + fclose(zf); + } + + data_tmp = (char *)malloc(client->response.data_length + 1); + Anode_memcpy((void *)data_tmp,client->response.data,client->response.data_length); + data_tmp[client->response.data_length] = (char)0; + + AnodeDictionary_clear(job->zone_dict); + AnodeDictionary_read( + job->zone_dict, + data_tmp, + "\r\n", + "=", + ";", + '\\', + 1,1); + + free((void *)data_tmp); + + job->zone_lookup_handler(job->ptr,&job->zone,(AnodeZoneFile *)job->zone_dict); + } else if (job->had_cached_zone) + job->zone_lookup_handler(job->ptr,&job->zone,(AnodeZoneFile *)job->zone_dict); + else { + AnodeDictionary_destroy(job->zone_dict); + free((void *)job->zone_dict); + job->zone_lookup_handler(job->ptr,&job->zone,(AnodeZoneFile *)0); + } + + free((void *)job); + AnodeHttpClient_free(client); +} + +void AnodeZone_lookup( + AnodeTransportEngine *transport, + const AnodeZone *zone, + void *ptr, + void (*zone_lookup_handler)(void *,const AnodeZone *,AnodeZone *)) +{ + char cached_zones_folder[2048]; + char cached_zone_file[2048]; + char if_modified_since[256]; + unsigned long file_size; + struct AnodeZoneLookupJob *job; + struct AnodeHttpClient *client; + char *file_data; + FILE *zf; + + if (Anode_get_cache_sub("zones",cached_zones_folder,sizeof(cached_zones_folder))) { + snprintf(cached_zone_file,sizeof(cached_zone_file),"%s%c%.2x%.2x%.2x%.2x.z",cached_zones_folder,ANODE_PATH_SEPARATOR,(unsigned int)zone->bits[0],(unsigned int)zone->bits[1],(unsigned int)zone->bits[2],(unsigned int)zone->bits[3]); + cached_zone_file[sizeof(cached_zone_file)-1] = (char)0; + + job = (struct AnodeZoneLookupJob *)malloc(sizeof(struct AnodeZoneLookupJob)); + Anode_str_copy(job->cached_zone_file,cached_zone_file,sizeof(job->cached_zone_file)); + job->zone_dict = (struct AnodeDictionary *)malloc(sizeof(struct AnodeDictionary)); + AnodeDictionary_init(job->zone_dict,0); + job->zone.bits[0] = zone->bits[0]; + job->zone.bits[1] = zone->bits[1]; + job->zone.bits[2] = zone->bits[2]; + job->zone.bits[3] = zone->bits[3]; + job->ptr = ptr; + job->zone_lookup_handler = zone_lookup_handler; + job->had_cached_zone = 0; + + client = AnodeHttpClient_new(transport); + + Anode_str_copy(client->uri.scheme,"http",sizeof(client->uri.scheme)); + snprintf(client->uri.host,sizeof(client->uri.host),"a--%.2x%.2x%.2x%.2x.net",(unsigned int)zone->bits[0],(unsigned int)zone->bits[1],(unsigned int)zone->bits[2],(unsigned int)zone->bits[3]); + client->uri.host[sizeof(client->uri.host)-1] = (char)0; + Anode_str_copy(client->uri.path,"/z",sizeof(client->uri.path)); + + client->handler = &AnodeZone_lookup_http_handler; + client->ptr[0] = job; + + if ((file_size = get_file_time_for_http(cached_zone_file,if_modified_since,sizeof(if_modified_since)))) { + zf = fopen(cached_zone_file,"r"); + if (zf) { + AnodeDictionary_put(&client->headers,"If-Modified-Since",if_modified_since); + file_data = (char *)malloc(file_size + 1); + if (fread((void *)file_data,1,file_size,zf)) { + file_data[file_size] = (char)0; + AnodeDictionary_read( + job->zone_dict, + file_data, + "\r\n", + "=", + ";", + '\\', + 1,1); + job->had_cached_zone = 1; + } + free((void *)file_data); + fclose(zf); + } + } + + AnodeHttpClient_send(client); + } else zone_lookup_handler(ptr,zone,(AnodeZone *)0); +} + +const char *AnodeZoneFile_get(AnodeZoneFile *zone,const char *key) +{ + return AnodeDictionary_get((struct AnodeDictionary *)zone,key); +} + +void AnodeZoneFile_free(AnodeZoneFile *zone) +{ + AnodeDictionary_destroy((struct AnodeDictionary *)zone); + free((void *)zone); +} diff --git a/attic/historic/anode/libspark/Makefile b/attic/historic/anode/libspark/Makefile new file mode 100644 index 00000000..0d3fedd8 --- /dev/null +++ b/attic/historic/anode/libspark/Makefile @@ -0,0 +1,16 @@ +SYSNAME:=${shell uname} +SYSNAME!=uname +include ../config.mk.${SYSNAME} + +LIBSPARK_OBJS= + +all: libspark + +libspark: $(LIBSPARK_OBJS) + ar rcs libspark.a $(LIBSPARK_OBJS) + ranlib libspark.a + +clean: force + rm -f *.a *.so *.dylib *.dll *.lib *.exe *.o + +force: ; diff --git a/attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp b/attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp new file mode 100644 index 00000000..9b1ecaa1 --- /dev/null +++ b/attic/historic/anode/libspark/experiments/FindGoodSegmentDelimiters.cpp @@ -0,0 +1,161 @@ +// Searches for good delimiters to cut streams into relatively well sized +// segments. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Desired size range +#define MIN_DESIRED_SIZE 4096 +#define MAX_DESIRED_SIZE 131072 + +#define DELIMITER_SET_SIZE 1 +typedef boost::array DelimArray; + +struct BestEntry +{ + DelimArray best; + double bestScore; + std::vector data; +}; + +boost::mutex bestLock; +boost::mutex outLock; +std::map best; + +static void runThread(const std::string &fileName) +{ + char tmp[4096]; + + boost::mt19937 prng; + { + boost::uint32_t seed; + FILE *ur = fopen("/dev/urandom","r"); + fread((void *)&seed,1,sizeof(seed),ur); + fclose(ur); + prng.seed(seed); + } + + BestEntry *myEntry; + { + boost::mutex::scoped_lock l(bestLock); + myEntry = &(best[fileName]); + myEntry->bestScore = 99999999.0; + } + + { + boost::mutex::scoped_lock l(outLock); + + std::cout << "*** Reading test data from: " << fileName << std::endl; + FILE *f = fopen(fileName.c_str(),"r"); + if (f) { + int n; + while ((n = fread((void *)tmp,1,sizeof(tmp),f)) > 0) { + for(int i=0;idata.push_back((unsigned char)tmp[i]); + } + fclose(f); + } + + if (myEntry->data.size() <= 0) { + std::cout << "Error: no data read." << std::endl; + exit(1); + } else std::cout << "*** Read " << myEntry->data.size() << " bytes of test data." << std::endl; + + std::cout.flush(); + } + + DelimArray current; + for(unsigned int i=0;i::iterator i=myEntry->data.begin();i!=myEntry->data.end();++i) { + shiftRegister <<= 1; + shiftRegister |= (((boost::uint32_t)*i) & 1); + + ++segSize; + + boost::uint16_t transformedShiftRegister = (boost::uint16_t)(shiftRegister); + + for(DelimArray::iterator d=current.begin();d!=current.end();++d) { + if (transformedShiftRegister == *d) { + if (segSize < MIN_DESIRED_SIZE) + ++numTooShort; + else if (segSize > MAX_DESIRED_SIZE) + ++numTooLong; + else ++numGood; + segSize = 0; + break; + } + } + } + if (segSize) { + if (segSize < MIN_DESIRED_SIZE) + ++numTooShort; + else if (segSize > MAX_DESIRED_SIZE) + ++numTooLong; + else ++numGood; + } + + if (numGood) { + double score = ((double)(numTooShort + numTooLong)) / ((double)numGood); + + if (score < myEntry->bestScore) { + myEntry->best = current; + myEntry->bestScore = score; + + boost::mutex::scoped_lock l(outLock); + + std::cout << fileName << ": "; + + for(DelimArray::iterator d=current.begin();d!=current.end();++d) { + sprintf(tmp,"0x%.4x",(unsigned int)*d); + if (d != current.begin()) + std::cout << ','; + std::cout << tmp; + } + + std::cout << ": " << numTooShort << " / " << numGood << " / " << numTooLong << " (" << score << ")" << std::endl; + std::cout.flush(); + + if ((numTooShort == 0)&&(numTooLong == 0)) + break; + } + } + + for(DelimArray::iterator i=current.begin();i!=current.end();++i) + *i = (boost::uint16_t)prng(); + } +} + +int main(int argc,char **argv) +{ + std::vector< boost::shared_ptr > threads; + + for(int i=1;i t(new boost::thread(boost::bind(&runThread,std::string(argv[i])))); + threads.push_back(t); + } + + for(std::vector< boost::shared_ptr >::iterator i=threads.begin();i!=threads.end();++i) + (*i)->join(); + + return 0; +} diff --git a/attic/historic/anode/libspark/experiments/Makefile b/attic/historic/anode/libspark/experiments/Makefile new file mode 100644 index 00000000..83aa4f67 --- /dev/null +++ b/attic/historic/anode/libspark/experiments/Makefile @@ -0,0 +1,5 @@ +all: + g++ -O6 -ftree-vectorize -o FindGoodSegmentDelimiters FindGoodSegmentDelimiters.cpp -lboost_thread -lpthread + +clean: + rm FindGoodSegmentDelimiters diff --git a/attic/historic/anode/libspark/streamencoder.h b/attic/historic/anode/libspark/streamencoder.h new file mode 100644 index 00000000..b487ca40 --- /dev/null +++ b/attic/historic/anode/libspark/streamencoder.h @@ -0,0 +1,108 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 _SPARK_STREAMENCODER_H +#define _SPARK_STREAMENCODER_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct +{ + unsigned char *input_buf; + unsigned long input_buf_capacity; + unsigned long input_length; + + unsigned char *stream_out_buf; + unsigned long stream_out_buf_capacity; + unsigned long stream_out_length; + + void (*data_segment_add_func)(const void *data,unsigned long len,const void *global_hash,unsigned long global_hash_len); +} SparkStreamEncoder; + +/** + * Initialize a spark stream encoder + * + * @param enc Encoder structure to initialize + * @param data_segment_add_func Function to call to store or cache data + */ +void SparkStreamEncoder_init( + SparkStreamEncoder *enc, + void (*data_segment_add_func)( + const void *data, + unsigned long len, + const void *global_hash, + unsigned long global_hash_len)); + +/** + * Clean up a spark stream encoder structure + * + * @param enc Structure to clear + */ +void SparkStreamEncoder_destroy(SparkStreamEncoder *enc); + +/** + * Add data to encode + * + * @param enc Encoder structure + * @param data Data to encode + * @param len Length of data in bytes + * @return Number of bytes of result stream now available + */ +unsigned long SparkStreamEncoder_put( + SparkStreamEncoder *enc, + const void *data, + unsigned long len); + +/** + * Flush all data currently in input buffer + * + * @param enc Encoder structure to flush + */ +void SparkStreamEncoder_flush(SparkStreamEncoder *enc); + +/** + * @return Number of bytes of output stream available + */ +static inline unsigned long SparkStreamEncoder_available(SparkStreamEncoder *enc) +{ + return enc->stream_out_length; +} + +/** + * @return Pointer to result stream bytes (may return null if none available) + */ +static inline const void *SparkStreamEncoder_get(SparkStreamEncoder *enc) +{ + return (const void *)(enc->stream_out_buf); +} + +/** + * @return "Consume" result stream bytes after they're read or sent + */ +static inline void SparkStreamEncoder_consume(SparkStreamEncoder *enc,unsigned long len) +{ + unsigned long i; + for(i=len;istream_out_length;++i) + enc->stream_out_buf[i - len] = enc->stream_out_buf[i]; +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/attic/historic/anode/libspark/wrapper.h b/attic/historic/anode/libspark/wrapper.h new file mode 100644 index 00000000..eb8c593d --- /dev/null +++ b/attic/historic/anode/libspark/wrapper.h @@ -0,0 +1,66 @@ +/* libanode: the Anode C reference implementation + * Copyright (C) 2009 Adam Ierymenko + * + * 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 _SPARK_WRAPPER_H +#define _SPARK_WRAPPER_H + +#include +#include "../libanode/aes128.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Spark uses SHA-256 with hash length 32 */ +#define SPARK_HASH_LENGTH 32 + +// Wrap a segment for forward propagation +static inline void Spark_wrap(void *data,unsigned long len,void *plaintext_hash_buf,void *global_hash_buf) +{ + unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE]; + + SHA256((const unsigned char *)data,len,(unsigned char *)plaintext_hash_buf); + + Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash_buf); + Anode_aes128_cfb_encrypt(expkey,((const unsigned char *)plaintext_hash_buf) + 16,(unsigned char *)data,len); + + SHA256((const unsigned char *)data,len,(unsigned char *)global_hash_buf); +} + +// Unwrap a segment and check its integrity +static inline int Spark_unwrap(void *data,unsigned long len,const void *plaintext_hash) +{ + unsigned char expkey[ANODE_AES128_EXP_KEY_SIZE]; + unsigned char check_hash[32]; + unsigned long i; + + Anode_aes128_expand_key(expkey,(const unsigned char *)plaintext_hash); + Anode_aes128_cfb_decrypt(expkey,((const unsigned char *)plaintext_hash) + 16,(unsigned char *)data,len); + + SHA256((const unsigned char *)data,len,check_hash); + + for(i=0;i<32;++i) { + if (check_hash[i] != ((const unsigned char *)plaintext_hash)[i]) + return 0; + } + return 1; +} + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3 From 64b7d9ef82d73038509b686a46ce5816847089af Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 1 Jun 2017 07:15:46 -0700 Subject: New clustering work. --- attic/DBM.cpp | 243 ++++++++ attic/DBM.hpp | 168 ++++++ ext/kissdb/Makefile | 7 - ext/kissdb/README.md | 69 --- ext/kissdb/SPEC.txt | 62 -- ext/kissdb/kissdb.c | 452 --------------- ext/kissdb/kissdb.h | 173 ------ ext/vsdm/LICENSE.txt | 22 - ext/vsdm/Makefile | 5 - ext/vsdm/README.md | 40 -- ext/vsdm/vsdm-test.cpp | 72 --- ext/vsdm/vsdm.hpp | 1491 ------------------------------------------------ include/ZeroTierOne.h | 56 ++ node/Buffer.hpp | 40 +- node/Constants.hpp | 6 + node/Hashtable.hpp | 7 +- 16 files changed, 481 insertions(+), 2432 deletions(-) create mode 100644 attic/DBM.cpp create mode 100644 attic/DBM.hpp delete mode 100644 ext/kissdb/Makefile delete mode 100644 ext/kissdb/README.md delete mode 100644 ext/kissdb/SPEC.txt delete mode 100644 ext/kissdb/kissdb.c delete mode 100644 ext/kissdb/kissdb.h delete mode 100644 ext/vsdm/LICENSE.txt delete mode 100644 ext/vsdm/Makefile delete mode 100644 ext/vsdm/README.md delete mode 100644 ext/vsdm/vsdm-test.cpp delete mode 100644 ext/vsdm/vsdm.hpp (limited to 'attic') diff --git a/attic/DBM.cpp b/attic/DBM.cpp new file mode 100644 index 00000000..54f017e0 --- /dev/null +++ b/attic/DBM.cpp @@ -0,0 +1,243 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#include "DBM.hpp" + +#include "../version.h" + +#include "../node/Salsa20.hpp" +#include "../node/Poly1305.hpp" +#include "../node/SHA512.hpp" + +#include "../osdep/OSUtils.hpp" + +#define ZT_STORED_OBJECT_TYPE__CLUSTER_NODE_STATUS (ZT_STORED_OBJECT__MAX_TYPE_ID + 1) +#define ZT_STORED_OBJECT_TYPE__CLUSTER_DEFINITION (ZT_STORED_OBJECT__MAX_TYPE_ID + 2) + +namespace ZeroTier { + +// We generate the cluster ID from our address and version info since this is +// not at all designed to allow interoperation between versions (or endians) +// in the same cluster. +static inline uint64_t _mkClusterId(const Address &myAddress) +{ + uint64_t x = ZEROTIER_ONE_VERSION_MAJOR; + x <<= 8; + x += ZEROTIER_ONE_VERSION_MINOR; + x <<= 8; + x += ZEROTIER_ONE_VERSION_REVISION; + x <<= 40; + x ^= myAddress.toInt(); +#if __BYTE_ORDER == __BIG_ENDIAN + ++x; +#endif; + return x; +} + +void DBM::onUpdate(uint64_t from,const _MapKey &k,const _MapValue &v,uint64_t rev) +{ + char p[4096]; + char tmp[ZT_DBM_MAX_VALUE_SIZE]; + if (_persistentPath((ZT_StoredObjectType)k.type,k.key,p,sizeof(p))) { + // Reduce unnecessary disk writes + FILE *f = fopen(p,"r"); + if (f) { + long n = (long)fread(tmp,1,sizeof(tmp),f); + fclose(f); + if ((n == (long)v.len)&&(!memcmp(v.data,tmp,n))) + return; + } + + // Write to disk if file has changed or was not already present + f = fopen(p,"w"); + if (f) { + if (fwrite(data,len,1,f) != 1) + fprintf(stderr,"WARNING: error writing to %s (I/O error)" ZT_EOL_S,p); + fclose(f); + if (type == ZT_STORED_OBJECT_IDENTITY_SECRET) + OSUtils::lockDownFile(p,false); + } else { + fprintf(stderr,"WARNING: error writing to %s (cannot open)" ZT_EOL_S,p); + } + } +} + +void DBM::onDelete(uint64_t from,const _MapKey &k) +{ + char p[4096]; + if (_persistentPath((ZT_StoredObjectType)k.type,k.key,p,sizeof(p))) + OSUtils::rm(p); +} + +DBM::_vsdm_cryptor::_vsdm_cryptor(const Identity &secretIdentity) +{ + uint8_t s512[64]; + SHA512::hash(h512,secretIdentity.privateKeyPair().priv.data,ZT_C25519_PRIVATE_KEY_LEN); + memcpy(_key,s512,sizeof(_key)); +} + +void DBM::_vsdm_cryptor::encrypt(void *d,unsigned long l) +{ + if (l >= 24) { // sanity check + uint8_t key[32]; + uint8_t authKey[32]; + uint8_t auth[16]; + + uint8_t *const iv = reinterpret_cast(d) + (l - 16); + Utils::getSecureRandom(iv,16); + memcpy(key,_key,32); + for(unsigned long i=0;i<8;++i) + _key[i] ^= iv[i]; + + Salsa20 s20(key,iv + 8); + memset(authKey,0,32); + s20.crypt12(authKey,authKey,32); + s20.crypt12(d,d,l - 24); + + Poly1305::compute(auth,d,l - 24,authKey); + memcpy(reinterpret_cast(d) + (l - 24),auth,8); + } +} + +bool DBM::_vsdm_cryptor::decrypt(void *d,unsigned long l) +{ + if (l >= 24) { // sanity check + uint8_t key[32]; + uint8_t authKey[32]; + uint8_t auth[16]; + + uint8_t *const iv = reinterpret_cast(d) + (l - 16); + memcpy(key,_key,32); + for(unsigned long i=0;i<8;++i) + _key[i] ^= iv[i]; + + Salsa20 s20(key,iv + 8); + memset(authKey,0,32); + s20.crypt12(authKey,authKey,32); + + Poly1305::compute(auth,d,l - 24,authKey); + if (!Utils::secureEq(reinterpret_cast(d) + (l - 24),auth,8)) + return false; + + s20.crypt12(d,d,l - 24); + + return true; + } + return false; +} + +DBM::DBM(const Identity &secretIdentity,uint64_t clusterMemberId,const std::string &basePath,Node *node) : + _basePath(basePath), + _node(node), + _startTime(OSUtils::now()), + _m(_mkClusterId(secretIdentity.address()),clusterMemberId,false,_vsdm_cryptor(secretIdentity),_vsdm_watcher(this)) +{ +} + +DBM::~DBM() +{ +} + +void DBM::put(const ZT_StoredObjectType type,const uint64_t key,const void *data,unsigned int len) +{ + char p[4096]; + if (_m.put(_MapKey(key,(uint16_t)type),Value(OSUtils::now(),(uint16_t)len,data))) { + if (_persistentPath(type,key,p,sizeof(p))) { + FILE *f = fopen(p,"w"); + if (f) { + if (fwrite(data,len,1,f) != 1) + fprintf(stderr,"WARNING: error writing to %s (I/O error)" ZT_EOL_S,p); + fclose(f); + if (type == ZT_STORED_OBJECT_IDENTITY_SECRET) + OSUtils::lockDownFile(p,false); + } else { + fprintf(stderr,"WARNING: error writing to %s (cannot open)" ZT_EOL_S,p); + } + } + } +} + +bool DBM::get(const ZT_StoredObjectType type,const uint64_t key,Value &value) +{ + char p[4096]; + if (_m.get(_MapKey(key,(uint16_t)type),value)) + return true; + if (_persistentPath(type,key,p,sizeof(p))) { + FILE *f = fopen(p,"r"); + if (f) { + long n = (long)fread(value.data,1,sizeof(value.data),f); + value.len = (n > 0) ? (uint16_t)n : (uint16_t)0; + fclose(f); + value.ts = OSUtils::getLastModified(p); + _m.put(_MapKey(key,(uint16_t)type),value); + return true; + } + } + return false; +} + +void DBM::del(const ZT_StoredObjectType type,const uint64_t key) +{ + char p[4096]; + _m.del(_MapKey(key,(uint16_t)type)); + if (_persistentPath(type,key,p,sizeof(p))) + OSUtils::rm(p); +} + +void DBM::clean() +{ +} + +bool DBM::_persistentPath(const ZT_StoredObjectType type,const uint64_t key,char *p,unsigned int maxlen) +{ + switch(type) { + case ZT_STORED_OBJECT_IDENTITY_PUBLIC: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "identity.public",_basePath.c_str()); + return true; + case ZT_STORED_OBJECT_IDENTITY_SECRET: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "identity.secret",_basePath.c_str()); + return true; + case ZT_STORED_OBJECT_IDENTITY: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "iddb.d" ZT_PATH_SEPARATOR_S "%.10llx",_basePath.c_str(),key); + return true; + case ZT_STORED_OBJECT_NETWORK_CONFIG: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf",_basePath.c_str(),key); + return true; + case ZT_STORED_OBJECT_PLANET: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "planet",_basePath.c_str()); + return true; + case ZT_STORED_OBJECT_MOON: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "moons.d" ZT_PATH_SEPARATOR_S "%.16llx.moon",_basePath.c_str(),key); + return true; + case (ZT_StoredObjectType)ZT_STORED_OBJECT_TYPE__CLUSTER_DEFINITION: + Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "cluster",_basePath.c_str()); + return true; + default: + return false; + } +} + +} // namespace ZeroTier diff --git a/attic/DBM.hpp b/attic/DBM.hpp new file mode 100644 index 00000000..c6d5b8c0 --- /dev/null +++ b/attic/DBM.hpp @@ -0,0 +1,168 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifndef ZT_DBM_HPP___ +#define ZT_DBM_HPP___ + +#include +#include +#include +#include + +#include + +#include "../node/Constants.hpp" +#include "../node/Mutex.hpp" +#include "../node/Utils.hpp" +#include "../node/Identity.hpp" +#include "../node/Peer.hpp" + +#include "../ext/vsdm/vsdm.hpp" + +// The Peer is the largest structure we persist here +#define ZT_DBM_MAX_VALUE_SIZE sizeof(Peer) + +namespace ZeroTier { + +class Node; +class DBM; + +class DBM +{ +public: + ZT_PACKED_STRUCT(struct Value + { + Value(const uint64_t t,const uint16_t l,const void *d) : + ts(t), + l(l) + { + memcpy(data,d,l); + } + uint64_t ts; + uint16_t len; + uint8_t data[ZT_DBM_MAX_VALUE_SIZE]; + }); + +private: + ZT_PACKED_STRUCT(struct _MapKey + { + _MapKey() : obj(0),type(0) {} + _MapKey(const uint16_t t,const uint64_t o) : obj(o),type(t) {} + uint64_t obj; + uint16_t type; + inline bool operator==(const _MapKey &k) const { return ((obj == k.obj)&&(type == k.type)); } + }); + struct _MapHasher + { + inline std::size_t operator()(const _MapKey &k) const { return (std::size_t)((k.obj ^ (k.obj >> 32)) + (uint64_t)k.type); } + }; + + void onUpdate(uint64_t from,const _MapKey &k,const Value &v,uint64_t rev); + void onDelete(uint64_t from,const _MapKey &k); + + class _vsdm_watcher + { + public: + _vsdm_watcher(DBM *p) : _parent(p) {} + inline void add(uint64_t from,const _MapKey &k,const Value &v,uint64_t rev) { _parent->onUpdate(from,k,v,rev); } + inline void update(uint64_t from,const _MapKey &k,const Value &v,uint64_t rev) { _parent->onUpdate(from,k,v,rev); } + inline void del(uint64_t from,const _MapKey &k) { _parent->onDelete(from,k); } + private: + DBM *_parent; + }; + class _vsdm_serializer + { + public: + static inline unsigned long objectSize(const _MapKey &k) { return 10; } + static inline unsigned long objectSize(const Value &v) { return (10 + v.len); } + static inline const char *objectData(const _MapKey &k) { return reinterpret_cast(&k); } + static inline const char *objectData(const Value &v) { return reinterpret_cast(&v); } + static inline bool objectDeserialize(const char *d,unsigned long l,_MapKey &k) + { + if (l == 10) { + memcpy(&k,d,10); + return true; + } + return false; + } + static inline bool objectDeserialize(const char *d,unsigned long l,Value &v) + { + if ((l >= 10)&&(l <= (10 + ZT_DBM_MAX_VALUE_SIZE))) { + memcpy(&v,d,l); + return true; + } + return false; + } + }; + class _vsdm_cryptor + { + public: + _vsdm_cryptor(const Identity &secretIdentity); + static inline unsigned long overhead() { return 24; } + void encrypt(void *d,unsigned long l); + bool decrypt(void *d,unsigned long l); + uint8_t _key[32]; + }; + + typedef vsdm< _MapKey,Value,16384,_vsdm_watcher,_vsdm_serializer,_vsdm_cryptor,_MapHasher > _Map; + + friend class _Map; + +public: + ZT_PACKED_STRUCT(struct ClusterPeerStatus + { + uint64_t startTime; + uint64_t currentTime; + uint64_t clusterPeersConnected; + uint64_t ztPeersConnected; + uint16_t platform; + uint16_t arch; + }); + + DBM(const Identity &secretIdentity,uint64_t clusterMemberId,const std::string &basePath,Node *node); + + ~DBM(); + + void put(const ZT_StoredObjectType type,const uint64_t key,const void *data,unsigned int len); + + bool get(const ZT_StoredObjectType type,const uint64_t key,Value &value); + + void del(const ZT_StoredObjectType type,const uint64_t key); + + void clean(); + +private: + bool DBM::_persistentPath(const ZT_StoredObjectType type,const uint64_t key,char *p,unsigned int maxlen); + + const std::string _basePath; + Node *const _node; + uint64_t _startTime; + _Map _m; +}; + +} // namespace ZeroTier + +#endif diff --git a/ext/kissdb/Makefile b/ext/kissdb/Makefile deleted file mode 100644 index f47372c6..00000000 --- a/ext/kissdb/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -# http://creativecommons.org/publicdomain/zero/1.0/ - -all: - gcc -Wall -O2 -DKISSDB_TEST -o kissdb-test kissdb.c - -clean: - rm -f kissdb-test *.o test.db diff --git a/ext/kissdb/README.md b/ext/kissdb/README.md deleted file mode 100644 index ab8a6cff..00000000 --- a/ext/kissdb/README.md +++ /dev/null @@ -1,69 +0,0 @@ -kissdb -====== - -(Keep It) Simple Stupid Database - -KISSDB is about the simplest key/value store you'll ever see, anywhere. -It's written in plain vanilla C using only the standard string and FILE -I/O functions, and should port to just about anything with a disk or -something that acts like one. - -It stores keys and values of fixed length in a stupid-simple file format -based on fixed-size hash tables. If a hash collision occurrs, a new "page" -of hash table is appended to the database. The format is append-only. -There is no delete. Puts that replace an existing value, however, will not -grow the file as they will overwrite the existing entry. - -Hash table size is a space/speed trade-off parameter. Larger hash tables -will reduce collisions and speed things up a bit, at the expense of memory -and disk space. A good size is usually about 1/2 the average number of -entries you expect. - -Features: - - * Tiny, compiles to ~4k on an x86_64 Linux system - * Small memory footprint (only caches hash tables) - * Very space-efficient (on disk) if small hash tables are used - * Makes a decent effort to be robust on power loss - * Pretty respectably fast, especially given its simplicity - * 64-bit, file size limit is 2^64 bytes - * Ports to anything with a C compiler and stdlib/stdio - * Public domain - -Limitations: - - * Fixed-size keys and values, must recreate and copy to change any init size parameter - * Add/update only, no delete - * Iteration is supported but key order is undefined - * No search for subsets of keys/values - * No indexes - * No transactions - * No special recovery features if a database gets corrupted - * No built-in thread-safety (guard it with a mutex in MT code) - * No built-in caching of data (only hash tables are cached for lookup speed) - * No endian-awareness (currently), so big-endian DBs won't read on little-endian machines - -Alternative key/value stores and embedded databases: - - * [MDB](http://symas.com/mdb/) uses mmap() and is very fast (not quite as tiny/simple/portable) - * [CDB](http://cr.yp.to/cdb.html) is also minimal and fast, probably the closest thing to this (but has a 4gb size limit) - * [Kyoto Cabinet](http://fallabs.com/kyotocabinet/) is very fast, full-featured, and modern (license required for commercial use) - * [SQLite](http://www.sqlite.org/) gives you a complete embedded SQL server (public domain, very mature, much larger) - * Others include GDBM, NDBM, Berkeley DB, etc. Use your Googles. :) - -KISSDB is good if you want space-efficient relatively fast write-once/read-many storage -of keys mapped to values. It's not a good choice if you need searches, indexes, delete, -structured storage, or widely varying key/value sizes. It's also probably not a good -choice if you need a long-lived database for critical data, since it lacks recovery -features and is brittle if its internals are modified. It would be better for a cache -of data that can be restored or "re-learned," such as keys, Bitcoin transactions, nodes -on a peer-to-peer network, log analysis results, rendered web pages, session cookies, -auth tokens, etc. - -KISSDB is in the public domain as according to the [Creative Commons Public Domain Dedication](http://creativecommons.org/publicdomain/zero/1.0/). -One reason it was written was the poverty of simple key/value databases with wide open licensing. Even old ones like GDBM have GPL, not LGPL, licenses. - -See comments in kissdb.h for documentation. Makefile can be used to build -a test program on systems with gcc. - -Author: Adam Ierymenko / ZeroTier Networks LLC diff --git a/ext/kissdb/SPEC.txt b/ext/kissdb/SPEC.txt deleted file mode 100644 index 732c4df5..00000000 --- a/ext/kissdb/SPEC.txt +++ /dev/null @@ -1,62 +0,0 @@ ------ - -KISSDB file format (version 2) -Author: Adam Ierymenko - -http://creativecommons.org/publicdomain/zero/1.0/ - ------ - -In keeping with the goal of minimalism the file format is very simple, the -sort of thing that would be given as an example in an introductory course in -data structures. It's a basic hash table that adds additional pages of hash -table entries on collision. - -It consists of a 28 byte header followed by a series of hash tables and data. -All integer values are stored in the native word order of the target -architecture (in the future the code might be fixed to make everything -little-endian if anyone cares about that). - -The header consists of the following fields: - -[0-3] magic numbers: (ASCII) 'K', 'd', 'B', KISSDB_VERSION (currently 2) -[4-11] 64-bit hash table size in entries -[12-19] 64-bit key size in bytes -[20-27] 64-bit value size in bytes - -Hash tables are arrays of [hash table size + 1] 64-bit integers. The extra -entry, if nonzero, is the offset in the file of the next hash table, forming -a linked list of hash tables across the file. - -Immediately following the header, the first hash table will be written when -the first key/value is added. The algorithm for adding new entries is as -follows: - -(1) The key is hashed using a 64-bit variant of the DJB2 hash function, and - this is taken modulo hash table size to get a bucket number. -(2) Hash tables are checked in order, starting with the first hash table, - until a zero (empty) bucket is found. If one is found, skip to step (4). -(3) If no empty buckets are found in any hash table, a new table is appended - to the file and the final pointer in the previous hash table is set to - its offset. (In the code the update of the next hash table pointer in - the previous hash table happens last, after the whole write is complete, - to avoid corruption on power loss.) -(4) The key and value are appended, in order with no additional meta-data, - to the database file. Before appending the offset in the file stream - where they will be stored is saved. After appending, this offset is - written to the empty hash table bucket we chose in steps 2/3. Hash table - updates happen last to avoid corruption if the write does not complete. - -Lookup of a key/value pair occurs as follows: - -(1) The key is hashed and taken modulo hash table size to get a bucket - number. -(2) If this bucket's entry in the hash table is nonzero, the key at the - offset specified by this bucket is compared to the key being looked up. - If they are equal, the value is read and returned. -(3) If the keys are not equal, the next hash table is checked and step (2) - is repeated. If an empty bucket is encountered or if we run out of hash - tables, the key was not found. - -To update an existing value, its location is looked up and the value portion -of the entry is rewritten. diff --git a/ext/kissdb/kissdb.c b/ext/kissdb/kissdb.c deleted file mode 100644 index 6b275686..00000000 --- a/ext/kissdb/kissdb.c +++ /dev/null @@ -1,452 +0,0 @@ -/* (Keep It) Simple Stupid Database - * - * Written by Adam Ierymenko - * KISSDB is in the public domain and is distributed with NO WARRANTY. - * - * http://creativecommons.org/publicdomain/zero/1.0/ */ - -/* Compile with KISSDB_TEST to build as a test program. */ - -/* Note: big-endian systems will need changes to implement byte swapping - * on hash table file I/O. Or you could just use it as-is if you don't care - * that your database files will be unreadable on little-endian systems. */ - -#define _FILE_OFFSET_BITS 64 - -#include "kissdb.h" - -#include -#include -#include - -#ifdef _WIN32 -#define fseeko _fseeki64 -#define ftello _ftelli64 -#endif - -#define KISSDB_HEADER_SIZE ((sizeof(uint64_t) * 3) + 4) - -/* djb2 hash function */ -static uint64_t KISSDB_hash(const void *b,unsigned long len) -{ - unsigned long i; - uint64_t hash = 5381; - for(i=0;if = (FILE *)0; - fopen_s(&db->f,path,((mode == KISSDB_OPEN_MODE_RWREPLACE) ? "w+b" : (((mode == KISSDB_OPEN_MODE_RDWR)||(mode == KISSDB_OPEN_MODE_RWCREAT)) ? "r+b" : "rb"))); -#else - db->f = fopen(path,((mode == KISSDB_OPEN_MODE_RWREPLACE) ? "w+b" : (((mode == KISSDB_OPEN_MODE_RDWR)||(mode == KISSDB_OPEN_MODE_RWCREAT)) ? "r+b" : "rb"))); -#endif - if (!db->f) { - if (mode == KISSDB_OPEN_MODE_RWCREAT) { -#ifdef _WIN32 - db->f = (FILE *)0; - fopen_s(&db->f,path,"w+b"); -#else - db->f = fopen(path,"w+b"); -#endif - } - if (!db->f) - return KISSDB_ERROR_IO; - } - - if (fseeko(db->f,0,SEEK_END)) { - fclose(db->f); - return KISSDB_ERROR_IO; - } - if (ftello(db->f) < KISSDB_HEADER_SIZE) { - /* write header if not already present */ - if ((hash_table_size)&&(key_size)&&(value_size)) { - if (fseeko(db->f,0,SEEK_SET)) { fclose(db->f); return KISSDB_ERROR_IO; } - tmp2[0] = 'K'; tmp2[1] = 'd'; tmp2[2] = 'B'; tmp2[3] = KISSDB_VERSION; - if (fwrite(tmp2,4,1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - tmp = hash_table_size; - if (fwrite(&tmp,sizeof(uint64_t),1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - tmp = key_size; - if (fwrite(&tmp,sizeof(uint64_t),1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - tmp = value_size; - if (fwrite(&tmp,sizeof(uint64_t),1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - fflush(db->f); - } else { - fclose(db->f); - return KISSDB_ERROR_INVALID_PARAMETERS; - } - } else { - if (fseeko(db->f,0,SEEK_SET)) { fclose(db->f); return KISSDB_ERROR_IO; } - if (fread(tmp2,4,1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - if ((tmp2[0] != 'K')||(tmp2[1] != 'd')||(tmp2[2] != 'B')||(tmp2[3] != KISSDB_VERSION)) { - fclose(db->f); - return KISSDB_ERROR_CORRUPT_DBFILE; - } - if (fread(&tmp,sizeof(uint64_t),1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - if (!tmp) { - fclose(db->f); - return KISSDB_ERROR_CORRUPT_DBFILE; - } - hash_table_size = (unsigned long)tmp; - if (fread(&tmp,sizeof(uint64_t),1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - if (!tmp) { - fclose(db->f); - return KISSDB_ERROR_CORRUPT_DBFILE; - } - key_size = (unsigned long)tmp; - if (fread(&tmp,sizeof(uint64_t),1,db->f) != 1) { fclose(db->f); return KISSDB_ERROR_IO; } - if (!tmp) { - fclose(db->f); - return KISSDB_ERROR_CORRUPT_DBFILE; - } - value_size = (unsigned long)tmp; - } - - db->hash_table_size = hash_table_size; - db->key_size = key_size; - db->value_size = value_size; - db->hash_table_size_bytes = sizeof(uint64_t) * (hash_table_size + 1); /* [hash_table_size] == next table */ - - httmp = malloc(db->hash_table_size_bytes); - if (!httmp) { - fclose(db->f); - return KISSDB_ERROR_MALLOC; - } - db->num_hash_tables = 0; - db->hash_tables = (uint64_t *)0; - while (fread(httmp,db->hash_table_size_bytes,1,db->f) == 1) { - hash_tables_rea = realloc(db->hash_tables,db->hash_table_size_bytes * (db->num_hash_tables + 1)); - if (!hash_tables_rea) { - KISSDB_close(db); - free(httmp); - return KISSDB_ERROR_MALLOC; - } - db->hash_tables = hash_tables_rea; - - memcpy(((uint8_t *)db->hash_tables) + (db->hash_table_size_bytes * db->num_hash_tables),httmp,db->hash_table_size_bytes); - ++db->num_hash_tables; - if (httmp[db->hash_table_size]) { - if (fseeko(db->f,httmp[db->hash_table_size],SEEK_SET)) { - KISSDB_close(db); - free(httmp); - return KISSDB_ERROR_IO; - } - } else break; - } - free(httmp); - - return 0; -} - -void KISSDB_close(KISSDB *db) -{ - if (db->hash_tables) - free(db->hash_tables); - if (db->f) - fclose(db->f); - memset(db,0,sizeof(KISSDB)); -} - -int KISSDB_get(KISSDB *db,const void *key,void *vbuf) -{ - uint8_t tmp[4096]; - const uint8_t *kptr; - unsigned long klen,i; - uint64_t hash = KISSDB_hash(key,db->key_size) % (uint64_t)db->hash_table_size; - uint64_t offset; - uint64_t *cur_hash_table; - long n; - - cur_hash_table = db->hash_tables; - for(i=0;inum_hash_tables;++i) { - offset = cur_hash_table[hash]; - if (offset) { - if (fseeko(db->f,offset,SEEK_SET)) - return KISSDB_ERROR_IO; - - kptr = (const uint8_t *)key; - klen = db->key_size; - while (klen) { - n = (long)fread(tmp,1,(klen > sizeof(tmp)) ? sizeof(tmp) : klen,db->f); - if (n > 0) { - if (memcmp(kptr,tmp,n)) - goto get_no_match_next_hash_table; - kptr += n; - klen -= (unsigned long)n; - } else return 1; /* not found */ - } - - if (fread(vbuf,db->value_size,1,db->f) == 1) - return 0; /* success */ - else return KISSDB_ERROR_IO; - } else return 1; /* not found */ -get_no_match_next_hash_table: - cur_hash_table += db->hash_table_size + 1; - } - - return 1; /* not found */ -} - -int KISSDB_put(KISSDB *db,const void *key,const void *value) -{ - uint8_t tmp[4096]; - const uint8_t *kptr; - unsigned long klen,i; - uint64_t hash = KISSDB_hash(key,db->key_size) % (uint64_t)db->hash_table_size; - uint64_t offset; - uint64_t htoffset,lasthtoffset; - uint64_t endoffset; - uint64_t *cur_hash_table; - uint64_t *hash_tables_rea; - long n; - - lasthtoffset = htoffset = KISSDB_HEADER_SIZE; - cur_hash_table = db->hash_tables; - for(i=0;inum_hash_tables;++i) { - offset = cur_hash_table[hash]; - if (offset) { - /* rewrite if already exists */ - if (fseeko(db->f,offset,SEEK_SET)) - return KISSDB_ERROR_IO; - - kptr = (const uint8_t *)key; - klen = db->key_size; - while (klen) { - n = (long)fread(tmp,1,(klen > sizeof(tmp)) ? sizeof(tmp) : klen,db->f); - if (n > 0) { - if (memcmp(kptr,tmp,n)) - goto put_no_match_next_hash_table; - kptr += n; - klen -= (unsigned long)n; - } - } - - /* C99 spec demands seek after fread(), required for Windows */ - fseeko(db->f,0,SEEK_CUR); - - if (fwrite(value,db->value_size,1,db->f) == 1) { - fflush(db->f); - return 0; /* success */ - } else return KISSDB_ERROR_IO; - } else { - /* add if an empty hash table slot is discovered */ - if (fseeko(db->f,0,SEEK_END)) - return KISSDB_ERROR_IO; - endoffset = ftello(db->f); - - if (fwrite(key,db->key_size,1,db->f) != 1) - return KISSDB_ERROR_IO; - if (fwrite(value,db->value_size,1,db->f) != 1) - return KISSDB_ERROR_IO; - - if (fseeko(db->f,htoffset + (sizeof(uint64_t) * hash),SEEK_SET)) - return KISSDB_ERROR_IO; - if (fwrite(&endoffset,sizeof(uint64_t),1,db->f) != 1) - return KISSDB_ERROR_IO; - cur_hash_table[hash] = endoffset; - - fflush(db->f); - - return 0; /* success */ - } -put_no_match_next_hash_table: - lasthtoffset = htoffset; - htoffset = cur_hash_table[db->hash_table_size]; - cur_hash_table += (db->hash_table_size + 1); - } - - /* if no existing slots, add a new page of hash table entries */ - if (fseeko(db->f,0,SEEK_END)) - return KISSDB_ERROR_IO; - endoffset = ftello(db->f); - - hash_tables_rea = realloc(db->hash_tables,db->hash_table_size_bytes * (db->num_hash_tables + 1)); - if (!hash_tables_rea) - return KISSDB_ERROR_MALLOC; - db->hash_tables = hash_tables_rea; - cur_hash_table = &(db->hash_tables[(db->hash_table_size + 1) * db->num_hash_tables]); - memset(cur_hash_table,0,db->hash_table_size_bytes); - - cur_hash_table[hash] = endoffset + db->hash_table_size_bytes; /* where new entry will go */ - - if (fwrite(cur_hash_table,db->hash_table_size_bytes,1,db->f) != 1) - return KISSDB_ERROR_IO; - - if (fwrite(key,db->key_size,1,db->f) != 1) - return KISSDB_ERROR_IO; - if (fwrite(value,db->value_size,1,db->f) != 1) - return KISSDB_ERROR_IO; - - if (db->num_hash_tables) { - if (fseeko(db->f,lasthtoffset + (sizeof(uint64_t) * db->hash_table_size),SEEK_SET)) - return KISSDB_ERROR_IO; - if (fwrite(&endoffset,sizeof(uint64_t),1,db->f) != 1) - return KISSDB_ERROR_IO; - db->hash_tables[((db->hash_table_size + 1) * (db->num_hash_tables - 1)) + db->hash_table_size] = endoffset; - } - - ++db->num_hash_tables; - - fflush(db->f); - - return 0; /* success */ -} - -void KISSDB_Iterator_init(KISSDB *db,KISSDB_Iterator *dbi) -{ - dbi->db = db; - dbi->h_no = 0; - dbi->h_idx = 0; -} - -int KISSDB_Iterator_next(KISSDB_Iterator *dbi,void *kbuf,void *vbuf) -{ - uint64_t offset; - - if ((dbi->h_no < dbi->db->num_hash_tables)&&(dbi->h_idx < dbi->db->hash_table_size)) { - while (!(offset = dbi->db->hash_tables[((dbi->db->hash_table_size + 1) * dbi->h_no) + dbi->h_idx])) { - if (++dbi->h_idx >= dbi->db->hash_table_size) { - dbi->h_idx = 0; - if (++dbi->h_no >= dbi->db->num_hash_tables) - return 0; - } - } - if (fseeko(dbi->db->f,offset,SEEK_SET)) - return KISSDB_ERROR_IO; - if (fread(kbuf,dbi->db->key_size,1,dbi->db->f) != 1) - return KISSDB_ERROR_IO; - if (fread(vbuf,dbi->db->value_size,1,dbi->db->f) != 1) - return KISSDB_ERROR_IO; - if (++dbi->h_idx >= dbi->db->hash_table_size) { - dbi->h_idx = 0; - ++dbi->h_no; - } - return 1; - } - - return 0; -} - -#ifdef KISSDB_TEST - -#include - -int main(int argc,char **argv) -{ - uint64_t i,j; - uint64_t v[8]; - KISSDB db; - KISSDB_Iterator dbi; - char got_all_values[10000]; - int q; - - printf("Opening new empty database test.db...\n"); - - if (KISSDB_open(&db,"test.db",KISSDB_OPEN_MODE_RWREPLACE,1024,8,sizeof(v))) { - printf("KISSDB_open failed\n"); - return 1; - } - - printf("Adding and then re-getting 10000 64-byte values...\n"); - - for(i=0;i<10000;++i) { - for(j=0;j<8;++j) - v[j] = i; - if (KISSDB_put(&db,&i,v)) { - printf("KISSDB_put failed (%"PRIu64")\n",i); - return 1; - } - memset(v,0,sizeof(v)); - if ((q = KISSDB_get(&db,&i,v))) { - printf("KISSDB_get (1) failed (%"PRIu64") (%d)\n",i,q); - return 1; - } - for(j=0;j<8;++j) { - if (v[j] != i) { - printf("KISSDB_get (1) failed, bad data (%"PRIu64")\n",i); - return 1; - } - } - } - - printf("Getting 10000 64-byte values...\n"); - - for(i=0;i<10000;++i) { - if ((q = KISSDB_get(&db,&i,v))) { - printf("KISSDB_get (2) failed (%"PRIu64") (%d)\n",i,q); - return 1; - } - for(j=0;j<8;++j) { - if (v[j] != i) { - printf("KISSDB_get (2) failed, bad data (%"PRIu64")\n",i); - return 1; - } - } - } - - printf("Closing and re-opening database in read-only mode...\n"); - - KISSDB_close(&db); - - if (KISSDB_open(&db,"test.db",KISSDB_OPEN_MODE_RDONLY,1024,8,sizeof(v))) { - printf("KISSDB_open failed\n"); - return 1; - } - - printf("Getting 10000 64-byte values...\n"); - - for(i=0;i<10000;++i) { - if ((q = KISSDB_get(&db,&i,v))) { - printf("KISSDB_get (3) failed (%"PRIu64") (%d)\n",i,q); - return 1; - } - for(j=0;j<8;++j) { - if (v[j] != i) { - printf("KISSDB_get (3) failed, bad data (%"PRIu64")\n",i); - return 1; - } - } - } - - printf("Iterator test...\n"); - - KISSDB_Iterator_init(&db,&dbi); - i = 0xdeadbeef; - memset(got_all_values,0,sizeof(got_all_values)); - while (KISSDB_Iterator_next(&dbi,&i,&v) > 0) { - if (i < 10000) - got_all_values[i] = 1; - else { - printf("KISSDB_Iterator_next failed, bad data (%"PRIu64")\n",i); - return 1; - } - } - for(i=0;i<10000;++i) { - if (!got_all_values[i]) { - printf("KISSDB_Iterator failed, missing value index %"PRIu64"\n",i); - return 1; - } - } - - KISSDB_close(&db); - - printf("All tests OK!\n"); - - return 0; -} - -#endif diff --git a/ext/kissdb/kissdb.h b/ext/kissdb/kissdb.h deleted file mode 100644 index 926906b0..00000000 --- a/ext/kissdb/kissdb.h +++ /dev/null @@ -1,173 +0,0 @@ -/* (Keep It) Simple Stupid Database - * - * Written by Adam Ierymenko - * KISSDB is in the public domain and is distributed with NO WARRANTY. - * - * http://creativecommons.org/publicdomain/zero/1.0/ */ - -#ifndef ___KISSDB_H -#define ___KISSDB_H - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * Version: 2 - * - * This is the file format identifier, and changes any time the file - * format changes. The code version will be this dot something, and can - * be seen in tags in the git repository. - */ -#define KISSDB_VERSION 2 - -/** - * KISSDB database state - * - * These fields can be read by a user, e.g. to look up key_size and - * value_size, but should never be changed. - */ -typedef struct { - unsigned long hash_table_size; - unsigned long key_size; - unsigned long value_size; - unsigned long hash_table_size_bytes; - unsigned long num_hash_tables; - uint64_t *hash_tables; - FILE *f; -} KISSDB; - -/** - * I/O error or file not found - */ -#define KISSDB_ERROR_IO -1 - -/** - * Out of memory - */ -#define KISSDB_ERROR_MALLOC -2 - -/** - * Invalid paramters (e.g. missing _size paramters on init to create database) - */ -#define KISSDB_ERROR_INVALID_PARAMETERS -3 - -/** - * Database file appears corrupt - */ -#define KISSDB_ERROR_CORRUPT_DBFILE -4 - -/** - * Open mode: read only - */ -#define KISSDB_OPEN_MODE_RDONLY 1 - -/** - * Open mode: read/write - */ -#define KISSDB_OPEN_MODE_RDWR 2 - -/** - * Open mode: read/write, create if doesn't exist - */ -#define KISSDB_OPEN_MODE_RWCREAT 3 - -/** - * Open mode: truncate database, open for reading and writing - */ -#define KISSDB_OPEN_MODE_RWREPLACE 4 - -/** - * Open database - * - * The three _size parameters must be specified if the database could - * be created or re-created. Otherwise an error will occur. If the - * database already exists, these parameters are ignored and are read - * from the database. You can check the struture afterwords to see what - * they were. - * - * @param db Database struct - * @param path Path to file - * @param mode One of the KISSDB_OPEN_MODE constants - * @param hash_table_size Size of hash table in 64-bit entries (must be >0) - * @param key_size Size of keys in bytes - * @param value_size Size of values in bytes - * @return 0 on success, nonzero on error - */ -extern int KISSDB_open( - KISSDB *db, - const char *path, - int mode, - unsigned long hash_table_size, - unsigned long key_size, - unsigned long value_size); - -/** - * Close database - * - * @param db Database struct - */ -extern void KISSDB_close(KISSDB *db); - -/** - * Get an entry - * - * @param db Database struct - * @param key Key (key_size bytes) - * @param vbuf Value buffer (value_size bytes capacity) - * @return -1 on I/O error, 0 on success, 1 on not found - */ -extern int KISSDB_get(KISSDB *db,const void *key,void *vbuf); - -/** - * Put an entry (overwriting it if it already exists) - * - * In the already-exists case the size of the database file does not - * change. - * - * @param db Database struct - * @param key Key (key_size bytes) - * @param value Value (value_size bytes) - * @return -1 on I/O error, 0 on success - */ -extern int KISSDB_put(KISSDB *db,const void *key,const void *value); - -/** - * Cursor used for iterating over all entries in database - */ -typedef struct { - KISSDB *db; - unsigned long h_no; - unsigned long h_idx; -} KISSDB_Iterator; - -/** - * Initialize an iterator - * - * @param db Database struct - * @param i Iterator to initialize - */ -extern void KISSDB_Iterator_init(KISSDB *db,KISSDB_Iterator *dbi); - -/** - * Get the next entry - * - * The order of entries returned by iterator is undefined. It depends on - * how keys hash. - * - * @param Database iterator - * @param kbuf Buffer to fill with next key (key_size bytes) - * @param vbuf Buffer to fill with next value (value_size bytes) - * @return 0 if there are no more entries, negative on error, positive if an kbuf/vbuf have been filled - */ -extern int KISSDB_Iterator_next(KISSDB_Iterator *dbi,void *kbuf,void *vbuf); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/ext/vsdm/LICENSE.txt b/ext/vsdm/LICENSE.txt deleted file mode 100644 index 00b65473..00000000 --- a/ext/vsdm/LICENSE.txt +++ /dev/null @@ -1,22 +0,0 @@ -MIT LICENSE - -Copyright 2017 ZeroTier, Inc. -https://www.zerotier.com/ - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to -deal in the Software without restriction, including without limitation the -rights to use, copy, modify, merge, publish, distribute, sublicense, and/or -sell copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/ext/vsdm/Makefile b/ext/vsdm/Makefile deleted file mode 100644 index 828d6edc..00000000 --- a/ext/vsdm/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -all: - c++ -Os -std=c++11 -o vsdm-test vsdm-test.cpp - -clean: - rm -f vsdm-test *.o *.dSYM diff --git a/ext/vsdm/README.md b/ext/vsdm/README.md deleted file mode 100644 index 03354be6..00000000 --- a/ext/vsdm/README.md +++ /dev/null @@ -1,40 +0,0 @@ -VSDM: Very Simple Distributed Map -====== - -VSDM is a super-minimal replicated in-memory associative container. Its advantages are small code size, small footprint, simplicity, and lack of dependencies. - -VSDM uses a rumor mill replication algorithm that provides fast best-effort replication. If connectivity is stable this results in eventual consistency, but data loss or regression can occur under split brain conditions. This class is not recommended for data that is intolerant of loss or regression. Its ideal use case is a distributed cache for small data objects or a distributed database for ephemeral data. - -Transport is via TCP and can optionally be encrypted if one specifies a cryptor class (see below). The transport protocol does not implement any features for versioning or backward compatibility and changes to key/value type, cryptor, or other relevant parameters will render it incompatible. Again this is designed for simple app-embedded use cases. Use something more feature complete if you need to support different versions across the network. - -Each node maintains a 64-bit monotonically increasing revision counter that starts at zero. When a node connects to another node it sends this revision counter and the other party will send all updates with revision numbers greater than or equal to it. When a node receives an update it replaces the entry it has if the revision counter is higher and also sets its own revision counter to the new counter if it is higher. It then re-transmits the update if a replace event occurred (if the new update was newer). This is the "rumor mill" part: each node retransmits all changes to all other connected nodes (excluding the source). - -VSDM nodes can be connected according to any arbitrary connectivity graph. A more fully connected graph trades increased bandwidth consumption (due to redundant messages) for decreased likelihood of data loss or split brain conditions. - -The VSDM class is thread safe. It launches a single background thread to handle network I/O and periodic cleanup. Deleted keys are purged from memory after a period of time to allow time for propagation and possible re-propagation. - -## Template parameters - -VSDM supports a number of template parameters for customization. The only required parameters are K and V, the key and value types. The default serializer allows these to be one of the *uintX_t* stdint numeric types or *std::string*. Specify a different serializer for anything else. - -Here are the other parameters after K and V (in order): - - * **L**: Maximum message length, which also limits the max size of the combined key and value for a given entry. This imposes a sanity limit to prevent memory exhaustion. Default is 131072. Absolute max is UINT32_MAX - 4 (4294967291). - * **W**: Watcher function type. Default is `vsdm_watcher_noop` which does nothing. The watcher function (or function object) is passed into the constructor and receives notifications of remotely initiated changes to the map's contents. (Local changes via set() or del() do not trigger the watcher). The watcher must have the following methods: - * `void add(uint64_t remoteNodeId,const K &key,const V &value,uint64_t revision)` - * `void update(uint64_t remoteNodeId,const K &key,const V &value,uint64_t revision)` - * `void del(uint64_t remoteNodeId,const K &key)` - * **S**: Serializer class, which must contain *static* methods for serializing and deserializing keys and values. The following must be present for both key and value types: - * `unsigned long objectSize(const [K|V] &)` - * `const char *objectData(const [K|V] &)` - * `bool objectDeserialize(const char *,unsigned long,[K|V] &)` (false return means object was invalid and causes disconnect) - * **C**: Cryptor type to encrypt transport, default is `vsdm_cryptor_noop` (no encryption). It is also passed into the constructor for internal initialization. This must implement a static method `static unsigned long overhead()` that returns the *constant* per-message overhead for things like IV and MAC, and two methods to encrypt and decrypt the payload in place. The vsdm class will add space for overhead at the *end* of the message. Encrypt/decrypt mathods have these signatures: - * `void encrypt(void *,unsigned long)` - * `bool decrypt(void *,unsigned long)` (false return means invalid MAC and causes disconnect) - * **M**: Map type for underlying storage. Default is `std::unordered_map` with default STL hashers. Substitute a different container if you need to deal with non-hashable keys or need a sorted map. Containers supporting duplicate keys should not be used as the replication algorithm will not function properly. - -## License - -(c)2017 ZeroTier, Inc. (MIT license) - -Written by [Adam Ierymenko](https://github.com/adamierymenko) diff --git a/ext/vsdm/vsdm-test.cpp b/ext/vsdm/vsdm-test.cpp deleted file mode 100644 index 13c49afe..00000000 --- a/ext/vsdm/vsdm-test.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include - -#include - -#define VSDM_DEBUG 1 - -#include "vsdm.hpp" - -int main(int argc,char **argv) -{ - if (argc < 4) { - printf("Usage: vsdm-test [//]\n"); - return 0; - } - - uint64_t id = (uint64_t)strtoull(argv[1],(char **)0,10); - uint64_t node = (uint64_t)strtoull(argv[2],(char **)0,10); - int port = (int)strtol(argv[3],(char **)0,10); - - struct sockaddr_in sa; - memset(&sa,0,sizeof(sa)); - sa.sin_family = AF_INET; - sa.sin_port = htons((uint16_t)port); - - vsdm m(id,node,false); - m.listen((const struct sockaddr *)&sa); - - for(int i=4;i - * License: MIT - */ - -#ifndef ZT_VSDM_HPP__ -#define ZT_VSDM_HPP__ - -#include -#include -#include - -#if defined(_WIN32) || defined(_WIN64) - -#include -#include -#include - -#define ZT_PHY_SOCKFD_TYPE SOCKET -#define ZT_PHY_SOCKFD_NULL (INVALID_SOCKET) -#define ZT_PHY_SOCKFD_VALID(s) ((s) != INVALID_SOCKET) -#define ZT_PHY_CLOSE_SOCKET(s) ::closesocket(s) -#define ZT_PHY_MAX_SOCKETS (FD_SETSIZE) -#define ZT_PHY_MAX_INTERCEPTS ZT_PHY_MAX_SOCKETS -#define ZT_PHY_SOCKADDR_STORAGE_TYPE struct sockaddr_storage - -#else // not Windows - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define ZT_PHY_SOCKFD_TYPE int -#define ZT_PHY_SOCKFD_NULL (-1) -#define ZT_PHY_SOCKFD_VALID(s) ((s) > -1) -#define ZT_PHY_CLOSE_SOCKET(s) ::close(s) -#define ZT_PHY_MAX_SOCKETS (FD_SETSIZE) -#define ZT_PHY_MAX_INTERCEPTS ZT_PHY_MAX_SOCKETS -#define ZT_PHY_SOCKADDR_STORAGE_TYPE struct sockaddr_storage - -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/*********************************************************************************************************/ - -namespace ztVsdmInternal { - -/* This is the Phy<> adapter implementation for selected sockets from ZeroTier One. - * It should build and run out of the box on Windows and most *nix systems. Parts - * not used by VSDM have been removed. */ - -typedef void PhySocket; - -template -class Phy -{ -private: - HANDLER_PTR_TYPE _handler; - - enum PhySocketType - { - ZT_PHY_SOCKET_CLOSED = 0x00, // socket is closed, will be removed on next poll() - ZT_PHY_SOCKET_TCP_OUT_PENDING = 0x01, - ZT_PHY_SOCKET_TCP_OUT_CONNECTED = 0x02, - ZT_PHY_SOCKET_TCP_IN = 0x03, - ZT_PHY_SOCKET_TCP_LISTEN = 0x04, - ZT_PHY_SOCKET_UDP = 0x05, - ZT_PHY_SOCKET_FD = 0x06, - ZT_PHY_SOCKET_UNIX_IN = 0x07, - ZT_PHY_SOCKET_UNIX_LISTEN = 0x08 - }; - - struct PhySocketImpl - { - PhySocketType type; - ZT_PHY_SOCKFD_TYPE sock; - void *uptr; // user-settable pointer - ZT_PHY_SOCKADDR_STORAGE_TYPE saddr; // remote for TCP_OUT and TCP_IN, local for TCP_LISTEN, RAW, and UDP - }; - - std::list _socks; - fd_set _readfds; - fd_set _writefds; -#if defined(_WIN32) || defined(_WIN64) - fd_set _exceptfds; -#endif - long _nfds; - - ZT_PHY_SOCKFD_TYPE _whackReceiveSocket; - ZT_PHY_SOCKFD_TYPE _whackSendSocket; - - bool _noDelay; - bool _noCheck; - -public: - /** - * @param handler Pointer of type HANDLER_PTR_TYPE to handler - * @param noDelay If true, disable TCP NAGLE algorithm on TCP sockets - * @param noCheck If true, attempt to set UDP SO_NO_CHECK option to disable sending checksums - */ - Phy(HANDLER_PTR_TYPE handler,bool noDelay,bool noCheck) : - _handler(handler) - { - FD_ZERO(&_readfds); - FD_ZERO(&_writefds); - -#if defined(_WIN32) || defined(_WIN64) - FD_ZERO(&_exceptfds); - - SOCKET pipes[2]; - { // hack copied from StackOverflow, behaves a bit like pipe() on *nix systems - struct sockaddr_in inaddr; - struct sockaddr addr; - SOCKET lst=::socket(AF_INET, SOCK_STREAM,IPPROTO_TCP); - if (lst == INVALID_SOCKET) - throw std::runtime_error("unable to create pipes for select() abort"); - memset(&inaddr, 0, sizeof(inaddr)); - memset(&addr, 0, sizeof(addr)); - inaddr.sin_family = AF_INET; - inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - inaddr.sin_port = 0; - int yes=1; - setsockopt(lst,SOL_SOCKET,SO_REUSEADDR,(char*)&yes,sizeof(yes)); - bind(lst,(struct sockaddr *)&inaddr,sizeof(inaddr)); - listen(lst,1); - int len=sizeof(inaddr); - getsockname(lst, &addr,&len); - pipes[0]=::socket(AF_INET, SOCK_STREAM,0); - if (pipes[0] == INVALID_SOCKET) - throw std::runtime_error("unable to create pipes for select() abort"); - connect(pipes[0],&addr,len); - pipes[1]=accept(lst,0,0); - closesocket(lst); - } -#else // not Windows - int pipes[2]; - if (::pipe(pipes)) - throw std::runtime_error("unable to create pipes for select() abort"); -#endif // Windows or not - - _nfds = (pipes[0] > pipes[1]) ? (long)pipes[0] : (long)pipes[1]; - _whackReceiveSocket = pipes[0]; - _whackSendSocket = pipes[1]; - _noDelay = noDelay; - _noCheck = noCheck; - } - - ~Phy() - { - for(typename std::list::const_iterator s(_socks.begin());s!=_socks.end();++s) { - if (s->type != ZT_PHY_SOCKET_CLOSED) - this->close((PhySocket *)&(*s),true); - } - ZT_PHY_CLOSE_SOCKET(_whackReceiveSocket); - ZT_PHY_CLOSE_SOCKET(_whackSendSocket); - } - - /** - * @param s Socket object - * @return Underlying OS-type (usually int or long) file descriptor associated with object - */ - static inline ZT_PHY_SOCKFD_TYPE getDescriptor(PhySocket *s) throw() { return reinterpret_cast(s)->sock; } - - /** - * @param s Socket object - * @return Pointer to user object - */ - static inline void** getuptr(PhySocket *s) throw() { return &(reinterpret_cast(s)->uptr); } - - /** - * Cause poll() to stop waiting immediately - * - * This can be used to reset the polling loop after changes that require - * attention, or to shut down a background thread that is waiting, etc. - */ - inline void whack() - { -#if defined(_WIN32) || defined(_WIN64) - ::send(_whackSendSocket,(const char *)this,1,0); -#else - (void)(::write(_whackSendSocket,(PhySocket *)this,1)); -#endif - } - - /** - * @return Number of open sockets - */ - inline unsigned long count() const throw() { return _socks.size(); } - - /** - * @return Maximum number of sockets allowed - */ - inline unsigned long maxCount() const throw() { return ZT_PHY_MAX_SOCKETS; } - - /** - * Bind a local listen socket to listen for new TCP connections - * - * @param localAddress Local address and port - * @param uptr Initial value of uptr for new socket (default: NULL) - * @return Socket or NULL on failure to bind - */ - inline PhySocket *tcpListen(const struct sockaddr *localAddress,void *uptr = (void *)0) - { - if (_socks.size() >= ZT_PHY_MAX_SOCKETS) - return (PhySocket *)0; - - ZT_PHY_SOCKFD_TYPE s = ::socket(localAddress->sa_family,SOCK_STREAM,0); - if (!ZT_PHY_SOCKFD_VALID(s)) - return (PhySocket *)0; - -#if defined(_WIN32) || defined(_WIN64) - { - BOOL f; - f = TRUE; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f)); - f = TRUE; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f)); - f = (_noDelay ? TRUE : FALSE); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); - u_long iMode=1; - ioctlsocket(s,FIONBIO,&iMode); - } -#else - { - int f; - f = 1; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f)); - f = 1; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f)); - f = (_noDelay ? 1 : 0); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); - fcntl(s,F_SETFL,O_NONBLOCK); - } -#endif - - if (::bind(s,localAddress,(localAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))) { - ZT_PHY_CLOSE_SOCKET(s); - return (PhySocket *)0; - } - - if (::listen(s,1024)) { - ZT_PHY_CLOSE_SOCKET(s); - return (PhySocket *)0; - } - - try { - _socks.push_back(PhySocketImpl()); - } catch ( ... ) { - ZT_PHY_CLOSE_SOCKET(s); - return (PhySocket *)0; - } - PhySocketImpl &sws = _socks.back(); - - if ((long)s > _nfds) - _nfds = (long)s; - FD_SET(s,&_readfds); - sws.type = ZT_PHY_SOCKET_TCP_LISTEN; - sws.sock = s; - sws.uptr = uptr; - memset(&(sws.saddr),0,sizeof(struct sockaddr_storage)); - memcpy(&(sws.saddr),localAddress,(localAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); - - return (PhySocket *)&sws; - } - - /** - * Start a non-blocking connect; CONNECT handler is called on success or failure - * - * A return value of NULL indicates a synchronous failure such as a - * failure to open a socket. The TCP connection handler is not called - * in this case. - * - * It is possible on some platforms for an "instant connect" to occur, - * such as when connecting to a loopback address. In this case, the - * 'connected' result parameter will be set to 'true' and if the - * 'callConnectHandler' flag is true (the default) the TCP connect - * handler will be called before the function returns. - * - * These semantics can be a bit confusing, but they're less so than - * the underlying semantics of asynchronous TCP connect. - * - * @param remoteAddress Remote address - * @param connected Result parameter: set to whether an "instant connect" has occurred (true if yes) - * @param uptr Initial value of uptr for new socket (default: NULL) - * @param callConnectHandler If true, call TCP connect handler even if result is known before function exit (default: true) - * @return New socket or NULL on failure - */ - inline PhySocket *tcpConnect(const struct sockaddr *remoteAddress,bool &connected,void *uptr = (void *)0,bool callConnectHandler = true) - { - if (_socks.size() >= ZT_PHY_MAX_SOCKETS) - return (PhySocket *)0; - - ZT_PHY_SOCKFD_TYPE s = ::socket(remoteAddress->sa_family,SOCK_STREAM,0); - if (!ZT_PHY_SOCKFD_VALID(s)) { - connected = false; - return (PhySocket *)0; - } - -#if defined(_WIN32) || defined(_WIN64) - { - BOOL f; - if (remoteAddress->sa_family == AF_INET6) { f = TRUE; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(const char *)&f,sizeof(f)); } - f = TRUE; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(const char *)&f,sizeof(f)); - f = (_noDelay ? TRUE : FALSE); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); - u_long iMode=1; - ioctlsocket(s,FIONBIO,&iMode); - } -#else - { - int f; - if (remoteAddress->sa_family == AF_INET6) { f = 1; ::setsockopt(s,IPPROTO_IPV6,IPV6_V6ONLY,(void *)&f,sizeof(f)); } - f = 1; ::setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(void *)&f,sizeof(f)); - f = (_noDelay ? 1 : 0); setsockopt(s,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); - fcntl(s,F_SETFL,O_NONBLOCK); - } -#endif - - connected = true; - if (::connect(s,remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))) { - connected = false; -#if defined(_WIN32) || defined(_WIN64) - if (WSAGetLastError() != WSAEWOULDBLOCK) { -#else - if (errno != EINPROGRESS) { -#endif - ZT_PHY_CLOSE_SOCKET(s); - return (PhySocket *)0; - } // else connection is proceeding asynchronously... - } - - try { - _socks.push_back(PhySocketImpl()); - } catch ( ... ) { - ZT_PHY_CLOSE_SOCKET(s); - return (PhySocket *)0; - } - PhySocketImpl &sws = _socks.back(); - - if ((long)s > _nfds) - _nfds = (long)s; - if (connected) { - FD_SET(s,&_readfds); - sws.type = ZT_PHY_SOCKET_TCP_OUT_CONNECTED; - } else { - FD_SET(s,&_writefds); -#if defined(_WIN32) || defined(_WIN64) - FD_SET(s,&_exceptfds); -#endif - sws.type = ZT_PHY_SOCKET_TCP_OUT_PENDING; - } - sws.sock = s; - sws.uptr = uptr; - memset(&(sws.saddr),0,sizeof(struct sockaddr_storage)); - memcpy(&(sws.saddr),remoteAddress,(remoteAddress->sa_family == AF_INET6) ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); - - if ((callConnectHandler)&&(connected)) { - try { - _handler->phyOnTcpConnect((PhySocket *)&sws,&(sws.uptr),true); - } catch ( ... ) {} - } - - return (PhySocket *)&sws; - } - - /** - * Attempt to send data to a stream socket (non-blocking) - * - * If -1 is returned, the socket should no longer be used as it is now - * destroyed. If callCloseHandler is true, the close handler will be - * called before the function returns. - * - * This can be used with TCP, Unix, or socket pair sockets. - * - * @param sock An open stream socket (other socket types will fail) - * @param data Data to send - * @param len Length of data - * @param callCloseHandler If true, call close handler on socket closing failure condition (default: true) - * @return Number of bytes actually sent or -1 on fatal error (socket closure) - */ - inline long streamSend(PhySocket *sock,const void *data,unsigned long len,bool callCloseHandler = true) - { - PhySocketImpl &sws = *(reinterpret_cast(sock)); -#if defined(_WIN32) || defined(_WIN64) - long n = (long)::send(sws.sock,reinterpret_cast(data),len,0); - if (n == SOCKET_ERROR) { - switch(WSAGetLastError()) { - case WSAEINTR: - case WSAEWOULDBLOCK: - return 0; - default: - this->close(sock,callCloseHandler); - return -1; - } - } -#else // not Windows - long n = (long)::send(sws.sock,data,len,0); - if (n < 0) { - switch(errno) { -#ifdef EAGAIN - case EAGAIN: -#endif -#if defined(EWOULDBLOCK) && ( !defined(EAGAIN) || (EWOULDBLOCK != EAGAIN) ) - case EWOULDBLOCK: -#endif -#ifdef EINTR - case EINTR: -#endif - return 0; - default: - this->close(sock,callCloseHandler); - return -1; - } - } -#endif // Windows or not - return n; - } - - /** - * For streams, sets whether we want to be notified that the socket is writable - * - * This can be used with TCP, Unix, or socket pair sockets. - * - * Call whack() if this is being done from another thread and you want - * it to take effect immediately. Otherwise it is only guaranteed to - * take effect on the next poll(). - * - * @param sock Stream connection socket - * @param notifyWritable Want writable notifications? - */ - inline const void setNotifyWritable(PhySocket *sock,bool notifyWritable) - { - PhySocketImpl &sws = *(reinterpret_cast(sock)); - if (notifyWritable) { - FD_SET(sws.sock,&_writefds); - } else { - FD_CLR(sws.sock,&_writefds); - } - } - - /** - * Set whether we want to be notified that a socket is readable - * - * This is primarily for raw sockets added with wrapSocket(). It could be - * used with others, but doing so would essentially lock them and prevent - * data from being read from them until this is set to 'true' again. - * - * @param sock Socket to modify - * @param notifyReadable True if socket should be monitored for readability - */ - inline const void setNotifyReadable(PhySocket *sock,bool notifyReadable) - { - PhySocketImpl &sws = *(reinterpret_cast(sock)); - if (notifyReadable) { - FD_SET(sws.sock,&_readfds); - } else { - FD_CLR(sws.sock,&_readfds); - } - } - - /** - * Wait for activity and handle one or more events - * - * Note that this is not guaranteed to wait up to 'timeout' even - * if nothing happens, as whack() or other events such as signals - * may cause premature termination. - * - * @param timeout Timeout in milliseconds or 0 for none (forever) - */ - inline void poll(unsigned long timeout) - { - char buf[131072]; - struct sockaddr_storage ss; - struct timeval tv; - fd_set rfds,wfds,efds; - - memcpy(&rfds,&_readfds,sizeof(rfds)); - memcpy(&wfds,&_writefds,sizeof(wfds)); -#if defined(_WIN32) || defined(_WIN64) - memcpy(&efds,&_exceptfds,sizeof(efds)); -#else - FD_ZERO(&efds); -#endif - - tv.tv_sec = (long)(timeout / 1000); - tv.tv_usec = (long)((timeout % 1000) * 1000); - if (::select((int)_nfds + 1,&rfds,&wfds,&efds,(timeout > 0) ? &tv : (struct timeval *)0) <= 0) - return; - - if (FD_ISSET(_whackReceiveSocket,&rfds)) { - char tmp[16]; -#if defined(_WIN32) || defined(_WIN64) - ::recv(_whackReceiveSocket,tmp,16,0); -#else - ::read(_whackReceiveSocket,tmp,16); -#endif - } - - for(typename std::list::iterator s(_socks.begin());s!=_socks.end();) { - switch (s->type) { - - case ZT_PHY_SOCKET_TCP_OUT_PENDING: -#if defined(_WIN32) || defined(_WIN64) - if (FD_ISSET(s->sock,&efds)) { - this->close((PhySocket *)&(*s),true); - } else // ... if -#endif - if (FD_ISSET(s->sock,&wfds)) { - socklen_t slen = sizeof(ss); - if (::getpeername(s->sock,(struct sockaddr *)&ss,&slen) != 0) { - this->close((PhySocket *)&(*s),true); - } else { - s->type = ZT_PHY_SOCKET_TCP_OUT_CONNECTED; - FD_SET(s->sock,&_readfds); - FD_CLR(s->sock,&_writefds); -#if defined(_WIN32) || defined(_WIN64) - FD_CLR(s->sock,&_exceptfds); -#endif - try { - _handler->phyOnTcpConnect((PhySocket *)&(*s),&(s->uptr),true); - } catch ( ... ) {} - } - } - break; - - case ZT_PHY_SOCKET_TCP_OUT_CONNECTED: - case ZT_PHY_SOCKET_TCP_IN: { - ZT_PHY_SOCKFD_TYPE sock = s->sock; // if closed, s->sock becomes invalid as s is no longer dereferencable - if (FD_ISSET(sock,&rfds)) { - long n = (long)::recv(sock,buf,sizeof(buf),0); - if (n <= 0) { - this->close((PhySocket *)&(*s),true); - } else { - try { - _handler->phyOnTcpData((PhySocket *)&(*s),&(s->uptr),(void *)buf,(unsigned long)n); - } catch ( ... ) {} - } - } - if ((FD_ISSET(sock,&wfds))&&(FD_ISSET(sock,&_writefds))) { - try { - _handler->phyOnTcpWritable((PhySocket *)&(*s),&(s->uptr)); - } catch ( ... ) {} - } - } break; - - case ZT_PHY_SOCKET_TCP_LISTEN: - if (FD_ISSET(s->sock,&rfds)) { - memset(&ss,0,sizeof(ss)); - socklen_t slen = sizeof(ss); - ZT_PHY_SOCKFD_TYPE newSock = ::accept(s->sock,(struct sockaddr *)&ss,&slen); - if (ZT_PHY_SOCKFD_VALID(newSock)) { - if (_socks.size() >= ZT_PHY_MAX_SOCKETS) { - ZT_PHY_CLOSE_SOCKET(newSock); - } else { -#if defined(_WIN32) || defined(_WIN64) - { BOOL f = (_noDelay ? TRUE : FALSE); setsockopt(newSock,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); } - { u_long iMode=1; ioctlsocket(newSock,FIONBIO,&iMode); } -#else - { int f = (_noDelay ? 1 : 0); setsockopt(newSock,IPPROTO_TCP,TCP_NODELAY,(char *)&f,sizeof(f)); } - fcntl(newSock,F_SETFL,O_NONBLOCK); -#endif - _socks.push_back(PhySocketImpl()); - PhySocketImpl &sws = _socks.back(); - FD_SET(newSock,&_readfds); - if ((long)newSock > _nfds) - _nfds = (long)newSock; - sws.type = ZT_PHY_SOCKET_TCP_IN; - sws.sock = newSock; - sws.uptr = (void *)0; - memcpy(&(sws.saddr),&ss,sizeof(struct sockaddr_storage)); - try { - _handler->phyOnTcpAccept((PhySocket *)&(*s),(PhySocket *)&(_socks.back()),&(s->uptr),&(sws.uptr),(const struct sockaddr *)&(sws.saddr)); - } catch ( ... ) {} - } - } - } - break; - - case ZT_PHY_SOCKET_UDP: - if (FD_ISSET(s->sock,&rfds)) { - for(;;) { - memset(&ss,0,sizeof(ss)); - socklen_t slen = sizeof(ss); - long n = (long)::recvfrom(s->sock,buf,sizeof(buf),0,(struct sockaddr *)&ss,&slen); - if (n > 0) { - try { - _handler->phyOnDatagram((PhySocket *)&(*s),&(s->uptr),(const struct sockaddr *)&(s->saddr),(const struct sockaddr *)&ss,(void *)buf,(unsigned long)n); - } catch ( ... ) {} - } else if (n < 0) - break; - } - } - break; - - case ZT_PHY_SOCKET_UNIX_IN: { -#ifdef __UNIX_LIKE__ - ZT_PHY_SOCKFD_TYPE sock = s->sock; // if closed, s->sock becomes invalid as s is no longer dereferencable - if ((FD_ISSET(sock,&wfds))&&(FD_ISSET(sock,&_writefds))) { - try { - _handler->phyOnUnixWritable((PhySocket *)&(*s),&(s->uptr),false); - } catch ( ... ) {} - } - if (FD_ISSET(sock,&rfds)) { - long n = (long)::read(sock,buf,sizeof(buf)); - if (n <= 0) { - this->close((PhySocket *)&(*s),true); - } else { - try { - _handler->phyOnUnixData((PhySocket *)&(*s),&(s->uptr),(void *)buf,(unsigned long)n); - } catch ( ... ) {} - } - } -#endif // __UNIX_LIKE__ - } break; - - case ZT_PHY_SOCKET_UNIX_LISTEN: -#ifdef __UNIX_LIKE__ - if (FD_ISSET(s->sock,&rfds)) { - memset(&ss,0,sizeof(ss)); - socklen_t slen = sizeof(ss); - ZT_PHY_SOCKFD_TYPE newSock = ::accept(s->sock,(struct sockaddr *)&ss,&slen); - if (ZT_PHY_SOCKFD_VALID(newSock)) { - if (_socks.size() >= ZT_PHY_MAX_SOCKETS) { - ZT_PHY_CLOSE_SOCKET(newSock); - } else { - fcntl(newSock,F_SETFL,O_NONBLOCK); - _socks.push_back(PhySocketImpl()); - PhySocketImpl &sws = _socks.back(); - FD_SET(newSock,&_readfds); - if ((long)newSock > _nfds) - _nfds = (long)newSock; - sws.type = ZT_PHY_SOCKET_UNIX_IN; - sws.sock = newSock; - sws.uptr = (void *)0; - memcpy(&(sws.saddr),&ss,sizeof(struct sockaddr_storage)); - try { - //_handler->phyOnUnixAccept((PhySocket *)&(*s),(PhySocket *)&(_socks.back()),&(s->uptr),&(sws.uptr)); - } catch ( ... ) {} - } - } - } -#endif // __UNIX_LIKE__ - break; - - case ZT_PHY_SOCKET_FD: { - ZT_PHY_SOCKFD_TYPE sock = s->sock; - const bool readable = ((FD_ISSET(sock,&rfds))&&(FD_ISSET(sock,&_readfds))); - const bool writable = ((FD_ISSET(sock,&wfds))&&(FD_ISSET(sock,&_writefds))); - if ((readable)||(writable)) { - try { - //_handler->phyOnFileDescriptorActivity((PhySocket *)&(*s),&(s->uptr),readable,writable); - } catch ( ... ) {} - } - } break; - - default: - break; - - } - - if (s->type == ZT_PHY_SOCKET_CLOSED) - _socks.erase(s++); - else ++s; - } - } - - /** - * @param sock Socket to close - * @param callHandlers If true, call handlers for TCP connect (success: false) or close (default: true) - */ - inline void close(PhySocket *sock,bool callHandlers = true) - { - if (!sock) - return; - PhySocketImpl &sws = *(reinterpret_cast(sock)); - if (sws.type == ZT_PHY_SOCKET_CLOSED) - return; - - FD_CLR(sws.sock,&_readfds); - FD_CLR(sws.sock,&_writefds); -#if defined(_WIN32) || defined(_WIN64) - FD_CLR(sws.sock,&_exceptfds); -#endif - - if (sws.type != ZT_PHY_SOCKET_FD) - ZT_PHY_CLOSE_SOCKET(sws.sock); - -#ifdef __UNIX_LIKE__ - if (sws.type == ZT_PHY_SOCKET_UNIX_LISTEN) - ::unlink(((struct sockaddr_un *)(&(sws.saddr)))->sun_path); -#endif // __UNIX_LIKE__ - - if (callHandlers) { - switch(sws.type) { - case ZT_PHY_SOCKET_TCP_OUT_PENDING: - try { - _handler->phyOnTcpConnect(sock,&(sws.uptr),false); - } catch ( ... ) {} - break; - case ZT_PHY_SOCKET_TCP_OUT_CONNECTED: - case ZT_PHY_SOCKET_TCP_IN: - try { - _handler->phyOnTcpClose(sock,&(sws.uptr)); - } catch ( ... ) {} - break; - case ZT_PHY_SOCKET_UNIX_IN: -#ifdef __UNIX_LIKE__ - try { - _handler->phyOnUnixClose(sock,&(sws.uptr)); - } catch ( ... ) {} -#endif // __UNIX_LIKE__ - break; - default: - break; - } - } - - // Causes entry to be deleted from list in poll(), ignored elsewhere - sws.type = ZT_PHY_SOCKET_CLOSED; - - if ((long)sws.sock >= (long)_nfds) { - long nfds = (long)_whackSendSocket; - if ((long)_whackReceiveSocket > nfds) - nfds = (long)_whackReceiveSocket; - for(typename std::list::iterator s(_socks.begin());s!=_socks.end();++s) { - if ((s->type != ZT_PHY_SOCKET_CLOSED)&&((long)s->sock > nfds)) - nfds = (long)s->sock; - } - _nfds = nfds; - } - } -}; - -static inline uint64_t _swap64(const uint64_t n) -{ - return ( - ((n & 0x00000000000000FFULL) << 56) | - ((n & 0x000000000000FF00ULL) << 40) | - ((n & 0x0000000000FF0000ULL) << 24) | - ((n & 0x00000000FF000000ULL) << 8) | - ((n & 0x000000FF00000000ULL) >> 8) | - ((n & 0x0000FF0000000000ULL) >> 24) | - ((n & 0x00FF000000000000ULL) >> 40) | - ((n & 0xFF00000000000000ULL) >> 56) - ); -} - -} // namespace ztVsdmInternal - -/*********************************************************************************************************/ - -/** - * No-op update watcher - */ -class vsdm_watcher_noop -{ -public: - template - inline void add(uint64_t,const K &k,const V &v,uint64_t) {} - template - inline void update(uint64_t,const K &k,const V &v,uint64_t) {} - template - inline void del(uint64_t,const K &k) {} -}; - -/** - * No-op cryptor that adds no overhead and does no encryption - */ -class vsdm_cryptor_noop -{ -public: - static inline unsigned long overhead() { return 0; } - inline void encrypt(void *d,unsigned long l) {} - inline bool decrypt(void *d,unsigned long l) { return true; } -}; - -/** - * Default serializer supporting std::string and stdint.h types - */ -class vsdm_default_serializer -{ -public: - static inline unsigned long objectSize(const std::string &o) { return o.length(); } - static inline unsigned long objectSize(const uint8_t o) { return 1; } - static inline unsigned long objectSize(const int8_t o) { return 1; } - static inline unsigned long objectSize(const uint16_t o) { return 2; } - static inline unsigned long objectSize(const int16_t o) { return 2; } - static inline unsigned long objectSize(const uint32_t o) { return 4; } - static inline unsigned long objectSize(const int32_t o) { return 4; } - static inline unsigned long objectSize(const uint64_t o) { return 8; } - static inline unsigned long objectSize(const int64_t o) { return 8; } - - static inline const char *objectData(const std::string &o) { return o.data(); } - static inline const char *objectData(const uint8_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const int8_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const uint16_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const int16_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const uint32_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const int32_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const uint64_t &o) { return reinterpret_cast(&o); } - static inline const char *objectData(const int64_t &o) { return reinterpret_cast(&o); } - - static inline bool objectDeserialize(const char *d,unsigned long l,std::string &o) { o.assign(d,l); return true; } - static inline bool objectDeserialize(const char *d,unsigned long l,uint8_t &o) { if (l == 1) { memcpy(&o,d,1); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,int8_t &o) { if (l == 1) { memcpy(&o,d,1); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,uint16_t &o) { if (l == 2) { memcpy(&o,d,2); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,int16_t &o) { if (l == 2) { memcpy(&o,d,2); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,uint32_t &o) { if (l == 4) { memcpy(&o,d,4); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,int32_t &o) { if (l == 4) { memcpy(&o,d,4); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,uint64_t &o) { if (l == 8) { memcpy(&o,d,8); return true; } else { return false; } } - static inline bool objectDeserialize(const char *d,unsigned long l,int64_t &o) { if (l == 8) { memcpy(&o,d,8); return true; } else { return false; } } -}; - -/** - * VSDM: Very Simple Distributed Map - * - * See README.md for full docs. - * - * @tparam K Key type (must be supported by serializer) - * @tparam V Value type (must be supported by serializer) - * @tparam L Maximum message length (max allowed: UINT32_MAX - 1, default: 131072) - * @tparam W Watcher function (default: vsdm_watcher_noop) - * @tparam S Serializer class with static methods to serialize keys and values (default: vsdm_default_serializer) - * @tparam C Cryptor to encrypt/decrypt and authenticate network traffic (default: vsdm_cryptor_noop) - * @tparam M Map type for underlying data store (default: std::unordered_map) - */ -template< - typename K, - typename V, - unsigned long L = 131072, - typename W = vsdm_watcher_noop, - typename S = vsdm_default_serializer, - typename C = vsdm_cryptor_noop, - template class M = std::unordered_map -> -class vsdm -{ - friend void vsdm_thread_main(void *parent); - friend class ztVsdmInternal::Phy; - -private: - struct vsdm_entry - { - vsdm_entry() : rev(0),deletedAt(0),v() {} - uint64_t rev; - uint64_t deletedAt; - V v; - }; - - struct _connection - { - _connection() : outbuf(),inbuf(),gotHello(false),node(0),sock((ztVsdmInternal::PhySocket *)0) {} - std::string outbuf; - std::string inbuf; - bool gotHello; - uint64_t node; - ztVsdmInternal::PhySocket *sock; - }; - -public: - typedef K key_type; - typedef V value_type; - - /** - * @param id Cluster ID, must be the same on all nodes - * @param node Arbitrary unique node ID - * @param restrictInbound If true, restrict inbound connections to known peer IPs (added via link()) - * @param cryptor Encryptor/decryptor instance (default: C()) - * @param watcher Watcher function instance (default: W()) - */ - vsdm(uint64_t id,uint64_t node,bool restrictInbound,const C &cryptor = C(),const W &watcher = W()) : - _node(node), - _id(id), - _rev(0), - _connections(), - _m(), - _lock(), - _phy(this,false,false), - _cryptor(cryptor), - _watcher(watcher), - _run(true), - _restrictInbound(restrictInbound), - _t(_threadMain,reinterpret_cast(this)) - { - } - - ~vsdm() - { - _run = false; - _phy.whack(); - _t.join(); - } - - /** - * @param k Key to set - * @param v New value for key - * @return Revision of entry in map - */ - inline uint64_t set(const K &k,const V &v) - { - std::lock_guard l(_lock); - - vsdm_entry &e = _m[k]; - e.rev = ++_rev; - e.deletedAt = 0; - e.v = v; - - std::vector sentToNodes; - for(typename std::unordered_map::iterator c2(_connections.begin());c2!=_connections.end();++c2) { - if ((c2->second.gotHello)&&(std::find(sentToNodes.begin(),sentToNodes.end(),c2->second.node) == sentToNodes.end())) { - sendUpdate(c2->second,k,e); - sentToNodes.push_back(c2->second.node); -#ifdef VSDM_DEBUG - fprintf(stderr,">> %lu: %s=%s\n",(unsigned long)c2->second.node,k.c_str(),v.c_str()); fflush(stderr); -#endif - } - } - _phy.whack(); - - return _rev; - } - - /** - * @param k Key to check - * @return Revision of key that we have or 0 if not found - */ - inline uint64_t have(const K &k) const - { - std::lock_guard l(_lock); - typename std::unordered_map::const_iterator i(_m.find(k)); - if ((i == _m.end())||(i->second.deletedAt)) - return 0; - return i->second.rev; - } - - /** - * @param k Key to get - * @param dfl Default value if key is not found - * @param have If non-NULL, set to revision of this key or 0 if not found - * @return Key value or dfl if not found - */ - inline V get(const K &k,const V &dfl = V(),uint64_t *have = (uint64_t *)0) const - { - std::lock_guard l(_lock); - typename std::unordered_map::const_iterator i(_m.find(k)); - if ((i == _m.end())||(i->second.deletedAt)) { - if (have) - *have = 0; - return dfl; - } - if (have) - *have = i->second.rev; - return i->second.v; - } - - /** - * @param k Key to get - * @param have If non-NULL, set to revision of this key or 0 if not found - * @return Key's value or default/empty V() if not found - */ - inline V get(const K &k,uint64_t *have) const - { - return get(k,V(),have); - } - - /** - * Erase a key - * - * Erased entries are not wholly purged from memory immediately. They - * are marked as erased and purged after sufficient time for propagation. - * - * @param k Key to erase - * @return Previous revision of this key in map or 0 if not found - */ - inline bool del(const K &k) - { - uint64_t prev = 0; - std::lock_guard l(_lock); - - typename std::unordered_map::iterator i(_m.find(k)); - if (i == _m.end()) - return 0; - prev = i->second.rev; - i->second.rev = ++_rev; - i->second.deletedAt = _rev; - i->second.v.clear(); - - std::vector sentToNodes; - for(typename std::unordered_map::iterator c2(_connections.begin());c2!=_connections.end();++c2) { - if ((c2->second.gotHello)&&(std::find(sentToNodes.begin(),sentToNodes.end(),c2->second.node) == sentToNodes.end())) { - sendUpdate(c2->second,k,i->second); - sentToNodes.push_back(c2->second.node); -#ifdef VSDM_DEBUG - fprintf(stderr,">> %lu: %s=\n",(unsigned long)c2->second.node,k.c_str()); fflush(stderr); -#endif - } - } - _phy.whack(); - - return prev; - } - - /** - * Listen for incoming node connections on an address - * - * This can be called more than once to listen on more than one address and port. - * - * @param sa Socket address - * @return True if bind succeeded - */ - inline bool listen(const struct sockaddr *sa) - { - std::lock_guard l(_lock); - return (_phy.tcpListen(sa) != (ztVsdmInternal::PhySocket *)0); - } - - /** - * Add a remote node endpoint - * - * This can be called for an arbitrary number of other endpoints in the - * network to tell this node to attempt to maintain a link to them. - * - * @param node Node ID of remote - * @param sa Socket address of remote - * @param salen Length of socket address structure - */ - inline void link(uint64_t node,const struct sockaddr_in *sa,unsigned int salen) - { - std::lock_guard l(_lock); - if ((node != _node)&&(salen <= sizeof(struct sockaddr_storage))) - memcpy(&(_peers[node]),sa,salen); - } - - /** - * @return Node IDs of nodes that are currently connected - */ - inline std::vector who() const - { - std::vector w; - std::lock_guard l(_lock); - for(typename std::unordered_map::const_iterator i(_connections.begin());i!=_connections.end();++i) { - if ((i->gotHello)&&(std::find(w.begin(),w.end(),i->second.node) == w.end())) - w.push_back(i->second.node); - } - return w; - } - - /** - * @return True if we are currently connected to at least one other node - */ - inline bool connected() const - { - std::lock_guard l(_lock); - for(typename std::unordered_map::const_iterator i(_connections.begin());i!=_connections.end();++i) { - if (i->gotHello) - return true; - } - return false; - } - - /** - * Iterate through all members of this map, with optional deletion - * - * The function is executed against all key/value pairs and returns a signed integer. - * A negative return value causes the entry to be deleted, while a positive return - * value means the key's value (which is passed into the function as a reference) has - * been modified and should be replicated. A return value of zero means no change. - * - * Other methods should not be called since doing so can result in a deadlock. - * - * @param func Function to execute against all members of map, returns integer (see description) - */ - template - inline void each(F func) - { - std::vector sentToNodes; - bool whack = false; - std::lock_guard l(_lock); - for(typename M::iterator i(_m.begin());i!=_m.end();++i) { - if (!i->second.deletedAt) { - try { - const int result = func(i->first,i->second.v); - if (result < 0) { - i->second.rev = ++_rev; - i->second.deletedAt = _rev; - i->second.v.clear(); - } else if (result > 0) { - i->second.rev = ++_rev; - i->second.deletedAt = 0; - // v will have been modified in place - } - if (result != 0) { - sentToNodes.clear(); - for(typename std::unordered_map::iterator c2(_connections.begin());c2!=_connections.end();++c2) { - if ((c2->second.gotHello)&&(std::find(sentToNodes.begin(),sentToNodes.end(),c2->second.node) == sentToNodes.end())) { - sendUpdate(c2->second,i->first,i->second); - sentToNodes.push_back(c2->second.node); - } - } - whack = true; - } - } catch ( ... ) {} - } - } - if (whack) - _phy.whack(); - } - -private: - inline vsdm &operator=(const vsdm &v) { return *this; } - - static void _threadMain(void *p) { reinterpret_cast(p)->threadMain(); } - inline void threadMain() - { - std::vector haveNodes; - time_t lastcheck = 0; - time_t lastclean = 0; - while (_run) { - _phy.poll(1000); - - time_t now = time(0); - - // Check connections with other nodes and try to establish them - // if they're not present. - if ((now - lastcheck) >= 2) { - lastcheck = now; - haveNodes.clear(); - - std::lock_guard l(_lock); - - for(typename std::unordered_map::const_iterator c(_connections.begin());c!=_connections.end();++c) { - if (std::find(haveNodes.begin(),haveNodes.end(),c->second.node) == haveNodes.end()) - haveNodes.push_back(c->second.node); - } - - for(std::unordered_map::iterator p(_peers.begin());p!=_peers.end();++p) { - if (std::find(haveNodes.begin(),haveNodes.end(),p->first) == haveNodes.end()) { - bool connected = false; - ztVsdmInternal::PhySocket *ns = _phy.tcpConnect((const struct sockaddr *)&(p->second),connected,(void *)0,true); - if (ns) { - _connection &c = _connections[ns]; - c.gotHello = false; - c.node = p->first; - c.sock = ns; - } - } - } - } - - // Forget deleted entries if they've had ample time to propagate - if ((now - lastclean) >= 120) { - lastclean = now; - uint64_t delHorizon = _m.size() * (2 + _peers.size()); - if (_rev > delHorizon) { - delHorizon -= _rev; - std::lock_guard l(_lock); - for(typename M::iterator i(_m.begin());i!=_m.end();) { - if ((i->second.deletedAt > 0)&&(i->second.deletedAt < delHorizon)) - _m.erase(i++); - else ++i; - } - } - } - } - } - - inline void sendUpdate(_connection &c,const std::string &k,const vsdm_entry &e) - { - // assumes lock is locked - const uint32_t ks = (uint32_t)S::objectSize(k); - uint32_t vs = 0; - uint32_t hdr[4]; - hdr[0] = htonl((uint32_t)((e.rev >> 32) & 0xffffffff)); - hdr[1] = htonl((uint32_t)(e.rev & 0xffffffff)); - hdr[2] = htonl(ks); - if (e.deletedAt) { - hdr[3] = 0xffffffff; - } else { - vs = (uint32_t)S::objectSize(e.v); - hdr[3] = htonl(vs); - } - - const uint32_t s = htonl((uint32_t)(16 + C::overhead() + ks + vs)); - c.outbuf.append((const char *)&s,4); - - const unsigned long start = (unsigned long)c.outbuf.length(); - c.outbuf.append((const char *)hdr,16); - c.outbuf.append(S::objectData(k),ks); - if (!e.deletedAt) - c.outbuf.append(S::objectData(e.v),vs); - c.outbuf.append(C::overhead(),(char)0); - const unsigned long end = (unsigned long)c.outbuf.length(); - - _cryptor.encrypt(reinterpret_cast(const_cast(c.outbuf.data()) + start),end - start); - - _phy.setNotifyWritable(c.sock,true); - } - - inline void sendUpdateToAll(ztVsdmInternal::PhySocket *receivedOnSock,const uint64_t receivedFromNode,const std::string &k,const vsdm_entry &e) - { - // assumes lock is locked - std::vector sentToNodes; - for(typename std::unordered_map::iterator c2(_connections.begin());c2!=_connections.end();++c2) { - if ((c2->first != receivedOnSock)&&(c2->second.gotHello)&&(c2->second.node != receivedFromNode)&&(std::find(sentToNodes.begin(),sentToNodes.end(),c2->second.node) == sentToNodes.end())) { - sendUpdate(c2->second,k,e); - sentToNodes.push_back(c2->second.node); -#ifdef VSDM_DEBUG - fprintf(stderr,">> %lu: %s=%s\n",(unsigned long)c2->second.node,k.c_str(),(e.deletedAt) ? "" : e.v.c_str()); fflush(stderr); -#endif - } - } - _phy.whack(); - } - - inline void sendHello(ztVsdmInternal::PhySocket *sock) - { - uint64_t hdr[3]; - if (htonl(1) == 1) { - hdr[0] = _node; - hdr[1] = _id; - hdr[2] = _rev; - } else { - hdr[0] = ztVsdmInternal::_swap64(_node); - hdr[1] = ztVsdmInternal::_swap64(_id); - hdr[2] = ztVsdmInternal::_swap64(_rev); - } - uint8_t tmp[24 + C::overhead()]; - memcpy(tmp,hdr,24); - _cryptor.encrypt(reinterpret_cast(tmp),sizeof(tmp)); - _phy.streamSend(sock,reinterpret_cast(tmp),sizeof(tmp)); - } - - inline void phyOnTcpConnect(ztVsdmInternal::PhySocket *sock,void **uptr,bool success) - { - std::lock_guard l(_lock); - if (success) { - _connection &c = _connections[sock]; - c.gotHello = false; - c.sock = sock; - *uptr = (void *)&c; - sendHello(sock); - } else { - _connections.erase(sock); - } - } - - inline void phyOnTcpAccept(ztVsdmInternal::PhySocket *sockL,ztVsdmInternal::PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) - { - std::lock_guard l(_lock); - - if (_restrictInbound) { - bool ok = false; - for(typename std::unordered_map::const_iterator i(_peers.begin());i!=_peers.end();++i) { - if (from->sa_family == i->second.ss_family) { - if ( (from->sa_family == AF_INET) && (reinterpret_cast(from)->sin_addr.s_addr == reinterpret_cast(&(i->second))->sin_addr.s_addr) ) { - ok = true; - break; - } else if ( (from->sa_family == AF_INET6) && (memcmp(reinterpret_cast(from)->sin6_addr.s6_addr,reinterpret_cast(&(i->second))->sin6_addr.s6_addr,16) == 0) ) { - ok = true; - break; - } - } - } - if (!ok) { -#ifdef VSDM_DEBUG - fprintf(stderr," * dropped inbound connection: peer not from a known IP address\n"); fflush(stderr); -#endif - _phy.close(sockN,false); - return; - } - } - - _connection &c = _connections[sockN]; - c.gotHello = false; - c.node = _node; // impossible value for a remote - c.sock = sockN; - *uptrN = (void *)&c; - sendHello(sockN); - } - - inline void phyOnTcpClose(ztVsdmInternal::PhySocket *sock,void **uptr) - { - std::lock_guard l(_lock); - _connections.erase(sock); - } - - inline void phyOnTcpData(ztVsdmInternal::PhySocket *sock,void **uptr,void *data,unsigned long len) - { - _connection *const c = (_connection *)*uptr; - if (!c) return; - - std::unique_lock l(_lock); - c->inbuf.append(reinterpret_cast(data),len); - for(;;) { - if (c->gotHello) { - - if (c->inbuf.length() >= 20) { // got message size and header - uint32_t _totalLen; - memcpy(&_totalLen,c->inbuf.data(),4); - const unsigned long totalLen = ntohl(_totalLen); - if ((totalLen > L)||(totalLen < 16)) { // message too small or too large - _connections.erase(sock); - _phy.close(sock,false); - return; - } - - if (c->inbuf.length() >= (4 + totalLen)) { // got full message - - if (!_cryptor.decrypt(reinterpret_cast(const_cast(c->inbuf.data()) + 4),totalLen)) { - _connections.erase(sock); - _phy.close(sock,false); - return; - } - - uint32_t hdr[4]; - memcpy(hdr,c->inbuf.data() + 4,16); - - const uint64_t objectRev = ((uint64_t)ntohl(hdr[0]) << 32) | (uint64_t)ntohl(hdr[1]); - const unsigned long keyLen = (unsigned long)ntohl(hdr[2]); - unsigned long valueLen = (unsigned long)ntohl(hdr[3]); - - if (objectRev > _rev) - _rev = objectRev; - - uint64_t deletedAt = 0; - if (valueLen == 0xffffffff) { - valueLen = 0; - deletedAt = _rev; - } - if ((keyLen + valueLen + 16 + C::overhead()) > totalLen) { // key and/or value length invalid - _connections.erase(sock); - _phy.close(sock,false); - return; - } - - K k; - if (!S::objectDeserialize(c->inbuf.data() + 16 + 4,keyLen,k)) { - _connections.erase(sock); - _phy.close(sock,false); - return; - } - - vsdm_entry &e = _m[k]; - if (e.rev < objectRev) { - const bool added = (e.rev == 0); - e.rev = objectRev; - e.deletedAt = deletedAt; - if (e.deletedAt) { - e.v = V(); - } else { - if (!S::objectDeserialize(c->inbuf.data() + 16 + 4 + keyLen,valueLen,e.v)) { - _connections.erase(sock); - _phy.close(sock,false); - return; - } - } -#ifdef VSDM_DEBUG - fprintf(stderr,"<< %lu: %s=%s\n",(unsigned long)c->node,k.c_str(),(deletedAt) ? "" : e.v.c_str()); fflush(stderr); -#endif - sendUpdateToAll(sock,c->node,k,e); - - l.unlock(); - try { - if (added) { - _watcher.add(c->node,k,e.v,objectRev); - } else if (deletedAt) { - _watcher.del(c->node,k); - } else { - _watcher.update(c->node,k,e.v,objectRev); - } - } catch ( ... ) {} - l.lock(); - } - - c->inbuf.erase(c->inbuf.begin(),c->inbuf.begin() + totalLen + 4); - - // continue and process more messages in queue, if any - } else { // still waiting on full message - break; - } - } else { // still waiting on message size and header - break; - } - - } else if (c->inbuf.length() >= (24 + C::overhead())) { // got hello header - - if (!_cryptor.decrypt(reinterpret_cast(const_cast(c->inbuf.data())),24 + C::overhead())) { - _connections.erase(sock); - _phy.close(sock,false); - return; - } - - uint64_t hdr[3]; - memcpy(hdr,c->inbuf.data(),24); - c->inbuf.erase(c->inbuf.begin(),c->inbuf.begin() + 24 + C::overhead()); - - if (htonl(1) != 1) { - hdr[0] = ztVsdmInternal::_swap64(hdr[0]); - hdr[1] = ztVsdmInternal::_swap64(hdr[1]); - hdr[2] = ztVsdmInternal::_swap64(hdr[2]); - } - - if ((hdr[0] == _node)||(hdr[1] != _id)) { // don't connect to self, and don't connect to other map IDs - _connections.erase(sock); - _phy.close(sock,false); - break; - } else { - c->gotHello = true; - c->node = hdr[0]; - - if (hdr[2] > _rev) - _rev = hdr[2]; - - for(typename M::const_iterator i(_m.begin());i!=_m.end();++i) { - if (i->second.rev >= hdr[2]) { - sendUpdate(*c,i->first,i->second); -#ifdef VSDM_DEBUG - fprintf(stderr,">> %lu: %s=%s (new link)\n",(unsigned long)c->node,i->first.c_str(),i->second.v.c_str()); fflush(stderr); -#endif - } - } - _phy.whack(); - } - - // continue and process more messages in queue, if any - } else { // still waiting on hello header - break; - } - } - } - - inline void phyOnTcpWritable(ztVsdmInternal::PhySocket *sock,void **uptr) - { - std::lock_guard l(_lock); - _connection *c = (_connection *)*uptr; - if (c) { - if (c->outbuf.length() > 0) { - long n = _phy.streamSend(sock,c->outbuf.data(),c->outbuf.length()); - if (n <= 0) { - _connections.erase(sock); - _phy.close(sock,false); - return; - } else if (n == (long)c->outbuf.length()) { - c->outbuf.clear(); - } else { - c->outbuf.erase(c->outbuf.begin(),c->outbuf.begin() + n); - } - } - if (c->outbuf.length() == 0) { - _phy.setNotifyWritable(c->sock,false); - } - } - } - - inline void phyOnDatagram(ztVsdmInternal::PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) {} - inline void phyOnFileDescriptorActivity(ztVsdmInternal::PhySocket *sock,void **uptr,bool readable,bool writable) {} - inline void phyOnUnixAccept(ztVsdmInternal::PhySocket *sockL,ztVsdmInternal::PhySocket *sockN,void **uptrL,void **uptrN) {} - inline void phyOnUnixClose(ztVsdmInternal::PhySocket *sock,void **uptr) {} - inline void phyOnUnixData(ztVsdmInternal::PhySocket *sock,void **uptr,void *data,unsigned long len) {} - inline void phyOnUnixWritable(ztVsdmInternal::PhySocket *sock,void **uptr) {} - - const uint64_t _node; - const uint64_t _id; - uint64_t _rev; - std::unordered_map _peers; - std::unordered_map _connections; - M _m; - mutable std::mutex _lock; - ztVsdmInternal::Phy _phy; - C _cryptor; - W _watcher; - volatile bool _run; - bool _restrictInbound; - std::thread _t; -}; - -#endif diff --git a/include/ZeroTierOne.h b/include/ZeroTierOne.h index 4709b116..74fa4301 100644 --- a/include/ZeroTierOne.h +++ b/include/ZeroTierOne.h @@ -1089,6 +1089,62 @@ typedef struct unsigned long peerCount; } ZT_PeerList; +/** + * Types of stored objects that the core may wish to save or load + */ +enum ZT_StoredObjectType +{ + /** + * Node status information (reserved, not currently used) + */ + ZT_STORED_OBJECT_STATUS = 0, + + /** + * String serialized public identity + */ + ZT_STORED_OBJECT_IDENTITY_PUBLIC = 1, + + /** + * String serialized secret identity + */ + ZT_STORED_OBJECT_IDENTITY_SECRET = 1, + + /** + * Binary serialized peer state + */ + ZT_STORED_OBJECT_PEER = 3, + + /** + * Identity (other node, not this one) + */ + ZT_STORED_OBJECT_IDENTITY = 4, + + /** + * Network configuration object + */ + ZT_STORED_OBJECT_NETWORK_CONFIG = 5, + + /** + * Planet definition (object ID will be zero and should be ignored since there's only one) + */ + ZT_STORED_OBJECT_PLANET = 6, + + /** + * Moon definition + */ + ZT_STORED_OBJECT_MOON = 7, + + /** + * Multicast membership + */ + ZT_STORED_OBJECT_MULTICAST_MEMBERSHIP = 8, + + /** + * IDs above this are never used by the core and are available for implementation use + */ + ZT_STORED_OBJECT__MAX_TYPE_ID = 255 +}; + /** * An instance of a ZeroTier One node (opaque) */ diff --git a/node/Buffer.hpp b/node/Buffer.hpp index 8e6b78fd..fea32767 100644 --- a/node/Buffer.hpp +++ b/node/Buffer.hpp @@ -93,7 +93,6 @@ public: } Buffer(unsigned int l) - throw(std::out_of_range) { if (l > C) throw std::out_of_range("Buffer: construct with size larger than capacity"); @@ -102,51 +101,42 @@ public: template Buffer(const Buffer &b) - throw(std::out_of_range) { *this = b; } Buffer(const void *b,unsigned int l) - throw(std::out_of_range) { copyFrom(b,l); } Buffer(const std::string &s) - throw(std::out_of_range) { copyFrom(s.data(),s.length()); } template inline Buffer &operator=(const Buffer &b) - throw(std::out_of_range) { if (unlikely(b._l > C)) throw std::out_of_range("Buffer: assignment from buffer larger than capacity"); - memcpy(_b,b._b,_l = b._l); - return *this; - } - - inline Buffer &operator=(const std::string &s) - throw(std::out_of_range) - { - copyFrom(s.data(),s.length()); + if (C2 == C) { + memcpy(this,&b,sizeof(Buffer)); + } else { + memcpy(_b,b._b,_l = b._l); + } return *this; } inline void copyFrom(const void *b,unsigned int l) - throw(std::out_of_range) { if (unlikely(l > C)) throw std::out_of_range("Buffer: set from C array larger than capacity"); - _l = l; memcpy(_b,b,l); + _l = l; } unsigned char operator[](const unsigned int i) const - throw(std::out_of_range) { if (unlikely(i >= _l)) throw std::out_of_range("Buffer: [] beyond end of data"); @@ -154,7 +144,6 @@ public: } unsigned char &operator[](const unsigned int i) - throw(std::out_of_range) { if (unlikely(i >= _l)) throw std::out_of_range("Buffer: [] beyond end of data"); @@ -175,14 +164,12 @@ public: * @throws std::out_of_range Field extends beyond data size */ unsigned char *field(unsigned int i,unsigned int l) - throw(std::out_of_range) { if (unlikely((i + l) > _l)) throw std::out_of_range("Buffer: field() beyond end of data"); return (unsigned char *)(_b + i); } const unsigned char *field(unsigned int i,unsigned int l) const - throw(std::out_of_range) { if (unlikely((i + l) > _l)) throw std::out_of_range("Buffer: field() beyond end of data"); @@ -198,7 +185,6 @@ public: */ template inline void setAt(unsigned int i,const T v) - throw(std::out_of_range) { if (unlikely((i + sizeof(T)) > _l)) throw std::out_of_range("Buffer: setAt() beyond end of data"); @@ -221,7 +207,6 @@ public: */ template inline T at(unsigned int i) const - throw(std::out_of_range) { if (unlikely((i + sizeof(T)) > _l)) throw std::out_of_range("Buffer: at() beyond end of data"); @@ -248,7 +233,6 @@ public: */ template inline void append(const T v) - throw(std::out_of_range) { if (unlikely((_l + sizeof(T)) > C)) throw std::out_of_range("Buffer: append beyond capacity"); @@ -271,7 +255,6 @@ public: * @throws std::out_of_range Attempt to append beyond capacity */ inline void append(unsigned char c,unsigned int n) - throw(std::out_of_range) { if (unlikely((_l + n) > C)) throw std::out_of_range("Buffer: append beyond capacity"); @@ -287,7 +270,6 @@ public: * @throws std::out_of_range Attempt to append beyond capacity */ inline void append(const void *b,unsigned int l) - throw(std::out_of_range) { if (unlikely((_l + l) > C)) throw std::out_of_range("Buffer: append beyond capacity"); @@ -302,7 +284,6 @@ public: * @throws std::out_of_range Attempt to append beyond capacity */ inline void append(const std::string &s) - throw(std::out_of_range) { append(s.data(),(unsigned int)s.length()); } @@ -314,7 +295,6 @@ public: * @throws std::out_of_range Attempt to append beyond capacity */ inline void appendCString(const char *s) - throw(std::out_of_range) { for(;;) { if (unlikely(_l >= C)) @@ -333,7 +313,6 @@ public: */ template inline void append(const Buffer &b) - throw(std::out_of_range) { append(b._b,b._l); } @@ -349,7 +328,6 @@ public: * @return Pointer to beginning of appended field of length 'l' */ inline char *appendField(unsigned int l) - throw(std::out_of_range) { if (unlikely((_l + l) > C)) throw std::out_of_range("Buffer: append beyond capacity"); @@ -367,7 +345,6 @@ public: * @throws std::out_of_range Capacity exceeded */ inline void addSize(unsigned int i) - throw(std::out_of_range) { if (unlikely((i + _l) > C)) throw std::out_of_range("Buffer: setSize to larger than capacity"); @@ -383,7 +360,6 @@ public: * @throws std::out_of_range Size larger than capacity */ inline void setSize(const unsigned int i) - throw(std::out_of_range) { if (unlikely(i > C)) throw std::out_of_range("Buffer: setSize to larger than capacity"); @@ -397,7 +373,6 @@ public: * @throw std::out_of_range Position is beyond size of buffer */ inline void behead(const unsigned int at) - throw(std::out_of_range) { if (!at) return; @@ -414,7 +389,6 @@ public: * @throw std::out_of_range Position plus length is beyond size of buffer */ inline void erase(const unsigned int at,const unsigned int length) - throw(std::out_of_range) { const unsigned int endr = at + length; if (unlikely(endr > _l)) @@ -495,8 +469,8 @@ public: } private: - unsigned int _l; char ZT_VAR_MAY_ALIAS _b[C]; + unsigned int _l; }; } // namespace ZeroTier diff --git a/node/Constants.hpp b/node/Constants.hpp index 3974f0ec..fbbba76e 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -150,6 +150,12 @@ #endif #endif +#ifdef __WINDOWS__ +#define ZT_PACKED_STRUCT(D) __pragma(pack(push,1)) D __pragma(pack(pop)) +#else +#define ZT_PACKED_STRUCT(D) D __attribute__((packed)) +#endif + /** * Length of a ZeroTier address in bytes */ diff --git a/node/Hashtable.hpp b/node/Hashtable.hpp index c46ed68f..b702f608 100644 --- a/node/Hashtable.hpp +++ b/node/Hashtable.hpp @@ -374,12 +374,7 @@ private: } static inline unsigned long _hc(const uint64_t i) { - /* NOTE: this assumes that 'i' is evenly distributed, which is the case for - * packet IDs and network IDs -- the two use cases in ZT for uint64_t keys. - * These values are also greater than 0xffffffff so they'll map onto a full - * bucket count just fine no matter what happens. Normally you'd want to - * hash an integer key index in a hash table. */ - return (unsigned long)i; + return (unsigned long)(i ^ (i >> 32)); // good for network IDs and addresses } static inline unsigned long _hc(const uint32_t i) { -- cgit v1.2.3 From 7f4da08ff7af6f5d2a06001f70dae906a859c66e Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 1 Jun 2017 12:57:44 -0700 Subject: . --- attic/root-watcher/README.md | 8 + attic/root-watcher/config.json.example | 30 ++++ attic/root-watcher/package.json | 16 ++ attic/root-watcher/schema.sql | 20 +++ attic/root-watcher/zerotier-root-watcher.js | 235 ++++++++++++++++++++++++++++ root-watcher/README.md | 8 - root-watcher/config.json.example | 30 ---- root-watcher/package.json | 16 -- root-watcher/schema.sql | 20 --- root-watcher/zerotier-root-watcher.js | 235 ---------------------------- 10 files changed, 309 insertions(+), 309 deletions(-) create mode 100644 attic/root-watcher/README.md create mode 100644 attic/root-watcher/config.json.example create mode 100644 attic/root-watcher/package.json create mode 100644 attic/root-watcher/schema.sql create mode 100644 attic/root-watcher/zerotier-root-watcher.js delete mode 100644 root-watcher/README.md delete mode 100644 root-watcher/config.json.example delete mode 100644 root-watcher/package.json delete mode 100644 root-watcher/schema.sql delete mode 100644 root-watcher/zerotier-root-watcher.js (limited to 'attic') diff --git a/attic/root-watcher/README.md b/attic/root-watcher/README.md new file mode 100644 index 00000000..ded6a63f --- /dev/null +++ b/attic/root-watcher/README.md @@ -0,0 +1,8 @@ +Root Server Watcher +====== + +This is a small daemon written in NodeJS that watches a set of root servers and records peer status information into a Postgres database. + +To use type `npm install` to install modules. Then edit `config.json.example` and rename to `config.json`. For each of your roots you will need to configure a way for this script to reach it. You will also need to use `schema.sql` to initialize a Postgres database to contain your logs and set it up in `config.json` as well. + +This doesn't (yet) include any software for reading the log database and doing anything useful with the information inside, though given that it's a simple SQL database it should not be hard to compose queries to show interesting statistics. diff --git a/attic/root-watcher/config.json.example b/attic/root-watcher/config.json.example new file mode 100644 index 00000000..0ad1bbe1 --- /dev/null +++ b/attic/root-watcher/config.json.example @@ -0,0 +1,30 @@ +{ + "interval": 30000, + "dbSaveInterval": 60000, + "peerTimeout": 600000, + "db": { + "database": "ztr", + "user": "postgres", + "password": "s00p3rs3kr3t", + "host": "127.0.0.1", + "port": 5432, + "max": 16, + "idleTimeoutMillis": 30000 + }, + "roots": { + "my-root-01": { + "id": 1, + "ip": "10.0.0.1", + "port": 9993, + "authToken": "foobarbaz", + "peers": "/peer" + }, + "my-root-02": { + "id": 2, + "ip": "10.0.0.2", + "port": 9993, + "authToken": "lalafoo", + "peers": "/peer" + } + } +} diff --git a/attic/root-watcher/package.json b/attic/root-watcher/package.json new file mode 100644 index 00000000..d6e86d78 --- /dev/null +++ b/attic/root-watcher/package.json @@ -0,0 +1,16 @@ +{ + "name": "zerotier-root-watcher", + "version": "1.0.0", + "description": "Simple background service to watch a cluster of roots and record peer info into a database", + "main": "zerotier-root-watcher.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "ZeroTier, Inc. ", + "license": "GPL-3.0", + "dependencies": { + "async": "^2.3.0", + "pg": "^6.1.5", + "zlib": "^1.0.5" + } +} diff --git a/attic/root-watcher/schema.sql b/attic/root-watcher/schema.sql new file mode 100644 index 00000000..ade0fa3e --- /dev/null +++ b/attic/root-watcher/schema.sql @@ -0,0 +1,20 @@ +/* Schema for ZeroTier root watcher log database */ + +CREATE TABLE "Peer" +( + "ztAddress" BIGINT NOT NULL, + "timestamp" BIGINT NOT NULL, + "versionMajor" INTEGER NOT NULL, + "versionMinor" INTEGER NOT NULL, + "versionRev" INTEGER NOT NULL, + "rootId" INTEGER NOT NULL, + "phyPort" INTEGER NOT NULL, + "phyLinkQuality" REAL NOT NULL, + "phyLastReceive" BIGINT NOT NULL, + "phyAddress" INET NOT NULL +); + +CREATE INDEX "Peer_ztAddress" ON "Peer" ("ztAddress"); +CREATE INDEX "Peer_timestamp" ON "Peer" ("timestamp"); +CREATE INDEX "Peer_rootId" ON "Peer" ("rootId"); +CREATE INDEX "Peer_phyAddress" ON "Peer" ("phyAddress"); diff --git a/attic/root-watcher/zerotier-root-watcher.js b/attic/root-watcher/zerotier-root-watcher.js new file mode 100644 index 00000000..d4607fc2 --- /dev/null +++ b/attic/root-watcher/zerotier-root-watcher.js @@ -0,0 +1,235 @@ +'use strict'; + +const pg = require('pg'); +const zlib = require('zlib'); +const http = require('http'); +const fs = require('fs'); +const async = require('async'); + +const config = JSON.parse(fs.readFileSync('./config.json')); +const roots = config.roots||{}; + +const db = new pg.Pool(config.db); + +process.on('uncaughtException',function(err) { + console.error('ERROR: uncaught exception: '+err); + if (err.stack) + console.error(err.stack); +}); + +function httpRequest(host,port,authToken,method,path,args,callback) +{ + var responseBody = []; + var postData = (args) ? JSON.stringify(args) : null; + + var req = http.request({ + host: host, + port: port, + path: path, + method: method, + headers: { + 'X-ZT1-Auth': (authToken||''), + 'Content-Length': (postData) ? postData.length : 0 + } + },function(res) { + res.on('data',function(chunk) { + if ((chunk)&&(chunk.length > 0)) + responseBody.push(chunk); + }); + res.on('timeout',function() { + try { + if (typeof callback === 'function') { + var cb = callback; + callback = null; + cb(new Error('connection timed out'),null); + } + req.abort(); + } catch (e) {} + }); + res.on('error',function(e) { + try { + if (typeof callback === 'function') { + var cb = callback; + callback = null; + cb(new Error('connection timed out'),null); + } + req.abort(); + } catch (e) {} + }); + res.on('end',function() { + if (typeof callback === 'function') { + var cb = callback; + callback = null; + if (responseBody.length === 0) { + return cb(null,{}); + } else { + responseBody = Buffer.concat(responseBody); + + if (responseBody.length < 2) { + return cb(null,{}); + } + + if ((responseBody.readUInt8(0,true) === 0x1f)&&(responseBody.readUInt8(1,true) === 0x8b)) { + try { + responseBody = zlib.gunzipSync(responseBody); + } catch (e) { + return cb(e,null); + } + } + + try { + return cb(null,JSON.parse(responseBody)); + } catch (e) { + return cb(e,null); + } + } + } + }); + }).on('error',function(e) { + try { + if (typeof callback === 'function') { + var cb = callback; + callback = null; + cb(e,null); + } + req.abort(); + } catch (e) {} + }).on('timeout',function() { + try { + if (typeof callback === 'function') { + var cb = callback; + callback = null; + cb(new Error('connection timed out'),null); + } + req.abort(); + } catch (e) {} + }); + + req.setTimeout(30000); + req.setNoDelay(true); + + if (postData !== null) + req.end(postData); + else req.end(); +}; + +var peerStatus = {}; + +function saveToDb() +{ + db.connect(function(err,client,clientDone) { + if (err) { + console.log('WARNING: database error writing peers: '+err.toString()); + clientDone(); + return setTimeout(saveToDb,config.dbSaveInterval||60000); + } + client.query('BEGIN',function(err) { + if (err) { + console.log('WARNING: database error writing peers: '+err.toString()); + clientDone(); + return setTimeout(saveToDb,config.dbSaveInterval||60000); + } + let timeout = Date.now() - (config.peerTimeout||600000); + let wtotal = 0; + async.eachSeries(Object.keys(peerStatus),function(address,nextAddress) { + let s = peerStatus[address]; + if (s[1] <= timeout) { + delete peerStatus[address]; + return process.nextTick(nextAddress); + } else { + ++wtotal; + client.query('INSERT INTO "Peer" ("ztAddress","timestamp","versionMajor","versionMinor","versionRev","rootId","phyPort","phyLinkQuality","phyLastReceive","phyAddress") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)',s,nextAddress); + } + },function(err) { + if (err) + console.log('WARNING database error writing peers: '+err.toString()); + console.log(Date.now().toString()+' '+wtotal); + client.query('COMMIT',function(err,result) { + clientDone(); + return setTimeout(saveToDb,config.dbSaveInterval||60000); + }); + }); + }); + }); +}; + +function doRootUpdate(name,id,ip,port,peersPath,authToken,interval) +{ + httpRequest(ip,port,authToken,"GET",peersPath,null,function(err,res) { + if (err) { + console.log('WARNING: cannot reach '+name+peersPath+' (will try again in 1s): '+err.toString()); + return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },1000); + } + if (!Array.isArray(res)) { + console.log('WARNING: cannot reach '+name+peersPath+' (will try again in 1s): response is not an array of peers'); + return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },1000); + } + + //console.log(name+': '+res.length+' peer entries.'); + let now = Date.now(); + let count = 0; + for(let pi=0;pi 0)) { + let bestPath = null; + for(let i=0;i 0)&&((!bestPath)||(bestPath.lastReceive < lr))) + bestPath = paths[i]; + } + } + + if (bestPath) { + let a = bestPath.address; + if (typeof a === 'string') { + let a2 = a.split('/'); + if (a2.length === 2) { + let vmaj = peer.versionMajor; + if ((typeof vmaj === 'undefined')||(vmaj === null)) vmaj = -1; + let vmin = peer.versionMinor; + if ((typeof vmin === 'undefined')||(vmin === null)) vmin = -1; + let vrev = peer.versionRev; + if ((typeof vrev === 'undefined')||(vrev === null)) vrev = -1; + let lr = parseInt(bestPath.lastReceive)||0; + + let s = peerStatus[address]; + if ((!s)||(s[8] < lr)) { + peerStatus[address] = [ + ztAddress, + now, + vmaj, + vmin, + vrev, + id, + parseInt(a2[1])||0, + parseFloat(bestPath.linkQuality)||1.0, + lr, + a2[0] + ]; + } + ++count; + } + } + } + } + } + + console.log(name+': '+count+' peers with active direct paths.'); + return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },interval); + }); +}; + +for(var r in roots) { + var rr = roots[r]; + if (rr.peers) + doRootUpdate(r,rr.id,rr.ip,rr.port,rr.peers,rr.authToken||null,config.interval||60000); +} + +return setTimeout(saveToDb,config.dbSaveInterval||60000); diff --git a/root-watcher/README.md b/root-watcher/README.md deleted file mode 100644 index ded6a63f..00000000 --- a/root-watcher/README.md +++ /dev/null @@ -1,8 +0,0 @@ -Root Server Watcher -====== - -This is a small daemon written in NodeJS that watches a set of root servers and records peer status information into a Postgres database. - -To use type `npm install` to install modules. Then edit `config.json.example` and rename to `config.json`. For each of your roots you will need to configure a way for this script to reach it. You will also need to use `schema.sql` to initialize a Postgres database to contain your logs and set it up in `config.json` as well. - -This doesn't (yet) include any software for reading the log database and doing anything useful with the information inside, though given that it's a simple SQL database it should not be hard to compose queries to show interesting statistics. diff --git a/root-watcher/config.json.example b/root-watcher/config.json.example deleted file mode 100644 index 0ad1bbe1..00000000 --- a/root-watcher/config.json.example +++ /dev/null @@ -1,30 +0,0 @@ -{ - "interval": 30000, - "dbSaveInterval": 60000, - "peerTimeout": 600000, - "db": { - "database": "ztr", - "user": "postgres", - "password": "s00p3rs3kr3t", - "host": "127.0.0.1", - "port": 5432, - "max": 16, - "idleTimeoutMillis": 30000 - }, - "roots": { - "my-root-01": { - "id": 1, - "ip": "10.0.0.1", - "port": 9993, - "authToken": "foobarbaz", - "peers": "/peer" - }, - "my-root-02": { - "id": 2, - "ip": "10.0.0.2", - "port": 9993, - "authToken": "lalafoo", - "peers": "/peer" - } - } -} diff --git a/root-watcher/package.json b/root-watcher/package.json deleted file mode 100644 index d6e86d78..00000000 --- a/root-watcher/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "zerotier-root-watcher", - "version": "1.0.0", - "description": "Simple background service to watch a cluster of roots and record peer info into a database", - "main": "zerotier-root-watcher.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "ZeroTier, Inc. ", - "license": "GPL-3.0", - "dependencies": { - "async": "^2.3.0", - "pg": "^6.1.5", - "zlib": "^1.0.5" - } -} diff --git a/root-watcher/schema.sql b/root-watcher/schema.sql deleted file mode 100644 index ade0fa3e..00000000 --- a/root-watcher/schema.sql +++ /dev/null @@ -1,20 +0,0 @@ -/* Schema for ZeroTier root watcher log database */ - -CREATE TABLE "Peer" -( - "ztAddress" BIGINT NOT NULL, - "timestamp" BIGINT NOT NULL, - "versionMajor" INTEGER NOT NULL, - "versionMinor" INTEGER NOT NULL, - "versionRev" INTEGER NOT NULL, - "rootId" INTEGER NOT NULL, - "phyPort" INTEGER NOT NULL, - "phyLinkQuality" REAL NOT NULL, - "phyLastReceive" BIGINT NOT NULL, - "phyAddress" INET NOT NULL -); - -CREATE INDEX "Peer_ztAddress" ON "Peer" ("ztAddress"); -CREATE INDEX "Peer_timestamp" ON "Peer" ("timestamp"); -CREATE INDEX "Peer_rootId" ON "Peer" ("rootId"); -CREATE INDEX "Peer_phyAddress" ON "Peer" ("phyAddress"); diff --git a/root-watcher/zerotier-root-watcher.js b/root-watcher/zerotier-root-watcher.js deleted file mode 100644 index d4607fc2..00000000 --- a/root-watcher/zerotier-root-watcher.js +++ /dev/null @@ -1,235 +0,0 @@ -'use strict'; - -const pg = require('pg'); -const zlib = require('zlib'); -const http = require('http'); -const fs = require('fs'); -const async = require('async'); - -const config = JSON.parse(fs.readFileSync('./config.json')); -const roots = config.roots||{}; - -const db = new pg.Pool(config.db); - -process.on('uncaughtException',function(err) { - console.error('ERROR: uncaught exception: '+err); - if (err.stack) - console.error(err.stack); -}); - -function httpRequest(host,port,authToken,method,path,args,callback) -{ - var responseBody = []; - var postData = (args) ? JSON.stringify(args) : null; - - var req = http.request({ - host: host, - port: port, - path: path, - method: method, - headers: { - 'X-ZT1-Auth': (authToken||''), - 'Content-Length': (postData) ? postData.length : 0 - } - },function(res) { - res.on('data',function(chunk) { - if ((chunk)&&(chunk.length > 0)) - responseBody.push(chunk); - }); - res.on('timeout',function() { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(new Error('connection timed out'),null); - } - req.abort(); - } catch (e) {} - }); - res.on('error',function(e) { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(new Error('connection timed out'),null); - } - req.abort(); - } catch (e) {} - }); - res.on('end',function() { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - if (responseBody.length === 0) { - return cb(null,{}); - } else { - responseBody = Buffer.concat(responseBody); - - if (responseBody.length < 2) { - return cb(null,{}); - } - - if ((responseBody.readUInt8(0,true) === 0x1f)&&(responseBody.readUInt8(1,true) === 0x8b)) { - try { - responseBody = zlib.gunzipSync(responseBody); - } catch (e) { - return cb(e,null); - } - } - - try { - return cb(null,JSON.parse(responseBody)); - } catch (e) { - return cb(e,null); - } - } - } - }); - }).on('error',function(e) { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(e,null); - } - req.abort(); - } catch (e) {} - }).on('timeout',function() { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(new Error('connection timed out'),null); - } - req.abort(); - } catch (e) {} - }); - - req.setTimeout(30000); - req.setNoDelay(true); - - if (postData !== null) - req.end(postData); - else req.end(); -}; - -var peerStatus = {}; - -function saveToDb() -{ - db.connect(function(err,client,clientDone) { - if (err) { - console.log('WARNING: database error writing peers: '+err.toString()); - clientDone(); - return setTimeout(saveToDb,config.dbSaveInterval||60000); - } - client.query('BEGIN',function(err) { - if (err) { - console.log('WARNING: database error writing peers: '+err.toString()); - clientDone(); - return setTimeout(saveToDb,config.dbSaveInterval||60000); - } - let timeout = Date.now() - (config.peerTimeout||600000); - let wtotal = 0; - async.eachSeries(Object.keys(peerStatus),function(address,nextAddress) { - let s = peerStatus[address]; - if (s[1] <= timeout) { - delete peerStatus[address]; - return process.nextTick(nextAddress); - } else { - ++wtotal; - client.query('INSERT INTO "Peer" ("ztAddress","timestamp","versionMajor","versionMinor","versionRev","rootId","phyPort","phyLinkQuality","phyLastReceive","phyAddress") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)',s,nextAddress); - } - },function(err) { - if (err) - console.log('WARNING database error writing peers: '+err.toString()); - console.log(Date.now().toString()+' '+wtotal); - client.query('COMMIT',function(err,result) { - clientDone(); - return setTimeout(saveToDb,config.dbSaveInterval||60000); - }); - }); - }); - }); -}; - -function doRootUpdate(name,id,ip,port,peersPath,authToken,interval) -{ - httpRequest(ip,port,authToken,"GET",peersPath,null,function(err,res) { - if (err) { - console.log('WARNING: cannot reach '+name+peersPath+' (will try again in 1s): '+err.toString()); - return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },1000); - } - if (!Array.isArray(res)) { - console.log('WARNING: cannot reach '+name+peersPath+' (will try again in 1s): response is not an array of peers'); - return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },1000); - } - - //console.log(name+': '+res.length+' peer entries.'); - let now = Date.now(); - let count = 0; - for(let pi=0;pi 0)) { - let bestPath = null; - for(let i=0;i 0)&&((!bestPath)||(bestPath.lastReceive < lr))) - bestPath = paths[i]; - } - } - - if (bestPath) { - let a = bestPath.address; - if (typeof a === 'string') { - let a2 = a.split('/'); - if (a2.length === 2) { - let vmaj = peer.versionMajor; - if ((typeof vmaj === 'undefined')||(vmaj === null)) vmaj = -1; - let vmin = peer.versionMinor; - if ((typeof vmin === 'undefined')||(vmin === null)) vmin = -1; - let vrev = peer.versionRev; - if ((typeof vrev === 'undefined')||(vrev === null)) vrev = -1; - let lr = parseInt(bestPath.lastReceive)||0; - - let s = peerStatus[address]; - if ((!s)||(s[8] < lr)) { - peerStatus[address] = [ - ztAddress, - now, - vmaj, - vmin, - vrev, - id, - parseInt(a2[1])||0, - parseFloat(bestPath.linkQuality)||1.0, - lr, - a2[0] - ]; - } - ++count; - } - } - } - } - } - - console.log(name+': '+count+' peers with active direct paths.'); - return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },interval); - }); -}; - -for(var r in roots) { - var rr = roots[r]; - if (rr.peers) - doRootUpdate(r,rr.id,rr.ip,rr.port,rr.peers,rr.authToken||null,config.interval||60000); -} - -return setTimeout(saveToDb,config.dbSaveInterval||60000); -- cgit v1.2.3 From e8d11eb5c548deecf75daa9542da6e27e17acff6 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 1 Jun 2017 17:21:04 -0700 Subject: . --- attic/tcp-proxy/Makefile | 7 + attic/tcp-proxy/README.md | 4 + attic/tcp-proxy/tcp-proxy.cpp | 317 ++++++++++++++++++++++++++++++++++++++++++ service/OneService.cpp | 32 +---- service/OneService.hpp | 45 ++---- tcp-proxy/Makefile | 7 - tcp-proxy/README.md | 4 - tcp-proxy/tcp-proxy.cpp | 317 ------------------------------------------ 8 files changed, 344 insertions(+), 389 deletions(-) create mode 100644 attic/tcp-proxy/Makefile create mode 100644 attic/tcp-proxy/README.md create mode 100644 attic/tcp-proxy/tcp-proxy.cpp delete mode 100644 tcp-proxy/Makefile delete mode 100644 tcp-proxy/README.md delete mode 100644 tcp-proxy/tcp-proxy.cpp (limited to 'attic') diff --git a/attic/tcp-proxy/Makefile b/attic/tcp-proxy/Makefile new file mode 100644 index 00000000..af4e71e3 --- /dev/null +++ b/attic/tcp-proxy/Makefile @@ -0,0 +1,7 @@ +CXX=$(shell which clang++ g++ c++ 2>/dev/null | head -n 1) + +all: + $(CXX) -O3 -fno-rtti -o tcp-proxy tcp-proxy.cpp + +clean: + rm -f *.o tcp-proxy *.dSYM diff --git a/attic/tcp-proxy/README.md b/attic/tcp-proxy/README.md new file mode 100644 index 00000000..6f347d64 --- /dev/null +++ b/attic/tcp-proxy/README.md @@ -0,0 +1,4 @@ +TCP Proxy Server +====== + +This is the TCP proxy server we run for TCP tunneling from peers behind fascist NATs. Regular users won't have much use for this. diff --git a/attic/tcp-proxy/tcp-proxy.cpp b/attic/tcp-proxy/tcp-proxy.cpp new file mode 100644 index 00000000..a7906aae --- /dev/null +++ b/attic/tcp-proxy/tcp-proxy.cpp @@ -0,0 +1,317 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + */ + +// HACK! Will eventually use epoll() or something in Phy<> instead of select(). +// Also be sure to change ulimit -n and fs.file-max in /etc/sysctl.conf on relays. +#if defined(__linux__) || defined(__LINUX__) || defined(__LINUX) || defined(LINUX) +#include +#include +#undef __FD_SETSIZE +#define __FD_SETSIZE 1048576 +#undef FD_SETSIZE +#define FD_SETSIZE 1048576 +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "../osdep/Phy.hpp" + +#define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300 +#define ZT_TCP_PROXY_TCP_PORT 443 + +using namespace ZeroTier; + +/* + * ZeroTier TCP Proxy Server + * + * This implements a simple packet encapsulation that is designed to look like + * a TLS connection. It's not a TLS connection, but it sends TLS format record + * headers. It could be extended in the future to implement a fake TLS + * handshake. + * + * At the moment, each packet is just made to look like TLS application data: + * <[1] TLS content type> - currently 0x17 for "application data" + * <[1] TLS major version> - currently 0x03 for TLS 1.2 + * <[1] TLS minor version> - currently 0x03 for TLS 1.2 + * <[2] payload length> - 16-bit length of payload in bytes + * <[...] payload> - Message payload + * + * TCP is inherently inefficient for encapsulating Ethernet, since TCP and TCP + * like protocols over TCP lead to double-ACKs. So this transport is only used + * to enable access when UDP or other datagram protocols are not available. + * + * Clients send a greeting, which is a four-byte message that contains: + * <[1] ZeroTier major version> + * <[1] minor version> + * <[2] revision> + * + * If a client has sent a greeting, it uses the new version of this protocol + * in which every encapsulated ZT packet is prepended by an IP address where + * it should be forwarded (or where it came from for replies). This causes + * this proxy to act as a remote UDP socket similar to a socks proxy, which + * will allow us to move this function off the rootservers and onto dedicated + * proxy nodes. + * + * Older ZT clients that do not send this message get their packets relayed + * to/from 127.0.0.1:9993, which will allow them to talk to and relay via + * the ZT node on the same machine as the proxy. We'll only support this for + * as long as such nodes appear to be in the wild. + */ + +struct TcpProxyService; +struct TcpProxyService +{ + Phy *phy; + int udpPortCounter; + struct Client + { + char tcpReadBuf[131072]; + char tcpWriteBuf[131072]; + unsigned long tcpWritePtr; + unsigned long tcpReadPtr; + PhySocket *tcp; + PhySocket *udp; + time_t lastActivity; + bool newVersion; + }; + std::map< PhySocket *,Client > clients; + + PhySocket *getUnusedUdp(void *uptr) + { + for(int i=0;i<65535;++i) { + ++udpPortCounter; + if (udpPortCounter > 0xfffe) + udpPortCounter = 1024; + struct sockaddr_in laddr; + memset(&laddr,0,sizeof(struct sockaddr_in)); + laddr.sin_family = AF_INET; + laddr.sin_port = htons((uint16_t)udpPortCounter); + PhySocket *udp = phy->udpBind(reinterpret_cast(&laddr),uptr); + if (udp) + return udp; + } + return (PhySocket *)0; + } + + void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) + { + if (!*uptr) + return; + if ((from->sa_family == AF_INET)&&(len >= 16)&&(len < 2048)) { + Client &c = *((Client *)*uptr); + c.lastActivity = time((time_t *)0); + + unsigned long mlen = len; + if (c.newVersion) + mlen += 7; // new clients get IP info + + if ((c.tcpWritePtr + 5 + mlen) <= sizeof(c.tcpWriteBuf)) { + if (!c.tcpWritePtr) + phy->setNotifyWritable(c.tcp,true); + + c.tcpWriteBuf[c.tcpWritePtr++] = 0x17; // look like TLS data + c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 + c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 + + c.tcpWriteBuf[c.tcpWritePtr++] = (char)((mlen >> 8) & 0xff); + c.tcpWriteBuf[c.tcpWritePtr++] = (char)(mlen & 0xff); + + if (c.newVersion) { + c.tcpWriteBuf[c.tcpWritePtr++] = (char)4; // IPv4 + *((uint32_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_addr.s_addr; + c.tcpWritePtr += 4; + *((uint16_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_port; + c.tcpWritePtr += 2; + } + + for(unsigned long i=0;i %.16llx\n",inet_ntoa(reinterpret_cast(from)->sin_addr),(int)ntohs(reinterpret_cast(from)->sin_port),(unsigned long long)&c); + } + } + + void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) + { + // unused, we don't initiate outbound connections + } + + void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) + { + Client &c = clients[sockN]; + PhySocket *udp = getUnusedUdp((void *)&c); + if (!udp) { + phy->close(sockN); + clients.erase(sockN); + //printf("** TCP rejected, no more UDP ports to assign\n"); + return; + } + c.tcpWritePtr = 0; + c.tcpReadPtr = 0; + c.tcp = sockN; + c.udp = udp; + c.lastActivity = time((time_t *)0); + c.newVersion = false; + *uptrN = (void *)&c; + //printf("<< TCP from %s -> %.16llx\n",inet_ntoa(reinterpret_cast(from)->sin_addr),(unsigned long long)&c); + } + + void phyOnTcpClose(PhySocket *sock,void **uptr) + { + if (!*uptr) + return; + Client &c = *((Client *)*uptr); + phy->close(c.udp); + clients.erase(sock); + //printf("** TCP %.16llx closed\n",(unsigned long long)*uptr); + } + + void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) + { + Client &c = *((Client *)*uptr); + c.lastActivity = time((time_t *)0); + + for(unsigned long i=0;i= sizeof(c.tcpReadBuf)) { + phy->close(sock); + return; + } + c.tcpReadBuf[c.tcpReadPtr++] = ((const char *)data)[i]; + + if (c.tcpReadPtr >= 5) { + unsigned long mlen = ( ((((unsigned long)c.tcpReadBuf[3]) & 0xff) << 8) | (((unsigned long)c.tcpReadBuf[4]) & 0xff) ); + if (c.tcpReadPtr >= (mlen + 5)) { + if (mlen == 4) { + // Right now just sending this means the client is 'new enough' for the IP header + c.newVersion = true; + //printf("<< TCP %.16llx HELLO\n",(unsigned long long)*uptr); + } else if (mlen >= 7) { + char *payload = c.tcpReadBuf + 5; + unsigned long payloadLen = mlen; + + struct sockaddr_in dest; + memset(&dest,0,sizeof(dest)); + if (c.newVersion) { + if (*payload == (char)4) { + // New clients tell us where their packets go. + ++payload; + dest.sin_family = AF_INET; + dest.sin_addr.s_addr = *((uint32_t *)payload); + payload += 4; + dest.sin_port = *((uint16_t *)payload); // will be in network byte order already + payload += 2; + payloadLen -= 7; + } + } else { + // For old clients we will just proxy everything to a local ZT instance. The + // fact that this will come from 127.0.0.1 will in turn prevent that instance + // from doing unite() with us. It'll just forward. There will not be many of + // these. + dest.sin_family = AF_INET; + dest.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 + dest.sin_port = htons(9993); + } + + // Note: we do not relay to privileged ports... just an abuse prevention rule. + if ((ntohs(dest.sin_port) > 1024)&&(payloadLen >= 16)) { + phy->udpSend(c.udp,(const struct sockaddr *)&dest,payload,payloadLen); + //printf(">> TCP %.16llx to %s:%d\n",(unsigned long long)*uptr,inet_ntoa(dest.sin_addr),(int)ntohs(dest.sin_port)); + } + } + + memmove(c.tcpReadBuf,c.tcpReadBuf + (mlen + 5),c.tcpReadPtr -= (mlen + 5)); + } + } + } + } + + void phyOnTcpWritable(PhySocket *sock,void **uptr) + { + Client &c = *((Client *)*uptr); + if (c.tcpWritePtr) { + long n = phy->streamSend(sock,c.tcpWriteBuf,c.tcpWritePtr); + if (n > 0) { + memmove(c.tcpWriteBuf,c.tcpWriteBuf + n,c.tcpWritePtr -= (unsigned long)n); + if (!c.tcpWritePtr) + phy->setNotifyWritable(sock,false); + } + } else phy->setNotifyWritable(sock,false); + } + + void doHousekeeping() + { + std::vector toClose; + time_t now = time((time_t *)0); + for(std::map< PhySocket *,Client >::iterator c(clients.begin());c!=clients.end();++c) { + if ((now - c->second.lastActivity) >= ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS) { + toClose.push_back(c->first); + toClose.push_back(c->second.udp); + } + } + for(std::vector::iterator s(toClose.begin());s!=toClose.end();++s) + phy->close(*s); + } +}; + +int main(int argc,char **argv) +{ + signal(SIGPIPE,SIG_IGN); + signal(SIGHUP,SIG_IGN); + srand(time((time_t *)0)); + + TcpProxyService svc; + Phy phy(&svc,false,true); + svc.phy = &phy; + svc.udpPortCounter = 1023; + + { + struct sockaddr_in laddr; + memset(&laddr,0,sizeof(laddr)); + laddr.sin_family = AF_INET; + laddr.sin_port = htons(ZT_TCP_PROXY_TCP_PORT); + if (!phy.tcpListen((const struct sockaddr *)&laddr)) { + fprintf(stderr,"%s: fatal error: unable to bind TCP port %d\n",argv[0],ZT_TCP_PROXY_TCP_PORT); + return 1; + } + } + + time_t lastDidHousekeeping = time((time_t *)0); + for(;;) { + phy.poll(120000); + time_t now = time((time_t *)0); + if ((now - lastDidHousekeeping) > 120) { + lastDidHousekeeping = now; + svc.doHousekeeping(); + } + } + + return 0; +} diff --git a/service/OneService.cpp b/service/OneService.cpp index 1fabb7d9..7d290df7 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -85,16 +85,6 @@ using json = nlohmann::json; -/** - * Uncomment to enable UDP breakage switch - * - * If this is defined, the presence of a file called /tmp/ZT_BREAK_UDP - * will cause direct UDP TX/RX to stop working. This can be used to - * test TCP tunneling fallback and other robustness features. Deleting - * this file will cause it to start working again. - */ -//#define ZT_BREAK_UDP - #include "../controller/EmbeddedNetworkController.hpp" #ifdef ZT_USE_TEST_TAP @@ -105,11 +95,13 @@ namespace ZeroTier { typedef TestEthernetTap EthernetTap; } #else #ifdef ZT_SDK - #include "../controller/EmbeddedNetworkController.hpp" - #include "../node/Node.hpp" - // Use the virtual netcon endpoint instead of a tun/tap port driver - #include "../src/SocketTap.hpp" - namespace ZeroTier { typedef SocketTap EthernetTap; } + +#include "../controller/EmbeddedNetworkController.hpp" +#include "../node/Node.hpp" +// Use the virtual netcon endpoint instead of a tun/tap port driver +#include "../src/SocketTap.hpp" +namespace ZeroTier { typedef SocketTap EthernetTap; } + #else #ifdef __APPLE__ @@ -1783,11 +1775,6 @@ public: } #endif -#ifdef ZT_BREAK_UDP - if (OSUtils::fileExists("/tmp/ZT_BREAK_UDP")) - return; -#endif - if ((len >= 16)&&(reinterpret_cast(from)->ipScope() == InetAddress::IP_SCOPE_GLOBAL)) _lastDirectReceiveFromGlobal = OSUtils::now(); @@ -2271,11 +2258,6 @@ public: return -1; } -#ifdef ZT_BREAK_UDP - if (OSUtils::fileExists("/tmp/ZT_BREAK_UDP")) - return 0; // silently break UDP -#endif - return (_bindings[fromBindingNo].udpSend(_phy,*(reinterpret_cast(localAddr)),*(reinterpret_cast(addr)),data,len,ttl)) ? 0 : -1; } diff --git a/service/OneService.hpp b/service/OneService.hpp index b770a3c0..eba10ca0 100644 --- a/service/OneService.hpp +++ b/service/OneService.hpp @@ -33,10 +33,10 @@ #include "../node/InetAddress.hpp" #ifdef ZT_SDK - #include "../node/Node.hpp" - // Use the virtual netcon endpoint instead of a tun/tap port driver - #include "../src/SocketTap.hpp" - namespace ZeroTier { typedef SocketTap EthernetTap; } +#include "../node/Node.hpp" +// Use the virtual netcon endpoint instead of a tun/tap port driver +#include "../src/SocketTap.hpp" +namespace ZeroTier { typedef SocketTap EthernetTap; } #endif namespace ZeroTier { @@ -147,42 +147,15 @@ public: virtual std::string portDeviceName(uint64_t nwid) const = 0; #ifdef ZT_SDK - /** - * Leaves a network - */ - virtual void leave(const char *hp) = 0; - - /** - * Joins a network - */ + virtual void leave(const char *hp) = 0; virtual void join(const char *hp) = 0; - - /** - * Returns the homePath given by the client application - */ - virtual std::string givenHomePath() = 0; - - /* - * Returns a SocketTap that is associated with the given nwid - */ - virtual EthernetTap * getTap(uint64_t nwid) = 0; - - /* - * Returns a SocketTap that cant function as a route to the specified host - */ - virtual EthernetTap * getTap(InetAddress &addr) = 0; - - /* - * Returns a pointer to the Node - */ + virtual std::string givenHomePath() = 0; + virtual EthernetTap * getTap(uint64_t nwid) = 0; + virtual EthernetTap * getTap(InetAddress &addr) = 0; virtual Node * getNode() = 0; - - /* - * Delete all SocketTap interfaces - */ virtual void removeNets() = 0; #endif - + /** * Terminate background service (can be called from other threads) */ diff --git a/tcp-proxy/Makefile b/tcp-proxy/Makefile deleted file mode 100644 index af4e71e3..00000000 --- a/tcp-proxy/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -CXX=$(shell which clang++ g++ c++ 2>/dev/null | head -n 1) - -all: - $(CXX) -O3 -fno-rtti -o tcp-proxy tcp-proxy.cpp - -clean: - rm -f *.o tcp-proxy *.dSYM diff --git a/tcp-proxy/README.md b/tcp-proxy/README.md deleted file mode 100644 index 6f347d64..00000000 --- a/tcp-proxy/README.md +++ /dev/null @@ -1,4 +0,0 @@ -TCP Proxy Server -====== - -This is the TCP proxy server we run for TCP tunneling from peers behind fascist NATs. Regular users won't have much use for this. diff --git a/tcp-proxy/tcp-proxy.cpp b/tcp-proxy/tcp-proxy.cpp deleted file mode 100644 index a7906aae..00000000 --- a/tcp-proxy/tcp-proxy.cpp +++ /dev/null @@ -1,317 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -// HACK! Will eventually use epoll() or something in Phy<> instead of select(). -// Also be sure to change ulimit -n and fs.file-max in /etc/sysctl.conf on relays. -#if defined(__linux__) || defined(__LINUX__) || defined(__LINUX) || defined(LINUX) -#include -#include -#undef __FD_SETSIZE -#define __FD_SETSIZE 1048576 -#undef FD_SETSIZE -#define FD_SETSIZE 1048576 -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "../osdep/Phy.hpp" - -#define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300 -#define ZT_TCP_PROXY_TCP_PORT 443 - -using namespace ZeroTier; - -/* - * ZeroTier TCP Proxy Server - * - * This implements a simple packet encapsulation that is designed to look like - * a TLS connection. It's not a TLS connection, but it sends TLS format record - * headers. It could be extended in the future to implement a fake TLS - * handshake. - * - * At the moment, each packet is just made to look like TLS application data: - * <[1] TLS content type> - currently 0x17 for "application data" - * <[1] TLS major version> - currently 0x03 for TLS 1.2 - * <[1] TLS minor version> - currently 0x03 for TLS 1.2 - * <[2] payload length> - 16-bit length of payload in bytes - * <[...] payload> - Message payload - * - * TCP is inherently inefficient for encapsulating Ethernet, since TCP and TCP - * like protocols over TCP lead to double-ACKs. So this transport is only used - * to enable access when UDP or other datagram protocols are not available. - * - * Clients send a greeting, which is a four-byte message that contains: - * <[1] ZeroTier major version> - * <[1] minor version> - * <[2] revision> - * - * If a client has sent a greeting, it uses the new version of this protocol - * in which every encapsulated ZT packet is prepended by an IP address where - * it should be forwarded (or where it came from for replies). This causes - * this proxy to act as a remote UDP socket similar to a socks proxy, which - * will allow us to move this function off the rootservers and onto dedicated - * proxy nodes. - * - * Older ZT clients that do not send this message get their packets relayed - * to/from 127.0.0.1:9993, which will allow them to talk to and relay via - * the ZT node on the same machine as the proxy. We'll only support this for - * as long as such nodes appear to be in the wild. - */ - -struct TcpProxyService; -struct TcpProxyService -{ - Phy *phy; - int udpPortCounter; - struct Client - { - char tcpReadBuf[131072]; - char tcpWriteBuf[131072]; - unsigned long tcpWritePtr; - unsigned long tcpReadPtr; - PhySocket *tcp; - PhySocket *udp; - time_t lastActivity; - bool newVersion; - }; - std::map< PhySocket *,Client > clients; - - PhySocket *getUnusedUdp(void *uptr) - { - for(int i=0;i<65535;++i) { - ++udpPortCounter; - if (udpPortCounter > 0xfffe) - udpPortCounter = 1024; - struct sockaddr_in laddr; - memset(&laddr,0,sizeof(struct sockaddr_in)); - laddr.sin_family = AF_INET; - laddr.sin_port = htons((uint16_t)udpPortCounter); - PhySocket *udp = phy->udpBind(reinterpret_cast(&laddr),uptr); - if (udp) - return udp; - } - return (PhySocket *)0; - } - - void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) - { - if (!*uptr) - return; - if ((from->sa_family == AF_INET)&&(len >= 16)&&(len < 2048)) { - Client &c = *((Client *)*uptr); - c.lastActivity = time((time_t *)0); - - unsigned long mlen = len; - if (c.newVersion) - mlen += 7; // new clients get IP info - - if ((c.tcpWritePtr + 5 + mlen) <= sizeof(c.tcpWriteBuf)) { - if (!c.tcpWritePtr) - phy->setNotifyWritable(c.tcp,true); - - c.tcpWriteBuf[c.tcpWritePtr++] = 0x17; // look like TLS data - c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 - c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 - - c.tcpWriteBuf[c.tcpWritePtr++] = (char)((mlen >> 8) & 0xff); - c.tcpWriteBuf[c.tcpWritePtr++] = (char)(mlen & 0xff); - - if (c.newVersion) { - c.tcpWriteBuf[c.tcpWritePtr++] = (char)4; // IPv4 - *((uint32_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_addr.s_addr; - c.tcpWritePtr += 4; - *((uint16_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_port; - c.tcpWritePtr += 2; - } - - for(unsigned long i=0;i %.16llx\n",inet_ntoa(reinterpret_cast(from)->sin_addr),(int)ntohs(reinterpret_cast(from)->sin_port),(unsigned long long)&c); - } - } - - void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) - { - // unused, we don't initiate outbound connections - } - - void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) - { - Client &c = clients[sockN]; - PhySocket *udp = getUnusedUdp((void *)&c); - if (!udp) { - phy->close(sockN); - clients.erase(sockN); - //printf("** TCP rejected, no more UDP ports to assign\n"); - return; - } - c.tcpWritePtr = 0; - c.tcpReadPtr = 0; - c.tcp = sockN; - c.udp = udp; - c.lastActivity = time((time_t *)0); - c.newVersion = false; - *uptrN = (void *)&c; - //printf("<< TCP from %s -> %.16llx\n",inet_ntoa(reinterpret_cast(from)->sin_addr),(unsigned long long)&c); - } - - void phyOnTcpClose(PhySocket *sock,void **uptr) - { - if (!*uptr) - return; - Client &c = *((Client *)*uptr); - phy->close(c.udp); - clients.erase(sock); - //printf("** TCP %.16llx closed\n",(unsigned long long)*uptr); - } - - void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) - { - Client &c = *((Client *)*uptr); - c.lastActivity = time((time_t *)0); - - for(unsigned long i=0;i= sizeof(c.tcpReadBuf)) { - phy->close(sock); - return; - } - c.tcpReadBuf[c.tcpReadPtr++] = ((const char *)data)[i]; - - if (c.tcpReadPtr >= 5) { - unsigned long mlen = ( ((((unsigned long)c.tcpReadBuf[3]) & 0xff) << 8) | (((unsigned long)c.tcpReadBuf[4]) & 0xff) ); - if (c.tcpReadPtr >= (mlen + 5)) { - if (mlen == 4) { - // Right now just sending this means the client is 'new enough' for the IP header - c.newVersion = true; - //printf("<< TCP %.16llx HELLO\n",(unsigned long long)*uptr); - } else if (mlen >= 7) { - char *payload = c.tcpReadBuf + 5; - unsigned long payloadLen = mlen; - - struct sockaddr_in dest; - memset(&dest,0,sizeof(dest)); - if (c.newVersion) { - if (*payload == (char)4) { - // New clients tell us where their packets go. - ++payload; - dest.sin_family = AF_INET; - dest.sin_addr.s_addr = *((uint32_t *)payload); - payload += 4; - dest.sin_port = *((uint16_t *)payload); // will be in network byte order already - payload += 2; - payloadLen -= 7; - } - } else { - // For old clients we will just proxy everything to a local ZT instance. The - // fact that this will come from 127.0.0.1 will in turn prevent that instance - // from doing unite() with us. It'll just forward. There will not be many of - // these. - dest.sin_family = AF_INET; - dest.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 - dest.sin_port = htons(9993); - } - - // Note: we do not relay to privileged ports... just an abuse prevention rule. - if ((ntohs(dest.sin_port) > 1024)&&(payloadLen >= 16)) { - phy->udpSend(c.udp,(const struct sockaddr *)&dest,payload,payloadLen); - //printf(">> TCP %.16llx to %s:%d\n",(unsigned long long)*uptr,inet_ntoa(dest.sin_addr),(int)ntohs(dest.sin_port)); - } - } - - memmove(c.tcpReadBuf,c.tcpReadBuf + (mlen + 5),c.tcpReadPtr -= (mlen + 5)); - } - } - } - } - - void phyOnTcpWritable(PhySocket *sock,void **uptr) - { - Client &c = *((Client *)*uptr); - if (c.tcpWritePtr) { - long n = phy->streamSend(sock,c.tcpWriteBuf,c.tcpWritePtr); - if (n > 0) { - memmove(c.tcpWriteBuf,c.tcpWriteBuf + n,c.tcpWritePtr -= (unsigned long)n); - if (!c.tcpWritePtr) - phy->setNotifyWritable(sock,false); - } - } else phy->setNotifyWritable(sock,false); - } - - void doHousekeeping() - { - std::vector toClose; - time_t now = time((time_t *)0); - for(std::map< PhySocket *,Client >::iterator c(clients.begin());c!=clients.end();++c) { - if ((now - c->second.lastActivity) >= ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS) { - toClose.push_back(c->first); - toClose.push_back(c->second.udp); - } - } - for(std::vector::iterator s(toClose.begin());s!=toClose.end();++s) - phy->close(*s); - } -}; - -int main(int argc,char **argv) -{ - signal(SIGPIPE,SIG_IGN); - signal(SIGHUP,SIG_IGN); - srand(time((time_t *)0)); - - TcpProxyService svc; - Phy phy(&svc,false,true); - svc.phy = &phy; - svc.udpPortCounter = 1023; - - { - struct sockaddr_in laddr; - memset(&laddr,0,sizeof(laddr)); - laddr.sin_family = AF_INET; - laddr.sin_port = htons(ZT_TCP_PROXY_TCP_PORT); - if (!phy.tcpListen((const struct sockaddr *)&laddr)) { - fprintf(stderr,"%s: fatal error: unable to bind TCP port %d\n",argv[0],ZT_TCP_PROXY_TCP_PORT); - return 1; - } - } - - time_t lastDidHousekeeping = time((time_t *)0); - for(;;) { - phy.poll(120000); - time_t now = time((time_t *)0); - if ((now - lastDidHousekeeping) > 120) { - lastDidHousekeeping = now; - svc.doHousekeeping(); - } - } - - return 0; -} -- cgit v1.2.3 From d7b4f24a7a8193992c39140fdc6c40a58f3fb2b3 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 1 Jun 2017 17:21:57 -0700 Subject: . --- attic/kubernetes_docs/.zerotierCliSettings | 18 ++++ attic/kubernetes_docs/Dockerfile | 19 ++++ attic/kubernetes_docs/README.md | 150 +++++++++++++++++++++++++++++ attic/kubernetes_docs/entrypoint.sh | 23 +++++ attic/kubernetes_docs/server.js | 8 ++ doc/ext/kubernetes/.zerotierCliSettings | 18 ---- doc/ext/kubernetes/Dockerfile | 19 ---- doc/ext/kubernetes/README.md | 150 ----------------------------- doc/ext/kubernetes/entrypoint.sh | 23 ----- doc/ext/kubernetes/server.js | 8 -- 10 files changed, 218 insertions(+), 218 deletions(-) create mode 100644 attic/kubernetes_docs/.zerotierCliSettings create mode 100644 attic/kubernetes_docs/Dockerfile create mode 100644 attic/kubernetes_docs/README.md create mode 100644 attic/kubernetes_docs/entrypoint.sh create mode 100644 attic/kubernetes_docs/server.js delete mode 100644 doc/ext/kubernetes/.zerotierCliSettings delete mode 100644 doc/ext/kubernetes/Dockerfile delete mode 100644 doc/ext/kubernetes/README.md delete mode 100644 doc/ext/kubernetes/entrypoint.sh delete mode 100644 doc/ext/kubernetes/server.js (limited to 'attic') diff --git a/attic/kubernetes_docs/.zerotierCliSettings b/attic/kubernetes_docs/.zerotierCliSettings new file mode 100644 index 00000000..0e7df9b6 --- /dev/null +++ b/attic/kubernetes_docs/.zerotierCliSettings @@ -0,0 +1,18 @@ +{ + "configVersion": 1, + "defaultCentral": "@my.zerotier.com", + "defaultController": "@my.zerotier.com", + "defaultOne": "@local", + "things": { + "local": { + "auth": "local_service_auth_token_replaced_automatically", + "type": "one", + "url": "http://127.0.0.1:9993/" + }, + "my.zerotier.com": { + "auth": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + "type": "central", + "url": "https://my.zerotier.com/" + } + } +} diff --git a/attic/kubernetes_docs/Dockerfile b/attic/kubernetes_docs/Dockerfile new file mode 100644 index 00000000..6437a2bb --- /dev/null +++ b/attic/kubernetes_docs/Dockerfile @@ -0,0 +1,19 @@ +FROM node:4.4 +EXPOSE 8080/tcp 9993/udp + +# Install ZT network conf files +RUN mkdir -p /var/lib/zerotier-one/networks.d +ADD *.conf /var/lib/zerotier-one/networks.d/ +ADD *.conf / +ADD zerotier-one / +ADD zerotier-cli / +ADD .zerotierCliSettings / + +# Install App +ADD server.js / + +# script which will start/auth VM on ZT network +ADD entrypoint.sh / +RUN chmod -v +x /entrypoint.sh + +CMD ["./entrypoint.sh"] \ No newline at end of file diff --git a/attic/kubernetes_docs/README.md b/attic/kubernetes_docs/README.md new file mode 100644 index 00000000..482e77e5 --- /dev/null +++ b/attic/kubernetes_docs/README.md @@ -0,0 +1,150 @@ +Kubernetes + ZeroTier +==== + +A self-authorizing Kubernetes cluster deployment over a private ZeroTier network. + +This is a quick tutorial for setting up a Kubernetes deployment which can self-authorize each new replica onto your private ZeroTier network with no additional configuration needed when you scale. The Kubernetes-specific instructions and content is based on the [hellonode](http://kubernetes.io/docs/hellonode/) tutorial. All of the files discussed below can be found [here](); + + + +## Preliminary tasks + +**Step 1: Go to [my.zerotier.com](https://my.zerotier.com) and generate a network controller API key. This key will be used by ZeroTier to automatically authorize new instances of your VMs to join your secure deployment network during replication.** + +**Step 2: Create a new `private` network. Take note of the network ID, henceforth: `nwid`** + +**Step 3: Follow the instructions from the [hellonode](ttp://kubernetes.io/docs/hellonode/) tutorial to set up your development system.** + +*** +## Construct docker image + +**Step 4: Create necessary files for inclusion into image, your resultant directory should contain:** + + - `ztkube/.conf` + - `ztkube/Dockerfile` + - `ztkube/entrypoint.sh` + - `ztkube/server.js` + - `ztkube/zerotier-cli` + - `ztkube/zerotier-one` + +Start by creating a build directory to copy all required files into `mkdir ztkube`. Then build the following: + - `make one` + - `make cli` + +Add the following files to the `ztkube` directory. These files will be compiled into the Docker image. + + - Create an empty `.conf` file to specify the private deployment network you created in *Step 2*: + + - Create a CLI tool config file `.zerotierCliSettings` which should only contain your network controller API key to authorize new devices on your network (the local service API key will be filled in automatically). In this example the default controller is hosted by us at [my.zerotier.com](https://my.zerotier.com). Alternatively, you can host your own network controller but you'll need to modify the CLI config file accordingly. + +``` +{ + "configVersion": 1, + "defaultCentral": "@my.zerotier.com", + "defaultController": "@my.zerotier.com", + "defaultOne": "@local", + "things": { + "local": { + "auth": "local_service_auth_token_replaced_automatically", + "type": "one", + "url": "http://127.0.0.1:9993/" + }, + "my.zerotier.com": { + "auth": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", + "type": "central", + "url": "https://my.zerotier.com/" + } + } +} +``` + + + - Create a `Dockerfile` which will copy the ZeroTier service as well as the ZeroTier CLI to the image: + +``` +FROM node:4.4 +EXPOSE 8080/tcp 9993/udp + +# Install ZT network conf files +RUN mkdir -p /var/lib/zerotier-one/networks.d +ADD *.conf /var/lib/zerotier-one/networks.d/ +ADD *.conf / +ADD zerotier-one / +ADD zerotier-cli / +ADD .zerotierCliSettings / + +# Install App +ADD server.js / + +# script which will start/auth VM on ZT network +ADD entrypoint.sh / +RUN chmod -v +x /entrypoint.sh + +CMD ["./entrypoint.sh"] +``` + + - Create the `entrypoint.sh` script which will start the ZeroTier service in the VM, attempt to join your deployment network and automatically authorize the new VM if your network is set to private: + +``` +#!/bin/bash + +echo '*** ZeroTier-Kubernetes self-auth test script' +chown -R daemon /var/lib/zerotier-one +chgrp -R daemon /var/lib/zerotier-one +su daemon -s /bin/bash -c '/zerotier-one -d -U -p9993 >>/tmp/zerotier-one.out 2>&1' +dev="" +nwconf=$(ls *.conf) +nwid="${nwconf%.*}" + +sleep 10 +dev=$(cat /var/lib/zerotier-one/identity.public| cut -d ':' -f 1) + +echo '*** Joining' +./zerotier-cli join "$nwid".conf +# Fill out local service auth token +AUTHTOKEN=$(cat /var/lib/zerotier-one/authtoken.secret) +sed "s|\local_service_auth_token_replaced_automatically|${AUTHTOKEN}|" .zerotierCliSettings > /root/.zerotierCliSettings +echo '*** Authorizing' +./zerotier-cli net-auth @my.zerotier.com "$nwid" "$dev" +echo '*** Cleaning up' # Remove controller auth token +rm -rf .zerotierCliSettings /root/.zerotierCliSettings +node server.js +``` + +**Step 5: Build the image:** + + - `docker build -t gcr.io/$PROJECT_ID/hello-node .` + + + +**Step 6: Push the docker image to your *Container Registry*** + + - `gcloud docker push gcr.io/$PROJECT_ID/hello-node:v1` + +*** +## Deploy! + +**Step 7: Create Kubernetes Cluster** + + - `gcloud config set compute/zone us-central1-a` + + - `gcloud container clusters create hello-world` + + - `gcloud container clusters get-credentials hello-world` + + + +**Step 8: Create your pod** + + - `kubectl run hello-node --image=gcr.io/$PROJECT_ID/hello-node:v1 --port=8080` + + + +**Step 9: Scale** + + - `kubectl scale deployment hello-node --replicas=4` + +*** +## Verify + +Now, after a minute or so you can use `zerotier-cli net-members ` to show all of your VM instances on your ZeroTier deployment network. If you haven't [configured your local CLI](https://github.com/zerotier/ZeroTierOne/tree/dev/cli), you can simply log into [my.zerotier.com](https://my.zerotier.com), go to *Networks -> nwid* to check that your VMs are indeed members of your private network. You should also note that the `entrypoint.sh` script will automatically delete your network controller API key once it has authorized your VM. This is merely a security measure and can be removed if needed. diff --git a/attic/kubernetes_docs/entrypoint.sh b/attic/kubernetes_docs/entrypoint.sh new file mode 100644 index 00000000..80cd278e --- /dev/null +++ b/attic/kubernetes_docs/entrypoint.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +echo '*** ZeroTier-Kubernetes self-auth test script' +chown -R daemon /var/lib/zerotier-one +chgrp -R daemon /var/lib/zerotier-one +su daemon -s /bin/bash -c '/zerotier-one -d -U -p9993 >>/tmp/zerotier-one.out 2>&1' +dev="" +nwconf=$(ls *.conf) +nwid="${nwconf%.*}" + +sleep 10 +dev=$(cat /var/lib/zerotier-one/identity.public| cut -d ':' -f 1) + +echo '*** Joining' +./zerotier-cli join "$nwid".conf +# Fill out local service auth token +AUTHTOKEN=$(cat /var/lib/zerotier-one/authtoken.secret) +sed "s|\local_service_auth_token_replaced_automatically|${AUTHTOKEN}|" .zerotierCliSettings > /root/.zerotierCliSettings +echo '*** Authorizing' +./zerotier-cli net-auth @my.zerotier.com "$nwid" "$dev" +echo '*** Cleaning up' # Remove controller auth token +rm -rf .zerotierCliSettings /root/.zerotierCliSettings +node server.js \ No newline at end of file diff --git a/attic/kubernetes_docs/server.js b/attic/kubernetes_docs/server.js new file mode 100644 index 00000000..a4b08bb8 --- /dev/null +++ b/attic/kubernetes_docs/server.js @@ -0,0 +1,8 @@ +var http = require('http'); +var handleRequest = function(request, response) { + console.log('Received request for URL: ' + request.url); + response.writeHead(200); + response.end('Hello World!'); +}; +var www = http.createServer(handleRequest); +www.listen(8080); diff --git a/doc/ext/kubernetes/.zerotierCliSettings b/doc/ext/kubernetes/.zerotierCliSettings deleted file mode 100644 index 0e7df9b6..00000000 --- a/doc/ext/kubernetes/.zerotierCliSettings +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configVersion": 1, - "defaultCentral": "@my.zerotier.com", - "defaultController": "@my.zerotier.com", - "defaultOne": "@local", - "things": { - "local": { - "auth": "local_service_auth_token_replaced_automatically", - "type": "one", - "url": "http://127.0.0.1:9993/" - }, - "my.zerotier.com": { - "auth": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "type": "central", - "url": "https://my.zerotier.com/" - } - } -} diff --git a/doc/ext/kubernetes/Dockerfile b/doc/ext/kubernetes/Dockerfile deleted file mode 100644 index 6437a2bb..00000000 --- a/doc/ext/kubernetes/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM node:4.4 -EXPOSE 8080/tcp 9993/udp - -# Install ZT network conf files -RUN mkdir -p /var/lib/zerotier-one/networks.d -ADD *.conf /var/lib/zerotier-one/networks.d/ -ADD *.conf / -ADD zerotier-one / -ADD zerotier-cli / -ADD .zerotierCliSettings / - -# Install App -ADD server.js / - -# script which will start/auth VM on ZT network -ADD entrypoint.sh / -RUN chmod -v +x /entrypoint.sh - -CMD ["./entrypoint.sh"] \ No newline at end of file diff --git a/doc/ext/kubernetes/README.md b/doc/ext/kubernetes/README.md deleted file mode 100644 index 482e77e5..00000000 --- a/doc/ext/kubernetes/README.md +++ /dev/null @@ -1,150 +0,0 @@ -Kubernetes + ZeroTier -==== - -A self-authorizing Kubernetes cluster deployment over a private ZeroTier network. - -This is a quick tutorial for setting up a Kubernetes deployment which can self-authorize each new replica onto your private ZeroTier network with no additional configuration needed when you scale. The Kubernetes-specific instructions and content is based on the [hellonode](http://kubernetes.io/docs/hellonode/) tutorial. All of the files discussed below can be found [here](); - - - -## Preliminary tasks - -**Step 1: Go to [my.zerotier.com](https://my.zerotier.com) and generate a network controller API key. This key will be used by ZeroTier to automatically authorize new instances of your VMs to join your secure deployment network during replication.** - -**Step 2: Create a new `private` network. Take note of the network ID, henceforth: `nwid`** - -**Step 3: Follow the instructions from the [hellonode](ttp://kubernetes.io/docs/hellonode/) tutorial to set up your development system.** - -*** -## Construct docker image - -**Step 4: Create necessary files for inclusion into image, your resultant directory should contain:** - - - `ztkube/.conf` - - `ztkube/Dockerfile` - - `ztkube/entrypoint.sh` - - `ztkube/server.js` - - `ztkube/zerotier-cli` - - `ztkube/zerotier-one` - -Start by creating a build directory to copy all required files into `mkdir ztkube`. Then build the following: - - `make one` - - `make cli` - -Add the following files to the `ztkube` directory. These files will be compiled into the Docker image. - - - Create an empty `.conf` file to specify the private deployment network you created in *Step 2*: - - - Create a CLI tool config file `.zerotierCliSettings` which should only contain your network controller API key to authorize new devices on your network (the local service API key will be filled in automatically). In this example the default controller is hosted by us at [my.zerotier.com](https://my.zerotier.com). Alternatively, you can host your own network controller but you'll need to modify the CLI config file accordingly. - -``` -{ - "configVersion": 1, - "defaultCentral": "@my.zerotier.com", - "defaultController": "@my.zerotier.com", - "defaultOne": "@local", - "things": { - "local": { - "auth": "local_service_auth_token_replaced_automatically", - "type": "one", - "url": "http://127.0.0.1:9993/" - }, - "my.zerotier.com": { - "auth": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", - "type": "central", - "url": "https://my.zerotier.com/" - } - } -} -``` - - - - Create a `Dockerfile` which will copy the ZeroTier service as well as the ZeroTier CLI to the image: - -``` -FROM node:4.4 -EXPOSE 8080/tcp 9993/udp - -# Install ZT network conf files -RUN mkdir -p /var/lib/zerotier-one/networks.d -ADD *.conf /var/lib/zerotier-one/networks.d/ -ADD *.conf / -ADD zerotier-one / -ADD zerotier-cli / -ADD .zerotierCliSettings / - -# Install App -ADD server.js / - -# script which will start/auth VM on ZT network -ADD entrypoint.sh / -RUN chmod -v +x /entrypoint.sh - -CMD ["./entrypoint.sh"] -``` - - - Create the `entrypoint.sh` script which will start the ZeroTier service in the VM, attempt to join your deployment network and automatically authorize the new VM if your network is set to private: - -``` -#!/bin/bash - -echo '*** ZeroTier-Kubernetes self-auth test script' -chown -R daemon /var/lib/zerotier-one -chgrp -R daemon /var/lib/zerotier-one -su daemon -s /bin/bash -c '/zerotier-one -d -U -p9993 >>/tmp/zerotier-one.out 2>&1' -dev="" -nwconf=$(ls *.conf) -nwid="${nwconf%.*}" - -sleep 10 -dev=$(cat /var/lib/zerotier-one/identity.public| cut -d ':' -f 1) - -echo '*** Joining' -./zerotier-cli join "$nwid".conf -# Fill out local service auth token -AUTHTOKEN=$(cat /var/lib/zerotier-one/authtoken.secret) -sed "s|\local_service_auth_token_replaced_automatically|${AUTHTOKEN}|" .zerotierCliSettings > /root/.zerotierCliSettings -echo '*** Authorizing' -./zerotier-cli net-auth @my.zerotier.com "$nwid" "$dev" -echo '*** Cleaning up' # Remove controller auth token -rm -rf .zerotierCliSettings /root/.zerotierCliSettings -node server.js -``` - -**Step 5: Build the image:** - - - `docker build -t gcr.io/$PROJECT_ID/hello-node .` - - - -**Step 6: Push the docker image to your *Container Registry*** - - - `gcloud docker push gcr.io/$PROJECT_ID/hello-node:v1` - -*** -## Deploy! - -**Step 7: Create Kubernetes Cluster** - - - `gcloud config set compute/zone us-central1-a` - - - `gcloud container clusters create hello-world` - - - `gcloud container clusters get-credentials hello-world` - - - -**Step 8: Create your pod** - - - `kubectl run hello-node --image=gcr.io/$PROJECT_ID/hello-node:v1 --port=8080` - - - -**Step 9: Scale** - - - `kubectl scale deployment hello-node --replicas=4` - -*** -## Verify - -Now, after a minute or so you can use `zerotier-cli net-members ` to show all of your VM instances on your ZeroTier deployment network. If you haven't [configured your local CLI](https://github.com/zerotier/ZeroTierOne/tree/dev/cli), you can simply log into [my.zerotier.com](https://my.zerotier.com), go to *Networks -> nwid* to check that your VMs are indeed members of your private network. You should also note that the `entrypoint.sh` script will automatically delete your network controller API key once it has authorized your VM. This is merely a security measure and can be removed if needed. diff --git a/doc/ext/kubernetes/entrypoint.sh b/doc/ext/kubernetes/entrypoint.sh deleted file mode 100644 index 80cd278e..00000000 --- a/doc/ext/kubernetes/entrypoint.sh +++ /dev/null @@ -1,23 +0,0 @@ -#!/bin/bash - -echo '*** ZeroTier-Kubernetes self-auth test script' -chown -R daemon /var/lib/zerotier-one -chgrp -R daemon /var/lib/zerotier-one -su daemon -s /bin/bash -c '/zerotier-one -d -U -p9993 >>/tmp/zerotier-one.out 2>&1' -dev="" -nwconf=$(ls *.conf) -nwid="${nwconf%.*}" - -sleep 10 -dev=$(cat /var/lib/zerotier-one/identity.public| cut -d ':' -f 1) - -echo '*** Joining' -./zerotier-cli join "$nwid".conf -# Fill out local service auth token -AUTHTOKEN=$(cat /var/lib/zerotier-one/authtoken.secret) -sed "s|\local_service_auth_token_replaced_automatically|${AUTHTOKEN}|" .zerotierCliSettings > /root/.zerotierCliSettings -echo '*** Authorizing' -./zerotier-cli net-auth @my.zerotier.com "$nwid" "$dev" -echo '*** Cleaning up' # Remove controller auth token -rm -rf .zerotierCliSettings /root/.zerotierCliSettings -node server.js \ No newline at end of file diff --git a/doc/ext/kubernetes/server.js b/doc/ext/kubernetes/server.js deleted file mode 100644 index a4b08bb8..00000000 --- a/doc/ext/kubernetes/server.js +++ /dev/null @@ -1,8 +0,0 @@ -var http = require('http'); -var handleRequest = function(request, response) { - console.log('Received request for URL: ' + request.url); - response.writeHead(200); - response.end('Hello World!'); -}; -var www = http.createServer(handleRequest); -www.listen(8080); -- cgit v1.2.3 From 355cce3938a815feba1085569263ae0225cebfa6 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 27 Jun 2017 11:31:29 -0700 Subject: Rename Utils::snprintf due to it being a #define on one platform. --- attic/DBM.cpp | 243 ------------------------------- attic/DBM.hpp | 168 --------------------- controller/EmbeddedNetworkController.cpp | 26 ++-- controller/JSONDB.cpp | 10 +- node/Address.hpp | 4 +- node/Dictionary.hpp | 4 +- node/InetAddress.cpp | 8 +- node/MAC.hpp | 2 +- node/MulticastGroup.hpp | 2 +- node/Network.cpp | 4 +- node/NetworkConfig.cpp | 2 +- node/Node.cpp | 2 +- node/Utils.cpp | 5 +- node/Utils.hpp | 3 +- one.cpp | 20 +-- osdep/BSDEthernetTap.cpp | 16 +- osdep/Http.cpp | 4 +- osdep/LinuxEthernetTap.cpp | 10 +- osdep/OSUtils.cpp | 6 +- osdep/OSXEthernetTap.cpp | 14 +- osdep/PortMapper.cpp | 4 +- osdep/WindowsEthernetTap.cpp | 10 +- selftest.cpp | 2 +- service/ClusterDefinition.hpp | 2 +- service/OneService.cpp | 62 ++++---- service/SoftwareUpdater.cpp | 2 +- 26 files changed, 111 insertions(+), 524 deletions(-) delete mode 100644 attic/DBM.cpp delete mode 100644 attic/DBM.hpp (limited to 'attic') diff --git a/attic/DBM.cpp b/attic/DBM.cpp deleted file mode 100644 index 54f017e0..00000000 --- a/attic/DBM.cpp +++ /dev/null @@ -1,243 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#include "DBM.hpp" - -#include "../version.h" - -#include "../node/Salsa20.hpp" -#include "../node/Poly1305.hpp" -#include "../node/SHA512.hpp" - -#include "../osdep/OSUtils.hpp" - -#define ZT_STORED_OBJECT_TYPE__CLUSTER_NODE_STATUS (ZT_STORED_OBJECT__MAX_TYPE_ID + 1) -#define ZT_STORED_OBJECT_TYPE__CLUSTER_DEFINITION (ZT_STORED_OBJECT__MAX_TYPE_ID + 2) - -namespace ZeroTier { - -// We generate the cluster ID from our address and version info since this is -// not at all designed to allow interoperation between versions (or endians) -// in the same cluster. -static inline uint64_t _mkClusterId(const Address &myAddress) -{ - uint64_t x = ZEROTIER_ONE_VERSION_MAJOR; - x <<= 8; - x += ZEROTIER_ONE_VERSION_MINOR; - x <<= 8; - x += ZEROTIER_ONE_VERSION_REVISION; - x <<= 40; - x ^= myAddress.toInt(); -#if __BYTE_ORDER == __BIG_ENDIAN - ++x; -#endif; - return x; -} - -void DBM::onUpdate(uint64_t from,const _MapKey &k,const _MapValue &v,uint64_t rev) -{ - char p[4096]; - char tmp[ZT_DBM_MAX_VALUE_SIZE]; - if (_persistentPath((ZT_StoredObjectType)k.type,k.key,p,sizeof(p))) { - // Reduce unnecessary disk writes - FILE *f = fopen(p,"r"); - if (f) { - long n = (long)fread(tmp,1,sizeof(tmp),f); - fclose(f); - if ((n == (long)v.len)&&(!memcmp(v.data,tmp,n))) - return; - } - - // Write to disk if file has changed or was not already present - f = fopen(p,"w"); - if (f) { - if (fwrite(data,len,1,f) != 1) - fprintf(stderr,"WARNING: error writing to %s (I/O error)" ZT_EOL_S,p); - fclose(f); - if (type == ZT_STORED_OBJECT_IDENTITY_SECRET) - OSUtils::lockDownFile(p,false); - } else { - fprintf(stderr,"WARNING: error writing to %s (cannot open)" ZT_EOL_S,p); - } - } -} - -void DBM::onDelete(uint64_t from,const _MapKey &k) -{ - char p[4096]; - if (_persistentPath((ZT_StoredObjectType)k.type,k.key,p,sizeof(p))) - OSUtils::rm(p); -} - -DBM::_vsdm_cryptor::_vsdm_cryptor(const Identity &secretIdentity) -{ - uint8_t s512[64]; - SHA512::hash(h512,secretIdentity.privateKeyPair().priv.data,ZT_C25519_PRIVATE_KEY_LEN); - memcpy(_key,s512,sizeof(_key)); -} - -void DBM::_vsdm_cryptor::encrypt(void *d,unsigned long l) -{ - if (l >= 24) { // sanity check - uint8_t key[32]; - uint8_t authKey[32]; - uint8_t auth[16]; - - uint8_t *const iv = reinterpret_cast(d) + (l - 16); - Utils::getSecureRandom(iv,16); - memcpy(key,_key,32); - for(unsigned long i=0;i<8;++i) - _key[i] ^= iv[i]; - - Salsa20 s20(key,iv + 8); - memset(authKey,0,32); - s20.crypt12(authKey,authKey,32); - s20.crypt12(d,d,l - 24); - - Poly1305::compute(auth,d,l - 24,authKey); - memcpy(reinterpret_cast(d) + (l - 24),auth,8); - } -} - -bool DBM::_vsdm_cryptor::decrypt(void *d,unsigned long l) -{ - if (l >= 24) { // sanity check - uint8_t key[32]; - uint8_t authKey[32]; - uint8_t auth[16]; - - uint8_t *const iv = reinterpret_cast(d) + (l - 16); - memcpy(key,_key,32); - for(unsigned long i=0;i<8;++i) - _key[i] ^= iv[i]; - - Salsa20 s20(key,iv + 8); - memset(authKey,0,32); - s20.crypt12(authKey,authKey,32); - - Poly1305::compute(auth,d,l - 24,authKey); - if (!Utils::secureEq(reinterpret_cast(d) + (l - 24),auth,8)) - return false; - - s20.crypt12(d,d,l - 24); - - return true; - } - return false; -} - -DBM::DBM(const Identity &secretIdentity,uint64_t clusterMemberId,const std::string &basePath,Node *node) : - _basePath(basePath), - _node(node), - _startTime(OSUtils::now()), - _m(_mkClusterId(secretIdentity.address()),clusterMemberId,false,_vsdm_cryptor(secretIdentity),_vsdm_watcher(this)) -{ -} - -DBM::~DBM() -{ -} - -void DBM::put(const ZT_StoredObjectType type,const uint64_t key,const void *data,unsigned int len) -{ - char p[4096]; - if (_m.put(_MapKey(key,(uint16_t)type),Value(OSUtils::now(),(uint16_t)len,data))) { - if (_persistentPath(type,key,p,sizeof(p))) { - FILE *f = fopen(p,"w"); - if (f) { - if (fwrite(data,len,1,f) != 1) - fprintf(stderr,"WARNING: error writing to %s (I/O error)" ZT_EOL_S,p); - fclose(f); - if (type == ZT_STORED_OBJECT_IDENTITY_SECRET) - OSUtils::lockDownFile(p,false); - } else { - fprintf(stderr,"WARNING: error writing to %s (cannot open)" ZT_EOL_S,p); - } - } - } -} - -bool DBM::get(const ZT_StoredObjectType type,const uint64_t key,Value &value) -{ - char p[4096]; - if (_m.get(_MapKey(key,(uint16_t)type),value)) - return true; - if (_persistentPath(type,key,p,sizeof(p))) { - FILE *f = fopen(p,"r"); - if (f) { - long n = (long)fread(value.data,1,sizeof(value.data),f); - value.len = (n > 0) ? (uint16_t)n : (uint16_t)0; - fclose(f); - value.ts = OSUtils::getLastModified(p); - _m.put(_MapKey(key,(uint16_t)type),value); - return true; - } - } - return false; -} - -void DBM::del(const ZT_StoredObjectType type,const uint64_t key) -{ - char p[4096]; - _m.del(_MapKey(key,(uint16_t)type)); - if (_persistentPath(type,key,p,sizeof(p))) - OSUtils::rm(p); -} - -void DBM::clean() -{ -} - -bool DBM::_persistentPath(const ZT_StoredObjectType type,const uint64_t key,char *p,unsigned int maxlen) -{ - switch(type) { - case ZT_STORED_OBJECT_IDENTITY_PUBLIC: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "identity.public",_basePath.c_str()); - return true; - case ZT_STORED_OBJECT_IDENTITY_SECRET: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "identity.secret",_basePath.c_str()); - return true; - case ZT_STORED_OBJECT_IDENTITY: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "iddb.d" ZT_PATH_SEPARATOR_S "%.10llx",_basePath.c_str(),key); - return true; - case ZT_STORED_OBJECT_NETWORK_CONFIG: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.conf",_basePath.c_str(),key); - return true; - case ZT_STORED_OBJECT_PLANET: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "planet",_basePath.c_str()); - return true; - case ZT_STORED_OBJECT_MOON: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "moons.d" ZT_PATH_SEPARATOR_S "%.16llx.moon",_basePath.c_str(),key); - return true; - case (ZT_StoredObjectType)ZT_STORED_OBJECT_TYPE__CLUSTER_DEFINITION: - Utils::snprintf(p,maxlen,"%s" ZT_PATH_SEPARATOR_S "cluster",_basePath.c_str()); - return true; - default: - return false; - } -} - -} // namespace ZeroTier diff --git a/attic/DBM.hpp b/attic/DBM.hpp deleted file mode 100644 index c6d5b8c0..00000000 --- a/attic/DBM.hpp +++ /dev/null @@ -1,168 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#ifndef ZT_DBM_HPP___ -#define ZT_DBM_HPP___ - -#include -#include -#include -#include - -#include - -#include "../node/Constants.hpp" -#include "../node/Mutex.hpp" -#include "../node/Utils.hpp" -#include "../node/Identity.hpp" -#include "../node/Peer.hpp" - -#include "../ext/vsdm/vsdm.hpp" - -// The Peer is the largest structure we persist here -#define ZT_DBM_MAX_VALUE_SIZE sizeof(Peer) - -namespace ZeroTier { - -class Node; -class DBM; - -class DBM -{ -public: - ZT_PACKED_STRUCT(struct Value - { - Value(const uint64_t t,const uint16_t l,const void *d) : - ts(t), - l(l) - { - memcpy(data,d,l); - } - uint64_t ts; - uint16_t len; - uint8_t data[ZT_DBM_MAX_VALUE_SIZE]; - }); - -private: - ZT_PACKED_STRUCT(struct _MapKey - { - _MapKey() : obj(0),type(0) {} - _MapKey(const uint16_t t,const uint64_t o) : obj(o),type(t) {} - uint64_t obj; - uint16_t type; - inline bool operator==(const _MapKey &k) const { return ((obj == k.obj)&&(type == k.type)); } - }); - struct _MapHasher - { - inline std::size_t operator()(const _MapKey &k) const { return (std::size_t)((k.obj ^ (k.obj >> 32)) + (uint64_t)k.type); } - }; - - void onUpdate(uint64_t from,const _MapKey &k,const Value &v,uint64_t rev); - void onDelete(uint64_t from,const _MapKey &k); - - class _vsdm_watcher - { - public: - _vsdm_watcher(DBM *p) : _parent(p) {} - inline void add(uint64_t from,const _MapKey &k,const Value &v,uint64_t rev) { _parent->onUpdate(from,k,v,rev); } - inline void update(uint64_t from,const _MapKey &k,const Value &v,uint64_t rev) { _parent->onUpdate(from,k,v,rev); } - inline void del(uint64_t from,const _MapKey &k) { _parent->onDelete(from,k); } - private: - DBM *_parent; - }; - class _vsdm_serializer - { - public: - static inline unsigned long objectSize(const _MapKey &k) { return 10; } - static inline unsigned long objectSize(const Value &v) { return (10 + v.len); } - static inline const char *objectData(const _MapKey &k) { return reinterpret_cast(&k); } - static inline const char *objectData(const Value &v) { return reinterpret_cast(&v); } - static inline bool objectDeserialize(const char *d,unsigned long l,_MapKey &k) - { - if (l == 10) { - memcpy(&k,d,10); - return true; - } - return false; - } - static inline bool objectDeserialize(const char *d,unsigned long l,Value &v) - { - if ((l >= 10)&&(l <= (10 + ZT_DBM_MAX_VALUE_SIZE))) { - memcpy(&v,d,l); - return true; - } - return false; - } - }; - class _vsdm_cryptor - { - public: - _vsdm_cryptor(const Identity &secretIdentity); - static inline unsigned long overhead() { return 24; } - void encrypt(void *d,unsigned long l); - bool decrypt(void *d,unsigned long l); - uint8_t _key[32]; - }; - - typedef vsdm< _MapKey,Value,16384,_vsdm_watcher,_vsdm_serializer,_vsdm_cryptor,_MapHasher > _Map; - - friend class _Map; - -public: - ZT_PACKED_STRUCT(struct ClusterPeerStatus - { - uint64_t startTime; - uint64_t currentTime; - uint64_t clusterPeersConnected; - uint64_t ztPeersConnected; - uint16_t platform; - uint16_t arch; - }); - - DBM(const Identity &secretIdentity,uint64_t clusterMemberId,const std::string &basePath,Node *node); - - ~DBM(); - - void put(const ZT_StoredObjectType type,const uint64_t key,const void *data,unsigned int len); - - bool get(const ZT_StoredObjectType type,const uint64_t key,Value &value); - - void del(const ZT_StoredObjectType type,const uint64_t key); - - void clean(); - -private: - bool DBM::_persistentPath(const ZT_StoredObjectType type,const uint64_t key,char *p,unsigned int maxlen); - - const std::string _basePath; - Node *const _node; - uint64_t _startTime; - _Map _m; -}; - -} // namespace ZeroTier - -#endif diff --git a/controller/EmbeddedNetworkController.cpp b/controller/EmbeddedNetworkController.cpp index e2eaa788..85c759e7 100644 --- a/controller/EmbeddedNetworkController.cpp +++ b/controller/EmbeddedNetworkController.cpp @@ -122,12 +122,12 @@ static json _renderRule(ZT_VirtualNetworkRule &rule) break; case ZT_NETWORK_RULE_MATCH_MAC_SOURCE: r["type"] = "MATCH_MAC_SOURCE"; - Utils::snprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)rule.v.mac[0],(unsigned int)rule.v.mac[1],(unsigned int)rule.v.mac[2],(unsigned int)rule.v.mac[3],(unsigned int)rule.v.mac[4],(unsigned int)rule.v.mac[5]); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)rule.v.mac[0],(unsigned int)rule.v.mac[1],(unsigned int)rule.v.mac[2],(unsigned int)rule.v.mac[3],(unsigned int)rule.v.mac[4],(unsigned int)rule.v.mac[5]); r["mac"] = tmp; break; case ZT_NETWORK_RULE_MATCH_MAC_DEST: r["type"] = "MATCH_MAC_DEST"; - Utils::snprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)rule.v.mac[0],(unsigned int)rule.v.mac[1],(unsigned int)rule.v.mac[2],(unsigned int)rule.v.mac[3],(unsigned int)rule.v.mac[4],(unsigned int)rule.v.mac[5]); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)rule.v.mac[0],(unsigned int)rule.v.mac[1],(unsigned int)rule.v.mac[2],(unsigned int)rule.v.mac[3],(unsigned int)rule.v.mac[4],(unsigned int)rule.v.mac[5]); r["mac"] = tmp; break; case ZT_NETWORK_RULE_MATCH_IPV4_SOURCE: @@ -179,7 +179,7 @@ static json _renderRule(ZT_VirtualNetworkRule &rule) break; case ZT_NETWORK_RULE_MATCH_CHARACTERISTICS: r["type"] = "MATCH_CHARACTERISTICS"; - Utils::snprintf(tmp,sizeof(tmp),"%.16llx",rule.v.characteristics); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",rule.v.characteristics); r["mask"] = tmp; break; case ZT_NETWORK_RULE_MATCH_FRAME_SIZE_RANGE: @@ -514,7 +514,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( _db.eachMember(nwid,[&responseBody](uint64_t networkId,uint64_t nodeId,const json &member) { if ((member.is_object())&&(member.size() > 0)) { char tmp[128]; - Utils::snprintf(tmp,sizeof(tmp),"%s%.10llx\":%llu",(responseBody.length() > 1) ? ",\"" : "\"",(unsigned long long)nodeId,(unsigned long long)OSUtils::jsonInt(member["revision"],0)); + Utils::ztsnprintf(tmp,sizeof(tmp),"%s%.10llx\":%llu",(responseBody.length() > 1) ? ",\"" : "\"",(unsigned long long)nodeId,(unsigned long long)OSUtils::jsonInt(member["revision"],0)); responseBody.append(tmp); } }); @@ -548,7 +548,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( for(std::vector::const_iterator i(networkIds.begin());i!=networkIds.end();++i) { if (responseBody.length() > 1) responseBody.push_back(','); - Utils::snprintf(tmp,sizeof(tmp),"\"%.16llx\"",(unsigned long long)*i); + Utils::ztsnprintf(tmp,sizeof(tmp),"\"%.16llx\"",(unsigned long long)*i); responseBody.append(tmp); } responseBody.push_back(']'); @@ -562,7 +562,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpGET( // Controller status char tmp[4096]; - Utils::snprintf(tmp,sizeof(tmp),"{\n\t\"controller\": true,\n\t\"apiVersion\": %d,\n\t\"clock\": %llu\n}\n",ZT_NETCONF_CONTROLLER_API_VERSION,(unsigned long long)OSUtils::now()); + Utils::ztsnprintf(tmp,sizeof(tmp),"{\n\t\"controller\": true,\n\t\"apiVersion\": %d,\n\t\"clock\": %llu\n}\n",ZT_NETCONF_CONTROLLER_API_VERSION,(unsigned long long)OSUtils::now()); responseBody = tmp; responseContentType = "application/json"; return 200; @@ -603,14 +603,14 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( if ((path.size() >= 2)&&(path[1].length() == 16)) { uint64_t nwid = Utils::hexStrToU64(path[1].c_str()); char nwids[24]; - Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid); + Utils::ztsnprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid); if (path.size() >= 3) { if ((path.size() == 4)&&(path[2] == "member")&&(path[3].length() == 10)) { uint64_t address = Utils::hexStrToU64(path[3].c_str()); char addrs[24]; - Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)address); + Utils::ztsnprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)address); json member; _db.getNetworkMember(nwid,address,member); @@ -748,7 +748,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( if (!nwid) return 503; } - Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid); + Utils::ztsnprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid); json network; _db.getNetwork(nwid,network); @@ -995,7 +995,7 @@ unsigned int EmbeddedNetworkController::handleControlPlaneHttpPOST( _queue.post(qe); char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"{\"clock\":%llu,\"ping\":%s}",(unsigned long long)now,OSUtils::jsonDump(b).c_str()); + Utils::ztsnprintf(tmp,sizeof(tmp),"{\"clock\":%llu,\"ping\":%s}",(unsigned long long)now,OSUtils::jsonDump(b).c_str()); responseBody = tmp; responseContentType = "application/json"; @@ -1083,7 +1083,7 @@ void EmbeddedNetworkController::threadMain() auto ms = this->_memberStatus.find(_MemberStatusKey(networkId,nodeId)); if (ms != _memberStatus.end()) lrt = ms->second.lastRequestTime; - Utils::snprintf(tmp,sizeof(tmp),"%s\"%.16llx-%.10llx\":%llu", + Utils::ztsnprintf(tmp,sizeof(tmp),"%s\"%.16llx-%.10llx\":%llu", (first) ? "" : ",", (unsigned long long)networkId, (unsigned long long)nodeId, @@ -1093,7 +1093,7 @@ void EmbeddedNetworkController::threadMain() }); } char tmp2[256]; - Utils::snprintf(tmp2,sizeof(tmp2),"},\"clock\":%llu,\"startTime\":%llu}",(unsigned long long)now,(unsigned long long)_startTime); + Utils::ztsnprintf(tmp2,sizeof(tmp2),"},\"clock\":%llu,\"startTime\":%llu}",(unsigned long long)now,(unsigned long long)_startTime); pong.append(tmp2); _db.writeRaw("pong",pong); } @@ -1126,7 +1126,7 @@ void EmbeddedNetworkController::_request( ms.lastRequestTime = now; } - Utils::snprintf(nwids,sizeof(nwids),"%.16llx",nwid); + Utils::ztsnprintf(nwids,sizeof(nwids),"%.16llx",nwid); if (!_db.getNetworkAndMember(nwid,identity.address().toInt(),network,member,ns)) { _sender->ncSendError(nwid,requestPacketId,identity.address(),NetworkController::NC_ERROR_OBJECT_NOT_FOUND); return; diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp index e0dd1742..acf23700 100644 --- a/controller/JSONDB.cpp +++ b/controller/JSONDB.cpp @@ -94,7 +94,7 @@ bool JSONDB::writeRaw(const std::string &n,const std::string &obj) std::string body; std::map reqHeaders; char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%lu",(unsigned long)obj.length()); + Utils::ztsnprintf(tmp,sizeof(tmp),"%lu",(unsigned long)obj.length()); reqHeaders["Content-Length"] = tmp; reqHeaders["Content-Type"] = "application/json"; const unsigned int sc = Http::PUT(0,ZT_JSONDB_HTTP_TIMEOUT,reinterpret_cast(&_httpAddr),(_basePath+"/"+n).c_str(),reqHeaders,obj.data(),(unsigned long)obj.length(),headers,body); @@ -164,7 +164,7 @@ bool JSONDB::getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlo void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig) { char n[64]; - Utils::snprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); + Utils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); writeRaw(n,OSUtils::jsonDump(networkConfig)); { Mutex::Lock _l(_networks_m); @@ -176,7 +176,7 @@ void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkC void JSONDB::saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig) { char n[256]; - Utils::snprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); + Utils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); writeRaw(n,OSUtils::jsonDump(memberConfig)); { Mutex::Lock _l(_networks_m); @@ -202,7 +202,7 @@ nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId) } char n[256]; - Utils::snprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); + Utils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); if (_httpAddr) { // Deletion is currently done by Central in harnessed mode @@ -229,7 +229,7 @@ nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId) nlohmann::json JSONDB::eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo) { char n[256]; - Utils::snprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); + Utils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); if (_httpAddr) { // Deletion is currently done by the caller in Central harnessed mode diff --git a/node/Address.hpp b/node/Address.hpp index 9d2d1734..98e32858 100644 --- a/node/Address.hpp +++ b/node/Address.hpp @@ -144,7 +144,7 @@ public: inline std::string toString() const { char buf[16]; - Utils::snprintf(buf,sizeof(buf),"%.10llx",(unsigned long long)_a); + Utils::ztsnprintf(buf,sizeof(buf),"%.10llx",(unsigned long long)_a); return std::string(buf); }; @@ -154,7 +154,7 @@ public: */ inline void toString(char *buf,unsigned int len) const { - Utils::snprintf(buf,len,"%.10llx",(unsigned long long)_a); + Utils::ztsnprintf(buf,len,"%.10llx",(unsigned long long)_a); } /** diff --git a/node/Dictionary.hpp b/node/Dictionary.hpp index 4413d628..0b000f13 100644 --- a/node/Dictionary.hpp +++ b/node/Dictionary.hpp @@ -391,7 +391,7 @@ public: inline bool add(const char *key,uint64_t value) { char tmp[32]; - Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value); + Utils::ztsnprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value); return this->add(key,tmp,-1); } @@ -401,7 +401,7 @@ public: inline bool add(const char *key,const Address &a) { char tmp[32]; - Utils::snprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt()); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt()); return this->add(key,tmp,-1); } diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp index 0fbb2d68..17d7c72e 100644 --- a/node/InetAddress.cpp +++ b/node/InetAddress.cpp @@ -152,7 +152,7 @@ std::string InetAddress::toString() const char buf[128]; switch(ss_family) { case AF_INET: - Utils::snprintf(buf,sizeof(buf),"%d.%d.%d.%d/%d", + Utils::ztsnprintf(buf,sizeof(buf),"%d.%d.%d.%d/%d", (int)(reinterpret_cast(&(reinterpret_cast(this)->sin_addr.s_addr)))[0], (int)(reinterpret_cast(&(reinterpret_cast(this)->sin_addr.s_addr)))[1], (int)(reinterpret_cast(&(reinterpret_cast(this)->sin_addr.s_addr)))[2], @@ -161,7 +161,7 @@ std::string InetAddress::toString() const ); return std::string(buf); case AF_INET6: - Utils::snprintf(buf,sizeof(buf),"%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x/%d", + Utils::ztsnprintf(buf,sizeof(buf),"%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x/%d", (int)(reinterpret_cast(this)->sin6_addr.s6_addr[0]), (int)(reinterpret_cast(this)->sin6_addr.s6_addr[1]), (int)(reinterpret_cast(this)->sin6_addr.s6_addr[2]), @@ -190,7 +190,7 @@ std::string InetAddress::toIpString() const char buf[128]; switch(ss_family) { case AF_INET: - Utils::snprintf(buf,sizeof(buf),"%d.%d.%d.%d", + Utils::ztsnprintf(buf,sizeof(buf),"%d.%d.%d.%d", (int)(reinterpret_cast(&(reinterpret_cast(this)->sin_addr.s_addr)))[0], (int)(reinterpret_cast(&(reinterpret_cast(this)->sin_addr.s_addr)))[1], (int)(reinterpret_cast(&(reinterpret_cast(this)->sin_addr.s_addr)))[2], @@ -198,7 +198,7 @@ std::string InetAddress::toIpString() const ); return std::string(buf); case AF_INET6: - Utils::snprintf(buf,sizeof(buf),"%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x", + Utils::ztsnprintf(buf,sizeof(buf),"%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x:%.2x%.2x", (int)(reinterpret_cast(this)->sin6_addr.s6_addr[0]), (int)(reinterpret_cast(this)->sin6_addr.s6_addr[1]), (int)(reinterpret_cast(this)->sin6_addr.s6_addr[2]), diff --git a/node/MAC.hpp b/node/MAC.hpp index e7717d99..db50aeb1 100644 --- a/node/MAC.hpp +++ b/node/MAC.hpp @@ -178,7 +178,7 @@ public: */ inline void toString(char *buf,unsigned int len) const { - Utils::snprintf(buf,len,"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)(*this)[0],(int)(*this)[1],(int)(*this)[2],(int)(*this)[3],(int)(*this)[4],(int)(*this)[5]); + Utils::ztsnprintf(buf,len,"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)(*this)[0],(int)(*this)[1],(int)(*this)[2],(int)(*this)[3],(int)(*this)[4],(int)(*this)[5]); } /** diff --git a/node/MulticastGroup.hpp b/node/MulticastGroup.hpp index 4240db67..7cbec2e0 100644 --- a/node/MulticastGroup.hpp +++ b/node/MulticastGroup.hpp @@ -100,7 +100,7 @@ public: inline std::string toString() const { char buf[64]; - Utils::snprintf(buf,sizeof(buf),"%.2x%.2x%.2x%.2x%.2x%.2x/%.8lx",(unsigned int)_mac[0],(unsigned int)_mac[1],(unsigned int)_mac[2],(unsigned int)_mac[3],(unsigned int)_mac[4],(unsigned int)_mac[5],(unsigned long)_adi); + Utils::ztsnprintf(buf,sizeof(buf),"%.2x%.2x%.2x%.2x%.2x%.2x/%.8lx",(unsigned int)_mac[0],(unsigned int)_mac[1],(unsigned int)_mac[2],(unsigned int)_mac[3],(unsigned int)_mac[4],(unsigned int)_mac[5],(unsigned long)_adi); return std::string(buf); } diff --git a/node/Network.cpp b/node/Network.cpp index 12deeeb7..8c6f2ce8 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -51,7 +51,7 @@ namespace ZeroTier { namespace { #ifdef ZT_RULES_ENGINE_DEBUGGING -#define FILTER_TRACE(f,...) { Utils::snprintf(dpbuf,sizeof(dpbuf),f,##__VA_ARGS__); dlog.push_back(std::string(dpbuf)); } +#define FILTER_TRACE(f,...) { Utils::ztsnprintf(dpbuf,sizeof(dpbuf),f,##__VA_ARGS__); dlog.push_back(std::string(dpbuf)); } static const char *_rtn(const ZT_VirtualNetworkRuleType rt) { switch(rt) { @@ -1261,7 +1261,7 @@ void Network::requestConfiguration(void *tPtr) nconf->rules[13].t = (uint8_t)ZT_NETWORK_RULE_ACTION_DROP; nconf->type = ZT_NETWORK_TYPE_PUBLIC; - Utils::snprintf(nconf->name,sizeof(nconf->name),"adhoc-%.04x-%.04x",(int)startPortRange,(int)endPortRange); + Utils::ztsnprintf(nconf->name,sizeof(nconf->name),"adhoc-%.04x-%.04x",(int)startPortRange,(int)endPortRange); this->setConfiguration(tPtr,*nconf,false); delete nconf; diff --git a/node/NetworkConfig.cpp b/node/NetworkConfig.cpp index c39f6cab..65101c3a 100644 --- a/node/NetworkConfig.cpp +++ b/node/NetworkConfig.cpp @@ -94,7 +94,7 @@ bool NetworkConfig::toDictionary(Dictionary &d,b if (ets.length() > 0) ets.push_back(','); char tmp2[16]; - Utils::snprintf(tmp2,sizeof(tmp2),"%x",et); + Utils::ztsnprintf(tmp2,sizeof(tmp2),"%x",et); ets.append(tmp2); } et = 0; diff --git a/node/Node.cpp b/node/Node.cpp index 39325b65..ab49e63b 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -742,7 +742,7 @@ void Node::postTrace(const char *module,unsigned int line,const char *fmt,...) va_end(ap); tmp2[sizeof(tmp2)-1] = (char)0; - Utils::snprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2); + Utils::ztsnprintf(tmp1,sizeof(tmp1),"[%s] %s:%u %s",nowstr,module,line,tmp2); postEvent((void *)0,ZT_EVENT_TRACE,tmp1); } #endif // ZT_TRACE diff --git a/node/Utils.cpp b/node/Utils.cpp index d69e5335..d2321e16 100644 --- a/node/Utils.cpp +++ b/node/Utils.cpp @@ -244,8 +244,7 @@ bool Utils::scopy(char *dest,unsigned int len,const char *src) return true; } -unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...) - throw(std::length_error) +unsigned int Utils::ztsnprintf(char *buf,unsigned int len,const char *fmt,...) { va_list ap; @@ -256,7 +255,7 @@ unsigned int Utils::snprintf(char *buf,unsigned int len,const char *fmt,...) if ((n >= (int)len)||(n < 0)) { if (len) buf[len - 1] = (char)0; - throw std::length_error("buf[] overflow in Utils::snprintf"); + throw std::length_error("buf[] overflow"); } return (unsigned int)n; diff --git a/node/Utils.hpp b/node/Utils.hpp index 25a90055..212ef247 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -244,8 +244,7 @@ public: * @param ... Format arguments * @throws std::length_error buf[] too short (buf[] will still be left null-terminated) */ - static unsigned int snprintf(char *buf,unsigned int len,const char *fmt,...) - throw(std::length_error); + static unsigned int ztsnprintf(char *buf,unsigned int len,const char *fmt,...); /** * Count the number of bits set in an integer diff --git a/one.cpp b/one.cpp index 93504cfb..cbf09121 100644 --- a/one.cpp +++ b/one.cpp @@ -260,9 +260,9 @@ static int cli(int argc,char **argv) if (hd) { char p[4096]; #ifdef __APPLE__ - Utils::snprintf(p,sizeof(p),"%s/Library/Application Support/ZeroTier/One/authtoken.secret",hd); + Utils::ztsnprintf(p,sizeof(p),"%s/Library/Application Support/ZeroTier/One/authtoken.secret",hd); #else - Utils::snprintf(p,sizeof(p),"%s/.zeroTierOneAuthToken",hd); + Utils::ztsnprintf(p,sizeof(p),"%s/.zeroTierOneAuthToken",hd); #endif OSUtils::readFile(p,authToken); } @@ -278,7 +278,7 @@ static int cli(int argc,char **argv) InetAddress addr; { char addrtmp[256]; - Utils::snprintf(addrtmp,sizeof(addrtmp),"%s/%u",ip.c_str(),port); + Utils::ztsnprintf(addrtmp,sizeof(addrtmp),"%s/%u",ip.c_str(),port); addr = InetAddress(addrtmp); } @@ -366,7 +366,7 @@ static int cli(int argc,char **argv) std::string addr = path["address"]; const uint64_t now = OSUtils::now(); const double lq = (path.count("linkQuality")) ? (double)path["linkQuality"] : -1.0; - Utils::snprintf(tmp,sizeof(tmp),"%s;%llu;%llu;%1.2f",addr.c_str(),now - (uint64_t)path["lastSend"],now - (uint64_t)path["lastReceive"],lq); + Utils::ztsnprintf(tmp,sizeof(tmp),"%s;%llu;%llu;%1.2f",addr.c_str(),now - (uint64_t)path["lastSend"],now - (uint64_t)path["lastReceive"],lq); bestPath = tmp; break; } @@ -378,7 +378,7 @@ static int cli(int argc,char **argv) int64_t vmin = p["versionMinor"]; int64_t vrev = p["versionRev"]; if (vmaj >= 0) { - Utils::snprintf(ver,sizeof(ver),"%lld.%lld.%lld",vmaj,vmin,vrev); + Utils::ztsnprintf(ver,sizeof(ver),"%lld.%lld.%lld",vmaj,vmin,vrev); } else { ver[0] = '-'; ver[1] = (char)0; @@ -527,9 +527,9 @@ static int cli(int argc,char **argv) const uint64_t seed = Utils::hexStrToU64(arg2.c_str()); if ((worldId)&&(seed)) { char jsons[1024]; - Utils::snprintf(jsons,sizeof(jsons),"{\"seed\":\"%s\"}",arg2.c_str()); + Utils::ztsnprintf(jsons,sizeof(jsons),"{\"seed\":\"%s\"}",arg2.c_str()); char cl[128]; - Utils::snprintf(cl,sizeof(cl),"%u",(unsigned int)strlen(jsons)); + Utils::ztsnprintf(cl,sizeof(cl),"%u",(unsigned int)strlen(jsons)); requestHeaders["Content-Type"] = "application/json"; requestHeaders["Content-Length"] = cl; unsigned int scode = Http::POST( @@ -579,11 +579,11 @@ static int cli(int argc,char **argv) if (eqidx != std::string::npos) { if ((arg2.substr(0,eqidx) == "allowManaged")||(arg2.substr(0,eqidx) == "allowGlobal")||(arg2.substr(0,eqidx) == "allowDefault")) { char jsons[1024]; - Utils::snprintf(jsons,sizeof(jsons),"{\"%s\":%s}", + Utils::ztsnprintf(jsons,sizeof(jsons),"{\"%s\":%s}", arg2.substr(0,eqidx).c_str(), (((arg2.substr(eqidx,2) == "=t")||(arg2.substr(eqidx,2) == "=1")) ? "true" : "false")); char cl[128]; - Utils::snprintf(cl,sizeof(cl),"%u",(unsigned int)strlen(jsons)); + Utils::ztsnprintf(cl,sizeof(cl),"%u",(unsigned int)strlen(jsons)); requestHeaders["Content-Type"] = "application/json"; requestHeaders["Content-Length"] = cl; unsigned int scode = Http::POST( @@ -864,7 +864,7 @@ static int idtool(int argc,char **argv) Buffer wbuf; w.serialize(wbuf); char fn[128]; - Utils::snprintf(fn,sizeof(fn),"%.16llx.moon",w.id()); + Utils::ztsnprintf(fn,sizeof(fn),"%.16llx.moon",w.id()); OSUtils::writeFile(fn,wbuf.data(),wbuf.size()); printf("wrote %s (signed world with timestamp %llu)" ZT_EOL_S,fn,(unsigned long long)now); } diff --git a/osdep/BSDEthernetTap.cpp b/osdep/BSDEthernetTap.cpp index 5bb5fbd1..f07f9e5a 100644 --- a/osdep/BSDEthernetTap.cpp +++ b/osdep/BSDEthernetTap.cpp @@ -114,8 +114,8 @@ BSDEthernetTap::BSDEthernetTap( std::vector devFiles(OSUtils::listDirectory("/dev")); for(int i=9993;i<(9993+128);++i) { - Utils::snprintf(tmpdevname,sizeof(tmpdevname),"tap%d",i); - Utils::snprintf(devpath,sizeof(devpath),"/dev/%s",tmpdevname); + Utils::ztsnprintf(tmpdevname,sizeof(tmpdevname),"tap%d",i); + Utils::ztsnprintf(devpath,sizeof(devpath),"/dev/%s",tmpdevname); if (std::find(devFiles.begin(),devFiles.end(),std::string(tmpdevname)) == devFiles.end()) { long cpid = (long)vfork(); if (cpid == 0) { @@ -152,8 +152,8 @@ BSDEthernetTap::BSDEthernetTap( /* Other BSDs like OpenBSD only have a limited number of tap devices that cannot be renamed */ for(int i=0;i<64;++i) { - Utils::snprintf(tmpdevname,sizeof(tmpdevname),"tap%d",i); - Utils::snprintf(devpath,sizeof(devpath),"/dev/%s",tmpdevname); + Utils::ztsnprintf(tmpdevname,sizeof(tmpdevname),"tap%d",i); + Utils::ztsnprintf(devpath,sizeof(devpath),"/dev/%s",tmpdevname); _fd = ::open(devpath,O_RDWR); if (_fd > 0) { _dev = tmpdevname; @@ -171,9 +171,9 @@ BSDEthernetTap::BSDEthernetTap( } // Configure MAC address and MTU, bring interface up - Utils::snprintf(ethaddr,sizeof(ethaddr),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]); - Utils::snprintf(mtustr,sizeof(mtustr),"%u",_mtu); - Utils::snprintf(metstr,sizeof(metstr),"%u",_metric); + Utils::ztsnprintf(ethaddr,sizeof(ethaddr),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]); + Utils::ztsnprintf(mtustr,sizeof(mtustr),"%u",_mtu); + Utils::ztsnprintf(metstr,sizeof(metstr),"%u",_metric); long cpid = (long)vfork(); if (cpid == 0) { ::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"lladdr",ethaddr,"mtu",mtustr,"metric",metstr,"up",(const char *)0); @@ -385,7 +385,7 @@ void BSDEthernetTap::setMtu(unsigned int mtu) long cpid = (long)vfork(); if (cpid == 0) { char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%u",mtu); + Utils::ztsnprintf(tmp,sizeof(tmp),"%u",mtu); execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"mtu",tmp,(const char *)0); _exit(-1); } else if (cpid > 0) { diff --git a/osdep/Http.cpp b/osdep/Http.cpp index f1d3bfe2..3c556f44 100644 --- a/osdep/Http.cpp +++ b/osdep/Http.cpp @@ -244,10 +244,10 @@ unsigned int Http::_do( try { char tmp[1024]; - Utils::snprintf(tmp,sizeof(tmp),"%s %s HTTP/1.1\r\n",method,path); + Utils::ztsnprintf(tmp,sizeof(tmp),"%s %s HTTP/1.1\r\n",method,path); handler.writeBuf.append(tmp); for(std::map::const_iterator h(requestHeaders.begin());h!=requestHeaders.end();++h) { - Utils::snprintf(tmp,sizeof(tmp),"%s: %s\r\n",h->first.c_str(),h->second.c_str()); + Utils::ztsnprintf(tmp,sizeof(tmp),"%s: %s\r\n",h->first.c_str(),h->second.c_str()); handler.writeBuf.append(tmp); } handler.writeBuf.append("\r\n"); diff --git a/osdep/LinuxEthernetTap.cpp b/osdep/LinuxEthernetTap.cpp index ccaa92ef..fc5199f1 100644 --- a/osdep/LinuxEthernetTap.cpp +++ b/osdep/LinuxEthernetTap.cpp @@ -97,7 +97,7 @@ LinuxEthernetTap::LinuxEthernetTap( char procpath[128],nwids[32]; struct stat sbuf; - Utils::snprintf(nwids,sizeof(nwids),"%.16llx",nwid); + Utils::ztsnprintf(nwids,sizeof(nwids),"%.16llx",nwid); Mutex::Lock _l(__tapCreateLock); // create only one tap at a time, globally @@ -134,7 +134,7 @@ LinuxEthernetTap::LinuxEthernetTap( std::map::const_iterator gdmEntry = globalDeviceMap.find(nwids); if (gdmEntry != globalDeviceMap.end()) { Utils::scopy(ifr.ifr_name,sizeof(ifr.ifr_name),gdmEntry->second.c_str()); - Utils::snprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name); + Utils::ztsnprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name); recalledDevice = (stat(procpath,&sbuf) != 0); } @@ -142,8 +142,8 @@ LinuxEthernetTap::LinuxEthernetTap( #ifdef __SYNOLOGY__ int devno = 50; do { - Utils::snprintf(ifr.ifr_name,sizeof(ifr.ifr_name),"eth%d",devno++); - Utils::snprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name); + Utils::ztsnprintf(ifr.ifr_name,sizeof(ifr.ifr_name),"eth%d",devno++); + Utils::ztsnprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name); } while (stat(procpath,&sbuf) == 0); // try zt#++ until we find one that does not exist #else char devno = 0; @@ -158,7 +158,7 @@ LinuxEthernetTap::LinuxEthernetTap( _base32_5_to_8(reinterpret_cast(tmp2) + 5,tmp3 + 10); tmp3[15] = (char)0; memcpy(ifr.ifr_name,tmp3,16); - Utils::snprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name); + Utils::ztsnprintf(procpath,sizeof(procpath),"/proc/sys/net/ipv4/conf/%s",ifr.ifr_name); } while (stat(procpath,&sbuf) == 0); #endif } diff --git a/osdep/OSUtils.cpp b/osdep/OSUtils.cpp index 53e8bb97..bf0dc04d 100644 --- a/osdep/OSUtils.cpp +++ b/osdep/OSUtils.cpp @@ -134,7 +134,7 @@ long OSUtils::cleanDirectory(const char *path,const uint64_t olderThan) if (date.QuadPart > 0) { date.QuadPart -= adjust.QuadPart; if ((uint64_t)((date.QuadPart / 10000000) * 1000) < olderThan) { - Utils::snprintf(tmp, sizeof(tmp), "%s\\%s", path, ffd.cFileName); + Utils::ztsnprintf(tmp, sizeof(tmp), "%s\\%s", path, ffd.cFileName); if (DeleteFileA(tmp)) ++cleaned; } @@ -157,7 +157,7 @@ long OSUtils::cleanDirectory(const char *path,const uint64_t olderThan) break; if (dptr) { if ((strcmp(dptr->d_name,"."))&&(strcmp(dptr->d_name,".."))&&(dptr->d_type == DT_REG)) { - Utils::snprintf(tmp,sizeof(tmp),"%s/%s",path,dptr->d_name); + Utils::ztsnprintf(tmp,sizeof(tmp),"%s/%s",path,dptr->d_name); if (stat(tmp,&st) == 0) { uint64_t mt = (uint64_t)(st.st_mtime); if ((mt > 0)&&((mt * 1000) < olderThan)) { @@ -446,7 +446,7 @@ std::string OSUtils::jsonString(const nlohmann::json &jv,const char *dfl) return jv; } else if (jv.is_number()) { char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%llu",(uint64_t)jv); + Utils::ztsnprintf(tmp,sizeof(tmp),"%llu",(uint64_t)jv); return tmp; } else if (jv.is_boolean()) { return ((bool)jv ? std::string("1") : std::string("0")); diff --git a/osdep/OSXEthernetTap.cpp b/osdep/OSXEthernetTap.cpp index f5e1c43f..e082408e 100644 --- a/osdep/OSXEthernetTap.cpp +++ b/osdep/OSXEthernetTap.cpp @@ -336,7 +336,7 @@ OSXEthernetTap::OSXEthernetTap( char devpath[64],ethaddr[64],mtustr[32],metstr[32],nwids[32]; struct stat stattmp; - Utils::snprintf(nwids,sizeof(nwids),"%.16llx",nwid); + Utils::ztsnprintf(nwids,sizeof(nwids),"%.16llx",nwid); Mutex::Lock _gl(globalTapCreateLock); @@ -391,13 +391,13 @@ OSXEthernetTap::OSXEthernetTap( // Open the first unused tap device if we didn't recall a previous one. if (!recalledDevice) { for(int i=0;i<64;++i) { - Utils::snprintf(devpath,sizeof(devpath),"/dev/zt%d",i); + Utils::ztsnprintf(devpath,sizeof(devpath),"/dev/zt%d",i); if (stat(devpath,&stattmp)) throw std::runtime_error("no more TAP devices available"); _fd = ::open(devpath,O_RDWR); if (_fd > 0) { char foo[16]; - Utils::snprintf(foo,sizeof(foo),"zt%d",i); + Utils::ztsnprintf(foo,sizeof(foo),"zt%d",i); _dev = foo; break; } @@ -413,9 +413,9 @@ OSXEthernetTap::OSXEthernetTap( } // Configure MAC address and MTU, bring interface up - Utils::snprintf(ethaddr,sizeof(ethaddr),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]); - Utils::snprintf(mtustr,sizeof(mtustr),"%u",_mtu); - Utils::snprintf(metstr,sizeof(metstr),"%u",_metric); + Utils::ztsnprintf(ethaddr,sizeof(ethaddr),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(int)mac[0],(int)mac[1],(int)mac[2],(int)mac[3],(int)mac[4],(int)mac[5]); + Utils::ztsnprintf(mtustr,sizeof(mtustr),"%u",_mtu); + Utils::ztsnprintf(metstr,sizeof(metstr),"%u",_metric); long cpid = (long)vfork(); if (cpid == 0) { ::execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"lladdr",ethaddr,"mtu",mtustr,"metric",metstr,"up",(const char *)0); @@ -636,7 +636,7 @@ void OSXEthernetTap::setMtu(unsigned int mtu) long cpid = (long)vfork(); if (cpid == 0) { char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%u",mtu); + Utils::ztsnprintf(tmp,sizeof(tmp),"%u",mtu); execl("/sbin/ifconfig","/sbin/ifconfig",_dev.c_str(),"mtu",tmp,(const char *)0); _exit(-1); } else if (cpid > 0) { diff --git a/osdep/PortMapper.cpp b/osdep/PortMapper.cpp index 99286172..df868e7a 100644 --- a/osdep/PortMapper.cpp +++ b/osdep/PortMapper.cpp @@ -205,7 +205,7 @@ public: memset(externalip,0,sizeof(externalip)); memset(&urls,0,sizeof(urls)); memset(&data,0,sizeof(data)); - Utils::snprintf(inport,sizeof(inport),"%d",localPort); + Utils::ztsnprintf(inport,sizeof(inport),"%d",localPort); if ((UPNP_GetValidIGD(devlist,&urls,&data,lanaddr,sizeof(lanaddr)))&&(lanaddr[0])) { #ifdef ZT_PORTMAPPER_TRACE @@ -220,7 +220,7 @@ public: int tryPort = (int)localPort + tries; if (tryPort >= 65535) tryPort = (tryPort - 65535) + 1025; - Utils::snprintf(outport,sizeof(outport),"%u",tryPort); + Utils::ztsnprintf(outport,sizeof(outport),"%u",tryPort); // First check and see if this port is already mapped to the // same unique name. If so, keep this mapping and don't try diff --git a/osdep/WindowsEthernetTap.cpp b/osdep/WindowsEthernetTap.cpp index c5d82d8e..b96ad791 100644 --- a/osdep/WindowsEthernetTap.cpp +++ b/osdep/WindowsEthernetTap.cpp @@ -484,7 +484,7 @@ WindowsEthernetTap::WindowsEthernetTap( char tag[24]; // We "tag" registry entries with the network ID to identify persistent devices - Utils::snprintf(tag,sizeof(tag),"%.16llx",(unsigned long long)nwid); + Utils::ztsnprintf(tag,sizeof(tag),"%.16llx",(unsigned long long)nwid); Mutex::Lock _l(_systemTapInitLock); @@ -601,10 +601,10 @@ WindowsEthernetTap::WindowsEthernetTap( if (_netCfgInstanceId.length() > 0) { char tmps[64]; - unsigned int tmpsl = Utils::snprintf(tmps,sizeof(tmps),"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",(unsigned int)mac[0],(unsigned int)mac[1],(unsigned int)mac[2],(unsigned int)mac[3],(unsigned int)mac[4],(unsigned int)mac[5]) + 1; + unsigned int tmpsl = Utils::ztsnprintf(tmps,sizeof(tmps),"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",(unsigned int)mac[0],(unsigned int)mac[1],(unsigned int)mac[2],(unsigned int)mac[3],(unsigned int)mac[4],(unsigned int)mac[5]) + 1; RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"NetworkAddress",REG_SZ,tmps,tmpsl); RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"MAC",REG_SZ,tmps,tmpsl); - tmpsl = Utils::snprintf(tmps, sizeof(tmps), "%d", mtu); + tmpsl = Utils::ztsnprintf(tmps, sizeof(tmps), "%d", mtu); RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"MTU",REG_SZ,tmps,tmpsl); DWORD tmp = 0; @@ -879,7 +879,7 @@ void WindowsEthernetTap::setMtu(unsigned int mtu) HKEY nwAdapters; if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ | KEY_WRITE, &nwAdapters) == ERROR_SUCCESS) { char tmps[64]; - unsigned int tmpsl = Utils::snprintf(tmps, sizeof(tmps), "%d", mtu); + unsigned int tmpsl = Utils::ztsnprintf(tmps, sizeof(tmps), "%d", mtu); RegSetKeyValueA(nwAdapters, _mySubkeyName.c_str(), "MTU", REG_SZ, tmps, tmpsl); RegCloseKey(nwAdapters); } @@ -902,7 +902,7 @@ void WindowsEthernetTap::threadMain() HANDLE wait4[3]; OVERLAPPED tapOvlRead,tapOvlWrite; - Utils::snprintf(tapPath,sizeof(tapPath),"\\\\.\\Global\\%s.tap",_netCfgInstanceId.c_str()); + Utils::ztsnprintf(tapPath,sizeof(tapPath),"\\\\.\\Global\\%s.tap",_netCfgInstanceId.c_str()); try { while (_run) { diff --git a/selftest.cpp b/selftest.cpp index 8175d708..ff171aa3 100644 --- a/selftest.cpp +++ b/selftest.cpp @@ -831,7 +831,7 @@ static int testOther() memset(key, 0, sizeof(key)); memset(value, 0, sizeof(value)); for(unsigned int q=0;q<32;++q) { - Utils::snprintf(key[q],16,"%.8lx",(unsigned long)(rand() % 1000) + (q * 1000)); + Utils::ztsnprintf(key[q],16,"%.8lx",(unsigned long)(rand() % 1000) + (q * 1000)); int r = rand() % 128; for(int x=0;x lines(OSUtils::split(cf.c_str(),"\r\n","","")); for(std::vector::iterator l(lines.begin());l!=lines.end();++l) { diff --git a/service/OneService.cpp b/service/OneService.cpp index 644454bc..993fb116 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -210,10 +210,10 @@ static void _networkToJson(nlohmann::json &nj,const ZT_VirtualNetworkConfig *nc, case ZT_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break; } - Utils::snprintf(tmp,sizeof(tmp),"%.16llx",nc->nwid); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",nc->nwid); nj["id"] = tmp; nj["nwid"] = tmp; - Utils::snprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff)); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff)); nj["mac"] = tmp; nj["name"] = nc->name; nj["status"] = nstatus; @@ -260,12 +260,12 @@ static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer) case ZT_PEER_ROLE_PLANET: prole = "PLANET"; break; } - Utils::snprintf(tmp,sizeof(tmp),"%.10llx",peer->address); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",peer->address); pj["address"] = tmp; pj["versionMajor"] = peer->versionMajor; pj["versionMinor"] = peer->versionMinor; pj["versionRev"] = peer->versionRev; - Utils::snprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev); + Utils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",peer->versionMajor,peer->versionMinor,peer->versionRev); pj["version"] = tmp; pj["latency"] = peer->latency; pj["role"] = prole; @@ -289,7 +289,7 @@ static void _peerToJson(nlohmann::json &pj,const ZT_Peer *peer) static void _moonToJson(nlohmann::json &mj,const World &world) { char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%.16llx",world.id()); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",world.id()); mj["id"] = tmp; mj["timestamp"] = world.timestamp(); mj["signature"] = Utils::hex(world.signature().data,(unsigned int)world.signature().size()); @@ -687,7 +687,7 @@ public: // Save primary port to a file so CLIs and GUIs can learn it easily char portstr[64]; - Utils::snprintf(portstr,sizeof(portstr),"%u",_ports[0]); + Utils::ztsnprintf(portstr,sizeof(portstr),"%u",_ports[0]); OSUtils::writeFile((_homePath + ZT_PATH_SEPARATOR_S "zerotier-one.port").c_str(),std::string(portstr)); // Attempt to bind to a secondary port chosen from our ZeroTier address. @@ -725,7 +725,7 @@ public: } if (_ports[2]) { char uniqueName[64]; - Utils::snprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]); + Utils::ztsnprintf(uniqueName,sizeof(uniqueName),"ZeroTier/%.10llx@%u",_node->address(),_ports[2]); _portMapper = new PortMapper(_ports[2],uniqueName); } } @@ -1069,7 +1069,7 @@ public: n->second.settings = settings; char nlcpath[4096]; - Utils::snprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_networksPath.c_str(),nwid); + Utils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_networksPath.c_str(),nwid); FILE *out = fopen(nlcpath,"w"); if (out) { fprintf(out,"allowManaged=%d\n",(int)n->second.settings.allowManaged); @@ -1188,7 +1188,7 @@ public: ZT_NodeStatus status; _node->status(&status); - Utils::snprintf(tmp,sizeof(tmp),"%.10llx",status.address); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.10llx",status.address); res["address"] = tmp; res["publicIdentity"] = status.publicIdentity; res["online"] = (bool)(status.online != 0); @@ -1197,7 +1197,7 @@ public: res["versionMinor"] = ZEROTIER_ONE_VERSION_MINOR; res["versionRev"] = ZEROTIER_ONE_VERSION_REVISION; res["versionBuild"] = ZEROTIER_ONE_VERSION_BUILD; - Utils::snprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION); + Utils::ztsnprintf(tmp,sizeof(tmp),"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION); res["version"] = tmp; res["clock"] = OSUtils::now(); @@ -1373,7 +1373,7 @@ public: if ((scode != 200)&&(seed != 0)) { char tmp[64]; - Utils::snprintf(tmp,sizeof(tmp),"%.16llx",id); + Utils::ztsnprintf(tmp,sizeof(tmp),"%.16llx",id); res["id"] = tmp; res["roots"] = json::array(); res["timestamp"] = 0; @@ -1617,7 +1617,7 @@ public: std::string h = controllerDbHttpHost; _controllerDbPath.append(h); char dbp[128]; - Utils::snprintf(dbp,sizeof(dbp),"%d",(int)controllerDbHttpPort); + Utils::ztsnprintf(dbp,sizeof(dbp),"%d",(int)controllerDbHttpPort); _controllerDbPath.push_back(':'); _controllerDbPath.append(dbp); if (controllerDbHttpPath.is_string()) { @@ -1711,7 +1711,7 @@ public: if (syncRoutes) { char tapdev[64]; #ifdef __WINDOWS__ - Utils::snprintf(tapdev,sizeof(tapdev),"%.16llx",(unsigned long long)n.tap->luid().Value); + Utils::ztsnprintf(tapdev,sizeof(tapdev),"%.16llx",(unsigned long long)n.tap->luid().Value); #else Utils::scopy(tapdev,sizeof(tapdev),n.tap->deviceName().c_str()); #endif @@ -1933,24 +1933,24 @@ public: bool secure = false; switch(type) { case ZT_STATE_OBJECT_IDENTITY_PUBLIC: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str()); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str()); break; case ZT_STATE_OBJECT_IDENTITY_SECRET: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str()); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str()); secure = true; break; case ZT_STATE_OBJECT_PEER_IDENTITY: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "iddb.d/%.10llx",_homePath.c_str(),(unsigned long long)id); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "iddb.d/%.10llx",_homePath.c_str(),(unsigned long long)id); break; case ZT_STATE_OBJECT_NETWORK_CONFIG: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d/%.16llx.conf",_homePath.c_str(),(unsigned long long)id); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d/%.16llx.conf",_homePath.c_str(),(unsigned long long)id); secure = true; break; case ZT_STATE_OBJECT_PLANET: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str()); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str()); break; case ZT_STATE_OBJECT_MOON: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "moons.d/%.16llx.moon",_homePath.c_str(),(unsigned long long)id); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "moons.d/%.16llx.moon",_homePath.c_str(),(unsigned long long)id); break; default: p[0] = (char)0; @@ -2022,7 +2022,7 @@ public: &_nextBackgroundTaskDeadline); if (ZT_ResultCode_isFatal(rc)) { char tmp[256]; - Utils::snprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc); + Utils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc); Mutex::Lock _l(_termReason_m); _termReason = ONE_UNRECOVERABLE_ERROR; _fatalErrorMessage = tmp; @@ -2235,7 +2235,7 @@ public: &_nextBackgroundTaskDeadline); if (ZT_ResultCode_isFatal(rc)) { char tmp[256]; - Utils::snprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc); + Utils::ztsnprintf(tmp,sizeof(tmp),"fatal error code from processWirePacket: %d",(int)rc); Mutex::Lock _l(_termReason_m); _termReason = ONE_UNRECOVERABLE_ERROR; _fatalErrorMessage = tmp; @@ -2402,7 +2402,7 @@ public: if (!n.tap) { try { char friendlyName[128]; - Utils::snprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid); + Utils::ztsnprintf(friendlyName,sizeof(friendlyName),"ZeroTier One [%.16llx]",nwid); n.tap = new EthernetTap( _homePath.c_str(), @@ -2416,7 +2416,7 @@ public: *nuptr = (void *)&n; char nlcpath[256]; - Utils::snprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid); + Utils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid); std::string nlcbuf; if (OSUtils::readFile(nlcpath,nlcbuf)) { Dictionary<4096> nc; @@ -2502,7 +2502,7 @@ public: #endif if (op == ZT_VIRTUAL_NETWORK_CONFIG_OPERATION_DESTROY) { char nlcpath[256]; - Utils::snprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid); + Utils::ztsnprintf(nlcpath,sizeof(nlcpath),"%s" ZT_PATH_SEPARATOR_S "networks.d" ZT_PATH_SEPARATOR_S "%.16llx.local.conf",_homePath.c_str(),nwid); OSUtils::rm(nlcpath); } } else { @@ -2554,22 +2554,22 @@ public: char p[4096]; switch(type) { case ZT_STATE_OBJECT_IDENTITY_PUBLIC: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str()); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.public",_homePath.c_str()); break; case ZT_STATE_OBJECT_IDENTITY_SECRET: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str()); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "identity.secret",_homePath.c_str()); break; case ZT_STATE_OBJECT_PEER_IDENTITY: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "iddb.d/%.10llx",_homePath.c_str(),(unsigned long long)id); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "iddb.d/%.10llx",_homePath.c_str(),(unsigned long long)id); break; case ZT_STATE_OBJECT_NETWORK_CONFIG: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d/%.16llx.conf",_homePath.c_str(),(unsigned long long)id); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "networks.d/%.16llx.conf",_homePath.c_str(),(unsigned long long)id); break; case ZT_STATE_OBJECT_PLANET: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str()); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "planet",_homePath.c_str()); break; case ZT_STATE_OBJECT_MOON: - Utils::snprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "moons.d/%.16llx.moon",_homePath.c_str(),(unsigned long long)id); + Utils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "moons.d/%.16llx.moon",_homePath.c_str(),(unsigned long long)id); break; default: return -1; @@ -2765,7 +2765,7 @@ public: default: scodestr = "Error"; break; } - Utils::snprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nContent-Type: %s\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n", + Utils::ztsnprintf(tmpn,sizeof(tmpn),"HTTP/1.1 %.3u %s\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nContent-Type: %s\r\nContent-Length: %lu\r\nConnection: close\r\n\r\n", scode, scodestr, contentType.c_str(), diff --git a/service/SoftwareUpdater.cpp b/service/SoftwareUpdater.cpp index d94beab5..e0519827 100644 --- a/service/SoftwareUpdater.cpp +++ b/service/SoftwareUpdater.cpp @@ -284,7 +284,7 @@ bool SoftwareUpdater::check(const uint64_t now) if ((now - _lastCheckTime) >= ZT_SOFTWARE_UPDATE_CHECK_PERIOD) { _lastCheckTime = now; char tmp[512]; - const unsigned int len = Utils::snprintf(tmp,sizeof(tmp), + const unsigned int len = Utils::ztsnprintf(tmp,sizeof(tmp), "%c{\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_MAJOR "\":%d," "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_MINOR "\":%d," "\"" ZT_SOFTWARE_UPDATE_JSON_VERSION_REVISION "\":%d," -- cgit v1.2.3 From cd63ecd3f3562b4513dd907bb7f805c2af2d87d4 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 6 Jul 2017 11:45:48 -0700 Subject: . --- attic/ClusterDefinition.hpp | 168 ++++++++++++++++++++++++++++++++++++++++++ service/ClusterDefinition.hpp | 168 ------------------------------------------ 2 files changed, 168 insertions(+), 168 deletions(-) create mode 100644 attic/ClusterDefinition.hpp delete mode 100644 service/ClusterDefinition.hpp (limited to 'attic') diff --git a/attic/ClusterDefinition.hpp b/attic/ClusterDefinition.hpp new file mode 100644 index 00000000..b6317ff7 --- /dev/null +++ b/attic/ClusterDefinition.hpp @@ -0,0 +1,168 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifndef ZT_CLUSTERDEFINITION_HPP +#define ZT_CLUSTERDEFINITION_HPP + +#ifdef ZT_ENABLE_CLUSTER + +#include +#include + +#include "../node/Constants.hpp" +#include "../node/Utils.hpp" +#include "../node/NonCopyable.hpp" +#include "../osdep/OSUtils.hpp" + +#include "ClusterGeoIpService.hpp" + +namespace ZeroTier { + +/** + * Parser for cluster definition file + */ +class ClusterDefinition : NonCopyable +{ +public: + struct MemberDefinition + { + MemberDefinition() : id(0),x(0),y(0),z(0) { name[0] = (char)0; } + + unsigned int id; + int x,y,z; + char name[256]; + InetAddress clusterEndpoint; + std::vector zeroTierEndpoints; + }; + + /** + * Load and initialize cluster definition and GeoIP data if any + * + * @param myAddress My ZeroTier address + * @param pathToClusterFile Path to cluster definition file + * @throws std::runtime_error Invalid cluster definition or unable to load data + */ + ClusterDefinition(uint64_t myAddress,const char *pathToClusterFile) + { + std::string cf; + if (!OSUtils::readFile(pathToClusterFile,cf)) + return; + + char myAddressStr[64]; + Utils::ztsnprintf(myAddressStr,sizeof(myAddressStr),"%.10llx",myAddress); + + std::vector lines(OSUtils::split(cf.c_str(),"\r\n","","")); + for(std::vector::iterator l(lines.begin());l!=lines.end();++l) { + std::vector fields(OSUtils::split(l->c_str()," \t","","")); + if ((fields.size() < 5)||(fields[0][0] == '#')||(fields[0] != myAddressStr)) + continue; + + //
geo + if (fields[1] == "geo") { + if ((fields.size() >= 7)&&(OSUtils::fileExists(fields[2].c_str()))) { + int ipStartColumn = Utils::strToInt(fields[3].c_str()); + int ipEndColumn = Utils::strToInt(fields[4].c_str()); + int latitudeColumn = Utils::strToInt(fields[5].c_str()); + int longitudeColumn = Utils::strToInt(fields[6].c_str()); + if (_geo.load(fields[2].c_str(),ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn) <= 0) + throw std::runtime_error(std::string("failed to load geo-ip data from ")+fields[2]); + } + continue; + } + + //
+ int id = Utils::strToUInt(fields[1].c_str()); + if ((id < 0)||(id > ZT_CLUSTER_MAX_MEMBERS)) + throw std::runtime_error(std::string("invalid cluster member ID: ")+fields[1]); + MemberDefinition &md = _md[id]; + + md.id = (unsigned int)id; + if (fields.size() >= 6) { + std::vector xyz(OSUtils::split(fields[5].c_str(),",","","")); + md.x = (xyz.size() > 0) ? Utils::strToInt(xyz[0].c_str()) : 0; + md.y = (xyz.size() > 1) ? Utils::strToInt(xyz[1].c_str()) : 0; + md.z = (xyz.size() > 2) ? Utils::strToInt(xyz[2].c_str()) : 0; + } + Utils::scopy(md.name,sizeof(md.name),fields[2].c_str()); + md.clusterEndpoint.fromString(fields[3]); + if (!md.clusterEndpoint) + continue; + std::vector zips(OSUtils::split(fields[4].c_str(),",","","")); + for(std::vector::iterator zip(zips.begin());zip!=zips.end();++zip) { + InetAddress i; + i.fromString(*zip); + if (i) + md.zeroTierEndpoints.push_back(i); + } + + _ids.push_back((unsigned int)id); + } + + std::sort(_ids.begin(),_ids.end()); + } + + /** + * @return All member definitions in this cluster by ID (ID is array index) + */ + inline const MemberDefinition &operator[](unsigned int id) const throw() { return _md[id]; } + + /** + * @return Number of members in this cluster + */ + inline unsigned int size() const throw() { return (unsigned int)_ids.size(); } + + /** + * @return IDs of members in this cluster sorted by ID + */ + inline const std::vector &ids() const throw() { return _ids; } + + /** + * @return GeoIP service for this cluster + */ + inline ClusterGeoIpService &geo() throw() { return _geo; } + + /** + * @return A vector (new copy) containing all cluster members + */ + inline std::vector members() const + { + std::vector m; + for(std::vector::const_iterator i(_ids.begin());i!=_ids.end();++i) + m.push_back(_md[*i]); + return m; + } + +private: + MemberDefinition _md[ZT_CLUSTER_MAX_MEMBERS]; + std::vector _ids; + ClusterGeoIpService _geo; +}; + +} // namespace ZeroTier + +#endif // ZT_ENABLE_CLUSTER + +#endif diff --git a/service/ClusterDefinition.hpp b/service/ClusterDefinition.hpp deleted file mode 100644 index b6317ff7..00000000 --- a/service/ClusterDefinition.hpp +++ /dev/null @@ -1,168 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#ifndef ZT_CLUSTERDEFINITION_HPP -#define ZT_CLUSTERDEFINITION_HPP - -#ifdef ZT_ENABLE_CLUSTER - -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Utils.hpp" -#include "../node/NonCopyable.hpp" -#include "../osdep/OSUtils.hpp" - -#include "ClusterGeoIpService.hpp" - -namespace ZeroTier { - -/** - * Parser for cluster definition file - */ -class ClusterDefinition : NonCopyable -{ -public: - struct MemberDefinition - { - MemberDefinition() : id(0),x(0),y(0),z(0) { name[0] = (char)0; } - - unsigned int id; - int x,y,z; - char name[256]; - InetAddress clusterEndpoint; - std::vector zeroTierEndpoints; - }; - - /** - * Load and initialize cluster definition and GeoIP data if any - * - * @param myAddress My ZeroTier address - * @param pathToClusterFile Path to cluster definition file - * @throws std::runtime_error Invalid cluster definition or unable to load data - */ - ClusterDefinition(uint64_t myAddress,const char *pathToClusterFile) - { - std::string cf; - if (!OSUtils::readFile(pathToClusterFile,cf)) - return; - - char myAddressStr[64]; - Utils::ztsnprintf(myAddressStr,sizeof(myAddressStr),"%.10llx",myAddress); - - std::vector lines(OSUtils::split(cf.c_str(),"\r\n","","")); - for(std::vector::iterator l(lines.begin());l!=lines.end();++l) { - std::vector fields(OSUtils::split(l->c_str()," \t","","")); - if ((fields.size() < 5)||(fields[0][0] == '#')||(fields[0] != myAddressStr)) - continue; - - //
geo - if (fields[1] == "geo") { - if ((fields.size() >= 7)&&(OSUtils::fileExists(fields[2].c_str()))) { - int ipStartColumn = Utils::strToInt(fields[3].c_str()); - int ipEndColumn = Utils::strToInt(fields[4].c_str()); - int latitudeColumn = Utils::strToInt(fields[5].c_str()); - int longitudeColumn = Utils::strToInt(fields[6].c_str()); - if (_geo.load(fields[2].c_str(),ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn) <= 0) - throw std::runtime_error(std::string("failed to load geo-ip data from ")+fields[2]); - } - continue; - } - - //
- int id = Utils::strToUInt(fields[1].c_str()); - if ((id < 0)||(id > ZT_CLUSTER_MAX_MEMBERS)) - throw std::runtime_error(std::string("invalid cluster member ID: ")+fields[1]); - MemberDefinition &md = _md[id]; - - md.id = (unsigned int)id; - if (fields.size() >= 6) { - std::vector xyz(OSUtils::split(fields[5].c_str(),",","","")); - md.x = (xyz.size() > 0) ? Utils::strToInt(xyz[0].c_str()) : 0; - md.y = (xyz.size() > 1) ? Utils::strToInt(xyz[1].c_str()) : 0; - md.z = (xyz.size() > 2) ? Utils::strToInt(xyz[2].c_str()) : 0; - } - Utils::scopy(md.name,sizeof(md.name),fields[2].c_str()); - md.clusterEndpoint.fromString(fields[3]); - if (!md.clusterEndpoint) - continue; - std::vector zips(OSUtils::split(fields[4].c_str(),",","","")); - for(std::vector::iterator zip(zips.begin());zip!=zips.end();++zip) { - InetAddress i; - i.fromString(*zip); - if (i) - md.zeroTierEndpoints.push_back(i); - } - - _ids.push_back((unsigned int)id); - } - - std::sort(_ids.begin(),_ids.end()); - } - - /** - * @return All member definitions in this cluster by ID (ID is array index) - */ - inline const MemberDefinition &operator[](unsigned int id) const throw() { return _md[id]; } - - /** - * @return Number of members in this cluster - */ - inline unsigned int size() const throw() { return (unsigned int)_ids.size(); } - - /** - * @return IDs of members in this cluster sorted by ID - */ - inline const std::vector &ids() const throw() { return _ids; } - - /** - * @return GeoIP service for this cluster - */ - inline ClusterGeoIpService &geo() throw() { return _geo; } - - /** - * @return A vector (new copy) containing all cluster members - */ - inline std::vector members() const - { - std::vector m; - for(std::vector::const_iterator i(_ids.begin());i!=_ids.end();++i) - m.push_back(_md[*i]); - return m; - } - -private: - MemberDefinition _md[ZT_CLUSTER_MAX_MEMBERS]; - std::vector _ids; - ClusterGeoIpService _geo; -}; - -} // namespace ZeroTier - -#endif // ZT_ENABLE_CLUSTER - -#endif -- cgit v1.2.3 From 640ad577d1f52140adbe42e87b2da931bf15f430 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 6 Jul 2017 11:56:46 -0700 Subject: . --- attic/ClusterGeoIpService.cpp | 243 ++++++++++++++++++++++++++++++++++++++++ attic/ClusterGeoIpService.hpp | 151 +++++++++++++++++++++++++ make-bsd.mk | 5 - make-linux.mk | 4 - make-mac.mk | 4 - node/Constants.hpp | 5 - node/Path.hpp | 12 -- node/Peer.cpp | 157 -------------------------- node/Peer.hpp | 31 ----- node/Topology.cpp | 5 +- objects.mk | 1 - service/ClusterGeoIpService.cpp | 243 ---------------------------------------- service/ClusterGeoIpService.hpp | 151 ------------------------- 13 files changed, 395 insertions(+), 617 deletions(-) create mode 100644 attic/ClusterGeoIpService.cpp create mode 100644 attic/ClusterGeoIpService.hpp delete mode 100644 service/ClusterGeoIpService.cpp delete mode 100644 service/ClusterGeoIpService.hpp (limited to 'attic') diff --git a/attic/ClusterGeoIpService.cpp b/attic/ClusterGeoIpService.cpp new file mode 100644 index 00000000..2dcc9179 --- /dev/null +++ b/attic/ClusterGeoIpService.cpp @@ -0,0 +1,243 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifdef ZT_ENABLE_CLUSTER + +#include + +#include + +#include "ClusterGeoIpService.hpp" + +#include "../node/Utils.hpp" +#include "../osdep/OSUtils.hpp" + +#define ZT_CLUSTERGEOIPSERVICE_FILE_MODIFICATION_CHECK_EVERY 10000 + +namespace ZeroTier { + +ClusterGeoIpService::ClusterGeoIpService() : + _pathToCsv(), + _ipStartColumn(-1), + _ipEndColumn(-1), + _latitudeColumn(-1), + _longitudeColumn(-1), + _lastFileCheckTime(0), + _csvModificationTime(0), + _csvFileSize(0) +{ +} + +ClusterGeoIpService::~ClusterGeoIpService() +{ +} + +bool ClusterGeoIpService::locate(const InetAddress &ip,int &x,int &y,int &z) +{ + Mutex::Lock _l(_lock); + + if ((_pathToCsv.length() > 0)&&((OSUtils::now() - _lastFileCheckTime) > ZT_CLUSTERGEOIPSERVICE_FILE_MODIFICATION_CHECK_EVERY)) { + _lastFileCheckTime = OSUtils::now(); + if ((_csvFileSize != OSUtils::getFileSize(_pathToCsv.c_str()))||(_csvModificationTime != OSUtils::getLastModified(_pathToCsv.c_str()))) + _load(_pathToCsv.c_str(),_ipStartColumn,_ipEndColumn,_latitudeColumn,_longitudeColumn); + } + + /* We search by looking up the upper bound of the sorted vXdb vectors + * and then iterating down for a matching IP range. We stop when we hit + * the beginning or an entry whose start and end are before the IP we + * are searching. */ + + if ((ip.ss_family == AF_INET)&&(_v4db.size() > 0)) { + _V4E key; + key.start = Utils::ntoh((uint32_t)(reinterpret_cast(&ip)->sin_addr.s_addr)); + std::vector<_V4E>::const_iterator i(std::upper_bound(_v4db.begin(),_v4db.end(),key)); + while (i != _v4db.begin()) { + --i; + if ((key.start >= i->start)&&(key.start <= i->end)) { + x = i->x; + y = i->y; + z = i->z; + //printf("%s : %f,%f %d,%d,%d\n",ip.toIpString().c_str(),i->lat,i->lon,x,y,z); + return true; + } else if ((key.start > i->start)&&(key.start > i->end)) + break; + } + } else if ((ip.ss_family == AF_INET6)&&(_v6db.size() > 0)) { + _V6E key; + memcpy(key.start,reinterpret_cast(&ip)->sin6_addr.s6_addr,16); + std::vector<_V6E>::const_iterator i(std::upper_bound(_v6db.begin(),_v6db.end(),key)); + while (i != _v6db.begin()) { + --i; + const int s_vs_s = memcmp(key.start,i->start,16); + const int s_vs_e = memcmp(key.start,i->end,16); + if ((s_vs_s >= 0)&&(s_vs_e <= 0)) { + x = i->x; + y = i->y; + z = i->z; + //printf("%s : %f,%f %d,%d,%d\n",ip.toIpString().c_str(),i->lat,i->lon,x,y,z); + return true; + } else if ((s_vs_s > 0)&&(s_vs_e > 0)) + break; + } + } + + return false; +} + +void ClusterGeoIpService::_parseLine(const char *line,std::vector<_V4E> &v4db,std::vector<_V6E> &v6db,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn) +{ + std::vector ls(OSUtils::split(line,",\t","\\","\"'")); + if ( ((ipStartColumn >= 0)&&(ipStartColumn < (int)ls.size()))&& + ((ipEndColumn >= 0)&&(ipEndColumn < (int)ls.size()))&& + ((latitudeColumn >= 0)&&(latitudeColumn < (int)ls.size()))&& + ((longitudeColumn >= 0)&&(longitudeColumn < (int)ls.size())) ) { + InetAddress ipStart(ls[ipStartColumn].c_str(),0); + InetAddress ipEnd(ls[ipEndColumn].c_str(),0); + const double lat = strtod(ls[latitudeColumn].c_str(),(char **)0); + const double lon = strtod(ls[longitudeColumn].c_str(),(char **)0); + + if ((ipStart.ss_family == ipEnd.ss_family)&&(ipStart)&&(ipEnd)&&(std::isfinite(lat))&&(std::isfinite(lon))) { + const double latRadians = lat * 0.01745329251994; // PI / 180 + const double lonRadians = lon * 0.01745329251994; // PI / 180 + const double cosLat = cos(latRadians); + const int x = (int)round((-6371.0) * cosLat * cos(lonRadians)); // 6371 == Earth's approximate radius in kilometers + const int y = (int)round(6371.0 * sin(latRadians)); + const int z = (int)round(6371.0 * cosLat * sin(lonRadians)); + + if (ipStart.ss_family == AF_INET) { + v4db.push_back(_V4E()); + v4db.back().start = Utils::ntoh((uint32_t)(reinterpret_cast(&ipStart)->sin_addr.s_addr)); + v4db.back().end = Utils::ntoh((uint32_t)(reinterpret_cast(&ipEnd)->sin_addr.s_addr)); + v4db.back().lat = (float)lat; + v4db.back().lon = (float)lon; + v4db.back().x = x; + v4db.back().y = y; + v4db.back().z = z; + //printf("%s - %s : %d,%d,%d\n",ipStart.toIpString().c_str(),ipEnd.toIpString().c_str(),x,y,z); + } else if (ipStart.ss_family == AF_INET6) { + v6db.push_back(_V6E()); + memcpy(v6db.back().start,reinterpret_cast(&ipStart)->sin6_addr.s6_addr,16); + memcpy(v6db.back().end,reinterpret_cast(&ipEnd)->sin6_addr.s6_addr,16); + v6db.back().lat = (float)lat; + v6db.back().lon = (float)lon; + v6db.back().x = x; + v6db.back().y = y; + v6db.back().z = z; + //printf("%s - %s : %d,%d,%d\n",ipStart.toIpString().c_str(),ipEnd.toIpString().c_str(),x,y,z); + } + } + } +} + +long ClusterGeoIpService::_load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn) +{ + // assumes _lock is locked + + FILE *f = fopen(pathToCsv,"rb"); + if (!f) + return -1; + + std::vector<_V4E> v4db; + std::vector<_V6E> v6db; + v4db.reserve(16777216); + v6db.reserve(16777216); + + char buf[4096]; + char linebuf[1024]; + unsigned int lineptr = 0; + for(;;) { + int n = (int)fread(buf,1,sizeof(buf),f); + if (n <= 0) + break; + for(int i=0;i 0)||(v6db.size() > 0)) { + std::sort(v4db.begin(),v4db.end()); + std::sort(v6db.begin(),v6db.end()); + + _pathToCsv = pathToCsv; + _ipStartColumn = ipStartColumn; + _ipEndColumn = ipEndColumn; + _latitudeColumn = latitudeColumn; + _longitudeColumn = longitudeColumn; + + _lastFileCheckTime = OSUtils::now(); + _csvModificationTime = OSUtils::getLastModified(pathToCsv); + _csvFileSize = OSUtils::getFileSize(pathToCsv); + + _v4db.swap(v4db); + _v6db.swap(v6db); + + return (long)(_v4db.size() + _v6db.size()); + } else { + return 0; + } +} + +} // namespace ZeroTier + +#endif // ZT_ENABLE_CLUSTER + +/* +int main(int argc,char **argv) +{ + char buf[1024]; + + ZeroTier::ClusterGeoIpService gip; + printf("loading...\n"); + gip.load("/Users/api/Code/ZeroTier/Infrastructure/root-servers/zerotier-one/cluster-geoip.csv",0,1,5,6); + printf("... done!\n"); fflush(stdout); + + while (gets(buf)) { // unsafe, testing only + ZeroTier::InetAddress addr(buf,0); + printf("looking up: %s\n",addr.toString().c_str()); fflush(stdout); + int x = 0,y = 0,z = 0; + if (gip.locate(addr,x,y,z)) { + //printf("%s: %d,%d,%d\n",addr.toString().c_str(),x,y,z); fflush(stdout); + } else { + printf("%s: not found!\n",addr.toString().c_str()); fflush(stdout); + } + } + + return 0; +} +*/ diff --git a/attic/ClusterGeoIpService.hpp b/attic/ClusterGeoIpService.hpp new file mode 100644 index 00000000..380f944f --- /dev/null +++ b/attic/ClusterGeoIpService.hpp @@ -0,0 +1,151 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifndef ZT_CLUSTERGEOIPSERVICE_HPP +#define ZT_CLUSTERGEOIPSERVICE_HPP + +#ifdef ZT_ENABLE_CLUSTER + +#include +#include +#include +#include + +#include +#include +#include + +#include "../node/Constants.hpp" +#include "../node/Mutex.hpp" +#include "../node/NonCopyable.hpp" +#include "../node/InetAddress.hpp" + +namespace ZeroTier { + +/** + * Loads a GeoIP CSV into memory for fast lookup, reloading as needed + * + * This was designed around the CSV from https://db-ip.com but can be used + * with any similar GeoIP CSV database that is presented in the form of an + * IP range and lat/long coordinates. + * + * It loads the whole database into memory, which can be kind of large. If + * the CSV file changes, the changes are loaded automatically. + */ +class ClusterGeoIpService : NonCopyable +{ +public: + ClusterGeoIpService(); + ~ClusterGeoIpService(); + + /** + * Load or reload CSV file + * + * CSV column indexes start at zero. CSVs can be quoted with single or + * double quotes. Whitespace before or after commas is ignored. Backslash + * may be used for escaping whitespace as well. + * + * @param pathToCsv Path to (uncompressed) CSV file + * @param ipStartColumn Column with IP range start + * @param ipEndColumn Column with IP range end (inclusive) + * @param latitudeColumn Column with latitude + * @param longitudeColumn Column with longitude + * @return Number of valid records loaded or -1 on error (invalid file, not found, etc.) + */ + inline long load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn) + { + Mutex::Lock _l(_lock); + return _load(pathToCsv,ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn); + } + + /** + * Attempt to locate an IP + * + * This returns true if x, y, and z are set. If the return value is false + * the values of x, y, and z are undefined. + * + * @param ip IPv4 or IPv6 address + * @param x Reference to variable to receive X + * @param y Reference to variable to receive Y + * @param z Reference to variable to receive Z + * @return True if coordinates were set + */ + bool locate(const InetAddress &ip,int &x,int &y,int &z); + + /** + * @return True if IP database/service is available for queries (otherwise locate() will always be false) + */ + inline bool available() const + { + Mutex::Lock _l(_lock); + return ((_v4db.size() + _v6db.size()) > 0); + } + +private: + struct _V4E + { + uint32_t start; + uint32_t end; + float lat,lon; + int16_t x,y,z; + + inline bool operator<(const _V4E &e) const { return (start < e.start); } + }; + + struct _V6E + { + uint8_t start[16]; + uint8_t end[16]; + float lat,lon; + int16_t x,y,z; + + inline bool operator<(const _V6E &e) const { return (memcmp(start,e.start,16) < 0); } + }; + + static void _parseLine(const char *line,std::vector<_V4E> &v4db,std::vector<_V6E> &v6db,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn); + long _load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn); + + std::string _pathToCsv; + int _ipStartColumn; + int _ipEndColumn; + int _latitudeColumn; + int _longitudeColumn; + + uint64_t _lastFileCheckTime; + uint64_t _csvModificationTime; + int64_t _csvFileSize; + + std::vector<_V4E> _v4db; + std::vector<_V6E> _v6db; + + Mutex _lock; +}; + +} // namespace ZeroTier + +#endif // ZT_ENABLE_CLUSTER + +#endif diff --git a/make-bsd.mk b/make-bsd.mk index cbab5810..c2fd6062 100644 --- a/make-bsd.mk +++ b/make-bsd.mk @@ -7,11 +7,6 @@ LIBS= include objects.mk ONE_OBJS+=osdep/BSDEthernetTap.o ext/http-parser/http_parser.o -# Build with ZT_ENABLE_CLUSTER=1 to build with cluster support -ifeq ($(ZT_ENABLE_CLUSTER),1) - DEFS+=-DZT_ENABLE_CLUSTER -endif - # "make debug" is a shortcut for this ifeq ($(ZT_DEBUG),1) CFLAGS+=-Wall -Werror -g -pthread $(INCLUDES) $(DEFS) diff --git a/make-linux.mk b/make-linux.mk index c27c600d..7017d31e 100644 --- a/make-linux.mk +++ b/make-linux.mk @@ -38,10 +38,6 @@ endif # Trying to use dynamically linked libhttp-parser causes tons of compatibility problems. ONE_OBJS+=ext/http-parser/http_parser.o -ifeq ($(ZT_ENABLE_CLUSTER),1) - DEFS+=-DZT_ENABLE_CLUSTER -endif - ifeq ($(ZT_SYNOLOGY), 1) DEFS+=-D__SYNOLOGY__ endif diff --git a/make-mac.mk b/make-mac.mk index 5622a41b..196b17cb 100644 --- a/make-mac.mk +++ b/make-mac.mk @@ -33,10 +33,6 @@ else DEFS+=-DZT_SOFTWARE_UPDATE_DEFAULT="\"download\"" endif -ifeq ($(ZT_ENABLE_CLUSTER),1) - DEFS+=-DZT_ENABLE_CLUSTER -endif - # Use fast ASM Salsa20/12 for x64 processors DEFS+=-DZT_USE_X64_ASM_SALSA2012 CORE_OBJS+=ext/x64-salsa2012-asm/salsa2012.o diff --git a/node/Constants.hpp b/node/Constants.hpp index 274b9564..12bf8d28 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -218,11 +218,6 @@ */ #define ZT_HOUSEKEEPING_PERIOD 60000 -/** - * How often in ms to write peer state to storage and/or cluster (approximate) - */ -#define ZT_PEER_STATE_WRITE_PERIOD 10000 - /** * How long to remember peer records in RAM if they haven't been used */ diff --git a/node/Path.hpp b/node/Path.hpp index 854b28e2..ac8e4c0e 100644 --- a/node/Path.hpp +++ b/node/Path.hpp @@ -283,18 +283,6 @@ public: */ inline uint64_t lastTrustEstablishedPacketReceived() const { return _lastTrustEstablishedPacketReceived; } - /** - * @param lo Last out send - * @param li Last in send - * @param lt Last trust established packet received - */ - inline void updateFromRemoteState(const uint64_t lo,const uint64_t li,const uint64_t lt) - { - _lastOut = lo; - _lastIn = li; - _lastTrustEstablishedPacketReceived = lt; - } - /** * Return and increment outgoing packet counter (used with Packet::armor()) * diff --git a/node/Peer.cpp b/node/Peer.cpp index 875d651e..fb9a72b1 100644 --- a/node/Peer.cpp +++ b/node/Peer.cpp @@ -38,8 +38,6 @@ namespace ZeroTier { Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Identity &peerIdentity) : RR(renv), - _lastWroteState(0), - _lastReceivedStateTimestamp(0), _lastReceive(0), _lastNontrivialReceive(0), _lastTriedMemorizedPath(0), @@ -184,7 +182,6 @@ void Peer::received( if (verb == Packet::VERB_OK) { potentialNewPeerPath->lr = now; potentialNewPeerPath->p = path; - _lastWroteState = 0; // force state write now } else { TRACE("got %s via unknown path %s(%s), confirming...",Packet::verbString(verb),_id.address().toString().c_str(),path->address().toString().c_str()); attemptToContactAt(tPtr,path->localSocket(),path->address(),now,true,path->nextOutgoingCounter()); @@ -263,9 +260,6 @@ void Peer::received( } } } - - if ((now - _lastWroteState) > ZT_PEER_STATE_WRITE_PERIOD) - writeState(tPtr,now); } bool Peer::sendDirect(void *tPtr,const void *data,unsigned int len,uint64_t now,bool force) @@ -428,155 +422,4 @@ bool Peer::doPingAndKeepalive(void *tPtr,uint64_t now,int inetAddressFamily) return false; } -void Peer::writeState(void *tPtr,const uint64_t now) -{ - try { - Buffer b; - - b.append((uint8_t)1); // version - b.append(now); - - _id.serialize(b); - - { - Mutex::Lock _l(_paths_m); - unsigned int count = 0; - if (_v4Path.lr) - ++count; - if (_v6Path.lr) - ++count; - b.append((uint8_t)count); - if (_v4Path.lr) { - b.append(_v4Path.lr); - b.append(_v4Path.p->lastOut()); - b.append(_v4Path.p->lastIn()); - b.append(_v4Path.p->lastTrustEstablishedPacketReceived()); - _v4Path.p->address().serialize(b); - } - if (_v6Path.lr) { - b.append(_v6Path.lr); - b.append(_v6Path.p->lastOut()); - b.append(_v6Path.p->lastIn()); - b.append(_v6Path.p->lastTrustEstablishedPacketReceived()); - _v6Path.p->address().serialize(b); - } - } - - // Save space by sending these as time since now at 100ms resolution - b.append((uint16_t)(std::max(now - _lastReceive,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastNontrivialReceive,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastTriedMemorizedPath,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastDirectPathPushSent,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastDirectPathPushReceive,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastCredentialRequestSent,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastWhoisRequestReceived,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastEchoRequestReceived,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastComRequestReceived,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastComRequestSent,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastCredentialsReceived,(uint64_t)6553500) / 100)); - b.append((uint16_t)(std::max(now - _lastTrustEstablishedPacketReceived,(uint64_t)6553500) / 100)); - - b.append((uint8_t)_vProto); - b.append((uint8_t)_vMajor); - b.append((uint8_t)_vMinor); - b.append((uint16_t)_vRevision); - - b.append((uint16_t)0); // length of additional fields - - uint64_t tmp[2]; - tmp[0] = _id.address().toInt(); tmp[1] = 0; - //RR->node->stateObjectPut(tPtr,ZT_STATE_OBJECT_PEER_STATE,tmp,b.data(),b.size()); - - _lastWroteState = now; - } catch ( ... ) {} // sanity check, should not be possible -} - -bool Peer::applyStateUpdate(const void *data,unsigned int len) -{ - try { - Buffer b(data,len); - unsigned int ptr = 0; - - if (b[ptr++] != 1) - return false; - const uint64_t ts = b.at(ptr); ptr += 8; - if (ts <= _lastReceivedStateTimestamp) - return false; - - Identity id; - ptr += id.deserialize(b,ptr); - if (id != _id) // sanity check - return false; - - const unsigned int pathCount = (unsigned int)b[ptr++]; - { - Mutex::Lock _l(_paths_m); - for(unsigned int i=0;i(ptr); ptr += 8; - const uint64_t lastOut = b.at(ptr); ptr += 8; - const uint64_t lastIn = b.at(ptr); ptr += 8; - const uint64_t lastTrustEstablishedPacketReceived = b.at(ptr); ptr += 8; - InetAddress addr; - ptr += addr.deserialize(b,ptr); - _PeerPath *p = (_PeerPath *)0; - switch(addr.ss_family) { - case AF_INET: p = &_v4Path; break; - case AF_INET6: p = &_v6Path; break; - } - if (p) { - if ( (!p->p) || (p->p->address() != addr) ) { - p->p = RR->topology->getPath(-1,addr); - } - p->lr = lr; - p->p->updateFromRemoteState(lastOut,lastIn,lastTrustEstablishedPacketReceived); - } - } - } - - _lastReceive = std::max(_lastReceive,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastNontrivialReceive = std::max(_lastNontrivialReceive,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastTriedMemorizedPath = std::max(_lastTriedMemorizedPath,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastDirectPathPushSent = std::max(_lastDirectPathPushSent,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastDirectPathPushReceive = std::max(_lastDirectPathPushReceive,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastCredentialRequestSent = std::max(_lastCredentialRequestSent,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastWhoisRequestReceived = std::max(_lastWhoisRequestReceived,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastEchoRequestReceived = std::max(_lastEchoRequestReceived,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastComRequestReceived = std::max(_lastComRequestReceived,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastComRequestSent = std::max(_lastComRequestSent,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastCredentialsReceived = std::max(_lastCredentialsReceived,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - _lastTrustEstablishedPacketReceived = std::max(_lastTrustEstablishedPacketReceived,ts - ((uint64_t)b.at(ptr) * 100ULL)); ptr += 2; - - _vProto = (uint16_t)b[ptr++]; - _vMajor = (uint16_t)b[ptr++]; - _vMinor = (uint16_t)b[ptr++]; - _vRevision = b.at(ptr); ptr += 2; - - _lastReceivedStateTimestamp = ts; - - return true; - } catch ( ... ) {} // ignore invalid state updates - return false; -} - -SharedPtr Peer::createFromStateUpdate(const RuntimeEnvironment *renv,void *tPtr,const void *data,unsigned int len) -{ - try { - Identity id; - { - Buffer b(data,len); - unsigned int ptr = 0; - if (b[ptr++] != 1) - return SharedPtr(); - ptr += 8; // skip TS, don't care - id.deserialize(b,ptr); - } - if (id) { - const SharedPtr p(new Peer(renv,renv->identity,id)); - if (p->applyStateUpdate(data,len)) - return renv->topology->addPeer(tPtr,p); - } - } catch ( ... ) {} - return SharedPtr(); -} - } // namespace ZeroTier diff --git a/node/Peer.hpp b/node/Peer.hpp index 478c7232..ad2d0ddc 100644 --- a/node/Peer.hpp +++ b/node/Peer.hpp @@ -195,23 +195,6 @@ public: */ bool doPingAndKeepalive(void *tPtr,uint64_t now,int inetAddressFamily); - /** - * Write object state to external storage and/or cluster network - * - * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call - * @param now Current time - */ - void writeState(void *tPtr,const uint64_t now); - - /** - * Apply a state update received from e.g. a remote cluster member - * - * @param data State update data - * @param len Length of state update - * @return True if state update was applied, false if ignored or invalid - */ - bool applyStateUpdate(const void *data,unsigned int len); - /** * Reset paths within a given IP scope and address family * @@ -440,17 +423,6 @@ public: return false; } - /** - * Create a peer from a remote state update - * - * @param renv Runtime environment - * @param tPtr Thread pointer to be handed through to any callbacks called as a result of this call - * @param data State update data - * @param len State update length - * @return Peer or NULL if data was invalid - */ - static SharedPtr createFromStateUpdate(const RuntimeEnvironment *renv,void *tPtr,const void *data,unsigned int len); - private: struct _PeerPath { @@ -463,9 +435,6 @@ private: const RuntimeEnvironment *RR; - uint64_t _lastWroteState; - uint64_t _lastReceivedStateTimestamp; - uint64_t _lastReceive; // direct or indirect uint64_t _lastNontrivialReceive; // frames, things like netconf, etc. uint64_t _lastTriedMemorizedPath; diff --git a/node/Topology.cpp b/node/Topology.cpp index d4632f43..809bc7e7 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -395,11 +395,8 @@ void Topology::doPeriodicTasks(void *tPtr,uint64_t now) Address *a = (Address *)0; SharedPtr *p = (SharedPtr *)0; while (i.next(a,p)) { - if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) { + if ( (!(*p)->isAlive(now)) && (std::find(_upstreamAddresses.begin(),_upstreamAddresses.end(),*a) == _upstreamAddresses.end()) ) _peers.erase(*a); - } else { - (*p)->writeState(tPtr,now); - } } } diff --git a/objects.mk b/objects.mk index c8231f08..3a8bd645 100644 --- a/objects.mk +++ b/objects.mk @@ -31,7 +31,6 @@ ONE_OBJS=\ osdep/ManagedRoute.o \ osdep/Http.o \ osdep/OSUtils.o \ - service/ClusterGeoIpService.o \ service/SoftwareUpdater.o \ service/OneService.o diff --git a/service/ClusterGeoIpService.cpp b/service/ClusterGeoIpService.cpp deleted file mode 100644 index 2dcc9179..00000000 --- a/service/ClusterGeoIpService.cpp +++ /dev/null @@ -1,243 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#ifdef ZT_ENABLE_CLUSTER - -#include - -#include - -#include "ClusterGeoIpService.hpp" - -#include "../node/Utils.hpp" -#include "../osdep/OSUtils.hpp" - -#define ZT_CLUSTERGEOIPSERVICE_FILE_MODIFICATION_CHECK_EVERY 10000 - -namespace ZeroTier { - -ClusterGeoIpService::ClusterGeoIpService() : - _pathToCsv(), - _ipStartColumn(-1), - _ipEndColumn(-1), - _latitudeColumn(-1), - _longitudeColumn(-1), - _lastFileCheckTime(0), - _csvModificationTime(0), - _csvFileSize(0) -{ -} - -ClusterGeoIpService::~ClusterGeoIpService() -{ -} - -bool ClusterGeoIpService::locate(const InetAddress &ip,int &x,int &y,int &z) -{ - Mutex::Lock _l(_lock); - - if ((_pathToCsv.length() > 0)&&((OSUtils::now() - _lastFileCheckTime) > ZT_CLUSTERGEOIPSERVICE_FILE_MODIFICATION_CHECK_EVERY)) { - _lastFileCheckTime = OSUtils::now(); - if ((_csvFileSize != OSUtils::getFileSize(_pathToCsv.c_str()))||(_csvModificationTime != OSUtils::getLastModified(_pathToCsv.c_str()))) - _load(_pathToCsv.c_str(),_ipStartColumn,_ipEndColumn,_latitudeColumn,_longitudeColumn); - } - - /* We search by looking up the upper bound of the sorted vXdb vectors - * and then iterating down for a matching IP range. We stop when we hit - * the beginning or an entry whose start and end are before the IP we - * are searching. */ - - if ((ip.ss_family == AF_INET)&&(_v4db.size() > 0)) { - _V4E key; - key.start = Utils::ntoh((uint32_t)(reinterpret_cast(&ip)->sin_addr.s_addr)); - std::vector<_V4E>::const_iterator i(std::upper_bound(_v4db.begin(),_v4db.end(),key)); - while (i != _v4db.begin()) { - --i; - if ((key.start >= i->start)&&(key.start <= i->end)) { - x = i->x; - y = i->y; - z = i->z; - //printf("%s : %f,%f %d,%d,%d\n",ip.toIpString().c_str(),i->lat,i->lon,x,y,z); - return true; - } else if ((key.start > i->start)&&(key.start > i->end)) - break; - } - } else if ((ip.ss_family == AF_INET6)&&(_v6db.size() > 0)) { - _V6E key; - memcpy(key.start,reinterpret_cast(&ip)->sin6_addr.s6_addr,16); - std::vector<_V6E>::const_iterator i(std::upper_bound(_v6db.begin(),_v6db.end(),key)); - while (i != _v6db.begin()) { - --i; - const int s_vs_s = memcmp(key.start,i->start,16); - const int s_vs_e = memcmp(key.start,i->end,16); - if ((s_vs_s >= 0)&&(s_vs_e <= 0)) { - x = i->x; - y = i->y; - z = i->z; - //printf("%s : %f,%f %d,%d,%d\n",ip.toIpString().c_str(),i->lat,i->lon,x,y,z); - return true; - } else if ((s_vs_s > 0)&&(s_vs_e > 0)) - break; - } - } - - return false; -} - -void ClusterGeoIpService::_parseLine(const char *line,std::vector<_V4E> &v4db,std::vector<_V6E> &v6db,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn) -{ - std::vector ls(OSUtils::split(line,",\t","\\","\"'")); - if ( ((ipStartColumn >= 0)&&(ipStartColumn < (int)ls.size()))&& - ((ipEndColumn >= 0)&&(ipEndColumn < (int)ls.size()))&& - ((latitudeColumn >= 0)&&(latitudeColumn < (int)ls.size()))&& - ((longitudeColumn >= 0)&&(longitudeColumn < (int)ls.size())) ) { - InetAddress ipStart(ls[ipStartColumn].c_str(),0); - InetAddress ipEnd(ls[ipEndColumn].c_str(),0); - const double lat = strtod(ls[latitudeColumn].c_str(),(char **)0); - const double lon = strtod(ls[longitudeColumn].c_str(),(char **)0); - - if ((ipStart.ss_family == ipEnd.ss_family)&&(ipStart)&&(ipEnd)&&(std::isfinite(lat))&&(std::isfinite(lon))) { - const double latRadians = lat * 0.01745329251994; // PI / 180 - const double lonRadians = lon * 0.01745329251994; // PI / 180 - const double cosLat = cos(latRadians); - const int x = (int)round((-6371.0) * cosLat * cos(lonRadians)); // 6371 == Earth's approximate radius in kilometers - const int y = (int)round(6371.0 * sin(latRadians)); - const int z = (int)round(6371.0 * cosLat * sin(lonRadians)); - - if (ipStart.ss_family == AF_INET) { - v4db.push_back(_V4E()); - v4db.back().start = Utils::ntoh((uint32_t)(reinterpret_cast(&ipStart)->sin_addr.s_addr)); - v4db.back().end = Utils::ntoh((uint32_t)(reinterpret_cast(&ipEnd)->sin_addr.s_addr)); - v4db.back().lat = (float)lat; - v4db.back().lon = (float)lon; - v4db.back().x = x; - v4db.back().y = y; - v4db.back().z = z; - //printf("%s - %s : %d,%d,%d\n",ipStart.toIpString().c_str(),ipEnd.toIpString().c_str(),x,y,z); - } else if (ipStart.ss_family == AF_INET6) { - v6db.push_back(_V6E()); - memcpy(v6db.back().start,reinterpret_cast(&ipStart)->sin6_addr.s6_addr,16); - memcpy(v6db.back().end,reinterpret_cast(&ipEnd)->sin6_addr.s6_addr,16); - v6db.back().lat = (float)lat; - v6db.back().lon = (float)lon; - v6db.back().x = x; - v6db.back().y = y; - v6db.back().z = z; - //printf("%s - %s : %d,%d,%d\n",ipStart.toIpString().c_str(),ipEnd.toIpString().c_str(),x,y,z); - } - } - } -} - -long ClusterGeoIpService::_load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn) -{ - // assumes _lock is locked - - FILE *f = fopen(pathToCsv,"rb"); - if (!f) - return -1; - - std::vector<_V4E> v4db; - std::vector<_V6E> v6db; - v4db.reserve(16777216); - v6db.reserve(16777216); - - char buf[4096]; - char linebuf[1024]; - unsigned int lineptr = 0; - for(;;) { - int n = (int)fread(buf,1,sizeof(buf),f); - if (n <= 0) - break; - for(int i=0;i 0)||(v6db.size() > 0)) { - std::sort(v4db.begin(),v4db.end()); - std::sort(v6db.begin(),v6db.end()); - - _pathToCsv = pathToCsv; - _ipStartColumn = ipStartColumn; - _ipEndColumn = ipEndColumn; - _latitudeColumn = latitudeColumn; - _longitudeColumn = longitudeColumn; - - _lastFileCheckTime = OSUtils::now(); - _csvModificationTime = OSUtils::getLastModified(pathToCsv); - _csvFileSize = OSUtils::getFileSize(pathToCsv); - - _v4db.swap(v4db); - _v6db.swap(v6db); - - return (long)(_v4db.size() + _v6db.size()); - } else { - return 0; - } -} - -} // namespace ZeroTier - -#endif // ZT_ENABLE_CLUSTER - -/* -int main(int argc,char **argv) -{ - char buf[1024]; - - ZeroTier::ClusterGeoIpService gip; - printf("loading...\n"); - gip.load("/Users/api/Code/ZeroTier/Infrastructure/root-servers/zerotier-one/cluster-geoip.csv",0,1,5,6); - printf("... done!\n"); fflush(stdout); - - while (gets(buf)) { // unsafe, testing only - ZeroTier::InetAddress addr(buf,0); - printf("looking up: %s\n",addr.toString().c_str()); fflush(stdout); - int x = 0,y = 0,z = 0; - if (gip.locate(addr,x,y,z)) { - //printf("%s: %d,%d,%d\n",addr.toString().c_str(),x,y,z); fflush(stdout); - } else { - printf("%s: not found!\n",addr.toString().c_str()); fflush(stdout); - } - } - - return 0; -} -*/ diff --git a/service/ClusterGeoIpService.hpp b/service/ClusterGeoIpService.hpp deleted file mode 100644 index 380f944f..00000000 --- a/service/ClusterGeoIpService.hpp +++ /dev/null @@ -1,151 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#ifndef ZT_CLUSTERGEOIPSERVICE_HPP -#define ZT_CLUSTERGEOIPSERVICE_HPP - -#ifdef ZT_ENABLE_CLUSTER - -#include -#include -#include -#include - -#include -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Mutex.hpp" -#include "../node/NonCopyable.hpp" -#include "../node/InetAddress.hpp" - -namespace ZeroTier { - -/** - * Loads a GeoIP CSV into memory for fast lookup, reloading as needed - * - * This was designed around the CSV from https://db-ip.com but can be used - * with any similar GeoIP CSV database that is presented in the form of an - * IP range and lat/long coordinates. - * - * It loads the whole database into memory, which can be kind of large. If - * the CSV file changes, the changes are loaded automatically. - */ -class ClusterGeoIpService : NonCopyable -{ -public: - ClusterGeoIpService(); - ~ClusterGeoIpService(); - - /** - * Load or reload CSV file - * - * CSV column indexes start at zero. CSVs can be quoted with single or - * double quotes. Whitespace before or after commas is ignored. Backslash - * may be used for escaping whitespace as well. - * - * @param pathToCsv Path to (uncompressed) CSV file - * @param ipStartColumn Column with IP range start - * @param ipEndColumn Column with IP range end (inclusive) - * @param latitudeColumn Column with latitude - * @param longitudeColumn Column with longitude - * @return Number of valid records loaded or -1 on error (invalid file, not found, etc.) - */ - inline long load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn) - { - Mutex::Lock _l(_lock); - return _load(pathToCsv,ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn); - } - - /** - * Attempt to locate an IP - * - * This returns true if x, y, and z are set. If the return value is false - * the values of x, y, and z are undefined. - * - * @param ip IPv4 or IPv6 address - * @param x Reference to variable to receive X - * @param y Reference to variable to receive Y - * @param z Reference to variable to receive Z - * @return True if coordinates were set - */ - bool locate(const InetAddress &ip,int &x,int &y,int &z); - - /** - * @return True if IP database/service is available for queries (otherwise locate() will always be false) - */ - inline bool available() const - { - Mutex::Lock _l(_lock); - return ((_v4db.size() + _v6db.size()) > 0); - } - -private: - struct _V4E - { - uint32_t start; - uint32_t end; - float lat,lon; - int16_t x,y,z; - - inline bool operator<(const _V4E &e) const { return (start < e.start); } - }; - - struct _V6E - { - uint8_t start[16]; - uint8_t end[16]; - float lat,lon; - int16_t x,y,z; - - inline bool operator<(const _V6E &e) const { return (memcmp(start,e.start,16) < 0); } - }; - - static void _parseLine(const char *line,std::vector<_V4E> &v4db,std::vector<_V6E> &v6db,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn); - long _load(const char *pathToCsv,int ipStartColumn,int ipEndColumn,int latitudeColumn,int longitudeColumn); - - std::string _pathToCsv; - int _ipStartColumn; - int _ipEndColumn; - int _latitudeColumn; - int _longitudeColumn; - - uint64_t _lastFileCheckTime; - uint64_t _csvModificationTime; - int64_t _csvFileSize; - - std::vector<_V4E> _v4db; - std::vector<_V6E> _v6db; - - Mutex _lock; -}; - -} // namespace ZeroTier - -#endif // ZT_ENABLE_CLUSTER - -#endif -- cgit v1.2.3 From 0655a1fcbe3aaae3ed5dee94ebe05edf10823b07 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 13 Jul 2017 16:42:43 -0700 Subject: Move old cluster code into attic. --- Cluster.cpp | 1042 ----------------------------------------------------- Cluster.hpp | 463 ------------------------ attic/Cluster.cpp | 1042 +++++++++++++++++++++++++++++++++++++++++++++++++++++ attic/Cluster.hpp | 463 ++++++++++++++++++++++++ 4 files changed, 1505 insertions(+), 1505 deletions(-) delete mode 100644 Cluster.cpp delete mode 100644 Cluster.hpp create mode 100644 attic/Cluster.cpp create mode 100644 attic/Cluster.hpp (limited to 'attic') diff --git a/Cluster.cpp b/Cluster.cpp deleted file mode 100644 index 119aec29..00000000 --- a/Cluster.cpp +++ /dev/null @@ -1,1042 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#ifdef ZT_ENABLE_CLUSTER - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "../version.h" - -#include "Cluster.hpp" -#include "RuntimeEnvironment.hpp" -#include "MulticastGroup.hpp" -#include "CertificateOfMembership.hpp" -#include "Salsa20.hpp" -#include "Poly1305.hpp" -#include "Identity.hpp" -#include "Topology.hpp" -#include "Packet.hpp" -#include "Switch.hpp" -#include "Node.hpp" -#include "Network.hpp" -#include "Array.hpp" - -namespace ZeroTier { - -static inline double _dist3d(int x1,int y1,int z1,int x2,int y2,int z2) - throw() -{ - double dx = ((double)x2 - (double)x1); - double dy = ((double)y2 - (double)y1); - double dz = ((double)z2 - (double)z1); - return sqrt((dx * dx) + (dy * dy) + (dz * dz)); -} - -// An entry in _ClusterSendQueue -struct _ClusterSendQueueEntry -{ - uint64_t timestamp; - Address fromPeerAddress; - Address toPeerAddress; - // if we ever support larger transport MTUs this must be increased - unsigned char data[ZT_CLUSTER_SEND_QUEUE_DATA_MAX]; - unsigned int len; - bool unite; -}; - -// A multi-index map with entry memory pooling -- this allows our queue to -// be O(log(N)) and is complex enough that it makes the code a lot cleaner -// to break it out from Cluster. -class _ClusterSendQueue -{ -public: - _ClusterSendQueue() : - _poolCount(0) {} - ~_ClusterSendQueue() {} // memory is automatically freed when _chunks is destroyed - - inline void enqueue(uint64_t now,const Address &from,const Address &to,const void *data,unsigned int len,bool unite) - { - if (len > ZT_CLUSTER_SEND_QUEUE_DATA_MAX) - return; - - Mutex::Lock _l(_lock); - - // Delete oldest queue entry for this sender if this enqueue() would take them over the per-sender limit - { - std::set< std::pair >::iterator qi(_bySrc.lower_bound(std::pair(from,(_ClusterSendQueueEntry *)0))); - std::set< std::pair >::iterator oldest(qi); - unsigned long countForSender = 0; - while ((qi != _bySrc.end())&&(qi->first == from)) { - if (qi->second->timestamp < oldest->second->timestamp) - oldest = qi; - ++countForSender; - ++qi; - } - if (countForSender >= ZT_CLUSTER_MAX_QUEUE_PER_SENDER) { - _byDest.erase(std::pair(oldest->second->toPeerAddress,oldest->second)); - _pool[_poolCount++] = oldest->second; - _bySrc.erase(oldest); - } - } - - _ClusterSendQueueEntry *e; - if (_poolCount > 0) { - e = _pool[--_poolCount]; - } else { - if (_chunks.size() >= ZT_CLUSTER_MAX_QUEUE_CHUNKS) - return; // queue is totally full! - _chunks.push_back(Array<_ClusterSendQueueEntry,ZT_CLUSTER_QUEUE_CHUNK_SIZE>()); - e = &(_chunks.back().data[0]); - for(unsigned int i=1;itimestamp = now; - e->fromPeerAddress = from; - e->toPeerAddress = to; - memcpy(e->data,data,len); - e->len = len; - e->unite = unite; - - _bySrc.insert(std::pair(from,e)); - _byDest.insert(std::pair(to,e)); - } - - inline void expire(uint64_t now) - { - Mutex::Lock _l(_lock); - for(std::set< std::pair >::iterator qi(_bySrc.begin());qi!=_bySrc.end();) { - if ((now - qi->second->timestamp) > ZT_CLUSTER_QUEUE_EXPIRATION) { - _byDest.erase(std::pair(qi->second->toPeerAddress,qi->second)); - _pool[_poolCount++] = qi->second; - _bySrc.erase(qi++); - } else ++qi; - } - } - - /** - * Get and dequeue entries for a given destination address - * - * After use these entries must be returned with returnToPool()! - * - * @param dest Destination address - * @param results Array to fill with results - * @param maxResults Size of results[] in pointers - * @return Number of actual results returned - */ - inline unsigned int getByDest(const Address &dest,_ClusterSendQueueEntry **results,unsigned int maxResults) - { - unsigned int count = 0; - Mutex::Lock _l(_lock); - std::set< std::pair >::iterator qi(_byDest.lower_bound(std::pair(dest,(_ClusterSendQueueEntry *)0))); - while ((qi != _byDest.end())&&(qi->first == dest)) { - _bySrc.erase(std::pair(qi->second->fromPeerAddress,qi->second)); - results[count++] = qi->second; - if (count == maxResults) - break; - _byDest.erase(qi++); - } - return count; - } - - /** - * Return entries to pool after use - * - * @param entries Array of entries - * @param count Number of entries - */ - inline void returnToPool(_ClusterSendQueueEntry **entries,unsigned int count) - { - Mutex::Lock _l(_lock); - for(unsigned int i=0;i > _chunks; - _ClusterSendQueueEntry *_pool[ZT_CLUSTER_QUEUE_CHUNK_SIZE * ZT_CLUSTER_MAX_QUEUE_CHUNKS]; - unsigned long _poolCount; - std::set< std::pair > _bySrc; - std::set< std::pair > _byDest; - Mutex _lock; -}; - -Cluster::Cluster( - const RuntimeEnvironment *renv, - uint16_t id, - const std::vector &zeroTierPhysicalEndpoints, - int32_t x, - int32_t y, - int32_t z, - void (*sendFunction)(void *,unsigned int,const void *,unsigned int), - void *sendFunctionArg, - int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *), - void *addressToLocationFunctionArg) : - RR(renv), - _sendQueue(new _ClusterSendQueue()), - _sendFunction(sendFunction), - _sendFunctionArg(sendFunctionArg), - _addressToLocationFunction(addressToLocationFunction), - _addressToLocationFunctionArg(addressToLocationFunctionArg), - _x(x), - _y(y), - _z(z), - _id(id), - _zeroTierPhysicalEndpoints(zeroTierPhysicalEndpoints), - _members(new _Member[ZT_CLUSTER_MAX_MEMBERS]), - _lastFlushed(0), - _lastCleanedRemotePeers(0), - _lastCleanedQueue(0) -{ - uint16_t stmp[ZT_SHA512_DIGEST_LEN / sizeof(uint16_t)]; - - // Generate master secret by hashing the secret from our Identity key pair - RR->identity.sha512PrivateKey(_masterSecret); - - // Generate our inbound message key, which is the master secret XORed with our ID and hashed twice - memcpy(stmp,_masterSecret,sizeof(stmp)); - stmp[0] ^= Utils::hton(id); - SHA512::hash(stmp,stmp,sizeof(stmp)); - SHA512::hash(stmp,stmp,sizeof(stmp)); - memcpy(_key,stmp,sizeof(_key)); - Utils::burn(stmp,sizeof(stmp)); -} - -Cluster::~Cluster() -{ - Utils::burn(_masterSecret,sizeof(_masterSecret)); - Utils::burn(_key,sizeof(_key)); - delete [] _members; - delete _sendQueue; -} - -void Cluster::handleIncomingStateMessage(const void *msg,unsigned int len) -{ - Buffer dmsg; - { - // FORMAT: <[16] iv><[8] MAC><... data> - if ((len < 24)||(len > ZT_CLUSTER_MAX_MESSAGE_LENGTH)) - return; - - // 16-byte IV: first 8 bytes XORed with key, last 8 bytes used as Salsa20 64-bit IV - char keytmp[32]; - memcpy(keytmp,_key,32); - for(int i=0;i<8;++i) - keytmp[i] ^= reinterpret_cast(msg)[i]; - Salsa20 s20(keytmp,reinterpret_cast(msg) + 8); - Utils::burn(keytmp,sizeof(keytmp)); - - // One-time-use Poly1305 key from first 32 bytes of Salsa20 keystream (as per DJB/NaCl "standard") - char polykey[ZT_POLY1305_KEY_LEN]; - memset(polykey,0,sizeof(polykey)); - s20.crypt12(polykey,polykey,sizeof(polykey)); - - // Compute 16-byte MAC - char mac[ZT_POLY1305_MAC_LEN]; - Poly1305::compute(mac,reinterpret_cast(msg) + 24,len - 24,polykey); - - // Check first 8 bytes of MAC against 64-bit MAC in stream - if (!Utils::secureEq(mac,reinterpret_cast(msg) + 16,8)) - return; - - // Decrypt! - dmsg.setSize(len - 24); - s20.crypt12(reinterpret_cast(msg) + 24,const_cast(dmsg.data()),dmsg.size()); - } - - if (dmsg.size() < 4) - return; - const uint16_t fromMemberId = dmsg.at(0); - unsigned int ptr = 2; - if (fromMemberId == _id) // sanity check: we don't talk to ourselves - return; - const uint16_t toMemberId = dmsg.at(ptr); - ptr += 2; - if (toMemberId != _id) // sanity check: message not for us? - return; - - { // make sure sender is actually considered a member - Mutex::Lock _l3(_memberIds_m); - if (std::find(_memberIds.begin(),_memberIds.end(),fromMemberId) == _memberIds.end()) - return; - } - - try { - while (ptr < dmsg.size()) { - const unsigned int mlen = dmsg.at(ptr); ptr += 2; - const unsigned int nextPtr = ptr + mlen; - if (nextPtr > dmsg.size()) - break; - - int mtype = -1; - try { - switch((StateMessageType)(mtype = (int)dmsg[ptr++])) { - default: - break; - - case CLUSTER_MESSAGE_ALIVE: { - _Member &m = _members[fromMemberId]; - Mutex::Lock mlck(m.lock); - ptr += 7; // skip version stuff, not used yet - m.x = dmsg.at(ptr); ptr += 4; - m.y = dmsg.at(ptr); ptr += 4; - m.z = dmsg.at(ptr); ptr += 4; - ptr += 8; // skip local clock, not used - m.load = dmsg.at(ptr); ptr += 8; - m.peers = dmsg.at(ptr); ptr += 8; - ptr += 8; // skip flags, unused -#ifdef ZT_TRACE - std::string addrs; -#endif - unsigned int physicalAddressCount = dmsg[ptr++]; - m.zeroTierPhysicalEndpoints.clear(); - for(unsigned int i=0;i 0) - addrs.push_back(','); - addrs.append(m.zeroTierPhysicalEndpoints.back().toString()); - } -#endif - } -#ifdef ZT_TRACE - if ((RR->node->now() - m.lastReceivedAliveAnnouncement) >= ZT_CLUSTER_TIMEOUT) { - TRACE("[%u] I'm alive! peers close to %d,%d,%d can be redirected to: %s",(unsigned int)fromMemberId,m.x,m.y,m.z,addrs.c_str()); - } -#endif - m.lastReceivedAliveAnnouncement = RR->node->now(); - } break; - - case CLUSTER_MESSAGE_HAVE_PEER: { - Identity id; - ptr += id.deserialize(dmsg,ptr); - if (id) { - { - Mutex::Lock _l(_remotePeers_m); - _RemotePeer &rp = _remotePeers[std::pair(id.address(),(unsigned int)fromMemberId)]; - if (!rp.lastHavePeerReceived) { - RR->topology->saveIdentity((void *)0,id); - RR->identity.agree(id,rp.key,ZT_PEER_SECRET_KEY_LENGTH); - } - rp.lastHavePeerReceived = RR->node->now(); - } - - _ClusterSendQueueEntry *q[16384]; // 16384 is "tons" - unsigned int qc = _sendQueue->getByDest(id.address(),q,16384); - for(unsigned int i=0;irelayViaCluster(q[i]->fromPeerAddress,q[i]->toPeerAddress,q[i]->data,q[i]->len,q[i]->unite); - _sendQueue->returnToPool(q,qc); - - TRACE("[%u] has %s (retried %u queued sends)",(unsigned int)fromMemberId,id.address().toString().c_str(),qc); - } - } break; - - case CLUSTER_MESSAGE_WANT_PEER: { - const Address zeroTierAddress(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; - SharedPtr peer(RR->topology->getPeerNoCache(zeroTierAddress)); - if ( (peer) && (peer->hasLocalClusterOptimalPath(RR->node->now())) ) { - Buffer<1024> buf; - peer->identity().serialize(buf); - Mutex::Lock _l2(_members[fromMemberId].lock); - _send(fromMemberId,CLUSTER_MESSAGE_HAVE_PEER,buf.data(),buf.size()); - } - } break; - - case CLUSTER_MESSAGE_REMOTE_PACKET: { - const unsigned int plen = dmsg.at(ptr); ptr += 2; - if (plen) { - Packet remotep(dmsg.field(ptr,plen),plen); ptr += plen; - //TRACE("remote %s from %s via %u (%u bytes)",Packet::verbString(remotep.verb()),remotep.source().toString().c_str(),fromMemberId,plen); - switch(remotep.verb()) { - case Packet::VERB_WHOIS: _doREMOTE_WHOIS(fromMemberId,remotep); break; - case Packet::VERB_MULTICAST_GATHER: _doREMOTE_MULTICAST_GATHER(fromMemberId,remotep); break; - default: break; // ignore things we don't care about across cluster - } - } - } break; - - case CLUSTER_MESSAGE_PROXY_UNITE: { - const Address localPeerAddress(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; - const Address remotePeerAddress(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; - const unsigned int numRemotePeerPaths = dmsg[ptr++]; - InetAddress remotePeerPaths[256]; // size is 8-bit, so 256 is max - for(unsigned int i=0;inode->now(); - SharedPtr localPeer(RR->topology->getPeerNoCache(localPeerAddress)); - if ((localPeer)&&(numRemotePeerPaths > 0)) { - InetAddress bestLocalV4,bestLocalV6; - localPeer->getRendezvousAddresses(now,bestLocalV4,bestLocalV6); - - InetAddress bestRemoteV4,bestRemoteV6; - for(unsigned int i=0;iidentity.address(),Packet::VERB_RENDEZVOUS); - rendezvousForLocal.append((uint8_t)0); - remotePeerAddress.appendTo(rendezvousForLocal); - - Buffer<2048> rendezvousForRemote; - remotePeerAddress.appendTo(rendezvousForRemote); - rendezvousForRemote.append((uint8_t)Packet::VERB_RENDEZVOUS); - rendezvousForRemote.addSize(2); // space for actual packet payload length - rendezvousForRemote.append((uint8_t)0); // flags == 0 - localPeerAddress.appendTo(rendezvousForRemote); - - bool haveMatch = false; - if ((bestLocalV6)&&(bestRemoteV6)) { - haveMatch = true; - - rendezvousForLocal.append((uint16_t)bestRemoteV6.port()); - rendezvousForLocal.append((uint8_t)16); - rendezvousForLocal.append(bestRemoteV6.rawIpData(),16); - - rendezvousForRemote.append((uint16_t)bestLocalV6.port()); - rendezvousForRemote.append((uint8_t)16); - rendezvousForRemote.append(bestLocalV6.rawIpData(),16); - rendezvousForRemote.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(9 + 16)); - } else if ((bestLocalV4)&&(bestRemoteV4)) { - haveMatch = true; - - rendezvousForLocal.append((uint16_t)bestRemoteV4.port()); - rendezvousForLocal.append((uint8_t)4); - rendezvousForLocal.append(bestRemoteV4.rawIpData(),4); - - rendezvousForRemote.append((uint16_t)bestLocalV4.port()); - rendezvousForRemote.append((uint8_t)4); - rendezvousForRemote.append(bestLocalV4.rawIpData(),4); - rendezvousForRemote.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(9 + 4)); - } - - if (haveMatch) { - { - Mutex::Lock _l2(_members[fromMemberId].lock); - _send(fromMemberId,CLUSTER_MESSAGE_PROXY_SEND,rendezvousForRemote.data(),rendezvousForRemote.size()); - } - RR->sw->send((void *)0,rendezvousForLocal,true); - } - } - } break; - - case CLUSTER_MESSAGE_PROXY_SEND: { - const Address rcpt(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; - const Packet::Verb verb = (Packet::Verb)dmsg[ptr++]; - const unsigned int len = dmsg.at(ptr); ptr += 2; - Packet outp(rcpt,RR->identity.address(),verb); - outp.append(dmsg.field(ptr,len),len); ptr += len; - RR->sw->send((void *)0,outp,true); - //TRACE("[%u] proxy send %s to %s length %u",(unsigned int)fromMemberId,Packet::verbString(verb),rcpt.toString().c_str(),len); - } break; - - case CLUSTER_MESSAGE_NETWORK_CONFIG: { - const SharedPtr network(RR->node->network(dmsg.at(ptr))); - if (network) { - // Copy into a Packet just to conform to Network API. Eventually - // will want to refactor. - network->handleConfigChunk((void *)0,0,Address(),Buffer(dmsg),ptr); - } - } break; - } - } catch ( ... ) { - TRACE("invalid message of size %u type %d (inner decode), discarding",mlen,mtype); - // drop invalids - } - - ptr = nextPtr; - } - } catch ( ... ) { - TRACE("invalid message (outer loop), discarding"); - // drop invalids - } -} - -void Cluster::broadcastHavePeer(const Identity &id) -{ - Buffer<1024> buf; - id.serialize(buf); - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - _send(*mid,CLUSTER_MESSAGE_HAVE_PEER,buf.data(),buf.size()); - } -} - -void Cluster::broadcastNetworkConfigChunk(const void *chunk,unsigned int len) -{ - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - _send(*mid,CLUSTER_MESSAGE_NETWORK_CONFIG,chunk,len); - } -} - -int Cluster::checkSendViaCluster(const Address &toPeerAddress,uint64_t &mostRecentTs,void *peerSecret) -{ - const uint64_t now = RR->node->now(); - mostRecentTs = 0; - int mostRecentMemberId = -1; - { - Mutex::Lock _l2(_remotePeers_m); - std::map< std::pair,_RemotePeer >::const_iterator rpe(_remotePeers.lower_bound(std::pair(toPeerAddress,0))); - for(;;) { - if ((rpe == _remotePeers.end())||(rpe->first.first != toPeerAddress)) - break; - else if (rpe->second.lastHavePeerReceived > mostRecentTs) { - mostRecentTs = rpe->second.lastHavePeerReceived; - memcpy(peerSecret,rpe->second.key,ZT_PEER_SECRET_KEY_LENGTH); - mostRecentMemberId = (int)rpe->first.second; - } - ++rpe; - } - } - - const uint64_t ageOfMostRecentHavePeerAnnouncement = now - mostRecentTs; - if (ageOfMostRecentHavePeerAnnouncement >= (ZT_PEER_ACTIVITY_TIMEOUT / 3)) { - if (ageOfMostRecentHavePeerAnnouncement >= ZT_PEER_ACTIVITY_TIMEOUT) - mostRecentMemberId = -1; - - bool sendWantPeer = true; - { - Mutex::Lock _l(_remotePeers_m); - _RemotePeer &rp = _remotePeers[std::pair(toPeerAddress,(unsigned int)_id)]; - if ((now - rp.lastSentWantPeer) >= ZT_CLUSTER_WANT_PEER_EVERY) { - rp.lastSentWantPeer = now; - } else { - sendWantPeer = false; // don't flood WANT_PEER - } - } - if (sendWantPeer) { - char tmp[ZT_ADDRESS_LENGTH]; - toPeerAddress.copyTo(tmp,ZT_ADDRESS_LENGTH); - { - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - _send(*mid,CLUSTER_MESSAGE_WANT_PEER,tmp,ZT_ADDRESS_LENGTH); - } - } - } - } - - return mostRecentMemberId; -} - -bool Cluster::sendViaCluster(int mostRecentMemberId,const Address &toPeerAddress,const void *data,unsigned int len) -{ - if ((mostRecentMemberId < 0)||(mostRecentMemberId >= ZT_CLUSTER_MAX_MEMBERS)) // sanity check - return false; - Mutex::Lock _l2(_members[mostRecentMemberId].lock); - for(std::vector::const_iterator i1(_zeroTierPhysicalEndpoints.begin());i1!=_zeroTierPhysicalEndpoints.end();++i1) { - for(std::vector::const_iterator i2(_members[mostRecentMemberId].zeroTierPhysicalEndpoints.begin());i2!=_members[mostRecentMemberId].zeroTierPhysicalEndpoints.end();++i2) { - if (i1->ss_family == i2->ss_family) { - TRACE("sendViaCluster sending %u bytes to %s by way of %u (%s->%s)",len,toPeerAddress.toString().c_str(),(unsigned int)mostRecentMemberId,i1->toString().c_str(),i2->toString().c_str()); - RR->node->putPacket((void *)0,*i1,*i2,data,len); - return true; - } - } - } - return false; -} - -void Cluster::relayViaCluster(const Address &fromPeerAddress,const Address &toPeerAddress,const void *data,unsigned int len,bool unite) -{ - if (len > ZT_PROTO_MAX_PACKET_LENGTH) // sanity check - return; - - const uint64_t now = RR->node->now(); - - uint64_t mostRecentTs = 0; - int mostRecentMemberId = -1; - { - Mutex::Lock _l2(_remotePeers_m); - std::map< std::pair,_RemotePeer >::const_iterator rpe(_remotePeers.lower_bound(std::pair(toPeerAddress,0))); - for(;;) { - if ((rpe == _remotePeers.end())||(rpe->first.first != toPeerAddress)) - break; - else if (rpe->second.lastHavePeerReceived > mostRecentTs) { - mostRecentTs = rpe->second.lastHavePeerReceived; - mostRecentMemberId = (int)rpe->first.second; - } - ++rpe; - } - } - - const uint64_t ageOfMostRecentHavePeerAnnouncement = now - mostRecentTs; - if (ageOfMostRecentHavePeerAnnouncement >= (ZT_PEER_ACTIVITY_TIMEOUT / 3)) { - // Enqueue and wait if peer seems alive, but do WANT_PEER to refresh homing - const bool enqueueAndWait = ((ageOfMostRecentHavePeerAnnouncement >= ZT_PEER_ACTIVITY_TIMEOUT)||(mostRecentMemberId < 0)); - - // Poll everyone with WANT_PEER if the age of our most recent entry is - // approaching expiration (or has expired, or does not exist). - bool sendWantPeer = true; - { - Mutex::Lock _l(_remotePeers_m); - _RemotePeer &rp = _remotePeers[std::pair(toPeerAddress,(unsigned int)_id)]; - if ((now - rp.lastSentWantPeer) >= ZT_CLUSTER_WANT_PEER_EVERY) { - rp.lastSentWantPeer = now; - } else { - sendWantPeer = false; // don't flood WANT_PEER - } - } - if (sendWantPeer) { - char tmp[ZT_ADDRESS_LENGTH]; - toPeerAddress.copyTo(tmp,ZT_ADDRESS_LENGTH); - { - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - _send(*mid,CLUSTER_MESSAGE_WANT_PEER,tmp,ZT_ADDRESS_LENGTH); - } - } - } - - // If there isn't a good place to send via, then enqueue this for retrying - // later and return after having broadcasted a WANT_PEER. - if (enqueueAndWait) { - TRACE("relayViaCluster %s -> %s enqueueing to wait for HAVE_PEER",fromPeerAddress.toString().c_str(),toPeerAddress.toString().c_str()); - _sendQueue->enqueue(now,fromPeerAddress,toPeerAddress,data,len,unite); - return; - } - } - - if (mostRecentMemberId >= 0) { - Buffer<1024> buf; - if (unite) { - InetAddress v4,v6; - if (fromPeerAddress) { - SharedPtr fromPeer(RR->topology->getPeerNoCache(fromPeerAddress)); - if (fromPeer) - fromPeer->getRendezvousAddresses(now,v4,v6); - } - uint8_t addrCount = 0; - if (v4) - ++addrCount; - if (v6) - ++addrCount; - if (addrCount) { - toPeerAddress.appendTo(buf); - fromPeerAddress.appendTo(buf); - buf.append(addrCount); - if (v4) - v4.serialize(buf); - if (v6) - v6.serialize(buf); - } - } - - { - Mutex::Lock _l2(_members[mostRecentMemberId].lock); - if (buf.size() > 0) - _send(mostRecentMemberId,CLUSTER_MESSAGE_PROXY_UNITE,buf.data(),buf.size()); - - for(std::vector::const_iterator i1(_zeroTierPhysicalEndpoints.begin());i1!=_zeroTierPhysicalEndpoints.end();++i1) { - for(std::vector::const_iterator i2(_members[mostRecentMemberId].zeroTierPhysicalEndpoints.begin());i2!=_members[mostRecentMemberId].zeroTierPhysicalEndpoints.end();++i2) { - if (i1->ss_family == i2->ss_family) { - TRACE("relayViaCluster relaying %u bytes from %s to %s by way of %u (%s->%s)",len,fromPeerAddress.toString().c_str(),toPeerAddress.toString().c_str(),(unsigned int)mostRecentMemberId,i1->toString().c_str(),i2->toString().c_str()); - RR->node->putPacket((void *)0,*i1,*i2,data,len); - return; - } - } - } - - TRACE("relayViaCluster relaying %u bytes from %s to %s by way of %u failed: no common endpoints with the same address family!",len,fromPeerAddress.toString().c_str(),toPeerAddress.toString().c_str(),(unsigned int)mostRecentMemberId); - } - } -} - -void Cluster::sendDistributedQuery(const Packet &pkt) -{ - Buffer<4096> buf; - buf.append((uint16_t)pkt.size()); - buf.append(pkt.data(),pkt.size()); - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - _send(*mid,CLUSTER_MESSAGE_REMOTE_PACKET,buf.data(),buf.size()); - } -} - -void Cluster::doPeriodicTasks() -{ - const uint64_t now = RR->node->now(); - - if ((now - _lastFlushed) >= ZT_CLUSTER_FLUSH_PERIOD) { - _lastFlushed = now; - - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - - if ((now - _members[*mid].lastAnnouncedAliveTo) >= ((ZT_CLUSTER_TIMEOUT / 2) - 1000)) { - _members[*mid].lastAnnouncedAliveTo = now; - - Buffer<2048> alive; - alive.append((uint16_t)ZEROTIER_ONE_VERSION_MAJOR); - alive.append((uint16_t)ZEROTIER_ONE_VERSION_MINOR); - alive.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION); - alive.append((uint8_t)ZT_PROTO_VERSION); - if (_addressToLocationFunction) { - alive.append((int32_t)_x); - alive.append((int32_t)_y); - alive.append((int32_t)_z); - } else { - alive.append((int32_t)0); - alive.append((int32_t)0); - alive.append((int32_t)0); - } - alive.append((uint64_t)now); - alive.append((uint64_t)0); // TODO: compute and send load average - alive.append((uint64_t)RR->topology->countActive(now)); - alive.append((uint64_t)0); // unused/reserved flags - alive.append((uint8_t)_zeroTierPhysicalEndpoints.size()); - for(std::vector::const_iterator pe(_zeroTierPhysicalEndpoints.begin());pe!=_zeroTierPhysicalEndpoints.end();++pe) - pe->serialize(alive); - _send(*mid,CLUSTER_MESSAGE_ALIVE,alive.data(),alive.size()); - } - - _flush(*mid); - } - } - - if ((now - _lastCleanedRemotePeers) >= (ZT_PEER_ACTIVITY_TIMEOUT * 2)) { - _lastCleanedRemotePeers = now; - - Mutex::Lock _l(_remotePeers_m); - for(std::map< std::pair,_RemotePeer >::iterator rp(_remotePeers.begin());rp!=_remotePeers.end();) { - if ((now - rp->second.lastHavePeerReceived) >= ZT_PEER_ACTIVITY_TIMEOUT) - _remotePeers.erase(rp++); - else ++rp; - } - } - - if ((now - _lastCleanedQueue) >= ZT_CLUSTER_QUEUE_EXPIRATION) { - _lastCleanedQueue = now; - _sendQueue->expire(now); - } -} - -void Cluster::addMember(uint16_t memberId) -{ - if ((memberId >= ZT_CLUSTER_MAX_MEMBERS)||(memberId == _id)) - return; - - Mutex::Lock _l2(_members[memberId].lock); - - { - Mutex::Lock _l(_memberIds_m); - if (std::find(_memberIds.begin(),_memberIds.end(),memberId) != _memberIds.end()) - return; - _memberIds.push_back(memberId); - std::sort(_memberIds.begin(),_memberIds.end()); - } - - _members[memberId].clear(); - - // Generate this member's message key from the master and its ID - uint16_t stmp[ZT_SHA512_DIGEST_LEN / sizeof(uint16_t)]; - memcpy(stmp,_masterSecret,sizeof(stmp)); - stmp[0] ^= Utils::hton(memberId); - SHA512::hash(stmp,stmp,sizeof(stmp)); - SHA512::hash(stmp,stmp,sizeof(stmp)); - memcpy(_members[memberId].key,stmp,sizeof(_members[memberId].key)); - Utils::burn(stmp,sizeof(stmp)); - - // Prepare q - _members[memberId].q.clear(); - char iv[16]; - Utils::getSecureRandom(iv,16); - _members[memberId].q.append(iv,16); - _members[memberId].q.addSize(8); // room for MAC - _members[memberId].q.append((uint16_t)_id); - _members[memberId].q.append((uint16_t)memberId); -} - -void Cluster::removeMember(uint16_t memberId) -{ - Mutex::Lock _l(_memberIds_m); - std::vector newMemberIds; - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - if (*mid != memberId) - newMemberIds.push_back(*mid); - } - _memberIds = newMemberIds; -} - -bool Cluster::findBetterEndpoint(InetAddress &redirectTo,const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload) -{ - if (_addressToLocationFunction) { - // Pick based on location if it can be determined - int px = 0,py = 0,pz = 0; - if (_addressToLocationFunction(_addressToLocationFunctionArg,reinterpret_cast(&peerPhysicalAddress),&px,&py,&pz) == 0) { - TRACE("no geolocation data for %s",peerPhysicalAddress.toIpString().c_str()); - return false; - } - - // Find member closest to this peer - const uint64_t now = RR->node->now(); - std::vector best; - const double currentDistance = _dist3d(_x,_y,_z,px,py,pz); - double bestDistance = (offload ? 2147483648.0 : currentDistance); -#ifdef ZT_TRACE - unsigned int bestMember = _id; -#endif - { - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - _Member &m = _members[*mid]; - Mutex::Lock _ml(m.lock); - - // Consider member if it's alive and has sent us a location and one or more physical endpoints to send peers to - if ( ((now - m.lastReceivedAliveAnnouncement) < ZT_CLUSTER_TIMEOUT) && ((m.x != 0)||(m.y != 0)||(m.z != 0)) && (m.zeroTierPhysicalEndpoints.size() > 0) ) { - const double mdist = _dist3d(m.x,m.y,m.z,px,py,pz); - if (mdist < bestDistance) { - bestDistance = mdist; -#ifdef ZT_TRACE - bestMember = *mid; -#endif - best = m.zeroTierPhysicalEndpoints; - } - } - } - } - - // Redirect to a closer member if it has a ZeroTier endpoint address in the same ss_family - for(std::vector::const_iterator a(best.begin());a!=best.end();++a) { - if (a->ss_family == peerPhysicalAddress.ss_family) { - TRACE("%s at [%d,%d,%d] is %f from us but %f from %u, can redirect to %s",peerAddress.toString().c_str(),px,py,pz,currentDistance,bestDistance,bestMember,a->toString().c_str()); - redirectTo = *a; - return true; - } - } - TRACE("%s at [%d,%d,%d] is %f from us, no better endpoints found",peerAddress.toString().c_str(),px,py,pz,currentDistance); - return false; - } else { - // TODO: pick based on load if no location info? - return false; - } -} - -bool Cluster::isClusterPeerFrontplane(const InetAddress &ip) const -{ - Mutex::Lock _l(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - Mutex::Lock _l2(_members[*mid].lock); - for(std::vector::const_iterator i2(_members[*mid].zeroTierPhysicalEndpoints.begin());i2!=_members[*mid].zeroTierPhysicalEndpoints.end();++i2) { - if (ip == *i2) - return true; - } - } - return false; -} - -void Cluster::status(ZT_ClusterStatus &status) const -{ - const uint64_t now = RR->node->now(); - memset(&status,0,sizeof(ZT_ClusterStatus)); - - status.myId = _id; - - { - ZT_ClusterMemberStatus *const s = &(status.members[status.clusterSize++]); - s->id = _id; - s->alive = 1; - s->x = _x; - s->y = _y; - s->z = _z; - s->load = 0; // TODO - s->peers = RR->topology->countActive(now); - for(std::vector::const_iterator ep(_zeroTierPhysicalEndpoints.begin());ep!=_zeroTierPhysicalEndpoints.end();++ep) { - if (s->numZeroTierPhysicalEndpoints >= ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES) // sanity check - break; - memcpy(&(s->zeroTierPhysicalEndpoints[s->numZeroTierPhysicalEndpoints++]),&(*ep),sizeof(struct sockaddr_storage)); - } - } - - { - Mutex::Lock _l1(_memberIds_m); - for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { - if (status.clusterSize >= ZT_CLUSTER_MAX_MEMBERS) // sanity check - break; - - _Member &m = _members[*mid]; - Mutex::Lock ml(m.lock); - - ZT_ClusterMemberStatus *const s = &(status.members[status.clusterSize++]); - s->id = *mid; - s->msSinceLastHeartbeat = (unsigned int)std::min((uint64_t)(~((unsigned int)0)),(now - m.lastReceivedAliveAnnouncement)); - s->alive = (s->msSinceLastHeartbeat < ZT_CLUSTER_TIMEOUT) ? 1 : 0; - s->x = m.x; - s->y = m.y; - s->z = m.z; - s->load = m.load; - s->peers = m.peers; - for(std::vector::const_iterator ep(m.zeroTierPhysicalEndpoints.begin());ep!=m.zeroTierPhysicalEndpoints.end();++ep) { - if (s->numZeroTierPhysicalEndpoints >= ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES) // sanity check - break; - memcpy(&(s->zeroTierPhysicalEndpoints[s->numZeroTierPhysicalEndpoints++]),&(*ep),sizeof(struct sockaddr_storage)); - } - } - } -} - -void Cluster::_send(uint16_t memberId,StateMessageType type,const void *msg,unsigned int len) -{ - if ((len + 3) > (ZT_CLUSTER_MAX_MESSAGE_LENGTH - (24 + 2 + 2))) // sanity check - return; - _Member &m = _members[memberId]; - // assumes m.lock is locked! - if ((m.q.size() + len + 3) > ZT_CLUSTER_MAX_MESSAGE_LENGTH) - _flush(memberId); - m.q.append((uint16_t)(len + 1)); - m.q.append((uint8_t)type); - m.q.append(msg,len); -} - -void Cluster::_flush(uint16_t memberId) -{ - _Member &m = _members[memberId]; - // assumes m.lock is locked! - if (m.q.size() > (24 + 2 + 2)) { // 16-byte IV + 8-byte MAC + 2 byte from-member-ID + 2 byte to-member-ID - // Create key from member's key and IV - char keytmp[32]; - memcpy(keytmp,m.key,32); - for(int i=0;i<8;++i) - keytmp[i] ^= m.q[i]; - Salsa20 s20(keytmp,m.q.field(8,8)); - Utils::burn(keytmp,sizeof(keytmp)); - - // One-time-use Poly1305 key from first 32 bytes of Salsa20 keystream (as per DJB/NaCl "standard") - char polykey[ZT_POLY1305_KEY_LEN]; - memset(polykey,0,sizeof(polykey)); - s20.crypt12(polykey,polykey,sizeof(polykey)); - - // Encrypt m.q in place - s20.crypt12(reinterpret_cast(m.q.data()) + 24,const_cast(reinterpret_cast(m.q.data())) + 24,m.q.size() - 24); - - // Add MAC for authentication (encrypt-then-MAC) - char mac[ZT_POLY1305_MAC_LEN]; - Poly1305::compute(mac,reinterpret_cast(m.q.data()) + 24,m.q.size() - 24,polykey); - memcpy(m.q.field(16,8),mac,8); - - // Send! - _sendFunction(_sendFunctionArg,memberId,m.q.data(),m.q.size()); - - // Prepare for more - m.q.clear(); - char iv[16]; - Utils::getSecureRandom(iv,16); - m.q.append(iv,16); - m.q.addSize(8); // room for MAC - m.q.append((uint16_t)_id); // from member ID - m.q.append((uint16_t)memberId); // to member ID - } -} - -void Cluster::_doREMOTE_WHOIS(uint64_t fromMemberId,const Packet &remotep) -{ - if (remotep.payloadLength() >= ZT_ADDRESS_LENGTH) { - Identity queried(RR->topology->getIdentity((void *)0,Address(remotep.payload(),ZT_ADDRESS_LENGTH))); - if (queried) { - Buffer<1024> routp; - remotep.source().appendTo(routp); - routp.append((uint8_t)Packet::VERB_OK); - routp.addSize(2); // space for length - routp.append((uint8_t)Packet::VERB_WHOIS); - routp.append(remotep.packetId()); - queried.serialize(routp); - routp.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(routp.size() - ZT_ADDRESS_LENGTH - 3)); - - TRACE("responding to remote WHOIS from %s @ %u with identity of %s",remotep.source().toString().c_str(),(unsigned int)fromMemberId,queried.address().toString().c_str()); - Mutex::Lock _l2(_members[fromMemberId].lock); - _send(fromMemberId,CLUSTER_MESSAGE_PROXY_SEND,routp.data(),routp.size()); - } - } -} - -void Cluster::_doREMOTE_MULTICAST_GATHER(uint64_t fromMemberId,const Packet &remotep) -{ - const uint64_t nwid = remotep.at(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID); - const MulticastGroup mg(MAC(remotep.field(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC,6),6),remotep.at(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI)); - unsigned int gatherLimit = remotep.at(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_GATHER_LIMIT); - const Address remotePeerAddress(remotep.source()); - - if (gatherLimit) { - Buffer routp; - remotePeerAddress.appendTo(routp); - routp.append((uint8_t)Packet::VERB_OK); - routp.addSize(2); // space for length - routp.append((uint8_t)Packet::VERB_MULTICAST_GATHER); - routp.append(remotep.packetId()); - routp.append(nwid); - mg.mac().appendTo(routp); - routp.append((uint32_t)mg.adi()); - - if (gatherLimit > ((ZT_CLUSTER_MAX_MESSAGE_LENGTH - 80) / 5)) - gatherLimit = ((ZT_CLUSTER_MAX_MESSAGE_LENGTH - 80) / 5); - if (RR->mc->gather(remotePeerAddress,nwid,mg,routp,gatherLimit)) { - routp.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(routp.size() - ZT_ADDRESS_LENGTH - 3)); - - TRACE("responding to remote MULTICAST_GATHER from %s @ %u with %u bytes",remotePeerAddress.toString().c_str(),(unsigned int)fromMemberId,routp.size()); - Mutex::Lock _l2(_members[fromMemberId].lock); - _send(fromMemberId,CLUSTER_MESSAGE_PROXY_SEND,routp.data(),routp.size()); - } - } -} - -} // namespace ZeroTier - -#endif // ZT_ENABLE_CLUSTER diff --git a/Cluster.hpp b/Cluster.hpp deleted file mode 100644 index 74b091f5..00000000 --- a/Cluster.hpp +++ /dev/null @@ -1,463 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - * - * -- - * - * You can be released from the requirements of the license by purchasing - * a commercial license. Buying such a license is mandatory as soon as you - * develop commercial closed-source software that incorporates or links - * directly against ZeroTier software without disclosing the source code - * of your own application. - */ - -#ifndef ZT_CLUSTER_HPP -#define ZT_CLUSTER_HPP - -#ifdef ZT_ENABLE_CLUSTER - -#include - -#include "Constants.hpp" -#include "../include/ZeroTierOne.h" -#include "Address.hpp" -#include "InetAddress.hpp" -#include "SHA512.hpp" -#include "Utils.hpp" -#include "Buffer.hpp" -#include "Mutex.hpp" -#include "SharedPtr.hpp" -#include "Hashtable.hpp" -#include "Packet.hpp" -#include "SharedPtr.hpp" - -/** - * Timeout for cluster members being considered "alive" - * - * A cluster member is considered dead and will no longer have peers - * redirected to it if we have not heard a heartbeat in this long. - */ -#define ZT_CLUSTER_TIMEOUT 5000 - -/** - * Desired period between doPeriodicTasks() in milliseconds - */ -#define ZT_CLUSTER_PERIODIC_TASK_PERIOD 20 - -/** - * How often to flush outgoing message queues (maximum interval) - */ -#define ZT_CLUSTER_FLUSH_PERIOD ZT_CLUSTER_PERIODIC_TASK_PERIOD - -/** - * Maximum number of queued outgoing packets per sender address - */ -#define ZT_CLUSTER_MAX_QUEUE_PER_SENDER 16 - -/** - * Expiration time for send queue entries - */ -#define ZT_CLUSTER_QUEUE_EXPIRATION 3000 - -/** - * Chunk size for allocating queue entries - * - * Queue entries are allocated in chunks of this many and are added to a pool. - * ZT_CLUSTER_MAX_QUEUE_GLOBAL must be evenly divisible by this. - */ -#define ZT_CLUSTER_QUEUE_CHUNK_SIZE 32 - -/** - * Maximum number of chunks to ever allocate - * - * This is a global sanity limit to prevent resource exhaustion attacks. It - * works out to about 600mb of RAM. You'll never see this on a normal edge - * node. We're unlikely to see this on a root server unless someone is DOSing - * us. In that case cluster relaying will be affected but other functions - * should continue to operate normally. - */ -#define ZT_CLUSTER_MAX_QUEUE_CHUNKS 8194 - -/** - * Max data per queue entry - */ -#define ZT_CLUSTER_SEND_QUEUE_DATA_MAX 1500 - -/** - * We won't send WANT_PEER to other members more than every (ms) per recipient - */ -#define ZT_CLUSTER_WANT_PEER_EVERY 1000 - -namespace ZeroTier { - -class RuntimeEnvironment; -class MulticastGroup; -class Peer; -class Identity; - -// Internal class implemented inside Cluster.cpp -class _ClusterSendQueue; - -/** - * Multi-homing cluster state replication and packet relaying - * - * Multi-homing means more than one node sharing the same ZeroTier identity. - * There is nothing in the protocol to prevent this, but to make it work well - * requires the devices sharing an identity to cooperate and share some - * information. - * - * There are three use cases we want to fulfill: - * - * (1) Multi-homing of root servers with handoff for efficient routing, - * HA, and load balancing across many commodity nodes. - * (2) Multi-homing of network controllers for the same reason. - * (3) Multi-homing of nodes on virtual networks, such as domain servers - * and other important endpoints. - * - * These use cases are in order of escalating difficulty. The initial - * version of Cluster is aimed at satisfying the first, though you are - * free to try #2 and #3. - */ -class Cluster -{ -public: - /** - * State message types - */ - enum StateMessageType - { - CLUSTER_MESSAGE_NOP = 0, - - /** - * This cluster member is alive: - * <[2] version minor> - * <[2] version major> - * <[2] version revision> - * <[1] protocol version> - * <[4] X location (signed 32-bit)> - * <[4] Y location (signed 32-bit)> - * <[4] Z location (signed 32-bit)> - * <[8] local clock at this member> - * <[8] load average> - * <[8] number of peers> - * <[8] flags (currently unused, must be zero)> - * <[1] number of preferred ZeroTier endpoints> - * <[...] InetAddress(es) of preferred ZeroTier endpoint(s)> - * - * Cluster members constantly broadcast an alive heartbeat and will only - * receive peer redirects if they've done so within the timeout. - */ - CLUSTER_MESSAGE_ALIVE = 1, - - /** - * Cluster member has this peer: - * <[...] serialized identity of peer> - * - * This is typically sent in response to WANT_PEER but can also be pushed - * to prepopulate if this makes sense. - */ - CLUSTER_MESSAGE_HAVE_PEER = 2, - - /** - * Cluster member wants this peer: - * <[5] ZeroTier address of peer> - * - * Members that have a direct link to this peer will respond with - * HAVE_PEER. - */ - CLUSTER_MESSAGE_WANT_PEER = 3, - - /** - * A remote packet that we should also possibly respond to: - * <[2] 16-bit length of remote packet> - * <[...] remote packet payload> - * - * Cluster members may relay requests by relaying the request packet. - * These may include requests such as WHOIS and MULTICAST_GATHER. The - * packet must be already decrypted, decompressed, and authenticated. - * - * This can only be used for small request packets as per the cluster - * message size limit, but since these are the only ones in question - * this is fine. - * - * If a response is generated it is sent via PROXY_SEND. - */ - CLUSTER_MESSAGE_REMOTE_PACKET = 4, - - /** - * Request that VERB_RENDEZVOUS be sent to a peer that we have: - * <[5] ZeroTier address of peer on recipient's side> - * <[5] ZeroTier address of peer on sender's side> - * <[1] 8-bit number of sender's peer's active path addresses> - * <[...] series of serialized InetAddresses of sender's peer's paths> - * - * This requests that we perform NAT-t introduction between a peer that - * we have and one on the sender's side. The sender furnishes contact - * info for its peer, and we send VERB_RENDEZVOUS to both sides: to ours - * directly and with PROXY_SEND to theirs. - */ - CLUSTER_MESSAGE_PROXY_UNITE = 5, - - /** - * Request that a cluster member send a packet to a locally-known peer: - * <[5] ZeroTier address of recipient> - * <[1] packet verb> - * <[2] length of packet payload> - * <[...] packet payload> - * - * This differs from RELAY in that it requests the receiving cluster - * member to actually compose a ZeroTier Packet from itself to the - * provided recipient. RELAY simply says "please forward this blob." - * RELAY is used to implement peer-to-peer relaying with RENDEZVOUS, - * while PROXY_SEND is used to implement proxy sending (which right - * now is only used to send RENDEZVOUS). - */ - CLUSTER_MESSAGE_PROXY_SEND = 6, - - /** - * Replicate a network config for a network we belong to: - * <[...] network config chunk> - * - * This is used by clusters to avoid every member having to query - * for the same netconf for networks all members belong to. - * - * The first field of a network config chunk is the network ID, - * so this can be checked to look up the network on receipt. - */ - CLUSTER_MESSAGE_NETWORK_CONFIG = 7 - }; - - /** - * Construct a new cluster - */ - Cluster( - const RuntimeEnvironment *renv, - uint16_t id, - const std::vector &zeroTierPhysicalEndpoints, - int32_t x, - int32_t y, - int32_t z, - void (*sendFunction)(void *,unsigned int,const void *,unsigned int), - void *sendFunctionArg, - int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *), - void *addressToLocationFunctionArg); - - ~Cluster(); - - /** - * @return This cluster member's ID - */ - inline uint16_t id() const throw() { return _id; } - - /** - * Handle an incoming intra-cluster message - * - * @param data Message data - * @param len Message length (max: ZT_CLUSTER_MAX_MESSAGE_LENGTH) - */ - void handleIncomingStateMessage(const void *msg,unsigned int len); - - /** - * Broadcast that we have a given peer - * - * This should be done when new peers are first contacted. - * - * @param id Identity of peer - */ - void broadcastHavePeer(const Identity &id); - - /** - * Broadcast a network config chunk to other members of cluster - * - * @param chunk Chunk data - * @param len Length of chunk - */ - void broadcastNetworkConfigChunk(const void *chunk,unsigned int len); - - /** - * If the cluster has this peer, prepare the packet to send via cluster - * - * Note that outp is only armored (or modified at all) if the return value is a member ID. - * - * @param toPeerAddress Value of outp.destination(), simply to save additional lookup - * @param ts Result: set to time of last HAVE_PEER from the cluster - * @param peerSecret Result: Buffer to fill with peer secret on valid return value, must be at least ZT_PEER_SECRET_KEY_LENGTH bytes - * @return -1 if cluster does not know this peer, or a member ID to pass to sendViaCluster() - */ - int checkSendViaCluster(const Address &toPeerAddress,uint64_t &mostRecentTs,void *peerSecret); - - /** - * Send data via cluster front plane (packet head or fragment) - * - * @param haveMemberId Member ID that has this peer as returned by prepSendviaCluster() - * @param toPeerAddress Destination peer address - * @param data Packet or packet fragment data - * @param len Length of packet or fragment - * @return True if packet was sent (and outp was modified via armoring) - */ - bool sendViaCluster(int haveMemberId,const Address &toPeerAddress,const void *data,unsigned int len); - - /** - * Relay a packet via the cluster - * - * This is used in the outgoing packet and relaying logic in Switch to - * relay packets to other cluster members. It isn't PROXY_SEND-- that is - * used internally in Cluster to send responses to peer queries. - * - * @param fromPeerAddress Source peer address (if known, should be NULL for fragments) - * @param toPeerAddress Destination peer address - * @param data Packet or packet fragment data - * @param len Length of packet or fragment - * @param unite If true, also request proxy unite across cluster - */ - void relayViaCluster(const Address &fromPeerAddress,const Address &toPeerAddress,const void *data,unsigned int len,bool unite); - - /** - * Send a distributed query to other cluster members - * - * Some queries such as WHOIS or MULTICAST_GATHER need a response from other - * cluster members. Replies (if any) will be sent back to the peer via - * PROXY_SEND across the cluster. - * - * @param pkt Packet to distribute - */ - void sendDistributedQuery(const Packet &pkt); - - /** - * Call every ~ZT_CLUSTER_PERIODIC_TASK_PERIOD milliseconds. - */ - void doPeriodicTasks(); - - /** - * Add a member ID to this cluster - * - * @param memberId Member ID - */ - void addMember(uint16_t memberId); - - /** - * Remove a member ID from this cluster - * - * @param memberId Member ID to remove - */ - void removeMember(uint16_t memberId); - - /** - * Find a better cluster endpoint for this peer (if any) - * - * @param redirectTo InetAddress to be set to a better endpoint (if there is one) - * @param peerAddress Address of peer to (possibly) redirect - * @param peerPhysicalAddress Physical address of peer's current best path (where packet was most recently received or getBestPath()->address()) - * @param offload Always redirect if possible -- can be used to offload peers during shutdown - * @return True if redirectTo was set to a new address, false if redirectTo was not modified - */ - bool findBetterEndpoint(InetAddress &redirectTo,const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload); - - /** - * @param ip Address to check - * @return True if this is a cluster frontplane address (excluding our addresses) - */ - bool isClusterPeerFrontplane(const InetAddress &ip) const; - - /** - * Fill out ZT_ClusterStatus structure (from core API) - * - * @param status Reference to structure to hold result (anything there is replaced) - */ - void status(ZT_ClusterStatus &status) const; - -private: - void _send(uint16_t memberId,StateMessageType type,const void *msg,unsigned int len); - void _flush(uint16_t memberId); - - void _doREMOTE_WHOIS(uint64_t fromMemberId,const Packet &remotep); - void _doREMOTE_MULTICAST_GATHER(uint64_t fromMemberId,const Packet &remotep); - - // These are initialized in the constructor and remain immutable ------------ - uint16_t _masterSecret[ZT_SHA512_DIGEST_LEN / sizeof(uint16_t)]; - unsigned char _key[ZT_PEER_SECRET_KEY_LENGTH]; - const RuntimeEnvironment *RR; - _ClusterSendQueue *const _sendQueue; - void (*_sendFunction)(void *,unsigned int,const void *,unsigned int); - void *_sendFunctionArg; - int (*_addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *); - void *_addressToLocationFunctionArg; - const int32_t _x; - const int32_t _y; - const int32_t _z; - const uint16_t _id; - const std::vector _zeroTierPhysicalEndpoints; - // end immutable fields ----------------------------------------------------- - - struct _Member - { - unsigned char key[ZT_PEER_SECRET_KEY_LENGTH]; - - uint64_t lastReceivedAliveAnnouncement; - uint64_t lastAnnouncedAliveTo; - - uint64_t load; - uint64_t peers; - int32_t x,y,z; - - std::vector zeroTierPhysicalEndpoints; - - Buffer q; - - Mutex lock; - - inline void clear() - { - lastReceivedAliveAnnouncement = 0; - lastAnnouncedAliveTo = 0; - load = 0; - peers = 0; - x = 0; - y = 0; - z = 0; - zeroTierPhysicalEndpoints.clear(); - q.clear(); - } - - _Member() { this->clear(); } - ~_Member() { Utils::burn(key,sizeof(key)); } - }; - _Member *const _members; - - std::vector _memberIds; - Mutex _memberIds_m; - - struct _RemotePeer - { - _RemotePeer() : lastHavePeerReceived(0),lastSentWantPeer(0) {} - ~_RemotePeer() { Utils::burn(key,ZT_PEER_SECRET_KEY_LENGTH); } - uint64_t lastHavePeerReceived; - uint64_t lastSentWantPeer; - uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]; // secret key from identity agreement - }; - std::map< std::pair,_RemotePeer > _remotePeers; // we need ordered behavior and lower_bound here - Mutex _remotePeers_m; - - uint64_t _lastFlushed; - uint64_t _lastCleanedRemotePeers; - uint64_t _lastCleanedQueue; -}; - -} // namespace ZeroTier - -#endif // ZT_ENABLE_CLUSTER - -#endif diff --git a/attic/Cluster.cpp b/attic/Cluster.cpp new file mode 100644 index 00000000..119aec29 --- /dev/null +++ b/attic/Cluster.cpp @@ -0,0 +1,1042 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifdef ZT_ENABLE_CLUSTER + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "../version.h" + +#include "Cluster.hpp" +#include "RuntimeEnvironment.hpp" +#include "MulticastGroup.hpp" +#include "CertificateOfMembership.hpp" +#include "Salsa20.hpp" +#include "Poly1305.hpp" +#include "Identity.hpp" +#include "Topology.hpp" +#include "Packet.hpp" +#include "Switch.hpp" +#include "Node.hpp" +#include "Network.hpp" +#include "Array.hpp" + +namespace ZeroTier { + +static inline double _dist3d(int x1,int y1,int z1,int x2,int y2,int z2) + throw() +{ + double dx = ((double)x2 - (double)x1); + double dy = ((double)y2 - (double)y1); + double dz = ((double)z2 - (double)z1); + return sqrt((dx * dx) + (dy * dy) + (dz * dz)); +} + +// An entry in _ClusterSendQueue +struct _ClusterSendQueueEntry +{ + uint64_t timestamp; + Address fromPeerAddress; + Address toPeerAddress; + // if we ever support larger transport MTUs this must be increased + unsigned char data[ZT_CLUSTER_SEND_QUEUE_DATA_MAX]; + unsigned int len; + bool unite; +}; + +// A multi-index map with entry memory pooling -- this allows our queue to +// be O(log(N)) and is complex enough that it makes the code a lot cleaner +// to break it out from Cluster. +class _ClusterSendQueue +{ +public: + _ClusterSendQueue() : + _poolCount(0) {} + ~_ClusterSendQueue() {} // memory is automatically freed when _chunks is destroyed + + inline void enqueue(uint64_t now,const Address &from,const Address &to,const void *data,unsigned int len,bool unite) + { + if (len > ZT_CLUSTER_SEND_QUEUE_DATA_MAX) + return; + + Mutex::Lock _l(_lock); + + // Delete oldest queue entry for this sender if this enqueue() would take them over the per-sender limit + { + std::set< std::pair >::iterator qi(_bySrc.lower_bound(std::pair(from,(_ClusterSendQueueEntry *)0))); + std::set< std::pair >::iterator oldest(qi); + unsigned long countForSender = 0; + while ((qi != _bySrc.end())&&(qi->first == from)) { + if (qi->second->timestamp < oldest->second->timestamp) + oldest = qi; + ++countForSender; + ++qi; + } + if (countForSender >= ZT_CLUSTER_MAX_QUEUE_PER_SENDER) { + _byDest.erase(std::pair(oldest->second->toPeerAddress,oldest->second)); + _pool[_poolCount++] = oldest->second; + _bySrc.erase(oldest); + } + } + + _ClusterSendQueueEntry *e; + if (_poolCount > 0) { + e = _pool[--_poolCount]; + } else { + if (_chunks.size() >= ZT_CLUSTER_MAX_QUEUE_CHUNKS) + return; // queue is totally full! + _chunks.push_back(Array<_ClusterSendQueueEntry,ZT_CLUSTER_QUEUE_CHUNK_SIZE>()); + e = &(_chunks.back().data[0]); + for(unsigned int i=1;itimestamp = now; + e->fromPeerAddress = from; + e->toPeerAddress = to; + memcpy(e->data,data,len); + e->len = len; + e->unite = unite; + + _bySrc.insert(std::pair(from,e)); + _byDest.insert(std::pair(to,e)); + } + + inline void expire(uint64_t now) + { + Mutex::Lock _l(_lock); + for(std::set< std::pair >::iterator qi(_bySrc.begin());qi!=_bySrc.end();) { + if ((now - qi->second->timestamp) > ZT_CLUSTER_QUEUE_EXPIRATION) { + _byDest.erase(std::pair(qi->second->toPeerAddress,qi->second)); + _pool[_poolCount++] = qi->second; + _bySrc.erase(qi++); + } else ++qi; + } + } + + /** + * Get and dequeue entries for a given destination address + * + * After use these entries must be returned with returnToPool()! + * + * @param dest Destination address + * @param results Array to fill with results + * @param maxResults Size of results[] in pointers + * @return Number of actual results returned + */ + inline unsigned int getByDest(const Address &dest,_ClusterSendQueueEntry **results,unsigned int maxResults) + { + unsigned int count = 0; + Mutex::Lock _l(_lock); + std::set< std::pair >::iterator qi(_byDest.lower_bound(std::pair(dest,(_ClusterSendQueueEntry *)0))); + while ((qi != _byDest.end())&&(qi->first == dest)) { + _bySrc.erase(std::pair(qi->second->fromPeerAddress,qi->second)); + results[count++] = qi->second; + if (count == maxResults) + break; + _byDest.erase(qi++); + } + return count; + } + + /** + * Return entries to pool after use + * + * @param entries Array of entries + * @param count Number of entries + */ + inline void returnToPool(_ClusterSendQueueEntry **entries,unsigned int count) + { + Mutex::Lock _l(_lock); + for(unsigned int i=0;i > _chunks; + _ClusterSendQueueEntry *_pool[ZT_CLUSTER_QUEUE_CHUNK_SIZE * ZT_CLUSTER_MAX_QUEUE_CHUNKS]; + unsigned long _poolCount; + std::set< std::pair > _bySrc; + std::set< std::pair > _byDest; + Mutex _lock; +}; + +Cluster::Cluster( + const RuntimeEnvironment *renv, + uint16_t id, + const std::vector &zeroTierPhysicalEndpoints, + int32_t x, + int32_t y, + int32_t z, + void (*sendFunction)(void *,unsigned int,const void *,unsigned int), + void *sendFunctionArg, + int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *), + void *addressToLocationFunctionArg) : + RR(renv), + _sendQueue(new _ClusterSendQueue()), + _sendFunction(sendFunction), + _sendFunctionArg(sendFunctionArg), + _addressToLocationFunction(addressToLocationFunction), + _addressToLocationFunctionArg(addressToLocationFunctionArg), + _x(x), + _y(y), + _z(z), + _id(id), + _zeroTierPhysicalEndpoints(zeroTierPhysicalEndpoints), + _members(new _Member[ZT_CLUSTER_MAX_MEMBERS]), + _lastFlushed(0), + _lastCleanedRemotePeers(0), + _lastCleanedQueue(0) +{ + uint16_t stmp[ZT_SHA512_DIGEST_LEN / sizeof(uint16_t)]; + + // Generate master secret by hashing the secret from our Identity key pair + RR->identity.sha512PrivateKey(_masterSecret); + + // Generate our inbound message key, which is the master secret XORed with our ID and hashed twice + memcpy(stmp,_masterSecret,sizeof(stmp)); + stmp[0] ^= Utils::hton(id); + SHA512::hash(stmp,stmp,sizeof(stmp)); + SHA512::hash(stmp,stmp,sizeof(stmp)); + memcpy(_key,stmp,sizeof(_key)); + Utils::burn(stmp,sizeof(stmp)); +} + +Cluster::~Cluster() +{ + Utils::burn(_masterSecret,sizeof(_masterSecret)); + Utils::burn(_key,sizeof(_key)); + delete [] _members; + delete _sendQueue; +} + +void Cluster::handleIncomingStateMessage(const void *msg,unsigned int len) +{ + Buffer dmsg; + { + // FORMAT: <[16] iv><[8] MAC><... data> + if ((len < 24)||(len > ZT_CLUSTER_MAX_MESSAGE_LENGTH)) + return; + + // 16-byte IV: first 8 bytes XORed with key, last 8 bytes used as Salsa20 64-bit IV + char keytmp[32]; + memcpy(keytmp,_key,32); + for(int i=0;i<8;++i) + keytmp[i] ^= reinterpret_cast(msg)[i]; + Salsa20 s20(keytmp,reinterpret_cast(msg) + 8); + Utils::burn(keytmp,sizeof(keytmp)); + + // One-time-use Poly1305 key from first 32 bytes of Salsa20 keystream (as per DJB/NaCl "standard") + char polykey[ZT_POLY1305_KEY_LEN]; + memset(polykey,0,sizeof(polykey)); + s20.crypt12(polykey,polykey,sizeof(polykey)); + + // Compute 16-byte MAC + char mac[ZT_POLY1305_MAC_LEN]; + Poly1305::compute(mac,reinterpret_cast(msg) + 24,len - 24,polykey); + + // Check first 8 bytes of MAC against 64-bit MAC in stream + if (!Utils::secureEq(mac,reinterpret_cast(msg) + 16,8)) + return; + + // Decrypt! + dmsg.setSize(len - 24); + s20.crypt12(reinterpret_cast(msg) + 24,const_cast(dmsg.data()),dmsg.size()); + } + + if (dmsg.size() < 4) + return; + const uint16_t fromMemberId = dmsg.at(0); + unsigned int ptr = 2; + if (fromMemberId == _id) // sanity check: we don't talk to ourselves + return; + const uint16_t toMemberId = dmsg.at(ptr); + ptr += 2; + if (toMemberId != _id) // sanity check: message not for us? + return; + + { // make sure sender is actually considered a member + Mutex::Lock _l3(_memberIds_m); + if (std::find(_memberIds.begin(),_memberIds.end(),fromMemberId) == _memberIds.end()) + return; + } + + try { + while (ptr < dmsg.size()) { + const unsigned int mlen = dmsg.at(ptr); ptr += 2; + const unsigned int nextPtr = ptr + mlen; + if (nextPtr > dmsg.size()) + break; + + int mtype = -1; + try { + switch((StateMessageType)(mtype = (int)dmsg[ptr++])) { + default: + break; + + case CLUSTER_MESSAGE_ALIVE: { + _Member &m = _members[fromMemberId]; + Mutex::Lock mlck(m.lock); + ptr += 7; // skip version stuff, not used yet + m.x = dmsg.at(ptr); ptr += 4; + m.y = dmsg.at(ptr); ptr += 4; + m.z = dmsg.at(ptr); ptr += 4; + ptr += 8; // skip local clock, not used + m.load = dmsg.at(ptr); ptr += 8; + m.peers = dmsg.at(ptr); ptr += 8; + ptr += 8; // skip flags, unused +#ifdef ZT_TRACE + std::string addrs; +#endif + unsigned int physicalAddressCount = dmsg[ptr++]; + m.zeroTierPhysicalEndpoints.clear(); + for(unsigned int i=0;i 0) + addrs.push_back(','); + addrs.append(m.zeroTierPhysicalEndpoints.back().toString()); + } +#endif + } +#ifdef ZT_TRACE + if ((RR->node->now() - m.lastReceivedAliveAnnouncement) >= ZT_CLUSTER_TIMEOUT) { + TRACE("[%u] I'm alive! peers close to %d,%d,%d can be redirected to: %s",(unsigned int)fromMemberId,m.x,m.y,m.z,addrs.c_str()); + } +#endif + m.lastReceivedAliveAnnouncement = RR->node->now(); + } break; + + case CLUSTER_MESSAGE_HAVE_PEER: { + Identity id; + ptr += id.deserialize(dmsg,ptr); + if (id) { + { + Mutex::Lock _l(_remotePeers_m); + _RemotePeer &rp = _remotePeers[std::pair(id.address(),(unsigned int)fromMemberId)]; + if (!rp.lastHavePeerReceived) { + RR->topology->saveIdentity((void *)0,id); + RR->identity.agree(id,rp.key,ZT_PEER_SECRET_KEY_LENGTH); + } + rp.lastHavePeerReceived = RR->node->now(); + } + + _ClusterSendQueueEntry *q[16384]; // 16384 is "tons" + unsigned int qc = _sendQueue->getByDest(id.address(),q,16384); + for(unsigned int i=0;irelayViaCluster(q[i]->fromPeerAddress,q[i]->toPeerAddress,q[i]->data,q[i]->len,q[i]->unite); + _sendQueue->returnToPool(q,qc); + + TRACE("[%u] has %s (retried %u queued sends)",(unsigned int)fromMemberId,id.address().toString().c_str(),qc); + } + } break; + + case CLUSTER_MESSAGE_WANT_PEER: { + const Address zeroTierAddress(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; + SharedPtr peer(RR->topology->getPeerNoCache(zeroTierAddress)); + if ( (peer) && (peer->hasLocalClusterOptimalPath(RR->node->now())) ) { + Buffer<1024> buf; + peer->identity().serialize(buf); + Mutex::Lock _l2(_members[fromMemberId].lock); + _send(fromMemberId,CLUSTER_MESSAGE_HAVE_PEER,buf.data(),buf.size()); + } + } break; + + case CLUSTER_MESSAGE_REMOTE_PACKET: { + const unsigned int plen = dmsg.at(ptr); ptr += 2; + if (plen) { + Packet remotep(dmsg.field(ptr,plen),plen); ptr += plen; + //TRACE("remote %s from %s via %u (%u bytes)",Packet::verbString(remotep.verb()),remotep.source().toString().c_str(),fromMemberId,plen); + switch(remotep.verb()) { + case Packet::VERB_WHOIS: _doREMOTE_WHOIS(fromMemberId,remotep); break; + case Packet::VERB_MULTICAST_GATHER: _doREMOTE_MULTICAST_GATHER(fromMemberId,remotep); break; + default: break; // ignore things we don't care about across cluster + } + } + } break; + + case CLUSTER_MESSAGE_PROXY_UNITE: { + const Address localPeerAddress(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; + const Address remotePeerAddress(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; + const unsigned int numRemotePeerPaths = dmsg[ptr++]; + InetAddress remotePeerPaths[256]; // size is 8-bit, so 256 is max + for(unsigned int i=0;inode->now(); + SharedPtr localPeer(RR->topology->getPeerNoCache(localPeerAddress)); + if ((localPeer)&&(numRemotePeerPaths > 0)) { + InetAddress bestLocalV4,bestLocalV6; + localPeer->getRendezvousAddresses(now,bestLocalV4,bestLocalV6); + + InetAddress bestRemoteV4,bestRemoteV6; + for(unsigned int i=0;iidentity.address(),Packet::VERB_RENDEZVOUS); + rendezvousForLocal.append((uint8_t)0); + remotePeerAddress.appendTo(rendezvousForLocal); + + Buffer<2048> rendezvousForRemote; + remotePeerAddress.appendTo(rendezvousForRemote); + rendezvousForRemote.append((uint8_t)Packet::VERB_RENDEZVOUS); + rendezvousForRemote.addSize(2); // space for actual packet payload length + rendezvousForRemote.append((uint8_t)0); // flags == 0 + localPeerAddress.appendTo(rendezvousForRemote); + + bool haveMatch = false; + if ((bestLocalV6)&&(bestRemoteV6)) { + haveMatch = true; + + rendezvousForLocal.append((uint16_t)bestRemoteV6.port()); + rendezvousForLocal.append((uint8_t)16); + rendezvousForLocal.append(bestRemoteV6.rawIpData(),16); + + rendezvousForRemote.append((uint16_t)bestLocalV6.port()); + rendezvousForRemote.append((uint8_t)16); + rendezvousForRemote.append(bestLocalV6.rawIpData(),16); + rendezvousForRemote.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(9 + 16)); + } else if ((bestLocalV4)&&(bestRemoteV4)) { + haveMatch = true; + + rendezvousForLocal.append((uint16_t)bestRemoteV4.port()); + rendezvousForLocal.append((uint8_t)4); + rendezvousForLocal.append(bestRemoteV4.rawIpData(),4); + + rendezvousForRemote.append((uint16_t)bestLocalV4.port()); + rendezvousForRemote.append((uint8_t)4); + rendezvousForRemote.append(bestLocalV4.rawIpData(),4); + rendezvousForRemote.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(9 + 4)); + } + + if (haveMatch) { + { + Mutex::Lock _l2(_members[fromMemberId].lock); + _send(fromMemberId,CLUSTER_MESSAGE_PROXY_SEND,rendezvousForRemote.data(),rendezvousForRemote.size()); + } + RR->sw->send((void *)0,rendezvousForLocal,true); + } + } + } break; + + case CLUSTER_MESSAGE_PROXY_SEND: { + const Address rcpt(dmsg.field(ptr,ZT_ADDRESS_LENGTH),ZT_ADDRESS_LENGTH); ptr += ZT_ADDRESS_LENGTH; + const Packet::Verb verb = (Packet::Verb)dmsg[ptr++]; + const unsigned int len = dmsg.at(ptr); ptr += 2; + Packet outp(rcpt,RR->identity.address(),verb); + outp.append(dmsg.field(ptr,len),len); ptr += len; + RR->sw->send((void *)0,outp,true); + //TRACE("[%u] proxy send %s to %s length %u",(unsigned int)fromMemberId,Packet::verbString(verb),rcpt.toString().c_str(),len); + } break; + + case CLUSTER_MESSAGE_NETWORK_CONFIG: { + const SharedPtr network(RR->node->network(dmsg.at(ptr))); + if (network) { + // Copy into a Packet just to conform to Network API. Eventually + // will want to refactor. + network->handleConfigChunk((void *)0,0,Address(),Buffer(dmsg),ptr); + } + } break; + } + } catch ( ... ) { + TRACE("invalid message of size %u type %d (inner decode), discarding",mlen,mtype); + // drop invalids + } + + ptr = nextPtr; + } + } catch ( ... ) { + TRACE("invalid message (outer loop), discarding"); + // drop invalids + } +} + +void Cluster::broadcastHavePeer(const Identity &id) +{ + Buffer<1024> buf; + id.serialize(buf); + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + _send(*mid,CLUSTER_MESSAGE_HAVE_PEER,buf.data(),buf.size()); + } +} + +void Cluster::broadcastNetworkConfigChunk(const void *chunk,unsigned int len) +{ + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + _send(*mid,CLUSTER_MESSAGE_NETWORK_CONFIG,chunk,len); + } +} + +int Cluster::checkSendViaCluster(const Address &toPeerAddress,uint64_t &mostRecentTs,void *peerSecret) +{ + const uint64_t now = RR->node->now(); + mostRecentTs = 0; + int mostRecentMemberId = -1; + { + Mutex::Lock _l2(_remotePeers_m); + std::map< std::pair,_RemotePeer >::const_iterator rpe(_remotePeers.lower_bound(std::pair(toPeerAddress,0))); + for(;;) { + if ((rpe == _remotePeers.end())||(rpe->first.first != toPeerAddress)) + break; + else if (rpe->second.lastHavePeerReceived > mostRecentTs) { + mostRecentTs = rpe->second.lastHavePeerReceived; + memcpy(peerSecret,rpe->second.key,ZT_PEER_SECRET_KEY_LENGTH); + mostRecentMemberId = (int)rpe->first.second; + } + ++rpe; + } + } + + const uint64_t ageOfMostRecentHavePeerAnnouncement = now - mostRecentTs; + if (ageOfMostRecentHavePeerAnnouncement >= (ZT_PEER_ACTIVITY_TIMEOUT / 3)) { + if (ageOfMostRecentHavePeerAnnouncement >= ZT_PEER_ACTIVITY_TIMEOUT) + mostRecentMemberId = -1; + + bool sendWantPeer = true; + { + Mutex::Lock _l(_remotePeers_m); + _RemotePeer &rp = _remotePeers[std::pair(toPeerAddress,(unsigned int)_id)]; + if ((now - rp.lastSentWantPeer) >= ZT_CLUSTER_WANT_PEER_EVERY) { + rp.lastSentWantPeer = now; + } else { + sendWantPeer = false; // don't flood WANT_PEER + } + } + if (sendWantPeer) { + char tmp[ZT_ADDRESS_LENGTH]; + toPeerAddress.copyTo(tmp,ZT_ADDRESS_LENGTH); + { + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + _send(*mid,CLUSTER_MESSAGE_WANT_PEER,tmp,ZT_ADDRESS_LENGTH); + } + } + } + } + + return mostRecentMemberId; +} + +bool Cluster::sendViaCluster(int mostRecentMemberId,const Address &toPeerAddress,const void *data,unsigned int len) +{ + if ((mostRecentMemberId < 0)||(mostRecentMemberId >= ZT_CLUSTER_MAX_MEMBERS)) // sanity check + return false; + Mutex::Lock _l2(_members[mostRecentMemberId].lock); + for(std::vector::const_iterator i1(_zeroTierPhysicalEndpoints.begin());i1!=_zeroTierPhysicalEndpoints.end();++i1) { + for(std::vector::const_iterator i2(_members[mostRecentMemberId].zeroTierPhysicalEndpoints.begin());i2!=_members[mostRecentMemberId].zeroTierPhysicalEndpoints.end();++i2) { + if (i1->ss_family == i2->ss_family) { + TRACE("sendViaCluster sending %u bytes to %s by way of %u (%s->%s)",len,toPeerAddress.toString().c_str(),(unsigned int)mostRecentMemberId,i1->toString().c_str(),i2->toString().c_str()); + RR->node->putPacket((void *)0,*i1,*i2,data,len); + return true; + } + } + } + return false; +} + +void Cluster::relayViaCluster(const Address &fromPeerAddress,const Address &toPeerAddress,const void *data,unsigned int len,bool unite) +{ + if (len > ZT_PROTO_MAX_PACKET_LENGTH) // sanity check + return; + + const uint64_t now = RR->node->now(); + + uint64_t mostRecentTs = 0; + int mostRecentMemberId = -1; + { + Mutex::Lock _l2(_remotePeers_m); + std::map< std::pair,_RemotePeer >::const_iterator rpe(_remotePeers.lower_bound(std::pair(toPeerAddress,0))); + for(;;) { + if ((rpe == _remotePeers.end())||(rpe->first.first != toPeerAddress)) + break; + else if (rpe->second.lastHavePeerReceived > mostRecentTs) { + mostRecentTs = rpe->second.lastHavePeerReceived; + mostRecentMemberId = (int)rpe->first.second; + } + ++rpe; + } + } + + const uint64_t ageOfMostRecentHavePeerAnnouncement = now - mostRecentTs; + if (ageOfMostRecentHavePeerAnnouncement >= (ZT_PEER_ACTIVITY_TIMEOUT / 3)) { + // Enqueue and wait if peer seems alive, but do WANT_PEER to refresh homing + const bool enqueueAndWait = ((ageOfMostRecentHavePeerAnnouncement >= ZT_PEER_ACTIVITY_TIMEOUT)||(mostRecentMemberId < 0)); + + // Poll everyone with WANT_PEER if the age of our most recent entry is + // approaching expiration (or has expired, or does not exist). + bool sendWantPeer = true; + { + Mutex::Lock _l(_remotePeers_m); + _RemotePeer &rp = _remotePeers[std::pair(toPeerAddress,(unsigned int)_id)]; + if ((now - rp.lastSentWantPeer) >= ZT_CLUSTER_WANT_PEER_EVERY) { + rp.lastSentWantPeer = now; + } else { + sendWantPeer = false; // don't flood WANT_PEER + } + } + if (sendWantPeer) { + char tmp[ZT_ADDRESS_LENGTH]; + toPeerAddress.copyTo(tmp,ZT_ADDRESS_LENGTH); + { + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + _send(*mid,CLUSTER_MESSAGE_WANT_PEER,tmp,ZT_ADDRESS_LENGTH); + } + } + } + + // If there isn't a good place to send via, then enqueue this for retrying + // later and return after having broadcasted a WANT_PEER. + if (enqueueAndWait) { + TRACE("relayViaCluster %s -> %s enqueueing to wait for HAVE_PEER",fromPeerAddress.toString().c_str(),toPeerAddress.toString().c_str()); + _sendQueue->enqueue(now,fromPeerAddress,toPeerAddress,data,len,unite); + return; + } + } + + if (mostRecentMemberId >= 0) { + Buffer<1024> buf; + if (unite) { + InetAddress v4,v6; + if (fromPeerAddress) { + SharedPtr fromPeer(RR->topology->getPeerNoCache(fromPeerAddress)); + if (fromPeer) + fromPeer->getRendezvousAddresses(now,v4,v6); + } + uint8_t addrCount = 0; + if (v4) + ++addrCount; + if (v6) + ++addrCount; + if (addrCount) { + toPeerAddress.appendTo(buf); + fromPeerAddress.appendTo(buf); + buf.append(addrCount); + if (v4) + v4.serialize(buf); + if (v6) + v6.serialize(buf); + } + } + + { + Mutex::Lock _l2(_members[mostRecentMemberId].lock); + if (buf.size() > 0) + _send(mostRecentMemberId,CLUSTER_MESSAGE_PROXY_UNITE,buf.data(),buf.size()); + + for(std::vector::const_iterator i1(_zeroTierPhysicalEndpoints.begin());i1!=_zeroTierPhysicalEndpoints.end();++i1) { + for(std::vector::const_iterator i2(_members[mostRecentMemberId].zeroTierPhysicalEndpoints.begin());i2!=_members[mostRecentMemberId].zeroTierPhysicalEndpoints.end();++i2) { + if (i1->ss_family == i2->ss_family) { + TRACE("relayViaCluster relaying %u bytes from %s to %s by way of %u (%s->%s)",len,fromPeerAddress.toString().c_str(),toPeerAddress.toString().c_str(),(unsigned int)mostRecentMemberId,i1->toString().c_str(),i2->toString().c_str()); + RR->node->putPacket((void *)0,*i1,*i2,data,len); + return; + } + } + } + + TRACE("relayViaCluster relaying %u bytes from %s to %s by way of %u failed: no common endpoints with the same address family!",len,fromPeerAddress.toString().c_str(),toPeerAddress.toString().c_str(),(unsigned int)mostRecentMemberId); + } + } +} + +void Cluster::sendDistributedQuery(const Packet &pkt) +{ + Buffer<4096> buf; + buf.append((uint16_t)pkt.size()); + buf.append(pkt.data(),pkt.size()); + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + _send(*mid,CLUSTER_MESSAGE_REMOTE_PACKET,buf.data(),buf.size()); + } +} + +void Cluster::doPeriodicTasks() +{ + const uint64_t now = RR->node->now(); + + if ((now - _lastFlushed) >= ZT_CLUSTER_FLUSH_PERIOD) { + _lastFlushed = now; + + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + + if ((now - _members[*mid].lastAnnouncedAliveTo) >= ((ZT_CLUSTER_TIMEOUT / 2) - 1000)) { + _members[*mid].lastAnnouncedAliveTo = now; + + Buffer<2048> alive; + alive.append((uint16_t)ZEROTIER_ONE_VERSION_MAJOR); + alive.append((uint16_t)ZEROTIER_ONE_VERSION_MINOR); + alive.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION); + alive.append((uint8_t)ZT_PROTO_VERSION); + if (_addressToLocationFunction) { + alive.append((int32_t)_x); + alive.append((int32_t)_y); + alive.append((int32_t)_z); + } else { + alive.append((int32_t)0); + alive.append((int32_t)0); + alive.append((int32_t)0); + } + alive.append((uint64_t)now); + alive.append((uint64_t)0); // TODO: compute and send load average + alive.append((uint64_t)RR->topology->countActive(now)); + alive.append((uint64_t)0); // unused/reserved flags + alive.append((uint8_t)_zeroTierPhysicalEndpoints.size()); + for(std::vector::const_iterator pe(_zeroTierPhysicalEndpoints.begin());pe!=_zeroTierPhysicalEndpoints.end();++pe) + pe->serialize(alive); + _send(*mid,CLUSTER_MESSAGE_ALIVE,alive.data(),alive.size()); + } + + _flush(*mid); + } + } + + if ((now - _lastCleanedRemotePeers) >= (ZT_PEER_ACTIVITY_TIMEOUT * 2)) { + _lastCleanedRemotePeers = now; + + Mutex::Lock _l(_remotePeers_m); + for(std::map< std::pair,_RemotePeer >::iterator rp(_remotePeers.begin());rp!=_remotePeers.end();) { + if ((now - rp->second.lastHavePeerReceived) >= ZT_PEER_ACTIVITY_TIMEOUT) + _remotePeers.erase(rp++); + else ++rp; + } + } + + if ((now - _lastCleanedQueue) >= ZT_CLUSTER_QUEUE_EXPIRATION) { + _lastCleanedQueue = now; + _sendQueue->expire(now); + } +} + +void Cluster::addMember(uint16_t memberId) +{ + if ((memberId >= ZT_CLUSTER_MAX_MEMBERS)||(memberId == _id)) + return; + + Mutex::Lock _l2(_members[memberId].lock); + + { + Mutex::Lock _l(_memberIds_m); + if (std::find(_memberIds.begin(),_memberIds.end(),memberId) != _memberIds.end()) + return; + _memberIds.push_back(memberId); + std::sort(_memberIds.begin(),_memberIds.end()); + } + + _members[memberId].clear(); + + // Generate this member's message key from the master and its ID + uint16_t stmp[ZT_SHA512_DIGEST_LEN / sizeof(uint16_t)]; + memcpy(stmp,_masterSecret,sizeof(stmp)); + stmp[0] ^= Utils::hton(memberId); + SHA512::hash(stmp,stmp,sizeof(stmp)); + SHA512::hash(stmp,stmp,sizeof(stmp)); + memcpy(_members[memberId].key,stmp,sizeof(_members[memberId].key)); + Utils::burn(stmp,sizeof(stmp)); + + // Prepare q + _members[memberId].q.clear(); + char iv[16]; + Utils::getSecureRandom(iv,16); + _members[memberId].q.append(iv,16); + _members[memberId].q.addSize(8); // room for MAC + _members[memberId].q.append((uint16_t)_id); + _members[memberId].q.append((uint16_t)memberId); +} + +void Cluster::removeMember(uint16_t memberId) +{ + Mutex::Lock _l(_memberIds_m); + std::vector newMemberIds; + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + if (*mid != memberId) + newMemberIds.push_back(*mid); + } + _memberIds = newMemberIds; +} + +bool Cluster::findBetterEndpoint(InetAddress &redirectTo,const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload) +{ + if (_addressToLocationFunction) { + // Pick based on location if it can be determined + int px = 0,py = 0,pz = 0; + if (_addressToLocationFunction(_addressToLocationFunctionArg,reinterpret_cast(&peerPhysicalAddress),&px,&py,&pz) == 0) { + TRACE("no geolocation data for %s",peerPhysicalAddress.toIpString().c_str()); + return false; + } + + // Find member closest to this peer + const uint64_t now = RR->node->now(); + std::vector best; + const double currentDistance = _dist3d(_x,_y,_z,px,py,pz); + double bestDistance = (offload ? 2147483648.0 : currentDistance); +#ifdef ZT_TRACE + unsigned int bestMember = _id; +#endif + { + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + _Member &m = _members[*mid]; + Mutex::Lock _ml(m.lock); + + // Consider member if it's alive and has sent us a location and one or more physical endpoints to send peers to + if ( ((now - m.lastReceivedAliveAnnouncement) < ZT_CLUSTER_TIMEOUT) && ((m.x != 0)||(m.y != 0)||(m.z != 0)) && (m.zeroTierPhysicalEndpoints.size() > 0) ) { + const double mdist = _dist3d(m.x,m.y,m.z,px,py,pz); + if (mdist < bestDistance) { + bestDistance = mdist; +#ifdef ZT_TRACE + bestMember = *mid; +#endif + best = m.zeroTierPhysicalEndpoints; + } + } + } + } + + // Redirect to a closer member if it has a ZeroTier endpoint address in the same ss_family + for(std::vector::const_iterator a(best.begin());a!=best.end();++a) { + if (a->ss_family == peerPhysicalAddress.ss_family) { + TRACE("%s at [%d,%d,%d] is %f from us but %f from %u, can redirect to %s",peerAddress.toString().c_str(),px,py,pz,currentDistance,bestDistance,bestMember,a->toString().c_str()); + redirectTo = *a; + return true; + } + } + TRACE("%s at [%d,%d,%d] is %f from us, no better endpoints found",peerAddress.toString().c_str(),px,py,pz,currentDistance); + return false; + } else { + // TODO: pick based on load if no location info? + return false; + } +} + +bool Cluster::isClusterPeerFrontplane(const InetAddress &ip) const +{ + Mutex::Lock _l(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + Mutex::Lock _l2(_members[*mid].lock); + for(std::vector::const_iterator i2(_members[*mid].zeroTierPhysicalEndpoints.begin());i2!=_members[*mid].zeroTierPhysicalEndpoints.end();++i2) { + if (ip == *i2) + return true; + } + } + return false; +} + +void Cluster::status(ZT_ClusterStatus &status) const +{ + const uint64_t now = RR->node->now(); + memset(&status,0,sizeof(ZT_ClusterStatus)); + + status.myId = _id; + + { + ZT_ClusterMemberStatus *const s = &(status.members[status.clusterSize++]); + s->id = _id; + s->alive = 1; + s->x = _x; + s->y = _y; + s->z = _z; + s->load = 0; // TODO + s->peers = RR->topology->countActive(now); + for(std::vector::const_iterator ep(_zeroTierPhysicalEndpoints.begin());ep!=_zeroTierPhysicalEndpoints.end();++ep) { + if (s->numZeroTierPhysicalEndpoints >= ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES) // sanity check + break; + memcpy(&(s->zeroTierPhysicalEndpoints[s->numZeroTierPhysicalEndpoints++]),&(*ep),sizeof(struct sockaddr_storage)); + } + } + + { + Mutex::Lock _l1(_memberIds_m); + for(std::vector::const_iterator mid(_memberIds.begin());mid!=_memberIds.end();++mid) { + if (status.clusterSize >= ZT_CLUSTER_MAX_MEMBERS) // sanity check + break; + + _Member &m = _members[*mid]; + Mutex::Lock ml(m.lock); + + ZT_ClusterMemberStatus *const s = &(status.members[status.clusterSize++]); + s->id = *mid; + s->msSinceLastHeartbeat = (unsigned int)std::min((uint64_t)(~((unsigned int)0)),(now - m.lastReceivedAliveAnnouncement)); + s->alive = (s->msSinceLastHeartbeat < ZT_CLUSTER_TIMEOUT) ? 1 : 0; + s->x = m.x; + s->y = m.y; + s->z = m.z; + s->load = m.load; + s->peers = m.peers; + for(std::vector::const_iterator ep(m.zeroTierPhysicalEndpoints.begin());ep!=m.zeroTierPhysicalEndpoints.end();++ep) { + if (s->numZeroTierPhysicalEndpoints >= ZT_CLUSTER_MAX_ZT_PHYSICAL_ADDRESSES) // sanity check + break; + memcpy(&(s->zeroTierPhysicalEndpoints[s->numZeroTierPhysicalEndpoints++]),&(*ep),sizeof(struct sockaddr_storage)); + } + } + } +} + +void Cluster::_send(uint16_t memberId,StateMessageType type,const void *msg,unsigned int len) +{ + if ((len + 3) > (ZT_CLUSTER_MAX_MESSAGE_LENGTH - (24 + 2 + 2))) // sanity check + return; + _Member &m = _members[memberId]; + // assumes m.lock is locked! + if ((m.q.size() + len + 3) > ZT_CLUSTER_MAX_MESSAGE_LENGTH) + _flush(memberId); + m.q.append((uint16_t)(len + 1)); + m.q.append((uint8_t)type); + m.q.append(msg,len); +} + +void Cluster::_flush(uint16_t memberId) +{ + _Member &m = _members[memberId]; + // assumes m.lock is locked! + if (m.q.size() > (24 + 2 + 2)) { // 16-byte IV + 8-byte MAC + 2 byte from-member-ID + 2 byte to-member-ID + // Create key from member's key and IV + char keytmp[32]; + memcpy(keytmp,m.key,32); + for(int i=0;i<8;++i) + keytmp[i] ^= m.q[i]; + Salsa20 s20(keytmp,m.q.field(8,8)); + Utils::burn(keytmp,sizeof(keytmp)); + + // One-time-use Poly1305 key from first 32 bytes of Salsa20 keystream (as per DJB/NaCl "standard") + char polykey[ZT_POLY1305_KEY_LEN]; + memset(polykey,0,sizeof(polykey)); + s20.crypt12(polykey,polykey,sizeof(polykey)); + + // Encrypt m.q in place + s20.crypt12(reinterpret_cast(m.q.data()) + 24,const_cast(reinterpret_cast(m.q.data())) + 24,m.q.size() - 24); + + // Add MAC for authentication (encrypt-then-MAC) + char mac[ZT_POLY1305_MAC_LEN]; + Poly1305::compute(mac,reinterpret_cast(m.q.data()) + 24,m.q.size() - 24,polykey); + memcpy(m.q.field(16,8),mac,8); + + // Send! + _sendFunction(_sendFunctionArg,memberId,m.q.data(),m.q.size()); + + // Prepare for more + m.q.clear(); + char iv[16]; + Utils::getSecureRandom(iv,16); + m.q.append(iv,16); + m.q.addSize(8); // room for MAC + m.q.append((uint16_t)_id); // from member ID + m.q.append((uint16_t)memberId); // to member ID + } +} + +void Cluster::_doREMOTE_WHOIS(uint64_t fromMemberId,const Packet &remotep) +{ + if (remotep.payloadLength() >= ZT_ADDRESS_LENGTH) { + Identity queried(RR->topology->getIdentity((void *)0,Address(remotep.payload(),ZT_ADDRESS_LENGTH))); + if (queried) { + Buffer<1024> routp; + remotep.source().appendTo(routp); + routp.append((uint8_t)Packet::VERB_OK); + routp.addSize(2); // space for length + routp.append((uint8_t)Packet::VERB_WHOIS); + routp.append(remotep.packetId()); + queried.serialize(routp); + routp.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(routp.size() - ZT_ADDRESS_LENGTH - 3)); + + TRACE("responding to remote WHOIS from %s @ %u with identity of %s",remotep.source().toString().c_str(),(unsigned int)fromMemberId,queried.address().toString().c_str()); + Mutex::Lock _l2(_members[fromMemberId].lock); + _send(fromMemberId,CLUSTER_MESSAGE_PROXY_SEND,routp.data(),routp.size()); + } + } +} + +void Cluster::_doREMOTE_MULTICAST_GATHER(uint64_t fromMemberId,const Packet &remotep) +{ + const uint64_t nwid = remotep.at(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_NETWORK_ID); + const MulticastGroup mg(MAC(remotep.field(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_MAC,6),6),remotep.at(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_ADI)); + unsigned int gatherLimit = remotep.at(ZT_PROTO_VERB_MULTICAST_GATHER_IDX_GATHER_LIMIT); + const Address remotePeerAddress(remotep.source()); + + if (gatherLimit) { + Buffer routp; + remotePeerAddress.appendTo(routp); + routp.append((uint8_t)Packet::VERB_OK); + routp.addSize(2); // space for length + routp.append((uint8_t)Packet::VERB_MULTICAST_GATHER); + routp.append(remotep.packetId()); + routp.append(nwid); + mg.mac().appendTo(routp); + routp.append((uint32_t)mg.adi()); + + if (gatherLimit > ((ZT_CLUSTER_MAX_MESSAGE_LENGTH - 80) / 5)) + gatherLimit = ((ZT_CLUSTER_MAX_MESSAGE_LENGTH - 80) / 5); + if (RR->mc->gather(remotePeerAddress,nwid,mg,routp,gatherLimit)) { + routp.setAt(ZT_ADDRESS_LENGTH + 1,(uint16_t)(routp.size() - ZT_ADDRESS_LENGTH - 3)); + + TRACE("responding to remote MULTICAST_GATHER from %s @ %u with %u bytes",remotePeerAddress.toString().c_str(),(unsigned int)fromMemberId,routp.size()); + Mutex::Lock _l2(_members[fromMemberId].lock); + _send(fromMemberId,CLUSTER_MESSAGE_PROXY_SEND,routp.data(),routp.size()); + } + } +} + +} // namespace ZeroTier + +#endif // ZT_ENABLE_CLUSTER diff --git a/attic/Cluster.hpp b/attic/Cluster.hpp new file mode 100644 index 00000000..74b091f5 --- /dev/null +++ b/attic/Cluster.hpp @@ -0,0 +1,463 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#ifndef ZT_CLUSTER_HPP +#define ZT_CLUSTER_HPP + +#ifdef ZT_ENABLE_CLUSTER + +#include + +#include "Constants.hpp" +#include "../include/ZeroTierOne.h" +#include "Address.hpp" +#include "InetAddress.hpp" +#include "SHA512.hpp" +#include "Utils.hpp" +#include "Buffer.hpp" +#include "Mutex.hpp" +#include "SharedPtr.hpp" +#include "Hashtable.hpp" +#include "Packet.hpp" +#include "SharedPtr.hpp" + +/** + * Timeout for cluster members being considered "alive" + * + * A cluster member is considered dead and will no longer have peers + * redirected to it if we have not heard a heartbeat in this long. + */ +#define ZT_CLUSTER_TIMEOUT 5000 + +/** + * Desired period between doPeriodicTasks() in milliseconds + */ +#define ZT_CLUSTER_PERIODIC_TASK_PERIOD 20 + +/** + * How often to flush outgoing message queues (maximum interval) + */ +#define ZT_CLUSTER_FLUSH_PERIOD ZT_CLUSTER_PERIODIC_TASK_PERIOD + +/** + * Maximum number of queued outgoing packets per sender address + */ +#define ZT_CLUSTER_MAX_QUEUE_PER_SENDER 16 + +/** + * Expiration time for send queue entries + */ +#define ZT_CLUSTER_QUEUE_EXPIRATION 3000 + +/** + * Chunk size for allocating queue entries + * + * Queue entries are allocated in chunks of this many and are added to a pool. + * ZT_CLUSTER_MAX_QUEUE_GLOBAL must be evenly divisible by this. + */ +#define ZT_CLUSTER_QUEUE_CHUNK_SIZE 32 + +/** + * Maximum number of chunks to ever allocate + * + * This is a global sanity limit to prevent resource exhaustion attacks. It + * works out to about 600mb of RAM. You'll never see this on a normal edge + * node. We're unlikely to see this on a root server unless someone is DOSing + * us. In that case cluster relaying will be affected but other functions + * should continue to operate normally. + */ +#define ZT_CLUSTER_MAX_QUEUE_CHUNKS 8194 + +/** + * Max data per queue entry + */ +#define ZT_CLUSTER_SEND_QUEUE_DATA_MAX 1500 + +/** + * We won't send WANT_PEER to other members more than every (ms) per recipient + */ +#define ZT_CLUSTER_WANT_PEER_EVERY 1000 + +namespace ZeroTier { + +class RuntimeEnvironment; +class MulticastGroup; +class Peer; +class Identity; + +// Internal class implemented inside Cluster.cpp +class _ClusterSendQueue; + +/** + * Multi-homing cluster state replication and packet relaying + * + * Multi-homing means more than one node sharing the same ZeroTier identity. + * There is nothing in the protocol to prevent this, but to make it work well + * requires the devices sharing an identity to cooperate and share some + * information. + * + * There are three use cases we want to fulfill: + * + * (1) Multi-homing of root servers with handoff for efficient routing, + * HA, and load balancing across many commodity nodes. + * (2) Multi-homing of network controllers for the same reason. + * (3) Multi-homing of nodes on virtual networks, such as domain servers + * and other important endpoints. + * + * These use cases are in order of escalating difficulty. The initial + * version of Cluster is aimed at satisfying the first, though you are + * free to try #2 and #3. + */ +class Cluster +{ +public: + /** + * State message types + */ + enum StateMessageType + { + CLUSTER_MESSAGE_NOP = 0, + + /** + * This cluster member is alive: + * <[2] version minor> + * <[2] version major> + * <[2] version revision> + * <[1] protocol version> + * <[4] X location (signed 32-bit)> + * <[4] Y location (signed 32-bit)> + * <[4] Z location (signed 32-bit)> + * <[8] local clock at this member> + * <[8] load average> + * <[8] number of peers> + * <[8] flags (currently unused, must be zero)> + * <[1] number of preferred ZeroTier endpoints> + * <[...] InetAddress(es) of preferred ZeroTier endpoint(s)> + * + * Cluster members constantly broadcast an alive heartbeat and will only + * receive peer redirects if they've done so within the timeout. + */ + CLUSTER_MESSAGE_ALIVE = 1, + + /** + * Cluster member has this peer: + * <[...] serialized identity of peer> + * + * This is typically sent in response to WANT_PEER but can also be pushed + * to prepopulate if this makes sense. + */ + CLUSTER_MESSAGE_HAVE_PEER = 2, + + /** + * Cluster member wants this peer: + * <[5] ZeroTier address of peer> + * + * Members that have a direct link to this peer will respond with + * HAVE_PEER. + */ + CLUSTER_MESSAGE_WANT_PEER = 3, + + /** + * A remote packet that we should also possibly respond to: + * <[2] 16-bit length of remote packet> + * <[...] remote packet payload> + * + * Cluster members may relay requests by relaying the request packet. + * These may include requests such as WHOIS and MULTICAST_GATHER. The + * packet must be already decrypted, decompressed, and authenticated. + * + * This can only be used for small request packets as per the cluster + * message size limit, but since these are the only ones in question + * this is fine. + * + * If a response is generated it is sent via PROXY_SEND. + */ + CLUSTER_MESSAGE_REMOTE_PACKET = 4, + + /** + * Request that VERB_RENDEZVOUS be sent to a peer that we have: + * <[5] ZeroTier address of peer on recipient's side> + * <[5] ZeroTier address of peer on sender's side> + * <[1] 8-bit number of sender's peer's active path addresses> + * <[...] series of serialized InetAddresses of sender's peer's paths> + * + * This requests that we perform NAT-t introduction between a peer that + * we have and one on the sender's side. The sender furnishes contact + * info for its peer, and we send VERB_RENDEZVOUS to both sides: to ours + * directly and with PROXY_SEND to theirs. + */ + CLUSTER_MESSAGE_PROXY_UNITE = 5, + + /** + * Request that a cluster member send a packet to a locally-known peer: + * <[5] ZeroTier address of recipient> + * <[1] packet verb> + * <[2] length of packet payload> + * <[...] packet payload> + * + * This differs from RELAY in that it requests the receiving cluster + * member to actually compose a ZeroTier Packet from itself to the + * provided recipient. RELAY simply says "please forward this blob." + * RELAY is used to implement peer-to-peer relaying with RENDEZVOUS, + * while PROXY_SEND is used to implement proxy sending (which right + * now is only used to send RENDEZVOUS). + */ + CLUSTER_MESSAGE_PROXY_SEND = 6, + + /** + * Replicate a network config for a network we belong to: + * <[...] network config chunk> + * + * This is used by clusters to avoid every member having to query + * for the same netconf for networks all members belong to. + * + * The first field of a network config chunk is the network ID, + * so this can be checked to look up the network on receipt. + */ + CLUSTER_MESSAGE_NETWORK_CONFIG = 7 + }; + + /** + * Construct a new cluster + */ + Cluster( + const RuntimeEnvironment *renv, + uint16_t id, + const std::vector &zeroTierPhysicalEndpoints, + int32_t x, + int32_t y, + int32_t z, + void (*sendFunction)(void *,unsigned int,const void *,unsigned int), + void *sendFunctionArg, + int (*addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *), + void *addressToLocationFunctionArg); + + ~Cluster(); + + /** + * @return This cluster member's ID + */ + inline uint16_t id() const throw() { return _id; } + + /** + * Handle an incoming intra-cluster message + * + * @param data Message data + * @param len Message length (max: ZT_CLUSTER_MAX_MESSAGE_LENGTH) + */ + void handleIncomingStateMessage(const void *msg,unsigned int len); + + /** + * Broadcast that we have a given peer + * + * This should be done when new peers are first contacted. + * + * @param id Identity of peer + */ + void broadcastHavePeer(const Identity &id); + + /** + * Broadcast a network config chunk to other members of cluster + * + * @param chunk Chunk data + * @param len Length of chunk + */ + void broadcastNetworkConfigChunk(const void *chunk,unsigned int len); + + /** + * If the cluster has this peer, prepare the packet to send via cluster + * + * Note that outp is only armored (or modified at all) if the return value is a member ID. + * + * @param toPeerAddress Value of outp.destination(), simply to save additional lookup + * @param ts Result: set to time of last HAVE_PEER from the cluster + * @param peerSecret Result: Buffer to fill with peer secret on valid return value, must be at least ZT_PEER_SECRET_KEY_LENGTH bytes + * @return -1 if cluster does not know this peer, or a member ID to pass to sendViaCluster() + */ + int checkSendViaCluster(const Address &toPeerAddress,uint64_t &mostRecentTs,void *peerSecret); + + /** + * Send data via cluster front plane (packet head or fragment) + * + * @param haveMemberId Member ID that has this peer as returned by prepSendviaCluster() + * @param toPeerAddress Destination peer address + * @param data Packet or packet fragment data + * @param len Length of packet or fragment + * @return True if packet was sent (and outp was modified via armoring) + */ + bool sendViaCluster(int haveMemberId,const Address &toPeerAddress,const void *data,unsigned int len); + + /** + * Relay a packet via the cluster + * + * This is used in the outgoing packet and relaying logic in Switch to + * relay packets to other cluster members. It isn't PROXY_SEND-- that is + * used internally in Cluster to send responses to peer queries. + * + * @param fromPeerAddress Source peer address (if known, should be NULL for fragments) + * @param toPeerAddress Destination peer address + * @param data Packet or packet fragment data + * @param len Length of packet or fragment + * @param unite If true, also request proxy unite across cluster + */ + void relayViaCluster(const Address &fromPeerAddress,const Address &toPeerAddress,const void *data,unsigned int len,bool unite); + + /** + * Send a distributed query to other cluster members + * + * Some queries such as WHOIS or MULTICAST_GATHER need a response from other + * cluster members. Replies (if any) will be sent back to the peer via + * PROXY_SEND across the cluster. + * + * @param pkt Packet to distribute + */ + void sendDistributedQuery(const Packet &pkt); + + /** + * Call every ~ZT_CLUSTER_PERIODIC_TASK_PERIOD milliseconds. + */ + void doPeriodicTasks(); + + /** + * Add a member ID to this cluster + * + * @param memberId Member ID + */ + void addMember(uint16_t memberId); + + /** + * Remove a member ID from this cluster + * + * @param memberId Member ID to remove + */ + void removeMember(uint16_t memberId); + + /** + * Find a better cluster endpoint for this peer (if any) + * + * @param redirectTo InetAddress to be set to a better endpoint (if there is one) + * @param peerAddress Address of peer to (possibly) redirect + * @param peerPhysicalAddress Physical address of peer's current best path (where packet was most recently received or getBestPath()->address()) + * @param offload Always redirect if possible -- can be used to offload peers during shutdown + * @return True if redirectTo was set to a new address, false if redirectTo was not modified + */ + bool findBetterEndpoint(InetAddress &redirectTo,const Address &peerAddress,const InetAddress &peerPhysicalAddress,bool offload); + + /** + * @param ip Address to check + * @return True if this is a cluster frontplane address (excluding our addresses) + */ + bool isClusterPeerFrontplane(const InetAddress &ip) const; + + /** + * Fill out ZT_ClusterStatus structure (from core API) + * + * @param status Reference to structure to hold result (anything there is replaced) + */ + void status(ZT_ClusterStatus &status) const; + +private: + void _send(uint16_t memberId,StateMessageType type,const void *msg,unsigned int len); + void _flush(uint16_t memberId); + + void _doREMOTE_WHOIS(uint64_t fromMemberId,const Packet &remotep); + void _doREMOTE_MULTICAST_GATHER(uint64_t fromMemberId,const Packet &remotep); + + // These are initialized in the constructor and remain immutable ------------ + uint16_t _masterSecret[ZT_SHA512_DIGEST_LEN / sizeof(uint16_t)]; + unsigned char _key[ZT_PEER_SECRET_KEY_LENGTH]; + const RuntimeEnvironment *RR; + _ClusterSendQueue *const _sendQueue; + void (*_sendFunction)(void *,unsigned int,const void *,unsigned int); + void *_sendFunctionArg; + int (*_addressToLocationFunction)(void *,const struct sockaddr_storage *,int *,int *,int *); + void *_addressToLocationFunctionArg; + const int32_t _x; + const int32_t _y; + const int32_t _z; + const uint16_t _id; + const std::vector _zeroTierPhysicalEndpoints; + // end immutable fields ----------------------------------------------------- + + struct _Member + { + unsigned char key[ZT_PEER_SECRET_KEY_LENGTH]; + + uint64_t lastReceivedAliveAnnouncement; + uint64_t lastAnnouncedAliveTo; + + uint64_t load; + uint64_t peers; + int32_t x,y,z; + + std::vector zeroTierPhysicalEndpoints; + + Buffer q; + + Mutex lock; + + inline void clear() + { + lastReceivedAliveAnnouncement = 0; + lastAnnouncedAliveTo = 0; + load = 0; + peers = 0; + x = 0; + y = 0; + z = 0; + zeroTierPhysicalEndpoints.clear(); + q.clear(); + } + + _Member() { this->clear(); } + ~_Member() { Utils::burn(key,sizeof(key)); } + }; + _Member *const _members; + + std::vector _memberIds; + Mutex _memberIds_m; + + struct _RemotePeer + { + _RemotePeer() : lastHavePeerReceived(0),lastSentWantPeer(0) {} + ~_RemotePeer() { Utils::burn(key,ZT_PEER_SECRET_KEY_LENGTH); } + uint64_t lastHavePeerReceived; + uint64_t lastSentWantPeer; + uint8_t key[ZT_PEER_SECRET_KEY_LENGTH]; // secret key from identity agreement + }; + std::map< std::pair,_RemotePeer > _remotePeers; // we need ordered behavior and lower_bound here + Mutex _remotePeers_m; + + uint64_t _lastFlushed; + uint64_t _lastCleanedRemotePeers; + uint64_t _lastCleanedQueue; +}; + +} // namespace ZeroTier + +#endif // ZT_ENABLE_CLUSTER + +#endif -- cgit v1.2.3 From 1613f42d0082cf6438ad0c62d89405ab82625f98 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 7 Nov 2017 14:44:46 -0800 Subject: Re-integrate in-filesystem DB into new controller DB structure. --- attic/JSONDB.cpp | 525 +++++++++++++++++++++++++++++++ attic/JSONDB.hpp | 192 +++++++++++ controller/DB.cpp | 314 ++++++++++++++++++ controller/DB.hpp | 116 +++++++ controller/EmbeddedNetworkController.hpp | 5 + controller/FileDB.cpp | 129 ++++++++ controller/FileDB.hpp | 47 +++ controller/JSONDB.cpp | 525 ------------------------------- controller/JSONDB.hpp | 192 ----------- controller/RethinkDB.cpp | 287 ++--------------- controller/RethinkDB.hpp | 111 +------ objects.mk | 2 + osdep/OSUtils.hpp | 15 +- 13 files changed, 1376 insertions(+), 1084 deletions(-) create mode 100644 attic/JSONDB.cpp create mode 100644 attic/JSONDB.hpp create mode 100644 controller/DB.cpp create mode 100644 controller/DB.hpp create mode 100644 controller/FileDB.cpp create mode 100644 controller/FileDB.hpp delete mode 100644 controller/JSONDB.cpp delete mode 100644 controller/JSONDB.hpp (limited to 'attic') diff --git a/attic/JSONDB.cpp b/attic/JSONDB.cpp new file mode 100644 index 00000000..67b13393 --- /dev/null +++ b/attic/JSONDB.cpp @@ -0,0 +1,525 @@ +/* + * 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 . + */ + +#include +#include +#include +#ifndef _WIN32 +#include +#include +#include +#include +#include +#include +#endif + +#include "JSONDB.hpp" +#include "EmbeddedNetworkController.hpp" + +namespace ZeroTier { + +static const nlohmann::json _EMPTY_JSON(nlohmann::json::object()); + +JSONDB::JSONDB(const std::string &basePath,EmbeddedNetworkController *parent) : + _parent(parent), + _basePath(basePath), + _rawInput(-1), + _rawOutput(-1), + _summaryThreadRun(true), + _dataReady(false) +{ +#ifndef __WINDOWS__ + if (_basePath == "-") { + // If base path is "-" we run in Central harnessed mode. We read pseudo-http-requests from stdin and write + // them to stdout. + _rawInput = STDIN_FILENO; + _rawOutput = STDOUT_FILENO; + fcntl(_rawInput,F_SETFL,O_NONBLOCK); + } else { +#endif + // Default mode of operation is to store files in the filesystem + OSUtils::mkdir(_basePath.c_str()); + OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions +#ifndef __WINDOWS__ + } +#endif + + _networks_m.lock(); // locked until data is loaded, etc. + + if (_rawInput < 0) { + _load(basePath); + _dataReady = true; + _networks_m.unlock(); + } else { + // In harnessed mode we leave the lock locked and wait for our initial DB from Central. + _summaryThread = Thread::start(this); + } +} + +JSONDB::~JSONDB() +{ + Thread t; + { + Mutex::Lock _l(_summaryThread_m); + _summaryThreadRun = false; + t = _summaryThread; + } + if (t) + Thread::join(t); +} + +bool JSONDB::writeRaw(const std::string &n,const std::string &obj) +{ + if (_rawOutput >= 0) { +#ifndef __WINDOWS__ + if (obj.length() > 0) { + Mutex::Lock _l(_rawLock); + //fprintf(stderr,"%s\n",obj.c_str()); + if ((long)write(_rawOutput,obj.data(),obj.length()) == (long)obj.length()) { + if (write(_rawOutput,"\n",1) == 1) + return true; + } + } else return true; +#endif + return false; + } else { + const std::string path(_genPath(n,true)); + if (!path.length()) + return false; + return OSUtils::writeFile(path.c_str(),obj); + } +} + +bool JSONDB::hasNetwork(const uint64_t networkId) const +{ + Mutex::Lock _l(_networks_m); + return (_networks.find(networkId) != _networks.end()); +} + +bool JSONDB::getNetwork(const uint64_t networkId,nlohmann::json &config) const +{ + Mutex::Lock _l(_networks_m); + const std::unordered_map::const_iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return false; + config = nlohmann::json::from_msgpack(i->second.config); + return true; +} + +bool JSONDB::getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const +{ + Mutex::Lock _l(_networks_m); + const std::unordered_map::const_iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return false; + ns = i->second.summaryInfo; + return true; +} + +int JSONDB::getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const +{ + Mutex::Lock _l(_networks_m); + const std::unordered_map::const_iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return 0; + const std::unordered_map< uint64_t,std::vector >::const_iterator j(i->second.members.find(nodeId)); + if (j == i->second.members.end()) + return 1; + networkConfig = nlohmann::json::from_msgpack(i->second.config); + memberConfig = nlohmann::json::from_msgpack(j->second); + ns = i->second.summaryInfo; + return 3; +} + +bool JSONDB::getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const +{ + Mutex::Lock _l(_networks_m); + const std::unordered_map::const_iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return false; + const std::unordered_map< uint64_t,std::vector >::const_iterator j(i->second.members.find(nodeId)); + if (j == i->second.members.end()) + return false; + memberConfig = nlohmann::json::from_msgpack(j->second); + return true; +} + +void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig) +{ + char n[64]; + OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); + writeRaw(n,OSUtils::jsonDump(networkConfig,-1)); + { + Mutex::Lock _l(_networks_m); + _NW &nw = _networks[networkId]; + nw.config = nlohmann::json::to_msgpack(networkConfig); + } + _recomputeSummaryInfo(networkId); +} + +void JSONDB::saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig) +{ + char n[256]; + OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); + writeRaw(n,OSUtils::jsonDump(memberConfig,-1)); + { + Mutex::Lock _l(_networks_m); + std::vector &m = _networks[networkId].members[nodeId]; + m = nlohmann::json::to_msgpack(memberConfig); + _members[nodeId].insert(networkId); + } + _recomputeSummaryInfo(networkId); +} + +nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId) +{ + if (_rawOutput >= 0) { + // In harnessed mode, DB deletes occur in the Central database and we do + // not need to erase files. + } else { + std::vector memberIds; + { + Mutex::Lock _l(_networks_m); + const std::unordered_map::iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return _EMPTY_JSON; + for(std::unordered_map< uint64_t,std::vector >::iterator m(i->second.members.begin());m!=i->second.members.end();++m) + memberIds.push_back(m->first); + } + for(std::vector::iterator m(memberIds.begin());m!=memberIds.end();++m) + eraseNetworkMember(networkId,*m,false); + + char n[256]; + OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); + const std::string path(_genPath(n,false)); + if (path.length()) + OSUtils::rm(path.c_str()); + } + + // This also erases all members from the memory cache + { + Mutex::Lock _l(_networks_m); + std::unordered_map::iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return _EMPTY_JSON; // sanity check, shouldn't happen + nlohmann::json tmp(nlohmann::json::from_msgpack(i->second.config)); + _networks.erase(i); + return tmp; + } +} + +nlohmann::json JSONDB::eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo) +{ + if (_rawOutput >= 0) { + // In harnessed mode, DB deletes occur in Central and we do not remove files. + } else { + char n[256]; + OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); + const std::string path(_genPath(n,false)); + if (path.length()) + OSUtils::rm(path.c_str()); + } + + { + Mutex::Lock _l(_networks_m); + _members[nodeId].erase(networkId); + std::unordered_map::iterator i(_networks.find(networkId)); + if (i == _networks.end()) + return _EMPTY_JSON; + std::unordered_map< uint64_t,std::vector >::iterator j(i->second.members.find(nodeId)); + if (j == i->second.members.end()) + return _EMPTY_JSON; + nlohmann::json tmp(j->second); + i->second.members.erase(j); + if (recomputeSummaryInfo) + _recomputeSummaryInfo(networkId); + return tmp; + } +} + +void JSONDB::threadMain() + throw() +{ +#ifndef __WINDOWS__ + fd_set readfds,nullfds; + char *const readbuf = (_rawInput >= 0) ? (new char[1048576]) : (char *)0; + std::string rawInputBuf; + FD_ZERO(&readfds); + FD_ZERO(&nullfds); + struct timeval tv; +#endif + + std::vector todo; + + while (_summaryThreadRun) { +#ifndef __WINDOWS__ + if (_rawInput < 0) { + Thread::sleep(25); + } else { + // In IPC mode we wait but also select() on STDIN to read database updates + FD_SET(_rawInput,&readfds); + tv.tv_sec = 0; + tv.tv_usec = 25000; + select(_rawInput+1,&readfds,&nullfds,&nullfds,&tv); + if (FD_ISSET(_rawInput,&readfds)) { + const long rn = (long)read(_rawInput,readbuf,1048576); + bool gotMessage = false; + for(long i=0;i 0) { + try { + const nlohmann::json obj(OSUtils::jsonParse(rawInputBuf)); + gotMessage = true; + + if (!_dataReady) { + _dataReady = true; + _networks_m.unlock(); + } + + if (obj.is_array()) { + for(unsigned long i=0;i::iterator ii(todo.begin());ii!=todo.end();++ii) { + const uint64_t networkId = *ii; + std::unordered_map::iterator n(_networks.find(networkId)); + if (n != _networks.end()) { + NetworkSummaryInfo &ns = n->second.summaryInfo; + ns.activeBridges.clear(); + ns.allocatedIps.clear(); + ns.authorizedMemberCount = 0; + ns.activeMemberCount = 0; + ns.totalMemberCount = 0; + ns.mostRecentDeauthTime = 0; + + for(std::unordered_map< uint64_t,std::vector >::const_iterator m(n->second.members.begin());m!=n->second.members.end();++m) { + try { + nlohmann::json member(nlohmann::json::from_msgpack(m->second)); + + if (OSUtils::jsonBool(member["authorized"],false)) { + ++ns.authorizedMemberCount; + + try { + const nlohmann::json &mlog = member["recentLog"]; + if ((mlog.is_array())&&(mlog.size() > 0)) { + const nlohmann::json &mlog1 = mlog[0]; + if (mlog1.is_object()) { + if ((now - OSUtils::jsonInt(mlog1["ts"],0ULL)) < (ZT_NETWORK_AUTOCONF_DELAY * 2)) + ++ns.activeMemberCount; + } + } + } catch ( ... ) {} + + try { + if (OSUtils::jsonBool(member["activeBridge"],false)) + ns.activeBridges.push_back(Address(m->first)); + } catch ( ... ) {} + + try { + const nlohmann::json &mips = member["ipAssignments"]; + if (mips.is_array()) { + for(unsigned long i=0;isecond.summaryInfoLastComputed = now; + } + } + } catch ( ... ) {} + + todo.clear(); + } + + if (!_dataReady) // sanity check + _networks_m.unlock(); + +#ifndef __WINDOWS__ + delete [] readbuf; +#endif +} + +bool JSONDB::_addOrUpdate(const nlohmann::json &j) +{ + try { + if (j.is_object()) { + std::string id(OSUtils::jsonString(j["id"],"0")); + const std::string objtype(OSUtils::jsonString(j["objtype"],"")); + if ((id.length() == 16)&&(objtype == "network")) { + + const uint64_t nwid = Utils::hexStrToU64(id.c_str()); + if (nwid) { + bool update; + { + Mutex::Lock _l(_networks_m); + _NW &nw = _networks[nwid]; + update = !nw.config.empty(); + nw.config = nlohmann::json::to_msgpack(j); + } + if (update) + _parent->onNetworkUpdate(nwid); + _recomputeSummaryInfo(nwid); + return true; + } + + } else if ((id.length() == 10)&&(objtype == "member")) { + + const uint64_t mid = Utils::hexStrToU64(id.c_str()); + const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str()); + if ((mid)&&(nwid)) { + bool update = false; + bool deauth = false; + { + Mutex::Lock _l(_networks_m); + std::vector &m = _networks[nwid].members[mid]; + if (!m.empty()) { + update = true; + nlohmann::json oldm(nlohmann::json::from_msgpack(m)); + deauth = ((OSUtils::jsonBool(oldm["authorized"],false))&&(!OSUtils::jsonBool(j["authorized"],false))); + } + m = nlohmann::json::to_msgpack(j); + _members[mid].insert(nwid); + } + if (update) { + _parent->onNetworkMemberUpdate(nwid,mid); + if (deauth) + _parent->onNetworkMemberDeauthorize(nwid,mid); + } + _recomputeSummaryInfo(nwid); + return true; + } + + } else if (objtype == "_delete") { // pseudo-object-type, only used in Central harnessed mode + + const std::string deleteType(OSUtils::jsonString(j["deleteType"],"")); + id = OSUtils::jsonString(j["deleteId"],""); + if ((deleteType == "network")&&(id.length() == 16)) { + eraseNetwork(Utils::hexStrToU64(id.c_str())); + } else if ((deleteType == "member")&&(id.length() == 10)) { + const std::string networkId(OSUtils::jsonString(j["deleteNetworkId"],"")); + const uint64_t nwid = Utils::hexStrToU64(networkId.c_str()); + const uint64_t mid = Utils::hexStrToU64(id.c_str()); + if (networkId.length() == 16) + eraseNetworkMember(nwid,mid,true); + _parent->onNetworkMemberDeauthorize(nwid,mid); + } + + } + } + } catch ( ... ) {} + return false; +} + +bool JSONDB::_load(const std::string &p) +{ + // This is not used in stdin/stdout mode. Instead data is populated by + // sending it all to stdin. + + std::vector dl(OSUtils::listDirectory(p.c_str(),true)); + for(std::vector::const_iterator di(dl.begin());di!=dl.end();++di) { + if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) { + std::string buf; + if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) { + try { + _addOrUpdate(OSUtils::jsonParse(buf)); + } catch ( ... ) {} + } + } else { + this->_load((p + ZT_PATH_SEPARATOR_S + *di)); + } + } + + return true; +} + +void JSONDB::_recomputeSummaryInfo(const uint64_t networkId) +{ + Mutex::Lock _l(_summaryThread_m); + if (std::find(_summaryThreadToDo.begin(),_summaryThreadToDo.end(),networkId) == _summaryThreadToDo.end()) + _summaryThreadToDo.push_back(networkId); + if (!_summaryThread) + _summaryThread = Thread::start(this); +} + +std::string JSONDB::_genPath(const std::string &n,bool create) +{ + std::vector pt(OSUtils::split(n.c_str(),"/","","")); + if (pt.size() == 0) + return std::string(); + + std::string p(_basePath); + if (create) OSUtils::mkdir(p.c_str()); + for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i. + */ + +#ifndef ZT_JSONDB_HPP +#define ZT_JSONDB_HPP + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../node/Constants.hpp" +#include "../node/Utils.hpp" +#include "../node/InetAddress.hpp" +#include "../node/Mutex.hpp" +#include "../ext/json/json.hpp" +#include "../osdep/OSUtils.hpp" +#include "../osdep/Thread.hpp" + +namespace ZeroTier { + +class EmbeddedNetworkController; + +/** + * Hierarchical JSON store that persists into the filesystem or via HTTP + */ +class JSONDB +{ +public: + struct NetworkSummaryInfo + { + NetworkSummaryInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {} + std::vector
activeBridges; + std::vector allocatedIps; + unsigned long authorizedMemberCount; + unsigned long activeMemberCount; + unsigned long totalMemberCount; + int64_t mostRecentDeauthTime; + }; + + JSONDB(const std::string &basePath,EmbeddedNetworkController *parent); + ~JSONDB(); + + /** + * Write a JSON object to the data store + * + * It's important that obj contain a valid JSON object with no newlines (jsonDump with -1 + * for indentation), since newline-delimited JSON is what nodeJS's IPC speaks and this + * is important in Central-harnessed mode. + * + * @param n Path name of object + * @param obj Object in single-line no-CRs JSON object format (OSUtils::jsonDump(obj,-1)) + * @return True if write appears successful + */ + bool writeRaw(const std::string &n,const std::string &obj); + + bool hasNetwork(const uint64_t networkId) const; + + bool getNetwork(const uint64_t networkId,nlohmann::json &config) const; + + bool getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const; + + /** + * @return Bit mask: 0 == none, 1 == network only, 3 == network and member + */ + int getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const; + + bool getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const; + + void saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig); + + void saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig); + + nlohmann::json eraseNetwork(const uint64_t networkId); + + nlohmann::json eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo = true); + + std::vector networkIds() const + { + std::vector r; + Mutex::Lock _l(_networks_m); + for(std::unordered_map::const_iterator n(_networks.begin());n!=_networks.end();++n) + r.push_back(n->first); + return r; + } + + inline unsigned long memberCount(const uint64_t networkId) + { + Mutex::Lock _l(_networks_m); + std::unordered_map::const_iterator i(_networks.find(networkId)); + if (i != _networks.end()) + return (unsigned long)i->second.members.size(); + return 0; + } + + template + inline void eachMember(const uint64_t networkId,F func) + { + Mutex::Lock _l(_networks_m); + std::unordered_map::const_iterator i(_networks.find(networkId)); + if (i != _networks.end()) { + for(std::unordered_map< uint64_t,std::vector >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) { + try { + func(networkId,m->first,nlohmann::json::from_msgpack(m->second)); + } catch ( ... ) {} + } + } + } + + template + inline void eachId(F func) + { + Mutex::Lock _l(_networks_m); + for(std::unordered_map::const_iterator i(_networks.begin());i!=_networks.end();++i) { + for(std::unordered_map< uint64_t,std::vector >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) { + try { + func(i->first,m->first); + } catch ( ... ) {} + } + } + } + + inline std::vector networksForMember(const uint64_t nodeId) + { + Mutex::Lock _l(_networks_m); + std::unordered_map< uint64_t,std::unordered_set< uint64_t > >::const_iterator m(_members.find(nodeId)); + if (m != _members.end()) { + return std::vector(m->second.begin(),m->second.end()); + } else { + return std::vector(); + } + } + + void threadMain() + throw(); + +private: + bool _addOrUpdate(const nlohmann::json &j); + bool _load(const std::string &p); + void _recomputeSummaryInfo(const uint64_t networkId); + std::string _genPath(const std::string &n,bool create); + + EmbeddedNetworkController *const _parent; + std::string _basePath; + int _rawInput,_rawOutput; + Mutex _rawLock; + + Thread _summaryThread; + std::vector _summaryThreadToDo; + volatile bool _summaryThreadRun; + Mutex _summaryThread_m; + + struct _NW + { + _NW() : summaryInfoLastComputed(0) {} + std::vector config; + NetworkSummaryInfo summaryInfo; + uint64_t summaryInfoLastComputed; + std::unordered_map< uint64_t,std::vector > members; + }; + + std::unordered_map< uint64_t,_NW > _networks; + std::unordered_map< uint64_t,std::unordered_set< uint64_t > > _members; + bool _dataReady; + Mutex _networks_m; +}; + +} // namespace ZeroTier + +#endif diff --git a/controller/DB.cpp b/controller/DB.cpp new file mode 100644 index 00000000..4a5688e3 --- /dev/null +++ b/controller/DB.cpp @@ -0,0 +1,314 @@ +/* + * 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 . + */ + +#include "DB.hpp" +#include "EmbeddedNetworkController.hpp" + +#include +#include +#include + +using json = nlohmann::json; + +namespace ZeroTier { + +DB::DB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) : + _controller(nc), + _myAddress(myAddress), + _path((path) ? path : "") +{ + { + char tmp[32]; + _myAddress.toString(tmp); + _myAddressStr = tmp; + } +} + +DB::~DB() +{ +} + +bool DB::get(const uint64_t networkId,nlohmann::json &network) +{ + waitForReady(); + std::shared_ptr<_Network> nw; + { + std::lock_guard l(_networks_l); + auto nwi = _networks.find(networkId); + if (nwi == _networks.end()) + return false; + nw = nwi->second; + } + { + std::lock_guard l2(nw->lock); + network = nw->config; + } + return true; +} + +bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member) +{ + waitForReady(); + std::shared_ptr<_Network> nw; + { + std::lock_guard l(_networks_l); + auto nwi = _networks.find(networkId); + if (nwi == _networks.end()) + return false; + nw = nwi->second; + } + { + std::lock_guard l2(nw->lock); + network = nw->config; + auto m = nw->members.find(memberId); + if (m == nw->members.end()) + return false; + member = m->second; + } + return true; +} + +bool DB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info) +{ + waitForReady(); + std::shared_ptr<_Network> nw; + { + std::lock_guard l(_networks_l); + auto nwi = _networks.find(networkId); + if (nwi == _networks.end()) + return false; + nw = nwi->second; + } + { + std::lock_guard l2(nw->lock); + network = nw->config; + _fillSummaryInfo(nw,info); + auto m = nw->members.find(memberId); + if (m == nw->members.end()) + return false; + member = m->second; + } + return true; +} + +bool DB::get(const uint64_t networkId,nlohmann::json &network,std::vector &members) +{ + waitForReady(); + std::shared_ptr<_Network> nw; + { + std::lock_guard l(_networks_l); + auto nwi = _networks.find(networkId); + if (nwi == _networks.end()) + return false; + nw = nwi->second; + } + { + std::lock_guard l2(nw->lock); + network = nw->config; + for(auto m=nw->members.begin();m!=nw->members.end();++m) + members.push_back(m->second); + } + return true; +} + +bool DB::summary(const uint64_t networkId,NetworkSummaryInfo &info) +{ + waitForReady(); + std::shared_ptr<_Network> nw; + { + std::lock_guard l(_networks_l); + auto nwi = _networks.find(networkId); + if (nwi == _networks.end()) + return false; + nw = nwi->second; + } + { + std::lock_guard l2(nw->lock); + _fillSummaryInfo(nw,info); + } + return true; +} + +void DB::networks(std::vector &networks) +{ + waitForReady(); + std::lock_guard l(_networks_l); + networks.reserve(_networks.size() + 1); + for(auto n=_networks.begin();n!=_networks.end();++n) + networks.push_back(n->first); +} + +void DB::_memberChanged(nlohmann::json &old,nlohmann::json &member,bool push) +{ + uint64_t memberId = 0; + uint64_t networkId = 0; + bool isAuth = false; + bool wasAuth = false; + std::shared_ptr<_Network> nw; + + if (old.is_object()) { + json &config = old["config"]; + if (config.is_object()) { + memberId = OSUtils::jsonIntHex(config["id"],0ULL); + networkId = OSUtils::jsonIntHex(config["nwid"],0ULL); + if ((memberId)&&(networkId)) { + { + std::lock_guard l(_networks_l); + auto nw2 = _networks.find(networkId); + if (nw2 != _networks.end()) + nw = nw2->second; + } + if (nw) { + std::lock_guard l(nw->lock); + if (OSUtils::jsonBool(config["activeBridge"],false)) + nw->activeBridgeMembers.erase(memberId); + wasAuth = OSUtils::jsonBool(config["authorized"],false); + if (wasAuth) + nw->authorizedMembers.erase(memberId); + json &ips = config["ipAssignments"]; + if (ips.is_array()) { + for(unsigned long i=0;iallocatedIps.erase(ipa); + } + } + } + } + } + } + } + + if (member.is_object()) { + json &config = member["config"]; + if (config.is_object()) { + if (!nw) { + memberId = OSUtils::jsonIntHex(config["id"],0ULL); + networkId = OSUtils::jsonIntHex(config["nwid"],0ULL); + if ((!memberId)||(!networkId)) + return; + std::lock_guard l(_networks_l); + std::shared_ptr<_Network> &nw2 = _networks[networkId]; + if (!nw2) + nw2.reset(new _Network); + nw = nw2; + } + + { + std::lock_guard l(nw->lock); + + nw->members[memberId] = config; + + if (OSUtils::jsonBool(config["activeBridge"],false)) + nw->activeBridgeMembers.insert(memberId); + isAuth = OSUtils::jsonBool(config["authorized"],false); + if (isAuth) + nw->authorizedMembers.insert(memberId); + json &ips = config["ipAssignments"]; + if (ips.is_array()) { + for(unsigned long i=0;iallocatedIps.insert(ipa); + } + } + } + + if (!isAuth) { + const int64_t ldt = (int64_t)OSUtils::jsonInt(config["lastDeauthorizedTime"],0ULL); + if (ldt > nw->mostRecentDeauthTime) + nw->mostRecentDeauthTime = ldt; + } + } + + if (push) + _controller->onNetworkMemberUpdate(networkId,memberId); + } + } else if (memberId) { + if (nw) { + std::lock_guard l(nw->lock); + nw->members.erase(memberId); + } + if (networkId) { + std::lock_guard l(_networks_l); + auto er = _networkByMember.equal_range(memberId); + for(auto i=er.first;i!=er.second;++i) { + if (i->second == networkId) { + _networkByMember.erase(i); + break; + } + } + } + } + + if ((push)&&((wasAuth)&&(!isAuth)&&(networkId)&&(memberId))) + _controller->onNetworkMemberDeauthorize(networkId,memberId); +} + +void DB::_networkChanged(nlohmann::json &old,nlohmann::json &network,bool push) +{ + if (network.is_object()) { + json &config = network["config"]; + if (config.is_object()) { + const std::string ids = config["id"]; + const uint64_t id = Utils::hexStrToU64(ids.c_str()); + if (id) { + std::shared_ptr<_Network> nw; + { + std::lock_guard l(_networks_l); + std::shared_ptr<_Network> &nw2 = _networks[id]; + if (!nw2) + nw2.reset(new _Network); + nw = nw2; + } + { + std::lock_guard l2(nw->lock); + nw->config = config; + } + if (push) + _controller->onNetworkUpdate(id); + } + } + } else if (old.is_object()) { + const std::string ids = old["id"]; + const uint64_t id = Utils::hexStrToU64(ids.c_str()); + if (id) { + std::lock_guard l(_networks_l); + _networks.erase(id); + } + } +} + +void DB::_fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info) +{ + for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab) + info.activeBridges.push_back(Address(*ab)); + for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip) + info.allocatedIps.push_back(*ip); + info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size(); + info.totalMemberCount = (unsigned long)nw->members.size(); + info.mostRecentDeauthTime = nw->mostRecentDeauthTime; +} + +} // namespace ZeroTier diff --git a/controller/DB.hpp b/controller/DB.hpp new file mode 100644 index 00000000..dfc8ac95 --- /dev/null +++ b/controller/DB.hpp @@ -0,0 +1,116 @@ +/* + * 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_CONTROLLER_DB_HPP +#define ZT_CONTROLLER_DB_HPP + +#include "../node/Constants.hpp" +#include "../node/Address.hpp" +#include "../node/InetAddress.hpp" +#include "../osdep/OSUtils.hpp" +#include "../osdep/BlockingQueue.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "../ext/json/json.hpp" + +#define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 2 + +namespace ZeroTier +{ + +class EmbeddedNetworkController; + +/** + * Base class with common infrastructure for all controller DB implementations + */ +class DB +{ +public: + struct NetworkSummaryInfo + { + NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {} + std::vector
activeBridges; + std::vector allocatedIps; + unsigned long authorizedMemberCount; + unsigned long totalMemberCount; + int64_t mostRecentDeauthTime; + }; + + DB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path); + virtual ~DB(); + + virtual bool waitForReady() = 0; + + inline bool hasNetwork(const uint64_t networkId) const + { + std::lock_guard l(_networks_l); + return (_networks.find(networkId) != _networks.end()); + } + + bool get(const uint64_t networkId,nlohmann::json &network); + bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member); + bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info); + bool get(const uint64_t networkId,nlohmann::json &network,std::vector &members); + + bool summary(const uint64_t networkId,NetworkSummaryInfo &info); + + void networks(std::vector &networks); + + virtual void save(const nlohmann::json &record) = 0; + + virtual void eraseNetwork(const uint64_t networkId) = 0; + + virtual void eraseMember(const uint64_t networkId,const uint64_t memberId) = 0; + +protected: + struct _Network + { + _Network() : mostRecentDeauthTime(0) {} + nlohmann::json config; + std::unordered_map members; + std::unordered_set activeBridgeMembers; + std::unordered_set authorizedMembers; + std::unordered_set allocatedIps; + int64_t mostRecentDeauthTime; + std::mutex lock; + }; + + void _memberChanged(nlohmann::json &old,nlohmann::json &member,bool push); + void _networkChanged(nlohmann::json &old,nlohmann::json &network,bool push); + void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info); + + EmbeddedNetworkController *const _controller; + const Address _myAddress; + const std::string _path; + std::string _myAddressStr; + + std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks; + std::unordered_multimap< uint64_t,uint64_t > _networkByMember; + mutable std::mutex _networks_l; +}; + +} // namespace ZeroTier + +#endif diff --git a/controller/EmbeddedNetworkController.hpp b/controller/EmbeddedNetworkController.hpp index f9b6fb5a..bc59b359 100644 --- a/controller/EmbeddedNetworkController.hpp +++ b/controller/EmbeddedNetworkController.hpp @@ -45,13 +45,18 @@ #ifdef ZT_CONTROLLER_USE_RETHINKDB #include "RethinkDB.hpp" +#else +#include "FileDB.hpp" #endif namespace ZeroTier { #ifdef ZT_CONTROLLER_USE_RETHINKDB typedef RethinkDB ControllerDB; +#else +typedef FileDB ControllerDB; #endif + class Node; class EmbeddedNetworkController : public NetworkController diff --git a/controller/FileDB.cpp b/controller/FileDB.cpp new file mode 100644 index 00000000..b48d5e87 --- /dev/null +++ b/controller/FileDB.cpp @@ -0,0 +1,129 @@ +/* + * 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 . + */ + +#include "FileDB.hpp" + +namespace ZeroTier +{ + +FileDB::FileDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) : + DB(nc,myAddress,path), + _networksPath(_path + ZT_PATH_SEPARATOR_S + "network") +{ + OSUtils::mkdir(_path.c_str()); + OSUtils::lockDownFile(_path.c_str(),true); + + std::vector networks(OSUtils::listDirectory(_networksPath.c_str(),false)); + std::string buf; + for(auto n=networks.begin();n!=networks.end();++n) { + buf.clear(); + if ((n->length() == 21)&&(OSUtils::readFile((_networksPath + ZT_PATH_SEPARATOR_S + *n).c_str(),buf))) { + try { + nlohmann::json network(OSUtils::jsonParse(buf)); + const std::string nwids = network["id"]; + if (nwids.length() == 16) { + nlohmann::json nullJson; + _networkChanged(nullJson,network,false); + std::string membersPath(_networksPath + ZT_PATH_SEPARATOR_S + nwids + ZT_PATH_SEPARATOR_S "member"); + std::vector members(OSUtils::listDirectory(membersPath.c_str(),false)); + for(auto m=members.begin();m!=members.end();++m) { + buf.clear(); + if ((m->length() == 15)&&(OSUtils::readFile((membersPath + ZT_PATH_SEPARATOR_S + *m).c_str(),buf))) { + try { + nlohmann::json member(OSUtils::jsonParse(buf)); + const std::string addrs = member["id"]; + if (addrs.length() == 10) { + nlohmann::json nullJson2; + _memberChanged(nullJson2,member,false); + } + } catch ( ... ) {} + } + } + } + } catch ( ... ) {} + } + } +} + +FileDB::~FileDB() +{ +} + +bool FileDB::waitForReady() +{ + return true; +} + +void FileDB::save(const nlohmann::json &record) +{ + char p1[16384],p2[16384]; + try { + nlohmann::json rec(record); + const std::string objtype = rec["objtype"]; + if (objtype == "network") { + const uint64_t nwid = OSUtils::jsonIntHex(rec["id"],0ULL); + if (nwid) { + nlohmann::json old; + get(nwid,old); + + OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json.new",_networksPath.c_str(),nwid); + OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),nwid); + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1))) + fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); + OSUtils::rename(p1,p2); + + _networkChanged(old,rec,true); + } + } else if (objtype == "member") { + const uint64_t id = OSUtils::jsonIntHex(rec["id"],0ULL); + const uint64_t nwid = OSUtils::jsonIntHex(rec["nwid"],0ULL); + if ((id)&&(nwid)) { + nlohmann::json network,old; + get(nwid,network,id,old); + + OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member" ZT_PATH_SEPARATOR_S "%.10llx.json.new",_networksPath.c_str(),nwid); + OSUtils::ztsnprintf(p2,sizeof(p2),"%s" ZT_PATH_SEPARATOR_S "%.16llx" ZT_PATH_SEPARATOR_S "member" ZT_PATH_SEPARATOR_S "%.10llx.json",_networksPath.c_str(),nwid); + if (!OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1))) + fprintf(stderr,"WARNING: controller unable to write to path: %s" ZT_EOL_S,p1); + OSUtils::rename(p1,p2); + + _memberChanged(old,rec,true); + } + } else if (objtype == "trace") { + const std::string id = rec["id"]; + OSUtils::ztsnprintf(p1,sizeof(p1),"%s" ZT_PATH_SEPARATOR_S "trace" ZT_PATH_SEPARATOR_S "%s.json",_path.c_str(),id.c_str()); + OSUtils::writeFile(p1,OSUtils::jsonDump(rec,-1)); + } + } catch ( ... ) {} // drop invalid records missing fields +} + +void FileDB::eraseNetwork(const uint64_t networkId) +{ + nlohmann::json network,nullJson; + get(networkId,network); + char p[16384]; + OSUtils::ztsnprintf(p,sizeof(p),"%s" ZT_PATH_SEPARATOR_S "%.16llx.json",_networksPath.c_str(),networkId); + OSUtils::rm(p); + _networkChanged(network,nullJson,true); +} + +void FileDB::eraseMember(const uint64_t networkId,const uint64_t memberId) +{ +} + +} // namespace ZeroTier diff --git a/controller/FileDB.hpp b/controller/FileDB.hpp new file mode 100644 index 00000000..fe9869b9 --- /dev/null +++ b/controller/FileDB.hpp @@ -0,0 +1,47 @@ +/* + * 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_CONTROLLER_FILEDB_HPP +#define ZT_CONTROLLER_FILEDB_HPP + +#include "DB.hpp" + +namespace ZeroTier +{ + +class FileDB : public DB +{ +public: + FileDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path); + virtual ~FileDB(); + + virtual bool waitForReady(); + + virtual void save(const nlohmann::json &record); + + virtual void eraseNetwork(const uint64_t networkId); + + virtual void eraseMember(const uint64_t networkId,const uint64_t memberId); + +protected: + std::string _networksPath; +}; + +} // namespace ZeroTier + +#endif diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp deleted file mode 100644 index 67b13393..00000000 --- a/controller/JSONDB.cpp +++ /dev/null @@ -1,525 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#ifndef _WIN32 -#include -#include -#include -#include -#include -#include -#endif - -#include "JSONDB.hpp" -#include "EmbeddedNetworkController.hpp" - -namespace ZeroTier { - -static const nlohmann::json _EMPTY_JSON(nlohmann::json::object()); - -JSONDB::JSONDB(const std::string &basePath,EmbeddedNetworkController *parent) : - _parent(parent), - _basePath(basePath), - _rawInput(-1), - _rawOutput(-1), - _summaryThreadRun(true), - _dataReady(false) -{ -#ifndef __WINDOWS__ - if (_basePath == "-") { - // If base path is "-" we run in Central harnessed mode. We read pseudo-http-requests from stdin and write - // them to stdout. - _rawInput = STDIN_FILENO; - _rawOutput = STDOUT_FILENO; - fcntl(_rawInput,F_SETFL,O_NONBLOCK); - } else { -#endif - // Default mode of operation is to store files in the filesystem - OSUtils::mkdir(_basePath.c_str()); - OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions -#ifndef __WINDOWS__ - } -#endif - - _networks_m.lock(); // locked until data is loaded, etc. - - if (_rawInput < 0) { - _load(basePath); - _dataReady = true; - _networks_m.unlock(); - } else { - // In harnessed mode we leave the lock locked and wait for our initial DB from Central. - _summaryThread = Thread::start(this); - } -} - -JSONDB::~JSONDB() -{ - Thread t; - { - Mutex::Lock _l(_summaryThread_m); - _summaryThreadRun = false; - t = _summaryThread; - } - if (t) - Thread::join(t); -} - -bool JSONDB::writeRaw(const std::string &n,const std::string &obj) -{ - if (_rawOutput >= 0) { -#ifndef __WINDOWS__ - if (obj.length() > 0) { - Mutex::Lock _l(_rawLock); - //fprintf(stderr,"%s\n",obj.c_str()); - if ((long)write(_rawOutput,obj.data(),obj.length()) == (long)obj.length()) { - if (write(_rawOutput,"\n",1) == 1) - return true; - } - } else return true; -#endif - return false; - } else { - const std::string path(_genPath(n,true)); - if (!path.length()) - return false; - return OSUtils::writeFile(path.c_str(),obj); - } -} - -bool JSONDB::hasNetwork(const uint64_t networkId) const -{ - Mutex::Lock _l(_networks_m); - return (_networks.find(networkId) != _networks.end()); -} - -bool JSONDB::getNetwork(const uint64_t networkId,nlohmann::json &config) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return false; - config = nlohmann::json::from_msgpack(i->second.config); - return true; -} - -bool JSONDB::getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return false; - ns = i->second.summaryInfo; - return true; -} - -int JSONDB::getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return 0; - const std::unordered_map< uint64_t,std::vector >::const_iterator j(i->second.members.find(nodeId)); - if (j == i->second.members.end()) - return 1; - networkConfig = nlohmann::json::from_msgpack(i->second.config); - memberConfig = nlohmann::json::from_msgpack(j->second); - ns = i->second.summaryInfo; - return 3; -} - -bool JSONDB::getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return false; - const std::unordered_map< uint64_t,std::vector >::const_iterator j(i->second.members.find(nodeId)); - if (j == i->second.members.end()) - return false; - memberConfig = nlohmann::json::from_msgpack(j->second); - return true; -} - -void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig) -{ - char n[64]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); - writeRaw(n,OSUtils::jsonDump(networkConfig,-1)); - { - Mutex::Lock _l(_networks_m); - _NW &nw = _networks[networkId]; - nw.config = nlohmann::json::to_msgpack(networkConfig); - } - _recomputeSummaryInfo(networkId); -} - -void JSONDB::saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig) -{ - char n[256]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); - writeRaw(n,OSUtils::jsonDump(memberConfig,-1)); - { - Mutex::Lock _l(_networks_m); - std::vector &m = _networks[networkId].members[nodeId]; - m = nlohmann::json::to_msgpack(memberConfig); - _members[nodeId].insert(networkId); - } - _recomputeSummaryInfo(networkId); -} - -nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId) -{ - if (_rawOutput >= 0) { - // In harnessed mode, DB deletes occur in the Central database and we do - // not need to erase files. - } else { - std::vector memberIds; - { - Mutex::Lock _l(_networks_m); - const std::unordered_map::iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return _EMPTY_JSON; - for(std::unordered_map< uint64_t,std::vector >::iterator m(i->second.members.begin());m!=i->second.members.end();++m) - memberIds.push_back(m->first); - } - for(std::vector::iterator m(memberIds.begin());m!=memberIds.end();++m) - eraseNetworkMember(networkId,*m,false); - - char n[256]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); - const std::string path(_genPath(n,false)); - if (path.length()) - OSUtils::rm(path.c_str()); - } - - // This also erases all members from the memory cache - { - Mutex::Lock _l(_networks_m); - std::unordered_map::iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return _EMPTY_JSON; // sanity check, shouldn't happen - nlohmann::json tmp(nlohmann::json::from_msgpack(i->second.config)); - _networks.erase(i); - return tmp; - } -} - -nlohmann::json JSONDB::eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo) -{ - if (_rawOutput >= 0) { - // In harnessed mode, DB deletes occur in Central and we do not remove files. - } else { - char n[256]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); - const std::string path(_genPath(n,false)); - if (path.length()) - OSUtils::rm(path.c_str()); - } - - { - Mutex::Lock _l(_networks_m); - _members[nodeId].erase(networkId); - std::unordered_map::iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return _EMPTY_JSON; - std::unordered_map< uint64_t,std::vector >::iterator j(i->second.members.find(nodeId)); - if (j == i->second.members.end()) - return _EMPTY_JSON; - nlohmann::json tmp(j->second); - i->second.members.erase(j); - if (recomputeSummaryInfo) - _recomputeSummaryInfo(networkId); - return tmp; - } -} - -void JSONDB::threadMain() - throw() -{ -#ifndef __WINDOWS__ - fd_set readfds,nullfds; - char *const readbuf = (_rawInput >= 0) ? (new char[1048576]) : (char *)0; - std::string rawInputBuf; - FD_ZERO(&readfds); - FD_ZERO(&nullfds); - struct timeval tv; -#endif - - std::vector todo; - - while (_summaryThreadRun) { -#ifndef __WINDOWS__ - if (_rawInput < 0) { - Thread::sleep(25); - } else { - // In IPC mode we wait but also select() on STDIN to read database updates - FD_SET(_rawInput,&readfds); - tv.tv_sec = 0; - tv.tv_usec = 25000; - select(_rawInput+1,&readfds,&nullfds,&nullfds,&tv); - if (FD_ISSET(_rawInput,&readfds)) { - const long rn = (long)read(_rawInput,readbuf,1048576); - bool gotMessage = false; - for(long i=0;i 0) { - try { - const nlohmann::json obj(OSUtils::jsonParse(rawInputBuf)); - gotMessage = true; - - if (!_dataReady) { - _dataReady = true; - _networks_m.unlock(); - } - - if (obj.is_array()) { - for(unsigned long i=0;i::iterator ii(todo.begin());ii!=todo.end();++ii) { - const uint64_t networkId = *ii; - std::unordered_map::iterator n(_networks.find(networkId)); - if (n != _networks.end()) { - NetworkSummaryInfo &ns = n->second.summaryInfo; - ns.activeBridges.clear(); - ns.allocatedIps.clear(); - ns.authorizedMemberCount = 0; - ns.activeMemberCount = 0; - ns.totalMemberCount = 0; - ns.mostRecentDeauthTime = 0; - - for(std::unordered_map< uint64_t,std::vector >::const_iterator m(n->second.members.begin());m!=n->second.members.end();++m) { - try { - nlohmann::json member(nlohmann::json::from_msgpack(m->second)); - - if (OSUtils::jsonBool(member["authorized"],false)) { - ++ns.authorizedMemberCount; - - try { - const nlohmann::json &mlog = member["recentLog"]; - if ((mlog.is_array())&&(mlog.size() > 0)) { - const nlohmann::json &mlog1 = mlog[0]; - if (mlog1.is_object()) { - if ((now - OSUtils::jsonInt(mlog1["ts"],0ULL)) < (ZT_NETWORK_AUTOCONF_DELAY * 2)) - ++ns.activeMemberCount; - } - } - } catch ( ... ) {} - - try { - if (OSUtils::jsonBool(member["activeBridge"],false)) - ns.activeBridges.push_back(Address(m->first)); - } catch ( ... ) {} - - try { - const nlohmann::json &mips = member["ipAssignments"]; - if (mips.is_array()) { - for(unsigned long i=0;isecond.summaryInfoLastComputed = now; - } - } - } catch ( ... ) {} - - todo.clear(); - } - - if (!_dataReady) // sanity check - _networks_m.unlock(); - -#ifndef __WINDOWS__ - delete [] readbuf; -#endif -} - -bool JSONDB::_addOrUpdate(const nlohmann::json &j) -{ - try { - if (j.is_object()) { - std::string id(OSUtils::jsonString(j["id"],"0")); - const std::string objtype(OSUtils::jsonString(j["objtype"],"")); - if ((id.length() == 16)&&(objtype == "network")) { - - const uint64_t nwid = Utils::hexStrToU64(id.c_str()); - if (nwid) { - bool update; - { - Mutex::Lock _l(_networks_m); - _NW &nw = _networks[nwid]; - update = !nw.config.empty(); - nw.config = nlohmann::json::to_msgpack(j); - } - if (update) - _parent->onNetworkUpdate(nwid); - _recomputeSummaryInfo(nwid); - return true; - } - - } else if ((id.length() == 10)&&(objtype == "member")) { - - const uint64_t mid = Utils::hexStrToU64(id.c_str()); - const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str()); - if ((mid)&&(nwid)) { - bool update = false; - bool deauth = false; - { - Mutex::Lock _l(_networks_m); - std::vector &m = _networks[nwid].members[mid]; - if (!m.empty()) { - update = true; - nlohmann::json oldm(nlohmann::json::from_msgpack(m)); - deauth = ((OSUtils::jsonBool(oldm["authorized"],false))&&(!OSUtils::jsonBool(j["authorized"],false))); - } - m = nlohmann::json::to_msgpack(j); - _members[mid].insert(nwid); - } - if (update) { - _parent->onNetworkMemberUpdate(nwid,mid); - if (deauth) - _parent->onNetworkMemberDeauthorize(nwid,mid); - } - _recomputeSummaryInfo(nwid); - return true; - } - - } else if (objtype == "_delete") { // pseudo-object-type, only used in Central harnessed mode - - const std::string deleteType(OSUtils::jsonString(j["deleteType"],"")); - id = OSUtils::jsonString(j["deleteId"],""); - if ((deleteType == "network")&&(id.length() == 16)) { - eraseNetwork(Utils::hexStrToU64(id.c_str())); - } else if ((deleteType == "member")&&(id.length() == 10)) { - const std::string networkId(OSUtils::jsonString(j["deleteNetworkId"],"")); - const uint64_t nwid = Utils::hexStrToU64(networkId.c_str()); - const uint64_t mid = Utils::hexStrToU64(id.c_str()); - if (networkId.length() == 16) - eraseNetworkMember(nwid,mid,true); - _parent->onNetworkMemberDeauthorize(nwid,mid); - } - - } - } - } catch ( ... ) {} - return false; -} - -bool JSONDB::_load(const std::string &p) -{ - // This is not used in stdin/stdout mode. Instead data is populated by - // sending it all to stdin. - - std::vector dl(OSUtils::listDirectory(p.c_str(),true)); - for(std::vector::const_iterator di(dl.begin());di!=dl.end();++di) { - if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) { - std::string buf; - if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) { - try { - _addOrUpdate(OSUtils::jsonParse(buf)); - } catch ( ... ) {} - } - } else { - this->_load((p + ZT_PATH_SEPARATOR_S + *di)); - } - } - - return true; -} - -void JSONDB::_recomputeSummaryInfo(const uint64_t networkId) -{ - Mutex::Lock _l(_summaryThread_m); - if (std::find(_summaryThreadToDo.begin(),_summaryThreadToDo.end(),networkId) == _summaryThreadToDo.end()) - _summaryThreadToDo.push_back(networkId); - if (!_summaryThread) - _summaryThread = Thread::start(this); -} - -std::string JSONDB::_genPath(const std::string &n,bool create) -{ - std::vector pt(OSUtils::split(n.c_str(),"/","","")); - if (pt.size() == 0) - return std::string(); - - std::string p(_basePath); - if (create) OSUtils::mkdir(p.c_str()); - for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i. - */ - -#ifndef ZT_JSONDB_HPP -#define ZT_JSONDB_HPP - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Utils.hpp" -#include "../node/InetAddress.hpp" -#include "../node/Mutex.hpp" -#include "../ext/json/json.hpp" -#include "../osdep/OSUtils.hpp" -#include "../osdep/Thread.hpp" - -namespace ZeroTier { - -class EmbeddedNetworkController; - -/** - * Hierarchical JSON store that persists into the filesystem or via HTTP - */ -class JSONDB -{ -public: - struct NetworkSummaryInfo - { - NetworkSummaryInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {} - std::vector
activeBridges; - std::vector allocatedIps; - unsigned long authorizedMemberCount; - unsigned long activeMemberCount; - unsigned long totalMemberCount; - int64_t mostRecentDeauthTime; - }; - - JSONDB(const std::string &basePath,EmbeddedNetworkController *parent); - ~JSONDB(); - - /** - * Write a JSON object to the data store - * - * It's important that obj contain a valid JSON object with no newlines (jsonDump with -1 - * for indentation), since newline-delimited JSON is what nodeJS's IPC speaks and this - * is important in Central-harnessed mode. - * - * @param n Path name of object - * @param obj Object in single-line no-CRs JSON object format (OSUtils::jsonDump(obj,-1)) - * @return True if write appears successful - */ - bool writeRaw(const std::string &n,const std::string &obj); - - bool hasNetwork(const uint64_t networkId) const; - - bool getNetwork(const uint64_t networkId,nlohmann::json &config) const; - - bool getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const; - - /** - * @return Bit mask: 0 == none, 1 == network only, 3 == network and member - */ - int getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const; - - bool getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const; - - void saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig); - - void saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig); - - nlohmann::json eraseNetwork(const uint64_t networkId); - - nlohmann::json eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo = true); - - std::vector networkIds() const - { - std::vector r; - Mutex::Lock _l(_networks_m); - for(std::unordered_map::const_iterator n(_networks.begin());n!=_networks.end();++n) - r.push_back(n->first); - return r; - } - - inline unsigned long memberCount(const uint64_t networkId) - { - Mutex::Lock _l(_networks_m); - std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i != _networks.end()) - return (unsigned long)i->second.members.size(); - return 0; - } - - template - inline void eachMember(const uint64_t networkId,F func) - { - Mutex::Lock _l(_networks_m); - std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i != _networks.end()) { - for(std::unordered_map< uint64_t,std::vector >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) { - try { - func(networkId,m->first,nlohmann::json::from_msgpack(m->second)); - } catch ( ... ) {} - } - } - } - - template - inline void eachId(F func) - { - Mutex::Lock _l(_networks_m); - for(std::unordered_map::const_iterator i(_networks.begin());i!=_networks.end();++i) { - for(std::unordered_map< uint64_t,std::vector >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) { - try { - func(i->first,m->first); - } catch ( ... ) {} - } - } - } - - inline std::vector networksForMember(const uint64_t nodeId) - { - Mutex::Lock _l(_networks_m); - std::unordered_map< uint64_t,std::unordered_set< uint64_t > >::const_iterator m(_members.find(nodeId)); - if (m != _members.end()) { - return std::vector(m->second.begin(),m->second.end()); - } else { - return std::vector(); - } - } - - void threadMain() - throw(); - -private: - bool _addOrUpdate(const nlohmann::json &j); - bool _load(const std::string &p); - void _recomputeSummaryInfo(const uint64_t networkId); - std::string _genPath(const std::string &n,bool create); - - EmbeddedNetworkController *const _parent; - std::string _basePath; - int _rawInput,_rawOutput; - Mutex _rawLock; - - Thread _summaryThread; - std::vector _summaryThreadToDo; - volatile bool _summaryThreadRun; - Mutex _summaryThread_m; - - struct _NW - { - _NW() : summaryInfoLastComputed(0) {} - std::vector config; - NetworkSummaryInfo summaryInfo; - uint64_t summaryInfoLastComputed; - std::unordered_map< uint64_t,std::vector > members; - }; - - std::unordered_map< uint64_t,_NW > _networks; - std::unordered_map< uint64_t,std::unordered_set< uint64_t > > _members; - bool _dataReady; - Mutex _networks_m; -}; - -} // namespace ZeroTier - -#endif diff --git a/controller/RethinkDB.cpp b/controller/RethinkDB.cpp index ffa1a188..bea941fa 100644 --- a/controller/RethinkDB.cpp +++ b/controller/RethinkDB.cpp @@ -33,12 +33,12 @@ using json = nlohmann::json; namespace ZeroTier { RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path) : - _controller(nc), - _myAddress(myAddress), + DB(nc,myAddress,path), _ready(2), // two tables need to be synchronized before we're ready, so this is ready when it reaches 0 _run(1), _waitNoticePrinted(false) { + // rethinkdb:host:port:db[:auth] std::vector ps(OSUtils::split(path,":","","")); if ((ps.size() < 4)||(ps[0] != "rethinkdb")) throw std::runtime_error("invalid rethinkdb database url"); @@ -50,12 +50,6 @@ RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddres _readyLock.lock(); - { - char tmp[32]; - _myAddress.toString(tmp); - _myAddressStr = tmp; - } - _membersDbWatcher = std::thread([this]() { try { while (_run == 1) { @@ -79,7 +73,7 @@ RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddres json &nv = tmp["new_val"]; if (ov.is_object()||nv.is_object()) { //if (nv.is_object()) printf("MEMBER: %s" ZT_EOL_S,nv.dump().c_str()); - this->_memberChanged(ov,nv); + this->_memberChanged(ov,nv,(this->_ready <= 0)); } } catch ( ... ) {} // ignore bad records } @@ -120,7 +114,7 @@ RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddres json &nv = tmp["new_val"]; if (ov.is_object()||nv.is_object()) { //if (nv.is_object()) printf("NETWORK: %s" ZT_EOL_S,nv.dump().c_str()); - this->_networkChanged(ov,nv); + this->_networkChanged(ov,nv,(this->_ready <= 0)); } } catch ( ... ) {} // ignore bad records } @@ -166,18 +160,18 @@ RethinkDB::RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddres record["controllerId"] = this->_myAddressStr; record["config"] = *config; table = "Network"; - } else if (objtype == "delete_network") { + } else if (objtype == "trace") { + record = *config; + table = "RemoteTrace"; + } else if (objtype == "_delete_network") { deleteId = (*config)["id"]; table = "Network"; - } else if (objtype == "delete_member") { + } else if (objtype == "_delete_member") { deleteId = (*config)["nwid"]; deleteId.push_back('-'); const std::string tmp = (*config)["id"]; deleteId.append(tmp); table = "Member"; - } else if (objtype == "trace") { - record = *config; - table = "RemoteTrace"; } else { delete config; continue; @@ -259,114 +253,16 @@ RethinkDB::~RethinkDB() _heartbeatThread.join(); } -bool RethinkDB::get(const uint64_t networkId,nlohmann::json &network) -{ - waitForReady(); - std::shared_ptr<_Network> nw; - { - std::lock_guard l(_networks_l); - auto nwi = _networks.find(networkId); - if (nwi == _networks.end()) - return false; - nw = nwi->second; - } - { - std::lock_guard l2(nw->lock); - network = nw->config; - } - return true; -} - -bool RethinkDB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member) -{ - waitForReady(); - std::shared_ptr<_Network> nw; - { - std::lock_guard l(_networks_l); - auto nwi = _networks.find(networkId); - if (nwi == _networks.end()) - return false; - nw = nwi->second; - } - { - std::lock_guard l2(nw->lock); - network = nw->config; - auto m = nw->members.find(memberId); - if (m == nw->members.end()) - return false; - member = m->second; - } - return true; -} - -bool RethinkDB::get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info) +void RethinkDB::waitForReady() const { - waitForReady(); - std::shared_ptr<_Network> nw; - { - std::lock_guard l(_networks_l); - auto nwi = _networks.find(networkId); - if (nwi == _networks.end()) - return false; - nw = nwi->second; - } - { - std::lock_guard l2(nw->lock); - network = nw->config; - _fillSummaryInfo(nw,info); - auto m = nw->members.find(memberId); - if (m == nw->members.end()) - return false; - member = m->second; - } - return true; -} - -bool RethinkDB::get(const uint64_t networkId,nlohmann::json &network,std::vector &members) -{ - waitForReady(); - std::shared_ptr<_Network> nw; - { - std::lock_guard l(_networks_l); - auto nwi = _networks.find(networkId); - if (nwi == _networks.end()) - return false; - nw = nwi->second; - } - { - std::lock_guard l2(nw->lock); - network = nw->config; - for(auto m=nw->members.begin();m!=nw->members.end();++m) - members.push_back(m->second); - } - return true; -} - -bool RethinkDB::summary(const uint64_t networkId,NetworkSummaryInfo &info) -{ - waitForReady(); - std::shared_ptr<_Network> nw; - { - std::lock_guard l(_networks_l); - auto nwi = _networks.find(networkId); - if (nwi == _networks.end()) - return false; - nw = nwi->second; - } - { - std::lock_guard l2(nw->lock); - _fillSummaryInfo(nw,info); + while (_ready > 0) { + if (!_waitNoticePrinted) { + _waitNoticePrinted = true; + fprintf(stderr,"NOTICE: controller RethinkDB waiting for initial data download..." ZT_EOL_S); + } + _readyLock.lock(); + _readyLock.unlock(); } - return true; -} - -void RethinkDB::networks(std::vector &networks) -{ - waitForReady(); - std::lock_guard l(_networks_l); - networks.reserve(_networks.size() + 1); - for(auto n=_networks.begin();n!=_networks.end();++n) - networks.push_back(n->first); } void RethinkDB::save(const nlohmann::json &record) @@ -382,7 +278,7 @@ void RethinkDB::eraseNetwork(const uint64_t networkId) Utils::hex(networkId,tmp2); json *tmp = new json(); (*tmp)["id"] = tmp2; - (*tmp)["objtype"] = "delete_network"; // pseudo-type, tells thread to delete network + (*tmp)["objtype"] = "_delete_network"; // pseudo-type, tells thread to delete network _commitQueue.post(tmp); } @@ -395,155 +291,10 @@ void RethinkDB::eraseMember(const uint64_t networkId,const uint64_t memberId) (*tmp)["nwid"] = tmp2; Utils::hex10(memberId,tmp2); (*tmp)["id"] = tmp2; - (*tmp)["objtype"] = "delete_member"; // pseudo-type, tells thread to delete network + (*tmp)["objtype"] = "_delete_member"; // pseudo-type, tells thread to delete network _commitQueue.post(tmp); } -void RethinkDB::_memberChanged(nlohmann::json &old,nlohmann::json &member) -{ - uint64_t memberId = 0; - uint64_t networkId = 0; - bool isAuth = false; - bool wasAuth = false; - std::shared_ptr<_Network> nw; - - if (old.is_object()) { - json &config = old["config"]; - if (config.is_object()) { - memberId = OSUtils::jsonIntHex(config["id"],0ULL); - networkId = OSUtils::jsonIntHex(config["nwid"],0ULL); - if ((memberId)&&(networkId)) { - { - std::lock_guard l(_networks_l); - auto nw2 = _networks.find(networkId); - if (nw2 != _networks.end()) - nw = nw2->second; - } - if (nw) { - std::lock_guard l(nw->lock); - if (OSUtils::jsonBool(config["activeBridge"],false)) - nw->activeBridgeMembers.erase(memberId); - wasAuth = OSUtils::jsonBool(config["authorized"],false); - if (wasAuth) - nw->authorizedMembers.erase(memberId); - json &ips = config["ipAssignments"]; - if (ips.is_array()) { - for(unsigned long i=0;iallocatedIps.erase(ipa); - } - } - } - } - } - } - } - - if (member.is_object()) { - json &config = member["config"]; - if (config.is_object()) { - if (!nw) { - memberId = OSUtils::jsonIntHex(config["id"],0ULL); - networkId = OSUtils::jsonIntHex(config["nwid"],0ULL); - if ((!memberId)||(!networkId)) - return; - std::lock_guard l(_networks_l); - std::shared_ptr<_Network> &nw2 = _networks[networkId]; - if (!nw2) - nw2.reset(new _Network); - nw = nw2; - } - - { - std::lock_guard l(nw->lock); - - nw->members[memberId] = config; - - if (OSUtils::jsonBool(config["activeBridge"],false)) - nw->activeBridgeMembers.insert(memberId); - isAuth = OSUtils::jsonBool(config["authorized"],false); - if (isAuth) - nw->authorizedMembers.insert(memberId); - json &ips = config["ipAssignments"]; - if (ips.is_array()) { - for(unsigned long i=0;iallocatedIps.insert(ipa); - } - } - } - - if (!isAuth) { - const int64_t ldt = (int64_t)OSUtils::jsonInt(config["lastDeauthorizedTime"],0ULL); - if (ldt > nw->mostRecentDeauthTime) - nw->mostRecentDeauthTime = ldt; - } - } - - _controller->onNetworkMemberUpdate(networkId,memberId); - } - } else if (memberId) { - if (nw) { - std::lock_guard l(nw->lock); - nw->members.erase(memberId); - } - if (networkId) { - std::lock_guard l(_networks_l); - auto er = _networkByMember.equal_range(memberId); - for(auto i=er.first;i!=er.second;++i) { - if (i->second == networkId) { - _networkByMember.erase(i); - break; - } - } - } - } - - if ((wasAuth)&&(!isAuth)&&(networkId)&&(memberId)) - _controller->onNetworkMemberDeauthorize(networkId,memberId); -} - -void RethinkDB::_networkChanged(nlohmann::json &old,nlohmann::json &network) -{ - if (network.is_object()) { - json &config = network["config"]; - if (config.is_object()) { - const std::string ids = config["id"]; - const uint64_t id = Utils::hexStrToU64(ids.c_str()); - if (id) { - std::shared_ptr<_Network> nw; - { - std::lock_guard l(_networks_l); - std::shared_ptr<_Network> &nw2 = _networks[id]; - if (!nw2) - nw2.reset(new _Network); - nw = nw2; - } - { - std::lock_guard l2(nw->lock); - nw->config = config; - } - _controller->onNetworkUpdate(id); - } - } - } else if (old.is_object()) { - const std::string ids = old["id"]; - const uint64_t id = Utils::hexStrToU64(ids.c_str()); - if (id) { - std::lock_guard l(_networks_l); - _networks.erase(id); - } - } -} - } // namespace ZeroTier #endif // ZT_CONTROLLER_USE_RETHINKDB diff --git a/controller/RethinkDB.hpp b/controller/RethinkDB.hpp index 62586803..2d7ba58e 100644 --- a/controller/RethinkDB.hpp +++ b/controller/RethinkDB.hpp @@ -16,112 +16,35 @@ * along with this program. If not, see . */ +#define ZT_CONTROLLER_USE_RETHINKDB + #ifdef ZT_CONTROLLER_USE_RETHINKDB #ifndef ZT_CONTROLLER_RETHINKDB_HPP #define ZT_CONTROLLER_RETHINKDB_HPP -#include "../node/Constants.hpp" -#include "../node/Address.hpp" -#include "../node/InetAddress.hpp" -#include "../osdep/OSUtils.hpp" -#include "../osdep/BlockingQueue.hpp" - -#include -#include -#include -#include -#include -#include -#include - -#include "../ext/json/json.hpp" +#include "DB.hpp" #define ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS 2 namespace ZeroTier { -class EmbeddedNetworkController; - -class RethinkDB +class RethinkDB : public DB { public: - struct NetworkSummaryInfo - { - NetworkSummaryInfo() : authorizedMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {} - std::vector
activeBridges; - std::vector allocatedIps; - unsigned long authorizedMemberCount; - unsigned long totalMemberCount; - int64_t mostRecentDeauthTime; - }; - RethinkDB(EmbeddedNetworkController *const nc,const Address &myAddress,const char *path); - ~RethinkDB(); - - inline void waitForReady() const - { - while (_ready > 0) { - if (!_waitNoticePrinted) { - _waitNoticePrinted = true; - fprintf(stderr,"NOTICE: controller RethinkDB waiting for initial data download..." ZT_EOL_S); - } - _readyLock.lock(); - _readyLock.unlock(); - } - } - - inline bool hasNetwork(const uint64_t networkId) const - { - std::lock_guard l(_networks_l); - return (_networks.find(networkId) != _networks.end()); - } - - bool get(const uint64_t networkId,nlohmann::json &network); - bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member); - bool get(const uint64_t networkId,nlohmann::json &network,const uint64_t memberId,nlohmann::json &member,NetworkSummaryInfo &info); - bool get(const uint64_t networkId,nlohmann::json &network,std::vector &members); - - bool summary(const uint64_t networkId,NetworkSummaryInfo &info); - - void networks(std::vector &networks); - - void save(const nlohmann::json &record); - - void eraseNetwork(const uint64_t networkId); - void eraseMember(const uint64_t networkId,const uint64_t memberId); - -private: - struct _Network - { - _Network() : mostRecentDeauthTime(0) {} - nlohmann::json config; - std::unordered_map members; - std::unordered_set activeBridgeMembers; - std::unordered_set authorizedMembers; - std::unordered_set allocatedIps; - int64_t mostRecentDeauthTime; - std::mutex lock; - }; - - void _memberChanged(nlohmann::json &old,nlohmann::json &member); - void _networkChanged(nlohmann::json &old,nlohmann::json &network); - - inline void _fillSummaryInfo(const std::shared_ptr<_Network> &nw,NetworkSummaryInfo &info) - { - for(auto ab=nw->activeBridgeMembers.begin();ab!=nw->activeBridgeMembers.end();++ab) - info.activeBridges.push_back(Address(*ab)); - for(auto ip=nw->allocatedIps.begin();ip!=nw->allocatedIps.end();++ip) - info.allocatedIps.push_back(*ip); - info.authorizedMemberCount = (unsigned long)nw->authorizedMembers.size(); - info.totalMemberCount = (unsigned long)nw->members.size(); - info.mostRecentDeauthTime = nw->mostRecentDeauthTime; - } - - EmbeddedNetworkController *const _controller; - const Address _myAddress; - std::string _myAddressStr; + virtual ~RethinkDB(); + + virtual void waitForReady() const; + + virtual void save(const nlohmann::json &record); + + virtual void eraseNetwork(const uint64_t networkId); + + virtual void eraseMember(const uint64_t networkId,const uint64_t memberId); + +protected: std::string _host; std::string _db; std::string _auth; @@ -132,10 +55,6 @@ private: std::thread _networksDbWatcher; std::thread _membersDbWatcher; - std::unordered_map< uint64_t,std::shared_ptr<_Network> > _networks; - std::unordered_multimap< uint64_t,uint64_t > _networkByMember; - mutable std::mutex _networks_l; - BlockingQueue< nlohmann::json * > _commitQueue; std::thread _commitThread[ZT_CONTROLLER_RETHINKDB_COMMIT_THREADS]; diff --git a/objects.mk b/objects.mk index d18efbe6..d4a3c16c 100644 --- a/objects.mk +++ b/objects.mk @@ -28,6 +28,8 @@ CORE_OBJS=\ ONE_OBJS=\ controller/EmbeddedNetworkController.o \ + controller/DB.o \ + controller/FileDB.o \ controller/RethinkDB.o \ osdep/ManagedRoute.o \ osdep/Http.o \ diff --git a/osdep/OSUtils.hpp b/osdep/OSUtils.hpp index fd1b31d9..274b48df 100644 --- a/osdep/OSUtils.hpp +++ b/osdep/OSUtils.hpp @@ -101,7 +101,6 @@ public: * @return True if delete was successful */ static inline bool rm(const char *path) - throw() { #ifdef __WINDOWS__ return (DeleteFileA(path) != FALSE); @@ -109,7 +108,7 @@ public: return (unlink(path) == 0); #endif } - static inline bool rm(const std::string &path) throw() { return rm(path.c_str()); } + static inline bool rm(const std::string &path) { return rm(path.c_str()); } static inline bool mkdir(const char *path) { @@ -123,7 +122,17 @@ public: return true; #endif } - static inline bool mkdir(const std::string &path) throw() { return OSUtils::mkdir(path.c_str()); } + static inline bool mkdir(const std::string &path) { return OSUtils::mkdir(path.c_str()); } + + static inline bool rename(const char *o,const char *n) + { +#ifdef __WINDOWS__ + DeleteFileA(n); + return (::rename(o,n) == 0); +#else + return (::rename(o,n) == 0); +#endif + } /** * List a directory's contents -- cgit v1.2.3 From e338c5f91d7724dcc16499a523089a31ab1f0a05 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 8 Jan 2018 14:27:55 -0800 Subject: cleanup --- attic/lat_lon_to_xyz.js | 25 +++++++++++++++++++++++++ lat_lon_to_xyz.js | 25 ------------------------- 2 files changed, 25 insertions(+), 25 deletions(-) create mode 100644 attic/lat_lon_to_xyz.js delete mode 100644 lat_lon_to_xyz.js (limited to 'attic') diff --git a/attic/lat_lon_to_xyz.js b/attic/lat_lon_to_xyz.js new file mode 100644 index 00000000..692a3687 --- /dev/null +++ b/attic/lat_lon_to_xyz.js @@ -0,0 +1,25 @@ +'use strict' + +/* This is a utility to convert latitude/longitude into X,Y,Z coordinates as used by clustering. */ + +if (process.argv.length !== 4) { + console.log('Usage: node lat_lon_to_xyz.js Date: Tue, 9 Jan 2018 11:23:39 -0800 Subject: cleanup --- attic/JSONDB.cpp | 525 ----------- attic/JSONDB.hpp | 192 ----- attic/cli/README.md | 57 -- attic/cli/zerotier.cpp | 957 --------------------- attic/linux-build-farm/README.md | 8 - .../linux-build-farm/amazon-2016.03/x64/Dockerfile | 13 - attic/linux-build-farm/build.sh | 69 -- attic/linux-build-farm/centos-6/x64/Dockerfile | 13 - attic/linux-build-farm/centos-6/x86/Dockerfile | 13 - attic/linux-build-farm/centos-7/x64/Dockerfile | 10 - attic/linux-build-farm/centos-7/x86/Dockerfile | 22 - .../linux-build-farm/debian-jessie/x64/Dockerfile | 12 - .../linux-build-farm/debian-jessie/x86/Dockerfile | 12 - .../linux-build-farm/debian-stretch/x64/Dockerfile | 12 - .../linux-build-farm/debian-stretch/x86/Dockerfile | 12 - .../linux-build-farm/debian-wheezy/x64/Dockerfile | 12 - .../linux-build-farm/debian-wheezy/x86/Dockerfile | 15 - attic/linux-build-farm/fedora-22/x64/Dockerfile | 10 - attic/linux-build-farm/fedora-22/x86/Dockerfile | 19 - attic/linux-build-farm/make-apt-repos.sh | 16 - attic/linux-build-farm/make-rpm-repos.sh | 64 -- .../linux-build-farm/ubuntu-trusty/x64/Dockerfile | 12 - .../linux-build-farm/ubuntu-trusty/x86/Dockerfile | 12 - attic/linux-build-farm/ubuntu-wily/x64/Dockerfile | 12 - attic/linux-build-farm/ubuntu-wily/x86/Dockerfile | 12 - .../linux-build-farm/ubuntu-xenial/x64/Dockerfile | 14 - .../linux-build-farm/ubuntu-xenial/x86/Dockerfile | 14 - attic/old-controller-schema.sql | 112 --- attic/old-linux-installer/buildinstaller.sh | 134 --- attic/old-linux-installer/install.tmpl.sh | 182 ---- attic/old-linux-installer/uninstall.sh | 76 -- attic/root-watcher/README.md | 8 - attic/root-watcher/config.json.example | 30 - attic/root-watcher/package.json | 16 - attic/root-watcher/schema.sql | 20 - attic/root-watcher/zerotier-root-watcher.js | 235 ----- attic/tcp-proxy/Makefile | 7 - attic/tcp-proxy/README.md | 4 - attic/tcp-proxy/tcp-proxy.cpp | 317 ------- 39 files changed, 3280 deletions(-) delete mode 100644 attic/JSONDB.cpp delete mode 100644 attic/JSONDB.hpp delete mode 100644 attic/cli/README.md delete mode 100644 attic/cli/zerotier.cpp delete mode 100644 attic/linux-build-farm/README.md delete mode 100644 attic/linux-build-farm/amazon-2016.03/x64/Dockerfile delete mode 100755 attic/linux-build-farm/build.sh delete mode 100644 attic/linux-build-farm/centos-6/x64/Dockerfile delete mode 100644 attic/linux-build-farm/centos-6/x86/Dockerfile delete mode 100644 attic/linux-build-farm/centos-7/x64/Dockerfile delete mode 100644 attic/linux-build-farm/centos-7/x86/Dockerfile delete mode 100644 attic/linux-build-farm/debian-jessie/x64/Dockerfile delete mode 100644 attic/linux-build-farm/debian-jessie/x86/Dockerfile delete mode 100644 attic/linux-build-farm/debian-stretch/x64/Dockerfile delete mode 100644 attic/linux-build-farm/debian-stretch/x86/Dockerfile delete mode 100644 attic/linux-build-farm/debian-wheezy/x64/Dockerfile delete mode 100644 attic/linux-build-farm/debian-wheezy/x86/Dockerfile delete mode 100644 attic/linux-build-farm/fedora-22/x64/Dockerfile delete mode 100644 attic/linux-build-farm/fedora-22/x86/Dockerfile delete mode 100755 attic/linux-build-farm/make-apt-repos.sh delete mode 100755 attic/linux-build-farm/make-rpm-repos.sh delete mode 100644 attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile delete mode 100644 attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile delete mode 100644 attic/linux-build-farm/ubuntu-wily/x64/Dockerfile delete mode 100644 attic/linux-build-farm/ubuntu-wily/x86/Dockerfile delete mode 100644 attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile delete mode 100644 attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile delete mode 100644 attic/old-controller-schema.sql delete mode 100755 attic/old-linux-installer/buildinstaller.sh delete mode 100644 attic/old-linux-installer/install.tmpl.sh delete mode 100755 attic/old-linux-installer/uninstall.sh delete mode 100644 attic/root-watcher/README.md delete mode 100644 attic/root-watcher/config.json.example delete mode 100644 attic/root-watcher/package.json delete mode 100644 attic/root-watcher/schema.sql delete mode 100644 attic/root-watcher/zerotier-root-watcher.js delete mode 100644 attic/tcp-proxy/Makefile delete mode 100644 attic/tcp-proxy/README.md delete mode 100644 attic/tcp-proxy/tcp-proxy.cpp (limited to 'attic') diff --git a/attic/JSONDB.cpp b/attic/JSONDB.cpp deleted file mode 100644 index 67b13393..00000000 --- a/attic/JSONDB.cpp +++ /dev/null @@ -1,525 +0,0 @@ -/* - * 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 . - */ - -#include -#include -#include -#ifndef _WIN32 -#include -#include -#include -#include -#include -#include -#endif - -#include "JSONDB.hpp" -#include "EmbeddedNetworkController.hpp" - -namespace ZeroTier { - -static const nlohmann::json _EMPTY_JSON(nlohmann::json::object()); - -JSONDB::JSONDB(const std::string &basePath,EmbeddedNetworkController *parent) : - _parent(parent), - _basePath(basePath), - _rawInput(-1), - _rawOutput(-1), - _summaryThreadRun(true), - _dataReady(false) -{ -#ifndef __WINDOWS__ - if (_basePath == "-") { - // If base path is "-" we run in Central harnessed mode. We read pseudo-http-requests from stdin and write - // them to stdout. - _rawInput = STDIN_FILENO; - _rawOutput = STDOUT_FILENO; - fcntl(_rawInput,F_SETFL,O_NONBLOCK); - } else { -#endif - // Default mode of operation is to store files in the filesystem - OSUtils::mkdir(_basePath.c_str()); - OSUtils::lockDownFile(_basePath.c_str(),true); // networks might contain auth tokens, etc., so restrict directory permissions -#ifndef __WINDOWS__ - } -#endif - - _networks_m.lock(); // locked until data is loaded, etc. - - if (_rawInput < 0) { - _load(basePath); - _dataReady = true; - _networks_m.unlock(); - } else { - // In harnessed mode we leave the lock locked and wait for our initial DB from Central. - _summaryThread = Thread::start(this); - } -} - -JSONDB::~JSONDB() -{ - Thread t; - { - Mutex::Lock _l(_summaryThread_m); - _summaryThreadRun = false; - t = _summaryThread; - } - if (t) - Thread::join(t); -} - -bool JSONDB::writeRaw(const std::string &n,const std::string &obj) -{ - if (_rawOutput >= 0) { -#ifndef __WINDOWS__ - if (obj.length() > 0) { - Mutex::Lock _l(_rawLock); - //fprintf(stderr,"%s\n",obj.c_str()); - if ((long)write(_rawOutput,obj.data(),obj.length()) == (long)obj.length()) { - if (write(_rawOutput,"\n",1) == 1) - return true; - } - } else return true; -#endif - return false; - } else { - const std::string path(_genPath(n,true)); - if (!path.length()) - return false; - return OSUtils::writeFile(path.c_str(),obj); - } -} - -bool JSONDB::hasNetwork(const uint64_t networkId) const -{ - Mutex::Lock _l(_networks_m); - return (_networks.find(networkId) != _networks.end()); -} - -bool JSONDB::getNetwork(const uint64_t networkId,nlohmann::json &config) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return false; - config = nlohmann::json::from_msgpack(i->second.config); - return true; -} - -bool JSONDB::getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return false; - ns = i->second.summaryInfo; - return true; -} - -int JSONDB::getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return 0; - const std::unordered_map< uint64_t,std::vector >::const_iterator j(i->second.members.find(nodeId)); - if (j == i->second.members.end()) - return 1; - networkConfig = nlohmann::json::from_msgpack(i->second.config); - memberConfig = nlohmann::json::from_msgpack(j->second); - ns = i->second.summaryInfo; - return 3; -} - -bool JSONDB::getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const -{ - Mutex::Lock _l(_networks_m); - const std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return false; - const std::unordered_map< uint64_t,std::vector >::const_iterator j(i->second.members.find(nodeId)); - if (j == i->second.members.end()) - return false; - memberConfig = nlohmann::json::from_msgpack(j->second); - return true; -} - -void JSONDB::saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig) -{ - char n[64]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); - writeRaw(n,OSUtils::jsonDump(networkConfig,-1)); - { - Mutex::Lock _l(_networks_m); - _NW &nw = _networks[networkId]; - nw.config = nlohmann::json::to_msgpack(networkConfig); - } - _recomputeSummaryInfo(networkId); -} - -void JSONDB::saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig) -{ - char n[256]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); - writeRaw(n,OSUtils::jsonDump(memberConfig,-1)); - { - Mutex::Lock _l(_networks_m); - std::vector &m = _networks[networkId].members[nodeId]; - m = nlohmann::json::to_msgpack(memberConfig); - _members[nodeId].insert(networkId); - } - _recomputeSummaryInfo(networkId); -} - -nlohmann::json JSONDB::eraseNetwork(const uint64_t networkId) -{ - if (_rawOutput >= 0) { - // In harnessed mode, DB deletes occur in the Central database and we do - // not need to erase files. - } else { - std::vector memberIds; - { - Mutex::Lock _l(_networks_m); - const std::unordered_map::iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return _EMPTY_JSON; - for(std::unordered_map< uint64_t,std::vector >::iterator m(i->second.members.begin());m!=i->second.members.end();++m) - memberIds.push_back(m->first); - } - for(std::vector::iterator m(memberIds.begin());m!=memberIds.end();++m) - eraseNetworkMember(networkId,*m,false); - - char n[256]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx",(unsigned long long)networkId); - const std::string path(_genPath(n,false)); - if (path.length()) - OSUtils::rm(path.c_str()); - } - - // This also erases all members from the memory cache - { - Mutex::Lock _l(_networks_m); - std::unordered_map::iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return _EMPTY_JSON; // sanity check, shouldn't happen - nlohmann::json tmp(nlohmann::json::from_msgpack(i->second.config)); - _networks.erase(i); - return tmp; - } -} - -nlohmann::json JSONDB::eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo) -{ - if (_rawOutput >= 0) { - // In harnessed mode, DB deletes occur in Central and we do not remove files. - } else { - char n[256]; - OSUtils::ztsnprintf(n,sizeof(n),"network/%.16llx/member/%.10llx",(unsigned long long)networkId,(unsigned long long)nodeId); - const std::string path(_genPath(n,false)); - if (path.length()) - OSUtils::rm(path.c_str()); - } - - { - Mutex::Lock _l(_networks_m); - _members[nodeId].erase(networkId); - std::unordered_map::iterator i(_networks.find(networkId)); - if (i == _networks.end()) - return _EMPTY_JSON; - std::unordered_map< uint64_t,std::vector >::iterator j(i->second.members.find(nodeId)); - if (j == i->second.members.end()) - return _EMPTY_JSON; - nlohmann::json tmp(j->second); - i->second.members.erase(j); - if (recomputeSummaryInfo) - _recomputeSummaryInfo(networkId); - return tmp; - } -} - -void JSONDB::threadMain() - throw() -{ -#ifndef __WINDOWS__ - fd_set readfds,nullfds; - char *const readbuf = (_rawInput >= 0) ? (new char[1048576]) : (char *)0; - std::string rawInputBuf; - FD_ZERO(&readfds); - FD_ZERO(&nullfds); - struct timeval tv; -#endif - - std::vector todo; - - while (_summaryThreadRun) { -#ifndef __WINDOWS__ - if (_rawInput < 0) { - Thread::sleep(25); - } else { - // In IPC mode we wait but also select() on STDIN to read database updates - FD_SET(_rawInput,&readfds); - tv.tv_sec = 0; - tv.tv_usec = 25000; - select(_rawInput+1,&readfds,&nullfds,&nullfds,&tv); - if (FD_ISSET(_rawInput,&readfds)) { - const long rn = (long)read(_rawInput,readbuf,1048576); - bool gotMessage = false; - for(long i=0;i 0) { - try { - const nlohmann::json obj(OSUtils::jsonParse(rawInputBuf)); - gotMessage = true; - - if (!_dataReady) { - _dataReady = true; - _networks_m.unlock(); - } - - if (obj.is_array()) { - for(unsigned long i=0;i::iterator ii(todo.begin());ii!=todo.end();++ii) { - const uint64_t networkId = *ii; - std::unordered_map::iterator n(_networks.find(networkId)); - if (n != _networks.end()) { - NetworkSummaryInfo &ns = n->second.summaryInfo; - ns.activeBridges.clear(); - ns.allocatedIps.clear(); - ns.authorizedMemberCount = 0; - ns.activeMemberCount = 0; - ns.totalMemberCount = 0; - ns.mostRecentDeauthTime = 0; - - for(std::unordered_map< uint64_t,std::vector >::const_iterator m(n->second.members.begin());m!=n->second.members.end();++m) { - try { - nlohmann::json member(nlohmann::json::from_msgpack(m->second)); - - if (OSUtils::jsonBool(member["authorized"],false)) { - ++ns.authorizedMemberCount; - - try { - const nlohmann::json &mlog = member["recentLog"]; - if ((mlog.is_array())&&(mlog.size() > 0)) { - const nlohmann::json &mlog1 = mlog[0]; - if (mlog1.is_object()) { - if ((now - OSUtils::jsonInt(mlog1["ts"],0ULL)) < (ZT_NETWORK_AUTOCONF_DELAY * 2)) - ++ns.activeMemberCount; - } - } - } catch ( ... ) {} - - try { - if (OSUtils::jsonBool(member["activeBridge"],false)) - ns.activeBridges.push_back(Address(m->first)); - } catch ( ... ) {} - - try { - const nlohmann::json &mips = member["ipAssignments"]; - if (mips.is_array()) { - for(unsigned long i=0;isecond.summaryInfoLastComputed = now; - } - } - } catch ( ... ) {} - - todo.clear(); - } - - if (!_dataReady) // sanity check - _networks_m.unlock(); - -#ifndef __WINDOWS__ - delete [] readbuf; -#endif -} - -bool JSONDB::_addOrUpdate(const nlohmann::json &j) -{ - try { - if (j.is_object()) { - std::string id(OSUtils::jsonString(j["id"],"0")); - const std::string objtype(OSUtils::jsonString(j["objtype"],"")); - if ((id.length() == 16)&&(objtype == "network")) { - - const uint64_t nwid = Utils::hexStrToU64(id.c_str()); - if (nwid) { - bool update; - { - Mutex::Lock _l(_networks_m); - _NW &nw = _networks[nwid]; - update = !nw.config.empty(); - nw.config = nlohmann::json::to_msgpack(j); - } - if (update) - _parent->onNetworkUpdate(nwid); - _recomputeSummaryInfo(nwid); - return true; - } - - } else if ((id.length() == 10)&&(objtype == "member")) { - - const uint64_t mid = Utils::hexStrToU64(id.c_str()); - const uint64_t nwid = Utils::hexStrToU64(OSUtils::jsonString(j["nwid"],"0").c_str()); - if ((mid)&&(nwid)) { - bool update = false; - bool deauth = false; - { - Mutex::Lock _l(_networks_m); - std::vector &m = _networks[nwid].members[mid]; - if (!m.empty()) { - update = true; - nlohmann::json oldm(nlohmann::json::from_msgpack(m)); - deauth = ((OSUtils::jsonBool(oldm["authorized"],false))&&(!OSUtils::jsonBool(j["authorized"],false))); - } - m = nlohmann::json::to_msgpack(j); - _members[mid].insert(nwid); - } - if (update) { - _parent->onNetworkMemberUpdate(nwid,mid); - if (deauth) - _parent->onNetworkMemberDeauthorize(nwid,mid); - } - _recomputeSummaryInfo(nwid); - return true; - } - - } else if (objtype == "_delete") { // pseudo-object-type, only used in Central harnessed mode - - const std::string deleteType(OSUtils::jsonString(j["deleteType"],"")); - id = OSUtils::jsonString(j["deleteId"],""); - if ((deleteType == "network")&&(id.length() == 16)) { - eraseNetwork(Utils::hexStrToU64(id.c_str())); - } else if ((deleteType == "member")&&(id.length() == 10)) { - const std::string networkId(OSUtils::jsonString(j["deleteNetworkId"],"")); - const uint64_t nwid = Utils::hexStrToU64(networkId.c_str()); - const uint64_t mid = Utils::hexStrToU64(id.c_str()); - if (networkId.length() == 16) - eraseNetworkMember(nwid,mid,true); - _parent->onNetworkMemberDeauthorize(nwid,mid); - } - - } - } - } catch ( ... ) {} - return false; -} - -bool JSONDB::_load(const std::string &p) -{ - // This is not used in stdin/stdout mode. Instead data is populated by - // sending it all to stdin. - - std::vector dl(OSUtils::listDirectory(p.c_str(),true)); - for(std::vector::const_iterator di(dl.begin());di!=dl.end();++di) { - if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) { - std::string buf; - if (OSUtils::readFile((p + ZT_PATH_SEPARATOR_S + *di).c_str(),buf)) { - try { - _addOrUpdate(OSUtils::jsonParse(buf)); - } catch ( ... ) {} - } - } else { - this->_load((p + ZT_PATH_SEPARATOR_S + *di)); - } - } - - return true; -} - -void JSONDB::_recomputeSummaryInfo(const uint64_t networkId) -{ - Mutex::Lock _l(_summaryThread_m); - if (std::find(_summaryThreadToDo.begin(),_summaryThreadToDo.end(),networkId) == _summaryThreadToDo.end()) - _summaryThreadToDo.push_back(networkId); - if (!_summaryThread) - _summaryThread = Thread::start(this); -} - -std::string JSONDB::_genPath(const std::string &n,bool create) -{ - std::vector pt(OSUtils::split(n.c_str(),"/","","")); - if (pt.size() == 0) - return std::string(); - - std::string p(_basePath); - if (create) OSUtils::mkdir(p.c_str()); - for(unsigned long i=0,j=(unsigned long)(pt.size()-1);i. - */ - -#ifndef ZT_JSONDB_HPP -#define ZT_JSONDB_HPP - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Utils.hpp" -#include "../node/InetAddress.hpp" -#include "../node/Mutex.hpp" -#include "../ext/json/json.hpp" -#include "../osdep/OSUtils.hpp" -#include "../osdep/Thread.hpp" - -namespace ZeroTier { - -class EmbeddedNetworkController; - -/** - * Hierarchical JSON store that persists into the filesystem or via HTTP - */ -class JSONDB -{ -public: - struct NetworkSummaryInfo - { - NetworkSummaryInfo() : authorizedMemberCount(0),activeMemberCount(0),totalMemberCount(0),mostRecentDeauthTime(0) {} - std::vector
activeBridges; - std::vector allocatedIps; - unsigned long authorizedMemberCount; - unsigned long activeMemberCount; - unsigned long totalMemberCount; - int64_t mostRecentDeauthTime; - }; - - JSONDB(const std::string &basePath,EmbeddedNetworkController *parent); - ~JSONDB(); - - /** - * Write a JSON object to the data store - * - * It's important that obj contain a valid JSON object with no newlines (jsonDump with -1 - * for indentation), since newline-delimited JSON is what nodeJS's IPC speaks and this - * is important in Central-harnessed mode. - * - * @param n Path name of object - * @param obj Object in single-line no-CRs JSON object format (OSUtils::jsonDump(obj,-1)) - * @return True if write appears successful - */ - bool writeRaw(const std::string &n,const std::string &obj); - - bool hasNetwork(const uint64_t networkId) const; - - bool getNetwork(const uint64_t networkId,nlohmann::json &config) const; - - bool getNetworkSummaryInfo(const uint64_t networkId,NetworkSummaryInfo &ns) const; - - /** - * @return Bit mask: 0 == none, 1 == network only, 3 == network and member - */ - int getNetworkAndMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &networkConfig,nlohmann::json &memberConfig,NetworkSummaryInfo &ns) const; - - bool getNetworkMember(const uint64_t networkId,const uint64_t nodeId,nlohmann::json &memberConfig) const; - - void saveNetwork(const uint64_t networkId,const nlohmann::json &networkConfig); - - void saveNetworkMember(const uint64_t networkId,const uint64_t nodeId,const nlohmann::json &memberConfig); - - nlohmann::json eraseNetwork(const uint64_t networkId); - - nlohmann::json eraseNetworkMember(const uint64_t networkId,const uint64_t nodeId,bool recomputeSummaryInfo = true); - - std::vector networkIds() const - { - std::vector r; - Mutex::Lock _l(_networks_m); - for(std::unordered_map::const_iterator n(_networks.begin());n!=_networks.end();++n) - r.push_back(n->first); - return r; - } - - inline unsigned long memberCount(const uint64_t networkId) - { - Mutex::Lock _l(_networks_m); - std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i != _networks.end()) - return (unsigned long)i->second.members.size(); - return 0; - } - - template - inline void eachMember(const uint64_t networkId,F func) - { - Mutex::Lock _l(_networks_m); - std::unordered_map::const_iterator i(_networks.find(networkId)); - if (i != _networks.end()) { - for(std::unordered_map< uint64_t,std::vector >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) { - try { - func(networkId,m->first,nlohmann::json::from_msgpack(m->second)); - } catch ( ... ) {} - } - } - } - - template - inline void eachId(F func) - { - Mutex::Lock _l(_networks_m); - for(std::unordered_map::const_iterator i(_networks.begin());i!=_networks.end();++i) { - for(std::unordered_map< uint64_t,std::vector >::const_iterator m(i->second.members.begin());m!=i->second.members.end();++m) { - try { - func(i->first,m->first); - } catch ( ... ) {} - } - } - } - - inline std::vector networksForMember(const uint64_t nodeId) - { - Mutex::Lock _l(_networks_m); - std::unordered_map< uint64_t,std::unordered_set< uint64_t > >::const_iterator m(_members.find(nodeId)); - if (m != _members.end()) { - return std::vector(m->second.begin(),m->second.end()); - } else { - return std::vector(); - } - } - - void threadMain() - throw(); - -private: - bool _addOrUpdate(const nlohmann::json &j); - bool _load(const std::string &p); - void _recomputeSummaryInfo(const uint64_t networkId); - std::string _genPath(const std::string &n,bool create); - - EmbeddedNetworkController *const _parent; - std::string _basePath; - int _rawInput,_rawOutput; - Mutex _rawLock; - - Thread _summaryThread; - std::vector _summaryThreadToDo; - volatile bool _summaryThreadRun; - Mutex _summaryThread_m; - - struct _NW - { - _NW() : summaryInfoLastComputed(0) {} - std::vector config; - NetworkSummaryInfo summaryInfo; - uint64_t summaryInfoLastComputed; - std::unordered_map< uint64_t,std::vector > members; - }; - - std::unordered_map< uint64_t,_NW > _networks; - std::unordered_map< uint64_t,std::unordered_set< uint64_t > > _members; - bool _dataReady; - Mutex _networks_m; -}; - -} // namespace ZeroTier - -#endif diff --git a/attic/cli/README.md b/attic/cli/README.md deleted file mode 100644 index 595df07e..00000000 --- a/attic/cli/README.md +++ /dev/null @@ -1,57 +0,0 @@ -The new ZeroTier CLI! -==== - -With this update we've expanded upon the previous CLI's functionality, so things should seem pretty familiar. Here are some of the new features we've introduced: - - - Create and administer networks on ZeroTier Central directly from the console. - - Service configurations, allows you to control local/remote instances of ZeroTier One - - Identity generation and management is now part of the same CLI tool - -*** -## Configurations - -Configurations are a way for you to nickname and logically organize the control of ZeroTier services running locally or remotely (this includes ZeroTier Central!). They're merely groupings of service API url's and auth tokens. The CLI's settings data is contained within `.zerotierCliSettings`. - -For instance, you can control your local instance of ZeroTier One via the `@local` config. By default it is represented as follows: - -``` -"local": { - "auth": "7tyqRoFytajf21j2l2t9QPm5", - "type": "one", - "url": "http://127.0.0.1:9993/" -} -``` - -As an example, if you issue the command `zerotier ls` is it implicitly stating `zerotier @local ls`. - -With the same line of thinking, you could create a `@my.zerotier.com` which would allow for something like `zerotier @my.zerotier.com net-create` which talks to our hosted ZeroTier Central to create a new network. - - - -## Command families - -- `cli-` is for configuring the settings data for the CLI itself, such as adding/removing `@thing` configurations, variables, etc. -- `net-` is for operating on a *ZeroTier Central* service such as `https://my.zerotier.com` -- `id-` is for handling ZeroTier identities. - -And those commands with no prefix are there to allow you to operate ZeroTier One instances either local or remote. - -*** -## Useful command examples - -*Add a ZeroTier One configuration:* - - - `zerotier cli-add-zt MyLocalConfigName https://127.0.0.1:9993/ ` - -*Add a ZeroTier Central configuration:* - - - `zerotier cli-add-central MyZTCentralConfigName https://my.zerotier.com/ ` - -*Set a default ZeroTier One instance:* - - - `zerotier cli-set defaultOne MyLocalConfigName` - -*Set a default ZeroTier Central:* - - - `zerotier cli-set defaultCentral MyZTCentralConfigName` - diff --git a/attic/cli/zerotier.cpp b/attic/cli/zerotier.cpp deleted file mode 100644 index e75268d1..00000000 --- a/attic/cli/zerotier.cpp +++ /dev/null @@ -1,957 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -// Note: unlike the rest of ZT's code base, this requires C++11 due to -// the JSON library it uses and other things. - -#include -#include -#include -#include - -#include "../node/Constants.hpp" -#include "../node/Identity.hpp" -#include "../version.h" -#include "../osdep/OSUtils.hpp" -#include "../ext/offbase/json/json.hpp" - -#ifdef __WINDOWS__ -#include -#include -#include -#include -#else -#include -#include -#endif - -#include -#include -#include -#include -#include -#include - -#include - -using json = nlohmann::json; -using namespace ZeroTier; - -#define ZT_CLI_FLAG_VERBOSE 'v' -#define ZT_CLI_FLAG_UNSAFE_SSL 'X' - -#define REQ_GET 0 -#define REQ_POST 1 -#define REQ_DEL 2 - -#define OK_STR "[OK ]: " -#define FAIL_STR "[FAIL]: " -#define WARN_STR "[WARN]: " -#define INVALID_ARGS_STR "Invalid args. Usage: " - -struct CLIState -{ - std::string atname; - std::string command; - std::string url; - std::map reqHeaders; - std::vector args; - std::map opts; - json settings; -}; - -namespace { - -static Identity getIdFromArg(char *arg) -{ - Identity id; - if ((strlen(arg) > 32)&&(arg[10] == ':')) { // identity is a literal on the command line - if (id.fromString(arg)) - return id; - } else { // identity is to be read from a file - std::string idser; - if (OSUtils::readFile(arg,idser)) { - if (id.fromString(idser)) - return id; - } - } - return Identity(); -} - -static std::string trimString(const std::string &s) -{ - unsigned long end = (unsigned long)s.length(); - while (end) { - char c = s[end - 1]; - if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t')) - --end; - else break; - } - unsigned long start = 0; - while (start < end) { - char c = s[start]; - if ((c == ' ')||(c == '\r')||(c == '\n')||(!c)||(c == '\t')) - ++start; - else break; - } - return s.substr(start,end - start); -} - -static inline std::string getSettingsFilePath() -{ -#ifdef __WINDOWS__ -#else - const char *home = getenv("HOME"); - if (!home) - home = "/"; - return (std::string(home) + "/.zerotierCliSettings"); -#endif -} - -static bool saveSettingsBackup(CLIState &state) -{ - std::string sfp(getSettingsFilePath().c_str()); - if(state.settings.find("generateBackupConfig") != state.settings.end() - && state.settings["generateBackupConfig"].get() == "true") { - std::string backup_file = getSettingsFilePath() + ".bak"; - if(!OSUtils::writeFile(sfp.c_str(), state.settings.dump(2))) { - OSUtils::lockDownFile(sfp.c_str(),false); - std::cout << WARN_STR << "unable to write backup config file" << std::endl; - return false; - } - return true; - } - return false; -} - -static bool saveSettings(CLIState &state) -{ - std::string sfp(getSettingsFilePath().c_str()); - if(OSUtils::writeFile(sfp.c_str(), state.settings.dump(2))) { - OSUtils::lockDownFile(sfp.c_str(),false); - std::cout << OK_STR << "changes saved." << std::endl; - return true; - } - std::cout << FAIL_STR << "unable to write to " << sfp << std::endl; - return false; -} - -static void dumpHelp() -{ - std::cout << "ZeroTier Newer-Spiffier CLI " << ZEROTIER_ONE_VERSION_MAJOR << "." << ZEROTIER_ONE_VERSION_MINOR << "." << ZEROTIER_ONE_VERSION_REVISION << std::endl; - std::cout << "(c)2016 ZeroTier, Inc. / Licensed under the GNU GPL v3" << std::endl; - std::cout << std::endl; - std::cout << "Configuration path: " << getSettingsFilePath() << std::endl; - std::cout << std::endl; - std::cout << "Usage: zerotier [-option] [@name] []" << std::endl; - std::cout << std::endl; - std::cout << "Options:" << std::endl; - std::cout << " -verbose - Verbose JSON output" << std::endl; - std::cout << " -X - Do not check SSL certs (CAUTION!)" << std::endl; - std::cout << std::endl; - std::cout << "CLI Configuration Commands:" << std::endl; - std::cout << " cli-set - Set a CLI option ('cli-set help')" << std::endl; - std::cout << " cli-unset - Un-sets a CLI option ('cli-unset help')" << std::endl; - std::cout << " cli-ls - List configured @things" << std::endl; - std::cout << " cli-rm @name - Remove a configured @thing" << std::endl; - std::cout << " cli-add-zt @name - Add a ZeroTier service" << std::endl; - std::cout << " cli-add-central @name - Add ZeroTier Central instance" << std::endl; - std::cout << std::endl; - std::cout << "ZeroTier One Service Commands:" << std::endl; - std::cout << " -v / -version - Displays default local instance's version'" << std::endl; - std::cout << " ls - List currently joined networks" << std::endl; - std::cout << " join [opt=value ...] - Join a network" << std::endl; - std::cout << " leave - Leave a network" << std::endl; - std::cout << " peers - List ZeroTier VL1 peers" << std::endl; - std::cout << " show [] - Get info about self or object" << std::endl; - std::cout << std::endl; - std::cout << "Network Controller Commands:" << std::endl; - std::cout << " net-create - Create a new network" << std::endl; - std::cout << " net-rm - Delete a network (CAUTION!)" << std::endl; - std::cout << " net-ls - List administered networks" << std::endl; - std::cout << " net-members - List members of a network" << std::endl; - std::cout << " net-show [
] - Get network or member info" << std::endl; - std::cout << " net-auth
- Authorize a member" << std::endl; - std::cout << " net-unauth
- De-authorize a member" << std::endl; - std::cout << " net-set - See 'net-set help'" << std::endl; - std::cout << std::endl; - std::cout << "Identity Commands:" << std::endl; - std::cout << " id-generate [] - Generate a ZeroTier identity" << std::endl; - std::cout << " id-validate - Locally validate an identity" << std::endl; - std::cout << " id-sign - Sign a file" << std::endl; - std::cout << " id-verify - Verify a file's signature" << std::endl; - std::cout << " id-getpublic - Get full identity's public portion" << std::endl; - std::cout << std::endl; -} - -static size_t _curlStringAppendCallback(void *contents,size_t size,size_t nmemb,void *stdstring) -{ - size_t totalSize = size * nmemb; - reinterpret_cast(stdstring)->append((const char *)contents,totalSize); - return totalSize; -} - -static std::tuple REQUEST(int requestType, CLIState &state, const std::map &headers, const std::string &postfield, const std::string &url) -{ - std::string body; - char errbuf[CURL_ERROR_SIZE]; - char urlbuf[4096]; - - CURL *curl; - curl = curl_easy_init(); - if (!curl) { - std::cerr << "FATAL: curl_easy_init() failed" << std::endl; - exit(-1); - } - - Utils::scopy(urlbuf,sizeof(urlbuf),url.c_str()); - curl_easy_setopt(curl,CURLOPT_URL,urlbuf); - - struct curl_slist *hdrs = (struct curl_slist *)0; - for(std::map::const_iterator i(headers.begin());i!=headers.end();++i) { - std::string htmp(i->first); - htmp.append(": "); - htmp.append(i->second); - hdrs = curl_slist_append(hdrs,htmp.c_str()); - } - if (hdrs) - curl_easy_setopt(curl,CURLOPT_HTTPHEADER,hdrs); - - //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); - curl_easy_setopt(curl,CURLOPT_WRITEDATA,(void *)&body); - curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,_curlStringAppendCallback); - - if(std::find(state.args.begin(), state.args.end(), "-X") == state.args.end()) - curl_easy_setopt(curl,CURLOPT_SSL_VERIFYPEER,(state.opts.count(ZT_CLI_FLAG_UNSAFE_SSL) > 0) ? 0L : 1L); - - if(requestType == REQ_POST) { - curl_easy_setopt(curl, CURLOPT_POST, 1); - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfield.c_str()); - } - if(requestType == REQ_DEL) - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); - if(requestType == REQ_GET) { - curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errbuf); - curl_easy_setopt(curl,CURLOPT_FOLLOWLOCATION,0L); - } - - curl_easy_setopt(curl,CURLOPT_USERAGENT,"ZeroTier-CLI"); - CURLcode res = curl_easy_perform(curl); - - errbuf[CURL_ERROR_SIZE-1] = (char)0; // sanity check - - if (res != CURLE_OK) - return std::make_tuple(-1,std::string(errbuf)); - - long response_code; - int rc = (int)curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE, &response_code); - - if(response_code == 401) { std::cout << FAIL_STR << response_code << "Unauthorized." << std::endl; exit(0); } - else if(response_code == 403) { std::cout << FAIL_STR << response_code << "Forbidden." << std::endl; exit(0); } - else if(response_code == 404) { std::cout << FAIL_STR << response_code << "Not found." << std::endl; exit(0); } - else if(response_code == 408) { std::cout << FAIL_STR << response_code << "Request timed out." << std::endl; exit(0); } - else if(response_code != 200) { std::cout << FAIL_STR << response_code << "Unable to process request." << std::endl; exit(0); } - - curl_easy_cleanup(curl); - if (hdrs) - curl_slist_free_all(hdrs); - return std::make_tuple(response_code,body); -} - -} // anonymous namespace - -////////////////////////////////////////////////////////////////////////////// - -// Check for user-specified @thing config -// Make sure it @thing makes sense -// Apply appropriate request headers -static void checkForThing(CLIState &state, std::string thingType, bool warnNoThingProvided) -{ - std::string configName; - if(state.atname.length()) { - configName = state.atname.erase(0,1); - // make sure specified @thing makes sense in the context of the command - if(thingType == "one" && state.settings["things"][configName]["type"].get() != "one") { - std::cout << FAIL_STR << "A ZeroTier Central config was specified for a ZeroTier One command." << std::endl; - exit(0); - } - if(thingType == "central" && state.settings["things"][configName]["type"].get() != "central") { - std::cout << FAIL_STR << "A ZeroTier One config was specified for a ZeroTier Central command." << std::endl; - exit(0); - } - } - else { // no @thing specified, check for defaults depending on type - if(thingType == "one") { - if(state.settings.find("defaultOne") != state.settings.end()) { - if(warnNoThingProvided) - std::cout << WARN_STR << "No @thing specified, assuming default for ZeroTier One command: " << state.settings["defaultOne"].get().c_str() << std::endl; - configName = state.settings["defaultOne"].get().erase(0,1); // get default - } - else { - std::cout << WARN_STR << "No @thing specified, and no default is known." << std::endl; - std::cout << "HELP: To set a default: zerotier cli-set defaultOne @my_default_thing" << std::endl; - exit(0); - } - } - if(thingType == "central") { - if(state.settings.find("defaultCentral") != state.settings.end()) { - if(warnNoThingProvided) - std::cout << WARN_STR << "No @thing specified, assuming default for ZeroTier Central command: " << state.settings["defaultCentral"].get().c_str() << std::endl; - configName = state.settings["defaultCentral"].get().erase(0,1); // get default - } - else { - std::cout << WARN_STR << "No @thing specified, and no default is known." << std::endl; - std::cout << "HELP: To set a default: zerotier cli-set defaultCentral @my_default_thing" << std::endl; - exit(0); - } - } - } - // Apply headers - if(thingType == "one") { - state.reqHeaders["X-ZT1-Auth"] = state.settings["things"][configName]["auth"]; - } - if(thingType == "central"){ - state.reqHeaders["Content-Type"] = "application/json"; - state.reqHeaders["Authorization"] = "Bearer " + state.settings["things"][configName]["auth"].get(); - state.reqHeaders["Accept"] = "application/json"; - } - state.url = state.settings["things"][configName]["url"]; -} - -static bool checkURL(std::string url) -{ - // TODO - return true; -} - -static std::string getLocalVersion(CLIState &state) -{ - json result; - std::tuple res; - checkForThing(state,"one",false); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "/status"); - if(std::get<0>(res) == 200) { - result = json::parse(std::get<1>(res)); - return result["version"].get(); - } - return "---"; -} - -#ifdef __WINDOWS__ -int _tmain(int argc, _TCHAR* argv[]) -#else -int main(int argc,char **argv) -#endif -{ -#ifdef __WINDOWS__ - { - WSADATA wsaData; - WSAStartup(MAKEWORD(2,2),&wsaData); - } -#endif - - curl_global_init(CURL_GLOBAL_DEFAULT); - CLIState state; - std::string arg1, arg2, authToken; - - for(int i=1;i 0)&&(port < 65536))&&(authToken.length() > 0)) { - state.settings["things"]["local"]["url"] = (std::string("http://127.0.0.1:") + portStr + "/"); - state.settings["things"]["local"]["auth"] = authToken; - initSuccess = true; - } - } - - if (!saveSettings(state)) { - std::cerr << "FATAL: unable to write " << getSettingsFilePath() << std::endl; - exit(-1); - } - - if (initSuccess) { - std::cerr << "INFO: initialized new config at " << getSettingsFilePath() << std::endl; - } else { - std::cerr << "INFO: initialized new config at " << getSettingsFilePath() << " but could not auto-init local ZeroTier One service config from " << oneHome << " -- you will need to set local service URL and port manually if you want to control a local instance of ZeroTier One. (This happens if you are not root/administrator.)" << std::endl; - } - } - } - - // PRE-REQUEST SETUP - - json result; - std::tuple res; - std::string url = ""; - - // META - - if ((state.command.length() == 0)||(state.command == "help")) { - dumpHelp(); - return -1; - } - - // zerotier version - else if (state.command == "v" || state.command == "version") { - std::cout << getLocalVersion(state) << std::endl; - return 1; - } - - // zerotier cli-set - else if (state.command == "cli-set") { - if(argc != 4) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-set " << std::endl; - return 1; - } - std::string settingName, settingValue; - if(state.atname.length()) { // User provided @thing erroneously, we will ignore it and adjust argument indices - settingName = argv[3]; - settingValue = argv[4]; - } - else { - settingName = argv[2]; - settingValue = argv[3]; - } - saveSettingsBackup(state); - state.settings[settingName] = settingValue; // changes - saveSettings(state); - } - - // zerotier cli-unset - else if (state.command == "cli-unset") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-unset " << std::endl; - return 1; - } - std::string settingName; - if(state.atname.length()) // User provided @thing erroneously, we will ignore it and adjust argument indices - settingName = argv[3]; - else - settingName = argv[2]; - saveSettingsBackup(state); - state.settings.erase(settingName); // changes - saveSettings(state); - } - - // zerotier @thing_to_remove cli-rm --- removes the configuration - else if (state.command == "cli-rm") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-rm <@thing>" << std::endl; - return 1; - } - if(state.settings["things"].find(state.atname) != state.settings["things"].end()) { - if(state.settings["defaultOne"] == state.atname) { - std::cout << "WARNING: The config you're trying to remove is currently set as your default. Set a new default first!" << std::endl; - std::cout << " | Usage: zerotier set defaultOne @your_other_thing" << std::endl; - } - else { - state.settings["things"].erase(state.atname.c_str()); - saveSettings(state); - } - } - } - - // zerotier cli-add-zt - // TODO: Check for malformed urls/auth - else if (state.command == "cli-add-zt") { - if(argc != 5) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-add-zt " << std::endl; - return 1; - } - std::string thing_name = argv[2], url = argv[3], auth = argv[4]; - if(!checkURL(url)) { - std::cout << FAIL_STR << "Malformed URL" << std::endl; - return 1; - } - if(state.settings.find(thing_name) != state.settings.end()) { - std::cout << "WARNING: A @thing with the shortname " << thing_name.c_str() - << " already exists. Choose another name or rename the old @thing" << std::endl; - std::cout << " | Usage: To rename a @thing: zerotier cli-rename @old_thing_name @new_thing_name" << std::endl; - } - else { - result = json::parse("{ \"auth\": \"" + auth + "\", \"type\": \"" + "one" + "\", \"url\": \"" + url + "\" }"); - saveSettingsBackup(state); - // TODO: Handle cases where user may or may not prepend an @ - state.settings["things"][thing_name] = result; // changes - saveSettings(state); - } - } - - // zerotier cli-add-central - // TODO: Check for malformed urls/auth - else if (state.command == "cli-add-central") { - if(argc != 5) { - std::cerr << INVALID_ARGS_STR << "zerotier cli-add-central " << std::endl; - return 1; - } - std::string thing_name = argv[2], url = argv[3], auth = argv[4]; - if(!checkURL(url)) { - std::cout << FAIL_STR << "Malformed URL" << std::endl; - return 1; - } - if(state.settings.find(thing_name) != state.settings.end()) { - std::cout << "WARNING: A @thing with the shortname " << thing_name.c_str() - << " already exists. Choose another name or rename the old @thing" << std::endl; - std::cout << " | Usage: To rename a @thing: zerotier cli-rename @old_thing_name @new_thing_name" << std::endl; - } - else { - result = json::parse("{ \"auth\": \"" + auth + "\", \"type\": \"" + "central" + "\", \"url\": \"" + url + "\" }"); - saveSettingsBackup(state); - // TODO: Handle cases where user may or may not prepend an @ - state.settings["things"]["@" + thing_name] = result; // changes - saveSettings(state); - } - } - - // ONE SERVICE - - // zerotier ls --- display all networks currently joined - else if (state.command == "ls" || state.command == "listnetworks") { - if(argc != 2) { - std::cerr << INVALID_ARGS_STR << "zerotier ls" << std::endl; - return 1; - } - checkForThing(state,"one",true); - url = state.url + "network"; - res = REQUEST(REQ_GET,state,state.reqHeaders,"",(const std::string)url); - if(std::get<0>(res) == 200) { - std::cout << "listnetworks " << std::endl; - auto j = json::parse(std::get<1>(res).c_str()); - if (j.type() == json::value_t::array) { - for(int i=0;i(); - std::string name = j[i]["name"].get(); - std::string mac = j[i]["mac"].get(); - std::string status = j[i]["status"].get(); - std::string type = j[i]["type"].get(); - std::string addrs; - for(int m=0; m() + " "; - } - std::string dev = j[i]["portDeviceName"].get(); - std::cout << "listnetworks " << nwid << " " << name << " " << mac << " " << status << " " << type << " " << dev << " " << addrs << std::endl; - } - } - } - } - - // zerotier join --- joins a network - else if (state.command == "join") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier join " << std::endl; - return 1; - } - checkForThing(state,"one",true); - res = REQUEST(REQ_POST,state,state.reqHeaders,"{}",state.url + "/network/" + state.args[0]); - if(std::get<0>(res) == 200) { - std::cout << OK_STR << "connected to " << state.args[0] << std::endl; - } - } - - // zerotier leave --- leaves a network - else if (state.command == "leave") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier leave " << std::endl; - return 1; - } - checkForThing(state,"one",true); - res = REQUEST(REQ_DEL,state,state.reqHeaders,"{}",state.url + "/network/" + state.args[0]); - if(std::get<0>(res) == 200) { - std::cout << OK_STR << "disconnected from " << state.args[0] << std::endl; - } - } - - // zerotier peers --- display address and role of all peers - else if (state.command == "peers") { - if(argc != 2) { - std::cerr << INVALID_ARGS_STR << "zerotier peers" << std::endl; - return 1; - } - checkForThing(state,"one",true); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "/peer"); - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - for(int i=0; i(res) == 200) { - result = json::parse(std::get<1>(res)); - std::string status_str = result["online"].get() ? "ONLINE" : "OFFLINE"; - std::cout << "info " << result["address"].get() - << " " << status_str << " " << result["version"].get() << std::endl; - } - } - - // REMOTE - - // zerotier @thing net-create --- creates a new network - else if (state.command == "net-create") { - if(argc > 3 || (argc == 3 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-create" << std::endl; - return 1; - } - checkForThing(state,"central",true); - res = REQUEST(REQ_POST,state,state.reqHeaders,"",state.url + "api/network"); - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - std::cout << OK_STR << "created network " << result["config"]["nwid"].get() << std::endl; - } - } - - // zerotier @thing net-rm --- deletes a network - else if (state.command == "net-rm") { - if(argc > 4 || (argc == 4 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-rm " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(!state.args.size()) { - std::cout << "Argument error: No network specified." << std::endl; - std::cout << " | Usage: zerotier net-rm " << std::endl; - } - else { - std::string nwid = state.args[0]; - res = REQUEST(REQ_DEL,state,state.reqHeaders,"",state.url + "api/network/" + nwid); - if(std::get<0>(res) == 200) { - std::cout << "deleted network " << nwid << std::endl; - } - } - } - - // zerotier @thing net-ls --- lists all networks - else if (state.command == "net-ls") { - if(argc > 3 || (argc == 3 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-ls" << std::endl; - return 1; - } - checkForThing(state,"central",true); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network"); - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - for(int m=0;m() << std::endl; - } - } - } - - // zerotier @thing net-members --- show all members of a network - else if (state.command == "net-members") { - if(argc > 4 || (argc == 4 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-members " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(!state.args.size()) { - std::cout << FAIL_STR << "Argument error: No network specified." << std::endl; - std::cout << " | Usage: zerotier net-members " << std::endl; - } - else { - std::string nwid = state.args[0]; - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member"); - json result = json::parse(std::get<1>(res)); - std::cout << "Members of " << nwid << ":" << std::endl; - for (json::iterator it = result.begin(); it != result.end(); ++it) { - std::cout << it.key() << std::endl; - } - } - } - - // zerotier @thing net-show --- show info about a device on a specific network - else if (state.command == "net-show") { - if(argc > 5 || (argc == 5 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-show " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(state.args.size() < 2) { - std::cout << FAIL_STR << "Argument error: Too few arguments." << std::endl; - std::cout << " | Usage: zerotier net-show " << std::endl; - } - else { - std::string nwid = state.args[0]; - std::string devid = state.args[1]; - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member/" + devid); - // TODO: More info, what would we like to show exactly? - if(std::get<0>(res) == 200) { - json result = json::parse(std::get<1>(res)); - std::cout << "Assigned IP: " << std::endl; - for(int m=0; m() << std::endl; - } - } - } - } - - // zerotier @thing net-auth --- authorize a device on a network - else if (state.command == "net-auth") { - if(argc > 5 || (argc == 5 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-auth " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(state.args.size() != 2) { - std::cout << FAIL_STR << "Argument error: Network and/or device ID not specified." << std::endl; - std::cout << " | Usage: zerotier net-auth " << std::endl; - } - std::string nwid = state.args[0]; - std::string devid = state.args[1]; - url = state.url + "api/network/" + nwid + "/member/" + devid; - // Add device to network - res = REQUEST(REQ_POST,state,state.reqHeaders,"",(const std::string)url); - if(std::get<0>(res) == 200) { - result = json::parse(std::get<1>(res)); - res = REQUEST(REQ_GET,state,state.reqHeaders,"",(const std::string)url); - result = json::parse(std::get<1>(res)); - result["config"]["authorized"] = "true"; - std::string newconfig = result.dump(); - res = REQUEST(REQ_POST,state,state.reqHeaders,newconfig,(const std::string)url); - if(std::get<0>(res) == 200) - std::cout << OK_STR << devid << " authorized on " << nwid << std::endl; - else - std::cout << FAIL_STR << "There was a problem authorizing that device." << std::endl; - } - } - - // zerotier @thing net-unauth - else if (state.command == "net-unauth") { - if(argc > 5 || (argc == 5 && !state.atname.length())) { - std::cerr << INVALID_ARGS_STR << "zerotier <@thing> net-unauth " << std::endl; - return 1; - } - checkForThing(state,"central",true); - if(state.args.size() != 2) { - std::cout << FAIL_STR << "Bad argument. No network and/or device ID specified." << std::endl; - std::cout << " | Usage: zerotier net-unauth " << std::endl; - } - std::string nwid = state.args[0]; - std::string devid = state.args[1]; - // If successful, get member config - res = REQUEST(REQ_GET,state,state.reqHeaders,"",state.url + "api/network/" + nwid + "/member/" + devid); - result = json::parse(std::get<1>(res)); - // modify auth field and re-POST - result["config"]["authorized"] = "false"; - std::string newconfig = result.dump(); - res = REQUEST(REQ_POST,state,state.reqHeaders,newconfig,state.url + "api/network/" + nwid + "/member/" + devid); - if(std::get<0>(res) == 200) - std::cout << OK_STR << devid << " de-authorized from " << nwid << std::endl; - else - std::cout << FAIL_STR << "There was a problem de-authorizing that device." << std::endl; - } - - // zerotier @thing net-set - else if (state.command == "net-set") { - } - - // ID - - // zerotier id-generate [] - else if (state.command == "id-generate") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier id-generate []" << std::endl; - return 1; - } - uint64_t vanity = 0; - int vanityBits = 0; - if (argc >= 5) { - vanity = Utils::hexStrToU64(argv[4]) & 0xffffffffffULL; - vanityBits = 4 * strlen(argv[4]); - if (vanityBits > 40) - vanityBits = 40; - } - - ZeroTier::Identity id; - for(;;) { - id.generate(); - if ((id.address().toInt() >> (40 - vanityBits)) == vanity) { - if (vanityBits > 0) { - fprintf(stderr,"vanity address: found %.10llx !\n",(unsigned long long)id.address().toInt()); - } - break; - } else { - fprintf(stderr,"vanity address: tried %.10llx looking for first %d bits of %.10llx\n",(unsigned long long)id.address().toInt(),vanityBits,(unsigned long long)(vanity << (40 - vanityBits))); - } - } - - std::string idser = id.toString(true); - if (argc >= 3) { - if (!OSUtils::writeFile(argv[2],idser)) { - std::cerr << "Error writing to " << argv[2] << std::endl; - return 1; - } else std::cout << argv[2] << " written" << std::endl; - if (argc >= 4) { - idser = id.toString(false); - if (!OSUtils::writeFile(argv[3],idser)) { - std::cerr << "Error writing to " << argv[3] << std::endl; - return 1; - } else std::cout << argv[3] << " written" << std::endl; - } - } else std::cout << idser << std::endl; - } - - // zerotier id-validate - else if (state.command == "id-validate") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier id-validate " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - if (!id.locallyValidate()) { - std::cerr << argv[2] << " FAILED validation." << std::endl; - return 1; - } else std::cout << argv[2] << "is a valid identity" << std::endl; - } - - // zerotier id-sign - else if (state.command == "id-sign") { - if(argc != 4) { - std::cerr << INVALID_ARGS_STR << "zerotier id-sign " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - if (!id.hasPrivate()) { - std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl; - return 1; - } - std::string inf; - if (!OSUtils::readFile(argv[3],inf)) { - std::cerr << argv[3] << " is not readable" << std::endl; - return 1; - } - C25519::Signature signature = id.sign(inf.data(),(unsigned int)inf.length()); - std::cout << Utils::hex(signature.data,(unsigned int)signature.size()) << std::endl; - } - - // zerotier id-verify - else if (state.command == "id-verify") { - if(argc != 4) { - std::cerr << INVALID_ARGS_STR << "zerotier id-verify " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - std::string inf; - if (!OSUtils::readFile(argv[3],inf)) { - std::cerr << argv[3] << " is not readable" << std::endl; - return 1; - } - std::string signature(Utils::unhex(argv[4])); - if ((signature.length() > ZT_ADDRESS_LENGTH)&&(id.verify(inf.data(),(unsigned int)inf.length(),signature.data(),(unsigned int)signature.length()))) { - std::cout << argv[3] << " signature valid" << std::endl; - } else { - std::cerr << argv[3] << " signature check FAILED" << std::endl; - return 1; - } - } - - // zerotier id-getpublic - else if (state.command == "id-getpublic") { - if(argc != 3) { - std::cerr << INVALID_ARGS_STR << "zerotier id-getpublic " << std::endl; - return 1; - } - Identity id = getIdFromArg(argv[2]); - if (!id) { - std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; - return 1; - } - std::cerr << id.toString(false) << std::endl; - } - // - else { - dumpHelp(); - return -1; - } - if(std::find(state.args.begin(), state.args.end(), "-verbose") != state.args.end()) - std::cout << "\n\nAPI response = " << std::get<1>(res) << std::endl; - curl_global_cleanup(); - return 0; -} diff --git a/attic/linux-build-farm/README.md b/attic/linux-build-farm/README.md deleted file mode 100644 index 8055eb0b..00000000 --- a/attic/linux-build-farm/README.md +++ /dev/null @@ -1,8 +0,0 @@ -Dockerized Linux Build Farm -====== - -This subfolder contains Dockerfiles and a script to build Linux packages for a variety of Linux distributions. It's also an excellent way to test your CPU fans and stress test your disk. - -Running `build.sh` with no arguments builds everything. You can run `build.sh` with the name of a distro (e.g. centos-7) to only build that. Both 32 and 64 bit packages are built except where no 32-bit version of the distribution exists. - -The `make-apt-repos.sh` and `make-rpm-repos.sh` scripts build repositories. They may require some editing for outside-of-ZeroTier use, and be careful with the apt one if you have an existing *aptly* configuration. diff --git a/attic/linux-build-farm/amazon-2016.03/x64/Dockerfile b/attic/linux-build-farm/amazon-2016.03/x64/Dockerfile deleted file mode 100644 index bd1a246a..00000000 --- a/attic/linux-build-farm/amazon-2016.03/x64/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -#FROM ambakshi/amazon-linux:2016.03 -#MAINTAINER Adam Ierymenko - -#RUN yum update -y -#RUN yum install -y epel-release -#RUN yum install -y make development-tools rpmdevtools clang gcc-c++ ruby ruby-devel - -#RUN gem install ronn - -FROM zerotier/zt1-build-amazon-2016.03-x64-base -MAINTAINER Adam Ierymenko - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/build.sh b/attic/linux-build-farm/build.sh deleted file mode 100755 index 0eb7c5d2..00000000 --- a/attic/linux-build-farm/build.sh +++ /dev/null @@ -1,69 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin - -subdirs=$* -if [ ! -n "$subdirs" ]; then - subdirs=`find . -type d -name '*-*' -printf '%f '` -fi - -if [ ! -d ./ubuntu-trusty ]; then - echo 'Must run from linux-build-farm subfolder.' - exit 1 -fi - -rm -f zt1-src.tar.gz -cd .. -git archive --format=tar.gz --prefix=ZeroTierOne/ -o linux-build-farm/zt1-src.tar.gz HEAD -cd linux-build-farm - -# Note that --privileged is used so we can bind mount VM shares when building in a VM. -# It has no other impact or purpose, but probably doesn't matter here in any case. - -for distro in $subdirs; do - echo - echo "--- BUILDING FOR $distro ---" - echo - - cd $distro - - if [ -d x64 ]; then - cd x64 - mv ../../zt1-src.tar.gz . - docker build -t zt1-build-${distro}-x64 . - mv zt1-src.tar.gz ../.. - cd .. - fi - - if [ -d x86 ]; then - cd x86 - mv ../../zt1-src.tar.gz . - docker build -t zt1-build-${distro}-x86 . - mv zt1-src.tar.gz ../.. - cd .. - fi - - rm -f *.deb *.rpm - -# exit 0 - - if [ ! -n "`echo $distro | grep -F debian`" -a ! -n "`echo $distro | grep -F ubuntu`" ]; then - if [ -d x64 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x64 /bin/bash -c 'cd /ZeroTierOne ; make redhat ; cd .. ; cp `find /root/rpmbuild -type f -name *.rpm` /artifacts ; ls -l /artifacts' - fi - if [ -d x86 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x86 /bin/bash -c 'cd /ZeroTierOne ; make redhat ; cd .. ; cp `find /root/rpmbuild -type f -name *.rpm` /artifacts ; ls -l /artifacts' - fi - else - if [ -d x64 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x64 /bin/bash -c 'cd /ZeroTierOne ; make debian ; cd .. ; cp *.deb /artifacts ; ls -l /artifacts' - fi - if [ -d x86 ]; then - docker run --rm -v `pwd`:/artifacts --privileged -it zt1-build-${distro}-x86 /bin/bash -c 'cd /ZeroTierOne ; make debian ; cd .. ; cp *.deb /artifacts ; ls -l /artifacts' - fi - fi - - cd .. -done - -rm -f zt1-src.tar.gz diff --git a/attic/linux-build-farm/centos-6/x64/Dockerfile b/attic/linux-build-farm/centos-6/x64/Dockerfile deleted file mode 100644 index 2796e422..00000000 --- a/attic/linux-build-farm/centos-6/x64/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM centos:6 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y epel-release -RUN yum install -y make development-tools rpmdevtools clang gcc-c++ tar - -RUN yum install -y nodejs npm - -# Stop use of http-parser-devel which is installed by nodejs/npm -RUN rm -f /usr/include/http_parser.h - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/centos-6/x86/Dockerfile b/attic/linux-build-farm/centos-6/x86/Dockerfile deleted file mode 100644 index 8192d139..00000000 --- a/attic/linux-build-farm/centos-6/x86/Dockerfile +++ /dev/null @@ -1,13 +0,0 @@ -FROM toopher/centos-i386:centos6 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y epel-release -RUN yum install -y make development-tools rpmdevtools clang gcc-c++ tar - -RUN yum install -y nodejs npm - -# Stop use of http-parser-devel which is installed by nodejs/npm -RUN rm -f /usr/include/http_parser.h - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/centos-7/x64/Dockerfile b/attic/linux-build-farm/centos-7/x64/Dockerfile deleted file mode 100644 index 10b58402..00000000 --- a/attic/linux-build-farm/centos-7/x64/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM centos:7 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y epel-release -RUN yum install -y make development-tools rpmdevtools clang gcc-c++ ruby ruby-devel - -RUN gem install ronn - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/centos-7/x86/Dockerfile b/attic/linux-build-farm/centos-7/x86/Dockerfile deleted file mode 100644 index a637a8d3..00000000 --- a/attic/linux-build-farm/centos-7/x86/Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -#FROM zerotier/centos7-32bit -#MAINTAINER Adam Ierymenko - -#RUN echo 'i686-redhat-linux' >/etc/rpm/platform - -#RUN yum update -y -#RUN yum install -y make development-tools rpmdevtools http-parser-devel lz4-devel libnatpmp-devel - -#RUN yum install -y gcc-c++ -#RUN rpm --install --force https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm -#RUN rpm --install --force ftp://rpmfind.net/linux/centos/6.8/os/i386/Packages/libffi-3.0.5-3.2.el6.i686.rpm -#RUN yum install -y clang - -FROM zerotier/zt1-build-centos-7-x86-base -MAINTAINER Adam Ierymenko - -RUN yum install -y ruby ruby-devel -RUN gem install ronn - -#RUN rpm --erase http-parser-devel lz4-devel libnatpmp-devel - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-jessie/x64/Dockerfile b/attic/linux-build-farm/debian-jessie/x64/Dockerfile deleted file mode 100644 index 316c1d83..00000000 --- a/attic/linux-build-farm/debian-jessie/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM debian:jessie -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.5 - -RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-jessie/x86/Dockerfile b/attic/linux-build-farm/debian-jessie/x86/Dockerfile deleted file mode 100644 index 3ad83329..00000000 --- a/attic/linux-build-farm/debian-jessie/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM 32bit/debian:jessie -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.5 - -RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-stretch/x64/Dockerfile b/attic/linux-build-farm/debian-stretch/x64/Dockerfile deleted file mode 100644 index c973c2b7..00000000 --- a/attic/linux-build-farm/debian-stretch/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM debian:stretch -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang - -#RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-stretch/x86/Dockerfile b/attic/linux-build-farm/debian-stretch/x86/Dockerfile deleted file mode 100644 index bfc7a86f..00000000 --- a/attic/linux-build-farm/debian-stretch/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM mcandre/docker-debian-32bit:stretch -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang - -#RUN ln -sf /usr/bin/clang++-3.5 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.5 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/debian-wheezy/x64/Dockerfile b/attic/linux-build-farm/debian-wheezy/x64/Dockerfile deleted file mode 100644 index 77e1c325..00000000 --- a/attic/linux-build-farm/debian-wheezy/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM debian:wheezy -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper ruby-ronn g++ make devscripts - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / - -RUN mv -f /ZeroTierOne/debian/control.wheezy /ZeroTierOne/debian/control -RUN mv -f /ZeroTierOne/debian/rules.wheezy /ZeroTierOne/debian/rules diff --git a/attic/linux-build-farm/debian-wheezy/x86/Dockerfile b/attic/linux-build-farm/debian-wheezy/x86/Dockerfile deleted file mode 100644 index 1f0117d2..00000000 --- a/attic/linux-build-farm/debian-wheezy/x86/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -#FROM tubia/debian:wheezy -#MAINTAINER Adam Ierymenko - -#RUN apt-get update -#RUN apt-get install -y build-essential debhelper ruby-ronn g++ make devscripts - -FROM zerotier/zt1-build-debian-wheezy-x86-base -MAINTAINER Adam Ierymenko - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / - -RUN mv -f /ZeroTierOne/debian/control.wheezy /ZeroTierOne/debian/control -RUN mv -f /ZeroTierOne/debian/rules.wheezy /ZeroTierOne/debian/rules diff --git a/attic/linux-build-farm/fedora-22/x64/Dockerfile b/attic/linux-build-farm/fedora-22/x64/Dockerfile deleted file mode 100644 index 6da0a921..00000000 --- a/attic/linux-build-farm/fedora-22/x64/Dockerfile +++ /dev/null @@ -1,10 +0,0 @@ -FROM fedora:22 -MAINTAINER Adam Ierymenko - -RUN yum update -y -RUN yum install -y make rpmdevtools gcc-c++ rubygem-ronn json-parser-devel lz4-devel http-parser-devel libnatpmp-devel - -RUN rpm --erase http-parser-devel -RUN yum install -y rubygem-ronn ruby - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/fedora-22/x86/Dockerfile b/attic/linux-build-farm/fedora-22/x86/Dockerfile deleted file mode 100644 index 3c24b844..00000000 --- a/attic/linux-build-farm/fedora-22/x86/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -#FROM nickcis/fedora-32:22 -#MAINTAINER Adam Ierymenko - -#RUN mkdir -p /etc/dnf/vars -#RUN echo 'i386' >/etc/dnf/vars/basearch -#RUN echo 'i386' >/etc/dnf/vars/arch - -#RUN yum update -y -#RUN yum install -y make rpmdevtools gcc-c++ rubygem-ronn json-parser-devel lz4-devel http-parser-devel libnatpmp-devel - -FROM zerotier/zt1-build-fedora-22-x86-base -MAINTAINER Adam Ierymenko - -RUN echo 'i686-redhat-linux' >/etc/rpm/platform - -RUN rpm --erase http-parser-devel -RUN yum install -y rubygem-ronn ruby - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/make-apt-repos.sh b/attic/linux-build-farm/make-apt-repos.sh deleted file mode 100755 index 7a81cc5c..00000000 --- a/attic/linux-build-farm/make-apt-repos.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -# This builds a series of Debian repositories for each distribution. - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -for distro in debian-* ubuntu-*; do - if [ -n "`find ${distro} -name '*.deb' -type f`" ]; then - arches=`ls ${distro}/*.deb | cut -d _ -f 3 | cut -d . -f 1 | xargs | sed 's/ /,/g'` - distro_name=`echo $distro | cut -d '-' -f 2` - echo '---' $distro / $distro_name / $arches - aptly repo create -architectures=${arches} -comment="ZeroTier, Inc. Debian Packages" -component="main" -distribution=${distro_name} zt-release-${distro_name} - aptly repo add zt-release-${distro_name} ${distro}/*.deb - aptly publish repo zt-release-${distro_name} $distro_name - fi -done diff --git a/attic/linux-build-farm/make-rpm-repos.sh b/attic/linux-build-farm/make-rpm-repos.sh deleted file mode 100755 index 0ed1cfe4..00000000 --- a/attic/linux-build-farm/make-rpm-repos.sh +++ /dev/null @@ -1,64 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -GPG_KEY=contact@zerotier.com - -rm -rf /tmp/zt-rpm-repo -mkdir /tmp/zt-rpm-repo - -for distro in centos-* fedora-* amazon-*; do - dname=`echo $distro | cut -d '-' -f 1` - if [ "$dname" = "centos" ]; then - dname=el - fi - if [ "$dname" = "fedora" ]; then - dname=fc - fi - if [ "$dname" = "amazon" ]; then - dname=amzn1 - fi - dvers=`echo $distro | cut -d '-' -f 2` - - mkdir -p /tmp/zt-rpm-repo/$dname/$dvers - - cp -v $distro/*.rpm /tmp/zt-rpm-repo/$dname/$dvers -done - -rpmsign --resign --key-id=$GPG_KEY --digest-algo=sha256 `find /tmp/zt-rpm-repo -type f -name '*.rpm'` - -for db in `find /tmp/zt-rpm-repo -mindepth 2 -maxdepth 2 -type d`; do - createrepo --database $db -done - -# Stupid RHEL stuff -cd /tmp/zt-rpm-repo/el -ln -sf 6 6Client -ln -sf 6 6Workstation -ln -sf 6 6Server -ln -sf 6 6.0 -ln -sf 6 6.1 -ln -sf 6 6.2 -ln -sf 6 6.3 -ln -sf 6 6.4 -ln -sf 6 6.5 -ln -sf 6 6.6 -ln -sf 6 6.7 -ln -sf 6 6.8 -ln -sf 6 6.9 -ln -sf 7 7Client -ln -sf 7 7Workstation -ln -sf 7 7Server -ln -sf 7 7.0 -ln -sf 7 7.1 -ln -sf 7 7.2 -ln -sf 7 7.3 -ln -sf 7 7.4 -ln -sf 7 7.5 -ln -sf 7 7.6 -ln -sf 7 7.7 -ln -sf 7 7.8 -ln -sf 7 7.9 - -echo -echo Repo created in /tmp/zt-rpm-repo diff --git a/attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile b/attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile deleted file mode 100644 index f84cc6e3..00000000 --- a/attic/linux-build-farm/ubuntu-trusty/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM ubuntu:14.04 -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.6 - -RUN ln -sf /usr/bin/clang++-3.6 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.6 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile b/attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile deleted file mode 100644 index 6be3ae87..00000000 --- a/attic/linux-build-farm/ubuntu-trusty/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM 32bit/ubuntu:14.04 -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.6 - -RUN ln -sf /usr/bin/clang++-3.6 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.6 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-wily/x64/Dockerfile b/attic/linux-build-farm/ubuntu-wily/x64/Dockerfile deleted file mode 100644 index 99b8d34c..00000000 --- a/attic/linux-build-farm/ubuntu-wily/x64/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM ubuntu:wily -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.7 - -RUN ln -sf /usr/bin/clang++-3.7 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.7 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-wily/x86/Dockerfile b/attic/linux-build-farm/ubuntu-wily/x86/Dockerfile deleted file mode 100644 index 86ad14f2..00000000 --- a/attic/linux-build-farm/ubuntu-wily/x86/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM daald/ubuntu32:wily -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.7 - -RUN ln -sf /usr/bin/clang++-3.7 /usr/bin/clang++ -RUN ln -sf /usr/bin/clang-3.7 /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile b/attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile deleted file mode 100644 index fa665a0a..00000000 --- a/attic/linux-build-farm/ubuntu-xenial/x64/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM ubuntu:xenial -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.8 - -#RUN ln -sf /usr/bin/clang++-3.8 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.8 /usr/bin/clang - -RUN rm -f /usr/bin/clang++ /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile b/attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile deleted file mode 100644 index d01eec9b..00000000 --- a/attic/linux-build-farm/ubuntu-xenial/x86/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ -FROM f69m/ubuntu32:xenial -MAINTAINER Adam Ierymenko - -RUN apt-get update -RUN apt-get install -y build-essential debhelper libhttp-parser-dev liblz4-dev libnatpmp-dev dh-systemd ruby-ronn g++ make devscripts clang-3.8 - -#RUN ln -sf /usr/bin/clang++-3.8 /usr/bin/clang++ -#RUN ln -sf /usr/bin/clang-3.8 /usr/bin/clang - -RUN rm -f /usr/bin/clang++ /usr/bin/clang - -RUN dpkg --purge libhttp-parser-dev - -ADD zt1-src.tar.gz / diff --git a/attic/old-controller-schema.sql b/attic/old-controller-schema.sql deleted file mode 100644 index d1daf8d0..00000000 --- a/attic/old-controller-schema.sql +++ /dev/null @@ -1,112 +0,0 @@ -CREATE TABLE Config ( - k varchar(16) PRIMARY KEY NOT NULL, - v varchar(1024) NOT NULL -); - -CREATE TABLE Network ( - id char(16) PRIMARY KEY NOT NULL, - name varchar(128) NOT NULL, - private integer NOT NULL DEFAULT(1), - enableBroadcast integer NOT NULL DEFAULT(1), - allowPassiveBridging integer NOT NULL DEFAULT(0), - multicastLimit integer NOT NULL DEFAULT(32), - creationTime integer NOT NULL DEFAULT(0), - revision integer NOT NULL DEFAULT(1), - memberRevisionCounter integer NOT NULL DEFAULT(1), - flags integer NOT NULL DEFAULT(0) -); - -CREATE TABLE AuthToken ( - id integer PRIMARY KEY NOT NULL, - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - authMode integer NOT NULL DEFAULT(1), - useCount integer NOT NULL DEFAULT(0), - maxUses integer NOT NULL DEFAULT(0), - expiresAt integer NOT NULL DEFAULT(0), - token varchar(256) NOT NULL -); - -CREATE INDEX AuthToken_networkId_token ON AuthToken(networkId,token); - -CREATE TABLE Node ( - id char(10) PRIMARY KEY NOT NULL, - identity varchar(4096) NOT NULL -); - -CREATE TABLE IpAssignment ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - nodeId char(10) REFERENCES Node(id) ON DELETE CASCADE, - type integer NOT NULL DEFAULT(0), - ip blob(16) NOT NULL, - ipNetmaskBits integer NOT NULL DEFAULT(0), - ipVersion integer NOT NULL DEFAULT(4) -); - -CREATE UNIQUE INDEX IpAssignment_networkId_ip ON IpAssignment (networkId, ip); - -CREATE INDEX IpAssignment_networkId_nodeId ON IpAssignment (networkId, nodeId); - -CREATE TABLE IpAssignmentPool ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - ipRangeStart blob(16) NOT NULL, - ipRangeEnd blob(16) NOT NULL, - ipVersion integer NOT NULL DEFAULT(4) -); - -CREATE UNIQUE INDEX IpAssignmentPool_networkId_ipRangeStart ON IpAssignmentPool (networkId,ipRangeStart); - -CREATE TABLE Member ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE, - authorized integer NOT NULL DEFAULT(0), - activeBridge integer NOT NULL DEFAULT(0), - memberRevision integer NOT NULL DEFAULT(0), - flags integer NOT NULL DEFAULT(0), - lastRequestTime integer NOT NULL DEFAULT(0), - lastPowDifficulty integer NOT NULL DEFAULT(0), - lastPowTime integer NOT NULL DEFAULT(0), - recentHistory blob, - PRIMARY KEY (networkId, nodeId) -); - -CREATE INDEX Member_networkId_nodeId ON Member(networkId,nodeId); -CREATE INDEX Member_networkId_activeBridge ON Member(networkId, activeBridge); -CREATE INDEX Member_networkId_memberRevision ON Member(networkId, memberRevision); -CREATE INDEX Member_networkId_lastRequestTime ON Member(networkId, lastRequestTime); - -CREATE TABLE Route ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - target blob(16) NOT NULL, - via blob(16), - targetNetmaskBits integer NOT NULL, - ipVersion integer NOT NULL, - flags integer NOT NULL, - metric integer NOT NULL -); - -CREATE INDEX Route_networkId ON Route (networkId); - -CREATE TABLE Rule ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - capId integer, - ruleNo integer NOT NULL, - ruleType integer NOT NULL DEFAULT(0), - "addr" blob(16), - "int1" integer, - "int2" integer, - "int3" integer, - "int4" integer -); - -CREATE INDEX Rule_networkId_capId ON Rule (networkId,capId); - -CREATE TABLE MemberTC ( - networkId char(16) NOT NULL REFERENCES Network(id) ON DELETE CASCADE, - nodeId char(10) NOT NULL REFERENCES Node(id) ON DELETE CASCADE, - tagId integer, - tagValue integer, - capId integer, - capMaxCustodyChainLength integer NOT NULL DEFAULT(1) -); - -CREATE INDEX MemberTC_networkId_nodeId ON MemberTC (networkId,nodeId); diff --git a/attic/old-linux-installer/buildinstaller.sh b/attic/old-linux-installer/buildinstaller.sh deleted file mode 100755 index 21f2f73e..00000000 --- a/attic/old-linux-installer/buildinstaller.sh +++ /dev/null @@ -1,134 +0,0 @@ -#!/bin/bash - -# This script builds the installer for *nix systems. Windows must do everything -# completely differently, as usual. - -export PATH=/bin:/usr/bin:/sbin:/usr/sbin - -if [ ! -f zerotier-one ]; then - echo "Could not find 'zerotier-one' binary, please build before running this script." - exit 2 -fi - -machine=`uname -m` -system=`uname -s` - -vmajor=`cat version.h | grep -F ZEROTIER_ONE_VERSION_MAJOR | cut -d ' ' -f 3` -vminor=`cat version.h | grep -F ZEROTIER_ONE_VERSION_MINOR | cut -d ' ' -f 3` -revision=`cat version.h | grep -F ZEROTIER_ONE_VERSION_REVISION | cut -d ' ' -f 3` - -if [ -z "$vmajor" -o -z "$vminor" -o -z "$revision" ]; then - echo "Unable to extract version info from version.h, aborting installer build." - exit 2 -fi - -rm -rf build-installer -mkdir build-installer - -case "$system" in - - Linux) - # Canonicalize $machine for some architectures... we use x86 - # and x64 for Intel stuff. ARM and others should be fine if - # we ever ship officially for those. - debian_arch=$machine - case "$machine" in - i386|i486|i586|i686) - machine="x86" - debian_arch="i386" - ;; - x86_64|amd64|x64) - machine="x64" - debian_arch="amd64" - ;; - armv6l|arm|armhf|arm7l|armv7l) - machine="armv6l" - debian_arch="armhf" - ;; - esac - - echo "Assembling Linux installer for $machine and version $vmajor.$vminor.$revision" - - mkdir -p 'build-installer/var/lib/zerotier-one/ui' - cp -fp 'ext/installfiles/linux/uninstall.sh' 'build-installer/var/lib/zerotier-one' - cp -fp 'zerotier-one' 'build-installer/var/lib/zerotier-one' - for f in ui/*.html ui/*.js ui/*.css ui/*.jsx ; do - cp -fp "$f" 'build-installer/var/lib/zerotier-one/ui' - done - mkdir -p 'build-installer/tmp' - cp -fp 'ext/installfiles/linux/init.d/zerotier-one' 'build-installer/tmp/init.d_zerotier-one' - cp -fp 'ext/installfiles/linux/systemd/zerotier-one.service' 'build-installer/tmp/systemd_zerotier-one.service' - - targ="ZeroTierOneInstaller-linux-${machine}-${vmajor}_${vminor}_${revision}" - # Use gzip in Linux since some minimal Linux systems do not have bunzip2 - rm -f build-installer-tmp.tar.gz - cd build-installer - tar -cf - * | gzip -9 >../build-installer-tmp.tar.gz - cd .. - rm -f $targ - cat ext/installfiles/linux/install.tmpl.sh build-installer-tmp.tar.gz >$targ - chmod 0755 $targ - rm -f build-installer-tmp.tar.gz - ls -l $targ - - if [ -f /usr/bin/dpkg-deb -a "$UID" -eq 0 ]; then - echo - echo Found dpkg-deb and you are root, trying to build Debian package. - - rm -rf build-installer-deb - - debbase="build-installer-deb/zerotier-one_${vmajor}.${vminor}.${revision}_$debian_arch" - debfolder="${debbase}/DEBIAN" - mkdir -p $debfolder - - cat 'ext/installfiles/linux/DEBIAN/control.in' | sed "s/__VERSION__/${vmajor}.${vminor}.${revision}/" | sed "s/__ARCH__/${debian_arch}/" >$debfolder/control - cat $debfolder/control - cp -f 'ext/installfiles/linux/DEBIAN/conffiles' "${debfolder}/conffiles" - - mkdir -p "${debbase}/var/lib/zerotier-one/updates.d" - cp -f $targ "${debbase}/var/lib/zerotier-one/updates.d" - - rm -f "${debfolder}/postinst" "${debfolder}/prerm" - - echo '#!/bin/bash' >${debfolder}/postinst - echo "/var/lib/zerotier-one/updates.d/${targ} >>/dev/null 2>&1" >>${debfolder}/postinst - echo "/bin/rm -f /var/lib/zerotier-one/updates.d/*" >>${debfolder}/postinst - chmod a+x ${debfolder}/postinst - - echo '#!/bin/bash' >${debfolder}/prerm - echo 'export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin' >>${debfolder}/prerm - echo 'if [ "$1" != "upgrade" ]; then' >>${debfolder}/prerm - echo ' /var/lib/zerotier-one/uninstall.sh >>/dev/null 2>&1' >>${debfolder}/prerm - echo 'fi' >>${debfolder}/prerm - chmod a+x ${debfolder}/prerm - - dpkg-deb --build $debbase - - mv -f build-installer-deb/*.deb . - rm -rf build-installer-deb - fi - - if [ -f /usr/bin/rpmbuild ]; then - echo - echo Found rpmbuild, trying to build RedHat/CentOS package. - - rm -f /tmp/zerotier-one.spec - curr_dir=`pwd` - cat ext/installfiles/linux/RPM/zerotier-one.spec.in | sed "s/__VERSION__/${vmajor}.${vminor}.${revision}/g" | sed "s/__INSTALLER__/${targ}/g" >/tmp/zerotier-one.spec - - rpmbuild -ba /tmp/zerotier-one.spec - - rm -f /tmp/zerotier-one.spec - fi - - ;; - - *) - echo "Unsupported platform: $system" - exit 2 - -esac - -rm -rf build-installer - -exit 0 diff --git a/attic/old-linux-installer/install.tmpl.sh b/attic/old-linux-installer/install.tmpl.sh deleted file mode 100644 index 2d18d24c..00000000 --- a/attic/old-linux-installer/install.tmpl.sh +++ /dev/null @@ -1,182 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin -shopt -s expand_aliases - -dryRun=0 - -echo "*** ZeroTier One install/update ***" -echo - -if [ "$UID" -ne 0 ]; then - echo "Not running as root so doing dry run (no modifications to system)..." - dryRun=1 -fi - -if [ $dryRun -gt 0 ]; then - alias ln="echo '>> ln'" - alias rm="echo '>> rm'" - alias mv="echo '>> mv'" - alias cp="echo '>> cp'" - alias chown="echo '>> chown'" - alias chgrp="echo '>> chgrp'" - alias chmod="echo '>> chmod'" - alias chkconfig="echo '>> chkconfig'" - alias zerotier-cli="echo '>> zerotier-cli'" - alias service="echo '>> service'" - alias systemctl="echo '>> systemctl'" -fi - -scriptPath="`dirname "$0"`/`basename "$0"`" -if [ ! -r "$scriptPath" ]; then - scriptPath="$0" - if [ ! -r "$scriptPath" ]; then - echo "Installer cannot determine its own path; $scriptPath is not readable." - exit 2 - fi -fi - -# Check for systemd vs. old school SysV init -SYSTEMDUNITDIR= -if [ -e /bin/systemctl -o -e /usr/bin/systemctl -o -e /usr/local/bin/systemctl -o -e /sbin/systemctl -o -e /usr/sbin/systemctl ]; then - # Second check: test if systemd appears to actually be running. Apparently Ubuntu - # thought it was a good idea to ship with systemd installed but not used. Issue #133 - if [ -d /var/run/systemd/system -o -d /run/systemd/system ]; then - if [ -e /usr/bin/pkg-config ]; then - SYSTEMDUNITDIR=`/usr/bin/pkg-config systemd --variable=systemdsystemunitdir` - fi - if [ -z "$SYSTEMDUNITDIR" -o ! -d "$SYSTEMDUNITDIR" ]; then - if [ -d /usr/lib/systemd/system ]; then - SYSTEMDUNITDIR=/usr/lib/systemd/system - fi - if [ -d /etc/systemd/system ]; then - SYSTEMDUNITDIR=/etc/systemd/system - fi - fi - fi -fi - -# Find the end of this script, which is where we have appended binary data. -endMarkerIndex=`grep -a -b -E '^################' "$scriptPath" | head -c 16 | cut -d : -f 1` -if [ "$endMarkerIndex" -le 100 ]; then - echo 'Internal error: unable to find end of script / start of binary data marker.' - exit 2 -fi -blobStart=`expr $endMarkerIndex + 17` -if [ "$blobStart" -le "$endMarkerIndex" ]; then - echo 'Internal error: unable to find end of script / start of binary data marker.' - exit 2 -fi - -echo -n 'Getting version of existing install... ' -origVersion=NONE -if [ -x /var/lib/zerotier-one/zerotier-one ]; then - origVersion=`/var/lib/zerotier-one/zerotier-one -v` -fi -echo $origVersion - -echo 'Extracting files...' -if [ $dryRun -gt 0 ]; then - echo ">> tail -c +$blobStart \"$scriptPath\" | gunzip -c | tar -xvop -C / -f -" - tail -c +$blobStart "$scriptPath" | gunzip -c | tar -t -f - | sed 's/^/>> /' -else - tail -c +$blobStart "$scriptPath" | gunzip -c | tar -xvop --no-overwrite-dir -C / -f - -fi - -if [ $dryRun -eq 0 -a ! -x "/var/lib/zerotier-one/zerotier-one" ]; then - echo 'Archive extraction failed, cannot find zerotier-one binary in "/var/lib/zerotier-one".' - exit 2 -fi - -echo -n 'Getting version of new install... ' -newVersion=`/var/lib/zerotier-one/zerotier-one -v` -echo $newVersion - -echo 'Creating symlinks...' - -rm -f /usr/bin/zerotier-cli /usr/bin/zerotier-idtool -ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-cli -ln -sf /var/lib/zerotier-one/zerotier-one /usr/bin/zerotier-idtool - -echo 'Installing zerotier-one service...' - -if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then - # SYSTEMD - - # If this was updated or upgraded from an init.d based system, clean up the old - # init.d stuff before installing directly via systemd. - if [ -f /etc/init.d/zerotier-one ]; then - if [ -e /sbin/chkconfig -o -e /usr/sbin/chkconfig -o -e /bin/chkconfig -o -e /usr/bin/chkconfig ]; then - chkconfig zerotier-one off - fi - rm -f /etc/init.d/zerotier-one - fi - - cp -f /tmp/systemd_zerotier-one.service "$SYSTEMDUNITDIR/zerotier-one.service" - chown 0 "$SYSTEMDUNITDIR/zerotier-one.service" - chgrp 0 "$SYSTEMDUNITDIR/zerotier-one.service" - chmod 0644 "$SYSTEMDUNITDIR/zerotier-one.service" - rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one - - systemctl enable zerotier-one.service - - echo - echo 'Done! Installed and service configured to start at system boot.' - echo - echo "To start now or restart the service if it's already running:" - echo ' sudo systemctl restart zerotier-one.service' -else - # SYSV INIT -- also covers upstart which supports SysVinit backward compatibility - - cp -f /tmp/init.d_zerotier-one /etc/init.d/zerotier-one - chmod 0755 /etc/init.d/zerotier-one - rm -f /tmp/systemd_zerotier-one.service /tmp/init.d_zerotier-one - - if [ -f /sbin/chkconfig -o -f /usr/sbin/chkconfig -o -f /usr/bin/chkconfig -o -f /bin/chkconfig ]; then - chkconfig zerotier-one on - else - # Yes Virginia, some systems lack chkconfig. - if [ -d /etc/rc0.d ]; then - rm -f /etc/rc0.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc0.d/K89zerotier-one - fi - if [ -d /etc/rc1.d ]; then - rm -f /etc/rc1.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc1.d/K89zerotier-one - fi - if [ -d /etc/rc2.d ]; then - rm -f /etc/rc2.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc2.d/S11zerotier-one - fi - if [ -d /etc/rc3.d ]; then - rm -f /etc/rc3.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc3.d/S11zerotier-one - fi - if [ -d /etc/rc4.d ]; then - rm -f /etc/rc4.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc4.d/S11zerotier-one - fi - if [ -d /etc/rc5.d ]; then - rm -f /etc/rc5.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc5.d/S11zerotier-one - fi - if [ -d /etc/rc6.d ]; then - rm -f /etc/rc6.d/???zerotier-one - ln -sf /etc/init.d/zerotier-one /etc/rc6.d/K89zerotier-one - fi - fi - - echo - echo 'Done! Installed and service configured to start at system boot.' - echo - echo "To start now or restart the service if it's already running:" - echo ' sudo service zerotier-one restart' -fi - -exit 0 - -# Do not remove the last line or add a carriage return to it! The installer -# looks for an unterminated line beginning with 16 #'s in itself to find -# the binary blob data, which is appended after it. - -################ \ No newline at end of file diff --git a/attic/old-linux-installer/uninstall.sh b/attic/old-linux-installer/uninstall.sh deleted file mode 100755 index d9495a18..00000000 --- a/attic/old-linux-installer/uninstall.sh +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin - -if [ "$UID" -ne 0 ]; then - echo "Must be run as root; try: sudo $0" - exit 1 -fi - -# Detect systemd vs. regular init -SYSTEMDUNITDIR= -if [ -e /bin/systemctl -o -e /usr/bin/systemctl -o -e /usr/local/bin/systemctl -o -e /sbin/systemctl -o -e /usr/sbin/systemctl ]; then - if [ -e /usr/bin/pkg-config ]; then - SYSTEMDUNITDIR=`/usr/bin/pkg-config systemd --variable=systemdsystemunitdir` - fi - if [ -z "$SYSTEMDUNITDIR" -o ! -d "$SYSTEMDUNITDIR" ]; then - if [ -d /usr/lib/systemd/system ]; then - SYSTEMDUNITDIR=/usr/lib/systemd/system - fi - if [ -d /etc/systemd/system ]; then - SYSTEMDUNITDIR=/etc/systemd/system - fi - fi -fi - -echo "Killing any running zerotier-one service..." -if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" ]; then - systemctl stop zerotier-one.service - systemctl disable zerotier-one.service -else - if [ -f /sbin/service -o -f /usr/sbin/service -o -f /bin/service -o -f /usr/bin/service ]; then - service zerotier-one stop - fi -fi - -sleep 1 -if [ -f /var/lib/zerotier-one/zerotier-one.pid ]; then - kill -TERM `cat /var/lib/zerotier-one/zerotier-one.pid` - sleep 1 -fi -if [ -f /var/lib/zerotier-one/zerotier-one.pid ]; then - kill -KILL `cat /var/lib/zerotier-one/zerotier-one.pid` -fi - -if [ -f /etc/init.d/zerotier-one ]; then - echo "Removing SysV init items..." - if [ -f /sbin/chkconfig -o -f /usr/sbin/chkconfig -o -f /bin/chkconfig -o -f /usr/bin/chkconfig ]; then - chkconfig zerotier-one off - fi - rm -f /etc/init.d/zerotier-one - find /etc/rc*.d -type f -name '???zerotier-one' -print0 | xargs -0 rm -f -fi - -if [ -n "$SYSTEMDUNITDIR" -a -d "$SYSTEMDUNITDIR" -a -f "$SYSTEMDUNITDIR/zerotier-one.service" ]; then - echo "Removing systemd service..." - rm -f "$SYSTEMDUNITDIR/zerotier-one.service" -fi - -echo "Erasing binary and support files..." -if [ -d /var/lib/zerotier-one ]; then - cd /var/lib/zerotier-one - rm -rf zerotier-one *.persist identity.public *.log *.pid *.sh updates.d networks.d iddb.d root-topology ui -fi - -echo "Erasing anything installed into system bin directories..." -rm -f /usr/local/bin/zerotier-cli /usr/bin/zerotier-cli /usr/local/bin/zerotier-idtool /usr/bin/zerotier-idtool - -echo "Done." -echo -echo "Your ZeroTier One identity is still preserved in /var/lib/zerotier-one" -echo "as identity.secret and can be manually deleted if you wish. Save it if" -echo "you wish to re-use the address of this node, as it cannot be regenerated." - -echo - -exit 0 diff --git a/attic/root-watcher/README.md b/attic/root-watcher/README.md deleted file mode 100644 index ded6a63f..00000000 --- a/attic/root-watcher/README.md +++ /dev/null @@ -1,8 +0,0 @@ -Root Server Watcher -====== - -This is a small daemon written in NodeJS that watches a set of root servers and records peer status information into a Postgres database. - -To use type `npm install` to install modules. Then edit `config.json.example` and rename to `config.json`. For each of your roots you will need to configure a way for this script to reach it. You will also need to use `schema.sql` to initialize a Postgres database to contain your logs and set it up in `config.json` as well. - -This doesn't (yet) include any software for reading the log database and doing anything useful with the information inside, though given that it's a simple SQL database it should not be hard to compose queries to show interesting statistics. diff --git a/attic/root-watcher/config.json.example b/attic/root-watcher/config.json.example deleted file mode 100644 index 0ad1bbe1..00000000 --- a/attic/root-watcher/config.json.example +++ /dev/null @@ -1,30 +0,0 @@ -{ - "interval": 30000, - "dbSaveInterval": 60000, - "peerTimeout": 600000, - "db": { - "database": "ztr", - "user": "postgres", - "password": "s00p3rs3kr3t", - "host": "127.0.0.1", - "port": 5432, - "max": 16, - "idleTimeoutMillis": 30000 - }, - "roots": { - "my-root-01": { - "id": 1, - "ip": "10.0.0.1", - "port": 9993, - "authToken": "foobarbaz", - "peers": "/peer" - }, - "my-root-02": { - "id": 2, - "ip": "10.0.0.2", - "port": 9993, - "authToken": "lalafoo", - "peers": "/peer" - } - } -} diff --git a/attic/root-watcher/package.json b/attic/root-watcher/package.json deleted file mode 100644 index d6e86d78..00000000 --- a/attic/root-watcher/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "zerotier-root-watcher", - "version": "1.0.0", - "description": "Simple background service to watch a cluster of roots and record peer info into a database", - "main": "zerotier-root-watcher.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "ZeroTier, Inc. ", - "license": "GPL-3.0", - "dependencies": { - "async": "^2.3.0", - "pg": "^6.1.5", - "zlib": "^1.0.5" - } -} diff --git a/attic/root-watcher/schema.sql b/attic/root-watcher/schema.sql deleted file mode 100644 index ade0fa3e..00000000 --- a/attic/root-watcher/schema.sql +++ /dev/null @@ -1,20 +0,0 @@ -/* Schema for ZeroTier root watcher log database */ - -CREATE TABLE "Peer" -( - "ztAddress" BIGINT NOT NULL, - "timestamp" BIGINT NOT NULL, - "versionMajor" INTEGER NOT NULL, - "versionMinor" INTEGER NOT NULL, - "versionRev" INTEGER NOT NULL, - "rootId" INTEGER NOT NULL, - "phyPort" INTEGER NOT NULL, - "phyLinkQuality" REAL NOT NULL, - "phyLastReceive" BIGINT NOT NULL, - "phyAddress" INET NOT NULL -); - -CREATE INDEX "Peer_ztAddress" ON "Peer" ("ztAddress"); -CREATE INDEX "Peer_timestamp" ON "Peer" ("timestamp"); -CREATE INDEX "Peer_rootId" ON "Peer" ("rootId"); -CREATE INDEX "Peer_phyAddress" ON "Peer" ("phyAddress"); diff --git a/attic/root-watcher/zerotier-root-watcher.js b/attic/root-watcher/zerotier-root-watcher.js deleted file mode 100644 index d4607fc2..00000000 --- a/attic/root-watcher/zerotier-root-watcher.js +++ /dev/null @@ -1,235 +0,0 @@ -'use strict'; - -const pg = require('pg'); -const zlib = require('zlib'); -const http = require('http'); -const fs = require('fs'); -const async = require('async'); - -const config = JSON.parse(fs.readFileSync('./config.json')); -const roots = config.roots||{}; - -const db = new pg.Pool(config.db); - -process.on('uncaughtException',function(err) { - console.error('ERROR: uncaught exception: '+err); - if (err.stack) - console.error(err.stack); -}); - -function httpRequest(host,port,authToken,method,path,args,callback) -{ - var responseBody = []; - var postData = (args) ? JSON.stringify(args) : null; - - var req = http.request({ - host: host, - port: port, - path: path, - method: method, - headers: { - 'X-ZT1-Auth': (authToken||''), - 'Content-Length': (postData) ? postData.length : 0 - } - },function(res) { - res.on('data',function(chunk) { - if ((chunk)&&(chunk.length > 0)) - responseBody.push(chunk); - }); - res.on('timeout',function() { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(new Error('connection timed out'),null); - } - req.abort(); - } catch (e) {} - }); - res.on('error',function(e) { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(new Error('connection timed out'),null); - } - req.abort(); - } catch (e) {} - }); - res.on('end',function() { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - if (responseBody.length === 0) { - return cb(null,{}); - } else { - responseBody = Buffer.concat(responseBody); - - if (responseBody.length < 2) { - return cb(null,{}); - } - - if ((responseBody.readUInt8(0,true) === 0x1f)&&(responseBody.readUInt8(1,true) === 0x8b)) { - try { - responseBody = zlib.gunzipSync(responseBody); - } catch (e) { - return cb(e,null); - } - } - - try { - return cb(null,JSON.parse(responseBody)); - } catch (e) { - return cb(e,null); - } - } - } - }); - }).on('error',function(e) { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(e,null); - } - req.abort(); - } catch (e) {} - }).on('timeout',function() { - try { - if (typeof callback === 'function') { - var cb = callback; - callback = null; - cb(new Error('connection timed out'),null); - } - req.abort(); - } catch (e) {} - }); - - req.setTimeout(30000); - req.setNoDelay(true); - - if (postData !== null) - req.end(postData); - else req.end(); -}; - -var peerStatus = {}; - -function saveToDb() -{ - db.connect(function(err,client,clientDone) { - if (err) { - console.log('WARNING: database error writing peers: '+err.toString()); - clientDone(); - return setTimeout(saveToDb,config.dbSaveInterval||60000); - } - client.query('BEGIN',function(err) { - if (err) { - console.log('WARNING: database error writing peers: '+err.toString()); - clientDone(); - return setTimeout(saveToDb,config.dbSaveInterval||60000); - } - let timeout = Date.now() - (config.peerTimeout||600000); - let wtotal = 0; - async.eachSeries(Object.keys(peerStatus),function(address,nextAddress) { - let s = peerStatus[address]; - if (s[1] <= timeout) { - delete peerStatus[address]; - return process.nextTick(nextAddress); - } else { - ++wtotal; - client.query('INSERT INTO "Peer" ("ztAddress","timestamp","versionMajor","versionMinor","versionRev","rootId","phyPort","phyLinkQuality","phyLastReceive","phyAddress") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)',s,nextAddress); - } - },function(err) { - if (err) - console.log('WARNING database error writing peers: '+err.toString()); - console.log(Date.now().toString()+' '+wtotal); - client.query('COMMIT',function(err,result) { - clientDone(); - return setTimeout(saveToDb,config.dbSaveInterval||60000); - }); - }); - }); - }); -}; - -function doRootUpdate(name,id,ip,port,peersPath,authToken,interval) -{ - httpRequest(ip,port,authToken,"GET",peersPath,null,function(err,res) { - if (err) { - console.log('WARNING: cannot reach '+name+peersPath+' (will try again in 1s): '+err.toString()); - return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },1000); - } - if (!Array.isArray(res)) { - console.log('WARNING: cannot reach '+name+peersPath+' (will try again in 1s): response is not an array of peers'); - return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },1000); - } - - //console.log(name+': '+res.length+' peer entries.'); - let now = Date.now(); - let count = 0; - for(let pi=0;pi 0)) { - let bestPath = null; - for(let i=0;i 0)&&((!bestPath)||(bestPath.lastReceive < lr))) - bestPath = paths[i]; - } - } - - if (bestPath) { - let a = bestPath.address; - if (typeof a === 'string') { - let a2 = a.split('/'); - if (a2.length === 2) { - let vmaj = peer.versionMajor; - if ((typeof vmaj === 'undefined')||(vmaj === null)) vmaj = -1; - let vmin = peer.versionMinor; - if ((typeof vmin === 'undefined')||(vmin === null)) vmin = -1; - let vrev = peer.versionRev; - if ((typeof vrev === 'undefined')||(vrev === null)) vrev = -1; - let lr = parseInt(bestPath.lastReceive)||0; - - let s = peerStatus[address]; - if ((!s)||(s[8] < lr)) { - peerStatus[address] = [ - ztAddress, - now, - vmaj, - vmin, - vrev, - id, - parseInt(a2[1])||0, - parseFloat(bestPath.linkQuality)||1.0, - lr, - a2[0] - ]; - } - ++count; - } - } - } - } - } - - console.log(name+': '+count+' peers with active direct paths.'); - return setTimeout(function() { doRootUpdate(name,id,ip,port,peersPath,authToken,interval); },interval); - }); -}; - -for(var r in roots) { - var rr = roots[r]; - if (rr.peers) - doRootUpdate(r,rr.id,rr.ip,rr.port,rr.peers,rr.authToken||null,config.interval||60000); -} - -return setTimeout(saveToDb,config.dbSaveInterval||60000); diff --git a/attic/tcp-proxy/Makefile b/attic/tcp-proxy/Makefile deleted file mode 100644 index af4e71e3..00000000 --- a/attic/tcp-proxy/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -CXX=$(shell which clang++ g++ c++ 2>/dev/null | head -n 1) - -all: - $(CXX) -O3 -fno-rtti -o tcp-proxy tcp-proxy.cpp - -clean: - rm -f *.o tcp-proxy *.dSYM diff --git a/attic/tcp-proxy/README.md b/attic/tcp-proxy/README.md deleted file mode 100644 index 6f347d64..00000000 --- a/attic/tcp-proxy/README.md +++ /dev/null @@ -1,4 +0,0 @@ -TCP Proxy Server -====== - -This is the TCP proxy server we run for TCP tunneling from peers behind fascist NATs. Regular users won't have much use for this. diff --git a/attic/tcp-proxy/tcp-proxy.cpp b/attic/tcp-proxy/tcp-proxy.cpp deleted file mode 100644 index a7906aae..00000000 --- a/attic/tcp-proxy/tcp-proxy.cpp +++ /dev/null @@ -1,317 +0,0 @@ -/* - * ZeroTier One - Network Virtualization Everywhere - * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/ - * - * 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 . - */ - -// HACK! Will eventually use epoll() or something in Phy<> instead of select(). -// Also be sure to change ulimit -n and fs.file-max in /etc/sysctl.conf on relays. -#if defined(__linux__) || defined(__LINUX__) || defined(__LINUX) || defined(LINUX) -#include -#include -#undef __FD_SETSIZE -#define __FD_SETSIZE 1048576 -#undef FD_SETSIZE -#define FD_SETSIZE 1048576 -#endif - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "../osdep/Phy.hpp" - -#define ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS 300 -#define ZT_TCP_PROXY_TCP_PORT 443 - -using namespace ZeroTier; - -/* - * ZeroTier TCP Proxy Server - * - * This implements a simple packet encapsulation that is designed to look like - * a TLS connection. It's not a TLS connection, but it sends TLS format record - * headers. It could be extended in the future to implement a fake TLS - * handshake. - * - * At the moment, each packet is just made to look like TLS application data: - * <[1] TLS content type> - currently 0x17 for "application data" - * <[1] TLS major version> - currently 0x03 for TLS 1.2 - * <[1] TLS minor version> - currently 0x03 for TLS 1.2 - * <[2] payload length> - 16-bit length of payload in bytes - * <[...] payload> - Message payload - * - * TCP is inherently inefficient for encapsulating Ethernet, since TCP and TCP - * like protocols over TCP lead to double-ACKs. So this transport is only used - * to enable access when UDP or other datagram protocols are not available. - * - * Clients send a greeting, which is a four-byte message that contains: - * <[1] ZeroTier major version> - * <[1] minor version> - * <[2] revision> - * - * If a client has sent a greeting, it uses the new version of this protocol - * in which every encapsulated ZT packet is prepended by an IP address where - * it should be forwarded (or where it came from for replies). This causes - * this proxy to act as a remote UDP socket similar to a socks proxy, which - * will allow us to move this function off the rootservers and onto dedicated - * proxy nodes. - * - * Older ZT clients that do not send this message get their packets relayed - * to/from 127.0.0.1:9993, which will allow them to talk to and relay via - * the ZT node on the same machine as the proxy. We'll only support this for - * as long as such nodes appear to be in the wild. - */ - -struct TcpProxyService; -struct TcpProxyService -{ - Phy *phy; - int udpPortCounter; - struct Client - { - char tcpReadBuf[131072]; - char tcpWriteBuf[131072]; - unsigned long tcpWritePtr; - unsigned long tcpReadPtr; - PhySocket *tcp; - PhySocket *udp; - time_t lastActivity; - bool newVersion; - }; - std::map< PhySocket *,Client > clients; - - PhySocket *getUnusedUdp(void *uptr) - { - for(int i=0;i<65535;++i) { - ++udpPortCounter; - if (udpPortCounter > 0xfffe) - udpPortCounter = 1024; - struct sockaddr_in laddr; - memset(&laddr,0,sizeof(struct sockaddr_in)); - laddr.sin_family = AF_INET; - laddr.sin_port = htons((uint16_t)udpPortCounter); - PhySocket *udp = phy->udpBind(reinterpret_cast(&laddr),uptr); - if (udp) - return udp; - } - return (PhySocket *)0; - } - - void phyOnDatagram(PhySocket *sock,void **uptr,const struct sockaddr *localAddr,const struct sockaddr *from,void *data,unsigned long len) - { - if (!*uptr) - return; - if ((from->sa_family == AF_INET)&&(len >= 16)&&(len < 2048)) { - Client &c = *((Client *)*uptr); - c.lastActivity = time((time_t *)0); - - unsigned long mlen = len; - if (c.newVersion) - mlen += 7; // new clients get IP info - - if ((c.tcpWritePtr + 5 + mlen) <= sizeof(c.tcpWriteBuf)) { - if (!c.tcpWritePtr) - phy->setNotifyWritable(c.tcp,true); - - c.tcpWriteBuf[c.tcpWritePtr++] = 0x17; // look like TLS data - c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 - c.tcpWriteBuf[c.tcpWritePtr++] = 0x03; // look like TLS 1.2 - - c.tcpWriteBuf[c.tcpWritePtr++] = (char)((mlen >> 8) & 0xff); - c.tcpWriteBuf[c.tcpWritePtr++] = (char)(mlen & 0xff); - - if (c.newVersion) { - c.tcpWriteBuf[c.tcpWritePtr++] = (char)4; // IPv4 - *((uint32_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_addr.s_addr; - c.tcpWritePtr += 4; - *((uint16_t *)(c.tcpWriteBuf + c.tcpWritePtr)) = ((const struct sockaddr_in *)from)->sin_port; - c.tcpWritePtr += 2; - } - - for(unsigned long i=0;i %.16llx\n",inet_ntoa(reinterpret_cast(from)->sin_addr),(int)ntohs(reinterpret_cast(from)->sin_port),(unsigned long long)&c); - } - } - - void phyOnTcpConnect(PhySocket *sock,void **uptr,bool success) - { - // unused, we don't initiate outbound connections - } - - void phyOnTcpAccept(PhySocket *sockL,PhySocket *sockN,void **uptrL,void **uptrN,const struct sockaddr *from) - { - Client &c = clients[sockN]; - PhySocket *udp = getUnusedUdp((void *)&c); - if (!udp) { - phy->close(sockN); - clients.erase(sockN); - //printf("** TCP rejected, no more UDP ports to assign\n"); - return; - } - c.tcpWritePtr = 0; - c.tcpReadPtr = 0; - c.tcp = sockN; - c.udp = udp; - c.lastActivity = time((time_t *)0); - c.newVersion = false; - *uptrN = (void *)&c; - //printf("<< TCP from %s -> %.16llx\n",inet_ntoa(reinterpret_cast(from)->sin_addr),(unsigned long long)&c); - } - - void phyOnTcpClose(PhySocket *sock,void **uptr) - { - if (!*uptr) - return; - Client &c = *((Client *)*uptr); - phy->close(c.udp); - clients.erase(sock); - //printf("** TCP %.16llx closed\n",(unsigned long long)*uptr); - } - - void phyOnTcpData(PhySocket *sock,void **uptr,void *data,unsigned long len) - { - Client &c = *((Client *)*uptr); - c.lastActivity = time((time_t *)0); - - for(unsigned long i=0;i= sizeof(c.tcpReadBuf)) { - phy->close(sock); - return; - } - c.tcpReadBuf[c.tcpReadPtr++] = ((const char *)data)[i]; - - if (c.tcpReadPtr >= 5) { - unsigned long mlen = ( ((((unsigned long)c.tcpReadBuf[3]) & 0xff) << 8) | (((unsigned long)c.tcpReadBuf[4]) & 0xff) ); - if (c.tcpReadPtr >= (mlen + 5)) { - if (mlen == 4) { - // Right now just sending this means the client is 'new enough' for the IP header - c.newVersion = true; - //printf("<< TCP %.16llx HELLO\n",(unsigned long long)*uptr); - } else if (mlen >= 7) { - char *payload = c.tcpReadBuf + 5; - unsigned long payloadLen = mlen; - - struct sockaddr_in dest; - memset(&dest,0,sizeof(dest)); - if (c.newVersion) { - if (*payload == (char)4) { - // New clients tell us where their packets go. - ++payload; - dest.sin_family = AF_INET; - dest.sin_addr.s_addr = *((uint32_t *)payload); - payload += 4; - dest.sin_port = *((uint16_t *)payload); // will be in network byte order already - payload += 2; - payloadLen -= 7; - } - } else { - // For old clients we will just proxy everything to a local ZT instance. The - // fact that this will come from 127.0.0.1 will in turn prevent that instance - // from doing unite() with us. It'll just forward. There will not be many of - // these. - dest.sin_family = AF_INET; - dest.sin_addr.s_addr = htonl(0x7f000001); // 127.0.0.1 - dest.sin_port = htons(9993); - } - - // Note: we do not relay to privileged ports... just an abuse prevention rule. - if ((ntohs(dest.sin_port) > 1024)&&(payloadLen >= 16)) { - phy->udpSend(c.udp,(const struct sockaddr *)&dest,payload,payloadLen); - //printf(">> TCP %.16llx to %s:%d\n",(unsigned long long)*uptr,inet_ntoa(dest.sin_addr),(int)ntohs(dest.sin_port)); - } - } - - memmove(c.tcpReadBuf,c.tcpReadBuf + (mlen + 5),c.tcpReadPtr -= (mlen + 5)); - } - } - } - } - - void phyOnTcpWritable(PhySocket *sock,void **uptr) - { - Client &c = *((Client *)*uptr); - if (c.tcpWritePtr) { - long n = phy->streamSend(sock,c.tcpWriteBuf,c.tcpWritePtr); - if (n > 0) { - memmove(c.tcpWriteBuf,c.tcpWriteBuf + n,c.tcpWritePtr -= (unsigned long)n); - if (!c.tcpWritePtr) - phy->setNotifyWritable(sock,false); - } - } else phy->setNotifyWritable(sock,false); - } - - void doHousekeeping() - { - std::vector toClose; - time_t now = time((time_t *)0); - for(std::map< PhySocket *,Client >::iterator c(clients.begin());c!=clients.end();++c) { - if ((now - c->second.lastActivity) >= ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS) { - toClose.push_back(c->first); - toClose.push_back(c->second.udp); - } - } - for(std::vector::iterator s(toClose.begin());s!=toClose.end();++s) - phy->close(*s); - } -}; - -int main(int argc,char **argv) -{ - signal(SIGPIPE,SIG_IGN); - signal(SIGHUP,SIG_IGN); - srand(time((time_t *)0)); - - TcpProxyService svc; - Phy phy(&svc,false,true); - svc.phy = &phy; - svc.udpPortCounter = 1023; - - { - struct sockaddr_in laddr; - memset(&laddr,0,sizeof(laddr)); - laddr.sin_family = AF_INET; - laddr.sin_port = htons(ZT_TCP_PROXY_TCP_PORT); - if (!phy.tcpListen((const struct sockaddr *)&laddr)) { - fprintf(stderr,"%s: fatal error: unable to bind TCP port %d\n",argv[0],ZT_TCP_PROXY_TCP_PORT); - return 1; - } - } - - time_t lastDidHousekeeping = time((time_t *)0); - for(;;) { - phy.poll(120000); - time_t now = time((time_t *)0); - if ((now - lastDidHousekeeping) > 120) { - lastDidHousekeeping = now; - svc.doHousekeeping(); - } - } - - return 0; -} -- cgit v1.2.3 From 4e79804cd3750e9a2b030bc8613171ec82805553 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 10 Jan 2018 16:56:39 -0800 Subject: cleanup --- attic/big-http-test/2015-11-10_01_50000.out.xz | Bin 730360 -> 0 bytes attic/big-http-test/2015-11-10_02_50000.out.xz | Bin 2373664 -> 0 bytes .../2015-11-10_03_12500_ec2-east-only.out.xz | Bin 802932 -> 0 bytes attic/big-http-test/Dockerfile | 24 --- attic/big-http-test/README.md | 12 -- attic/big-http-test/agent.js | 196 --------------------- attic/big-http-test/big-test-kill.sh | 9 - attic/big-http-test/big-test-start.sh | 13 -- attic/big-http-test/crunch-results.js | 65 ------- attic/big-http-test/docker-main.sh | 16 -- attic/big-http-test/nodesource-el.repo | 6 - attic/big-http-test/package.json | 16 -- attic/big-http-test/server.js | 53 ------ 13 files changed, 410 deletions(-) delete mode 100644 attic/big-http-test/2015-11-10_01_50000.out.xz delete mode 100644 attic/big-http-test/2015-11-10_02_50000.out.xz delete mode 100644 attic/big-http-test/2015-11-10_03_12500_ec2-east-only.out.xz delete mode 100644 attic/big-http-test/Dockerfile delete mode 100644 attic/big-http-test/README.md delete mode 100644 attic/big-http-test/agent.js delete mode 100755 attic/big-http-test/big-test-kill.sh delete mode 100755 attic/big-http-test/big-test-start.sh delete mode 100644 attic/big-http-test/crunch-results.js delete mode 100755 attic/big-http-test/docker-main.sh delete mode 100644 attic/big-http-test/nodesource-el.repo delete mode 100644 attic/big-http-test/package.json delete mode 100644 attic/big-http-test/server.js (limited to 'attic') diff --git a/attic/big-http-test/2015-11-10_01_50000.out.xz b/attic/big-http-test/2015-11-10_01_50000.out.xz deleted file mode 100644 index d3e2a666..00000000 Binary files a/attic/big-http-test/2015-11-10_01_50000.out.xz and /dev/null differ diff --git a/attic/big-http-test/2015-11-10_02_50000.out.xz b/attic/big-http-test/2015-11-10_02_50000.out.xz deleted file mode 100644 index 0154da79..00000000 Binary files a/attic/big-http-test/2015-11-10_02_50000.out.xz and /dev/null differ diff --git a/attic/big-http-test/2015-11-10_03_12500_ec2-east-only.out.xz b/attic/big-http-test/2015-11-10_03_12500_ec2-east-only.out.xz deleted file mode 100644 index 3ae3555e..00000000 Binary files a/attic/big-http-test/2015-11-10_03_12500_ec2-east-only.out.xz and /dev/null differ diff --git a/attic/big-http-test/Dockerfile b/attic/big-http-test/Dockerfile deleted file mode 100644 index e19b3fee..00000000 --- a/attic/big-http-test/Dockerfile +++ /dev/null @@ -1,24 +0,0 @@ -FROM centos:latest - -MAINTAINER https://www.zerotier.com/ - -EXPOSE 9993/udp - -ADD nodesource-el.repo /etc/yum.repos.d/nodesource-el.repo -RUN yum -y update && yum install -y nodejs && yum clean all - -RUN mkdir -p /var/lib/zerotier-one -RUN mkdir -p /var/lib/zerotier-one/networks.d -RUN touch /var/lib/zerotier-one/networks.d/ffffffffffffffff.conf - -ADD package.json / -RUN npm install - -ADD zerotier-one / -RUN chmod a+x /zerotier-one - -ADD agent.js / -ADD docker-main.sh / -RUN chmod a+x /docker-main.sh - -CMD ["./docker-main.sh"] diff --git a/attic/big-http-test/README.md b/attic/big-http-test/README.md deleted file mode 100644 index 23a95605..00000000 --- a/attic/big-http-test/README.md +++ /dev/null @@ -1,12 +0,0 @@ -HTTP one-to-all test -====== - -*This is really internal use code. You're free to test it out but expect to do some editing/tweaking to make it work. We used this to run some massive scale tests of our new geo-cluster-based root server infrastructure prior to taking it live.* - -Before using this code you will want to edit agent.js to change SERVER_HOST to the IP address of where you will run server.js. This should typically be an open Internet IP, since this makes reporting not dependent upon the thing being tested. Also note that this thing does no security of any kind. It's designed for one-off tests run over a short period of time, not to be anything that runs permanently. You will also want to edit the Dockerfile if you want to build containers and change the network ID to the network you want to run tests over. - -This code can be deployed across a large number of VMs or containers to test and benchmark HTTP traffic within a virtual network at scale. The agent acts as a server and can query other agents, while the server collects agent data and tells agents about each other. It's designed to use RFC4193-based ZeroTier IPv6 addresses within the cluster, which allows the easy provisioning of a large cluster without IP conflicts. - -The Dockerfile builds an image that launches the agent. The image must be "docker run" with "--device=/dev/net/tun --privileged" to permit it to open a tun/tap device within the container. (Unfortunately CAP_NET_ADMIN may not work due to a bug in Docker and/or Linux.) You can run a bunch with a command like: - - for ((n=0;n<10;n++)); do docker run --device=/dev/net/tun --privileged -d zerotier/http-test; done diff --git a/attic/big-http-test/agent.js b/attic/big-http-test/agent.js deleted file mode 100644 index 9ab2e019..00000000 --- a/attic/big-http-test/agent.js +++ /dev/null @@ -1,196 +0,0 @@ -// ZeroTier distributed HTTP test agent - -// --------------------------------------------------------------------------- -// Customizable parameters: - -// Time between startup and first test attempt -var TEST_STARTUP_LAG = 10000; - -// Maximum interval between test attempts (actual timing is random % this) -var TEST_INTERVAL_MAX = (60000 * 10); - -// Test timeout in ms -var TEST_TIMEOUT = 30000; - -// Where should I get other agents' IDs and POST results? -var SERVER_HOST = '52.26.196.147'; -var SERVER_PORT = 18080; - -// Which port do agents use to serve up test data to each other? -var AGENT_PORT = 18888; - -// Payload size in bytes -var PAYLOAD_SIZE = 5000; - -// --------------------------------------------------------------------------- - -var ipaddr = require('ipaddr.js'); -var os = require('os'); -var http = require('http'); -var async = require('async'); - -var express = require('express'); -var app = express(); - -// Find our ZeroTier-assigned RFC4193 IPv6 address -var thisAgentId = null; -var interfaces = os.networkInterfaces(); -if (!interfaces) { - console.error('FATAL: os.networkInterfaces() failed.'); - process.exit(1); -} -for(var ifname in interfaces) { - var ifaddrs = interfaces[ifname]; - if (Array.isArray(ifaddrs)) { - for(var i=0;i 1) { - - var target = agents[Math.floor(Math.random() * agents.length)]; - while (target === thisAgentId) - target = agents[Math.floor(Math.random() * agents.length)]; - - var testRequest = null; - var timeoutId = null; - timeoutId = setTimeout(function() { - if (testRequest !== null) - testRequest.abort(); - timeoutId = null; - },TEST_TIMEOUT); - var startTime = Date.now(); - - testRequest = http.get({ - host: agentIdToIp(target), - port: AGENT_PORT, - path: '/' - },function(res) { - var bytes = 0; - res.on('data',function(chunk) { bytes += chunk.length; }); - res.on('end',function() { - lastTestResult = { - source: thisAgentId, - target: target, - time: (Date.now() - startTime), - bytes: bytes, - timedOut: (timeoutId === null), - error: null - }; - if (timeoutId !== null) - clearTimeout(timeoutId); - return setTimeout(doTest,Math.round(Math.random() * TEST_INTERVAL_MAX) + 1); - }); - }).on('error',function(e) { - lastTestResult = { - source: thisAgentId, - target: target, - time: (Date.now() - startTime), - bytes: 0, - timedOut: (timeoutId === null), - error: e.toString() - }; - if (timeoutId !== null) - clearTimeout(timeoutId); - return setTimeout(doTest,Math.round(Math.random() * TEST_INTERVAL_MAX) + 1); - }); - - } else { - return setTimeout(doTest,1000); - } - - }); - }).on('error',function(e) { - console.log('POST failed: '+e.toString()); - return setTimeout(doTest,1000); - }); - if (lastTestResult !== null) { - submit.write(JSON.stringify(lastTestResult)); - lastTestResult = null; - } - submit.end(); -}; - -// Agents just serve up a test payload -app.get('/',function(req,res) { return res.status(200).send(payload); }); - -var expressServer = app.listen(AGENT_PORT,function () { - // Start timeout-based loop - setTimeout(doTest(),TEST_STARTUP_LAG); -}); diff --git a/attic/big-http-test/big-test-kill.sh b/attic/big-http-test/big-test-kill.sh deleted file mode 100755 index fa7f3cc4..00000000 --- a/attic/big-http-test/big-test-kill.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -# Kills all running Docker containers on all big-test-hosts - -export PATH=/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin - -pssh -h big-test-hosts -x '-t -t' -i -OUserKnownHostsFile=/dev/null -OStrictHostKeyChecking=no -t 0 -p 256 "sudo docker ps -aq | xargs -r sudo docker rm -f" - -exit 0 diff --git a/attic/big-http-test/big-test-start.sh b/attic/big-http-test/big-test-start.sh deleted file mode 100755 index 2411eeda..00000000 --- a/attic/big-http-test/big-test-start.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -# More than 500 container seems to result in a lot of sporadic failures, probably due to Linux kernel scaling issues with virtual network ports -# 250 with a 16GB RAM VM like Amazon m4.xlarge seems good -NUM_CONTAINERS=250 -CONTAINER_IMAGE=zerotier/http-test -SCALE_UP_DELAY=10 - -export PATH=/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin - -pssh -h big-test-hosts -x '-t -t' -i -OUserKnownHostsFile=/dev/null -OStrictHostKeyChecking=no -t 0 -p 256 "sudo sysctl -w net.netfilter.nf_conntrack_max=262144 ; for ((n=0;n<$NUM_CONTAINERS;n++)); do sudo docker run --device=/dev/net/tun --privileged -d $CONTAINER_IMAGE; sleep $SCALE_UP_DELAY; done" - -exit 0 diff --git a/attic/big-http-test/crunch-results.js b/attic/big-http-test/crunch-results.js deleted file mode 100644 index 50e5c49a..00000000 --- a/attic/big-http-test/crunch-results.js +++ /dev/null @@ -1,65 +0,0 @@ -// -// Pipe the output of server.js into this to convert raw test results into bracketed statistics -// suitable for graphing. -// - -// Time duration per statistical bracket -var BRACKET_SIZE = 10000; - -// Number of bytes expected from each test -var EXPECTED_BYTES = 5000; - -var readline = require('readline'); -var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout, - terminal: false -}); - -var count = 0.0; -var overallCount = 0.0; -var totalFailures = 0.0; -var totalOverallFailures = 0.0; -var totalMs = 0; -var totalData = 0; -var devices = {}; -var lastBracketTs = 0; - -rl.on('line',function(line) { - line = line.trim(); - var ls = line.split(','); - if (ls.length == 7) { - var ts = parseInt(ls[0]); - var fromId = ls[1]; - var toId = ls[2]; - var ms = parseFloat(ls[3]); - var bytes = parseInt(ls[4]); - var timedOut = (ls[5] == 'true') ? true : false; - var errMsg = ls[6]; - - count += 1.0; - overallCount += 1.0; - if ((bytes !== EXPECTED_BYTES)||(timedOut)) { - totalFailures += 1.0; - totalOverallFailures += 1.0; - } - totalMs += ms; - totalData += bytes; - - devices[fromId] = true; - devices[toId] = true; - - if (lastBracketTs === 0) - lastBracketTs = ts; - - if (((ts - lastBracketTs) >= BRACKET_SIZE)&&(count > 0.0)) { - console.log(count.toString()+','+overallCount.toString()+','+(totalMs / count)+','+(totalFailures / count)+','+(totalOverallFailures / overallCount)+','+totalData+','+Object.keys(devices).length); - - count = 0.0; - totalFailures = 0.0; - totalMs = 0; - totalData = 0; - lastBracketTs = ts; - } - } // else ignore junk -}); diff --git a/attic/big-http-test/docker-main.sh b/attic/big-http-test/docker-main.sh deleted file mode 100755 index 29cdced9..00000000 --- a/attic/big-http-test/docker-main.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/bin/bash - -export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin - -/zerotier-one -d >>zerotier-one.out 2>&1 - -# Wait for ZeroTier to start and join the network -while [ ! -d "/proc/sys/net/ipv6/conf/zt0" ]; do - sleep 0.25 -done - -# Wait just a bit longer for stuff to settle -sleep 5 - -exec node --harmony /agent.js >>agent.out 2>&1 -#exec node --harmony /agent.js diff --git a/attic/big-http-test/nodesource-el.repo b/attic/big-http-test/nodesource-el.repo deleted file mode 100644 index b785d3d0..00000000 --- a/attic/big-http-test/nodesource-el.repo +++ /dev/null @@ -1,6 +0,0 @@ -[nodesource] -name=Node.js Packages for Enterprise Linux 7 - $basearch -baseurl=https://rpm.nodesource.com/pub_4.x/el/7/$basearch -failovermethod=priority -enabled=1 -gpgcheck=0 diff --git a/attic/big-http-test/package.json b/attic/big-http-test/package.json deleted file mode 100644 index 173a6f99..00000000 --- a/attic/big-http-test/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "zerotier-test-http", - "version": "1.0.0", - "description": "ZeroTier in-network HTTP test", - "main": "agent.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "ZeroTier, Inc.", - "license": "GPL-3.0", - "dependencies": { - "async": "^1.5.0", - "express": "^4.13.3", - "ipaddr.js": "^1.0.3" - } -} diff --git a/attic/big-http-test/server.js b/attic/big-http-test/server.js deleted file mode 100644 index 629784da..00000000 --- a/attic/big-http-test/server.js +++ /dev/null @@ -1,53 +0,0 @@ -// ZeroTier distributed HTTP test coordinator and result-reporting server - -// --------------------------------------------------------------------------- -// Customizable parameters: - -var SERVER_PORT = 18080; - -// --------------------------------------------------------------------------- - -var fs = require('fs'); - -var express = require('express'); -var app = express(); - -app.use(function(req,res,next) { - req.rawBody = ''; - req.on('data', function(chunk) { req.rawBody += chunk.toString(); }); - req.on('end', function() { return next(); }); -}); - -var knownAgents = {}; - -app.post('/:agentId',function(req,res) { - var agentId = req.params.agentId; - if ((!agentId)||(agentId.length !== 32)) - return res.status(404).send(''); - - if (req.rawBody) { - var receiveTime = Date.now(); - var resultData = null; - try { - resultData = JSON.parse(req.rawBody); - console.log(Date.now().toString()+','+resultData.source+','+resultData.target+','+resultData.time+','+resultData.bytes+','+resultData.timedOut+',"'+((resultData.error) ? resultData.error : '')+'"'); - } catch (e) {} - } - - knownAgents[agentId] = true; - var thisUpdate = []; - var agents = Object.keys(knownAgents); - if (agents.length < 100) - thisUpdate = agents; - else { - for(var xx=0;xx<100;++xx) - thisUpdate.push(agents[Math.floor(Math.random() * agents.length)]); - } - - return res.status(200).send(JSON.stringify(thisUpdate)); -}); - -var expressServer = app.listen(SERVER_PORT,function () { - console.log('LISTENING ON '+SERVER_PORT); - console.log(''); -}); -- cgit v1.2.3 From f821db29f34d040d59b6118164bf3c7242959a0e Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 24 Jan 2018 17:12:53 -0500 Subject: . --- attic/FCV.hpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ node/NetworkConfig.hpp | 4 +- 2 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 attic/FCV.hpp (limited to 'attic') diff --git a/attic/FCV.hpp b/attic/FCV.hpp new file mode 100644 index 00000000..0fb9e250 --- /dev/null +++ b/attic/FCV.hpp @@ -0,0 +1,101 @@ +/* + * ZeroTier One - Network Virtualization Everywhere + * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/ + * + * 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 . + * + * -- + * + * You can be released from the requirements of the license by purchasing + * a commercial license. Buying such a license is mandatory as soon as you + * develop commercial closed-source software that incorporates or links + * directly against ZeroTier software without disclosing the source code + * of your own application. + */ + +#include "Constants.hpp" + +namespace ZeroTier { + +/** + * A really simple fixed capacity vector + * + * This class does no bounds checking, so the user must ensure that + * no more than C elements are ever added and that accesses are in + * bounds. + * + * @tparam T Type to contain + * @tparam C Capacity of vector + */ +template +class FCV +{ +public: + FCV() : _s(0) {} + ~FCV() { clear(); } + + FCV(const FCV &v) : + _s(v._s) + { + for(unsigned long i=0;i<_s;++i) { + new (reinterpret_cast(_mem + (sizeof(T) * i))) T(reinterpret_cast(v._mem)[i]); + } + } + + inline FCV &operator=(const FCV &v) + { + clear(); + _s = v._s; + for(unsigned long i=0;i<_s;++i) { + new (reinterpret_cast(_mem + (sizeof(T) * i))) T(reinterpret_cast(v._mem)[i]); + } + return *this; + } + + typedef T * iterator; + typedef const T * const_iterator; + typedef unsigned long size_type; + + inline iterator begin() { return (T *)_mem; } + inline iterator end() { return (T *)(_mem + (sizeof(T) * _s)); } + inline iterator begin() const { return (const T *)_mem; } + inline iterator end() const { return (const T *)(_mem + (sizeof(T) * _s)); } + + inline T &operator[](const size_type i) { return reinterpret_cast(_mem)[i]; } + inline const T &operator[](const size_type i) const { return reinterpret_cast(_mem)[i]; } + + inline T &front() { return reinterpret_cast(_mem)[0]; } + inline const T &front() const { return reinterpret_cast(_mem)[0]; } + inline T &back() { return reinterpret_cast(_mem)[_s - 1]; } + inline const T &back() const { return reinterpret_cast(_mem)[_s - 1]; } + + inline void push_back(const T &v) { new (reinterpret_cast(_mem + (sizeof(T) * _s++))) T(v); } + inline void pop_back() { reinterpret_cast(_mem + (sizeof(T) * --_s))->~T(); } + + inline size_type size() const { return _s; } + inline size_type capacity() const { return C; } + + inline void clear() + { + for(unsigned long i=0;i<_s;++i) + reinterpret_cast(_mem + (sizeof(T) * i))->~T(); + _s = 0; + } + +private: + char _mem[sizeof(T) * C]; + unsigned long _s; +}; + +} // namespace ZeroTier diff --git a/node/NetworkConfig.hpp b/node/NetworkConfig.hpp index 576db676..3a2664a2 100644 --- a/node/NetworkConfig.hpp +++ b/node/NetworkConfig.hpp @@ -305,13 +305,13 @@ public: } /** - * @return ZeroTier addresses of anchors that are also multicast replicators + * @return ZeroTier addresses of multicast replicators */ inline std::vector
multicastReplicators() const { std::vector
r; for(unsigned int i=0;i