Simplify dump command. Fix bond list command

This commit is contained in:
Joseph Henry 2023-01-20 13:54:04 -08:00
parent 15cfa3bf62
commit b88d7091c8
No known key found for this signature in database
GPG Key ID: C45B33FF5EBC9344

33
one.cpp
View File

@ -517,7 +517,7 @@ static int cli(int argc,char **argv)
} }
/* zerotier-cli bond list */ /* zerotier-cli bond list */
if (arg1 == "list") { if (arg1 == "list") {
const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody); const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/peer",requestHeaders,responseHeaders,responseBody);
if (scode == 0) { 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()); 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; return 1;
@ -543,13 +543,13 @@ static int cli(int argc,char **argv)
nlohmann::json &p = j[k]; nlohmann::json &p = j[k];
bool isBonded = p["isBonded"]; bool isBonded = p["isBonded"];
if (isBonded) { if (isBonded) {
int8_t bondingPolicy = p["bondingPolicy"]; int8_t bondingPolicyCode = p["bondingPolicyCode"];
int8_t numAliveLinks = p["numAliveLinks"]; int8_t numAliveLinks = p["numAliveLinks"];
int8_t numTotalLinks = p["numTotalLinks"]; int8_t numTotalLinks = p["numTotalLinks"];
bFoundBond = true; bFoundBond = true;
std::string policyStr = "none"; std::string policyStr = "none";
if (bondingPolicy >= ZT_BOND_POLICY_NONE && bondingPolicy <= ZT_BOND_POLICY_BALANCE_AWARE) { if (bondingPolicyCode >= ZT_BOND_POLICY_NONE && bondingPolicyCode <= ZT_BOND_POLICY_BALANCE_AWARE) {
policyStr = Bond::getPolicyStrByCode(bondingPolicy); policyStr = Bond::getPolicyStrByCode(bondingPolicyCode);
} }
printf("%10s %32s %d/%d" ZT_EOL_S, printf("%10s %32s %d/%d" ZT_EOL_S,
OSUtils::jsonString(p ["address"],"-").c_str(), OSUtils::jsonString(p ["address"],"-").c_str(),
@ -683,13 +683,8 @@ static int cli(int argc,char **argv)
/* zerotier-cli bond command was malformed in some way */ /* zerotier-cli bond command was malformed in some way */
printf("(bond) command is missing required arguments" ZT_EOL_S); printf("(bond) command is missing required arguments" ZT_EOL_S);
return 2; return 2;
const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",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;
}
} else if (command == "listbonds") { } else if (command == "listbonds") {
const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody); const unsigned int scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/peer",requestHeaders,responseHeaders,responseBody);
if (scode == 0) { 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()); 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());
@ -718,13 +713,13 @@ static int cli(int argc,char **argv)
nlohmann::json &p = j[k]; nlohmann::json &p = j[k];
bool isBonded = p["isBonded"]; bool isBonded = p["isBonded"];
if (isBonded) { if (isBonded) {
int8_t bondingPolicy = p["bondingPolicy"]; int8_t bondingPolicyCode = p["bondingPolicyCode"];
int8_t numAliveLinks = p["numAliveLinks"]; int8_t numAliveLinks = p["numAliveLinks"];
int8_t numTotalLinks = p["numTotalLinks"]; int8_t numTotalLinks = p["numTotalLinks"];
bFoundBond = true; bFoundBond = true;
std::string policyStr = "none"; std::string policyStr = "none";
if (bondingPolicy >= ZT_BOND_POLICY_NONE && bondingPolicy <= ZT_BOND_POLICY_BALANCE_AWARE) { if (bondingPolicyCode >= ZT_BOND_POLICY_NONE && bondingPolicyCode <= ZT_BOND_POLICY_BALANCE_AWARE) {
policyStr = Bond::getPolicyStrByCode(bondingPolicy); policyStr = Bond::getPolicyStrByCode(bondingPolicyCode);
} }
printf("%10s %32s %d/%d" ZT_EOL_S, printf("%10s %32s %d/%d" ZT_EOL_S,
OSUtils::jsonString(p["address"],"-").c_str(), OSUtils::jsonString(p["address"],"-").c_str(),
@ -1121,17 +1116,7 @@ static int cli(int argc,char **argv)
} }
dump << responseBody << ZT_EOL_S; dump << responseBody << ZT_EOL_S;
responseHeaders.clear(); // Bonds don't need to be queried separately since their data originates from "/peer" responses anyway
responseBody = "";
// get bonds
dump << ZT_EOL_S << "bonds" << ZT_EOL_S << "-----" << ZT_EOL_S;
scode = Http::GET(1024 * 1024 * 16,60000,(const struct sockaddr *)&addr,"/bonds",requestHeaders,responseHeaders,responseBody);
if (scode != 200) {
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;
}
dump << responseBody << ZT_EOL_S;
responseHeaders.clear(); responseHeaders.clear();
responseBody = ""; responseBody = "";