NodeControlService.cpp 9.1 KB

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