blob: e91b94f8c81e580ef3c3a7766bad2854d3fd248a [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, 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 Tiller485d7762015-01-23 12:54:05 -080039#include "src/core/support/string.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
nnoble0c475f02014-12-05 15:37:39 -080045static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0xff, 0xff};
47
48int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
49 struct sockaddr_in *addr4_out) {
50 GPR_ASSERT(addr != (struct sockaddr *)addr4_out);
51 if (addr->sa_family == AF_INET6) {
52 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
53 if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix,
54 sizeof(kV4MappedPrefix)) == 0) {
55 if (addr4_out != NULL) {
56 /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
57 memset(addr4_out, 0, sizeof(*addr4_out));
58 addr4_out->sin_family = AF_INET;
59 /* s6_addr32 would be nice, but it's non-standard. */
60 memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4);
61 addr4_out->sin_port = addr6->sin6_port;
62 }
63 return 1;
64 }
65 }
66 return 0;
67}
68
69int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
70 struct sockaddr_in6 *addr6_out) {
71 GPR_ASSERT(addr != (struct sockaddr *)addr6_out);
72 if (addr->sa_family == AF_INET) {
73 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
74 memset(addr6_out, 0, sizeof(*addr6_out));
75 addr6_out->sin6_family = AF_INET6;
76 memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
77 memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
78 addr6_out->sin6_port = addr4->sin_port;
79 return 1;
80 }
81 return 0;
82}
83
84int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) {
85 struct sockaddr_in addr4_normalized;
86 if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) {
87 addr = (struct sockaddr *)&addr4_normalized;
88 }
89 if (addr->sa_family == AF_INET) {
90 /* Check for 0.0.0.0 */
91 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
92 if (addr4->sin_addr.s_addr != 0) {
93 return 0;
94 }
95 *port_out = ntohs(addr4->sin_port);
96 return 1;
97 } else if (addr->sa_family == AF_INET6) {
98 /* Check for :: */
99 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
100 int i;
101 for (i = 0; i < 16; i++) {
102 if (addr6->sin6_addr.s6_addr[i] != 0) {
103 return 0;
104 }
105 }
106 *port_out = ntohs(addr6->sin6_port);
107 return 1;
108 } else {
109 return 0;
110 }
111}
112
113void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
114 struct sockaddr_in6 *wild6_out) {
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100115 grpc_sockaddr_make_wildcard4(port, wild4_out);
116 grpc_sockaddr_make_wildcard6(port, wild6_out);
117}
nnoble0c475f02014-12-05 15:37:39 -0800118
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100119void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) {
120 memset(wild_out, 0, sizeof(*wild_out));
121 wild_out->sin_family = AF_INET;
122 wild_out->sin_port = htons(port);
123}
124
125void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) {
126 memset(wild_out, 0, sizeof(*wild_out));
127 wild_out->sin6_family = AF_INET6;
128 wild_out->sin6_port = htons(port);
nnoble0c475f02014-12-05 15:37:39 -0800129}
130
131int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
132 int normalize) {
133 const int save_errno = errno;
134 struct sockaddr_in addr_normalized;
135 char ntop_buf[INET6_ADDRSTRLEN];
136 const void *ip = NULL;
137 int port;
138 int ret;
139
140 *out = NULL;
141 if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
142 addr = (const struct sockaddr *)&addr_normalized;
143 }
144 if (addr->sa_family == AF_INET) {
145 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
146 ip = &addr4->sin_addr;
147 port = ntohs(addr4->sin_port);
148 } else if (addr->sa_family == AF_INET6) {
149 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
150 ip = &addr6->sin6_addr;
151 port = ntohs(addr6->sin6_port);
152 }
153 if (ip != NULL &&
154 inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) {
155 ret = gpr_join_host_port(out, ntop_buf, port);
156 } else {
157 ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family);
158 }
159 /* This is probably redundant, but we wouldn't want to log the wrong error. */
160 errno = save_errno;
161 return ret;
162}
ctiller570d1f42015-01-12 16:29:52 -0800163
164int grpc_sockaddr_get_port(const struct sockaddr *addr) {
165 switch (addr->sa_family) {
166 case AF_INET:
167 return ntohs(((struct sockaddr_in *)addr)->sin_port);
168 case AF_INET6:
169 return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
Craig Tillerae7fe922015-02-13 23:16:32 -0800170 case AF_UNIX:
171 return 1;
ctiller570d1f42015-01-12 16:29:52 -0800172 default:
Craig Tiller35696192015-05-24 15:00:37 -0700173 gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", addr->sa_family);
ctiller570d1f42015-01-12 16:29:52 -0800174 return 0;
175 }
176}
177
178int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
179 switch (addr->sa_family) {
180 case AF_INET:
181 ((struct sockaddr_in *)addr)->sin_port = htons(port);
182 return 1;
183 case AF_INET6:
184 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
185 return 1;
186 default:
Craig Tiller35696192015-05-24 15:00:37 -0700187 gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", addr->sa_family);
ctiller570d1f42015-01-12 16:29:52 -0800188 return 0;
189 }
Craig Tiller190d3602015-02-18 09:23:38 -0800190}