Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | |
| 34 | #include <grpc/grpc.h> |
| 35 | |
| 36 | #include "src/core/channel/http_filter.h" |
| 37 | #include "src/core/channel/http_server_filter.h" |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/resolve_address.h" |
| 39 | #include "src/core/iomgr/tcp_server.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 40 | #include "src/core/surface/server.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include "src/core/transport/chttp2_transport.h" |
| 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/log.h> |
| 44 | #include <grpc/support/useful.h> |
| 45 | |
| 46 | static grpc_transport_setup_result setup_transport(void *server, |
| 47 | grpc_transport *transport, |
| 48 | grpc_mdctx *mdctx) { |
| 49 | static grpc_channel_filter const *extra_filters[] = {&grpc_http_server_filter, |
| 50 | &grpc_http_filter}; |
| 51 | return grpc_server_setup_transport(server, transport, extra_filters, |
| 52 | GPR_ARRAY_SIZE(extra_filters), mdctx); |
| 53 | } |
| 54 | |
| 55 | static void new_transport(void *server, grpc_endpoint *tcp) { |
| 56 | grpc_create_chttp2_transport(setup_transport, server, |
| 57 | grpc_server_get_channel_args(server), tcp, NULL, |
| 58 | 0, grpc_mdctx_create(), 0); |
| 59 | } |
| 60 | |
| 61 | /* Server callback: start listening on our ports */ |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 62 | static void start(grpc_server *server, void *tcpp, grpc_pollset *pollset) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | grpc_tcp_server *tcp = tcpp; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 64 | grpc_tcp_server_start(tcp, pollset, new_transport, server); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* Server callback: destroy the tcp listener (so we don't generate further |
| 68 | callbacks) */ |
| 69 | static void destroy(grpc_server *server, void *tcpp) { |
| 70 | grpc_tcp_server *tcp = tcpp; |
| 71 | grpc_tcp_server_destroy(tcp); |
| 72 | } |
| 73 | |
| 74 | int grpc_server_add_http2_port(grpc_server *server, const char *addr) { |
| 75 | grpc_resolved_addresses *resolved = NULL; |
| 76 | grpc_tcp_server *tcp = NULL; |
| 77 | size_t i; |
Nicolas "Pixel" Noble | 213ed91 | 2015-01-30 02:11:35 +0100 | [diff] [blame^] | 78 | unsigned count = 0; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 79 | int port_num = -1; |
| 80 | int port_temp; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 81 | |
| 82 | resolved = grpc_blocking_resolve_address(addr, "http"); |
| 83 | if (!resolved) { |
| 84 | goto error; |
| 85 | } |
| 86 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 87 | tcp = grpc_tcp_server_create(); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 88 | if (!tcp) { |
| 89 | goto error; |
| 90 | } |
| 91 | |
| 92 | for (i = 0; i < resolved->naddrs; i++) { |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 93 | port_temp = grpc_tcp_server_add_port( |
| 94 | tcp, (struct sockaddr *)&resolved->addrs[i].addr, |
| 95 | resolved->addrs[i].len); |
| 96 | if (port_temp >= 0) { |
| 97 | if (port_num == -1) { |
| 98 | port_num = port_temp; |
| 99 | } else { |
| 100 | GPR_ASSERT(port_num == port_temp); |
| 101 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | count++; |
| 103 | } |
| 104 | } |
| 105 | if (count == 0) { |
| 106 | gpr_log(GPR_ERROR, "No address added out of total %d resolved", |
| 107 | resolved->naddrs); |
| 108 | goto error; |
| 109 | } |
| 110 | if (count != resolved->naddrs) { |
| 111 | gpr_log(GPR_ERROR, "Only %d addresses added out of total %d resolved", |
| 112 | count, resolved->naddrs); |
| 113 | } |
| 114 | grpc_resolved_addresses_destroy(resolved); |
| 115 | |
| 116 | /* Register with the server only upon success */ |
| 117 | grpc_server_add_listener(server, tcp, start, destroy); |
| 118 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 119 | return port_num; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 120 | |
| 121 | /* Error path: cleanup and return */ |
| 122 | error: |
| 123 | if (resolved) { |
| 124 | grpc_resolved_addresses_destroy(resolved); |
| 125 | } |
| 126 | if (tcp) { |
| 127 | grpc_tcp_server_destroy(tcp); |
| 128 | } |
| 129 | return 0; |
| 130 | } |