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