ControlPlane.hpp 2.5 KB

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