1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/sockaddr_utils.h"
22
23 #include <errno.h>
24 #include <inttypes.h>
25 #include <string.h>
26
27 #include <string>
28
29 #include "absl/strings/str_cat.h"
30 #include "absl/strings/str_format.h"
31
32 #include <grpc/support/alloc.h>
33 #include <grpc/support/log.h>
34
35 #include "src/core/lib/gpr/string.h"
36 #include "src/core/lib/gprpp/host_port.h"
37 #include "src/core/lib/iomgr/sockaddr.h"
38 #include "src/core/lib/iomgr/socket_utils.h"
39 #include "src/core/lib/iomgr/unix_sockets_posix.h"
40
41 static const uint8_t kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0xff, 0xff};
43
grpc_sockaddr_is_v4mapped(const grpc_resolved_address * resolved_addr,grpc_resolved_address * resolved_addr4_out)44 int grpc_sockaddr_is_v4mapped(const grpc_resolved_address* resolved_addr,
45 grpc_resolved_address* resolved_addr4_out) {
46 GPR_ASSERT(resolved_addr != resolved_addr4_out);
47 const grpc_sockaddr* addr =
48 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
49 grpc_sockaddr_in* addr4_out =
50 resolved_addr4_out == nullptr
51 ? nullptr
52 : reinterpret_cast<grpc_sockaddr_in*>(resolved_addr4_out->addr);
53 if (addr->sa_family == GRPC_AF_INET6) {
54 const grpc_sockaddr_in6* addr6 =
55 reinterpret_cast<const grpc_sockaddr_in6*>(addr);
56 if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix,
57 sizeof(kV4MappedPrefix)) == 0) {
58 if (resolved_addr4_out != nullptr) {
59 /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
60 memset(resolved_addr4_out, 0, sizeof(*resolved_addr4_out));
61 addr4_out->sin_family = GRPC_AF_INET;
62 /* s6_addr32 would be nice, but it's non-standard. */
63 memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4);
64 addr4_out->sin_port = addr6->sin6_port;
65 resolved_addr4_out->len =
66 static_cast<socklen_t>(sizeof(grpc_sockaddr_in));
67 }
68 return 1;
69 }
70 }
71 return 0;
72 }
73
grpc_sockaddr_to_v4mapped(const grpc_resolved_address * resolved_addr,grpc_resolved_address * resolved_addr6_out)74 int grpc_sockaddr_to_v4mapped(const grpc_resolved_address* resolved_addr,
75 grpc_resolved_address* resolved_addr6_out) {
76 GPR_ASSERT(resolved_addr != resolved_addr6_out);
77 const grpc_sockaddr* addr =
78 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
79 grpc_sockaddr_in6* addr6_out =
80 reinterpret_cast<grpc_sockaddr_in6*>(resolved_addr6_out->addr);
81 if (addr->sa_family == GRPC_AF_INET) {
82 const grpc_sockaddr_in* addr4 =
83 reinterpret_cast<const grpc_sockaddr_in*>(addr);
84 memset(resolved_addr6_out, 0, sizeof(*resolved_addr6_out));
85 addr6_out->sin6_family = GRPC_AF_INET6;
86 memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
87 memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
88 addr6_out->sin6_port = addr4->sin_port;
89 resolved_addr6_out->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
90 return 1;
91 }
92 return 0;
93 }
94
grpc_sockaddr_is_wildcard(const grpc_resolved_address * resolved_addr,int * port_out)95 int grpc_sockaddr_is_wildcard(const grpc_resolved_address* resolved_addr,
96 int* port_out) {
97 const grpc_sockaddr* addr;
98 grpc_resolved_address addr4_normalized;
99 if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr4_normalized)) {
100 resolved_addr = &addr4_normalized;
101 }
102 addr = reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
103 if (addr->sa_family == GRPC_AF_INET) {
104 /* Check for 0.0.0.0 */
105 const grpc_sockaddr_in* addr4 =
106 reinterpret_cast<const grpc_sockaddr_in*>(addr);
107 if (addr4->sin_addr.s_addr != 0) {
108 return 0;
109 }
110 *port_out = grpc_ntohs(addr4->sin_port);
111 return 1;
112 } else if (addr->sa_family == GRPC_AF_INET6) {
113 /* Check for :: */
114 const grpc_sockaddr_in6* addr6 =
115 reinterpret_cast<const grpc_sockaddr_in6*>(addr);
116 int i;
117 for (i = 0; i < 16; i++) {
118 if (addr6->sin6_addr.s6_addr[i] != 0) {
119 return 0;
120 }
121 }
122 *port_out = grpc_ntohs(addr6->sin6_port);
123 return 1;
124 } else {
125 return 0;
126 }
127 }
128
grpc_sockaddr_make_wildcards(int port,grpc_resolved_address * wild4_out,grpc_resolved_address * wild6_out)129 void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address* wild4_out,
130 grpc_resolved_address* wild6_out) {
131 grpc_sockaddr_make_wildcard4(port, wild4_out);
132 grpc_sockaddr_make_wildcard6(port, wild6_out);
133 }
134
grpc_sockaddr_make_wildcard4(int port,grpc_resolved_address * resolved_wild_out)135 void grpc_sockaddr_make_wildcard4(int port,
136 grpc_resolved_address* resolved_wild_out) {
137 grpc_sockaddr_in* wild_out =
138 reinterpret_cast<grpc_sockaddr_in*>(resolved_wild_out->addr);
139 GPR_ASSERT(port >= 0 && port < 65536);
140 memset(resolved_wild_out, 0, sizeof(*resolved_wild_out));
141 wild_out->sin_family = GRPC_AF_INET;
142 wild_out->sin_port = grpc_htons(static_cast<uint16_t>(port));
143 resolved_wild_out->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in));
144 }
145
grpc_sockaddr_make_wildcard6(int port,grpc_resolved_address * resolved_wild_out)146 void grpc_sockaddr_make_wildcard6(int port,
147 grpc_resolved_address* resolved_wild_out) {
148 grpc_sockaddr_in6* wild_out =
149 reinterpret_cast<grpc_sockaddr_in6*>(resolved_wild_out->addr);
150 GPR_ASSERT(port >= 0 && port < 65536);
151 memset(resolved_wild_out, 0, sizeof(*resolved_wild_out));
152 wild_out->sin6_family = GRPC_AF_INET6;
153 wild_out->sin6_port = grpc_htons(static_cast<uint16_t>(port));
154 resolved_wild_out->len = static_cast<socklen_t>(sizeof(grpc_sockaddr_in6));
155 }
156
grpc_sockaddr_to_string(const grpc_resolved_address * resolved_addr,bool normalize)157 std::string grpc_sockaddr_to_string(const grpc_resolved_address* resolved_addr,
158 bool normalize) {
159 const int save_errno = errno;
160 grpc_resolved_address addr_normalized;
161 if (normalize && grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) {
162 resolved_addr = &addr_normalized;
163 }
164 const grpc_sockaddr* addr =
165 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
166 const void* ip = nullptr;
167 int port = 0;
168 uint32_t sin6_scope_id = 0;
169 if (addr->sa_family == GRPC_AF_INET) {
170 const grpc_sockaddr_in* addr4 =
171 reinterpret_cast<const grpc_sockaddr_in*>(addr);
172 ip = &addr4->sin_addr;
173 port = grpc_ntohs(addr4->sin_port);
174 } else if (addr->sa_family == GRPC_AF_INET6) {
175 const grpc_sockaddr_in6* addr6 =
176 reinterpret_cast<const grpc_sockaddr_in6*>(addr);
177 ip = &addr6->sin6_addr;
178 port = grpc_ntohs(addr6->sin6_port);
179 sin6_scope_id = addr6->sin6_scope_id;
180 }
181 char ntop_buf[GRPC_INET6_ADDRSTRLEN];
182 std::string out;
183 if (ip != nullptr && grpc_inet_ntop(addr->sa_family, ip, ntop_buf,
184 sizeof(ntop_buf)) != nullptr) {
185 if (sin6_scope_id != 0) {
186 // Enclose sin6_scope_id with the format defined in RFC 6874 section 2.
187 std::string host_with_scope =
188 absl::StrFormat("%s%%25%" PRIu32, ntop_buf, sin6_scope_id);
189 out = grpc_core::JoinHostPort(host_with_scope, port);
190 } else {
191 out = grpc_core::JoinHostPort(ntop_buf, port);
192 }
193 } else {
194 out = absl::StrFormat("(sockaddr family=%d)", addr->sa_family);
195 }
196 /* This is probably redundant, but we wouldn't want to log the wrong error. */
197 errno = save_errno;
198 return out;
199 }
200
grpc_string_to_sockaddr(grpc_resolved_address * out,const char * addr,int port)201 void grpc_string_to_sockaddr(grpc_resolved_address* out, const char* addr,
202 int port) {
203 memset(out, 0, sizeof(grpc_resolved_address));
204 grpc_sockaddr_in6* addr6 = reinterpret_cast<grpc_sockaddr_in6*>(out->addr);
205 grpc_sockaddr_in* addr4 = reinterpret_cast<grpc_sockaddr_in*>(out->addr);
206 if (grpc_inet_pton(GRPC_AF_INET6, addr, &addr6->sin6_addr) == 1) {
207 addr6->sin6_family = GRPC_AF_INET6;
208 out->len = sizeof(grpc_sockaddr_in6);
209 } else if (grpc_inet_pton(GRPC_AF_INET, addr, &addr4->sin_addr) == 1) {
210 addr4->sin_family = GRPC_AF_INET;
211 out->len = sizeof(grpc_sockaddr_in);
212 } else {
213 GPR_ASSERT(0);
214 }
215 grpc_sockaddr_set_port(out, port);
216 }
217
grpc_sockaddr_to_uri(const grpc_resolved_address * resolved_addr)218 std::string grpc_sockaddr_to_uri(const grpc_resolved_address* resolved_addr) {
219 if (resolved_addr->len == 0) return "";
220 grpc_resolved_address addr_normalized;
221 if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) {
222 resolved_addr = &addr_normalized;
223 }
224 const char* scheme = grpc_sockaddr_get_uri_scheme(resolved_addr);
225 if (scheme == nullptr || strcmp("unix", scheme) == 0) {
226 return grpc_sockaddr_to_uri_unix_if_possible(resolved_addr);
227 }
228 std::string path =
229 grpc_sockaddr_to_string(resolved_addr, false /* normalize */);
230 std::string uri_str;
231 if (scheme != nullptr) {
232 uri_str = absl::StrCat(scheme, ":", path);
233 }
234 return uri_str;
235 }
236
grpc_sockaddr_get_uri_scheme(const grpc_resolved_address * resolved_addr)237 const char* grpc_sockaddr_get_uri_scheme(
238 const grpc_resolved_address* resolved_addr) {
239 const grpc_sockaddr* addr =
240 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
241 switch (addr->sa_family) {
242 case GRPC_AF_INET:
243 return "ipv4";
244 case GRPC_AF_INET6:
245 return "ipv6";
246 case GRPC_AF_UNIX:
247 return "unix";
248 }
249 return nullptr;
250 }
251
grpc_sockaddr_get_family(const grpc_resolved_address * resolved_addr)252 int grpc_sockaddr_get_family(const grpc_resolved_address* resolved_addr) {
253 const grpc_sockaddr* addr =
254 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
255 return addr->sa_family;
256 }
257
grpc_sockaddr_get_port(const grpc_resolved_address * resolved_addr)258 int grpc_sockaddr_get_port(const grpc_resolved_address* resolved_addr) {
259 const grpc_sockaddr* addr =
260 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
261 switch (addr->sa_family) {
262 case GRPC_AF_INET:
263 return grpc_ntohs(
264 (reinterpret_cast<const grpc_sockaddr_in*>(addr))->sin_port);
265 case GRPC_AF_INET6:
266 return grpc_ntohs(
267 (reinterpret_cast<const grpc_sockaddr_in6*>(addr))->sin6_port);
268 default:
269 if (grpc_is_unix_socket(resolved_addr)) {
270 return 1;
271 }
272 gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port",
273 addr->sa_family);
274 return 0;
275 }
276 }
277
grpc_sockaddr_set_port(grpc_resolved_address * resolved_addr,int port)278 int grpc_sockaddr_set_port(grpc_resolved_address* resolved_addr, int port) {
279 grpc_sockaddr* addr = reinterpret_cast<grpc_sockaddr*>(resolved_addr->addr);
280 switch (addr->sa_family) {
281 case GRPC_AF_INET:
282 GPR_ASSERT(port >= 0 && port < 65536);
283 (reinterpret_cast<grpc_sockaddr_in*>(addr))->sin_port =
284 grpc_htons(static_cast<uint16_t>(port));
285 return 1;
286 case GRPC_AF_INET6:
287 GPR_ASSERT(port >= 0 && port < 65536);
288 (reinterpret_cast<grpc_sockaddr_in6*>(addr))->sin6_port =
289 grpc_htons(static_cast<uint16_t>(port));
290 return 1;
291 default:
292 gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port",
293 addr->sa_family);
294 return 0;
295 }
296 }
297