ControlPlane.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef ZT_ONE_CONTROLPLANE_HPP
  28. #define ZT_ONE_CONTROLPLANE_HPP
  29. #include <string>
  30. #include <map>
  31. #include <set>
  32. #include "../include/ZeroTierOne.h"
  33. #include "../node/Mutex.hpp"
  34. namespace ZeroTier {
  35. class OneService;
  36. class Node;
  37. class SqliteNetworkController;
  38. struct InetAddress;
  39. /**
  40. * HTTP control plane and static web server
  41. */
  42. class ControlPlane
  43. {
  44. public:
  45. ControlPlane(OneService *svc,Node *n,const char *uiStaticPath);
  46. ~ControlPlane();
  47. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  48. /**
  49. * Set controller, which will be available under /controller
  50. *
  51. * @param c Network controller instance
  52. */
  53. inline void setController(SqliteNetworkController *c)
  54. {
  55. Mutex::Lock _l(_lock);
  56. _controller = c;
  57. }
  58. #endif
  59. /**
  60. * Add an authentication token for API access
  61. */
  62. inline void addAuthToken(const char *tok)
  63. {
  64. Mutex::Lock _l(_lock);
  65. _authTokens.insert(std::string(tok));
  66. }
  67. /**
  68. * Handle HTTP request
  69. *
  70. * @param fromAddress Originating IP address of request
  71. * @param httpMethod HTTP method (as defined in ext/http-parser/http_parser.h)
  72. * @param path Request path
  73. * @param headers Request headers
  74. * @param body Request body
  75. * @param responseBody Result parameter: fill with response data
  76. * @param responseContentType Result parameter: fill with content type
  77. * @return HTTP response code
  78. */
  79. unsigned int handleRequest(
  80. const InetAddress &fromAddress,
  81. unsigned int httpMethod,
  82. const std::string &path,
  83. const std::map<std::string,std::string> &headers,
  84. const std::string &body,
  85. std::string &responseBody,
  86. std::string &responseContentType);
  87. private:
  88. OneService *const _svc;
  89. Node *const _node;
  90. #ifdef ZT_ENABLE_NETWORK_CONTROLLER
  91. SqliteNetworkController *_controller;
  92. #endif
  93. std::string _uiStaticPath;
  94. std::set<std::string> _authTokens;
  95. Mutex _lock;
  96. };
  97. } // namespace ZeroTier
  98. #endif