main.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. using namespace ZeroTier;
  52. static Node *node = (Node *)0;
  53. static void printHelp(const char *cn,FILE *out)
  54. {
  55. 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());
  56. fprintf(out,"Licensed under the GNU General Public License v3"ZT_EOL_S""ZT_EOL_S);
  57. fprintf(out,"Usage: %s [-switches] [home directory]"ZT_EOL_S""ZT_EOL_S,cn);
  58. fprintf(out,"Available switches:"ZT_EOL_S);
  59. fprintf(out," -h - Display this help"ZT_EOL_S);
  60. fprintf(out," -p<port> - Bind to this port for network I/O"ZT_EOL_S);
  61. fprintf(out," -c<port> - Bind to this port for local control packets"ZT_EOL_S);
  62. }
  63. #ifdef __UNIX_LIKE__
  64. static void sighandlerQuit(int sig)
  65. {
  66. Node *n = node;
  67. if (n)
  68. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  69. else exit(0);
  70. }
  71. #endif
  72. #ifdef __WINDOWS__
  73. static BOOL WINAPI _handlerRoutine(DWORD dwCtrlType)
  74. {
  75. switch(dwCtrlType) {
  76. case CTRL_C_EVENT:
  77. case CTRL_BREAK_EVENT:
  78. case CTRL_CLOSE_EVENT:
  79. case CTRL_SHUTDOWN_EVENT:
  80. Node *n = node;
  81. if (n)
  82. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  83. return TRUE;
  84. }
  85. return FALSE;
  86. }
  87. #endif
  88. #ifdef __WINDOWS__
  89. int _tmain(int argc, _TCHAR* argv[])
  90. #else
  91. int main(int argc,char **argv)
  92. #endif
  93. {
  94. #ifdef __UNIX_LIKE__
  95. signal(SIGHUP,SIG_IGN);
  96. signal(SIGPIPE,SIG_IGN);
  97. signal(SIGUSR1,SIG_IGN);
  98. signal(SIGUSR2,SIG_IGN);
  99. signal(SIGALRM,SIG_IGN);
  100. signal(SIGINT,&sighandlerQuit);
  101. signal(SIGTERM,&sighandlerQuit);
  102. signal(SIGQUIT,&sighandlerQuit);
  103. #endif
  104. #ifdef __WINDOWS__
  105. WSADATA wsaData;
  106. WSAStartup(MAKEWORD(2,2),&wsaData);
  107. SetConsoleCtrlHandler(&_handlerRoutine,TRUE);
  108. #endif
  109. const char *homeDir = (const char *)0;
  110. unsigned int port = 0;
  111. unsigned int controlPort = 0;
  112. for(int i=1;i<argc;++i) {
  113. if (argv[i][0] == '-') {
  114. switch(argv[i][1]) {
  115. case 'p':
  116. port = Utils::strToUInt(argv[i] + 2);
  117. if (port > 65535) {
  118. printHelp(argv[0],stderr);
  119. return -1;
  120. }
  121. break;
  122. case 'c':
  123. controlPort = Utils::strToUInt(argv[i] + 2);
  124. if (controlPort > 65535) {
  125. printHelp(argv[0],stderr);
  126. return -1;
  127. }
  128. break;
  129. case 'h':
  130. case '?':
  131. default:
  132. printHelp(argv[0],stderr);
  133. return 0;
  134. }
  135. } else {
  136. if (homeDir) {
  137. printHelp(argv[0],stderr);
  138. return 0;
  139. }
  140. homeDir = argv[i];
  141. break;
  142. }
  143. }
  144. if ((!homeDir)||(strlen(homeDir) == 0))
  145. homeDir = ZT_DEFAULTS.defaultHomePath.c_str();
  146. #ifdef __UNIX_LIKE__
  147. mkdir(homeDir,0755); // will fail if it already exists
  148. #endif
  149. int exitCode = 0;
  150. node = new Node(homeDir,port,controlPort);
  151. const char *termReason = (char *)0;
  152. switch(node->run()) {
  153. case Node::NODE_UNRECOVERABLE_ERROR:
  154. exitCode = -1;
  155. termReason = node->reasonForTermination();
  156. fprintf(stderr,"%s: abnormal termination: %s\n",argv[0],(termReason) ? termReason : "(unknown reason)");
  157. break;
  158. default:
  159. break;
  160. }
  161. delete node;
  162. node = (Node *)0;
  163. return exitCode;
  164. }