blob: 740bbe716ecbb458fcafc00747871d6c389b9ff2 [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>
43
nnoble0c475f02014-12-05 15:37:39 -080044static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0xff, 0xff};
46
47int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr,
48 struct sockaddr_in *addr4_out) {
49 GPR_ASSERT(addr != (struct sockaddr *)addr4_out);
50 if (addr->sa_family == AF_INET6) {
51 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
52 if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix,
53 sizeof(kV4MappedPrefix)) == 0) {
54 if (addr4_out != NULL) {
55 /* Normalize ::ffff:0.0.0.0/96 to IPv4. */
56 memset(addr4_out, 0, sizeof(*addr4_out));
57 addr4_out->sin_family = AF_INET;
58 /* s6_addr32 would be nice, but it's non-standard. */
59 memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4);
60 addr4_out->sin_port = addr6->sin6_port;
61 }
62 return 1;
63 }
64 }
65 return 0;
66}
67
68int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr,
69 struct sockaddr_in6 *addr6_out) {
70 GPR_ASSERT(addr != (struct sockaddr *)addr6_out);
71 if (addr->sa_family == AF_INET) {
72 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
73 memset(addr6_out, 0, sizeof(*addr6_out));
74 addr6_out->sin6_family = AF_INET6;
75 memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12);
76 memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4);
77 addr6_out->sin6_port = addr4->sin_port;
78 return 1;
79 }
80 return 0;
81}
82
83int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) {
84 struct sockaddr_in addr4_normalized;
85 if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) {
86 addr = (struct sockaddr *)&addr4_normalized;
87 }
88 if (addr->sa_family == AF_INET) {
89 /* Check for 0.0.0.0 */
90 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
91 if (addr4->sin_addr.s_addr != 0) {
92 return 0;
93 }
94 *port_out = ntohs(addr4->sin_port);
95 return 1;
96 } else if (addr->sa_family == AF_INET6) {
97 /* Check for :: */
98 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
99 int i;
100 for (i = 0; i < 16; i++) {
101 if (addr6->sin6_addr.s6_addr[i] != 0) {
102 return 0;
103 }
104 }
105 *port_out = ntohs(addr6->sin6_port);
106 return 1;
107 } else {
108 return 0;
109 }
110}
111
112void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out,
113 struct sockaddr_in6 *wild6_out) {
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100114 grpc_sockaddr_make_wildcard4(port, wild4_out);
115 grpc_sockaddr_make_wildcard6(port, wild6_out);
116}
nnoble0c475f02014-12-05 15:37:39 -0800117
Nicolas "Pixel" Noble0f3ec822015-02-05 19:40:38 +0100118void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) {
119 memset(wild_out, 0, sizeof(*wild_out));
120 wild_out->sin_family = AF_INET;
121 wild_out->sin_port = htons(port);
122}
123
124void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) {
125 memset(wild_out, 0, sizeof(*wild_out));
126 wild_out->sin6_family = AF_INET6;
127 wild_out->sin6_port = htons(port);
nnoble0c475f02014-12-05 15:37:39 -0800128}
129
130int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
131 int normalize) {
132 const int save_errno = errno;
133 struct sockaddr_in addr_normalized;
134 char ntop_buf[INET6_ADDRSTRLEN];
135 const void *ip = NULL;
136 int port;
137 int ret;
138
139 *out = NULL;
140 if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
141 addr = (const struct sockaddr *)&addr_normalized;
142 }
143 if (addr->sa_family == AF_INET) {
144 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
145 ip = &addr4->sin_addr;
146 port = ntohs(addr4->sin_port);
147 } else if (addr->sa_family == AF_INET6) {
148 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
149 ip = &addr6->sin6_addr;
150 port = ntohs(addr6->sin6_port);
151 }
152 if (ip != NULL &&
153 inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) {
154 ret = gpr_join_host_port(out, ntop_buf, port);
155 } else {
156 ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family);
157 }
158 /* This is probably redundant, but we wouldn't want to log the wrong error. */
159 errno = save_errno;
160 return ret;
161}
ctiller570d1f42015-01-12 16:29:52 -0800162
163int grpc_sockaddr_get_port(const struct sockaddr *addr) {
164 switch (addr->sa_family) {
165 case AF_INET:
166 return ntohs(((struct sockaddr_in *)addr)->sin_port);
167 case AF_INET6:
168 return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
Craig Tillerae7fe922015-02-13 23:16:32 -0800169 case AF_UNIX:
170 return 1;
ctiller570d1f42015-01-12 16:29:52 -0800171 default:
172 gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
173 __FUNCTION__);
174 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:
187 gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
188 __FUNCTION__);
189 return 0;
190 }
Craig Tiller190d3602015-02-18 09:23:38 -0800191}