ControlPlane.hpp 3.1 KB

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