Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, 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 | |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 34 | #include "src/core/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 | #ifdef GPR_POSIX_SOCKET |
| 40 | #include <sys/un.h> |
| 41 | #endif |
| 42 | |
| 43 | #include <grpc/support/alloc.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 44 | #include <grpc/support/host_port.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 45 | #include <grpc/support/log.h> |
| 46 | #include <grpc/support/port_platform.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 47 | #include <grpc/support/string_util.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 48 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 49 | #include "src/core/support/string.h" |
| 50 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 51 | static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, |
| 52 | 0, 0, 0, 0, 0xff, 0xff}; |
| 53 | |
| 54 | int grpc_sockaddr_is_v4mapped(const struct sockaddr *addr, |
| 55 | struct sockaddr_in *addr4_out) { |
| 56 | GPR_ASSERT(addr != (struct sockaddr *)addr4_out); |
| 57 | if (addr->sa_family == AF_INET6) { |
| 58 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 59 | if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix, |
| 60 | sizeof(kV4MappedPrefix)) == 0) { |
| 61 | if (addr4_out != NULL) { |
| 62 | /* Normalize ::ffff:0.0.0.0/96 to IPv4. */ |
| 63 | memset(addr4_out, 0, sizeof(*addr4_out)); |
| 64 | addr4_out->sin_family = AF_INET; |
| 65 | /* s6_addr32 would be nice, but it's non-standard. */ |
| 66 | memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4); |
| 67 | addr4_out->sin_port = addr6->sin6_port; |
| 68 | } |
| 69 | return 1; |
| 70 | } |
| 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int grpc_sockaddr_to_v4mapped(const struct sockaddr *addr, |
| 76 | struct sockaddr_in6 *addr6_out) { |
| 77 | GPR_ASSERT(addr != (struct sockaddr *)addr6_out); |
| 78 | if (addr->sa_family == AF_INET) { |
| 79 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 80 | memset(addr6_out, 0, sizeof(*addr6_out)); |
| 81 | addr6_out->sin6_family = AF_INET6; |
| 82 | memcpy(&addr6_out->sin6_addr.s6_addr[0], kV4MappedPrefix, 12); |
| 83 | memcpy(&addr6_out->sin6_addr.s6_addr[12], &addr4->sin_addr, 4); |
| 84 | addr6_out->sin6_port = addr4->sin_port; |
| 85 | return 1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | int grpc_sockaddr_is_wildcard(const struct sockaddr *addr, int *port_out) { |
| 91 | struct sockaddr_in addr4_normalized; |
| 92 | if (grpc_sockaddr_is_v4mapped(addr, &addr4_normalized)) { |
| 93 | addr = (struct sockaddr *)&addr4_normalized; |
| 94 | } |
| 95 | if (addr->sa_family == AF_INET) { |
| 96 | /* Check for 0.0.0.0 */ |
| 97 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 98 | if (addr4->sin_addr.s_addr != 0) { |
| 99 | return 0; |
| 100 | } |
| 101 | *port_out = ntohs(addr4->sin_port); |
| 102 | return 1; |
| 103 | } else if (addr->sa_family == AF_INET6) { |
| 104 | /* Check for :: */ |
| 105 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 106 | int i; |
| 107 | for (i = 0; i < 16; i++) { |
| 108 | if (addr6->sin6_addr.s6_addr[i] != 0) { |
| 109 | return 0; |
| 110 | } |
| 111 | } |
| 112 | *port_out = ntohs(addr6->sin6_port); |
| 113 | return 1; |
| 114 | } else { |
| 115 | return 0; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void grpc_sockaddr_make_wildcards(int port, struct sockaddr_in *wild4_out, |
| 120 | struct sockaddr_in6 *wild6_out) { |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 121 | grpc_sockaddr_make_wildcard4(port, wild4_out); |
| 122 | grpc_sockaddr_make_wildcard6(port, wild6_out); |
| 123 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 124 | |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 125 | void grpc_sockaddr_make_wildcard4(int port, struct sockaddr_in *wild_out) { |
| 126 | memset(wild_out, 0, sizeof(*wild_out)); |
| 127 | wild_out->sin_family = AF_INET; |
| 128 | wild_out->sin_port = htons(port); |
| 129 | } |
| 130 | |
| 131 | void grpc_sockaddr_make_wildcard6(int port, struct sockaddr_in6 *wild_out) { |
| 132 | memset(wild_out, 0, sizeof(*wild_out)); |
| 133 | wild_out->sin6_family = AF_INET6; |
| 134 | wild_out->sin6_port = htons(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 && |
| 160 | inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) { |
| 161 | ret = gpr_join_host_port(out, ntop_buf, port); |
| 162 | } else { |
| 163 | ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family); |
| 164 | } |
| 165 | /* This is probably redundant, but we wouldn't want to log the wrong error. */ |
| 166 | errno = save_errno; |
| 167 | return ret; |
| 168 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 169 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 170 | char *grpc_sockaddr_to_uri(const struct sockaddr *addr) { |
| 171 | char *temp; |
| 172 | char *result; |
| 173 | |
| 174 | switch (addr->sa_family) { |
| 175 | case AF_INET: |
| 176 | grpc_sockaddr_to_string(&temp, addr, 0); |
| 177 | gpr_asprintf(&result, "ipv4:%s", temp); |
| 178 | gpr_free(temp); |
| 179 | return result; |
| 180 | case AF_INET6: |
| 181 | grpc_sockaddr_to_string(&temp, addr, 0); |
| 182 | gpr_asprintf(&result, "ipv6:%s", temp); |
| 183 | gpr_free(temp); |
| 184 | return result; |
| 185 | #ifdef GPR_POSIX_SOCKET |
| 186 | case AF_UNIX: |
| 187 | gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path); |
| 188 | return result; |
| 189 | #endif |
| 190 | } |
| 191 | |
| 192 | return NULL; |
| 193 | } |
| 194 | |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 195 | int grpc_sockaddr_get_port(const struct sockaddr *addr) { |
| 196 | switch (addr->sa_family) { |
| 197 | case AF_INET: |
| 198 | return ntohs(((struct sockaddr_in *)addr)->sin_port); |
| 199 | case AF_INET6: |
| 200 | return ntohs(((struct sockaddr_in6 *)addr)->sin6_port); |
Craig Tiller | ae7fe92 | 2015-02-13 23:16:32 -0800 | [diff] [blame] | 201 | case AF_UNIX: |
| 202 | return 1; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 203 | default: |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 204 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int grpc_sockaddr_set_port(const struct sockaddr *addr, int port) { |
| 210 | switch (addr->sa_family) { |
| 211 | case AF_INET: |
| 212 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 213 | return 1; |
| 214 | case AF_INET6: |
| 215 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
| 216 | return 1; |
| 217 | default: |
Craig Tiller | 3569619 | 2015-05-24 15:00:37 -0700 | [diff] [blame] | 218 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 219 | return 0; |
| 220 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 221 | } |