Browse Source

Cleanup, revise join command

Adam Ierymenko 5 years ago
parent
commit
e2ca065f28

+ 7 - 6
cmd/zerotier/cli/help.go

@@ -34,11 +34,11 @@ Global Options:
 Commands:
 Commands:
   help                                   Show this help
   help                                   Show this help
   version                                Print version
   version                                Print version
-  service                                Start as service
+  service                                Start a node (see below)
   status                                 Show node status and configuration
   status                                 Show node status and configuration
-  join <network> [option]                Join a virtual network
-    auth <token>                         Join authorization token
-    fingerprint <fingerprint>            Full controller identity fingerprint
+  join [-options] <network>              Join a virtual network
+    -a <token>                           Join authorization token
+    -c <identity|fingerprint>            Controller identity or fingerprint
   leave <network>                        Leave a virtual network
   leave <network>                        Leave a virtual network
   networks                               List VL2 virtual networks
   networks                               List VL2 virtual networks
   network <network> [command] [option] - Network management commands
   network <network> [command] [option] - Network management commands
@@ -82,8 +82,6 @@ Commands:
     sign <identity> <file>               Sign a file with an identity's key
     sign <identity> <file>               Sign a file with an identity's key
     verify <identity> <file> <sig>       Verify a signature
     verify <identity> <file> <sig>       Verify a signature
 
 
-The 'service' command does not exit until the service receives a signal.
-
 An <address> may be specified as a 10-digit short ZeroTier address, a
 An <address> may be specified as a 10-digit short ZeroTier address, a
 fingerprint containing both an address and a SHA384 hash, or an identity.
 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
 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 <endpoint> is a place where a peer may be reached. Currently these are
 An <endpoint> 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.
 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)
 `,zerotier.CoreVersionMajor, zerotier.CoreVersionMinor, zerotier.CoreVersionRevision)
 }
 }

+ 27 - 14
cmd/zerotier/cli/join.go

@@ -14,6 +14,7 @@
 package cli
 package cli
 
 
 import (
 import (
+	"flag"
 	"fmt"
 	"fmt"
 	"os"
 	"os"
 	"strconv"
 	"strconv"
@@ -22,42 +23,54 @@ import (
 	"zerotier/pkg/zerotier"
 	"zerotier/pkg/zerotier"
 )
 )
 
 
-// Join CLI command
 func Join(basePath, authToken string, args []string) {
 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()
 		Help()
 		os.Exit(1)
 		os.Exit(1)
+		return
 	}
 	}
-
-	if len(args[0]) != zerotier.NetworkIDStringLength {
-		fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
+	args = joinOpts.Args()
+	if len(args) < 1 {
+		Help()
 		os.Exit(1)
 		os.Exit(1)
+		return
 	}
 	}
-	nwid, err := strconv.ParseUint(args[0], 16, 64)
-	if err != nil {
+	if len(args[0]) != zerotier.NetworkIDStringLength {
 		fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
 		fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
 		os.Exit(1)
 		os.Exit(1)
 	}
 	}
-	nwids := fmt.Sprintf("%.16x", nwid)
+
+	_ = *controllerAuthToken // TODO: not implemented yet
 
 
 	var fp *zerotier.Fingerprint
 	var fp *zerotier.Fingerprint
-	if len(args) == 2 {
-		if strings.ContainsRune(args[1], '-') {
-			fp, err = zerotier.NewFingerprintFromString(args[1])
+	if len(*controllerFingerprint) > 0 {
+		if strings.ContainsRune(*controllerFingerprint, '-') {
+			fp, err = zerotier.NewFingerprintFromString(*controllerFingerprint)
 			if err != nil {
 			if err != nil {
-				fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", args[1])
+				fmt.Printf("ERROR: invalid network controller fingerprint: %s\n", *controllerFingerprint)
 				os.Exit(1)
 				os.Exit(1)
 			}
 			}
 		} else {
 		} else {
-			id, err := zerotier.NewIdentityFromString(args[1])
+			id, err := zerotier.NewIdentityFromString(*controllerFingerprint)
 			if err != nil {
 			if err != nil {
-				fmt.Printf("ERROR: invalid network controller identity: %s\n", args[1])
+				fmt.Printf("ERROR: invalid network controller identity: %s\n", *controllerFingerprint)
 				os.Exit(1)
 				os.Exit(1)
 			}
 			}
 			fp = id.Fingerprint()
 			fp = id.Fingerprint()
 		}
 		}
 	}
 	}
 
 
+	nwid, err := strconv.ParseUint(args[0], 16, 64)
+	if err != nil {
+		fmt.Printf("ERROR: invalid network ID: %s\n", args[0])
+		os.Exit(1)
+	}
+	nwids := fmt.Sprintf("%.16x", nwid)
+
 	var network zerotier.APINetwork
 	var network zerotier.APINetwork
 	network.ID = zerotier.NetworkID(nwid)
 	network.ID = zerotier.NetworkID(nwid)
 	network.ControllerFingerprint = fp
 	network.ControllerFingerprint = fp

+ 0 - 1
cmd/zerotier/cli/leave.go

@@ -20,7 +20,6 @@ import (
 	"zerotier/pkg/zerotier"
 	"zerotier/pkg/zerotier"
 )
 )
 
 
-// Leave CLI command
 func Leave(basePath, authToken string, args []string) {
 func Leave(basePath, authToken string, args []string) {
 	if len(args) != 1 {
 	if len(args) != 1 {
 		Help()
 		Help()

+ 0 - 1
cmd/zerotier/cli/networks.go

@@ -20,7 +20,6 @@ import (
 	"zerotier/pkg/zerotier"
 	"zerotier/pkg/zerotier"
 )
 )
 
 
-// Networks CLI command
 func Networks(basePath, authToken string, args []string, jsonOutput bool) {
 func Networks(basePath, authToken string, args []string, jsonOutput bool) {
 	var networks []zerotier.APINetwork
 	var networks []zerotier.APINetwork
 	apiGet(basePath, authToken, "/network", &networks)
 	apiGet(basePath, authToken, "/network", &networks)

+ 0 - 1
cmd/zerotier/cli/peers.go

@@ -21,7 +21,6 @@ import (
 	"zerotier/pkg/zerotier"
 	"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) {
 func Peers(basePath, authToken string, args []string, jsonOutput bool, rootsOnly bool) {
 	var peers []zerotier.Peer
 	var peers []zerotier.Peer
 	apiGet(basePath, authToken, "/peer", &peers)
 	apiGet(basePath, authToken, "/peer", &peers)

+ 4 - 0
cmd/zerotier/cli/root.go

@@ -21,6 +21,10 @@ func Root(basePath, authToken string, args []string, jsonOutput bool) {
 
 
 		case "remove":
 		case "remove":
 
 
+		case "subscribe":
+
+		case "unsubscribe":
+
 		}
 		}
 	}
 	}
 }
 }

+ 0 - 1
cmd/zerotier/cli/service.go

@@ -22,7 +22,6 @@ import (
 	"zerotier/pkg/zerotier"
 	"zerotier/pkg/zerotier"
 )
 )
 
 
-// Service is "zerotier service ..."
 func Service(basePath, authToken string, args []string) {
 func Service(basePath, authToken string, args []string) {
 	if len(args) > 0 {
 	if len(args) > 0 {
 		Help()
 		Help()

+ 0 - 1
cmd/zerotier/cli/set.go

@@ -13,6 +13,5 @@
 
 
 package cli
 package cli
 
 
-// Set CLI command
 func Set(basePath, authToken string, args []string) {
 func Set(basePath, authToken string, args []string) {
 }
 }

+ 0 - 1
cmd/zerotier/cli/status.go

@@ -20,7 +20,6 @@ import (
 	"zerotier/pkg/zerotier"
 	"zerotier/pkg/zerotier"
 )
 )
 
 
-// Status shows service status info
 func Status(basePath, authToken string, args []string, jsonOutput bool) {
 func Status(basePath, authToken string, args []string, jsonOutput bool) {
 	var status zerotier.APIStatus
 	var status zerotier.APIStatus
 	apiGet(basePath, authToken, "/status", &status)
 	apiGet(basePath, authToken, "/status", &status)

+ 12 - 12
core/Mutex.hpp

@@ -28,7 +28,7 @@ namespace ZeroTier {
 class Mutex
 class Mutex
 {
 {
 public:
 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 ~Mutex() noexcept { pthread_mutex_destroy(&_mh); }
 
 
 	ZT_INLINE void lock() const noexcept { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
 	ZT_INLINE void lock() const noexcept { pthread_mutex_lock(&((const_cast <Mutex *> (this))->_mh)); }
@@ -37,15 +37,15 @@ public:
 	class Lock
 	class Lock
 	{
 	{
 	public:
 	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<Mutex *>(&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<Mutex *>(&m)) { _m->lock(); }
 		ZT_INLINE ~Lock() { _m->unlock(); }
 		ZT_INLINE ~Lock() { _m->unlock(); }
 	private:
 	private:
 		Mutex *const _m;
 		Mutex *const _m;
 	};
 	};
 
 
 private:
 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; }
 	ZT_INLINE const Mutex &operator=(const Mutex &) noexcept { return *this; }
 
 
 	pthread_mutex_t _mh;
 	pthread_mutex_t _mh;
@@ -54,7 +54,7 @@ private:
 class RWMutex
 class RWMutex
 {
 {
 public:
 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 ~RWMutex() noexcept { pthread_rwlock_destroy(&_mh); }
 
 
 	ZT_INLINE void lock() const noexcept { pthread_rwlock_wrlock(&((const_cast <RWMutex *> (this))->_mh)); }
 	ZT_INLINE void lock() const noexcept { pthread_rwlock_wrlock(&((const_cast <RWMutex *> (this))->_mh)); }
@@ -68,8 +68,8 @@ public:
 	class RLock
 	class RLock
 	{
 	{
 	public:
 	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<RWMutex *>(&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<RWMutex *>(&m)) { _m->rlock(); }
 		ZT_INLINE ~RLock() { _m->runlock(); }
 		ZT_INLINE ~RLock() { _m->runlock(); }
 	private:
 	private:
 		RWMutex *const _m;
 		RWMutex *const _m;
@@ -81,8 +81,8 @@ public:
 	class Lock
 	class Lock
 	{
 	{
 	public:
 	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<RWMutex *>(&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<RWMutex *>(&m)) { _m->lock(); }
 		ZT_INLINE ~Lock() { _m->unlock(); }
 		ZT_INLINE ~Lock() { _m->unlock(); }
 	private:
 	private:
 		RWMutex *const _m;
 		RWMutex *const _m;
@@ -97,8 +97,8 @@ public:
 	class RMaybeWLock
 	class RMaybeWLock
 	{
 	{
 	public:
 	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<RWMutex *>(&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<RWMutex *>(&m)),_w(false) { _m->rlock(); }
 		ZT_INLINE void writing() noexcept { if (!_w) { _w = true; _m->runlock(); _m->lock(); } }
 		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 void reading() noexcept { if (_w) { _w = false; _m->unlock(); _m->rlock(); } }
 		ZT_INLINE ~RMaybeWLock() { if (_w) _m->unlock(); else _m->runlock(); }
 		ZT_INLINE ~RMaybeWLock() { if (_w) _m->unlock(); else _m->runlock(); }
@@ -108,7 +108,7 @@ public:
 	};
 	};
 
 
 private:
 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; }
 	ZT_INLINE const RWMutex &operator=(const RWMutex &) noexcept { return *this; }
 
 
 	pthread_rwlock_t _mh;
 	pthread_rwlock_t _mh;