From 7ec433a45248dac9bbf5faebfa7cbf6f055ae279 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 8 Nov 2013 15:23:48 -0500 Subject: Incorporate CLI functionality into core binary with binary name aliasing to save space in updater/installer. --- main.cpp | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index b0834531..dce3f8c2 100644 --- a/main.cpp +++ b/main.cpp @@ -53,6 +53,7 @@ #include "node/Defaults.hpp" #include "node/Utils.hpp" #include "node/Node.hpp" +#include "node/Condition.hpp" using namespace ZeroTier; @@ -67,8 +68,131 @@ static void printHelp(const char *cn,FILE *out) fprintf(out," -h - Display this help"ZT_EOL_S); fprintf(out," -p - Bind to this port for network I/O"ZT_EOL_S); fprintf(out," -c - Bind to this port for local control packets"ZT_EOL_S); + fprintf(out," -q - Send a query to a running service (zerotier-cli)"); } +namespace ZeroTierCLI { // ---------------------------------------------------- + +static void printHelp(FILE *out,const char *exename) +{ + fprintf(out,"Usage: %s [-switches] "ZT_EOL_S,exename); + fprintf(out,ZT_EOL_S); + fprintf(out,"Available switches:"ZT_EOL_S); + fprintf(out," -c - Communicate with daemon over this local port"ZT_EOL_S); + fprintf(out," -t - Specify token on command line"ZT_EOL_S); + fprintf(out," -T - Read token from file"ZT_EOL_S); + fprintf(out,ZT_EOL_S); + fprintf(out,"Use the 'help' command to get help from ZeroTier One itself."ZT_EOL_S); +} + +static volatile unsigned int numResults = 0; +static Condition doneCondition; + +static void resultHandler(void *arg,unsigned long id,const char *line) +{ + ++numResults; + if (strlen(line)) + fprintf(stdout,"%s"ZT_EOL_S,line); + else doneCondition.signal(); +} + +// Runs instead of rest of main() if process is called zerotier-cli or if +// -q is specified as an option. +#ifdef __WINDOWS__ +static int main(int argc,_TCHAR* argv[]) +#else +static int main(int argc,char **argv) +#endif +{ + if (argc <= 1) { + printHelp(stdout,argv[0]); + return -1; + } + + std::string authToken; + std::string command; + bool pastSwitches = false; + unsigned int controlPort = 0; + for(int i=1;i