main.cpp 4.9 KB

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