main.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 (!command.length()) {
  138. printHelp(stdout,argv[0]);
  139. return -1;
  140. }
  141. if (!authToken.length()) {
  142. const char *home = getenv("HOME");
  143. if (home) {
  144. std::string dotZeroTierAuthToken(home);
  145. dotZeroTierAuthToken.push_back(ZT_PATH_SEPARATOR);
  146. dotZeroTierAuthToken.append(".zerotierOneAuthToken");
  147. if (!Utils::readFile(dotZeroTierAuthToken.c_str(),authToken)) {
  148. #ifndef __WINDOWS__
  149. #ifdef __APPLE__
  150. const char *systemAuthTokenPath = "/Library/Application Support/ZeroTier/One/authtoken.secret";
  151. #else
  152. const char *systemAuthTokenPath = "/var/lib/zerotier-one/authtoken.secret";
  153. #endif
  154. if (!Utils::readFile(systemAuthTokenPath,authToken)) {
  155. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s' or '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str(),systemAuthTokenPath);
  156. return -2;
  157. }
  158. #else // __WINDOWS__
  159. fprintf(stdout,"FATAL ERROR: no token specified on command line and could not read '%s'"ZT_EOL_S,dotZeroTierAuthToken.c_str());
  160. return -2;
  161. #endif // __WINDOWS__
  162. }
  163. }
  164. }
  165. if (!authToken.length()) {
  166. fprintf(stdout,"FATAL ERROR: could not find auth token"ZT_EOL_S);
  167. return -2;
  168. }
  169. Node::LocalClient client(authToken.c_str(),controlPort,&resultHandler,(void *)0);
  170. client.send(command.c_str());
  171. doneCondition.wait(1000);
  172. if (!numResults) {
  173. fprintf(stdout,"ERROR: no results received. Is ZeroTier One running?"ZT_EOL_S);
  174. return -1;
  175. }
  176. return 0;
  177. }
  178. } // namespace ZeroTierCLI ----------------------------------------------------
  179. #ifdef __UNIX_LIKE__
  180. static void sighandlerQuit(int sig)
  181. {
  182. Node *n = node;
  183. if (n)
  184. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  185. else exit(0);
  186. }
  187. #endif
  188. #ifdef __WINDOWS__
  189. static BOOL WINAPI _handlerRoutine(DWORD dwCtrlType)
  190. {
  191. switch(dwCtrlType) {
  192. case CTRL_C_EVENT:
  193. case CTRL_BREAK_EVENT:
  194. case CTRL_CLOSE_EVENT:
  195. case CTRL_SHUTDOWN_EVENT:
  196. Node *n = node;
  197. if (n)
  198. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  199. return TRUE;
  200. }
  201. return FALSE;
  202. }
  203. #endif
  204. #ifdef __WINDOWS__
  205. int _tmain(int argc, _TCHAR* argv[])
  206. #else
  207. int main(int argc,char **argv)
  208. #endif
  209. {
  210. #ifdef __UNIX_LIKE__
  211. signal(SIGHUP,SIG_IGN);
  212. signal(SIGPIPE,SIG_IGN);
  213. signal(SIGUSR1,SIG_IGN);
  214. signal(SIGUSR2,SIG_IGN);
  215. signal(SIGALRM,SIG_IGN);
  216. signal(SIGINT,&sighandlerQuit);
  217. signal(SIGTERM,&sighandlerQuit);
  218. signal(SIGQUIT,&sighandlerQuit);
  219. #endif
  220. #ifdef __WINDOWS__
  221. WSADATA wsaData;
  222. WSAStartup(MAKEWORD(2,2),&wsaData);
  223. SetConsoleCtrlHandler(&_handlerRoutine,TRUE);
  224. #endif
  225. if ((strstr(argv[0],"zerotier-cli"))||(strstr(argv[0],"ZEROTIER-CLI")))
  226. return ZeroTierCLI::main(argc,argv);
  227. const char *homeDir = (const char *)0;
  228. unsigned int port = 0;
  229. unsigned int controlPort = 0;
  230. for(int i=1;i<argc;++i) {
  231. if (argv[i][0] == '-') {
  232. switch(argv[i][1]) {
  233. case 'p':
  234. port = Utils::strToUInt(argv[i] + 2);
  235. if (port > 65535) {
  236. printHelp(argv[0],stderr);
  237. return -1;
  238. }
  239. break;
  240. case 'c':
  241. controlPort = Utils::strToUInt(argv[i] + 2);
  242. if (controlPort > 65535) {
  243. printHelp(argv[0],stderr);
  244. return -1;
  245. }
  246. break;
  247. case 'q':
  248. if (argv[i][2]) {
  249. printHelp(argv[0],stderr);
  250. return 0;
  251. } else return ZeroTierCLI::main(argc,argv);
  252. case 'h':
  253. case '?':
  254. default:
  255. printHelp(argv[0],stderr);
  256. return 0;
  257. }
  258. } else {
  259. if (homeDir) {
  260. printHelp(argv[0],stderr);
  261. return 0;
  262. }
  263. homeDir = argv[i];
  264. break;
  265. }
  266. }
  267. if ((!homeDir)||(strlen(homeDir) == 0))
  268. homeDir = ZT_DEFAULTS.defaultHomePath.c_str();
  269. #ifdef __UNIX_LIKE__
  270. mkdir(homeDir,0755); // will fail if it already exists
  271. {
  272. char pidpath[4096];
  273. Utils::snprintf(pidpath,sizeof(pidpath),"%s/zerotier-one.pid",homeDir);
  274. FILE *pf = fopen(pidpath,"w");
  275. if (pf) {
  276. fprintf(pf,"%ld",(long)getpid());
  277. fclose(pf);
  278. }
  279. }
  280. #endif
  281. int exitCode = 0;
  282. try {
  283. node = new Node(homeDir,port,controlPort);
  284. const char *termReason = (char *)0;
  285. switch(node->run()) {
  286. case Node::NODE_UNRECOVERABLE_ERROR:
  287. exitCode = -1;
  288. termReason = node->reasonForTermination();
  289. fprintf(stderr,"%s: abnormal termination: %s\n",argv[0],(termReason) ? termReason : "(unknown reason)");
  290. break;
  291. default:
  292. break;
  293. }
  294. delete node;
  295. node = (Node *)0;
  296. } catch ( ... ) {}
  297. #ifdef __UNIX_LIKE__
  298. {
  299. char pidpath[4096];
  300. Utils::snprintf(pidpath,sizeof(pidpath),"%s/zerotier-one.pid",homeDir);
  301. Utils::rm(pidpath);
  302. }
  303. #endif
  304. return exitCode;
  305. }