ControlPlane.hpp 2.6 KB

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