main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <openssl/rand.h>
  48. #include "node/Constants.hpp"
  49. #include "node/Defaults.hpp"
  50. #include "node/Utils.hpp"
  51. #include "node/Node.hpp"
  52. using namespace ZeroTier;
  53. // ---------------------------------------------------------------------------
  54. // Override libcrypto default RAND_ with Utils::getSecureRandom(), which uses
  55. // a system strong random source. This is because OpenSSL libcrypto's default
  56. // RAND_ implementation uses uninitialized memory as one of its entropy
  57. // sources, which plays havoc with all kinds of debuggers and auditing tools.
  58. static void _zeroTier_rand_cleanup() {}
  59. static void _zeroTier_rand_add(const void *buf, int num, double add_entropy) {}
  60. static int _zeroTier_rand_status() { return 1; }
  61. static void _zeroTier_rand_seed(const void *buf, int num) {}
  62. static int _zeroTier_rand_bytes(unsigned char *buf, int num)
  63. {
  64. Utils::getSecureRandom(buf,num);
  65. return 1;
  66. }
  67. static RAND_METHOD _zeroTierRandMethod = {
  68. _zeroTier_rand_seed,
  69. _zeroTier_rand_bytes,
  70. _zeroTier_rand_cleanup,
  71. _zeroTier_rand_add,
  72. _zeroTier_rand_bytes,
  73. _zeroTier_rand_status
  74. };
  75. static void _initLibCrypto()
  76. {
  77. RAND_set_rand_method(&_zeroTierRandMethod);
  78. }
  79. // ---------------------------------------------------------------------------
  80. static Node *node = (Node *)0;
  81. static void printHelp(const char *cn,FILE *out)
  82. {
  83. fprintf(out,"ZeroTier One version %d.%d.%d"ZT_EOL_S"(c)2012-2013 ZeroTier Networks LLC"ZT_EOL_S"Licensed under the GNU General Public License v3"ZT_EOL_S""ZT_EOL_S"Usage: %s [home directory]"ZT_EOL_S,Node::versionMajor(),Node::versionMinor(),Node::versionRevision(),cn);
  84. }
  85. #ifdef __UNIX_LIKE__
  86. static void sighandlerQuit(int sig)
  87. {
  88. Node *n = node;
  89. if (n)
  90. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  91. else exit(0);
  92. }
  93. #endif
  94. #ifdef __WINDOWS__
  95. static BOOL WINAPI _handlerRoutine(DWORD dwCtrlType)
  96. {
  97. switch(dwCtrlType) {
  98. case CTRL_C_EVENT:
  99. case CTRL_BREAK_EVENT:
  100. case CTRL_CLOSE_EVENT:
  101. case CTRL_SHUTDOWN_EVENT:
  102. Node *n = node;
  103. if (n)
  104. n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal");
  105. return TRUE;
  106. }
  107. return FALSE;
  108. }
  109. #endif
  110. #ifdef __WINDOWS__
  111. int _tmain(int argc, _TCHAR* argv[])
  112. #else
  113. int main(int argc,char **argv)
  114. #endif
  115. {
  116. #ifdef __UNIX_LIKE__
  117. signal(SIGHUP,SIG_IGN);
  118. signal(SIGPIPE,SIG_IGN);
  119. signal(SIGUSR1,SIG_IGN);
  120. signal(SIGUSR2,SIG_IGN);
  121. signal(SIGALRM,SIG_IGN);
  122. signal(SIGINT,&sighandlerQuit);
  123. signal(SIGTERM,&sighandlerQuit);
  124. signal(SIGQUIT,&sighandlerQuit);
  125. #endif
  126. #ifdef __WINDOWS__
  127. WSADATA wsaData;
  128. WSAStartup(MAKEWORD(2,2),&wsaData);
  129. SetConsoleCtrlHandler(&_handlerRoutine,TRUE);
  130. #endif
  131. _initLibCrypto();
  132. const char *homeDir = (const char *)0;
  133. for(int i=1;i<argc;++i) {
  134. if (argv[i][0] == '-') {
  135. switch(argv[i][1]) {
  136. case 'h':
  137. case '?':
  138. default:
  139. printHelp(argv[0],stderr);
  140. return 0;
  141. }
  142. } else {
  143. if (homeDir) {
  144. printHelp(argv[0],stderr);
  145. return 0;
  146. }
  147. homeDir = argv[i];
  148. break;
  149. }
  150. }
  151. if ((!homeDir)||(strlen(homeDir) == 0))
  152. homeDir = ZT_DEFAULTS.defaultHomePath.c_str();
  153. #ifdef __UNIX_LIKE__
  154. mkdir(homeDir,0755); // will fail if it already exists
  155. #endif
  156. int exitCode = 0;
  157. node = new Node(homeDir);
  158. const char *termReason = (char *)0;
  159. switch(node->run()) {
  160. case Node::NODE_UNRECOVERABLE_ERROR:
  161. exitCode = -1;
  162. termReason = node->reasonForTermination();
  163. fprintf(stderr,"%s: abnormal termination: %s\n",argv[0],(termReason) ? termReason : "(unknown reason)");
  164. break;
  165. default:
  166. break;
  167. }
  168. delete node;
  169. node = (Node *)0;
  170. return exitCode;
  171. }