ControlPlane.hpp 3.1 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. struct InetAddress;
  38. /**
  39. * HTTP control plane and static web server
  40. */
  41. class ControlPlane
  42. {
  43. public:
  44. ControlPlane(OneService *svc,Node *n);
  45. ~ControlPlane();
  46. /**
  47. * Add an authentication token for API access
  48. */
  49. inline void addAuthToken(const char *tok)
  50. {
  51. Mutex::Lock _l(_lock);
  52. _authTokens.insert(std::string(tok));
  53. }
  54. /**
  55. * Mount a subsystem under a prefix
  56. *
  57. * Note that the prefix must not contain a dot -- this is reserved for
  58. * static pages -- and must not be a reserved prefix such as /peer
  59. * or /network. Do not include path / characters in the prefix. Example
  60. * would be 'controller' for SqliteNetworkController.
  61. *
  62. * @param prefix First element in URI path
  63. * @param subsys Object to call for results of GET and POST/PUT operations
  64. */
  65. inline void mount(const char *prefix,ControlPlaneSubsystem *subsys)
  66. {
  67. Mutex::Lock _l(_lock);
  68. _subsystems[std::string(prefix)] = subsys;
  69. }
  70. /**
  71. * Handle HTTP request
  72. *
  73. * @param fromAddress Originating IP address of request
  74. * @param httpMethod HTTP method (as defined in ext/http-parser/http_parser.h)
  75. * @param path Request path
  76. * @param headers Request headers
  77. * @param body Request body
  78. * @param responseBody Result parameter: fill with response data
  79. * @param responseContentType Result parameter: fill with content type
  80. * @return HTTP response code
  81. */
  82. unsigned int handleRequest(
  83. const InetAddress &fromAddress,
  84. unsigned int httpMethod,
  85. const std::string &path,
  86. const std::map<std::string,std::string> &headers,
  87. const std::string &body,
  88. std::string &responseBody,
  89. std::string &responseContentType);
  90. private:
  91. OneService *const _svc;
  92. Node *const _node;
  93. std::set<std::string> _authTokens;
  94. std::map<std::string,ControlPlaneSubsystem *> _subsystems;
  95. Mutex _lock;
  96. };
  97. } // namespace ZeroTier
  98. #endif