Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
murgatroid99 | 5407089 | 2016-08-08 17:01:18 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/port.h" |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 35 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 36 | #ifdef GRPC_POSIX_SOCKET |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 37 | |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 38 | #include "src/core/lib/iomgr/tcp_client_posix.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 39 | |
| 40 | #include <errno.h> |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 41 | #include <netinet/in.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include <unistd.h> |
| 44 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 45 | #include <grpc/support/alloc.h> |
| 46 | #include <grpc/support/log.h> |
Masood Malekghassemi | 701af60 | 2015-06-03 15:01:17 -0700 | [diff] [blame] | 47 | #include <grpc/support/string_util.h> |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | #include <grpc/support/time.h> |
| 49 | |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 50 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 8a03448 | 2016-03-28 16:09:04 -0700 | [diff] [blame] | 51 | #include "src/core/lib/iomgr/ev_posix.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 52 | #include "src/core/lib/iomgr/iomgr_posix.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 53 | #include "src/core/lib/iomgr/sockaddr_utils.h" |
Yuchen Zeng | de3daf5 | 2016-10-13 17:26:26 -0700 | [diff] [blame] | 54 | #include "src/core/lib/iomgr/socket_mutator.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 55 | #include "src/core/lib/iomgr/socket_utils_posix.h" |
| 56 | #include "src/core/lib/iomgr/tcp_posix.h" |
| 57 | #include "src/core/lib/iomgr/timer.h" |
| 58 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
| 59 | #include "src/core/lib/support/string.h" |
Craig Tiller | c46beaa | 2016-02-24 09:17:19 -0800 | [diff] [blame] | 60 | |
Craig Tiller | 5e53ddb | 2015-09-16 10:53:15 -0700 | [diff] [blame] | 61 | extern int grpc_tcp_trace; |
| 62 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 63 | typedef struct { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 64 | gpr_mu mu; |
ctiller | 18b49ab | 2014-12-09 14:39:16 -0800 | [diff] [blame] | 65 | grpc_fd *fd; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 66 | gpr_timespec deadline; |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 67 | grpc_timer alarm; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 68 | int refs; |
Craig Tiller | 3382511 | 2015-09-18 07:44:19 -0700 | [diff] [blame] | 69 | grpc_closure write_closure; |
Craig Tiller | b4973682 | 2015-06-30 08:15:08 -0700 | [diff] [blame] | 70 | grpc_pollset_set *interested_parties; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 71 | char *addr_str; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 72 | grpc_endpoint **ep; |
| 73 | grpc_closure *closure; |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 74 | grpc_channel_args *channel_args; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } async_connect; |
| 76 | |
Yuchen Zeng | 64d518e | 2016-11-02 16:27:49 -0700 | [diff] [blame] | 77 | static grpc_error *prepare_socket(const grpc_resolved_address *addr, int fd, |
Yuchen Zeng | 929f4c6 | 2016-10-10 16:02:43 -0700 | [diff] [blame] | 78 | const grpc_channel_args *channel_args) { |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 79 | grpc_error *err = GRPC_ERROR_NONE; |
| 80 | |
Craig Tiller | 80384bd | 2016-05-06 16:12:31 -0700 | [diff] [blame] | 81 | GPR_ASSERT(fd >= 0); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 82 | |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 83 | err = grpc_set_socket_nonblocking(fd, 1); |
| 84 | if (err != GRPC_ERROR_NONE) goto error; |
| 85 | err = grpc_set_socket_cloexec(fd, 1); |
| 86 | if (err != GRPC_ERROR_NONE) goto error; |
| 87 | if (!grpc_is_unix_socket(addr)) { |
| 88 | err = grpc_set_socket_low_latency(fd, 1); |
| 89 | if (err != GRPC_ERROR_NONE) goto error; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 90 | } |
Craig Tiller | 4f1d0f3 | 2016-05-06 17:12:37 -0700 | [diff] [blame] | 91 | err = grpc_set_socket_no_sigpipe_if_possible(fd); |
| 92 | if (err != GRPC_ERROR_NONE) goto error; |
Yuchen Zeng | 929f4c6 | 2016-10-10 16:02:43 -0700 | [diff] [blame] | 93 | if (channel_args) { |
| 94 | for (size_t i = 0; i < channel_args->num_args; i++) { |
Yuchen Zeng | a4f708a | 2016-10-11 18:36:24 -0700 | [diff] [blame] | 95 | if (0 == strcmp(channel_args->args[i].key, GRPC_ARG_SOCKET_MUTATOR)) { |
| 96 | GPR_ASSERT(channel_args->args[i].type == GRPC_ARG_POINTER); |
| 97 | grpc_socket_mutator *mutator = channel_args->args[i].value.pointer.p; |
| 98 | err = grpc_set_socket_with_mutator(fd, mutator); |
Yuchen Zeng | 929f4c6 | 2016-10-10 16:02:43 -0700 | [diff] [blame] | 99 | if (err != GRPC_ERROR_NONE) goto error; |
Yuchen Zeng | 929f4c6 | 2016-10-10 16:02:43 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 103 | goto done; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 104 | |
| 105 | error: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | if (fd >= 0) { |
| 107 | close(fd); |
| 108 | } |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 109 | done: |
| 110 | return err; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 113 | static void tc_on_alarm(grpc_exec_ctx *exec_ctx, void *acp, grpc_error *error) { |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 114 | int done; |
| 115 | async_connect *ac = acp; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 116 | if (grpc_tcp_trace) { |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 117 | const char *str = grpc_error_string(error); |
| 118 | gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %s: on_alarm: error=%s", ac->addr_str, |
| 119 | str); |
| 120 | grpc_error_free_string(str); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 121 | } |
| 122 | gpr_mu_lock(&ac->mu); |
| 123 | if (ac->fd != NULL) { |
| 124 | grpc_fd_shutdown(exec_ctx, ac->fd); |
| 125 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 126 | done = (--ac->refs == 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | gpr_mu_unlock(&ac->mu); |
| 128 | if (done) { |
| 129 | gpr_mu_destroy(&ac->mu); |
| 130 | gpr_free(ac->addr_str); |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 131 | grpc_channel_args_destroy(ac->channel_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | gpr_free(ac); |
| 133 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 136 | grpc_endpoint *grpc_tcp_client_create_from_fd( |
| 137 | grpc_exec_ctx *exec_ctx, grpc_fd *fd, const grpc_channel_args *channel_args, |
| 138 | const char *addr_str) { |
| 139 | size_t tcp_read_chunk_size = GRPC_TCP_DEFAULT_READ_SLICE_SIZE; |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 140 | grpc_resource_quota *resource_quota = grpc_resource_quota_create(NULL); |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 141 | if (channel_args != NULL) { |
| 142 | for (size_t i = 0; i < channel_args->num_args; i++) { |
| 143 | if (0 == |
| 144 | strcmp(channel_args->args[i].key, GRPC_ARG_TCP_READ_CHUNK_SIZE)) { |
| 145 | grpc_integer_options options = {(int)tcp_read_chunk_size, 1, |
| 146 | 8 * 1024 * 1024}; |
| 147 | tcp_read_chunk_size = (size_t)grpc_channel_arg_get_integer( |
| 148 | &channel_args->args[i], options); |
Craig Tiller | 153eaa7 | 2016-10-21 13:52:36 -0700 | [diff] [blame] | 149 | } else if (0 == |
| 150 | strcmp(channel_args->args[i].key, GRPC_ARG_RESOURCE_QUOTA)) { |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 151 | grpc_resource_quota_internal_unref(exec_ctx, resource_quota); |
| 152 | resource_quota = grpc_resource_quota_internal_ref( |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 153 | channel_args->args[i].value.pointer.p); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | grpc_endpoint *ep = |
Craig Tiller | 20afa3d | 2016-10-17 14:52:14 -0700 | [diff] [blame] | 159 | grpc_tcp_create(fd, resource_quota, tcp_read_chunk_size, addr_str); |
| 160 | grpc_resource_quota_internal_unref(exec_ctx, resource_quota); |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 161 | return ep; |
| 162 | } |
| 163 | |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 164 | static void on_writable(grpc_exec_ctx *exec_ctx, void *acp, grpc_error *error) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 165 | async_connect *ac = acp; |
| 166 | int so_error = 0; |
| 167 | socklen_t so_error_size; |
| 168 | int err; |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 169 | int done; |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 170 | grpc_endpoint **ep = ac->ep; |
| 171 | grpc_closure *closure = ac->closure; |
Craig Tiller | 5553eb3 | 2015-07-21 12:28:56 -0700 | [diff] [blame] | 172 | grpc_fd *fd; |
| 173 | |
Craig Tiller | 82c63eb | 2016-05-10 15:28:01 -0700 | [diff] [blame] | 174 | GRPC_ERROR_REF(error); |
| 175 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 176 | if (grpc_tcp_trace) { |
Craig Tiller | c027e77 | 2016-05-03 16:27:00 -0700 | [diff] [blame] | 177 | const char *str = grpc_error_string(error); |
| 178 | gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %s: on_writable: error=%s", |
| 179 | ac->addr_str, str); |
| 180 | grpc_error_free_string(str); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 181 | } |
Craig Tiller | 5e53ddb | 2015-09-16 10:53:15 -0700 | [diff] [blame] | 182 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | gpr_mu_lock(&ac->mu); |
| 184 | GPR_ASSERT(ac->fd); |
Craig Tiller | 5553eb3 | 2015-07-21 12:28:56 -0700 | [diff] [blame] | 185 | fd = ac->fd; |
| 186 | ac->fd = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | gpr_mu_unlock(&ac->mu); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 188 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 189 | grpc_timer_cancel(exec_ctx, &ac->alarm); |
Craig Tiller | 2620536 | 2015-07-21 08:21:57 -0700 | [diff] [blame] | 190 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 191 | gpr_mu_lock(&ac->mu); |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 192 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 193 | error = |
| 194 | grpc_error_set_str(error, GRPC_ERROR_STR_OS_ERROR, "Timeout occurred"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 195 | goto finish; |
| 196 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 197 | |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 198 | do { |
| 199 | so_error_size = sizeof(so_error); |
| 200 | err = getsockopt(grpc_fd_wrapped_fd(fd), SOL_SOCKET, SO_ERROR, &so_error, |
| 201 | &so_error_size); |
| 202 | } while (err < 0 && errno == EINTR); |
| 203 | if (err < 0) { |
| 204 | error = GRPC_OS_ERROR(errno, "getsockopt"); |
| 205 | goto finish; |
| 206 | } |
| 207 | |
| 208 | switch (so_error) { |
| 209 | case 0: |
| 210 | grpc_pollset_set_del_fd(exec_ctx, ac->interested_parties, fd); |
Craig Tiller | d88c461 | 2016-09-29 10:35:38 -0700 | [diff] [blame] | 211 | *ep = grpc_tcp_client_create_from_fd(exec_ctx, fd, ac->channel_args, |
Craig Tiller | 9c7f4f7 | 2016-09-29 10:36:20 -0700 | [diff] [blame] | 212 | ac->addr_str); |
Mark D. Roth | e65ff11 | 2016-09-09 13:48:38 -0700 | [diff] [blame] | 213 | fd = NULL; |
| 214 | break; |
| 215 | case ENOBUFS: |
| 216 | /* We will get one of these errors if we have run out of |
| 217 | memory in the kernel for the data structures allocated |
| 218 | when you connect a socket. If this happens it is very |
| 219 | likely that if we wait a little bit then try again the |
| 220 | connection will work (since other programs or this |
| 221 | program will close their network connections and free up |
| 222 | memory). This does _not_ indicate that there is anything |
| 223 | wrong with the server we are connecting to, this is a |
| 224 | local problem. |
| 225 | |
| 226 | If you are looking at this code, then chances are that |
| 227 | your program or another program on the same computer |
| 228 | opened too many network connections. The "easy" fix: |
| 229 | don't do that! */ |
| 230 | gpr_log(GPR_ERROR, "kernel out of buffers"); |
| 231 | gpr_mu_unlock(&ac->mu); |
| 232 | grpc_fd_notify_on_write(exec_ctx, fd, &ac->write_closure); |
| 233 | return; |
| 234 | case ECONNREFUSED: |
| 235 | /* This error shouldn't happen for anything other than connect(). */ |
| 236 | error = GRPC_OS_ERROR(so_error, "connect"); |
| 237 | break; |
| 238 | default: |
| 239 | /* We don't really know which syscall triggered the problem here, |
| 240 | so punt by reporting getsockopt(). */ |
| 241 | error = GRPC_OS_ERROR(so_error, "getsockopt(SO_ERROR)"); |
| 242 | break; |
| 243 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 244 | |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 245 | finish: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 246 | if (fd != NULL) { |
| 247 | grpc_pollset_set_del_fd(exec_ctx, ac->interested_parties, fd); |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 248 | grpc_fd_orphan(exec_ctx, fd, NULL, NULL, "tcp_client_orphan"); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 249 | fd = NULL; |
| 250 | } |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 251 | done = (--ac->refs == 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 252 | gpr_mu_unlock(&ac->mu); |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 253 | if (error != GRPC_ERROR_NONE) { |
David Garcia Quintas | 19badff | 2016-11-20 20:17:46 -0800 | [diff] [blame] | 254 | char *error_descr; |
| 255 | gpr_asprintf(&error_descr, "Failed to connect to remote host: %s", |
| 256 | grpc_error_get_str(error, GRPC_ERROR_STR_DESCRIPTION)); |
| 257 | error = grpc_error_set_str(error, GRPC_ERROR_STR_DESCRIPTION, error_descr); |
| 258 | gpr_free(error_descr); |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 259 | error = |
| 260 | grpc_error_set_str(error, GRPC_ERROR_STR_TARGET_ADDRESS, ac->addr_str); |
| 261 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 262 | if (done) { |
| 263 | gpr_mu_destroy(&ac->mu); |
| 264 | gpr_free(ac->addr_str); |
Craig Tiller | 287928c | 2016-09-23 16:04:30 -0700 | [diff] [blame] | 265 | grpc_channel_args_destroy(ac->channel_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 266 | gpr_free(ac); |
| 267 | } |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 268 | grpc_closure_sched(exec_ctx, closure, error); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 269 | } |
| 270 | |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 271 | static void tcp_client_connect_impl(grpc_exec_ctx *exec_ctx, |
| 272 | grpc_closure *closure, grpc_endpoint **ep, |
| 273 | grpc_pollset_set *interested_parties, |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 274 | const grpc_channel_args *channel_args, |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 275 | const grpc_resolved_address *addr, |
| 276 | gpr_timespec deadline) { |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 277 | int fd; |
| 278 | grpc_dualstack_mode dsmode; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 279 | int err; |
| 280 | async_connect *ac; |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 281 | grpc_resolved_address addr6_v4mapped; |
| 282 | grpc_resolved_address addr4_copy; |
Craig Tiller | 9bcc751 | 2015-05-11 14:59:48 -0700 | [diff] [blame] | 283 | grpc_fd *fdobj; |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 284 | char *name; |
| 285 | char *addr_str; |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 286 | grpc_error *error; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 287 | |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 288 | *ep = NULL; |
| 289 | |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 290 | /* Use dualstack sockets where available. */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 291 | if (grpc_sockaddr_to_v4mapped(addr, &addr6_v4mapped)) { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 292 | addr = &addr6_v4mapped; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 293 | } |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 294 | |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 295 | error = grpc_create_dualstack_socket(addr, SOCK_STREAM, 0, &dsmode, &fd); |
| 296 | if (error != GRPC_ERROR_NONE) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 297 | grpc_closure_sched(exec_ctx, closure, error); |
Craig Tiller | 27f59af | 2016-04-28 14:19:48 -0700 | [diff] [blame] | 298 | return; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 299 | } |
| 300 | if (dsmode == GRPC_DSMODE_IPV4) { |
| 301 | /* If we got an AF_INET socket, map the address back to IPv4. */ |
| 302 | GPR_ASSERT(grpc_sockaddr_is_v4mapped(addr, &addr4_copy)); |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 303 | addr = &addr4_copy; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 304 | } |
Yuchen Zeng | 67ffe3f | 2016-11-11 10:50:42 -0800 | [diff] [blame] | 305 | if ((error = prepare_socket(addr, fd, channel_args)) != GRPC_ERROR_NONE) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 306 | grpc_closure_sched(exec_ctx, closure, error); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 307 | return; |
| 308 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 309 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 310 | do { |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 311 | GPR_ASSERT(addr->len < ~(socklen_t)0); |
murgatroid99 | dedb923 | 2016-09-26 13:54:04 -0700 | [diff] [blame] | 312 | err = |
| 313 | connect(fd, (const struct sockaddr *)addr->addr, (socklen_t)addr->len); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 314 | } while (err < 0 && errno == EINTR); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 315 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 316 | addr_str = grpc_sockaddr_to_uri(addr); |
| 317 | gpr_asprintf(&name, "tcp-client:%s", addr_str); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 318 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 319 | fdobj = grpc_fd_create(fd, name); |
Craig Tiller | 9bcc751 | 2015-05-11 14:59:48 -0700 | [diff] [blame] | 320 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 321 | if (err >= 0) { |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 322 | *ep = |
| 323 | grpc_tcp_client_create_from_fd(exec_ctx, fdobj, channel_args, addr_str); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 324 | grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 325 | goto done; |
| 326 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 327 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 328 | if (errno != EWOULDBLOCK && errno != EINPROGRESS) { |
yang-g | 5d85037 | 2015-12-01 10:32:28 -0800 | [diff] [blame] | 329 | grpc_fd_orphan(exec_ctx, fdobj, NULL, NULL, "tcp_client_connect_error"); |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 330 | grpc_closure_sched(exec_ctx, closure, GRPC_OS_ERROR(errno, "connect")); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 331 | goto done; |
| 332 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 333 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 334 | grpc_pollset_set_add_fd(exec_ctx, interested_parties, fdobj); |
Craig Tiller | 4b678bd | 2015-06-02 16:12:24 -0700 | [diff] [blame] | 335 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 336 | ac = gpr_malloc(sizeof(async_connect)); |
Craig Tiller | d1bec03 | 2015-09-18 17:29:00 -0700 | [diff] [blame] | 337 | ac->closure = closure; |
| 338 | ac->ep = ep; |
Craig Tiller | 9bcc751 | 2015-05-11 14:59:48 -0700 | [diff] [blame] | 339 | ac->fd = fdobj; |
Craig Tiller | b4973682 | 2015-06-30 08:15:08 -0700 | [diff] [blame] | 340 | ac->interested_parties = interested_parties; |
Craig Tiller | 1b22b9d | 2015-07-20 13:42:22 -0700 | [diff] [blame] | 341 | ac->addr_str = addr_str; |
| 342 | addr_str = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 343 | gpr_mu_init(&ac->mu); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 344 | ac->refs = 2; |
Craig Tiller | d6887e0 | 2016-12-29 10:11:40 -0800 | [diff] [blame^] | 345 | grpc_closure_init(&ac->write_closure, on_writable, ac, |
| 346 | grpc_schedule_on_exec_ctx); |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 347 | ac->channel_args = grpc_channel_args_copy(channel_args); |
ctiller | 58393c2 | 2015-01-07 14:03:30 -0800 | [diff] [blame] | 348 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 349 | if (grpc_tcp_trace) { |
| 350 | gpr_log(GPR_DEBUG, "CLIENT_CONNECT: %s: asynchronously connecting", |
| 351 | ac->addr_str); |
| 352 | } |
Craig Tiller | 5e53ddb | 2015-09-16 10:53:15 -0700 | [diff] [blame] | 353 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 354 | gpr_mu_lock(&ac->mu); |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 355 | grpc_timer_init(exec_ctx, &ac->alarm, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 356 | gpr_convert_clock_type(deadline, GPR_CLOCK_MONOTONIC), |
| 357 | tc_on_alarm, ac, gpr_now(GPR_CLOCK_MONOTONIC)); |
| 358 | grpc_fd_notify_on_write(exec_ctx, ac->fd, &ac->write_closure); |
| 359 | gpr_mu_unlock(&ac->mu); |
Craig Tiller | fa275a9 | 2015-06-01 13:55:54 -0700 | [diff] [blame] | 360 | |
| 361 | done: |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 362 | gpr_free(name); |
| 363 | gpr_free(addr_str); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 364 | } |
Craig Tiller | 0c0b60c | 2015-01-21 15:49:28 -0800 | [diff] [blame] | 365 | |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 366 | // overridden by api_fuzzer.c |
| 367 | void (*grpc_tcp_client_connect_impl)( |
| 368 | grpc_exec_ctx *exec_ctx, grpc_closure *closure, grpc_endpoint **ep, |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 369 | grpc_pollset_set *interested_parties, const grpc_channel_args *channel_args, |
Craig Tiller | 3d357d9 | 2016-10-26 20:52:03 -0700 | [diff] [blame] | 370 | const grpc_resolved_address *addr, |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 371 | gpr_timespec deadline) = tcp_client_connect_impl; |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 372 | |
| 373 | void grpc_tcp_client_connect(grpc_exec_ctx *exec_ctx, grpc_closure *closure, |
| 374 | grpc_endpoint **ep, |
| 375 | grpc_pollset_set *interested_parties, |
Craig Tiller | 8695876 | 2016-09-23 12:05:34 -0700 | [diff] [blame] | 376 | const grpc_channel_args *channel_args, |
murgatroid99 | 7871f73 | 2016-09-23 13:49:05 -0700 | [diff] [blame] | 377 | const grpc_resolved_address *addr, |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 378 | gpr_timespec deadline) { |
Craig Tiller | e34c285 | 2016-09-23 09:43:32 -0700 | [diff] [blame] | 379 | grpc_tcp_client_connect_impl(exec_ctx, closure, ep, interested_parties, |
Craig Tiller | 3d357d9 | 2016-10-26 20:52:03 -0700 | [diff] [blame] | 380 | channel_args, addr, deadline); |
Craig Tiller | e628261 | 2016-04-13 14:14:34 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 383 | #endif |