netconf.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. /*
  28. * This is the netconf service. It's currently used only by netconf nodes that
  29. * are run by ZeroTier itself. There is nothing to prevent you from running
  30. * your own if you wanted to create your own networks outside our system.
  31. *
  32. * That being said, we'd like to charge for private networks to support
  33. * ZeroTier One and future development efforts. So while this software is
  34. * open source and we're not going to stop you from sidestepping this, we
  35. * do ask -- honor system here -- that you pay for private networks if you
  36. * are going to use them for any commercial purpose such as a business VPN
  37. * alternative.
  38. *
  39. * This will at the moment only build on Linux and requires the mysql++
  40. * library, which is available here:
  41. *
  42. * http://tangentsoft.net/mysql++/
  43. *
  44. * (Packages are available for CentOS via EPEL and for any Debian distro.)
  45. *
  46. * This program must be built and installed in the services.d subfolder of
  47. * the ZeroTier One home folder of the node designated to act as a master
  48. * for networks. Doing so will enable the NETWORK_CONFIG_REQUEST protocol
  49. * verb.
  50. */
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <stdint.h>
  55. #include <unistd.h>
  56. #include <errno.h>
  57. #include <sys/stat.h>
  58. #include <sys/types.h>
  59. #include <arpa/inet.h>
  60. #include <iostream>
  61. #include <string>
  62. #include <map>
  63. #include <list>
  64. #include <vector>
  65. #include <algorithm>
  66. #include <mysql++/mysql++.h>
  67. #include "../version.h"
  68. #include "../node/Constants.hpp"
  69. #include "../node/Dictionary.hpp"
  70. #include "../node/Identity.hpp"
  71. #include "../node/Utils.hpp"
  72. #include "../node/Mutex.hpp"
  73. #include "../node/NetworkConfig.hpp"
  74. #include "../node/CertificateOfMembership.hpp"
  75. using namespace ZeroTier;
  76. using namespace mysqlpp;
  77. static Mutex stdoutWriteLock;
  78. static Connection *dbCon = (Connection *)0;
  79. static char mysqlHost[64],mysqlPort[64],mysqlDatabase[64],mysqlUser[64],mysqlPassword[64];
  80. int main(int argc,char **argv)
  81. {
  82. {
  83. char *ee = getenv("ZT_NETCONF_MYSQL_HOST");
  84. if (!ee) {
  85. fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_HOST\n");
  86. return -1;
  87. }
  88. strcpy(mysqlHost,ee);
  89. ee = getenv("ZT_NETCONF_MYSQL_PORT");
  90. if (!ee)
  91. strcpy(mysqlPort,"3306");
  92. else strcpy(mysqlPort,ee);
  93. ee = getenv("ZT_NETCONF_MYSQL_DATABASE");
  94. if (!ee) {
  95. fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_DATABASE\n");
  96. return -1;
  97. }
  98. strcpy(mysqlDatabase,ee);
  99. ee = getenv("ZT_NETCONF_MYSQL_USER");
  100. if (!ee) {
  101. fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_USER\n");
  102. return -1;
  103. }
  104. strcpy(mysqlUser,ee);
  105. ee = getenv("ZT_NETCONF_MYSQL_PASSWORD");
  106. if (!ee) {
  107. fprintf(stderr,"missing environment variable: ZT_NETCONF_MYSQL_PASSWORD\n");
  108. return -1;
  109. }
  110. strcpy(mysqlPassword,ee);
  111. }
  112. char buf[131072],buf2[131072];
  113. Identity signingIdentity;
  114. std::string dictBuf;
  115. try {
  116. dbCon = new Connection(mysqlDatabase,mysqlHost,mysqlUser,mysqlPassword,(unsigned int)strtol(mysqlPort,(char **)0,10));
  117. if (dbCon->connected()) {
  118. fprintf(stderr,"connected to mysql server successfully\n");
  119. } else {
  120. fprintf(stderr,"unable to connect to database server\n");
  121. return -1;
  122. }
  123. } catch (std::exception &exc) {
  124. fprintf(stderr,"unable to connect to database server: %s\n",exc.what());
  125. return -1;
  126. }
  127. // Send ready message to tell parent that the service is up, and to
  128. // solicit netconf-init.
  129. {
  130. Dictionary response;
  131. response["type"] = "ready";
  132. std::string respm = response.toString();
  133. uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());
  134. stdoutWriteLock.lock();
  135. write(STDOUT_FILENO,&respml,4);
  136. write(STDOUT_FILENO,respm.data(),respm.length());
  137. stdoutWriteLock.unlock();
  138. }
  139. for(;;) {
  140. for(int l=0;l<4;) {
  141. int n = (int)read(STDIN_FILENO,buf + l,4 - l);
  142. if (n < 0) {
  143. fprintf(stderr,"error reading frame size from stdin: %s\n",strerror(errno));
  144. return -1;
  145. }
  146. l += n;
  147. }
  148. unsigned int fsize = (unsigned int)ntohl(*((const uint32_t *)buf));
  149. while (dictBuf.length() < fsize) {
  150. int n = (int)read(STDIN_FILENO,buf,std::min((int)sizeof(buf),(int)(fsize - dictBuf.length())));
  151. if (n < 0) {
  152. fprintf(stderr,"error reading frame from stdin: %s\n",strerror(errno));
  153. return -1;
  154. }
  155. for(int i=0;i<n;++i)
  156. dictBuf.push_back(buf[i]);
  157. }
  158. Dictionary request(dictBuf);
  159. dictBuf = "";
  160. if (!dbCon->connected()) {
  161. fprintf(stderr,"connection to database server lost\n");
  162. return -1;
  163. }
  164. // Check QNetworkConfigRefresh (MEMORY table) and push network
  165. // config refreshes to queued peer/network pairs.
  166. try {
  167. Dictionary to;
  168. {
  169. Query q = dbCon->query();
  170. q << "SELECT DISTINCT LOWER(HEX(Node_id)) AS Node_id,LOWER(HEX(Network_id)) AS Network_id FROM QNetworkConfigRefresh";
  171. StoreQueryResult rs = q.store();
  172. for(unsigned long i=0;i<rs.num_rows();++i) {
  173. std::string &nwids = to[rs[i]["Node_id"].c_str()];
  174. if (nwids.length())
  175. nwids.push_back(',');
  176. nwids.append(rs[i]["Network_id"]);
  177. }
  178. }
  179. {
  180. Query q = dbCon->query();
  181. q << "DELETE FROM QNetworkConfigRefresh";
  182. q.exec();
  183. }
  184. Dictionary response;
  185. response["type"] = "netconf-push";
  186. response["to"] = to.toString();
  187. std::string respm = response.toString();
  188. uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());
  189. stdoutWriteLock.lock();
  190. write(STDOUT_FILENO,&respml,4);
  191. write(STDOUT_FILENO,respm.data(),respm.length());
  192. stdoutWriteLock.unlock();
  193. } catch ( ... ) {}
  194. try {
  195. const std::string &reqType = request.get("type");
  196. if (reqType == "netconf-init") { // initialization to set things like netconf's identity
  197. Identity netconfId(request.get("netconfId"));
  198. if ((netconfId)&&(netconfId.hasPrivate())) {
  199. signingIdentity = netconfId;
  200. fprintf(stderr,"got netconf signing identity: %s\n",signingIdentity.toString(false).c_str());
  201. } else {
  202. fprintf(stderr,"netconfId invalid or lacks private key\n");
  203. return -1;
  204. }
  205. } else if (reqType == "netconf-request") { // NETWORK_CONFIG_REQUEST packet
  206. if (!signingIdentity) {
  207. fprintf(stderr,"no signing identity; missing netconf-init?\n");
  208. return -1;
  209. }
  210. // Deserialize querying peer identity and network ID
  211. Identity peerIdentity(request.get("peerId"));
  212. uint64_t nwid = strtoull(request.get("nwid").c_str(),(char **)0,16);
  213. std::string fromAddr(request.get("from",""));
  214. // Meta-information from node, such as (future) geo-location stuff
  215. Dictionary meta;
  216. if (request.contains("meta"))
  217. meta.fromString(request.get("meta"));
  218. // Check validity of node's identity, ignore request on failure
  219. if (!peerIdentity.locallyValidate()) {
  220. fprintf(stderr,"identity failed validity check: %s\n",peerIdentity.toString(false).c_str());
  221. continue;
  222. }
  223. // Save node's identity if unknown
  224. {
  225. Query q = dbCon->query();
  226. q << "SELECT identity FROM Node WHERE id = " << peerIdentity.address().toInt();
  227. StoreQueryResult rs = q.store();
  228. if (rs.num_rows() > 0) {
  229. if (rs[0]["identity"] != peerIdentity.toString(false)) {
  230. // TODO: handle collisions...
  231. continue;
  232. }
  233. } else {
  234. q = dbCon->query();
  235. q << "INSERT INTO Node (id,creationTime,identity) VALUES (" << peerIdentity.address().toInt() << "," << Utils::now() << "," << quote << peerIdentity.toString(false) << ")";
  236. if (!q.exec()) {
  237. fprintf(stderr,"error inserting Node row for peer %s, aborting netconf request\n",peerIdentity.address().toString().c_str());
  238. continue;
  239. }
  240. // TODO: launch background validation
  241. }
  242. }
  243. // Look up core network information
  244. bool isOpen = false;
  245. unsigned int multicastPrefixBits = 0;
  246. unsigned int multicastDepth = 0;
  247. bool emulateArp = false;
  248. bool emulateNdp = false;
  249. unsigned int arpCacheTtl = 0;
  250. unsigned int ndpCacheTtl = 0;
  251. std::string name;
  252. std::string desc;
  253. {
  254. Query q = dbCon->query();
  255. q << "SELECT * FROM Network WHERE id = " << nwid;
  256. StoreQueryResult rs = q.store();
  257. if (rs.num_rows() > 0) {
  258. name = rs[0]["name"].c_str();
  259. desc = rs[0]["desc"].c_str();
  260. isOpen = ((int)rs[0]["isOpen"] > 0);
  261. emulateArp = ((int)rs[0]["emulateArp"] > 0);
  262. emulateNdp = ((int)rs[0]["emulateNdp"] > 0);
  263. arpCacheTtl = (unsigned int)rs[0]["arpCacheTtl"];
  264. ndpCacheTtl = (unsigned int)rs[0]["ndpCacheTtl"];
  265. multicastPrefixBits = (unsigned int)rs[0]["multicastPrefixBits"];
  266. multicastDepth = (unsigned int)rs[0]["multicastDepth"];
  267. } else {
  268. Dictionary response;
  269. response["peer"] = peerIdentity.address().toString();
  270. response["nwid"] = request.get("nwid");
  271. response["type"] = "netconf-response";
  272. response["requestId"] = request.get("requestId");
  273. response["error"] = "OBJ_NOT_FOUND";
  274. std::string respm = response.toString();
  275. uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());
  276. stdoutWriteLock.lock();
  277. write(STDOUT_FILENO,&respml,4);
  278. write(STDOUT_FILENO,respm.data(),respm.length());
  279. stdoutWriteLock.unlock();
  280. continue; // ABORT, wait for next request
  281. }
  282. }
  283. // Check membership if this is a closed network
  284. bool authenticated = true;
  285. if (!isOpen) {
  286. Query q = dbCon->query();
  287. q << "SELECT Node_id FROM NetworkNodes WHERE Network_id = " << nwid << " AND Node_id = " << peerIdentity.address().toInt();
  288. StoreQueryResult rs = q.store();
  289. if (!rs.num_rows()) {
  290. Dictionary response;
  291. response["peer"] = peerIdentity.address().toString();
  292. response["nwid"] = request.get("nwid");
  293. response["type"] = "netconf-response";
  294. response["requestId"] = request.get("requestId");
  295. response["error"] = "ACCESS_DENIED";
  296. std::string respm = response.toString();
  297. uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());
  298. stdoutWriteLock.lock();
  299. write(STDOUT_FILENO,&respml,4);
  300. write(STDOUT_FILENO,respm.data(),respm.length());
  301. stdoutWriteLock.unlock();
  302. authenticated = false;
  303. }
  304. }
  305. // Update most recent activity entry for this peer, also indicating
  306. // whether authentication was successful.
  307. {
  308. if (fromAddr.length()) {
  309. Query q = dbCon->query();
  310. q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime,authenticated,lastActivityFrom) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << "," << (authenticated ? 1 : 0) << "," << quote << fromAddr << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime),authenticated = VALUES(authenticated),lastActivityFrom = VALUES(lastActivityFrom)";
  311. q.exec();
  312. } else {
  313. Query q = dbCon->query();
  314. q << "INSERT INTO NetworkActivity (Network_id,Node_id,lastActivityTime,authenticated) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << Utils::now() << "," << (authenticated ? 1 : 0) << ") ON DUPLICATE KEY UPDATE lastActivityTime = VALUES(lastActivityTime),authenticated = VALUES(authenticated)";
  315. q.exec();
  316. }
  317. }
  318. if (!authenticated)
  319. continue; // ABORT, wait for next request
  320. // Get list of etherTypes in comma-delimited hex format
  321. std::string etherTypeWhitelist;
  322. {
  323. Query q = dbCon->query();
  324. q << "SELECT DISTINCT LOWER(HEX(etherType)) AS etherType FROM NetworkEthertypes WHERE Network_id = " << nwid;
  325. StoreQueryResult rs = q.store();
  326. for(unsigned long i=0;i<rs.num_rows();++i) {
  327. if (etherTypeWhitelist.length() > 0)
  328. etherTypeWhitelist.push_back(',');
  329. etherTypeWhitelist.append(rs[i]["etherType"].c_str());
  330. }
  331. }
  332. // Get multicast group rates in dictionary format
  333. Dictionary multicastRates;
  334. {
  335. Query q = dbCon->query();
  336. q << "SELECT DISTINCT multicastGroupMac,multicastGroupAdi,preload,maxBalance,accrual FROM NetworkMulticastRates WHERE Network_id = " << nwid;
  337. StoreQueryResult rs = q.store();
  338. for(unsigned long i=0;i<rs.num_rows();++i) {
  339. unsigned long preload = (unsigned long)rs[i]["preload"];
  340. unsigned long maxBalance = (unsigned long)rs[i]["maxBalance"];
  341. unsigned long accrual = (unsigned long)rs[i]["accrual"];
  342. unsigned long long mac = (unsigned long long)rs[i]["multicastGroupMac"];
  343. sprintf(buf,"%.12llx/%lx",(mac & 0xffffffffffffULL),(unsigned long)rs[i]["multicastGroupAdi"]);
  344. sprintf(buf2,"%lx,%lx,%lx",preload,maxBalance,accrual);
  345. multicastRates[buf] = buf2;
  346. }
  347. }
  348. // Check for (or assign?) static IP address assignments
  349. std::string ipv4Static;
  350. std::string ipv6Static;
  351. {
  352. Query q = dbCon->query();
  353. q << "SELECT INET_NTOA(ip) AS ip,netmaskBits FROM IPv4Static WHERE Node_id = " << peerIdentity.address().toInt() << " AND Network_id = " << nwid;
  354. StoreQueryResult rs = q.store();
  355. if (rs.num_rows() > 0) {
  356. for(int i=0;i<rs.num_rows();++i) {
  357. if (ipv4Static.length())
  358. ipv4Static.push_back(',');
  359. ipv4Static.append(rs[i]["ip"].c_str());
  360. ipv4Static.push_back('/');
  361. ipv4Static.append(rs[i]["netmaskBits"].c_str());
  362. }
  363. }
  364. // Try to auto-assign if there's any auto-assign networks with space
  365. // available.
  366. if (!ipv4Static.length()) {
  367. unsigned char addressBytes[5];
  368. peerIdentity.address().copyTo(addressBytes,5);
  369. q = dbCon->query();
  370. q << "SELECT ipNet,netmaskBits FROM IPv4AutoAssign WHERE Network_id = " << nwid;
  371. rs = q.store();
  372. if (rs.num_rows() > 0) {
  373. for(int aaRow=0;aaRow<rs.num_rows();++aaRow) {
  374. uint32_t ipNet = (uint32_t)((unsigned long)rs[aaRow]["ipNet"]);
  375. unsigned int netmaskBits = (unsigned int)rs[aaRow]["netmaskBits"];
  376. if ((netmaskBits > 0)&&(ipNet)) {
  377. uint32_t tryIp = (((uint32_t)addressBytes[1]) << 24) |
  378. (((uint32_t)addressBytes[2]) << 16) |
  379. (((uint32_t)addressBytes[3]) << 8) |
  380. ((((uint32_t)addressBytes[4]) % 254) + 1);
  381. tryIp &= (0xffffffff >> netmaskBits);
  382. tryIp |= ipNet;
  383. for(int k=0;k<100000;++k) {
  384. Query q2 = dbCon->query();
  385. q2 << "INSERT INTO IPv4Static (Network_id,Node_id,ip,netmaskBits) VALUES (" << nwid << "," << peerIdentity.address().toInt() << "," << tryIp << "," << netmaskBits << ")";
  386. if (q2.exec()) {
  387. sprintf(buf,"%u.%u.%u.%u",(unsigned int)((tryIp >> 24) & 0xff),(unsigned int)((tryIp >> 16) & 0xff),(unsigned int)((tryIp >> 8) & 0xff),(unsigned int)(tryIp & 0xff));
  388. if (ipv4Static.length())
  389. ipv4Static.push_back(',');
  390. ipv4Static.append(buf);
  391. ipv4Static.push_back('/');
  392. sprintf(buf,"%u",netmaskBits);
  393. ipv4Static.append(buf);
  394. break;
  395. } else { // insert will fail if IP is in use due to uniqueness constraints in DB
  396. ++tryIp;
  397. if ((tryIp & 0xff) == 0)
  398. tryIp |= 1;
  399. tryIp &= (0xffffffff >> netmaskBits);
  400. tryIp |= ipNet;
  401. }
  402. }
  403. if (ipv4Static.length())
  404. break;
  405. }
  406. }
  407. }
  408. }
  409. }
  410. // Assemble response dictionary to send to peer
  411. Dictionary netconf;
  412. sprintf(buf,"%d.%d.%d",ZEROTIER_ONE_VERSION_MAJOR,ZEROTIER_ONE_VERSION_MINOR,ZEROTIER_ONE_VERSION_REVISION);
  413. netconf[ZT_NETWORKCONFIG_DICT_KEY_NETCONF_SERVICE_VERSION] = buf;
  414. sprintf(buf,"%.16llx",(unsigned long long)nwid);
  415. netconf[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = buf;
  416. netconf[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = peerIdentity.address().toString();
  417. netconf[ZT_NETWORKCONFIG_DICT_KEY_NAME] = name;
  418. netconf[ZT_NETWORKCONFIG_DICT_KEY_DESC] = desc;
  419. netconf[ZT_NETWORKCONFIG_DICT_KEY_IS_OPEN] = (isOpen ? "1" : "0");
  420. netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = etherTypeWhitelist;
  421. netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = multicastRates.toString();
  422. sprintf(buf,"%llx",(unsigned long long)Utils::now());
  423. netconf[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = buf;
  424. netconf[ZT_NETWORKCONFIG_DICT_KEY_EMULATE_ARP] = (emulateArp ? "1" : "0");
  425. netconf[ZT_NETWORKCONFIG_DICT_KEY_EMULATE_NDP] = (emulateNdp ? "1" : "0");
  426. if (arpCacheTtl) {
  427. sprintf(buf,"%x",arpCacheTtl);
  428. netconf[ZT_NETWORKCONFIG_DICT_KEY_ARP_CACHE_TTL] = buf;
  429. }
  430. if (ndpCacheTtl) {
  431. sprintf(buf,"%x",ndpCacheTtl);
  432. netconf[ZT_NETWORKCONFIG_DICT_KEY_NDP_CACHE_TTL] = buf;
  433. }
  434. if (multicastPrefixBits) {
  435. sprintf(buf,"%x",multicastPrefixBits);
  436. netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_PREFIX_BITS] = buf;
  437. }
  438. if (multicastDepth) {
  439. sprintf(buf,"%x",multicastDepth);
  440. netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_DEPTH] = buf;
  441. }
  442. if (ipv4Static.length())
  443. netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = ipv4Static;
  444. if (ipv6Static.length())
  445. netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = ipv6Static;
  446. if ((!isOpen)&&(authenticated)) {
  447. CertificateOfMembership com(Utils::now(),ZT_NETWORK_AUTOCONF_DELAY * 3,nwid,peerIdentity.address());
  448. com.sign(signingIdentity);
  449. netconf[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
  450. }
  451. // Send netconf as service bus response
  452. {
  453. Dictionary response;
  454. response["peer"] = peerIdentity.address().toString();
  455. response["nwid"] = request.get("nwid");
  456. response["type"] = "netconf-response";
  457. response["requestId"] = request.get("requestId");
  458. response["netconf"] = netconf.toString();
  459. std::string respm = response.toString();
  460. uint32_t respml = (uint32_t)htonl((uint32_t)respm.length());
  461. stdoutWriteLock.lock();
  462. write(STDOUT_FILENO,&respml,4);
  463. write(STDOUT_FILENO,respm.data(),respm.length());
  464. stdoutWriteLock.unlock();
  465. // LOOP, wait for next request
  466. }
  467. }
  468. } catch (std::exception &exc) {
  469. fprintf(stderr,"unexpected exception handling message: %s\n",exc.what());
  470. } catch ( ... ) {
  471. fprintf(stderr,"unexpected exception handling message: unknown exception\n");
  472. }
  473. }
  474. }