Adam Ierymenko 6 years ago
parent
commit
848f822171
7 changed files with 155 additions and 13 deletions
  1. 1 1
      controller/PostgreSQL.cpp
  2. 4 0
      node/Constants.hpp
  3. 8 1
      node/NetworkConfig.hpp
  4. 1 1
      node/Node.cpp
  5. 2 0
      node/RingBuffer.hpp
  6. 3 3
      node/Switch.cpp
  7. 136 7
      one.cpp

+ 1 - 1
controller/PostgreSQL.cpp

@@ -1512,7 +1512,7 @@ void PostgreSQL::onlineNotificationThread()
 		// 	PQclear(res);
 		// }
 
-		std::this_thread::sleep_for(std::chrono::milliseconds(0));
+		std::this_thread::sleep_for(std::chrono::milliseconds(10));
 	}
 	fprintf(stderr, "%s: Fell out of run loop in onlineNotificationThread\n", _myAddressStr.c_str());
 	PQfinish(conn);

+ 4 - 0
node/Constants.hpp

@@ -482,7 +482,11 @@
 /**
  * Timeout for overall peer activity (measured from last receive)
  */
+#ifndef ZT_SDK
 #define ZT_PEER_ACTIVITY_TIMEOUT 500000
+#else
+#define ZT_PEER_ACTIVITY_TIMEOUT 30000
+#endif
 
 /**
  * General rate limit timeout for multiple packet types (HELLO, etc.)

+ 8 - 1
node/NetworkConfig.hpp

@@ -274,7 +274,14 @@ public:
 	/**
 	 * @return True if frames should not be compressed
 	 */
-	inline bool disableCompression() const { return ((this->flags & ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION) != 0); }
+	inline bool disableCompression() const
+	{
+#ifndef ZT_SDK
+		return ((this->flags & ZT_NETWORKCONFIG_FLAG_DISABLE_COMPRESSION) != 0);
+#else
+		return false; // Compression is disabled for SDK builds since it doesn't play nice with lwIP
+#endif
+	}
 
 	/**
 	 * @return Network type is public (no access control)

+ 1 - 1
node/Node.cpp

@@ -340,7 +340,7 @@ ZT_ResultCode Node::processBackgroundTasks(void *tptr,int64_t now,volatile int64
 
 			// Update online status, post status change as event
 			const bool oldOnline = _online;
-			_online = (((now - lastReceivedFromUpstream) < (ZT_PEER_ACTIVITY_TIMEOUT / (ZT_SDK ? 16 : 1)))||(RR->topology->amUpstream()));
+			_online = (((now - lastReceivedFromUpstream) < ZT_PEER_ACTIVITY_TIMEOUT)||(RR->topology->amUpstream()));
 			if (oldOnline != _online)
 				postEvent(tptr,_online ? ZT_EVENT_ONLINE : ZT_EVENT_OFFLINE);
 		} catch ( ... ) {

+ 2 - 0
node/RingBuffer.hpp

@@ -312,6 +312,7 @@ public:
 	/**
 	 * Print the contents of the buffer
 	 */
+	/*
 	inline void dump()
 	{
 		size_t iterator = begin;
@@ -325,6 +326,7 @@ public:
 			}
 		}
 	}
+	*/
 };
 
 } // namespace ZeroTier

+ 3 - 3
node/Switch.cpp

@@ -425,7 +425,7 @@ void Switch::onLocalEthernet(void *tPtr,const SharedPtr<Network> &network,const
 			from.appendTo(outp);
 			outp.append((uint16_t)etherType);
 			outp.append(data,len);
-			if (!network->config().disableCompression() && !ZT_SDK)
+			if (!network->config().disableCompression())
 				outp.compress();
 			aqm_enqueue(tPtr,network,outp,true,qosBucket);
 		} else {
@@ -433,7 +433,7 @@ void Switch::onLocalEthernet(void *tPtr,const SharedPtr<Network> &network,const
 			outp.append(network->id());
 			outp.append((uint16_t)etherType);
 			outp.append(data,len);
-			if (!network->config().disableCompression() && !ZT_SDK)
+			if (!network->config().disableCompression())
 				outp.compress();
 			aqm_enqueue(tPtr,network,outp,true,qosBucket);
 		}
@@ -490,7 +490,7 @@ void Switch::onLocalEthernet(void *tPtr,const SharedPtr<Network> &network,const
 				from.appendTo(outp);
 				outp.append((uint16_t)etherType);
 				outp.append(data,len);
-				if (!network->config().disableCompression() && !ZT_SDK)
+				if (!network->config().disableCompression())
 					outp.compress();
 				aqm_enqueue(tPtr,network,outp,true,qosBucket);
 			} else {

+ 136 - 7
one.cpp

@@ -69,6 +69,7 @@
 #include <stdexcept>
 #include <iostream>
 #include <sstream>
+#include <algorithm>
 
 #include "version.h"
 #include "include/ZeroTierOne.h"
@@ -129,6 +130,7 @@ static void cliPrintHelp(const char *pn,FILE *out)
 	fprintf(out,ZT_EOL_S"Available commands:" ZT_EOL_S);
 	fprintf(out,"  info                    - Display status info" ZT_EOL_S);
 	fprintf(out,"  listpeers               - List all peers" ZT_EOL_S);
+	fprintf(out,"  peers                   - List all peers (prettier)" ZT_EOL_S);
 	fprintf(out,"  listnetworks            - List all networks" ZT_EOL_S);
 	fprintf(out,"  join <network>          - Join a network" ZT_EOL_S);
 	fprintf(out,"  leave <network>         - Leave a network" ZT_EOL_S);
@@ -137,6 +139,12 @@ static void cliPrintHelp(const char *pn,FILE *out)
 	fprintf(out,"  listmoons               - List moons (federated root sets)" ZT_EOL_S);
 	fprintf(out,"  orbit <world ID> <seed> - Join a moon via any member root" ZT_EOL_S);
 	fprintf(out,"  deorbit <world ID>      - Leave a moon" ZT_EOL_S);
+	fprintf(out,ZT_EOL_S"Available settings:" ZT_EOL_S);
+	fprintf(out,"  Settings to use with [get/set] may include property names from " ZT_EOL_S);
+	fprintf(out,"  the JSON output of \"zerotier-cli -j listnetworks\". Additionally, " ZT_EOL_S);
+	fprintf(out,"  (ip, ip4, ip6, ip6plane, and ip6prefix can be used). For instance:" ZT_EOL_S);
+	fprintf(out,"  zerotier-cli get <nwid> ip6plane will return the 6PLANE address" ZT_EOL_S);
+	fprintf(out,"  assigned to this node." ZT_EOL_S);
 }
 
 static std::string cliFixJsonCRs(const std::string &s)
@@ -409,6 +417,73 @@ static int cli(int argc,char **argv)
 			printf("%u %s %s" ZT_EOL_S,scode,command.c_str(),responseBody.c_str());
 			return 1;
 		}
+	} else if (command == "peers") {
+		const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/peer",requestHeaders,responseHeaders,responseBody);
+
+		if (scode == 0) {
+			printf("Error connecting to the ZeroTier service: %s\n\nPlease check that the service is running and that TCP port 9993 can be contacted via 127.0.0.1." ZT_EOL_S, responseBody.c_str());
+			return 1;
+		}
+
+		nlohmann::json j;
+		try {
+			j = OSUtils::jsonParse(responseBody);
+		} catch (std::exception &exc) {
+			printf("%u %s invalid JSON response (%s)" ZT_EOL_S,scode,command.c_str(),exc.what());
+			return 1;
+		} catch ( ... ) {
+			printf("%u %s invalid JSON response (unknown exception)" ZT_EOL_S,scode,command.c_str());
+			return 1;
+		}
+
+		if (scode == 200) {
+			if (json) {
+				printf("%s" ZT_EOL_S,OSUtils::jsonDump(j).c_str());
+			} else {
+				printf("200 peers\n<ztaddr>   <ver>  <role> <lat> <link> <lastTX> <lastRX> <path>" ZT_EOL_S);
+				if (j.is_array()) {
+					for(unsigned long k=0;k<j.size();++k) {
+						nlohmann::json &p = j[k];
+						std::string bestPath;
+						nlohmann::json &paths = p["paths"];
+						if (paths.is_array()) {
+							for(unsigned long i=0;i<paths.size();++i) {
+								nlohmann::json &path = paths[i];
+								if (path["preferred"]) {
+									char tmp[256];
+									std::string addr = path["address"];
+									const int64_t now = OSUtils::now();
+									OSUtils::ztsnprintf(tmp,sizeof(tmp),"%-8lld %-8lld %s",now - (int64_t)path["lastSend"],now - (int64_t)path["lastReceive"],addr.c_str());
+									bestPath = std::string("DIRECT ") + tmp;
+									break;
+								}
+							}
+						}
+						if (bestPath.length() == 0) bestPath = "RELAY";
+						char ver[128];
+						int64_t vmaj = p["versionMajor"];
+						int64_t vmin = p["versionMinor"];
+						int64_t vrev = p["versionRev"];
+						if (vmaj >= 0) {
+							OSUtils::ztsnprintf(ver,sizeof(ver),"%lld.%lld.%lld",vmaj,vmin,vrev);
+						} else {
+							ver[0] = '-';
+							ver[1] = (char)0;
+						}
+						printf("%s %-6s %-6s %5d %s" ZT_EOL_S,
+							OSUtils::jsonString(p["address"],"-").c_str(),
+							ver,
+							OSUtils::jsonString(p["role"],"-").c_str(),
+							(int)OSUtils::jsonInt(p["latency"],0),
+							bestPath.c_str());
+					}
+				}
+			}
+			return 0;
+		} else {
+			printf("%u %s %s" ZT_EOL_S,scode,command.c_str(),responseBody.c_str());
+			return 1;
+		}
 	} else if (command == "listnetworks") {
 		const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/network",requestHeaders,responseHeaders,responseBody);
 
@@ -468,7 +543,7 @@ static int cli(int argc,char **argv)
 		}
 	} else if (command == "join") {
 		if (arg1.length() != 16) {
-                        printf("invalid network id" ZT_EOL_S);
+			printf("invalid network id" ZT_EOL_S);
 			return 2;
 		}
 		requestHeaders["Content-Type"] = "application/json";
@@ -496,7 +571,7 @@ static int cli(int argc,char **argv)
 		}
 	} else if (command == "leave") {
 		if (arg1.length() != 16) {
-                        printf("invalid network id" ZT_EOL_S);
+			printf("invalid network id" ZT_EOL_S);
 			return 2;
 		}
 		unsigned int scode = Http::DEL(
@@ -594,7 +669,11 @@ static int cli(int argc,char **argv)
 		}
 	} else if (command == "set") {
 		if (arg1.length() != 16) {
-                        printf("invalid network id" ZT_EOL_S);
+			fprintf(stderr,"invalid format: must be a 16-digit (network) ID\n");
+			return 2;
+		}
+		if (!arg2.length()) {
+			fprintf(stderr,"invalid format: include a property name to set\n");
 			return 2;
 		}
 		std::size_t eqidx = arg2.find('=');
@@ -632,16 +711,18 @@ static int cli(int argc,char **argv)
 		}
 	} else if (command == "get") {
 		if (arg1.length() != 16) {
-			fprintf(stderr,"invalid network ID format, must be a 16-digit hexidecimal number\n");
+			fprintf(stderr,"invalid format: must be a 16-digit (network) ID\n");
+			return 2;
+		}
+		if (!arg2.length()) {
+			fprintf(stderr,"invalid format: include a property name to get\n");
 			return 2;
 		}
 		const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/network",requestHeaders,responseHeaders,responseBody);
-
 		if (scode == 0) {
 			printf("Error connecting to the ZeroTier service: %s\n\nPlease check that the service is running and that TCP port 9993 can be contacted via 127.0.0.1." ZT_EOL_S, responseBody.c_str());
 			return 1;
 		}
-
 		nlohmann::json j;
 		try {
 			j = OSUtils::jsonParse(responseBody);
@@ -658,8 +739,56 @@ static int cli(int argc,char **argv)
 				nlohmann::json &n = j[i];
 				if (n.is_object()) {
 					if (n["id"] == arg1) {
-						printf("%s\n", OSUtils::jsonString(n[arg2],"-").c_str());
 						bNetworkFound = true;
+						std::string aa;
+						if (arg2 != "ip" && arg2 != "ip4" && arg2 != "ip6" && arg2 != "ip6plane" && arg2 != "ip6prefix") {
+							aa.append(OSUtils::jsonString(n[arg2],"-")); // Standard network property field
+							if (aa == "-") {
+								printf("error, unknown property name\n");
+								break;
+							}
+							printf("%s\n",aa.c_str());
+							break;
+						}
+						nlohmann::json &assignedAddresses = n["assignedAddresses"];
+						if (assignedAddresses.is_array()) {
+							int matchingIdxs[ZT_MAX_ZT_ASSIGNED_ADDRESSES];
+							int addressCountOfType = 0;
+							for (int k = 0; k<std::min(ZT_MAX_ZT_ASSIGNED_ADDRESSES, (int)assignedAddresses.size());++k) {
+								nlohmann::json &addr = assignedAddresses[k];
+								if ((arg2 == "ip4" && addr.get<std::string>().find(".") != std::string::npos)
+									|| ((arg2.find("ip6") == 0) && addr.get<std::string>().find(":") != std::string::npos)
+									|| (arg2 == "ip")
+									) {
+									matchingIdxs[addressCountOfType++] = k;
+								}
+							}
+							for (int k=0; k<addressCountOfType; k++) {
+								nlohmann::json &addr = assignedAddresses[matchingIdxs[k]];
+								if (!addr.is_string()) {
+									continue;
+								}
+								if (arg2.find("ip6p") == 0) {
+									if (arg2 == "ip6plane") {
+										if (addr.get<std::string>().find("fc") == 0) {
+											aa.append(addr.get<std::string>().substr(0,addr.get<std::string>().find("/")));
+											if (k < addressCountOfType-1) aa.append("\n");
+										}
+									}
+									if (arg2 == "ip6prefix") {
+										if (addr.get<std::string>().find("fc") == 0) {
+											aa.append(addr.get<std::string>().substr(0,addr.get<std::string>().find("/")).substr(0,24));
+											if (k < addressCountOfType-1) aa.append("\n");
+										}
+									}
+								}
+								else {
+									aa.append(addr.get<std::string>().substr(0,addr.get<std::string>().find("/")));
+									if (k < addressCountOfType-1) aa.append("\n");
+								}
+							}
+						}
+						printf("%s\n",aa.c_str());
 					}
 				}
 			}