server_secure_chttp2.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include <grpc/grpc.h>
  34. #include <string.h>
  35. #include "src/core/channel/channel_args.h"
  36. #include "src/core/channel/http_server_filter.h"
  37. #include "src/core/iomgr/endpoint.h"
  38. #include "src/core/iomgr/resolve_address.h"
  39. #include "src/core/iomgr/tcp_server.h"
  40. #include "src/core/security/auth_filters.h"
  41. #include "src/core/security/credentials.h"
  42. #include "src/core/security/security_connector.h"
  43. #include "src/core/security/secure_transport_setup.h"
  44. #include "src/core/surface/server.h"
  45. #include "src/core/transport/chttp2_transport.h"
  46. #include <grpc/support/alloc.h>
  47. #include <grpc/support/log.h>
  48. #include <grpc/support/sync.h>
  49. #include <grpc/support/useful.h>
  50. typedef struct tcp_endpoint_list {
  51. grpc_endpoint *tcp_endpoint;
  52. struct tcp_endpoint_list *next;
  53. } tcp_endpoint_list;
  54. typedef struct grpc_server_secure_state {
  55. grpc_server *server;
  56. grpc_tcp_server *tcp;
  57. grpc_security_connector *sc;
  58. tcp_endpoint_list *handshaking_tcp_endpoints;
  59. int is_shutdown;
  60. gpr_mu mu;
  61. gpr_refcount refcount;
  62. } grpc_server_secure_state;
  63. static void state_ref(grpc_server_secure_state *state) {
  64. gpr_ref(&state->refcount);
  65. }
  66. static void state_unref(grpc_server_secure_state *state) {
  67. if (gpr_unref(&state->refcount)) {
  68. /* ensure all threads have unlocked */
  69. gpr_mu_lock(&state->mu);
  70. gpr_mu_unlock(&state->mu);
  71. /* clean up */
  72. GRPC_SECURITY_CONNECTOR_UNREF(state->sc, "server");
  73. gpr_free(state);
  74. }
  75. }
  76. static void setup_transport(void *statep, grpc_transport *transport,
  77. grpc_mdctx *mdctx) {
  78. static grpc_channel_filter const *extra_filters[] = {
  79. &grpc_server_auth_filter, &grpc_http_server_filter};
  80. grpc_server_secure_state *state = statep;
  81. grpc_arg connector_arg = grpc_security_connector_to_arg(state->sc);
  82. grpc_channel_args *args_copy = grpc_channel_args_copy_and_add(
  83. grpc_server_get_channel_args(state->server), &connector_arg, 1);
  84. grpc_server_setup_transport(state->server, transport, extra_filters,
  85. GPR_ARRAY_SIZE(extra_filters), mdctx, args_copy);
  86. grpc_channel_args_destroy(args_copy);
  87. }
  88. static int remove_tcp_from_list_locked(grpc_server_secure_state *state,
  89. grpc_endpoint *tcp) {
  90. tcp_endpoint_list *node = state->handshaking_tcp_endpoints;
  91. tcp_endpoint_list *tmp = NULL;
  92. if (node && node->tcp_endpoint == tcp) {
  93. state->handshaking_tcp_endpoints = state->handshaking_tcp_endpoints->next;
  94. gpr_free(node);
  95. return 0;
  96. }
  97. while (node) {
  98. if (node->next->tcp_endpoint == tcp) {
  99. tmp = node->next;
  100. node->next = node->next->next;
  101. gpr_free(tmp);
  102. return 0;
  103. }
  104. node = node->next;
  105. }
  106. return -1;
  107. }
  108. static void on_secure_transport_setup_done(void *statep,
  109. grpc_security_status status,
  110. grpc_endpoint *wrapped_endpoint,
  111. grpc_endpoint *secure_endpoint) {
  112. grpc_server_secure_state *state = statep;
  113. grpc_transport *transport;
  114. grpc_mdctx *mdctx;
  115. if (status == GRPC_SECURITY_OK) {
  116. gpr_mu_lock(&state->mu);
  117. remove_tcp_from_list_locked(state, wrapped_endpoint);
  118. if (!state->is_shutdown) {
  119. mdctx = grpc_mdctx_create();
  120. transport = grpc_create_chttp2_transport(
  121. grpc_server_get_channel_args(state->server), secure_endpoint, mdctx,
  122. 0);
  123. setup_transport(state, transport, mdctx);
  124. grpc_chttp2_transport_start_reading(transport, NULL, 0);
  125. } else {
  126. /* We need to consume this here, because the server may already have gone
  127. * away. */
  128. grpc_endpoint_destroy(secure_endpoint);
  129. }
  130. gpr_mu_unlock(&state->mu);
  131. } else {
  132. gpr_mu_lock(&state->mu);
  133. remove_tcp_from_list_locked(state, wrapped_endpoint);
  134. gpr_mu_unlock(&state->mu);
  135. gpr_log(GPR_ERROR, "Secure transport failed with error %d", status);
  136. }
  137. state_unref(state);
  138. }
  139. static void on_accept(void *statep, grpc_endpoint *tcp) {
  140. grpc_server_secure_state *state = statep;
  141. tcp_endpoint_list *node;
  142. state_ref(state);
  143. node = gpr_malloc(sizeof(tcp_endpoint_list));
  144. node->tcp_endpoint = tcp;
  145. gpr_mu_lock(&state->mu);
  146. node->next = state->handshaking_tcp_endpoints;
  147. state->handshaking_tcp_endpoints = node;
  148. gpr_mu_unlock(&state->mu);
  149. grpc_setup_secure_transport(state->sc, tcp, on_secure_transport_setup_done,
  150. state);
  151. }
  152. /* Server callback: start listening on our ports */
  153. static void start(grpc_server *server, void *statep, grpc_pollset **pollsets,
  154. size_t pollset_count) {
  155. grpc_server_secure_state *state = statep;
  156. grpc_tcp_server_start(state->tcp, pollsets, pollset_count, on_accept, state);
  157. }
  158. static void destroy_done(void *statep) {
  159. grpc_server_secure_state *state = statep;
  160. grpc_server_listener_destroy_done(state->server);
  161. gpr_mu_lock(&state->mu);
  162. while (state->handshaking_tcp_endpoints != NULL) {
  163. grpc_endpoint_shutdown(state->handshaking_tcp_endpoints->tcp_endpoint);
  164. remove_tcp_from_list_locked(state,
  165. state->handshaking_tcp_endpoints->tcp_endpoint);
  166. }
  167. gpr_mu_unlock(&state->mu);
  168. state_unref(state);
  169. }
  170. /* Server callback: destroy the tcp listener (so we don't generate further
  171. callbacks) */
  172. static void destroy(grpc_server *server, void *statep) {
  173. grpc_server_secure_state *state = statep;
  174. grpc_tcp_server *tcp;
  175. gpr_mu_lock(&state->mu);
  176. state->is_shutdown = 1;
  177. tcp = state->tcp;
  178. gpr_mu_unlock(&state->mu);
  179. grpc_tcp_server_destroy(tcp, destroy_done, state);
  180. }
  181. int grpc_server_add_secure_http2_port(grpc_server *server, const char *addr,
  182. grpc_server_credentials *creds) {
  183. grpc_resolved_addresses *resolved = NULL;
  184. grpc_tcp_server *tcp = NULL;
  185. grpc_server_secure_state *state = NULL;
  186. size_t i;
  187. unsigned count = 0;
  188. int port_num = -1;
  189. int port_temp;
  190. grpc_security_status status = GRPC_SECURITY_ERROR;
  191. grpc_security_connector *sc = NULL;
  192. /* create security context */
  193. if (creds == NULL) goto error;
  194. status = grpc_server_credentials_create_security_connector(creds, &sc);
  195. if (status != GRPC_SECURITY_OK) {
  196. gpr_log(GPR_ERROR,
  197. "Unable to create secure server with credentials of type %s.",
  198. creds->type);
  199. goto error;
  200. }
  201. /* resolve address */
  202. resolved = grpc_blocking_resolve_address(addr, "https");
  203. if (!resolved) {
  204. goto error;
  205. }
  206. tcp = grpc_tcp_server_create();
  207. if (!tcp) {
  208. goto error;
  209. }
  210. for (i = 0; i < resolved->naddrs; i++) {
  211. port_temp = grpc_tcp_server_add_port(
  212. tcp, (struct sockaddr *)&resolved->addrs[i].addr,
  213. resolved->addrs[i].len);
  214. if (port_temp >= 0) {
  215. if (port_num == -1) {
  216. port_num = port_temp;
  217. } else {
  218. GPR_ASSERT(port_num == port_temp);
  219. }
  220. count++;
  221. }
  222. }
  223. if (count == 0) {
  224. gpr_log(GPR_ERROR, "No address added out of total %d resolved",
  225. resolved->naddrs);
  226. goto error;
  227. }
  228. if (count != resolved->naddrs) {
  229. gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
  230. count, resolved->naddrs);
  231. /* if it's an error, don't we want to goto error; here ? */
  232. }
  233. grpc_resolved_addresses_destroy(resolved);
  234. state = gpr_malloc(sizeof(*state));
  235. state->server = server;
  236. state->tcp = tcp;
  237. state->sc = sc;
  238. state->handshaking_tcp_endpoints = NULL;
  239. state->is_shutdown = 0;
  240. gpr_mu_init(&state->mu);
  241. gpr_ref_init(&state->refcount, 1);
  242. /* Register with the server only upon success */
  243. grpc_server_add_listener(server, state, start, destroy);
  244. return port_num;
  245. /* Error path: cleanup and return */
  246. error:
  247. if (sc) {
  248. GRPC_SECURITY_CONNECTOR_UNREF(sc, "server");
  249. }
  250. if (resolved) {
  251. grpc_resolved_addresses_destroy(resolved);
  252. }
  253. if (tcp) {
  254. grpc_tcp_server_destroy(tcp, NULL, NULL);
  255. }
  256. if (state) {
  257. gpr_free(state);
  258. }
  259. return 0;
  260. }