From 1bec8ddce4e7e144d3a27d69275e6c352bc962d9 Mon Sep 17 00:00:00 2001
From: Grant Limberg <grant.limberg@zerotier.com>
Date: Tue, 13 Feb 2018 16:41:21 -0800
Subject: [PATCH] Better error messages when the ZeroTier service can't be
 contacted

Fixes #681
---
 one.cpp | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/one.cpp b/one.cpp
index 727044de4..e5884557e 100644
--- a/one.cpp
+++ b/one.cpp
@@ -299,7 +299,7 @@ static int cli(int argc,char **argv)
 			responseHeaders,
 			responseBody);
 		if (scode == 200) {
-			printf("%s",cliFixJsonCRs(responseBody).c_str());
+			printf("%s", cliFixJsonCRs(responseBody).c_str());
 			return 0;
 		} else {
 			printf("%u %s %s" ZT_EOL_S,scode,command.c_str(),responseBody.c_str());
@@ -308,6 +308,11 @@ static int cli(int argc,char **argv)
 	} else if ((command == "info")||(command == "status")) {
 		const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/status",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);
@@ -338,6 +343,11 @@ static int cli(int argc,char **argv)
 	} else if (command == "listpeers") {
 		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);
@@ -400,6 +410,11 @@ static int cli(int argc,char **argv)
 	} else if (command == "listnetworks") {
 		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);
@@ -504,6 +519,11 @@ static int cli(int argc,char **argv)
 	} else if (command == "listmoons") {
 		const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/moon",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);
@@ -619,6 +639,11 @@ static int cli(int argc,char **argv)
 		requestHeaders["Content-Length"] = cl;
 		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);