ControlPlane.hpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 EmbeddedNetworkController;
  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);
  37. /**
  38. * Set controller, which will be available under /controller
  39. *
  40. * @param c Network controller instance
  41. */
  42. inline void setController(EmbeddedNetworkController *c)
  43. {
  44. Mutex::Lock _l(_lock);
  45. _controller = c;
  46. }
  47. /**
  48. * Add an authentication token for API access
  49. */
  50. inline void addAuthToken(const char *tok)
  51. {
  52. Mutex::Lock _l(_lock);
  53. _authTokens.insert(std::string(tok));
  54. }
  55. /**
  56. * Handle HTTP request
  57. *
  58. * @param fromAddress Originating IP address of request
  59. * @param httpMethod HTTP method (as defined in ext/http-parser/http_parser.h)
  60. * @param path Request path
  61. * @param headers Request headers
  62. * @param body Request body
  63. * @param responseBody Result parameter: fill with response data
  64. * @param responseContentType Result parameter: fill with content type
  65. * @return HTTP response code
  66. */
  67. unsigned int handleRequest(
  68. const InetAddress &fromAddress,
  69. unsigned int httpMethod,
  70. const std::string &path,
  71. const std::map<std::string,std::string> &headers,
  72. const std::string &body,
  73. std::string &responseBody,
  74. std::string &responseContentType);
  75. private:
  76. OneService *const _svc;
  77. Node *const _node;
  78. EmbeddedNetworkController *_controller;
  79. std::set<std::string> _authTokens;
  80. Mutex _lock;
  81. };
  82. } // namespace ZeroTier
  83. #endif