blob: eca14a4f398b8b1642cd4fa0b904ef60cd0ac150 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
3 * Copyright 2014, Google Inc.
4 * 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
nnoble0c475f02014-12-05 15:37:39 -080036#include <arpa/inet.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <errno.h>
ctiller18b49ab2014-12-09 14:39:16 -080038#include <string.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080039
nnoble0c475f02014-12-05 15:37:39 -080040#include <grpc/support/host_port.h>
41#include <grpc/support/string.h>
42#include <grpc/support/log.h>
43#include <grpc/support/port_platform.h>
44
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) {
115 memset(wild4_out, 0, sizeof(*wild4_out));
116 wild4_out->sin_family = AF_INET;
117 wild4_out->sin_port = htons(port);
118
119 memset(wild6_out, 0, sizeof(*wild6_out));
120 wild6_out->sin6_family = AF_INET6;
121 wild6_out->sin6_port = htons(port);
122}
123
124int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr,
125 int normalize) {
126 const int save_errno = errno;
127 struct sockaddr_in addr_normalized;
128 char ntop_buf[INET6_ADDRSTRLEN];
129 const void *ip = NULL;
130 int port;
131 int ret;
132
133 *out = NULL;
134 if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) {
135 addr = (const struct sockaddr *)&addr_normalized;
136 }
137 if (addr->sa_family == AF_INET) {
138 const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr;
139 ip = &addr4->sin_addr;
140 port = ntohs(addr4->sin_port);
141 } else if (addr->sa_family == AF_INET6) {
142 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr;
143 ip = &addr6->sin6_addr;
144 port = ntohs(addr6->sin6_port);
145 }
146 if (ip != NULL &&
147 inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) {
148 ret = gpr_join_host_port(out, ntop_buf, port);
149 } else {
150 ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family);
151 }
152 /* This is probably redundant, but we wouldn't want to log the wrong error. */
153 errno = save_errno;
154 return ret;
155}
ctiller570d1f42015-01-12 16:29:52 -0800156
157int grpc_sockaddr_get_port(const struct sockaddr *addr) {
158 switch (addr->sa_family) {
159 case AF_INET:
160 return ntohs(((struct sockaddr_in *)addr)->sin_port);
161 case AF_INET6:
162 return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
163 default:
164 gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
165 __FUNCTION__);
166 return 0;
167 }
168}
169
170int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) {
171 switch (addr->sa_family) {
172 case AF_INET:
173 ((struct sockaddr_in *)addr)->sin_port = htons(port);
174 return 1;
175 case AF_INET6:
176 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
177 return 1;
178 default:
179 gpr_log(GPR_ERROR, "Unknown socket family %d in %s", addr->sa_family,
180 __FUNCTION__);
181 return 0;
182 }
183}