Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 19 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 21 | #include <errno.h> |
Yash Tibrewal | fcd26bc | 2017-09-25 15:08:28 -0700 | [diff] [blame^] | 22 | #include <inttypes.h> |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 23 | #include <string.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 24 | |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 25 | #include <grpc/support/alloc.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 26 | #include <grpc/support/host_port.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 27 | #include <grpc/support/log.h> |
| 28 | #include <grpc/support/port_platform.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 29 | #include <grpc/support/string_util.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 30 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 31 | #include "src/core/lib/iomgr/sockaddr.h" |
murgatroid99 | 3196363 | 2016-08-09 14:00:41 -0700 | [diff] [blame] | 32 | #include "src/core/lib/iomgr/socket_utils.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 33 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
| 34 | #include "src/core/lib/support/string.h" |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 35 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 36 | static const uint8_t kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, |
| 37 | 0, 0, 0, 0, 0xff, 0xff}; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 38 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 39 | int grpc_sockaddr_is_v4mapped(const grpc_resolved_address *resolved_addr, |
| 40 | grpc_resolved_address *resolved_addr4_out) { |
| 41 | GPR_ASSERT(resolved_addr != resolved_addr4_out); |
| 42 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 43 | struct sockaddr_in *addr4_out = |
Craig Tiller | 7fea751 | 2017-04-12 09:43:34 -0700 | [diff] [blame] | 44 | resolved_addr4_out == NULL |
| 45 | ? NULL |
| 46 | : (struct sockaddr_in *)resolved_addr4_out->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 47 | if (addr->sa_family == AF_INET6) { |
| 48 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 49 | if (memcmp(addr6->sin6_addr.s6_addr, kV4MappedPrefix, |
| 50 | sizeof(kV4MappedPrefix)) == 0) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 51 | if (resolved_addr4_out != NULL) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 52 | /* Normalize ::ffff:0.0.0.0/96 to IPv4. */ |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 53 | memset(resolved_addr4_out, 0, sizeof(*resolved_addr4_out)); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 54 | addr4_out->sin_family = AF_INET; |
| 55 | /* s6_addr32 would be nice, but it's non-standard. */ |
| 56 | memcpy(&addr4_out->sin_addr, &addr6->sin6_addr.s6_addr[12], 4); |
| 57 | addr4_out->sin_port = addr6->sin6_port; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 58 | resolved_addr4_out->len = sizeof(struct sockaddr_in); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 59 | } |
| 60 | return 1; |
| 61 | } |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 66 | int grpc_sockaddr_to_v4mapped(const grpc_resolved_address *resolved_addr, |
| 67 | grpc_resolved_address *resolved_addr6_out) { |
| 68 | GPR_ASSERT(resolved_addr != resolved_addr6_out); |
| 69 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 70 | struct sockaddr_in6 *addr6_out = |
| 71 | (struct sockaddr_in6 *)resolved_addr6_out->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 72 | if (addr->sa_family == AF_INET) { |
| 73 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 74 | memset(resolved_addr6_out, 0, sizeof(*resolved_addr6_out)); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 79 | resolved_addr6_out->len = sizeof(struct sockaddr_in6); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 80 | return 1; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 85 | int grpc_sockaddr_is_wildcard(const grpc_resolved_address *resolved_addr, |
| 86 | int *port_out) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 87 | const struct sockaddr *addr; |
| 88 | grpc_resolved_address addr4_normalized; |
| 89 | if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr4_normalized)) { |
| 90 | resolved_addr = &addr4_normalized; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 91 | } |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 92 | addr = (const struct sockaddr *)resolved_addr->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 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 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 117 | void grpc_sockaddr_make_wildcards(int port, grpc_resolved_address *wild4_out, |
| 118 | grpc_resolved_address *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 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 123 | void grpc_sockaddr_make_wildcard4(int port, |
| 124 | grpc_resolved_address *resolved_wild_out) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 125 | struct sockaddr_in *wild_out = (struct sockaddr_in *)resolved_wild_out->addr; |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 126 | GPR_ASSERT(port >= 0 && port < 65536); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 127 | memset(resolved_wild_out, 0, sizeof(*resolved_wild_out)); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 128 | wild_out->sin_family = AF_INET; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 129 | wild_out->sin_port = htons((uint16_t)port); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 130 | resolved_wild_out->len = sizeof(struct sockaddr_in); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 131 | } |
| 132 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 133 | void grpc_sockaddr_make_wildcard6(int port, |
| 134 | grpc_resolved_address *resolved_wild_out) { |
| 135 | struct sockaddr_in6 *wild_out = |
| 136 | (struct sockaddr_in6 *)resolved_wild_out->addr; |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 137 | GPR_ASSERT(port >= 0 && port < 65536); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 138 | memset(resolved_wild_out, 0, sizeof(*resolved_wild_out)); |
Nicolas "Pixel" Noble | 0f3ec82 | 2015-02-05 19:40:38 +0100 | [diff] [blame] | 139 | wild_out->sin6_family = AF_INET6; |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 140 | wild_out->sin6_port = htons((uint16_t)port); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 141 | resolved_wild_out->len = sizeof(struct sockaddr_in6); |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 142 | } |
| 143 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 144 | int grpc_sockaddr_to_string(char **out, |
| 145 | const grpc_resolved_address *resolved_addr, |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 146 | int normalize) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 147 | const struct sockaddr *addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 148 | const int save_errno = errno; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 149 | grpc_resolved_address addr_normalized; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 150 | char ntop_buf[INET6_ADDRSTRLEN]; |
| 151 | const void *ip = NULL; |
| 152 | int port; |
Yuchen Zeng | aa76d3d | 2017-02-15 14:00:01 -0800 | [diff] [blame] | 153 | uint32_t sin6_scope_id = 0; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 154 | int ret; |
| 155 | |
| 156 | *out = NULL; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 157 | if (normalize && grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) { |
| 158 | resolved_addr = &addr_normalized; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 159 | } |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 160 | addr = (const struct sockaddr *)resolved_addr->addr; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 161 | if (addr->sa_family == AF_INET) { |
| 162 | const struct sockaddr_in *addr4 = (const struct sockaddr_in *)addr; |
| 163 | ip = &addr4->sin_addr; |
| 164 | port = ntohs(addr4->sin_port); |
| 165 | } else if (addr->sa_family == AF_INET6) { |
| 166 | const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6 *)addr; |
| 167 | ip = &addr6->sin6_addr; |
| 168 | port = ntohs(addr6->sin6_port); |
Yuchen Zeng | aa76d3d | 2017-02-15 14:00:01 -0800 | [diff] [blame] | 169 | sin6_scope_id = addr6->sin6_scope_id; |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 170 | } |
| 171 | if (ip != NULL && |
murgatroid99 | 7e924a0 | 2016-08-10 10:06:43 -0700 | [diff] [blame] | 172 | grpc_inet_ntop(addr->sa_family, ip, ntop_buf, sizeof(ntop_buf)) != NULL) { |
Yuchen Zeng | aa76d3d | 2017-02-15 14:00:01 -0800 | [diff] [blame] | 173 | if (sin6_scope_id != 0) { |
| 174 | char *host_with_scope; |
| 175 | /* Enclose sin6_scope_id with the format defined in RFC 6784 section 2. */ |
| 176 | gpr_asprintf(&host_with_scope, "%s%%25%" PRIu32, ntop_buf, sin6_scope_id); |
| 177 | ret = gpr_join_host_port(out, host_with_scope, port); |
| 178 | gpr_free(host_with_scope); |
| 179 | } else { |
| 180 | ret = gpr_join_host_port(out, ntop_buf, port); |
| 181 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 182 | } else { |
| 183 | ret = gpr_asprintf(out, "(sockaddr family=%d)", addr->sa_family); |
| 184 | } |
| 185 | /* This is probably redundant, but we wouldn't want to log the wrong error. */ |
| 186 | errno = save_errno; |
| 187 | return ret; |
| 188 | } |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 189 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 190 | char *grpc_sockaddr_to_uri(const grpc_resolved_address *resolved_addr) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 191 | grpc_resolved_address addr_normalized; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 192 | if (grpc_sockaddr_is_v4mapped(resolved_addr, &addr_normalized)) { |
| 193 | resolved_addr = &addr_normalized; |
Paul Marks | 63541a1 | 2015-08-04 15:05:00 -0700 | [diff] [blame] | 194 | } |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 195 | const char *scheme = grpc_sockaddr_get_uri_scheme(resolved_addr); |
| 196 | if (scheme == NULL || strcmp("unix", scheme) == 0) { |
| 197 | return grpc_sockaddr_to_uri_unix_if_possible(resolved_addr); |
| 198 | } |
| 199 | char *path = NULL; |
| 200 | char *uri_str = NULL; |
| 201 | if (grpc_sockaddr_to_string(&path, resolved_addr, |
| 202 | false /* suppress errors */) && |
| 203 | scheme != NULL) { |
| 204 | gpr_asprintf(&uri_str, "%s:%s", scheme, path); |
| 205 | } |
| 206 | gpr_free(path); |
| 207 | return uri_str != NULL ? uri_str : NULL; |
| 208 | } |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 209 | |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 210 | const char *grpc_sockaddr_get_uri_scheme( |
| 211 | const grpc_resolved_address *resolved_addr) { |
| 212 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 213 | switch (addr->sa_family) { |
| 214 | case AF_INET: |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 215 | return "ipv4"; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 216 | case AF_INET6: |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 217 | return "ipv6"; |
| 218 | case AF_UNIX: |
| 219 | return "unix"; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 220 | } |
David Garcia Quintas | 0129150 | 2017-02-07 13:26:41 -0800 | [diff] [blame] | 221 | return NULL; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 222 | } |
| 223 | |
murgatroid99 | 6617790 | 2017-07-18 18:00:38 -0700 | [diff] [blame] | 224 | int grpc_sockaddr_get_family(const grpc_resolved_address *resolved_addr) { |
| 225 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
| 226 | return addr->sa_family; |
| 227 | } |
| 228 | |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 229 | int grpc_sockaddr_get_port(const grpc_resolved_address *resolved_addr) { |
| 230 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 231 | switch (addr->sa_family) { |
| 232 | case AF_INET: |
| 233 | return ntohs(((struct sockaddr_in *)addr)->sin_port); |
| 234 | case AF_INET6: |
| 235 | return ntohs(((struct sockaddr_in6 *)addr)->sin6_port); |
| 236 | default: |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 237 | if (grpc_is_unix_socket(resolved_addr)) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 238 | return 1; |
| 239 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 240 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_get_port", |
| 241 | addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 246 | int grpc_sockaddr_set_port(const grpc_resolved_address *resolved_addr, |
| 247 | int port) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 248 | const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr; |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 249 | switch (addr->sa_family) { |
| 250 | case AF_INET: |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 251 | GPR_ASSERT(port >= 0 && port < 65536); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 252 | ((struct sockaddr_in *)addr)->sin_port = htons((uint16_t)port); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 253 | return 1; |
| 254 | case AF_INET6: |
Craig Tiller | 6a6b36c | 2015-09-10 16:00:22 -0700 | [diff] [blame] | 255 | GPR_ASSERT(port >= 0 && port < 65536); |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 256 | ((struct sockaddr_in6 *)addr)->sin6_port = htons((uint16_t)port); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 257 | return 1; |
| 258 | default: |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 259 | gpr_log(GPR_ERROR, "Unknown socket family %d in grpc_sockaddr_set_port", |
| 260 | addr->sa_family); |
ctiller | 570d1f4 | 2015-01-12 16:29:52 -0800 | [diff] [blame] | 261 | return 0; |
| 262 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 263 | } |