3 * Copyright 2016 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/address_utils/parse_address.h"
25 #ifdef GRPC_HAVE_UNIX_SOCKET
28 #ifdef GRPC_POSIX_SOCKET
33 #include "absl/strings/str_cat.h"
34 #include "absl/strings/strip.h"
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
38 #include <grpc/support/string_util.h>
40 #include "src/core/lib/gpr/string.h"
41 #include "src/core/lib/gprpp/host_port.h"
42 #include "src/core/lib/iomgr/grpc_if_nametoindex.h"
43 #include "src/core/lib/iomgr/sockaddr.h"
44 #include "src/core/lib/iomgr/socket_utils.h"
46 #ifdef GRPC_HAVE_UNIX_SOCKET
48 bool grpc_parse_unix(const grpc_core::URI& uri,
49 grpc_resolved_address* resolved_addr) {
50 if (uri.scheme() != "unix") {
51 gpr_log(GPR_ERROR, "Expected 'unix' scheme, got '%s'",
52 uri.scheme().c_str());
55 grpc_error_handle error =
56 grpc_core::UnixSockaddrPopulate(uri.path(), resolved_addr);
57 if (error != GRPC_ERROR_NONE) {
58 gpr_log(GPR_ERROR, "%s", grpc_error_std_string(error).c_str());
59 GRPC_ERROR_UNREF(error);
65 bool grpc_parse_unix_abstract(const grpc_core::URI& uri,
66 grpc_resolved_address* resolved_addr) {
67 if (uri.scheme() != "unix-abstract") {
68 gpr_log(GPR_ERROR, "Expected 'unix-abstract' scheme, got '%s'",
69 uri.scheme().c_str());
72 grpc_error_handle error =
73 grpc_core::UnixAbstractSockaddrPopulate(uri.path(), resolved_addr);
74 if (error != GRPC_ERROR_NONE) {
75 gpr_log(GPR_ERROR, "%s", grpc_error_std_string(error).c_str());
76 GRPC_ERROR_UNREF(error);
84 grpc_error_handle UnixSockaddrPopulate(absl::string_view path,
85 grpc_resolved_address* resolved_addr) {
86 struct sockaddr_un* un =
87 reinterpret_cast<struct sockaddr_un*>(resolved_addr->addr);
88 const size_t maxlen = sizeof(un->sun_path) - 1;
89 if (path.size() > maxlen) {
90 return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
91 absl::StrCat("Path name should not have more than ", maxlen,
95 un->sun_family = AF_UNIX;
96 path.copy(un->sun_path, path.size());
97 un->sun_path[path.size()] = '\0';
98 resolved_addr->len = static_cast<socklen_t>(sizeof(*un));
99 return GRPC_ERROR_NONE;
102 grpc_error_handle UnixAbstractSockaddrPopulate(
103 absl::string_view path, grpc_resolved_address* resolved_addr) {
104 struct sockaddr_un* un =
105 reinterpret_cast<struct sockaddr_un*>(resolved_addr->addr);
106 const size_t maxlen = sizeof(un->sun_path) - 1;
107 if (path.size() > maxlen) {
108 return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
109 absl::StrCat("Path name should not have more than ", maxlen,
113 un->sun_family = AF_UNIX;
114 un->sun_path[0] = '\0';
115 path.copy(un->sun_path + 1, path.size());
117 static_cast<socklen_t>(sizeof(un->sun_family) + path.size() + 1);
118 return GRPC_ERROR_NONE;
121 } // namespace grpc_core
123 #else /* GRPC_HAVE_UNIX_SOCKET */
125 bool grpc_parse_unix(const grpc_core::URI& /* uri */,
126 grpc_resolved_address* /* resolved_addr */) {
130 bool grpc_parse_unix_abstract(const grpc_core::URI& /* uri */,
131 grpc_resolved_address* /* resolved_addr */) {
135 namespace grpc_core {
137 grpc_error_handle UnixSockaddrPopulate(
138 absl::string_view /* path */, grpc_resolved_address* /* resolved_addr */) {
142 grpc_error_handle UnixAbstractSockaddrPopulate(
143 absl::string_view /* path */, grpc_resolved_address* /* resolved_addr */) {
147 } // namespace grpc_core
148 #endif /* GRPC_HAVE_UNIX_SOCKET */
150 bool grpc_parse_ipv4_hostport(absl::string_view hostport,
151 grpc_resolved_address* addr, bool log_errors) {
152 bool success = false;
153 // Split host and port.
156 if (!grpc_core::SplitHostPort(hostport, &host, &port)) {
158 gpr_log(GPR_ERROR, "Failed gpr_split_host_port(%s, ...)",
159 std::string(hostport).c_str());
164 memset(addr, 0, sizeof(*addr));
165 addr->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in));
166 grpc_sockaddr_in* in = reinterpret_cast<grpc_sockaddr_in*>(addr->addr);
167 in->sin_family = GRPC_AF_INET;
168 if (grpc_inet_pton(GRPC_AF_INET, host.c_str(), &in->sin_addr) == 0) {
170 gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host.c_str());
176 if (log_errors) gpr_log(GPR_ERROR, "no port given for ipv4 scheme");
180 if (sscanf(port.c_str(), "%d", &port_num) != 1 || port_num < 0 ||
182 if (log_errors) gpr_log(GPR_ERROR, "invalid ipv4 port: '%s'", port.c_str());
185 in->sin_port = grpc_htons(static_cast<uint16_t>(port_num));
191 bool grpc_parse_ipv4(const grpc_core::URI& uri,
192 grpc_resolved_address* resolved_addr) {
193 if (uri.scheme() != "ipv4") {
194 gpr_log(GPR_ERROR, "Expected 'ipv4' scheme, got '%s'",
195 uri.scheme().c_str());
198 return grpc_parse_ipv4_hostport(absl::StripPrefix(uri.path(), "/"),
199 resolved_addr, true /* log_errors */);
202 bool grpc_parse_ipv6_hostport(absl::string_view hostport,
203 grpc_resolved_address* addr, bool log_errors) {
204 bool success = false;
205 // Split host and port.
208 if (!grpc_core::SplitHostPort(hostport, &host, &port)) {
210 gpr_log(GPR_ERROR, "Failed gpr_split_host_port(%s, ...)",
211 std::string(hostport).c_str());
216 memset(addr, 0, sizeof(*addr));
217 addr->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
218 grpc_sockaddr_in6* in6 = reinterpret_cast<grpc_sockaddr_in6*>(addr->addr);
219 in6->sin6_family = GRPC_AF_INET6;
220 // Handle the RFC6874 syntax for IPv6 zone identifiers.
222 static_cast<char*>(gpr_memrchr(host.c_str(), '%', host.size()));
223 if (host_end != nullptr) {
224 GPR_ASSERT(host_end >= host.c_str());
225 char host_without_scope[GRPC_INET6_ADDRSTRLEN + 1];
226 size_t host_without_scope_len =
227 static_cast<size_t>(host_end - host.c_str());
228 uint32_t sin6_scope_id = 0;
229 if (host_without_scope_len > GRPC_INET6_ADDRSTRLEN) {
233 "invalid ipv6 address length %zu. Length cannot be greater than "
234 "GRPC_INET6_ADDRSTRLEN i.e %d)",
235 host_without_scope_len, GRPC_INET6_ADDRSTRLEN);
239 strncpy(host_without_scope, host.c_str(), host_without_scope_len);
240 host_without_scope[host_without_scope_len] = '\0';
241 if (grpc_inet_pton(GRPC_AF_INET6, host_without_scope, &in6->sin6_addr) ==
244 gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host_without_scope);
248 if (gpr_parse_bytes_to_uint32(host_end + 1,
249 host.size() - host_without_scope_len - 1,
250 &sin6_scope_id) == 0) {
251 if ((sin6_scope_id = grpc_if_nametoindex(host_end + 1)) == 0) {
253 "Invalid interface name: '%s'. "
254 "Non-numeric and failed if_nametoindex.",
259 // Handle "sin6_scope_id" being type "u_long". See grpc issue #10027.
260 in6->sin6_scope_id = sin6_scope_id;
262 if (grpc_inet_pton(GRPC_AF_INET6, host.c_str(), &in6->sin6_addr) == 0) {
264 gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host.c_str());
271 if (log_errors) gpr_log(GPR_ERROR, "no port given for ipv6 scheme");
275 if (sscanf(port.c_str(), "%d", &port_num) != 1 || port_num < 0 ||
277 if (log_errors) gpr_log(GPR_ERROR, "invalid ipv6 port: '%s'", port.c_str());
280 in6->sin6_port = grpc_htons(static_cast<uint16_t>(port_num));
286 bool grpc_parse_ipv6(const grpc_core::URI& uri,
287 grpc_resolved_address* resolved_addr) {
288 if (uri.scheme() != "ipv6") {
289 gpr_log(GPR_ERROR, "Expected 'ipv6' scheme, got '%s'",
290 uri.scheme().c_str());
293 return grpc_parse_ipv6_hostport(absl::StripPrefix(uri.path(), "/"),
294 resolved_addr, true /* log_errors */);
297 bool grpc_parse_uri(const grpc_core::URI& uri,
298 grpc_resolved_address* resolved_addr) {
299 if (uri.scheme() == "unix") {
300 return grpc_parse_unix(uri, resolved_addr);
302 if (uri.scheme() == "unix-abstract") {
303 return grpc_parse_unix_abstract(uri, resolved_addr);
305 if (uri.scheme() == "ipv4") {
306 return grpc_parse_ipv4(uri, resolved_addr);
308 if (uri.scheme() == "ipv6") {
309 return grpc_parse_ipv6(uri, resolved_addr);
311 gpr_log(GPR_ERROR, "Can't parse scheme '%s'", uri.scheme().c_str());
315 uint16_t grpc_strhtons(const char* port) {
316 if (strcmp(port, "http") == 0) {
318 } else if (strcmp(port, "https") == 0) {
321 return htons(static_cast<unsigned short>(atoi(port)));