NodeControlService.cpp 7.6 KB

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