blob: aa4eb9b05b1f5e2d563a0d7013fe1598244a154c [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
34#include <grpc/grpc.h>
35
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include "src/core/channel/http_server_filter.h"
ctiller18b49ab2014-12-09 14:39:16 -080037#include "src/core/iomgr/resolve_address.h"
38#include "src/core/iomgr/tcp_server.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039#include "src/core/surface/server.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080040#include "src/core/transport/chttp2_transport.h"
41#include <grpc/support/alloc.h>
42#include <grpc/support/log.h>
43#include <grpc/support/useful.h>
44
Craig Tiller45724b32015-09-22 10:42:19 -070045static void
Craig Tiller1be70cc2015-09-22 10:45:28 -070046setup_transport (grpc_exec_ctx * exec_ctx, void *server, grpc_transport * transport, grpc_mdctx * mdctx)
Craig Tiller45724b32015-09-22 10:42:19 -070047{
Craig Tiller06aeea72015-04-23 10:54:45 -070048 static grpc_channel_filter const *extra_filters[] = {
Craig Tiller45724b32015-09-22 10:42:19 -070049 &grpc_http_server_filter
50 };
51 grpc_server_setup_transport (server, transport, extra_filters, GPR_ARRAY_SIZE (extra_filters), mdctx, grpc_server_get_channel_args (server), closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080052}
53
Craig Tiller45724b32015-09-22 10:42:19 -070054static void
Craig Tiller1be70cc2015-09-22 10:45:28 -070055new_transport (grpc_exec_ctx * exec_ctx, void *server, grpc_endpoint * tcp)
Craig Tiller45724b32015-09-22 10:42:19 -070056{
David Klempnerfd5d8ff2015-03-05 14:17:38 -080057 /*
58 * Beware that the call to grpc_create_chttp2_transport() has to happen before
59 * grpc_tcp_server_destroy(). This is fine here, but similar code
60 * asynchronously doing a handshake instead of calling grpc_tcp_server_start()
61 * (as in server_secure_chttp2.c) needs to add synchronization to avoid this
62 * case.
63 */
Craig Tiller45724b32015-09-22 10:42:19 -070064 grpc_mdctx *mdctx = grpc_mdctx_create ();
65 grpc_transport *transport = grpc_create_chttp2_transport (grpc_server_get_channel_args (server), tcp, mdctx, 0, closure_list);
66 setup_transport (server, transport, mdctx, closure_list);
67 grpc_chttp2_transport_start_reading (transport, NULL, 0, closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068}
69
70/* Server callback: start listening on our ports */
Craig Tiller45724b32015-09-22 10:42:19 -070071static void
Craig Tiller1be70cc2015-09-22 10:45:28 -070072start (grpc_exec_ctx * exec_ctx, grpc_server * server, void *tcpp, grpc_pollset ** pollsets, size_t pollset_count)
Craig Tiller45724b32015-09-22 10:42:19 -070073{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080074 grpc_tcp_server *tcp = tcpp;
Craig Tiller45724b32015-09-22 10:42:19 -070075 grpc_tcp_server_start (tcp, pollsets, pollset_count, new_transport, server, closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080076}
77
78/* Server callback: destroy the tcp listener (so we don't generate further
79 callbacks) */
Craig Tiller45724b32015-09-22 10:42:19 -070080static void
Craig Tiller1be70cc2015-09-22 10:45:28 -070081destroy (grpc_exec_ctx * exec_ctx, grpc_server * server, void *tcpp, grpc_closure * destroy_done)
Craig Tiller45724b32015-09-22 10:42:19 -070082{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 grpc_tcp_server *tcp = tcpp;
Craig Tiller45724b32015-09-22 10:42:19 -070084 grpc_tcp_server_destroy (tcp, destroy_done, closure_list);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080085}
86
Craig Tiller45724b32015-09-22 10:42:19 -070087int
88grpc_server_add_insecure_http2_port (grpc_server * server, const char *addr)
89{
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080090 grpc_resolved_addresses *resolved = NULL;
91 grpc_tcp_server *tcp = NULL;
92 size_t i;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010093 unsigned count = 0;
ctiller570d1f42015-01-12 16:29:52 -080094 int port_num = -1;
95 int port_temp;
Craig Tillerd9ccbbf2015-09-22 09:30:00 -070096 grpc_closure_list closure_list = GRPC_CLOSURE_LIST_INIT;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097
Craig Tiller45724b32015-09-22 10:42:19 -070098 resolved = grpc_blocking_resolve_address (addr, "http");
99 if (!resolved)
100 {
101 goto error;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102 }
Craig Tiller45724b32015-09-22 10:42:19 -0700103
104 tcp = grpc_tcp_server_create ();
105 if (!tcp)
106 {
107 goto error;
108 }
109
110 for (i = 0; i < resolved->naddrs; i++)
111 {
112 port_temp = grpc_tcp_server_add_port (tcp, (struct sockaddr *) &resolved->addrs[i].addr, resolved->addrs[i].len);
113 if (port_temp >= 0)
114 {
115 if (port_num == -1)
116 {
117 port_num = port_temp;
118 }
119 else
120 {
121 GPR_ASSERT (port_num == port_temp);
122 }
123 count++;
124 }
125 }
126 if (count == 0)
127 {
128 gpr_log (GPR_ERROR, "No address added out of total %d resolved", resolved->naddrs);
129 goto error;
130 }
131 if (count != resolved->naddrs)
132 {
133 gpr_log (GPR_ERROR, "Only %d addresses added out of total %d resolved", count, resolved->naddrs);
134 }
135 grpc_resolved_addresses_destroy (resolved);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800136
137 /* Register with the server only upon success */
Craig Tiller45724b32015-09-22 10:42:19 -0700138 grpc_server_add_listener (server, tcp, start, destroy, &closure_list);
Craig Tillerdfff1b82015-09-21 14:39:57 -0700139 goto done;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800140
141/* Error path: cleanup and return */
142error:
Craig Tiller45724b32015-09-22 10:42:19 -0700143 if (resolved)
144 {
145 grpc_resolved_addresses_destroy (resolved);
146 }
147 if (tcp)
148 {
149 grpc_tcp_server_destroy (tcp, NULL, NULL);
150 }
Craig Tillerdfff1b82015-09-21 14:39:57 -0700151 port_num = 0;
152
153done:
Craig Tiller45724b32015-09-22 10:42:19 -0700154 grpc_closure_list_run (&closure_list);
Craig Tillerba496452015-09-21 17:15:19 -0700155 return port_num;
Craig Tiller190d3602015-02-18 09:23:38 -0800156}