blob: 7b5c2f227b3425bbcda3b7191cdf77e46082bb51 [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
45static grpc_transport_setup_result setup_transport(void *server,
46 grpc_transport *transport,
47 grpc_mdctx *mdctx) {
Craig Tiller06aeea72015-04-23 10:54:45 -070048 static grpc_channel_filter const *extra_filters[] = {
49 &grpc_http_server_filter};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050 return grpc_server_setup_transport(server, transport, extra_filters,
51 GPR_ARRAY_SIZE(extra_filters), mdctx);
52}
53
54static void new_transport(void *server, grpc_endpoint *tcp) {
David Klempnerfd5d8ff2015-03-05 14:17:38 -080055 /*
56 * Beware that the call to grpc_create_chttp2_transport() has to happen before
57 * grpc_tcp_server_destroy(). This is fine here, but similar code
58 * asynchronously doing a handshake instead of calling grpc_tcp_server_start()
59 * (as in server_secure_chttp2.c) needs to add synchronization to avoid this
60 * case.
61 */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062 grpc_create_chttp2_transport(setup_transport, server,
63 grpc_server_get_channel_args(server), tcp, NULL,
64 0, grpc_mdctx_create(), 0);
65}
66
67/* Server callback: start listening on our ports */
Craig Tillerc02c1d82015-04-07 16:21:55 -070068static void start(grpc_server *server, void *tcpp, grpc_pollset **pollsets,
69 size_t pollset_count) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070 grpc_tcp_server *tcp = tcpp;
Craig Tiller20bc56d2015-02-12 09:02:56 -080071 grpc_tcp_server_start(tcp, pollsets, pollset_count, new_transport, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080072}
73
74/* Server callback: destroy the tcp listener (so we don't generate further
75 callbacks) */
76static void destroy(grpc_server *server, void *tcpp) {
77 grpc_tcp_server *tcp = tcpp;
Craig Tilleraec96aa2015-04-07 14:32:15 -070078 grpc_tcp_server_destroy(tcp, grpc_server_listener_destroy_done, server);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080079}
80
81int grpc_server_add_http2_port(grpc_server *server, const char *addr) {
82 grpc_resolved_addresses *resolved = NULL;
83 grpc_tcp_server *tcp = NULL;
84 size_t i;
Nicolas "Pixel" Noble213ed912015-01-30 02:11:35 +010085 unsigned count = 0;
ctiller570d1f42015-01-12 16:29:52 -080086 int port_num = -1;
87 int port_temp;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080088
89 resolved = grpc_blocking_resolve_address(addr, "http");
90 if (!resolved) {
91 goto error;
92 }
93
ctiller18b49ab2014-12-09 14:39:16 -080094 tcp = grpc_tcp_server_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080095 if (!tcp) {
96 goto error;
97 }
98
99 for (i = 0; i < resolved->naddrs; i++) {
ctiller570d1f42015-01-12 16:29:52 -0800100 port_temp = grpc_tcp_server_add_port(
101 tcp, (struct sockaddr *)&resolved->addrs[i].addr,
102 resolved->addrs[i].len);
103 if (port_temp >= 0) {
104 if (port_num == -1) {
105 port_num = port_temp;
106 } else {
107 GPR_ASSERT(port_num == port_temp);
108 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800109 count++;
110 }
111 }
112 if (count == 0) {
113 gpr_log(GPR_ERROR, "No address added out of total %d resolved",
114 resolved->naddrs);
115 goto error;
116 }
117 if (count != resolved->naddrs) {
118 gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved",
119 count, resolved->naddrs);
120 }
121 grpc_resolved_addresses_destroy(resolved);
122
123 /* Register with the server only upon success */
124 grpc_server_add_listener(server, tcp, start, destroy);
125
ctiller570d1f42015-01-12 16:29:52 -0800126 return port_num;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800127
128/* Error path: cleanup and return */
129error:
130 if (resolved) {
131 grpc_resolved_addresses_destroy(resolved);
132 }
133 if (tcp) {
Craig Tilleraec96aa2015-04-07 14:32:15 -0700134 grpc_tcp_server_destroy(tcp, NULL, NULL);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135 }
136 return 0;
Craig Tiller190d3602015-02-18 09:23:38 -0800137}