Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
ahedberg | 8afb88d | 2016-03-22 13:57:47 -0400 | [diff] [blame] | 3 | * Copyright 2016, Google Inc. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 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 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 35 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | #include <errno.h> |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 37 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 38 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 40 | #include <grpc/support/host_port.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | #include <grpc/support/log.h> |
| 42 | #include <grpc/support/port_platform.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 43 | #include <grpc/support/string_util.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 44 | |
murgatroid99 | 3196363 | 2016-08-09 14:00:41 -0700 | [diff] [blame^] | 45 | #include "src/core/lib/iomgr/socket_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 46 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
| 47 | #include "src/core/lib/support/string.h" |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 48 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 49 | static const uint8_t kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, |
| 50 | 0, 0, 0, 0, 0xff, 0xff}; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 51 | |
| 52 | int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr, |
| 53 | struct sockaddr_in *addr4_out) { |
| 54 | GPR_ASSERT(addr != (struct sockaddr *)addr4_out); |
| 55 | if (addr->sa_family == AF_INET6) { |
| 56 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 57 | if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix, |
| 58 | sizeof(kV4MappedPrefix)) == 0) { |
| 59 | if (addr4_out != NULL) { |
| 60 | /* Normalize ::ffff:0.0.0.0/96 to IPv4. */ |
| 61 | memset(addr4_out, 0, sizeof(*addr4_out)); |
| 62 | addr4_out->sin_family = AF_INET; |
| 63 | /* s6_addr32 would be nice, but it's non-standard. */ |
| 64 | memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4); |
| 65 | addr4_out->sin_port = addr6->sin6_port; |
| 66 | } |
| 67 | return 1; |
| 68 | } |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr, |
| 74 | struct sockaddr_in6 *addr6_out) { |
| 75 | GPR_ASSERT(addr != (struct sockaddr *)addr6_out); |
| 76 | if (addr->sa_family == AF_INET) { |
| 77 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 78 | memset(addr6_out, 0, sizeof(*addr6_out)); |
| 79 | addr6_out->sin6_family = AF_INET6; |
| 80 | memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12); |
| 81 | memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4); |
| 82 | addr6_out->sin6_port = addr4->sin_port; |
| 83 | return 1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) { |
| 89 | struct sockaddr_in addr4_normalized; |
| 90 | if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) { |
| 91 | addr = (struct sockaddr *)&addr4_normalized; |
| 92 | } |
| 93 | if (addr->sa_family == AF_INET) { |
| 94 | /* Check for 0.0.0.0 */ |
| 95 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 96 | if (addr4->sin_addr.s_addr != 0) { |
| 97 | return 0; |
| 98 | } |
| 99 | *port_out = ntohs(addr4->sin_port); |
| 100 | return 1; |
| 101 | } else if (addr->sa_family == AF_INET6) { |
| 102 | /* Check for :: */ |
| 103 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 104 | int i; |
| 105 | for (i = 0; i < 16; i++) { |
| 106 | if (addr6->sin6_addr.s6_addr[i] != 0) { |
| 107 | return 0; |
| 108 | } |
| 109 | } |
| 110 | *port_out = ntohs(addr6->sin6_port); |
| 111 | return 1; |
| 112 | } else { |
| 113 | return 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out, |
| 118 | struct sockaddr_in6 *wild6_out) { |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 119 | grpc_sockaddr_make_wildcard4(port, wild4_out); |
| 120 | grpc_sockaddr_make_wildcard6(port, wild6_out); |
| 121 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 122 | |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 123 | void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) { |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 124 | GPR_ASSERT(port >= 0 && port < 65536); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 125 | memset(wild_out, 0, sizeof(*wild_out)); |
| 126 | wild_out->sin_family = AF_INET; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 127 | wild_out->sin_port = htons((uint16_t)port); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) { |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 131 | GPR_ASSERT(port >= 0 && port < 65536); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 132 | memset(wild_out, 0, sizeof(*wild_out)); |
| 133 | wild_out->sin6_family = AF_INET6; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 134 | wild_out->sin6_port = htons((uint16_t)port); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, |
| 138 | int normalize) { |
| 139 | const int save_errno = errno; |
| 140 | struct sockaddr_in addr_normalized; |
| 141 | char ntop_buf[INET6_ADDRSTRLEN]; |
| 142 | const void *ip = NULL; |
| 143 | int port; |
| 144 | int ret; |
| 145 | |
| 146 | *out = NULL; |
| 147 | if (normalize && grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) { |
| 148 | addr = (const struct sockaddr *)&addr_normalized; |
| 149 | } |
| 150 | if (addr->sa_family == AF_INET) { |
| 151 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 152 | ip = &addr4->sin_addr; |
| 153 | port = ntohs(addr4->sin_port); |
| 154 | } else if (addr->sa_family == AF_INET6) { |
| 155 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 156 | ip = &addr6->sin6_addr; |
| 157 | port = ntohs(addr6->sin6_port); |
| 158 | } |
| 159 | if (ip != NULL && |
murgatroid99 | 3196363 | 2016-08-09 14:00:41 -0700 | [diff] [blame^] | 160 | grpc_inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 161 | NULL) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 162 | 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 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 170 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 171 | char *grpc_sockaddr_to_uri(const struct sockaddr *addr) { |
| 172 | char *temp; |
| 173 | char *result; |
Paul Marks | 63541a1 | 2015-08-04 15:05:00 -0700 | [diff] [blame] | 174 | struct sockaddr_in addr_normalized; |
| 175 | |
| 176 | if (grpc_sockaddr_is_v4mapped(addr, &addr_normalized)) { |
| 177 | addr = (const struct sockaddr *)&addr_normalized; |
| 178 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 179 | |
| 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; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 191 | default: |
| 192 | return grpc_sockaddr_to_uri_unix_if_possible(addr); |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 193 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 196 | int 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: |
ahedberg | 43df295 | 2016-03-18 10:46:38 -0400 | [diff] [blame] | 203 | if (grpc_is_unix_socket(addr)) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 204 | return 1; |
| 205 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 206 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", |
| 207 | addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 208 | return 0; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) { |
| 213 | switch (addr->sa_family) { |
| 214 | case AF_INET: |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 215 | GPR_ASSERT(port >= 0 && port < 65536); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 216 | ((struct sockaddr_in *)addr)->sin_port = htons((uint16_t)port); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 217 | return 1; |
| 218 | case AF_INET6: |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 219 | GPR_ASSERT(port >= 0 && port < 65536); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 220 | ((struct sockaddr_in6 *)addr)->sin6_port = htons((uint16_t)port); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 221 | return 1; |
| 222 | default: |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 223 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", |
| 224 | addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 227 | } |