2
0

NodeControlService.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 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 <string.h>
  29. #include <stdlib.h>
  30. #include "../node/Constants.hpp"
  31. #include "NodeControlService.hpp"
  32. #include "NodeControlClient.hpp"
  33. #include "../node/Node.hpp"
  34. #include "../node/Utils.hpp"
  35. namespace ZeroTier {
  36. NodeControlService::NodeControlService(Node *node,const char *authToken) :
  37. _node(node),
  38. _listener((IpcListener *)0),
  39. _authToken(authToken),
  40. _running(true),
  41. _thread(Thread::start(this))
  42. {
  43. }
  44. NodeControlService::~NodeControlService()
  45. {
  46. _running = false;
  47. Thread::join(_thread);
  48. {
  49. Mutex::Lock _l(_connections_m);
  50. for(std::map< IpcConnection *,bool >::iterator c(_connections.begin());c!=_connections.end();++c)
  51. delete c->first;
  52. _connections.clear();
  53. }
  54. delete _listener;
  55. }
  56. void NodeControlService::threadMain()
  57. throw()
  58. {
  59. char tmp[1024];
  60. try {
  61. while (_running) {
  62. if (!_node->running()) {
  63. if (_node->started())
  64. break;
  65. } else if ((_node->initialized())&&(_node->address())) {
  66. Utils::snprintf(tmp,sizeof(tmp),"%s%.10llx",ZT_IPC_ENDPOINT_BASE,(unsigned long long)_node->address());
  67. _listener = new IpcListener(tmp,ZT_IPC_TIMEOUT,&_CBcommandHandler,this);
  68. break;
  69. }
  70. Thread::sleep(100); // wait for Node to start
  71. }
  72. } catch ( ... ) {
  73. delete _listener;
  74. _listener = (IpcListener *)0;
  75. }
  76. }
  77. void NodeControlService::_CBcommandHandler(void *arg,IpcConnection *ipcc,IpcConnection::EventType event,const char *commandLine)
  78. {
  79. switch(event) {
  80. case IpcConnection::IPC_EVENT_COMMAND: {
  81. if ((!((NodeControlService *)arg)->_running)||(!commandLine)||(!commandLine[0]))
  82. return;
  83. ((NodeControlService *)arg)->_doCommand(ipcc,commandLine);
  84. } break;
  85. case IpcConnection::IPC_EVENT_NEW_CONNECTION: {
  86. Mutex::Lock _l(((NodeControlService *)arg)->_connections_m);
  87. ((NodeControlService *)arg)->_connections[ipcc] = false; // not yet authenticated
  88. } break;
  89. case IpcConnection::IPC_EVENT_CONNECTION_CLOSED: {
  90. Mutex::Lock _l(((NodeControlService *)arg)->_connections_m);
  91. ((NodeControlService *)arg)->_connections.erase(ipcc);
  92. delete ipcc;
  93. } break;
  94. }
  95. }
  96. void NodeControlService::_doCommand(IpcConnection *ipcc,const char *commandLine)
  97. {
  98. std::vector<std::string> r;
  99. std::vector<std::string> cmd(Utils::split(commandLine,"\r\n \t","\\","'"));
  100. if ((cmd.empty())||(cmd[0] == "help")) {
  101. ipcc->printf("200 help help"ZT_EOL_S);
  102. ipcc->printf("200 help auth <token>"ZT_EOL_S);
  103. ipcc->printf("200 help info"ZT_EOL_S);
  104. ipcc->printf("200 help listpeers"ZT_EOL_S);
  105. ipcc->printf("200 help listnetworks"ZT_EOL_S);
  106. ipcc->printf("200 help join <network ID>"ZT_EOL_S);
  107. ipcc->printf("200 help leave <network ID>"ZT_EOL_S);
  108. ipcc->printf("200 help terminate [<reason>]"ZT_EOL_S);
  109. ipcc->printf("200 help updatecheck"ZT_EOL_S);
  110. } else if (cmd[0] == "auth") {
  111. if ((cmd.size() > 1)&&(_authToken.length() > 0)&&(_authToken == cmd[1])) {
  112. Mutex::Lock _l(_connections_m);
  113. _connections[ipcc] = true;
  114. ipcc->printf("200 auth OK"ZT_EOL_S);
  115. } else ipcc->printf("403 auth failed"ZT_EOL_S);
  116. } else {
  117. {
  118. Mutex::Lock _l(_connections_m);
  119. if (!_connections[ipcc]) {
  120. ipcc->printf("403 %s unauthorized"ZT_EOL_S"."ZT_EOL_S,cmd[0].c_str());
  121. return;
  122. }
  123. }
  124. if (cmd[0] == "info") {
  125. ipcc->printf("200 info %.10llx %s %s"ZT_EOL_S,_node->address(),(_node->online() ? "ONLINE" : "OFFLINE"),Node::versionString());
  126. } else if (cmd[0] == "listpeers") {
  127. ipcc->printf("200 listpeers <ztaddr> <paths> <latency> <version>"ZT_EOL_S);
  128. ZT1_Node_PeerList *pl = _node->listPeers();
  129. if (pl) {
  130. for(unsigned int i=0;i<pl->numPeers;++i) {
  131. ipcc->printf("200 listpeers %.10llx ",(unsigned long long)pl->peers[i].rawAddress);
  132. if (pl->peers[i].numPaths == 0)
  133. ipcc->printf("-");
  134. else {
  135. for(unsigned int j=0;j<pl->peers[i].numPaths;++j) {
  136. if (j > 0)
  137. ipcc->printf(",");
  138. switch(pl->peers[i].paths[j].type) {
  139. default:
  140. ipcc->printf("unknown;");
  141. break;
  142. case ZT1_Node_PhysicalPath::ZT1_Node_PhysicalPath_TYPE_UDP:
  143. ipcc->printf("udp;");
  144. break;
  145. case ZT1_Node_PhysicalPath::ZT1_Node_PhysicalPath_TYPE_TCP_OUT:
  146. ipcc->printf("tcp_out;");
  147. break;
  148. case ZT1_Node_PhysicalPath::ZT1_Node_PhysicalPath_TYPE_TCP_IN:
  149. ipcc->printf("tcp_in;");
  150. break;
  151. case ZT1_Node_PhysicalPath::ZT1_Node_PhysicalPath_TYPE_ETHERNET:
  152. ipcc->printf("eth;");
  153. break;
  154. }
  155. ipcc->printf("%s/%d;%ld;%ld;%ld;%s",
  156. pl->peers[i].paths[j].address.ascii,
  157. (int)pl->peers[i].paths[j].address.port,
  158. pl->peers[i].paths[j].lastSend,
  159. pl->peers[i].paths[j].lastReceive,
  160. pl->peers[i].paths[j].lastPing,
  161. (pl->peers[i].paths[j].fixed ? "fixed" : (pl->peers[i].paths[j].active ? "active" : "inactive")));
  162. }
  163. }
  164. ipcc->printf(" %u %s"ZT_EOL_S,pl->peers[i].latency,(pl->peers[i].remoteVersion[0]) ? pl->peers[i].remoteVersion : "-");
  165. }
  166. _node->freeQueryResult(pl);
  167. }
  168. } else if (cmd[0] == "listnetworks") {
  169. ipcc->printf("200 listnetworks <nwid> <name> <mac> <status> <config age> <type> <dev> <ips>"ZT_EOL_S);
  170. ZT1_Node_NetworkList *nl = _node->listNetworks();
  171. if (nl) {
  172. for(unsigned int i=0;i<nl->numNetworks;++i) {
  173. ipcc->printf("200 listnetworks %s %s %s %s %ld %s %s ",
  174. nl->networks[i].nwidHex,
  175. nl->networks[i].name,
  176. nl->networks[i].macStr,
  177. nl->networks[i].statusStr,
  178. nl->networks[i].configAge,
  179. (nl->networks[i].isPrivate ? "private" : "public"),
  180. nl->networks[i].device);
  181. if (nl->networks[i].numIps > 0) {
  182. for(unsigned int j=0;j<nl->networks[i].numIps;++j) {
  183. if (j > 0)
  184. ipcc->printf(",");
  185. ipcc->printf("%s/%d",nl->networks[i].ips[j].ascii,(int)nl->networks[i].ips[j].port);
  186. }
  187. } else ipcc->printf("-");
  188. ipcc->printf(ZT_EOL_S);
  189. }
  190. _node->freeQueryResult(nl);
  191. }
  192. } else if (cmd[0] == "join") {
  193. if (cmd.size() > 1) {
  194. uint64_t nwid = Utils::hexStrToU64(cmd[1].c_str());
  195. _node->join(nwid);
  196. ipcc->printf("200 join %.16llx OK"ZT_EOL_S,(unsigned long long)nwid);
  197. } else {
  198. ipcc->printf("400 join requires a network ID (>0) in hexadecimal format"ZT_EOL_S);
  199. }
  200. } else if (cmd[0] == "leave") {
  201. if (cmd.size() > 1) {
  202. uint64_t nwid = Utils::hexStrToU64(cmd[1].c_str());
  203. _node->leave(nwid);
  204. ipcc->printf("200 leave %.16llx OK"ZT_EOL_S,(unsigned long long)nwid);
  205. } else {
  206. ipcc->printf("400 leave requires a network ID (>0) in hexadecimal format"ZT_EOL_S);
  207. }
  208. } else if (cmd[0] == "terminate") {
  209. if (cmd.size() > 1)
  210. _node->terminate(Node::NODE_NORMAL_TERMINATION,cmd[1].c_str());
  211. else _node->terminate(Node::NODE_NORMAL_TERMINATION,"terminate via IPC command");
  212. } else if (cmd[0] == "updatecheck") {
  213. if (_node->updateCheck()) {
  214. ipcc->printf("500 software updates are not enabled"ZT_EOL_S);
  215. } else {
  216. ipcc->printf("200 OK"ZT_EOL_S);
  217. }
  218. } else {
  219. ipcc->printf("404 %s No such command. Use 'help' for help."ZT_EOL_S,cmd[0].c_str());
  220. }
  221. }
  222. ipcc->printf("."ZT_EOL_S); // blank line ends response
  223. }
  224. } // namespace ZeroTier