Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | a93a25f | 2016-01-28 13:55:49 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 94964fd | 2015-02-21 07:19:19 +0100 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
| 35 | #ifdef GPR_POSIX_SOCKET |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 37 | #include "src/core/iomgr/resolve_address.h" |
Craig Tiller | e1e4559 | 2016-03-11 08:01:58 -0800 | [diff] [blame] | 38 | #include "src/core/iomgr/sockaddr.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
Craig Tiller | e1e4559 | 2016-03-11 08:01:58 -0800 | [diff] [blame] | 40 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 41 | #include <sys/types.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | |
| 43 | #include <grpc/support/alloc.h> |
Nicolas "Pixel" Noble | 589cba2 | 2015-02-20 22:58:54 -0800 | [diff] [blame] | 44 | #include <grpc/support/host_port.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 46 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 47 | #include <grpc/support/thd.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 48 | #include <grpc/support/time.h> |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 49 | #include <grpc/support/useful.h> |
Craig Tiller | e1e4559 | 2016-03-11 08:01:58 -0800 | [diff] [blame] | 50 | #include "src/core/iomgr/executor.h" |
| 51 | #include "src/core/iomgr/iomgr_internal.h" |
| 52 | #include "src/core/iomgr/sockaddr_utils.h" |
ahedberg | 43df295 | 2016-03-18 10:46:38 -0400 | [diff] [blame] | 53 | #include "src/core/iomgr/unix_sockets_posix.h" |
Craig Tiller | e1e4559 | 2016-03-11 08:01:58 -0800 | [diff] [blame] | 54 | #include "src/core/support/block_annotate.h" |
| 55 | #include "src/core/support/string.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 56 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 57 | typedef struct { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 58 | char *name; |
| 59 | char *default_port; |
| 60 | grpc_resolve_cb cb; |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 61 | grpc_closure request_closure; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 62 | void *arg; |
| 63 | } request; |
| 64 | |
Craig Tiller | e1e4559 | 2016-03-11 08:01:58 -0800 | [diff] [blame] | 65 | static grpc_resolved_addresses *blocking_resolve_address_impl( |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 66 | const char *name, const char *default_port) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | struct addrinfo hints; |
| 68 | struct addrinfo *result = NULL, *resp; |
| 69 | char *host; |
| 70 | char *port; |
| 71 | int s; |
| 72 | size_t i; |
| 73 | grpc_resolved_addresses *addrs = NULL; |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 74 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 75 | if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' && |
| 76 | name[4] == ':' && name[5] != 0) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 77 | return grpc_resolve_unix_domain_address(name + 5); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 78 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 79 | |
| 80 | /* parse name, splitting it into host and port parts */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 81 | gpr_split_host_port(name, &host, &port); |
| 82 | if (host == NULL) { |
| 83 | gpr_log(GPR_ERROR, "unparseable host:port: '%s'", name); |
| 84 | goto done; |
| 85 | } |
| 86 | if (port == NULL) { |
| 87 | if (default_port == NULL) { |
| 88 | gpr_log(GPR_ERROR, "no port in name '%s'", name); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 89 | goto done; |
| 90 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 91 | port = gpr_strdup(default_port); |
| 92 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | |
| 94 | /* Call getaddrinfo */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 95 | memset(&hints, 0, sizeof(hints)); |
| 96 | hints.ai_family = AF_UNSPEC; /* ipv4 or ipv6 */ |
| 97 | hints.ai_socktype = SOCK_STREAM; /* stream socket */ |
| 98 | hints.ai_flags = AI_PASSIVE; /* for wildcard IP address */ |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 100 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 101 | s = getaddrinfo(host, port, &hints, &result); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 102 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
Vijay Pai | ba13055 | 2015-09-24 13:43:01 -0700 | [diff] [blame] | 103 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 104 | if (s != 0) { |
| 105 | /* Retry if well-known service name is recognized */ |
| 106 | char *svc[][2] = {{"http", "80"}, {"https", "443"}}; |
Craig Tiller | b9d3596 | 2015-09-11 13:31:16 -0700 | [diff] [blame] | 107 | for (i = 0; i < GPR_ARRAY_SIZE(svc); i++) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 108 | if (strcmp(port, svc[i][0]) == 0) { |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 109 | GRPC_SCHEDULING_START_BLOCKING_REGION; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 110 | s = getaddrinfo(host, svc[i][1], &hints, &result); |
vjpai | 9839d28 | 2015-09-24 17:55:18 -0700 | [diff] [blame] | 111 | GRPC_SCHEDULING_END_BLOCKING_REGION; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | break; |
| 113 | } |
Raul Silvera | 659be5a | 2015-03-04 11:51:11 -0800 | [diff] [blame] | 114 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 115 | } |
Raul Silvera | 659be5a | 2015-03-04 11:51:11 -0800 | [diff] [blame] | 116 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | if (s != 0) { |
| 118 | gpr_log(GPR_ERROR, "getaddrinfo: %s", gai_strerror(s)); |
| 119 | goto done; |
| 120 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 121 | |
| 122 | /* Success path: set addrs non-NULL, fill it in */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 124 | addrs->naddrs = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 125 | for (resp = result; resp != NULL; resp = resp->ai_next) { |
| 126 | addrs->naddrs++; |
| 127 | } |
| 128 | addrs->addrs = gpr_malloc(sizeof(grpc_resolved_address) * addrs->naddrs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 129 | i = 0; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 130 | for (resp = result; resp != NULL; resp = resp->ai_next) { |
| 131 | memcpy(&addrs->addrs[i].addr, resp->ai_addr, resp->ai_addrlen); |
| 132 | addrs->addrs[i].len = resp->ai_addrlen; |
| 133 | i++; |
| 134 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 136 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 137 | gpr_free(host); |
| 138 | gpr_free(port); |
| 139 | if (result) { |
| 140 | freeaddrinfo(result); |
| 141 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | return addrs; |
| 143 | } |
| 144 | |
Craig Tiller | e1e4559 | 2016-03-11 08:01:58 -0800 | [diff] [blame] | 145 | grpc_resolved_addresses *(*grpc_blocking_resolve_address)( |
| 146 | const char *name, const char *default_port) = blocking_resolve_address_impl; |
| 147 | |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 148 | /* Callback to be passed to grpc_executor to asynch-ify |
| 149 | * grpc_blocking_resolve_address */ |
Craig Tiller | 6c39686 | 2016-01-28 13:53:40 -0800 | [diff] [blame] | 150 | static void do_request_thread(grpc_exec_ctx *exec_ctx, void *rp, bool success) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 151 | request *r = rp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | grpc_resolved_addresses *resolved = |
| 153 | grpc_blocking_resolve_address(r->name, r->default_port); |
ctiller | ccd27fd | 2014-12-11 09:12:02 -0800 | [diff] [blame] | 154 | void *arg = r->arg; |
| 155 | grpc_resolve_cb cb = r->cb; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 156 | gpr_free(r->name); |
| 157 | gpr_free(r->default_port); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 158 | cb(exec_ctx, arg, resolved); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | gpr_free(r); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 162 | void grpc_resolved_addresses_destroy(grpc_resolved_addresses *addrs) { |
| 163 | gpr_free(addrs->addrs); |
| 164 | gpr_free(addrs); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 167 | void grpc_resolve_address(const char *name, const char *default_port, |
| 168 | grpc_resolve_cb cb, void *arg) { |
| 169 | request *r = gpr_malloc(sizeof(request)); |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 170 | grpc_closure_init(&r->request_closure, do_request_thread, r); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 171 | r->name = gpr_strdup(name); |
| 172 | r->default_port = gpr_strdup(default_port); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 173 | r->cb = cb; |
| 174 | r->arg = arg; |
David Garcia Quintas | 4bc3463 | 2015-10-07 16:12:35 -0700 | [diff] [blame] | 175 | grpc_executor_enqueue(&r->request_closure, 1); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 176 | } |
Nicolas "Pixel" Noble | 94964fd | 2015-02-21 07:19:19 +0100 | [diff] [blame] | 177 | |
| 178 | #endif |