blob: a3c3a874c1094b9898013be9e3aba709fdd338d7 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
ahedberg8afb88d2016-03-22 13:57:47 -04003 * Copyright 2016, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * 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
ctiller18b49ab2014-12-09 14:39:16 -080034#include "src/core/iomgr/sockaddr_utils.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080035
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036#include <errno.h>
ctiller18b49ab2014-12-09 14:39:16 -080037#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038
Craig Tiller1b22b9d2015-07-20 13:42:22 -070039#include <grpc/support/alloc.h>
nnoble0c475f02014-12-05 15:37:39 -080040#include <grpc/support/host_port.h>
nnoble0c475f02014-12-05 15:37:39 -080041#include <grpc/support/log.h>
42#include <grpc/support/port_platform.h>
Masood Malekghassemi701af602015-06-03 15:01:17 -070043#include <grpc/support/string_util.h>
nnoble0c475f02014-12-05 15:37:39 -080044
ahedberg80d6b122016-03-17 17:37:35 -040045#include "src/core/iomgr/unix_sockets_posix.h"
Craig Tiller1b22b9d2015-07-20 13:42:22 -070046#include "src/core/support/string.h"
47
Craig Tiller7536af02015-12-22 13:49:30 -080048static const uint8_t kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0xff, 0xff};
nnoble0c475f02014-12-05 15:37:39 -080050
51int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
52 struct sockaddr_in *addr4_out) {
53 GPR_ASSERT(addr != (struct sockaddr *)addr4_out);
54 if (addr->sa_family == AF_INET6) {
55 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
56 if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix,
57 sizeof(kV4MappedPrefix)) == 0) {
58 if (addr4_out != NULL) {
59 /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
60 memset(addr4_out, 0, sizeof(*addr4_out));
61 addr4_out->sin_family = 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 }
66 return 1;
67 }
68 }
69 return 0;
70}
71
72int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
73 struct sockaddr_in6 *addr6_out) {
74 GPR_ASSERT(addr != (struct sockaddr *)addr6_out);
75 if (addr->sa_family == AF_INET) {
76 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
77 memset(addr6_out, 0, sizeof(*addr6_out));
78 addr6_out->sin6_family = AF_INET6;
79 memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
80 memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
81 addr6_out->sin6_port = addr4->sin_port;
82 return 1;
83 }
84 return 0;
85}
86
87int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) {
88 struct sockaddr_in addr4_normalized;
89 if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) {
90 addr = (struct sockaddr *)&addr4_normalized;
91 }
92 if (addr->sa_family == AF_INET) {
93 /* Check for 0.0.0.0 */
94 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
95 if (addr4->sin_addr.s_addr != 0) {
96 return 0;
97 }
98 *port_out = ntohs(addr4->sin_port);
99 return 1;
100 } else if (addr->sa_family == AF_INET6) {
101 /* Check for :: */
102 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
103 int i;
104 for (i = 0; i < 16; i++) {
105 if (addr6->sin6_addr.s6_addr[i] != 0) {
106 return 0;
107 }
108 }
109 *port_out = ntohs(addr6->sin6_port);
110 return 1;
111 } else {
112 return 0;
113 }
114}
115
116void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
117 struct sockaddr_in6 *wild6_out) {
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100118 grpc_sockaddr_make_wildcard4(port, wild4_out);
119 grpc_sockaddr_make_wildcard6(port, wild6_out);
120}
nnoble0c475f02014-12-05 15:37:39 -0800121
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100122void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) {
Craig Tiller6a6b36c2015-09-10 16:00:22 -0700123 GPR_ASSERT(port >= 0 && port < 65536);
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100124 memset(wild_out, 0, sizeof(*wild_out));
125 wild_out->sin_family = AF_INET;
Craig Tiller7536af02015-12-22 13:49:30 -0800126 wild_out->sin_port = htons((uint16_t)port);
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100127}
128
129void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) {
Craig Tiller6a6b36c2015-09-10 16:00:22 -0700130 GPR_ASSERT(port >= 0 && port < 65536);
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100131 memset(wild_out, 0, sizeof(*wild_out));
132 wild_out->sin6_family = AF_INET6;
Craig Tiller7536af02015-12-22 13:49:30 -0800133 wild_out->sin6_port = htons((uint16_t)port);
nnoble0c475f02014-12-05 15:37:39 -0800134}
135
136int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
137 int normalize) {
138 const int save_errno = errno;
139 struct sockaddr_in addr_normalized;
140 char ntop_buf[INET6_ADDRSTRLEN];
141 const void *ip = NULL;
142 int port;
143 int ret;
144
145 *out = NULL;
146 if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
147 addr = (const struct sockaddr *)&addr_normalized;
148 }
149 if (addr->sa_family == AF_INET) {
150 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
151 ip = &addr4->sin_addr;
152 port = ntohs(addr4->sin_port);
153 } else if (addr->sa_family == AF_INET6) {
154 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
155 ip = &addr6->sin6_addr;
156 port = ntohs(addr6->sin6_port);
157 }
Craig Tiller82f9bd82015-09-23 09:31:51 -0700158 /* Windows inet_ntop wants a mutable ip pointer */
nnoble0c475f02014-12-05 15:37:39 -0800159 if (ip != NULL &&
Craig Tiller565b18b2015-09-23 10:09:42 -0700160 inet_ntop(addr->sa_family, (void *)ip, ntop_buf, sizeof(ntop_buf)) !=
161 NULL) {
nnoble0c475f02014-12-05 15:37:39 -0800162 ret = gpr_join_host_port(out, ntop_buf, port);
163 } else {
164 ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family);
165 }
166 /* This is probably redundant, but we wouldn't want to log the wrong error. */
167 errno = save_errno;
168 return ret;
169}
ctiller570d1f42015-01-12 16:29:52 -0800170
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700171char *grpc_sockaddr_to_uri(const struct sockaddr *addr) {
172 char *temp;
173 char *result;
Paul Marks63541a12015-08-04 15:05:00 -0700174 struct sockaddr_in addr_normalized;
175
176 if (grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
177 addr = (const struct sockaddr *)&addr_normalized;
178 }
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700179
180 switch (addr->sa_family) {
181 case AF_INET:
182 grpc_sockaddr_to_string(&temp, addr, 0);
183 gpr_asprintf(&result, "ipv4:%s", temp);
184 gpr_free(temp);
185 return result;
186 case AF_INET6:
187 grpc_sockaddr_to_string(&temp, addr, 0);
188 gpr_asprintf(&result, "ipv6:%s", temp);
189 gpr_free(temp);
190 return result;
ahedberg80d6b122016-03-17 17:37:35 -0400191 default:
192 return grpc_sockaddr_to_uri_unix_if_possible(addr);
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700193 }
Craig Tiller1b22b9d2015-07-20 13:42:22 -0700194}
195
ctiller570d1f42015-01-12 16:29:52 -0800196int grpc_sockaddr_get_port(const struct sockaddr *addr) {
197 switch (addr->sa_family) {
198 case AF_INET:
199 return ntohs(((struct sockaddr_in *)addr)->sin_port);
200 case AF_INET6:
201 return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
202 default:
ahedberg43df2952016-03-18 10:46:38 -0400203 if (grpc_is_unix_socket(addr)) {
ahedberg80d6b122016-03-17 17:37:35 -0400204 return 1;
205 }
Craig Tillerd6c98df2015-08-18 09:33:44 -0700206 gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port",
207 addr->sa_family);
ctiller570d1f42015-01-12 16:29:52 -0800208 return 0;
209 }
210}
211
212int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
213 switch (addr->sa_family) {
214 case AF_INET:
Craig Tiller6a6b36c2015-09-10 16:00:22 -0700215 GPR_ASSERT(port >= 0 && port < 65536);
Craig Tiller7536af02015-12-22 13:49:30 -0800216 ((struct sockaddr_in *)addr)->sin_port = htons((uint16_t)port);
ctiller570d1f42015-01-12 16:29:52 -0800217 return 1;
218 case AF_INET6:
Craig Tiller6a6b36c2015-09-10 16:00:22 -0700219 GPR_ASSERT(port >= 0 && port < 65536);
Craig Tiller7536af02015-12-22 13:49:30 -0800220 ((struct sockaddr_in6 *)addr)->sin6_port = htons((uint16_t)port);
ctiller570d1f42015-01-12 16:29:52 -0800221 return 1;
222 default:
Craig Tillerd6c98df2015-08-18 09:33:44 -0700223 gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port",
224 addr->sa_family);
ctiller570d1f42015-01-12 16:29:52 -0800225 return 0;
226 }
Craig Tiller190d3602015-02-18 09:23:38 -0800227}