netconf.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <unistd.h>
  54. #include <string.h>
  55. #include <iostream>
  56. #include <string>
  57. #include <map>
  58. #include <list>
  59. #include <vector>
  60. #include <algorithm>
  61. #include <mysql++.h>
  62. #include "../node/Dictionary.hpp"
  63. using namespace ZeroTier;
  64. using namespace mysqlpp;
  65. static Connection *dbCon = (Connection *)0;
  66. static void connectOrReconnect()
  67. {
  68. if (dbCon)
  69. delete dbCon;
  70. dbCon = new Connection(mysqlDatabase,mysqlHost,mysqlUser,mysqlPassword,(unsigned int)strtol(mysqlPort,(char **)0,10));
  71. if (dbCon->connected())
  72. break;
  73. else {
  74. fprintf(stderr,"Unable to connect to database server.\n");
  75. usleep(1000);
  76. }
  77. }
  78. int main(int argc,char **argv)
  79. {
  80. char mysqlHost[64],mysqlPort[64],mysqlDatabase[64],mysqlUser[64],mysqlPassword[64];
  81. {
  82. char *ee = getenv("ZT_NETCONF_MYSQL_HOST");
  83. if (!ee) {
  84. fprintf(stderr,"Missing environment variable: ZT_NETCONF_MYSQL_HOST\n");
  85. return -1;
  86. }
  87. strcpy(mysqlHost,ee);
  88. ee = getenv("ZT_NETCONF_MYSQL_PORT");
  89. if (ee == null)
  90. strcpy(mysqlPort,"3306");
  91. else strcpy(mysqlPort,ee);
  92. ee = getenv("ZT_NETCONF_MYSQL_DATABASE");
  93. if (!ee) {
  94. fprintf(stderr,"Missing environment variable: ZT_NETCONF_MYSQL_DATABASE\n");
  95. return -1;
  96. }
  97. strcpy(mysqlDatabase,ee);
  98. ee = getenv("ZT_NETCONF_MYSQL_USER");
  99. if (!ee) {
  100. fprintf(stderr,"Missing environment variable: ZT_NETCONF_MYSQL_USER\n");
  101. return -1;
  102. }
  103. strcpy(mysqlUser,ee);
  104. ee = getenv("ZT_NETCONF_MYSQL_PASSWORD");
  105. if (!ee) {
  106. fprintf(stderr,"Missing environment variable: ZT_NETCONF_MYSQL_PASSWORD\n");
  107. return -1;
  108. }
  109. strcpy(mysqlPassword,ee);
  110. }
  111. connectOrReconnect();
  112. for(;;) {
  113. if (!dbCon->connected())
  114. connectOrReconnect();
  115. }
  116. }