ControlPlane.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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 "ControlPlane.hpp"
  28. #include "OneService.hpp"
  29. #include "../version.h"
  30. #include "../include/ZeroTierOne.h"
  31. #include "../ext/http-parser/http_parser.h"
  32. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  33. #include "../controller/SqliteNetworkController.hpp"
  34. #endif
  35. #include "../node/InetAddress.hpp"
  36. #include "../node/Node.hpp"
  37. #include "../node/Utils.hpp"
  38. #include "../osdep/OSUtils.hpp"
  39. namespace ZeroTier {
  40. static std::string _jsonEscape(const char *s)
  41. {
  42. std::string buf;
  43. for(const char *p=s;(*p);++p) {
  44. switch(*p) {
  45. case '\t': buf.append("\\t"); break;
  46. case '\b': buf.append("\\b"); break;
  47. case '\r': buf.append("\\r"); break;
  48. case '\n': buf.append("\\n"); break;
  49. case '\f': buf.append("\\f"); break;
  50. case '"': buf.append("\\\""); break;
  51. case '\\': buf.append("\\\\"); break;
  52. case '/': buf.append("\\/"); break;
  53. default: buf.push_back(*p); break;
  54. }
  55. }
  56. return buf;
  57. }
  58. static std::string _jsonEscape(const std::string &s) { return _jsonEscape(s.c_str()); }
  59. static std::string _jsonEnumerate(const ZT1_MulticastGroup *mg,unsigned int count)
  60. {
  61. std::string buf;
  62. char tmp[128];
  63. buf.push_back('[');
  64. for(unsigned int i=0;i<count;++i) {
  65. if (i > 0)
  66. buf.push_back(',');
  67. Utils::snprintf(tmp,sizeof(tmp),"\"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\\/%.8lx\"",
  68. (unsigned int)((mg[i].mac >> 40) & 0xff),
  69. (unsigned int)((mg[i].mac >> 32) & 0xff),
  70. (unsigned int)((mg[i].mac >> 24) & 0xff),
  71. (unsigned int)((mg[i].mac >> 16) & 0xff),
  72. (unsigned int)((mg[i].mac >> 8) & 0xff),
  73. (unsigned int)(mg[i].mac & 0xff),
  74. (unsigned long)(mg[i].adi));
  75. buf.append(tmp);
  76. }
  77. buf.push_back(']');
  78. return buf;
  79. }
  80. static std::string _jsonEnumerate(const struct sockaddr_storage *ss,unsigned int count)
  81. {
  82. std::string buf;
  83. buf.push_back('[');
  84. for(unsigned int i=0;i<count;++i) {
  85. if (i > 0)
  86. buf.push_back(',');
  87. buf.push_back('"');
  88. buf.append(_jsonEscape(reinterpret_cast<const InetAddress *>(&(ss[i]))->toString()));
  89. buf.push_back('"');
  90. }
  91. buf.push_back(']');
  92. return buf;
  93. }
  94. static void _jsonAppend(unsigned int depth,std::string &buf,const ZT1_VirtualNetworkConfig *nc,const std::string &portDeviceName)
  95. {
  96. char json[4096];
  97. char prefix[32];
  98. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  99. return;
  100. for(unsigned int i=0;i<depth;++i)
  101. prefix[i] = '\t';
  102. prefix[depth] = '\0';
  103. const char *nstatus = "",*ntype = "";
  104. switch(nc->status) {
  105. case ZT1_NETWORK_STATUS_REQUESTING_CONFIGURATION: nstatus = "REQUESTING_CONFIGURATION"; break;
  106. case ZT1_NETWORK_STATUS_OK: nstatus = "OK"; break;
  107. case ZT1_NETWORK_STATUS_ACCESS_DENIED: nstatus = "ACCESS_DENIED"; break;
  108. case ZT1_NETWORK_STATUS_NOT_FOUND: nstatus = "NOT_FOUND"; break;
  109. case ZT1_NETWORK_STATUS_PORT_ERROR: nstatus = "PORT_ERROR"; break;
  110. case ZT1_NETWORK_STATUS_CLIENT_TOO_OLD: nstatus = "CLIENT_TOO_OLD"; break;
  111. }
  112. switch(nc->type) {
  113. case ZT1_NETWORK_TYPE_PRIVATE: ntype = "PRIVATE"; break;
  114. case ZT1_NETWORK_TYPE_PUBLIC: ntype = "PUBLIC"; break;
  115. }
  116. Utils::snprintf(json,sizeof(json),
  117. "%s{\n"
  118. "%s\t\"nwid\": \"%.16llx\",\n"
  119. "%s\t\"mac\": \"%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\",\n"
  120. "%s\t\"name\": \"%s\",\n"
  121. "%s\t\"status\": \"%s\",\n"
  122. "%s\t\"type\": \"%s\",\n"
  123. "%s\t\"mtu\": %u,\n"
  124. "%s\t\"dhcp\": %s,\n"
  125. "%s\t\"bridge\": %s,\n"
  126. "%s\t\"broadcastEnabled\": %s,\n"
  127. "%s\t\"portError\": %d,\n"
  128. "%s\t\"netconfRevision\": %lu,\n"
  129. "%s\t\"multicastSubscriptions\": %s,\n"
  130. "%s\t\"assignedAddresses\": %s,\n"
  131. "%s\t\"portDeviceName\": \"%s\"\n"
  132. "%s}",
  133. prefix,
  134. prefix,nc->nwid,
  135. prefix,(unsigned int)((nc->mac >> 40) & 0xff),(unsigned int)((nc->mac >> 32) & 0xff),(unsigned int)((nc->mac >> 24) & 0xff),(unsigned int)((nc->mac >> 16) & 0xff),(unsigned int)((nc->mac >> 8) & 0xff),(unsigned int)(nc->mac & 0xff),
  136. prefix,_jsonEscape(nc->name).c_str(),
  137. prefix,nstatus,
  138. prefix,ntype,
  139. prefix,nc->mtu,
  140. prefix,(nc->dhcp == 0) ? "false" : "true",
  141. prefix,(nc->bridge == 0) ? "false" : "true",
  142. prefix,(nc->broadcastEnabled == 0) ? "false" : "true",
  143. prefix,nc->portError,
  144. prefix,nc->netconfRevision,
  145. prefix,_jsonEnumerate(nc->multicastSubscriptions,nc->multicastSubscriptionCount).c_str(),
  146. prefix,_jsonEnumerate(nc->assignedAddresses,nc->assignedAddressCount).c_str(),
  147. prefix,_jsonEscape(portDeviceName).c_str(),
  148. prefix);
  149. buf.append(json);
  150. }
  151. static std::string _jsonEnumerate(unsigned int depth,const ZT1_PeerPhysicalPath *pp,unsigned int count)
  152. {
  153. char json[1024];
  154. char prefix[32];
  155. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  156. return std::string();
  157. for(unsigned int i=0;i<depth;++i)
  158. prefix[i] = '\t';
  159. prefix[depth] = '\0';
  160. std::string buf;
  161. for(unsigned int i=0;i<count;++i) {
  162. if (i > 0)
  163. buf.push_back(',');
  164. Utils::snprintf(json,sizeof(json),
  165. "{\n"
  166. "%s\t\"address\": \"%s\",\n"
  167. "%s\t\"lastSend\": %llu,\n"
  168. "%s\t\"lastReceive\": %llu,\n"
  169. "%s\t\"fixed\": %s,\n"
  170. "%s\t\"active\": %s,\n"
  171. "%s\t\"preferred\": %s\n"
  172. "%s}",
  173. prefix,_jsonEscape(reinterpret_cast<const InetAddress *>(&(pp[i].address))->toString()).c_str(),
  174. prefix,pp[i].lastSend,
  175. prefix,pp[i].lastReceive,
  176. prefix,(pp[i].fixed == 0) ? "false" : "true",
  177. prefix,(pp[i].active == 0) ? "false" : "true",
  178. prefix,(pp[i].preferred == 0) ? "false" : "true",
  179. prefix);
  180. buf.append(json);
  181. }
  182. return buf;
  183. }
  184. static void _jsonAppend(unsigned int depth,std::string &buf,const ZT1_Peer *peer)
  185. {
  186. char json[1024];
  187. char prefix[32];
  188. if (depth >= sizeof(prefix)) // sanity check -- shouldn't be possible
  189. return;
  190. for(unsigned int i=0;i<depth;++i)
  191. prefix[i] = '\t';
  192. prefix[depth] = '\0';
  193. const char *prole = "";
  194. switch(peer->role) {
  195. case ZT1_PEER_ROLE_LEAF: prole = "LEAF"; break;
  196. case ZT1_PEER_ROLE_HUB: prole = "HUB"; break;
  197. case ZT1_PEER_ROLE_SUPERNODE: prole = "SUPERNODE"; break;
  198. }
  199. Utils::snprintf(json,sizeof(json),
  200. "%s{\n"
  201. "%s\t\"address\": \"%.10llx\",\n"
  202. "%s\t\"lastUnicastFrame\": %llu,\n"
  203. "%s\t\"lastMulticastFrame\": %llu,\n"
  204. "%s\t\"versionMajor\": %d,\n"
  205. "%s\t\"versionMinor\": %d,\n"
  206. "%s\t\"versionRev\": %d,\n"
  207. "%s\t\"version\": \"%d.%d.%d\",\n"
  208. "%s\t\"latency\": %u,\n"
  209. "%s\t\"role\": \"%s\",\n"
  210. "%s\t\"paths\": [%s]\n"
  211. "%s}",
  212. prefix,
  213. prefix,peer->address,
  214. prefix,peer->lastUnicastFrame,
  215. prefix,peer->lastMulticastFrame,
  216. prefix,peer->versionMajor,
  217. prefix,peer->versionMinor,
  218. prefix,peer->versionRev,
  219. prefix,peer->versionMajor,peer->versionMinor,peer->versionRev,
  220. prefix,peer->latency,
  221. prefix,prole,
  222. prefix,_jsonEnumerate(depth+1,peer->paths,peer->pathCount).c_str(),
  223. prefix);
  224. buf.append(json);
  225. }
  226. ControlPlane::ControlPlane(OneService *svc,Node *n,const char *uiStaticPath) :
  227. _svc(svc),
  228. _node(n),
  229. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  230. _controller((SqliteNetworkController *)0),
  231. #endif
  232. _uiStaticPath((uiStaticPath) ? uiStaticPath : "")
  233. {
  234. }
  235. ControlPlane::~ControlPlane()
  236. {
  237. }
  238. unsigned int ControlPlane::handleRequest(
  239. const InetAddress &fromAddress,
  240. unsigned int httpMethod,
  241. const std::string &path,
  242. const std::map<std::string,std::string> &headers,
  243. const std::string &body,
  244. std::string &responseBody,
  245. std::string &responseContentType)
  246. {
  247. char json[1024];
  248. unsigned int scode = 404;
  249. std::vector<std::string> ps(Utils::split(path.c_str(),"/","",""));
  250. std::map<std::string,std::string> urlArgs;
  251. Mutex::Lock _l(_lock);
  252. if (!((fromAddress.ipsEqual(InetAddress::LO4))||(fromAddress.ipsEqual(InetAddress::LO6))))
  253. return 403; // Forbidden: we only allow access from localhost right now
  254. /* Note: this is kind of restricted in what it'll take. It does not support
  255. * URL encoding, and /'s in URL args will screw it up. But the only URL args
  256. * it really uses in ?jsonp=funcionName, and otherwise it just takes simple
  257. * paths to simply-named resources. */
  258. if (ps.size() > 0) {
  259. std::size_t qpos = ps[ps.size() - 1].find('?');
  260. if (qpos != std::string::npos) {
  261. std::string args(ps[ps.size() - 1].substr(qpos + 1));
  262. ps[ps.size() - 1] = ps[ps.size() - 1].substr(0,qpos);
  263. std::vector<std::string> asplit(Utils::split(args.c_str(),"&","",""));
  264. for(std::vector<std::string>::iterator a(asplit.begin());a!=asplit.end();++a) {
  265. std::size_t eqpos = a->find('=');
  266. if (eqpos == std::string::npos)
  267. urlArgs[*a] = "";
  268. else urlArgs[a->substr(0,eqpos)] = a->substr(eqpos + 1);
  269. }
  270. }
  271. } else {
  272. ps.push_back(std::string("index.html"));
  273. }
  274. bool isAuth = false;
  275. {
  276. std::map<std::string,std::string>::const_iterator ah(headers.find("x-zt1-auth"));
  277. if ((ah != headers.end())&&(_authTokens.count(ah->second) > 0)) {
  278. isAuth = true;
  279. } else {
  280. ah = urlArgs.find("auth");
  281. if ((ah != urlArgs.end())&&(_authTokens.count(ah->second) > 0))
  282. isAuth = true;
  283. }
  284. }
  285. if (httpMethod == HTTP_GET) {
  286. std::string ext;
  287. std::size_t dotIdx = ps[0].find_last_of('.');
  288. if (dotIdx != std::string::npos)
  289. ext = ps[0].substr(dotIdx);
  290. if ((ps.size() == 1)&&(ext.length() >= 2)&&(ext[0] == '.')) {
  291. /* Static web pages can be served without authentication to enable a simple web
  292. * UI. This is still only allowed from approved IP addresses. Anything with a
  293. * dot in the first path element (e.g. foo.html) is considered a static page,
  294. * as nothing in the API is so named. */
  295. if (_uiStaticPath.length() > 0) {
  296. if (ext == ".html")
  297. responseContentType = "text/html";
  298. else if (ext == ".js")
  299. responseContentType = "application/javascript";
  300. else if (ext == ".jsx")
  301. responseContentType = "text/jsx";
  302. else if (ext == ".json")
  303. responseContentType = "application/json";
  304. else if (ext == ".css")
  305. responseContentType = "text/css";
  306. else if (ext == ".png")
  307. responseContentType = "image/png";
  308. else if (ext == ".jpg")
  309. responseContentType = "image/jpeg";
  310. else if (ext == ".gif")
  311. responseContentType = "image/gif";
  312. else if (ext == ".txt")
  313. responseContentType = "text/plain";
  314. else if (ext == ".xml")
  315. responseContentType = "text/xml";
  316. else if (ext == ".svg")
  317. responseContentType = "image/svg+xml";
  318. else responseContentType = "application/octet-stream";
  319. scode = OSUtils::readFile((_uiStaticPath + ZT_PATH_SEPARATOR_S + ps[0]).c_str(),responseBody) ? 200 : 404;
  320. } else {
  321. scode = 404;
  322. }
  323. } else if (isAuth) {
  324. /* Things that require authentication -- a.k.a. everything but static web app pages. */
  325. if (ps[0] == "status") {
  326. responseContentType = "application/json";
  327. ZT1_NodeStatus status;
  328. _node->status(&status);
  329. Utils::snprintf(json,sizeof(json),
  330. "{\n"
  331. "\t\"address\": \"%.10llx\",\n"
  332. "\t\"publicIdentity\": \"%s\",\n"
  333. "\t\"online\": %s,\n"
  334. "\t\"tcpFallbackActive\": %s,\n"
  335. "\t\"versionMajor\": %d,\n"
  336. "\t\"versionMinor\": %d,\n"
  337. "\t\"versionRev\": %d,\n"
  338. "\t\"version\": \"%d.%d.%d\",\n"
  339. "\t\"clock\": %llu\n"
  340. "}\n",
  341. status.address,
  342. status.publicIdentity,
  343. (status.online) ? "true" : "false",
  344. (_svc->tcpFallbackActive()) ? "true" : "false",
  345. ZEROTIER_ONE_VERSION_MAJOR,
  346. ZEROTIER_ONE_VERSION_MINOR,
  347. ZEROTIER_ONE_VERSION_REVISION,
  348. ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION,
  349. (unsigned long long)OSUtils::now());
  350. responseBody = json;
  351. scode = 200;
  352. } else if (ps[0] == "config") {
  353. responseContentType = "application/json";
  354. responseBody = "{}"; // TODO
  355. scode = 200;
  356. } else if (ps[0] == "network") {
  357. ZT1_VirtualNetworkList *nws = _node->networks();
  358. if (nws) {
  359. if (ps.size() == 1) {
  360. // Return [array] of all networks
  361. responseContentType = "application/json";
  362. responseBody = "[\n";
  363. for(unsigned long i=0;i<nws->networkCount;++i) {
  364. if (i > 0)
  365. responseBody.append(",");
  366. _jsonAppend(1,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
  367. }
  368. responseBody.append("\n]\n");
  369. scode = 200;
  370. } else if (ps.size() == 2) {
  371. // Return a single network by ID or 404 if not found
  372. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  373. for(unsigned long i=0;i<nws->networkCount;++i) {
  374. if (nws->networks[i].nwid == wantnw) {
  375. responseContentType = "application/json";
  376. _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
  377. responseBody.push_back('\n');
  378. scode = 200;
  379. break;
  380. }
  381. }
  382. } // else 404
  383. _node->freeQueryResult((void *)nws);
  384. } else scode = 500;
  385. } else if (ps[0] == "peer") {
  386. ZT1_PeerList *pl = _node->peers();
  387. if (pl) {
  388. if (ps.size() == 1) {
  389. // Return [array] of all peers
  390. responseContentType = "application/json";
  391. responseBody = "[\n";
  392. for(unsigned long i=0;i<pl->peerCount;++i) {
  393. if (i > 0)
  394. responseBody.append(",\n");
  395. _jsonAppend(1,responseBody,&(pl->peers[i]));
  396. }
  397. responseBody.append("\n]\n");
  398. scode = 200;
  399. } else if (ps.size() == 2) {
  400. // Return a single peer by ID or 404 if not found
  401. uint64_t wantp = Utils::hexStrToU64(ps[1].c_str());
  402. for(unsigned long i=0;i<pl->peerCount;++i) {
  403. if (pl->peers[i].address == wantp) {
  404. responseContentType = "application/json";
  405. _jsonAppend(0,responseBody,&(pl->peers[i]));
  406. responseBody.push_back('\n');
  407. scode = 200;
  408. break;
  409. }
  410. }
  411. } // else 404
  412. _node->freeQueryResult((void *)pl);
  413. } else scode = 500;
  414. } else if (ps[0] == "newIdentity") {
  415. // Return a newly generated ZeroTier identity -- this is primarily for debugging
  416. // and testing to make it easy for automated test scripts to generate test IDs.
  417. Identity newid;
  418. newid.generate();
  419. responseBody = newid.toString(true);
  420. responseContentType = "text/plain";
  421. scode = 200;
  422. } else {
  423. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  424. if (_controller)
  425. scode = _controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  426. else scode = 404;
  427. #else
  428. scode = 404;
  429. #endif
  430. }
  431. } else scode = 401; // isAuth == false
  432. } else if ((httpMethod == HTTP_POST)||(httpMethod == HTTP_PUT)) {
  433. if (isAuth) {
  434. if (ps[0] == "config") {
  435. // TODO
  436. } else if (ps[0] == "network") {
  437. if (ps.size() == 2) {
  438. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  439. _node->join(wantnw); // does nothing if we are a member
  440. ZT1_VirtualNetworkList *nws = _node->networks();
  441. if (nws) {
  442. for(unsigned long i=0;i<nws->networkCount;++i) {
  443. if (nws->networks[i].nwid == wantnw) {
  444. responseContentType = "application/json";
  445. _jsonAppend(0,responseBody,&(nws->networks[i]),_svc->portDeviceName(nws->networks[i].nwid));
  446. responseBody.push_back('\n');
  447. scode = 200;
  448. break;
  449. }
  450. }
  451. _node->freeQueryResult((void *)nws);
  452. } else scode = 500;
  453. }
  454. } else {
  455. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  456. if (_controller)
  457. scode = _controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  458. else scode = 404;
  459. #else
  460. scode = 404;
  461. #endif
  462. }
  463. } else scode = 401; // isAuth == false
  464. } else if (httpMethod == HTTP_DELETE) {
  465. if (isAuth) {
  466. if (ps[0] == "config") {
  467. // TODO
  468. } else if (ps[0] == "network") {
  469. ZT1_VirtualNetworkList *nws = _node->networks();
  470. if (nws) {
  471. if (ps.size() == 2) {
  472. uint64_t wantnw = Utils::hexStrToU64(ps[1].c_str());
  473. for(unsigned long i=0;i<nws->networkCount;++i) {
  474. if (nws->networks[i].nwid == wantnw) {
  475. _node->leave(wantnw);
  476. responseBody = "true";
  477. responseContentType = "application/json";
  478. scode = 200;
  479. break;
  480. }
  481. }
  482. } // else 404
  483. _node->freeQueryResult((void *)nws);
  484. } else scode = 500;
  485. } else {
  486. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  487. if (_controller)
  488. scode = _controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
  489. else scode = 404;
  490. #else
  491. scode = 404;
  492. #endif
  493. }
  494. } else {
  495. scode = 401; // isAuth = false
  496. }
  497. } else {
  498. scode = 400;
  499. responseBody = "Method not supported.";
  500. }
  501. // Wrap result in jsonp function call if the user included a jsonp= url argument.
  502. // Also double-check isAuth since forbidding this without auth feels safer.
  503. std::map<std::string,std::string>::const_iterator jsonp(urlArgs.find("jsonp"));
  504. if ((isAuth)&&(jsonp != urlArgs.end())&&(responseContentType == "application/json")) {
  505. if (responseBody.length() > 0)
  506. responseBody = jsonp->second + "(" + responseBody + ");";
  507. else responseBody = jsonp->second + "(null);";
  508. responseContentType = "application/javascript";
  509. }
  510. return scode;
  511. }
  512. } // namespace ZeroTier