main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <time.h>
  31. #include <errno.h>
  32. #include <string>
  33. #include <stdexcept>
  34. #include "node/Constants.hpp"
  35. #ifdef __WINDOWS__
  36. #include <WinSock2.h>
  37. #include <Windows.h>
  38. #include <tchar.h>
  39. #include <wchar.h>
  40. #else
  41. #include <unistd.h>
  42. #include <pwd.h>
  43. #include <sys/types.h>
  44. #include <sys/stat.h>
  45. #include <signal.h>
  46. #endif
  47. #include "node/Constants.hpp"
  48. #include "node/Defaults.hpp"
  49. #include "node/Utils.hpp"
  50. #include "node/Node.hpp"
  51. #include "node/Condition.hpp"
  52. using namespace ZeroTier;
  53. static Node *node = (Node *)0;
  54. static void printHelp(const char *cn,FILE *out)
  55. {
  56. fprintf(out,"ZeroTier One version %d.%d.%d"ZT_EOL_S"(c)2012-2013 ZeroTier Networks LLC"ZT_EOL_S,Node::versionMajor(),Node::versionMinor(),Node::versionRevision());
  57. fprintf(out,"Licensed under the GNU General Public License v3"ZT_EOL_S""ZT_EOL_S);
  58. fprintf(out,"Usage: %s [-switches] [home directory]"ZT_EOL_S""ZT_EOL_S,cn);
  59. fprintf(out,"Available switches:"ZT_EOL_S);
  60. fprintf(out," -h - Display this help"ZT_EOL_S);
  61. fprintf(out," -p<port> - Bind to this port for network I/O"ZT_EOL_S);
  62. fprintf(out," -c<port> - Bind to this port for local control packets"ZT_EOL_S);
  63. fprintf(out," -q - Send a query to a running service (zerotier-cli)");
  64. }
  65. namespace ZeroTierCLI { // ----------------------------------------------------
  66. static void printHelp(FILE *out,const char *exename)
  67. {
  68. fprintf(out,"Usage: %s [-switches] <command>"ZT_EOL_S,exename);
  69. fprintf(out,ZT_EOL_S);
  70. fprintf(out,"Available switches:"ZT_EOL_S);
  71. fprintf(out," -c<port> - Communicate with daemon over this local port"ZT_EOL_S);
  72. fprintf(out," -t<token> - Specify token on command line"ZT_EOL_S);
  73. fprintf(out," -T<file> - Read token from file"ZT_EOL_S);
  74. fprintf(out,ZT_EOL_S);
  75. fprintf(out,"Use the 'help' command to get help from ZeroTier One itself."ZT_EOL_S);
  76. }
  77. static volatile unsigned int numResults = 0;
  78. static Condition doneCondition;
  79. static void resultHandler(void *arg,unsigned long id,const char *line)
  80. {
  81. ++numResults;
  82. if (strlen(line))
  83. fprintf(stdout,"%s"ZT_EOL_S,line);
  84. else doneCondition.signal();
  85. }
  86. // Runs instead of rest of main() if process is called zerotier-cli or if
  87. // -q is specified as an option.
  88. #ifdef __WINDOWS__
  89. static int main(int argc,_TCHAR* argv[])
  90. #else
  91. static int main(int argc,char **argv)
  92. #endif
  93. {
  94. if (argc <= 1) {
  95. printHelp(stdout,argv[0]);
  96. return -1;
  97. }
  98. std::string authToken;
  99. std::string command;
  100. bool pastSwitches = false;
  101. unsigned int controlPort = 0;
  102. for(int i=1;i<argc;++i) {
  103. if ((argv[i][0] == '-')&&(!pastSwitches)) {
  104. if (strlen(argv[i]) <= 1) {
  105. printHelp(stdout,argv[0]);
  106. return -1;
  107. }
  108. switch(argv[i][1]) {
  109. case 'q': // does nothing, for invocation without binary path name aliasing
  110. if (argv[i][2]) {
  111. printHelp(argv[0],stderr);
  112. return 0;
  113. }
  114. break;
  115. case 'c':
  116. controlPort = Utils::strToUInt(argv[i] + 2);
  117. break;
  118. case 't':
  119. authToken.assign(argv[i] + 2);
  120. break;
  121. case 'T':
  122. if (!Utils::readFile(argv[i] + 2,authToken)) {
  123. fprintf(stdout,"FATAL ERROR: unable to read token from '%s'"ZT_EOL_S,argv[i] + 2);
  124. return -2;
  125. }
  126. break;
  127. default:
  128. return -1;
  129. }
  130. } else {
  131. pastSwitches = true;
  132. if (command.length())
  133. command.push_back(' ');
  134. command.append(argv[i]);
  135. }
  136. }
  137. if (!authToken.length()) {
  138. const char *home = getenv("HOME");
  139. if (home) {
  140. std::string dotZeroTierAuthToken(home);
  141. dotZeroTierAuthToken.push_back(ZT_PATH_SEPARATOR);
  142. dotZeroTierAuthToken.append(".zerotierOneAuthToken");
  143. if (!Utils::readFile(dotZeroTierAuthToken.c_str(),authToken)) {
  144. #ifndef __WINDOWS__
  145. #ifdef __APPLE__
  146. const char *systemAuthTokenPath = "/Library/Application Support/ZeroTier/One/authtoken.secret";
  147. #else
  148. const char *systemAuthTokenPath = "/var/lib/zerotier-one/authtoken.secret";
  149. #endif
  150. if (!Utils::readFile(systemAuthTokenPath,authToken)) {
  151. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s' or '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str(),systemAuthTokenPath);
  152. return -2;
  153. }
  154. #else // __WINDOWS__
  155. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str());
  156. return -2;
  157. #endif // __WINDOWS__
  158. }
  159. }
  160. }
  161. if (!authToken.length()) {
  162. fprintf(stdout,"FATAL ERROR: could not find auth token"ZT_EOL_S);
  163. return -2;
  164. }
  165. Node::LocalClient client(authToken.c_str(),controlPort,&resultHandler,(void *)0);
  166. client.send(command.c_str());
  167. doneCondition.wait(1000);
  168. if (!numResults) {
  169. fprintf(stdout,"ERROR: no results received. Is ZeroTier One running?"ZT_EOL_S);
  170. return -1;
  171. }
  172. return 0;
  173. }
  174. } // namespace ZeroTierCLI ----------------------------------------------------
  175. #ifdef __UNIX_LIKE__
  176. static void sighandlerQuit(int sig)
  177. {
  178. Node *n = node;
  179. if (n)
  180. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  181. else exit(0);
  182. }
  183. #endif
  184. #ifdef __WINDOWS__
  185. static BOOL WINAPI _handlerRoutine(DWORD dwCtrlType)
  186. {
  187. switch(dwCtrlType) {
  188. case CTRL_C_EVENT:
  189. case CTRL_BREAK_EVENT:
  190. case CTRL_CLOSE_EVENT:
  191. case CTRL_SHUTDOWN_EVENT:
  192. Node *n = node;
  193. if (n)
  194. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  195. return TRUE;
  196. }
  197. return FALSE;
  198. }
  199. #endif
  200. #ifdef __WINDOWS__
  201. int _tmain(int argc, _TCHAR* argv[])
  202. #else
  203. int main(int argc,char **argv)
  204. #endif
  205. {
  206. #ifdef __UNIX_LIKE__
  207. signal(SIGHUP,SIG_IGN);
  208. signal(SIGPIPE,SIG_IGN);
  209. signal(SIGUSR1,SIG_IGN);
  210. signal(SIGUSR2,SIG_IGN);
  211. signal(SIGALRM,SIG_IGN);
  212. signal(SIGINT,&sighandlerQuit);
  213. signal(SIGTERM,&sighandlerQuit);
  214. signal(SIGQUIT,&sighandlerQuit);
  215. #endif
  216. #ifdef __WINDOWS__
  217. WSADATA wsaData;
  218. WSAStartup(MAKEWORD(2,2),&wsaData);
  219. SetConsoleCtrlHandler(&_handlerRoutine,TRUE);
  220. #endif
  221. if ((strstr(argv[0],"zerotier-cli"))||(strstr(argv[0],"ZEROTIER-CLI")))
  222. return ZeroTierCLI::main(argc,argv);
  223. const char *homeDir = (const char *)0;
  224. unsigned int port = 0;
  225. unsigned int controlPort = 0;
  226. for(int i=1;i<argc;++i) {
  227. if (argv[i][0] == '-') {
  228. switch(argv[i][1]) {
  229. case 'p':
  230. port = Utils::strToUInt(argv[i] + 2);
  231. if (port > 65535) {
  232. printHelp(argv[0],stderr);
  233. return -1;
  234. }
  235. break;
  236. case 'c':
  237. controlPort = Utils::strToUInt(argv[i] + 2);
  238. if (controlPort > 65535) {
  239. printHelp(argv[0],stderr);
  240. return -1;
  241. }
  242. break;
  243. case 'q':
  244. if (argv[i][2]) {
  245. printHelp(argv[0],stderr);
  246. return 0;
  247. } else return ZeroTierCLI::main(argc,argv);
  248. case 'h':
  249. case '?':
  250. default:
  251. printHelp(argv[0],stderr);
  252. return 0;
  253. }
  254. } else {
  255. if (homeDir) {
  256. printHelp(argv[0],stderr);
  257. return 0;
  258. }
  259. homeDir = argv[i];
  260. break;
  261. }
  262. }
  263. if ((!homeDir)||(strlen(homeDir) == 0))
  264. homeDir = ZT_DEFAULTS.defaultHomePath.c_str();
  265. #ifdef __UNIX_LIKE__
  266. mkdir(homeDir,0755); // will fail if it already exists
  267. {
  268. char pidpath[4096];
  269. Utils::snprintf(pidpath,sizeof(pidpath),"%s/zerotier-one.pid",homeDir);
  270. FILE *pf = fopen(pidpath,"w");
  271. if (pf) {
  272. fprintf(pf,"%ld",(long)getpid());
  273. fclose(pf);
  274. }
  275. }
  276. #endif
  277. int exitCode = 0;
  278. try {
  279. node = new Node(homeDir,port,controlPort);
  280. const char *termReason = (char *)0;
  281. switch(node->run()) {
  282. case Node::NODE_UNRECOVERABLE_ERROR:
  283. exitCode = -1;
  284. termReason = node->reasonForTermination();
  285. fprintf(stderr,"%s: abnormal termination: %s\n",argv[0],(termReason) ? termReason : "(unknown reason)");
  286. break;
  287. default:
  288. break;
  289. }
  290. delete node;
  291. node = (Node *)0;
  292. } catch ( ... ) {}
  293. #ifdef __UNIX_LIKE__
  294. {
  295. char pidpath[4096];
  296. Utils::snprintf(pidpath,sizeof(pidpath),"%s/zerotier-one.pid",homeDir);
  297. Utils::rm(pidpath);
  298. }
  299. #endif
  300. return exitCode;
  301. }