From e2ca065f28bc3f42f18ba1f0085752cde20b02aa Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 5 Jun 2020 16:36:31 -0700 Subject: [PATCH] Cleanup, revise join command --- cmd/zerotier/cli/help.go | 13 +++++---- cmd/zerotier/cli/join.go | 55 ++++++++++++++++++++++-------------- cmd/zerotier/cli/leave.go | 1 - cmd/zerotier/cli/networks.go | 1 - cmd/zerotier/cli/peers.go | 1 - cmd/zerotier/cli/root.go | 4 +++ cmd/zerotier/cli/service.go | 1 - cmd/zerotier/cli/set.go | 1 - cmd/zerotier/cli/status.go | 1 - core/Mutex.hpp | 24 ++++++++-------- 10 files changed, 57 insertions(+), 45 deletions(-) diff --git a/cmd/zerotier/cli/help.go b/cmd/zerotier/cli/help.go index 9982612cc..fc3bf97a8 100644 --- a/cmd/zerotier/cli/help.go +++ b/cmd/zerotier/cli/help.go @@ -34,11 +34,11 @@ Global Options: Commands: help Show this help version Print version - service Start as service + service Start a node (see below) status Show node status and configuration - join [option] Join a virtual network - auth Join authorization token - fingerprint Full controller identity fingerprint + join [-options] Join a virtual network + -a Join authorization token + -c Controller identity or fingerprint leave Leave a virtual network networks List VL2 virtual networks network [command] [option] - Network management commands @@ -82,8 +82,6 @@ Commands: sign Sign a file with an identity's key verify Verify a signature -The 'service' command does not exit until the service receives a signal. - An
may be specified as a 10-digit short ZeroTier address, a fingerprint containing both an address and a SHA384 hash, or an identity. The latter two options are equivalent in terms of specificity and may be @@ -93,5 +91,8 @@ full identities and may be specified either verbatim or as a path to a file. An is a place where a peer may be reached. Currently these are just 'IP/port' format addresses but other types may be added in the future. + +The 'service' command starts a node. It will run until the node receives +an exit signal and is normally not used directly. `,zerotier.CoreVersionMajor, zerotier.CoreVersionMinor, zerotier.CoreVersionRevision) } diff --git a/cmd/zerotier/cli/join.go b/cmd/zerotier/cli/join.go index e3af47f0b..54b4c7bbb 100644 --- a/cmd/zerotier/cli/join.go +++ b/cmd/zerotier/cli/join.go @@ -14,6 +14,7 @@ package cli import ( + "flag" "fmt" "os" "strconv" @@ -22,17 +23,47 @@ import ( "zerotier/pkg/zerotier" ) -// Join CLI command func Join(basePath, authToken string, args []string) { - if len(args) < 1 || len(args) > 2 { + joinOpts := flag.NewFlagSet("join", flag.ContinueOnError) + controllerAuthToken := joinOpts.String("a", "", "") + controllerFingerprint := joinOpts.String("c", "", "") + err := joinOpts.Parse(os.Args[1:]) + if err != nil { Help() os.Exit(1) + return + } + args = joinOpts.Args() + if len(args) < 1 { + Help() + os.Exit(1) + return } - if len(args[0]) != zerotier.NetworkIDStringLength { fmt.Printf("ERROR: invalid network ID: %s\n", args[0]) os.Exit(1) } + + _ = *controllerAuthToken // TODO: not implemented yet + + var fp *zerotier.Fingerprint + if len(*controllerFingerprint) > 0 { + if strings.ContainsRune(*controllerFingerprint, '-') { + fp, err = zerotier.NewFingerprintFromString(*controllerFingerprint) + if err != nil { + fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", *controllerFingerprint) + os.Exit(1) + } + } else { + id, err := zerotier.NewIdentityFromString(*controllerFingerprint) + if err != nil { + fmt.Printf("ERROR: invalid network controller identity: %s\n", *controllerFingerprint) + os.Exit(1) + } + fp = id.Fingerprint() + } + } + nwid, err := strconv.ParseUint(args[0], 16, 64) if err != nil { fmt.Printf("ERROR: invalid network ID: %s\n", args[0]) @@ -40,24 +71,6 @@ func Join(basePath, authToken string, args []string) { } nwids := fmt.Sprintf("%.16x", nwid) - var fp *zerotier.Fingerprint - if len(args) == 2 { - if strings.ContainsRune(args[1], '-') { - fp, err = zerotier.NewFingerprintFromString(args[1]) - if err != nil { - fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", args[1]) - os.Exit(1) - } - } else { - id, err := zerotier.NewIdentityFromString(args[1]) - if err != nil { - fmt.Printf("ERROR: invalid network controller identity: %s\n", args[1]) - os.Exit(1) - } - fp = id.Fingerprint() - } - } - var network zerotier.APINetwork network.ID = zerotier.NetworkID(nwid) network.ControllerFingerprint = fp diff --git a/cmd/zerotier/cli/leave.go b/cmd/zerotier/cli/leave.go index f561a78bf..0cabb34da 100644 --- a/cmd/zerotier/cli/leave.go +++ b/cmd/zerotier/cli/leave.go @@ -20,7 +20,6 @@ import ( "zerotier/pkg/zerotier" ) -// Leave CLI command func Leave(basePath, authToken string, args []string) { if len(args) != 1 { Help() diff --git a/cmd/zerotier/cli/networks.go b/cmd/zerotier/cli/networks.go index 473f26ff5..ed2d29233 100644 --- a/cmd/zerotier/cli/networks.go +++ b/cmd/zerotier/cli/networks.go @@ -20,7 +20,6 @@ import ( "zerotier/pkg/zerotier" ) -// Networks CLI command func Networks(basePath, authToken string, args []string, jsonOutput bool) { var networks []zerotier.APINetwork apiGet(basePath, authToken, "/network", &networks) diff --git a/cmd/zerotier/cli/peers.go b/cmd/zerotier/cli/peers.go index bdccf3d25..7d8d8071e 100644 --- a/cmd/zerotier/cli/peers.go +++ b/cmd/zerotier/cli/peers.go @@ -21,7 +21,6 @@ import ( "zerotier/pkg/zerotier" ) -// Peers CLI command (also used for 'roots' command with rootsOnly set to true) func Peers(basePath, authToken string, args []string, jsonOutput bool, rootsOnly bool) { var peers []zerotier.Peer apiGet(basePath, authToken, "/peer", &peers) diff --git a/cmd/zerotier/cli/root.go b/cmd/zerotier/cli/root.go index 887709826..53dba84e4 100644 --- a/cmd/zerotier/cli/root.go +++ b/cmd/zerotier/cli/root.go @@ -21,6 +21,10 @@ func Root(basePath, authToken string, args []string, jsonOutput bool) { case "remove": + case "subscribe": + + case "unsubscribe": + } } } diff --git a/cmd/zerotier/cli/service.go b/cmd/zerotier/cli/service.go index f8e413685..345c7f512 100644 --- a/cmd/zerotier/cli/service.go +++ b/cmd/zerotier/cli/service.go @@ -22,7 +22,6 @@ import ( "zerotier/pkg/zerotier" ) -// Service is "zerotier service ..." func Service(basePath, authToken string, args []string) { if len(args) > 0 { Help() diff --git a/cmd/zerotier/cli/set.go b/cmd/zerotier/cli/set.go index 41c3cf39e..245ce5705 100644 --- a/cmd/zerotier/cli/set.go +++ b/cmd/zerotier/cli/set.go @@ -13,6 +13,5 @@ package cli -// Set CLI command func Set(basePath, authToken string, args []string) { } diff --git a/cmd/zerotier/cli/status.go b/cmd/zerotier/cli/status.go index 3a0e36157..bf098e2cd 100644 --- a/cmd/zerotier/cli/status.go +++ b/cmd/zerotier/cli/status.go @@ -20,7 +20,6 @@ import ( "zerotier/pkg/zerotier" ) -// Status shows service status info func Status(basePath, authToken string, args []string, jsonOutput bool) { var status zerotier.APIStatus apiGet(basePath, authToken, "/status", &status) diff --git a/core/Mutex.hpp b/core/Mutex.hpp index 9058c0596..4b8ede85a 100644 --- a/core/Mutex.hpp +++ b/core/Mutex.hpp @@ -28,7 +28,7 @@ namespace ZeroTier { class Mutex { public: - ZT_INLINE Mutex() noexcept { pthread_mutex_init(&_mh,nullptr); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + ZT_INLINE Mutex() noexcept { pthread_mutex_init(&_mh,nullptr); } ZT_INLINE ~Mutex() noexcept { pthread_mutex_destroy(&_mh); } ZT_INLINE void lock() const noexcept { pthread_mutex_lock(&((const_cast (this))->_mh)); } @@ -37,15 +37,15 @@ public: class Lock { public: - explicit ZT_INLINE Lock(Mutex &m) noexcept : _m(&m) { m.lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) - explicit ZT_INLINE Lock(const Mutex &m) noexcept : _m(const_cast(&m)) { _m->lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + explicit ZT_INLINE Lock(Mutex &m) noexcept : _m(&m) { m.lock(); } + explicit ZT_INLINE Lock(const Mutex &m) noexcept : _m(const_cast(&m)) { _m->lock(); } ZT_INLINE ~Lock() { _m->unlock(); } private: Mutex *const _m; }; private: - ZT_INLINE Mutex(const Mutex &) noexcept {} // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + ZT_INLINE Mutex(const Mutex &) noexcept {} ZT_INLINE const Mutex &operator=(const Mutex &) noexcept { return *this; } pthread_mutex_t _mh; @@ -54,7 +54,7 @@ private: class RWMutex { public: - ZT_INLINE RWMutex() noexcept { pthread_rwlock_init(&_mh,nullptr); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + ZT_INLINE RWMutex() noexcept { pthread_rwlock_init(&_mh,nullptr); } ZT_INLINE ~RWMutex() noexcept { pthread_rwlock_destroy(&_mh); } ZT_INLINE void lock() const noexcept { pthread_rwlock_wrlock(&((const_cast (this))->_mh)); } @@ -68,8 +68,8 @@ public: class RLock { public: - explicit ZT_INLINE RLock(RWMutex &m) noexcept : _m(&m) { m.rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) - explicit ZT_INLINE RLock(const RWMutex &m) noexcept : _m(const_cast(&m)) { _m->rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + explicit ZT_INLINE RLock(RWMutex &m) noexcept : _m(&m) { m.rlock(); } + explicit ZT_INLINE RLock(const RWMutex &m) noexcept : _m(const_cast(&m)) { _m->rlock(); } ZT_INLINE ~RLock() { _m->runlock(); } private: RWMutex *const _m; @@ -81,8 +81,8 @@ public: class Lock { public: - explicit ZT_INLINE Lock(RWMutex &m) noexcept : _m(&m) { m.lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) - explicit ZT_INLINE Lock(const RWMutex &m) noexcept : _m(const_cast(&m)) { _m->lock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + explicit ZT_INLINE Lock(RWMutex &m) noexcept : _m(&m) { m.lock(); } + explicit ZT_INLINE Lock(const RWMutex &m) noexcept : _m(const_cast(&m)) { _m->lock(); } ZT_INLINE ~Lock() { _m->unlock(); } private: RWMutex *const _m; @@ -97,8 +97,8 @@ public: class RMaybeWLock { public: - explicit ZT_INLINE RMaybeWLock(RWMutex &m) noexcept : _m(&m),_w(false) { m.rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) - explicit ZT_INLINE RMaybeWLock(const RWMutex &m) noexcept : _m(const_cast(&m)),_w(false) { _m->rlock(); } // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + explicit ZT_INLINE RMaybeWLock(RWMutex &m) noexcept : _m(&m),_w(false) { m.rlock(); } + explicit ZT_INLINE RMaybeWLock(const RWMutex &m) noexcept : _m(const_cast(&m)),_w(false) { _m->rlock(); } ZT_INLINE void writing() noexcept { if (!_w) { _w = true; _m->runlock(); _m->lock(); } } ZT_INLINE void reading() noexcept { if (_w) { _w = false; _m->unlock(); _m->rlock(); } } ZT_INLINE ~RMaybeWLock() { if (_w) _m->unlock(); else _m->runlock(); } @@ -108,7 +108,7 @@ public: }; private: - ZT_INLINE RWMutex(const RWMutex &) noexcept {} // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init) + ZT_INLINE RWMutex(const RWMutex &) noexcept {} ZT_INLINE const RWMutex &operator=(const RWMutex &) noexcept { return *this; } pthread_rwlock_t _mh;