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