main.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <iostream>
  35. #ifdef _WIN32
  36. #include <Windows.h>
  37. #else
  38. #include <unistd.h>
  39. #include <pwd.h>
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <signal.h>
  43. #endif
  44. #include "node/Node.hpp"
  45. #include "node/Utils.hpp"
  46. #include "node/Defaults.hpp"
  47. #include "launcher.h"
  48. using namespace ZeroTier;
  49. static Node *node = (Node *)0;
  50. static void printHelp(const char *cn,FILE *out)
  51. {
  52. fprintf(out,"ZeroTier One version %d.%d.%d\n(c)2012-2013 ZeroTier Networks LLC\nLicensed under the GNU General Public License v3\n\nUsage: %s <home directory>\n",Node::versionMajor(),Node::versionMinor(),Node::versionRevision(),cn);
  53. }
  54. #ifndef _WIN32
  55. static void sighandlerQuit(int sig)
  56. {
  57. Node *n = node;
  58. if (n)
  59. n->terminate();
  60. else exit(0);
  61. }
  62. static void sighandlerUsr(int sig)
  63. {
  64. }
  65. static void sighandlerHup(int sig)
  66. {
  67. Node *n = node;
  68. if (n)
  69. n->updateStatusNow();
  70. }
  71. #endif
  72. int main(int argc,char **argv)
  73. {
  74. #ifndef _WIN32
  75. signal(SIGHUP,&sighandlerHup);
  76. signal(SIGPIPE,SIG_IGN);
  77. signal(SIGUSR1,&sighandlerUsr);
  78. signal(SIGUSR2,&sighandlerUsr);
  79. signal(SIGALRM,SIG_IGN);
  80. signal(SIGINT,&sighandlerQuit);
  81. signal(SIGTERM,&sighandlerQuit);
  82. signal(SIGQUIT,&sighandlerQuit);
  83. #endif
  84. if (argc < 2) {
  85. printHelp(argv[0],stderr);
  86. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  87. }
  88. const char *homeDir = (const char *)0;
  89. for(int i=1;i<argc;++i) {
  90. if (argv[i][0] == '-') {
  91. switch(argv[i][1]) {
  92. default:
  93. printHelp(argv[0],stderr);
  94. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  95. }
  96. } else {
  97. if (homeDir) {
  98. printHelp(argv[0],stderr);
  99. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  100. }
  101. homeDir = argv[i];
  102. break;
  103. }
  104. }
  105. if ((!homeDir)||(strlen(homeDir) <= 0)) {
  106. printHelp(argv[0],stderr);
  107. return ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  108. }
  109. #ifndef _WIN32
  110. mkdir(homeDir,0755); // will fail if it already exists
  111. #endif
  112. int exitCode = ZT_EXEC_RETURN_VALUE_NORMAL_TERMINATION;
  113. node = new Node(homeDir,ZT_DEFAULTS.configUrlPrefix.c_str(),ZT_DEFAULTS.configAuthority.c_str());
  114. switch(node->run()) {
  115. case Node::NODE_RESTART_FOR_RECONFIGURATION:
  116. exitCode = ZT_EXEC_RETURN_VALUE_PLEASE_RESTART;
  117. break;
  118. case Node::NODE_UNRECOVERABLE_ERROR:
  119. exitCode = ZT_EXEC_RETURN_VALUE_UNRECOVERABLE_ERROR;
  120. break;
  121. case Node::NODE_NEW_VERSION_AVAILABLE:
  122. exitCode = ZT_EXEC_RETURN_VALUE_TERMINATED_FOR_UPGRADE;
  123. break;
  124. default:
  125. break;
  126. }
  127. delete node;
  128. node = (Node *)0;
  129. return exitCode;
  130. }