Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/lib/cf-socket.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h> /* <netinet/tcp.h> may need it */
28
#endif
29
#ifdef HAVE_LINUX_TCP_H
30
#include <linux/tcp.h>
31
#elif defined(HAVE_NETINET_TCP_H)
32
#include <netinet/tcp.h>
33
#endif
34
#ifdef HAVE_NETINET_UDP_H
35
#include <netinet/udp.h>
36
#endif
37
#ifdef HAVE_SYS_IOCTL_H
38
#include <sys/ioctl.h>
39
#endif
40
#ifdef HAVE_NETDB_H
41
#include <netdb.h>
42
#endif
43
#ifdef HAVE_ARPA_INET_H
44
#include <arpa/inet.h>
45
#endif
46
47
#ifdef __VMS
48
#include <in.h>
49
#include <inet.h>
50
#endif
51
52
#ifdef __DragonFly__
53
/* Required for __DragonFly_version */
54
#include <sys/param.h>
55
#endif
56
57
#include "urldata.h"
58
#include "bufq.h"
59
#include "curl_trc.h"
60
#include "if2ip.h"
61
#include "cfilters.h"
62
#include "cf-socket.h"
63
#include "connect.h"
64
#include "select.h"
65
#include "multiif.h"
66
#include "curlx/inet_pton.h"
67
#include "progress.h"
68
#include "conncache.h"
69
#include "multihandle.h"
70
#include "rand.h"
71
#include "strdup.h"
72
#include "system_win32.h"
73
#include "curlx/nonblock.h"
74
#include "curlx/version_win32.h"
75
#include "curlx/strerr.h"
76
#include "curlx/strparse.h"
77
78
79
static void tcpnodelay(struct Curl_cfilter *cf,
80
                       struct Curl_easy *data,
81
                       curl_socket_t sockfd)
82
0
{
83
0
#if defined(TCP_NODELAY) && defined(CURL_TCP_NODELAY_SUPPORTED)
84
0
  curl_socklen_t onoff = (curl_socklen_t)1;
85
0
  int level = IPPROTO_TCP;
86
0
  char buffer[STRERROR_LEN];
87
88
0
  if(setsockopt(sockfd, level, TCP_NODELAY,
89
0
                (void *)&onoff, sizeof(onoff)) < 0)
90
0
    CURL_TRC_CF(data, cf, "Could not set TCP_NODELAY: %s",
91
0
                curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
92
#else
93
  (void)cf;
94
  (void)data;
95
  (void)sockfd;
96
#endif
97
0
}
98
99
#ifdef SO_NOSIGPIPE
100
/* The preferred method on macOS (10.2 and later) to prevent SIGPIPEs when
101
   sending data to a dead peer (instead of relying on the 4th argument to send
102
   being MSG_NOSIGNAL). Possibly also existing and in use on other BSD
103
   systems? */
104
static void nosigpipe(struct Curl_cfilter *cf,
105
                      struct Curl_easy *data,
106
                      curl_socket_t sockfd)
107
{
108
  int onoff = 1;
109
  if(setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE,
110
                (void *)&onoff, sizeof(onoff)) < 0) {
111
#ifndef CURL_DISABLE_VERBOSE_STRINGS
112
    char buffer[STRERROR_LEN];
113
    CURL_TRC_CF(data, cf, "Could not set SO_NOSIGPIPE: %s",
114
                curlx_strerror(SOCKERRNO, buffer, sizeof(buffer)));
115
#else
116
    (void)cf;
117
    (void)data;
118
#endif
119
  }
120
}
121
#else
122
0
#define nosigpipe(x, y, z) Curl_nop_stmt
123
#endif
124
125
#if defined(USE_WINSOCK) || \
126
   (defined(__sun) && !defined(TCP_KEEPIDLE)) || \
127
   (defined(__DragonFly__) && __DragonFly_version < 500702) || \
128
   (defined(_WIN32) && !defined(TCP_KEEPIDLE))
129
/* Solaris < 11.4, DragonFlyBSD < 500702 and Windows < 10.0.16299
130
 * use millisecond units. */
131
#define KEEPALIVE_FACTOR(x) (x *= 1000)
132
#else
133
#define KEEPALIVE_FACTOR(x)
134
#endif
135
136
static void tcpkeepalive(struct Curl_cfilter *cf,
137
                         struct Curl_easy *data,
138
                         curl_socket_t sockfd)
139
0
{
140
0
  int optval = data->set.tcp_keepalive ? 1 : 0;
141
142
  /* only set IDLE and INTVL if setting KEEPALIVE is successful */
143
0
  if(setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
144
0
                (void *)&optval, sizeof(optval)) < 0) {
145
0
    CURL_TRC_CF(data, cf, "Failed to set SO_KEEPALIVE on fd "
146
0
                "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
147
0
  }
148
0
  else {
149
#ifdef USE_WINSOCK
150
    /* Windows 10, version 1709 (10.0.16299) and later versions can use
151
       setsockopt() TCP_KEEP*. Older versions return with failure. */
152
    if(curlx_verify_windows_version(10, 0, 16299, PLATFORM_WINNT,
153
                                    VERSION_GREATER_THAN_EQUAL)) {
154
      CURL_TRC_CF(data, cf, "Set TCP_KEEP* on fd=%" FMT_SOCKET_T, sockfd);
155
      optval = curlx_sltosi(data->set.tcp_keepidle);
156
/* Offered by mingw-w64 v12+. MS SDK 6.0A+. */
157
#ifndef TCP_KEEPALIVE
158
#define TCP_KEEPALIVE 3
159
#endif
160
/* Offered by mingw-w64 v12+. MS SDK ~10+/~VS2017+. */
161
#ifndef TCP_KEEPCNT
162
#define TCP_KEEPCNT 16
163
#endif
164
#ifndef TCP_KEEPIDLE
165
#define TCP_KEEPIDLE TCP_KEEPALIVE
166
#endif
167
#ifndef TCP_KEEPINTVL
168
#define TCP_KEEPINTVL 17
169
#endif
170
      if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE,
171
                    (const char *)&optval, sizeof(optval)) < 0) {
172
        CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPIDLE on fd "
173
                    "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
174
      }
175
      optval = curlx_sltosi(data->set.tcp_keepintvl);
176
      if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL,
177
                    (const char *)&optval, sizeof(optval)) < 0) {
178
        CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPINTVL on fd "
179
                    "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
180
      }
181
      optval = curlx_sltosi(data->set.tcp_keepcnt);
182
      if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT,
183
                    (const char *)&optval, sizeof(optval)) < 0) {
184
        CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPCNT on fd "
185
                    "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
186
      }
187
    }
188
    else {
189
/* Offered by mingw-w64 and MS SDK. Latter only when targeting Win7+. */
190
#ifndef SIO_KEEPALIVE_VALS
191
#define SIO_KEEPALIVE_VALS  _WSAIOW(IOC_VENDOR, 4)
192
      struct tcp_keepalive {
193
        u_long onoff;
194
        u_long keepalivetime;
195
        u_long keepaliveinterval;
196
      };
197
#endif
198
      struct tcp_keepalive vals;
199
      DWORD dummy;
200
      vals.onoff = 1;
201
      optval = curlx_sltosi(data->set.tcp_keepidle);
202
      KEEPALIVE_FACTOR(optval);
203
      vals.keepalivetime = (u_long)optval;
204
      optval = curlx_sltosi(data->set.tcp_keepintvl);
205
      KEEPALIVE_FACTOR(optval);
206
      vals.keepaliveinterval = (u_long)optval;
207
      if(WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, (LPVOID)&vals, sizeof(vals),
208
                  NULL, 0, &dummy, NULL, NULL) != 0) {
209
        CURL_TRC_CF(data, cf, "Failed to set SIO_KEEPALIVE_VALS on fd "
210
                    "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
211
      }
212
    }
213
#else /* !USE_WINSOCK */
214
0
#ifdef TCP_KEEPIDLE
215
0
    optval = curlx_sltosi(data->set.tcp_keepidle);
216
0
    KEEPALIVE_FACTOR(optval);
217
0
    if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE,
218
0
                  (void *)&optval, sizeof(optval)) < 0) {
219
0
      CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPIDLE on fd "
220
0
                  "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
221
0
    }
222
#elif defined(TCP_KEEPALIVE)
223
    /* macOS style */
224
    optval = curlx_sltosi(data->set.tcp_keepidle);
225
    KEEPALIVE_FACTOR(optval);
226
    if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE,
227
                  (void *)&optval, sizeof(optval)) < 0) {
228
      CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPALIVE on fd "
229
                  "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
230
    }
231
#elif defined(TCP_KEEPALIVE_THRESHOLD)
232
    /* Solaris <11.4 style */
233
    optval = curlx_sltosi(data->set.tcp_keepidle);
234
    KEEPALIVE_FACTOR(optval);
235
    if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE_THRESHOLD,
236
                  (void *)&optval, sizeof(optval)) < 0) {
237
      CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPALIVE_THRESHOLD on fd "
238
                  "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
239
    }
240
#endif
241
0
#ifdef TCP_KEEPINTVL
242
0
    optval = curlx_sltosi(data->set.tcp_keepintvl);
243
0
    KEEPALIVE_FACTOR(optval);
244
0
    if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL,
245
0
                  (void *)&optval, sizeof(optval)) < 0) {
246
0
      CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPINTVL on fd "
247
0
                  "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
248
0
    }
249
#elif defined(TCP_KEEPALIVE_ABORT_THRESHOLD)
250
    /* Solaris <11.4 style */
251
    /* TCP_KEEPALIVE_ABORT_THRESHOLD should equal to
252
     * TCP_KEEPCNT * TCP_KEEPINTVL on other platforms.
253
     * The default value of TCP_KEEPCNT is 9 on Linux,
254
     * 8 on *BSD/macOS, 5 or 10 on Windows. We use the
255
     * default config for Solaris <11.4 because there is
256
     * no default value for TCP_KEEPCNT on Solaris 11.4.
257
     *
258
     * Note that the consequent probes will not be sent
259
     * at equal intervals on Solaris, but will be sent
260
     * using the exponential backoff algorithm. */
261
    optval = curlx_sltosi(data->set.tcp_keepcnt) *
262
             curlx_sltosi(data->set.tcp_keepintvl);
263
    KEEPALIVE_FACTOR(optval);
264
    if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD,
265
                  (void *)&optval, sizeof(optval)) < 0) {
266
      CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPALIVE_ABORT_THRESHOLD"
267
                  " on fd %" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
268
    }
269
#endif
270
0
#ifdef TCP_KEEPCNT
271
0
    optval = curlx_sltosi(data->set.tcp_keepcnt);
272
0
    if(setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT,
273
0
                  (void *)&optval, sizeof(optval)) < 0) {
274
0
      CURL_TRC_CF(data, cf, "Failed to set TCP_KEEPCNT on fd "
275
0
                  "%" FMT_SOCKET_T ": errno %d", sockfd, SOCKERRNO);
276
0
    }
277
0
#endif
278
0
#endif /* USE_WINSOCK */
279
0
  }
280
0
}
281
282
/**
283
 * Assign the address `ai` to the Curl_sockaddr_ex `dest` and
284
 * set the transport used.
285
 */
286
static CURLcode sock_assign_addr(struct Curl_sockaddr_ex *dest,
287
                                 const struct Curl_addrinfo *ai,
288
                                 uint8_t transport)
289
0
{
290
  /*
291
   * The Curl_sockaddr_ex structure is basically libcurl's external API
292
   * curl_sockaddr structure with enough space available to directly hold
293
   * any protocol-specific address structures. The variable declared here
294
   * will be used to pass / receive data to/from the fopensocket callback
295
   * if this has been set, before that, it is initialized from parameters.
296
   */
297
0
  dest->family = ai->ai_family;
298
0
  switch(transport) {
299
0
  case TRNSPRT_TCP:
300
0
    dest->socktype = SOCK_STREAM;
301
0
    dest->protocol = IPPROTO_TCP;
302
0
    break;
303
0
  case TRNSPRT_UNIX:
304
0
    dest->socktype = SOCK_STREAM;
305
0
    dest->protocol = IPPROTO_IP;
306
0
    break;
307
0
  default: /* UDP and QUIC */
308
0
    dest->socktype = SOCK_DGRAM;
309
0
    dest->protocol = IPPROTO_UDP;
310
0
    break;
311
0
  }
312
0
  dest->addrlen = (unsigned int)ai->ai_addrlen;
313
314
0
  DEBUGASSERT(dest->addrlen <= sizeof(dest->curl_sa_addrbuf));
315
0
  if(dest->addrlen > sizeof(dest->curl_sa_addrbuf))
316
0
    return CURLE_TOO_LARGE;
317
318
0
  memcpy(&dest->curl_sa_addrbuf, ai->ai_addr, dest->addrlen);
319
0
  return CURLE_OK;
320
0
}
321
322
static CURLcode socket_open(struct Curl_easy *data,
323
                            struct Curl_sockaddr_ex *addr,
324
                            curl_socket_t *sockfd)
325
0
{
326
0
  char errbuf[STRERROR_LEN];
327
328
0
  DEBUGASSERT(data);
329
0
  DEBUGASSERT(data->conn);
330
0
  if(data->set.fopensocket) {
331
    /*
332
     * If the opensocket callback is set, all the destination address
333
     * information is passed to the callback. Depending on this information the
334
     * callback may opt to abort the connection, this is indicated returning
335
     * CURL_SOCKET_BAD; otherwise it will return a not-connected socket. When
336
     * the callback returns a valid socket the destination address information
337
     * might have been changed and this 'new' address will actually be used
338
     * here to connect.
339
     */
340
0
    Curl_set_in_callback(data, TRUE);
341
0
    *sockfd = data->set.fopensocket(data->set.opensocket_client,
342
0
                                    CURLSOCKTYPE_IPCXN,
343
0
                                    (struct curl_sockaddr *)addr);
344
0
    Curl_set_in_callback(data, FALSE);
345
0
  }
346
0
  else {
347
    /* opensocket callback not set, so simply create the socket now */
348
0
    *sockfd = CURL_SOCKET(addr->family, addr->socktype, addr->protocol);
349
0
    if((*sockfd == CURL_SOCKET_BAD) && (SOCKERRNO == SOCKENOMEM))
350
0
      return CURLE_OUT_OF_MEMORY;
351
0
  }
352
353
0
  if(*sockfd == CURL_SOCKET_BAD) {
354
    /* no socket, no connection */
355
0
    failf(data, "failed to open socket: %s",
356
0
          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
357
0
    return CURLE_COULDNT_CONNECT;
358
0
  }
359
360
0
#ifdef HAVE_FCNTL
361
0
  if(fcntl(*sockfd, F_SETFD, FD_CLOEXEC) < 0) {
362
0
    failf(data, "fcntl set CLOEXEC: %s",
363
0
          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
364
0
    close(*sockfd);
365
0
    *sockfd = CURL_SOCKET_BAD;
366
0
    return CURLE_COULDNT_CONNECT;
367
0
  }
368
0
#endif
369
370
0
#if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
371
0
  if(data->conn->scope_id && (addr->family == AF_INET6)) {
372
0
    struct sockaddr_in6 * const sa6 = (void *)&addr->curl_sa_addr;
373
0
    sa6->sin6_scope_id = data->conn->scope_id;
374
0
  }
375
0
#endif
376
0
  return CURLE_OK;
377
0
}
378
379
/*
380
 * Create a socket based on info from 'conn' and 'ai'.
381
 *
382
 * 'addr' should be a pointer to the correct struct to get data back, or NULL.
383
 * 'sockfd' must be a pointer to a socket descriptor.
384
 *
385
 * If the open socket callback is set, used that!
386
 *
387
 */
388
CURLcode Curl_socket_open(struct Curl_easy *data,
389
                          const struct Curl_addrinfo *ai,
390
                          struct Curl_sockaddr_ex *addr,
391
                          uint8_t transport,
392
                          curl_socket_t *sockfd)
393
0
{
394
0
  struct Curl_sockaddr_ex dummy;
395
0
  CURLcode result;
396
397
0
  if(!addr)
398
    /* if the caller does not want info back, use a local temp copy */
399
0
    addr = &dummy;
400
401
0
  result = sock_assign_addr(addr, ai, transport);
402
0
  if(result)
403
0
    return result;
404
405
0
  return socket_open(data, addr, sockfd);
406
0
}
407
408
static int socket_close(struct Curl_easy *data, struct connectdata *conn,
409
                        int use_callback, curl_socket_t sock)
410
0
{
411
0
  if(CURL_SOCKET_BAD == sock)
412
0
    return 0;
413
414
0
  if(use_callback && conn && conn->fclosesocket) {
415
0
    int rc;
416
0
    Curl_multi_will_close(data, sock);
417
0
    Curl_set_in_callback(data, TRUE);
418
0
    rc = conn->fclosesocket(conn->closesocket_client, sock);
419
0
    Curl_set_in_callback(data, FALSE);
420
0
    return rc;
421
0
  }
422
423
0
  if(conn)
424
    /* tell the multi-socket code about this */
425
0
    Curl_multi_will_close(data, sock);
426
427
0
  sclose(sock);
428
429
0
  return 0;
430
0
}
431
432
/*
433
 * Close a socket.
434
 *
435
 * 'conn' can be NULL, beware!
436
 */
437
int Curl_socket_close(struct Curl_easy *data, struct connectdata *conn,
438
                      curl_socket_t sock)
439
0
{
440
0
  return socket_close(data, conn, FALSE, sock);
441
0
}
442
443
#ifdef USE_WINSOCK
444
/* When you run a program that uses the Windows Sockets API, you may
445
   experience slow performance when you copy data to a TCP server.
446
447
   https://learn.microsoft.com/troubleshoot/windows-server/networking/slow-performance-copy-data-tcp-server-sockets-api
448
449
   Work-around: Make the Socket Send Buffer Size Larger Than the Program Send
450
   Buffer Size
451
452
   The problem described in this knowledge-base is applied only to pre-Vista
453
   Windows. Following function trying to detect OS version and skips
454
   SO_SNDBUF adjustment for Windows Vista and above.
455
*/
456
457
void Curl_sndbuf_init(curl_socket_t sockfd)
458
{
459
  int val = CURL_MAX_WRITE_SIZE + 32;
460
  int curval = 0;
461
  int curlen = sizeof(curval);
462
463
  if(Curl_isVistaOrGreater)
464
    return;
465
466
  if(getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (char *)&curval, &curlen) == 0)
467
    if(curval > val)
468
      return;
469
470
  setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, (const char *)&val, sizeof(val));
471
}
472
#endif /* USE_WINSOCK */
473
474
/*
475
 * Curl_parse_interface()
476
 *
477
 * This is used to parse interface argument in the following formats.
478
 * In all the examples, `host` can be an IP address or a hostname.
479
 *
480
 *   <iface_or_host> - can be either an interface name or a host.
481
 *   if!<iface> - interface name.
482
 *   host!<host> - hostname.
483
 *   ifhost!<iface>!<host> - interface name and hostname.
484
 *
485
 * Parameters:
486
 *
487
 * input  [in]     - input string.
488
 * len    [in]     - length of the input string.
489
 * dev    [in/out] - address where a pointer to newly allocated memory
490
 *                   holding the interface-or-host will be stored upon
491
 *                   completion.
492
 * iface  [in/out] - address where a pointer to newly allocated memory
493
 *                   holding the interface will be stored upon completion.
494
 * host   [in/out] - address where a pointer to newly allocated memory
495
 *                   holding the host will be stored upon completion.
496
 *
497
 * Returns CURLE_OK on success.
498
 */
499
CURLcode Curl_parse_interface(const char *input,
500
                              char **dev, char **iface, char **host)
501
0
{
502
0
  static const char if_prefix[] = "if!";
503
0
  static const char host_prefix[] = "host!";
504
0
  static const char if_host_prefix[] = "ifhost!";
505
0
  size_t len;
506
507
0
  DEBUGASSERT(dev);
508
0
  DEBUGASSERT(iface);
509
0
  DEBUGASSERT(host);
510
511
0
  len = strlen(input);
512
0
  if(len > 512)
513
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
514
515
0
  if(!strncmp(if_prefix, input, strlen(if_prefix))) {
516
0
    input += strlen(if_prefix);
517
0
    if(!*input)
518
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
519
0
    *iface = Curl_memdup0(input, len - strlen(if_prefix));
520
0
    return *iface ? CURLE_OK : CURLE_OUT_OF_MEMORY;
521
0
  }
522
0
  else if(!strncmp(host_prefix, input, strlen(host_prefix))) {
523
0
    input += strlen(host_prefix);
524
0
    if(!*input)
525
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
526
0
    *host = Curl_memdup0(input, len - strlen(host_prefix));
527
0
    return *host ? CURLE_OK : CURLE_OUT_OF_MEMORY;
528
0
  }
529
0
  else if(!strncmp(if_host_prefix, input, strlen(if_host_prefix))) {
530
0
    const char *host_part;
531
0
    input += strlen(if_host_prefix);
532
0
    len -= strlen(if_host_prefix);
533
0
    host_part = memchr(input, '!', len);
534
0
    if(!host_part || !*(host_part + 1))
535
0
      return CURLE_BAD_FUNCTION_ARGUMENT;
536
0
    *iface = Curl_memdup0(input, host_part - input);
537
0
    if(!*iface)
538
0
      return CURLE_OUT_OF_MEMORY;
539
0
    ++host_part;
540
0
    *host = Curl_memdup0(host_part, len - (host_part - input));
541
0
    if(!*host) {
542
0
      curlx_free(*iface);
543
0
      *iface = NULL;
544
0
      return CURLE_OUT_OF_MEMORY;
545
0
    }
546
0
    return CURLE_OK;
547
0
  }
548
549
0
  if(!*input)
550
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
551
0
  *dev = Curl_memdup0(input, len);
552
0
  return *dev ? CURLE_OK : CURLE_OUT_OF_MEMORY;
553
0
}
554
555
#ifndef CURL_DISABLE_BINDLOCAL
556
static CURLcode bindlocal(struct Curl_easy *data, struct connectdata *conn,
557
                          curl_socket_t sockfd, int af, unsigned int scope)
558
0
{
559
0
  struct Curl_sockaddr_storage sa;
560
0
  struct sockaddr *sock = (struct sockaddr *)&sa;  /* bind to this address */
561
0
  curl_socklen_t sizeof_sa = 0; /* size of the data sock points to */
562
0
  struct sockaddr_in *si4 = (struct sockaddr_in *)&sa;
563
0
#ifdef USE_IPV6
564
0
  struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)&sa;
565
0
#endif
566
567
0
  struct Curl_dns_entry *h = NULL;
568
0
  unsigned short port = data->set.localport; /* use this port number, 0 for
569
                                                "random" */
570
  /* how many port numbers to try to bind to, increasing one at a time */
571
0
  int portnum = data->set.localportrange;
572
0
  const char *dev = data->set.str[STRING_DEVICE];
573
0
  const char *iface_input = data->set.str[STRING_INTERFACE];
574
0
  const char *host_input = data->set.str[STRING_BINDHOST];
575
0
  const char *iface = iface_input ? iface_input : dev;
576
0
  const char *host = host_input ? host_input : dev;
577
0
  int error;
578
0
#ifdef IP_BIND_ADDRESS_NO_PORT
579
0
  int on = 1;
580
0
#endif
581
#ifndef USE_IPV6
582
  (void)scope;
583
#endif
584
585
  /*************************************************************
586
   * Select device to bind socket to
587
   *************************************************************/
588
0
  if(!iface && !host && !port)
589
    /* no local kind of binding was requested */
590
0
    return CURLE_OK;
591
0
  else if(iface && (strlen(iface) >= 255))
592
0
    return CURLE_BAD_FUNCTION_ARGUMENT;
593
594
0
  memset(&sa, 0, sizeof(struct Curl_sockaddr_storage));
595
596
0
  if(iface || host) {
597
0
    char myhost[256] = "";
598
0
    int done = 0; /* -1 for error, 1 for address found */
599
0
    if2ip_result_t if2ip_result = IF2IP_NOT_FOUND;
600
601
0
#ifdef SO_BINDTODEVICE
602
0
    if(iface) {
603
      /*
604
       * This binds the local socket to a particular interface. This will
605
       * force even requests to other local interfaces to go out the external
606
       * interface. Only bind to the interface when specified as interface,
607
       * not just as a hostname or ip address.
608
       *
609
       * The interface might be a VRF, eg: vrf-blue, which means it cannot be
610
       * converted to an IP address and would fail Curl_if2ip. Simply try to
611
       * use it straight away.
612
       */
613
0
      if(setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE,
614
0
                    iface, (curl_socklen_t)strlen(iface) + 1) == 0) {
615
        /* This is often "errno 1, error: Operation not permitted" if you are
616
         * not running as root or another suitable privileged user. If it
617
         * succeeds it means the parameter was a valid interface and not an IP
618
         * address. Return immediately.
619
         */
620
0
        if(!host_input) {
621
0
          infof(data, "socket successfully bound to interface '%s'", iface);
622
0
          return CURLE_OK;
623
0
        }
624
0
      }
625
0
    }
626
0
#endif
627
0
    if(!host_input) {
628
      /* Discover IP from input device, then bind to it */
629
0
      if2ip_result = Curl_if2ip(af,
630
0
#ifdef USE_IPV6
631
0
                                scope, conn->scope_id,
632
0
#endif
633
0
                                iface, myhost, sizeof(myhost));
634
0
    }
635
0
    switch(if2ip_result) {
636
0
    case IF2IP_NOT_FOUND:
637
0
      if(iface_input && !host_input) {
638
        /* Do not fall back to treating it as a hostname */
639
0
        char buffer[STRERROR_LEN];
640
0
        data->state.os_errno = error = SOCKERRNO;
641
0
        failf(data, "Could not bind to interface '%s' with errno %d: %s",
642
0
              iface, error, curlx_strerror(error, buffer, sizeof(buffer)));
643
0
        return CURLE_INTERFACE_FAILED;
644
0
      }
645
0
      break;
646
0
    case IF2IP_AF_NOT_SUPPORTED:
647
      /* Signal the caller to try another address family if available */
648
0
      return CURLE_UNSUPPORTED_PROTOCOL;
649
0
    case IF2IP_FOUND:
650
      /*
651
       * We now have the numerical IP address in the 'myhost' buffer
652
       */
653
0
      host = myhost;
654
0
      infof(data, "Local Interface %s is ip %s using address family %i",
655
0
            iface, host, af);
656
0
      done = 1;
657
0
      break;
658
0
    }
659
0
    if(!iface_input || host_input) {
660
      /*
661
       * This was not an interface, resolve the name as a hostname
662
       * or IP number
663
       *
664
       * Temporarily force name resolution to use only the address type
665
       * of the connection. The resolve functions should really be changed
666
       * to take a type parameter instead.
667
       */
668
0
      int ip_version = (af == AF_INET) ?
669
0
                       CURL_IPRESOLVE_V4 : CURL_IPRESOLVE_WHATEVER;
670
0
#ifdef USE_IPV6
671
0
      if(af == AF_INET6)
672
0
        ip_version = CURL_IPRESOLVE_V6;
673
0
#endif
674
675
0
      (void)Curl_resolv_blocking(data, host, 80, ip_version, &h);
676
0
      if(h) {
677
0
        int h_af = h->addr->ai_family;
678
        /* convert the resolved address, sizeof myhost >= INET_ADDRSTRLEN */
679
0
        Curl_printable_address(h->addr, myhost, sizeof(myhost));
680
0
        infof(data, "Name '%s' family %i resolved to '%s' family %i",
681
0
              host, af, myhost, h_af);
682
0
        Curl_resolv_unlink(data, &h); /* this will NULL, potential free h */
683
0
        if(af != h_af) {
684
          /* bad IP version combo, signal the caller to try another address
685
             family if available */
686
0
          return CURLE_UNSUPPORTED_PROTOCOL;
687
0
        }
688
0
        done = 1;
689
0
      }
690
0
      else {
691
        /*
692
         * provided dev was no interface (or interfaces are not supported
693
         * e.g. Solaris) no ip address and no domain we fail here
694
         */
695
0
        done = -1;
696
0
      }
697
0
    }
698
699
0
    if(done > 0) {
700
0
#ifdef USE_IPV6
701
      /* IPv6 address */
702
0
      if(af == AF_INET6) {
703
0
#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
704
0
        char *scope_ptr = strchr(myhost, '%');
705
0
        if(scope_ptr)
706
0
          *(scope_ptr++) = '\0';
707
0
#endif
708
0
        if(curlx_inet_pton(AF_INET6, myhost, &si6->sin6_addr) > 0) {
709
0
          si6->sin6_family = AF_INET6;
710
0
          si6->sin6_port = htons(port);
711
0
#ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID
712
0
          if(scope_ptr) {
713
            /* The "myhost" string either comes from Curl_if2ip or from
714
               Curl_printable_address. The latter returns only numeric scope
715
               IDs and the former returns none at all. So the scope ID, if
716
               present, is known to be numeric */
717
0
            curl_off_t scope_id;
718
0
            if(curlx_str_number((const char **)CURL_UNCONST(&scope_ptr),
719
0
                                &scope_id, UINT_MAX))
720
0
              return CURLE_UNSUPPORTED_PROTOCOL;
721
0
            si6->sin6_scope_id = (unsigned int)scope_id;
722
0
          }
723
0
#endif
724
0
        }
725
0
        sizeof_sa = sizeof(struct sockaddr_in6);
726
0
      }
727
0
      else
728
0
#endif
729
      /* IPv4 address */
730
0
      if((af == AF_INET) &&
731
0
         (curlx_inet_pton(AF_INET, myhost, &si4->sin_addr) > 0)) {
732
0
        si4->sin_family = AF_INET;
733
0
        si4->sin_port = htons(port);
734
0
        sizeof_sa = sizeof(struct sockaddr_in);
735
0
      }
736
0
    }
737
738
0
    if(done < 1) {
739
      /* errorbuf is set false so failf will overwrite any message already in
740
         the error buffer, so the user receives this error message instead of a
741
         generic resolve error. */
742
0
      char buffer[STRERROR_LEN];
743
0
      data->state.errorbuf = FALSE;
744
0
      data->state.os_errno = error = SOCKERRNO;
745
0
      failf(data, "Could not bind to '%s' with errno %d: %s", host,
746
0
            error, curlx_strerror(error, buffer, sizeof(buffer)));
747
0
      return CURLE_INTERFACE_FAILED;
748
0
    }
749
0
  }
750
0
  else {
751
    /* no device was given, prepare sa to match af's needs */
752
0
#ifdef USE_IPV6
753
0
    if(af == AF_INET6) {
754
0
      si6->sin6_family = AF_INET6;
755
0
      si6->sin6_port = htons(port);
756
0
      sizeof_sa = sizeof(struct sockaddr_in6);
757
0
    }
758
0
    else
759
0
#endif
760
0
    if(af == AF_INET) {
761
0
      si4->sin_family = AF_INET;
762
0
      si4->sin_port = htons(port);
763
0
      sizeof_sa = sizeof(struct sockaddr_in);
764
0
    }
765
0
  }
766
0
#ifdef IP_BIND_ADDRESS_NO_PORT
767
0
  (void)setsockopt(sockfd, SOL_IP, IP_BIND_ADDRESS_NO_PORT, &on, sizeof(on));
768
0
#endif
769
0
  for(;;) {
770
0
    if(bind(sockfd, sock, sizeof_sa) >= 0) {
771
      /* we succeeded to bind */
772
0
      infof(data, "Local port: %hu", port);
773
0
      conn->bits.bound = TRUE;
774
0
      return CURLE_OK;
775
0
    }
776
777
0
    if(--portnum > 0) {
778
0
      port++; /* try next port */
779
0
      if(port == 0)
780
0
        break;
781
0
      infof(data, "Bind to local port %d failed, trying next", port - 1);
782
      /* We reuse/clobber the port variable here below */
783
0
      if(sock->sa_family == AF_INET)
784
0
        si4->sin_port = htons(port);
785
0
#ifdef USE_IPV6
786
0
      else
787
0
        si6->sin6_port = htons(port);
788
0
#endif
789
0
    }
790
0
    else
791
0
      break;
792
0
  }
793
0
  {
794
0
    char buffer[STRERROR_LEN];
795
0
    data->state.os_errno = error = SOCKERRNO;
796
0
    failf(data, "bind failed with errno %d: %s",
797
0
          error, curlx_strerror(error, buffer, sizeof(buffer)));
798
0
  }
799
800
0
  return CURLE_INTERFACE_FAILED;
801
0
}
802
#endif
803
804
/*
805
 * verifyconnect() returns TRUE if the connect really has happened.
806
 */
807
static bool verifyconnect(curl_socket_t sockfd, int *error)
808
0
{
809
0
  bool rc = TRUE;
810
0
#ifdef SO_ERROR
811
0
  int err = 0;
812
0
  curl_socklen_t errSize = sizeof(err);
813
814
#ifdef _WIN32
815
  /*
816
   * In October 2003 we effectively nullified this function on Windows due to
817
   * problems with it using all CPU in multi-threaded cases.
818
   *
819
   * In May 2004, we brought it back to offer more info back on connect
820
   * failures. We could reproduce the former problems with this function, but
821
   * could avoid them by adding this SleepEx() call below:
822
   *
823
   *    "I do not have Rational Quantify, but the hint from his post was
824
   *    ntdll::NtRemoveIoCompletion(). I would assume the SleepEx (or maybe
825
   *    just Sleep(0) would be enough?) would release whatever
826
   *    mutex/critical-section the ntdll call is waiting on.
827
   *
828
   *    Someone got to verify this on Win-NT 4.0, 2000."
829
   */
830
  SleepEx(0, FALSE);
831
#endif
832
833
0
  if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&err, &errSize))
834
0
    err = SOCKERRNO;
835
#if defined(EBADIOCTL) && defined(__minix)
836
  /* Minix 3.1.x does not support getsockopt on UDP sockets */
837
  if(EBADIOCTL == err) {
838
    SET_SOCKERRNO(0);
839
    err = 0;
840
  }
841
#endif
842
0
  if((err == 0) || (SOCKEISCONN == err))
843
    /* we are connected, awesome! */
844
0
    rc = TRUE;
845
0
  else
846
    /* This was not a successful connect */
847
0
    rc = FALSE;
848
0
  if(error)
849
0
    *error = err;
850
#else
851
  (void)sockfd;
852
  if(error)
853
    *error = SOCKERRNO;
854
#endif
855
0
  return rc;
856
0
}
857
858
/**
859
 * Determine the curl code for a socket connect() == -1 with errno.
860
 */
861
static CURLcode socket_connect_result(struct Curl_easy *data,
862
                                      const char *ipaddress, int error)
863
0
{
864
0
  switch(error) {
865
0
  case SOCKEINPROGRESS:
866
0
  case SOCKEWOULDBLOCK:
867
0
#ifdef EAGAIN
868
#if (EAGAIN) != (SOCKEWOULDBLOCK)
869
    /* On some platforms EAGAIN and EWOULDBLOCK are the
870
     * same value, and on others they are different, hence
871
     * the odd #if
872
     */
873
  case EAGAIN:
874
#endif
875
0
#endif
876
0
    return CURLE_OK;
877
878
0
  default:
879
    /* unknown error, fallthrough and try another address! */
880
#ifdef CURL_DISABLE_VERBOSE_STRINGS
881
    (void)ipaddress;
882
#else
883
0
    {
884
0
      char buffer[STRERROR_LEN];
885
0
      infof(data, "Immediate connect fail for %s: %s", ipaddress,
886
0
            curlx_strerror(error, buffer, sizeof(buffer)));
887
0
    }
888
0
#endif
889
0
    data->state.os_errno = error;
890
    /* connect failed */
891
0
    return CURLE_COULDNT_CONNECT;
892
0
  }
893
0
}
894
895
struct cf_socket_ctx {
896
  uint8_t transport;
897
  struct Curl_sockaddr_ex addr;      /* address to connect to */
898
  curl_socket_t sock;                /* current attempt socket */
899
  struct ip_quadruple ip;            /* The IP quadruple 2x(addr+port) */
900
  struct curltime started_at;        /* when socket was created */
901
  struct curltime connected_at;      /* when socket connected/got first byte */
902
  struct curltime first_byte_at;     /* when first byte was recvd */
903
#ifdef USE_WINSOCK
904
  struct curltime last_sndbuf_query_at;  /* when SO_SNDBUF last queried */
905
  ULONG sndbuf_size;                     /* the last set SO_SNDBUF size */
906
#endif
907
  int error;                         /* errno of last failure or 0 */
908
#ifdef DEBUGBUILD
909
  int wblock_percent;                /* percent of writes doing EAGAIN */
910
  int wpartial_percent;              /* percent of bytes written in send */
911
  int rblock_percent;                /* percent of reads doing EAGAIN */
912
  size_t recv_max;                   /* max enforced read size */
913
#endif
914
  BIT(got_first_byte);               /* if first byte was received */
915
  BIT(listening);                    /* socket is listening */
916
  BIT(accepted);                     /* socket was accepted, not connected */
917
  BIT(sock_connected);               /* socket is "connected", e.g. in UDP */
918
  BIT(active);
919
};
920
921
static CURLcode cf_socket_ctx_init(struct cf_socket_ctx *ctx,
922
                                   const struct Curl_addrinfo *ai,
923
                                   uint8_t transport)
924
0
{
925
0
  CURLcode result;
926
927
0
  memset(ctx, 0, sizeof(*ctx));
928
0
  ctx->sock = CURL_SOCKET_BAD;
929
0
  ctx->transport = transport;
930
931
0
  result = sock_assign_addr(&ctx->addr, ai, transport);
932
0
  if(result)
933
0
    return result;
934
935
#ifdef DEBUGBUILD
936
  {
937
    const char *p = getenv("CURL_DBG_SOCK_WBLOCK");
938
    if(p) {
939
      curl_off_t l;
940
      if(!curlx_str_number(&p, &l, 100))
941
        ctx->wblock_percent = (int)l;
942
    }
943
    p = getenv("CURL_DBG_SOCK_WPARTIAL");
944
    if(p) {
945
      curl_off_t l;
946
      if(!curlx_str_number(&p, &l, 100))
947
        ctx->wpartial_percent = (int)l;
948
    }
949
    p = getenv("CURL_DBG_SOCK_RBLOCK");
950
    if(p) {
951
      curl_off_t l;
952
      if(!curlx_str_number(&p, &l, 100))
953
        ctx->rblock_percent = (int)l;
954
    }
955
    p = getenv("CURL_DBG_SOCK_RMAX");
956
    if(p) {
957
      curl_off_t l;
958
      if(!curlx_str_number(&p, &l, CURL_OFF_T_MAX))
959
        ctx->recv_max = (size_t)l;
960
    }
961
  }
962
#endif
963
964
0
  return result;
965
0
}
966
967
static void cf_socket_close(struct Curl_cfilter *cf, struct Curl_easy *data)
968
0
{
969
0
  struct cf_socket_ctx *ctx = cf->ctx;
970
971
0
  if(ctx && CURL_SOCKET_BAD != ctx->sock) {
972
0
    CURL_TRC_CF(data, cf, "cf_socket_close, fd=%" FMT_SOCKET_T, ctx->sock);
973
0
    if(ctx->sock == cf->conn->sock[cf->sockindex])
974
0
      cf->conn->sock[cf->sockindex] = CURL_SOCKET_BAD;
975
0
    socket_close(data, cf->conn, !ctx->accepted, ctx->sock);
976
0
    ctx->sock = CURL_SOCKET_BAD;
977
0
    ctx->active = FALSE;
978
0
    memset(&ctx->started_at, 0, sizeof(ctx->started_at));
979
0
    memset(&ctx->connected_at, 0, sizeof(ctx->connected_at));
980
0
  }
981
982
0
  cf->connected = FALSE;
983
0
}
984
985
static CURLcode cf_socket_shutdown(struct Curl_cfilter *cf,
986
                                   struct Curl_easy *data,
987
                                   bool *done)
988
0
{
989
0
  if(cf->connected) {
990
0
    struct cf_socket_ctx *ctx = cf->ctx;
991
992
0
    CURL_TRC_CF(data, cf, "cf_socket_shutdown, fd=%" FMT_SOCKET_T, ctx->sock);
993
    /* On TCP, and when the socket looks well and non-blocking mode
994
     * can be enabled, receive dangling bytes before close to avoid
995
     * entering RST states unnecessarily. */
996
0
    if(ctx->sock != CURL_SOCKET_BAD &&
997
0
       ctx->transport == TRNSPRT_TCP &&
998
0
       (curlx_nonblock(ctx->sock, TRUE) >= 0)) {
999
0
      unsigned char buf[1024];
1000
0
      (void)sread(ctx->sock, buf, sizeof(buf));
1001
0
    }
1002
0
  }
1003
0
  *done = TRUE;
1004
0
  return CURLE_OK;
1005
0
}
1006
1007
static void cf_socket_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
1008
0
{
1009
0
  struct cf_socket_ctx *ctx = cf->ctx;
1010
1011
0
  cf_socket_close(cf, data);
1012
0
  CURL_TRC_CF(data, cf, "destroy");
1013
0
  curlx_free(ctx);
1014
0
  cf->ctx = NULL;
1015
0
}
1016
1017
static void set_local_ip(struct Curl_cfilter *cf,
1018
                         struct Curl_easy *data)
1019
0
{
1020
0
  struct cf_socket_ctx *ctx = cf->ctx;
1021
0
  ctx->ip.local_ip[0] = 0;
1022
0
  ctx->ip.local_port = 0;
1023
1024
0
#ifdef HAVE_GETSOCKNAME
1025
0
  if((ctx->sock != CURL_SOCKET_BAD) &&
1026
0
     !(data->conn->handler->protocol & CURLPROTO_TFTP)) {
1027
    /* TFTP does not connect, so it cannot get the IP like this */
1028
1029
0
    char buffer[STRERROR_LEN];
1030
0
    struct Curl_sockaddr_storage ssloc;
1031
0
    curl_socklen_t slen = sizeof(struct Curl_sockaddr_storage);
1032
1033
0
    memset(&ssloc, 0, sizeof(ssloc));
1034
0
    if(getsockname(ctx->sock, (struct sockaddr *)&ssloc, &slen)) {
1035
0
      int error = SOCKERRNO;
1036
0
      infof(data, "getsockname() failed with errno %d: %s",
1037
0
            error, curlx_strerror(error, buffer, sizeof(buffer)));
1038
0
    }
1039
0
    else if(!Curl_addr2string((struct sockaddr *)&ssloc, slen,
1040
0
                              ctx->ip.local_ip, &ctx->ip.local_port)) {
1041
0
      infof(data, "ssloc inet_ntop() failed with errno %d: %s",
1042
0
            errno, curlx_strerror(errno, buffer, sizeof(buffer)));
1043
0
    }
1044
0
  }
1045
#else
1046
  (void)data;
1047
#endif
1048
0
}
1049
1050
static CURLcode set_remote_ip(struct Curl_cfilter *cf,
1051
                              struct Curl_easy *data)
1052
0
{
1053
0
  struct cf_socket_ctx *ctx = cf->ctx;
1054
1055
  /* store remote address and port used in this connection attempt */
1056
0
  ctx->ip.transport = ctx->transport;
1057
0
  if(!Curl_addr2string(&ctx->addr.curl_sa_addr,
1058
0
                       (curl_socklen_t)ctx->addr.addrlen,
1059
0
                       ctx->ip.remote_ip, &ctx->ip.remote_port)) {
1060
0
    char buffer[STRERROR_LEN];
1061
1062
0
    ctx->error = errno;
1063
    /* malformed address or bug in inet_ntop, try next address */
1064
0
    failf(data, "curl_sa_addr inet_ntop() failed with errno %d: %s",
1065
0
          errno, curlx_strerror(errno, buffer, sizeof(buffer)));
1066
0
    return CURLE_FAILED_INIT;
1067
0
  }
1068
0
  return CURLE_OK;
1069
0
}
1070
1071
static CURLcode cf_socket_open(struct Curl_cfilter *cf,
1072
                               struct Curl_easy *data)
1073
0
{
1074
0
  struct cf_socket_ctx *ctx = cf->ctx;
1075
0
  int error = 0;
1076
0
  bool isconnected = FALSE;
1077
0
  CURLcode result = CURLE_COULDNT_CONNECT;
1078
0
  bool is_tcp;
1079
1080
0
  (void)data;
1081
0
  DEBUGASSERT(ctx->sock == CURL_SOCKET_BAD);
1082
0
  ctx->started_at = *Curl_pgrs_now(data);
1083
0
#ifdef SOCK_NONBLOCK
1084
  /* Do not tuck SOCK_NONBLOCK into socktype when opensocket callback is set
1085
   * because we would not know how socketype is about to be used in the
1086
   * callback, SOCK_NONBLOCK might get factored out before calling socket().
1087
   */
1088
0
  if(!data->set.fopensocket)
1089
0
    ctx->addr.socktype |= SOCK_NONBLOCK;
1090
0
#endif
1091
0
  result = socket_open(data, &ctx->addr, &ctx->sock);
1092
0
#ifdef SOCK_NONBLOCK
1093
  /* Restore the socktype after the socket is created. */
1094
0
  if(!data->set.fopensocket)
1095
0
    ctx->addr.socktype &= ~SOCK_NONBLOCK;
1096
0
#endif
1097
0
  if(result)
1098
0
    goto out;
1099
1100
0
  result = set_remote_ip(cf, data);
1101
0
  if(result)
1102
0
    goto out;
1103
1104
0
#ifdef USE_IPV6
1105
0
  if(ctx->addr.family == AF_INET6) {
1106
#ifdef USE_WINSOCK
1107
    /* Turn on support for IPv4-mapped IPv6 addresses.
1108
     * Linux kernel, NetBSD, FreeBSD, Darwin, lwIP: default is off;
1109
     * Windows Vista and later: default is on;
1110
     * DragonFly BSD: acts like off, and dummy setting;
1111
     * OpenBSD and earlier Windows: unsupported.
1112
     * Linux: controlled by /proc/sys/net/ipv6/bindv6only.
1113
     */
1114
    int on = 0;
1115
    (void)setsockopt(ctx->sock, IPPROTO_IPV6, IPV6_V6ONLY,
1116
                     (void *)&on, sizeof(on));
1117
#endif
1118
0
    infof(data, "  Trying [%s]:%d...", ctx->ip.remote_ip, ctx->ip.remote_port);
1119
0
  }
1120
0
  else
1121
0
#endif
1122
0
    infof(data, "  Trying %s:%d...", ctx->ip.remote_ip, ctx->ip.remote_port);
1123
1124
0
#ifdef USE_IPV6
1125
0
  is_tcp = (ctx->addr.family == AF_INET ||
1126
0
            ctx->addr.family == AF_INET6) &&
1127
0
           ctx->addr.socktype == SOCK_STREAM;
1128
#else
1129
  is_tcp = (ctx->addr.family == AF_INET) &&
1130
           ctx->addr.socktype == SOCK_STREAM;
1131
#endif
1132
0
  if(is_tcp && data->set.tcp_nodelay)
1133
0
    tcpnodelay(cf, data, ctx->sock);
1134
1135
0
  nosigpipe(cf, data, ctx->sock);
1136
1137
0
  Curl_sndbuf_init(ctx->sock);
1138
1139
0
  if(is_tcp && data->set.tcp_keepalive)
1140
0
    tcpkeepalive(cf, data, ctx->sock);
1141
1142
0
  if(data->set.fsockopt) {
1143
    /* activate callback for setting socket options */
1144
0
    Curl_set_in_callback(data, TRUE);
1145
0
    error = data->set.fsockopt(data->set.sockopt_client,
1146
0
                               ctx->sock,
1147
0
                               CURLSOCKTYPE_IPCXN);
1148
0
    Curl_set_in_callback(data, FALSE);
1149
1150
0
    if(error == CURL_SOCKOPT_ALREADY_CONNECTED)
1151
0
      isconnected = TRUE;
1152
0
    else if(error) {
1153
0
      result = CURLE_ABORTED_BY_CALLBACK;
1154
0
      goto out;
1155
0
    }
1156
0
  }
1157
1158
0
#ifndef CURL_DISABLE_BINDLOCAL
1159
  /* possibly bind the local end to an IP, interface or port */
1160
0
  if(ctx->addr.family == AF_INET
1161
0
#ifdef USE_IPV6
1162
0
     || ctx->addr.family == AF_INET6
1163
0
#endif
1164
0
    ) {
1165
0
    result = bindlocal(data, cf->conn, ctx->sock, ctx->addr.family,
1166
0
                       Curl_ipv6_scope(&ctx->addr.curl_sa_addr));
1167
0
    if(result) {
1168
0
      if(result == CURLE_UNSUPPORTED_PROTOCOL) {
1169
        /* The address family is not supported on this interface.
1170
           We can continue trying addresses */
1171
0
        result = CURLE_COULDNT_CONNECT;
1172
0
      }
1173
0
      goto out;
1174
0
    }
1175
0
  }
1176
0
#endif
1177
1178
#ifndef SOCK_NONBLOCK
1179
  /* Set socket non-blocking, must be a non-blocking socket for
1180
   * a non-blocking connect. */
1181
  error = curlx_nonblock(ctx->sock, TRUE);
1182
  if(error < 0) {
1183
    result = CURLE_UNSUPPORTED_PROTOCOL;
1184
    ctx->error = SOCKERRNO;
1185
    goto out;
1186
  }
1187
#else
1188
0
  if(data->set.fopensocket) {
1189
    /* Set socket non-blocking, must be a non-blocking socket for
1190
     * a non-blocking connect. */
1191
0
    error = curlx_nonblock(ctx->sock, TRUE);
1192
0
    if(error < 0) {
1193
0
      result = CURLE_UNSUPPORTED_PROTOCOL;
1194
0
      ctx->error = SOCKERRNO;
1195
0
      goto out;
1196
0
    }
1197
0
  }
1198
0
#endif
1199
0
  ctx->sock_connected = (ctx->addr.socktype != SOCK_DGRAM);
1200
0
out:
1201
0
  if(result) {
1202
0
    if(ctx->sock != CURL_SOCKET_BAD) {
1203
0
      socket_close(data, cf->conn, TRUE, ctx->sock);
1204
0
      ctx->sock = CURL_SOCKET_BAD;
1205
0
    }
1206
0
  }
1207
0
  else if(isconnected) {
1208
0
    set_local_ip(cf, data);
1209
0
    ctx->connected_at = *Curl_pgrs_now(data);
1210
0
    cf->connected = TRUE;
1211
0
  }
1212
0
  CURL_TRC_CF(data, cf, "cf_socket_open() -> %d, fd=%" FMT_SOCKET_T,
1213
0
              result, ctx->sock);
1214
0
  return result;
1215
0
}
1216
1217
static int do_connect(struct Curl_cfilter *cf, struct Curl_easy *data,
1218
                      bool is_tcp_fastopen)
1219
0
{
1220
0
  struct cf_socket_ctx *ctx = cf->ctx;
1221
0
#ifdef TCP_FASTOPEN_CONNECT
1222
0
  int optval = 1;
1223
0
#endif
1224
0
  int rc = -1;
1225
1226
0
  (void)data;
1227
0
  if(is_tcp_fastopen) {
1228
#ifdef CONNECT_DATA_IDEMPOTENT /* Darwin */
1229
#  ifdef HAVE_BUILTIN_AVAILABLE
1230
    /* while connectx function is available since macOS 10.11 / iOS 9,
1231
       it did not have the interface declared correctly until
1232
       Xcode 9 / macOS SDK 10.13 */
1233
    if(__builtin_available(macOS 10.11, iOS 9.0, tvOS 9.0, watchOS 2.0, *)) {
1234
      sa_endpoints_t endpoints;
1235
      endpoints.sae_srcif = 0;
1236
      endpoints.sae_srcaddr = NULL;
1237
      endpoints.sae_srcaddrlen = 0;
1238
      endpoints.sae_dstaddr = &ctx->addr.curl_sa_addr;
1239
      endpoints.sae_dstaddrlen = ctx->addr.addrlen;
1240
1241
      rc = connectx(ctx->sock, &endpoints, SAE_ASSOCID_ANY,
1242
                    CONNECT_RESUME_ON_READ_WRITE | CONNECT_DATA_IDEMPOTENT,
1243
                    NULL, 0, NULL, NULL);
1244
    }
1245
    else {
1246
      rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen);
1247
    }
1248
#  else
1249
    rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen);
1250
#  endif /* HAVE_BUILTIN_AVAILABLE */
1251
#elif defined(TCP_FASTOPEN_CONNECT) /* Linux >= 4.11 */
1252
0
    if(setsockopt(ctx->sock, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
1253
0
                  (void *)&optval, sizeof(optval)) < 0)
1254
0
      CURL_TRC_CF(data, cf, "Failed to enable TCP Fast Open on fd %"
1255
0
                  FMT_SOCKET_T, ctx->sock);
1256
1257
0
    rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen);
1258
#elif defined(MSG_FASTOPEN) /* old Linux */
1259
    if(Curl_conn_is_ssl(cf->conn, cf->sockindex))
1260
      rc = connect(ctx->sock, &ctx->addr.curl_sa_addr, ctx->addr.addrlen);
1261
    else
1262
      rc = 0; /* Do nothing */
1263
#endif
1264
0
  }
1265
0
  else {
1266
0
    rc = connect(ctx->sock, &ctx->addr.curl_sa_addr,
1267
0
                 (curl_socklen_t)ctx->addr.addrlen);
1268
0
  }
1269
0
  return rc;
1270
0
}
1271
1272
static CURLcode cf_tcp_connect(struct Curl_cfilter *cf,
1273
                               struct Curl_easy *data,
1274
                               bool *done)
1275
0
{
1276
0
  struct cf_socket_ctx *ctx = cf->ctx;
1277
0
  CURLcode result = CURLE_COULDNT_CONNECT;
1278
0
  int rc = 0;
1279
1280
0
  (void)data;
1281
0
  if(cf->connected) {
1282
0
    *done = TRUE;
1283
0
    return CURLE_OK;
1284
0
  }
1285
1286
0
  *done = FALSE; /* a negative world view is best */
1287
0
  if(ctx->sock == CURL_SOCKET_BAD) {
1288
0
    int error;
1289
1290
0
    result = cf_socket_open(cf, data);
1291
0
    if(result)
1292
0
      goto out;
1293
1294
0
    if(cf->connected) {
1295
0
      *done = TRUE;
1296
0
      return CURLE_OK;
1297
0
    }
1298
1299
    /* Connect TCP socket */
1300
0
    rc = do_connect(cf, data, cf->conn->bits.tcp_fastopen);
1301
0
    error = SOCKERRNO;
1302
0
    set_local_ip(cf, data);
1303
0
    CURL_TRC_CF(data, cf, "local address %s port %d...",
1304
0
                ctx->ip.local_ip, ctx->ip.local_port);
1305
0
    if(-1 == rc) {
1306
0
      result = socket_connect_result(data, ctx->ip.remote_ip, error);
1307
0
      goto out;
1308
0
    }
1309
0
  }
1310
1311
#ifdef mpeix
1312
  /* Call this function once now, and ignore the results. We do this to
1313
     "clear" the error state on the socket so that we can later read it
1314
     reliably. This is reported necessary on the MPE/iX operating
1315
     system. */
1316
  (void)verifyconnect(ctx->sock, NULL);
1317
#endif
1318
  /* check socket for connect */
1319
0
  rc = SOCKET_WRITABLE(ctx->sock, 0);
1320
1321
0
  if(rc == 0) { /* no connection yet */
1322
0
    CURL_TRC_CF(data, cf, "not connected yet on fd=%" FMT_SOCKET_T, ctx->sock);
1323
0
    return CURLE_OK;
1324
0
  }
1325
0
  else if(rc == CURL_CSELECT_OUT || cf->conn->bits.tcp_fastopen) {
1326
0
    if(verifyconnect(ctx->sock, &ctx->error)) {
1327
      /* we are connected with TCP, awesome! */
1328
0
      ctx->connected_at = *Curl_pgrs_now(data);
1329
0
      set_local_ip(cf, data);
1330
0
      *done = TRUE;
1331
0
      cf->connected = TRUE;
1332
0
      CURL_TRC_CF(data, cf, "connected on fd=%" FMT_SOCKET_T, ctx->sock);
1333
0
      return CURLE_OK;
1334
0
    }
1335
0
  }
1336
0
  else if(rc & CURL_CSELECT_ERR) {
1337
0
    (void)verifyconnect(ctx->sock, &ctx->error);
1338
0
    result = CURLE_COULDNT_CONNECT;
1339
0
  }
1340
1341
0
out:
1342
0
  if(result) {
1343
0
    if(ctx->error) {
1344
0
      set_local_ip(cf, data);
1345
0
      data->state.os_errno = ctx->error;
1346
0
      SET_SOCKERRNO(ctx->error);
1347
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
1348
0
      {
1349
0
        char buffer[STRERROR_LEN];
1350
0
        infof(data, "connect to %s port %u from %s port %d failed: %s",
1351
0
              ctx->ip.remote_ip, ctx->ip.remote_port,
1352
0
              ctx->ip.local_ip, ctx->ip.local_port,
1353
0
              curlx_strerror(ctx->error, buffer, sizeof(buffer)));
1354
0
      }
1355
0
#endif
1356
0
    }
1357
0
    if(ctx->sock != CURL_SOCKET_BAD) {
1358
0
      socket_close(data, cf->conn, TRUE, ctx->sock);
1359
0
      ctx->sock = CURL_SOCKET_BAD;
1360
0
    }
1361
0
    *done = FALSE;
1362
0
  }
1363
0
  return result;
1364
0
}
1365
1366
static CURLcode cf_socket_adjust_pollset(struct Curl_cfilter *cf,
1367
                                         struct Curl_easy *data,
1368
                                         struct easy_pollset *ps)
1369
0
{
1370
0
  struct cf_socket_ctx *ctx = cf->ctx;
1371
0
  CURLcode result = CURLE_OK;
1372
1373
0
  if(ctx->sock != CURL_SOCKET_BAD) {
1374
    /* A listening socket filter needs to be connected before the accept
1375
     * for some weird FTP interaction. This should be rewritten, so that
1376
     * FTP no longer does the socket checks and accept calls and delegates
1377
     * all that to the filter. */
1378
0
    if(ctx->listening) {
1379
0
      result = Curl_pollset_set_in_only(data, ps, ctx->sock);
1380
0
      CURL_TRC_CF(data, cf, "adjust_pollset, listening, POLLIN fd=%"
1381
0
                  FMT_SOCKET_T, ctx->sock);
1382
0
    }
1383
0
    else if(!cf->connected) {
1384
0
      result = Curl_pollset_set_out_only(data, ps, ctx->sock);
1385
0
      CURL_TRC_CF(data, cf, "adjust_pollset, !connected, POLLOUT fd=%"
1386
0
                  FMT_SOCKET_T, ctx->sock);
1387
0
    }
1388
0
    else if(!ctx->active) {
1389
0
      result = Curl_pollset_add_in(data, ps, ctx->sock);
1390
0
      CURL_TRC_CF(data, cf, "adjust_pollset, !active, POLLIN fd=%"
1391
0
                  FMT_SOCKET_T, ctx->sock);
1392
0
    }
1393
0
  }
1394
0
  return result;
1395
0
}
1396
1397
#ifdef USE_WINSOCK
1398
1399
/* Offered by mingw-w64 v13+. MS SDK 7.0A+. */
1400
#ifndef SIO_IDEAL_SEND_BACKLOG_QUERY
1401
#define SIO_IDEAL_SEND_BACKLOG_QUERY 0x4004747B
1402
#endif
1403
1404
static void win_update_sndbuf_size(struct Curl_easy *data,
1405
                                   struct cf_socket_ctx *ctx)
1406
{
1407
  ULONG ideal;
1408
  DWORD ideallen;
1409
1410
  if(curlx_ptimediff_ms(Curl_pgrs_now(data),
1411
                        &ctx->last_sndbuf_query_at) > 1000) {
1412
    if(!WSAIoctl(ctx->sock, SIO_IDEAL_SEND_BACKLOG_QUERY, 0, 0,
1413
                 &ideal, sizeof(ideal), &ideallen, 0, 0) &&
1414
       ideal != ctx->sndbuf_size &&
1415
       !setsockopt(ctx->sock, SOL_SOCKET, SO_SNDBUF,
1416
                   (const char *)&ideal, sizeof(ideal))) {
1417
      ctx->sndbuf_size = ideal;
1418
    }
1419
    ctx->last_sndbuf_query_at = *Curl_pgrs_now(data);
1420
  }
1421
}
1422
1423
#endif /* USE_WINSOCK */
1424
1425
static CURLcode cf_socket_send(struct Curl_cfilter *cf, struct Curl_easy *data,
1426
                               const uint8_t *buf, size_t len, bool eos,
1427
                               size_t *pnwritten)
1428
0
{
1429
0
  struct cf_socket_ctx *ctx = cf->ctx;
1430
0
  curl_socket_t fdsave;
1431
0
  ssize_t rv;
1432
0
  size_t orig_len = len;
1433
0
  CURLcode result = CURLE_OK;
1434
1435
0
  (void)eos;
1436
0
  *pnwritten = 0;
1437
0
  fdsave = cf->conn->sock[cf->sockindex];
1438
0
  cf->conn->sock[cf->sockindex] = ctx->sock;
1439
1440
#ifdef DEBUGBUILD
1441
  /* simulate network blocking/partial writes */
1442
  if(ctx->wblock_percent > 0) {
1443
    unsigned char c = 0;
1444
    Curl_rand_bytes(data, FALSE, &c, 1);
1445
    if(c >= ((100 - ctx->wblock_percent) * 256 / 100)) {
1446
      CURL_TRC_CF(data, cf, "send(len=%zu) SIMULATE EWOULDBLOCK", orig_len);
1447
      cf->conn->sock[cf->sockindex] = fdsave;
1448
      return CURLE_AGAIN;
1449
    }
1450
  }
1451
  if(cf->cft != &Curl_cft_udp && ctx->wpartial_percent > 0 && len > 8) {
1452
    len = len * ctx->wpartial_percent / 100;
1453
    if(!len)
1454
      len = 1;
1455
    CURL_TRC_CF(data, cf, "send(len=%zu) SIMULATE partial write of %zu bytes",
1456
                orig_len, len);
1457
  }
1458
#endif
1459
1460
#if defined(MSG_FASTOPEN) && !defined(TCP_FASTOPEN_CONNECT) /* Linux */
1461
  if(cf->conn->bits.tcp_fastopen) {
1462
    rv = sendto(ctx->sock, buf, len, MSG_FASTOPEN,
1463
                &ctx->addr.curl_sa_addr, ctx->addr.addrlen);
1464
    cf->conn->bits.tcp_fastopen = FALSE;
1465
  }
1466
  else
1467
#endif
1468
0
    rv = swrite(ctx->sock, buf, len);
1469
1470
0
  if(!curlx_sztouz(rv, pnwritten)) {
1471
0
    int sockerr = SOCKERRNO;
1472
1473
0
    if(
1474
#ifdef USE_WINSOCK
1475
      /* This is how Windows does it */
1476
      (SOCKEWOULDBLOCK == sockerr)
1477
#else
1478
      /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
1479
         due to its inability to send off data without blocking. We therefore
1480
         treat both error codes the same here */
1481
0
      (SOCKEWOULDBLOCK == sockerr) ||
1482
0
      (EAGAIN == sockerr) || (SOCKEINTR == sockerr) ||
1483
0
      (SOCKEINPROGRESS == sockerr)
1484
0
#endif
1485
0
      ) {
1486
      /* this is just a case of EWOULDBLOCK */
1487
0
      result = CURLE_AGAIN;
1488
0
    }
1489
0
    else {
1490
0
      char buffer[STRERROR_LEN];
1491
0
      failf(data, "Send failure: %s",
1492
0
            curlx_strerror(sockerr, buffer, sizeof(buffer)));
1493
0
      data->state.os_errno = sockerr;
1494
0
      result = CURLE_SEND_ERROR;
1495
0
    }
1496
0
  }
1497
1498
#ifdef USE_WINSOCK
1499
  if(!result)
1500
    win_update_sndbuf_size(data, ctx);
1501
#endif
1502
1503
0
  CURL_TRC_CF(data, cf, "send(len=%zu) -> %d, %zu",
1504
0
              orig_len, result, *pnwritten);
1505
0
  cf->conn->sock[cf->sockindex] = fdsave;
1506
0
  return result;
1507
0
}
1508
1509
static CURLcode cf_socket_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
1510
                               char *buf, size_t len, size_t *pnread)
1511
0
{
1512
0
  struct cf_socket_ctx *ctx = cf->ctx;
1513
0
  CURLcode result = CURLE_OK;
1514
0
  ssize_t rv;
1515
1516
0
  *pnread = 0;
1517
#ifdef DEBUGBUILD
1518
  /* simulate network blocking/partial reads */
1519
  if(cf->cft != &Curl_cft_udp && ctx->rblock_percent > 0) {
1520
    unsigned char c = 0;
1521
    Curl_rand(data, &c, 1);
1522
    if(c >= ((100 - ctx->rblock_percent) * 256 / 100)) {
1523
      CURL_TRC_CF(data, cf, "recv(len=%zu) SIMULATE EWOULDBLOCK", len);
1524
      return CURLE_AGAIN;
1525
    }
1526
  }
1527
  if(cf->cft != &Curl_cft_udp && ctx->recv_max && ctx->recv_max < len) {
1528
    size_t orig_len = len;
1529
    len = ctx->recv_max;
1530
    CURL_TRC_CF(data, cf, "recv(len=%zu) SIMULATE max read of %zu bytes",
1531
                orig_len, len);
1532
  }
1533
#endif
1534
1535
0
  rv = sread(ctx->sock, buf, len);
1536
1537
0
  if(!curlx_sztouz(rv, pnread)) {
1538
0
    int sockerr = SOCKERRNO;
1539
1540
0
    if(
1541
#ifdef USE_WINSOCK
1542
      /* This is how Windows does it */
1543
      (SOCKEWOULDBLOCK == sockerr)
1544
#else
1545
      /* errno may be EWOULDBLOCK or on some systems EAGAIN when it returned
1546
         due to its inability to send off data without blocking. We therefore
1547
         treat both error codes the same here */
1548
0
      (SOCKEWOULDBLOCK == sockerr) ||
1549
0
      (EAGAIN == sockerr) || (SOCKEINTR == sockerr)
1550
0
#endif
1551
0
      ) {
1552
      /* this is just a case of EWOULDBLOCK */
1553
0
      result = CURLE_AGAIN;
1554
0
    }
1555
0
    else {
1556
0
      char buffer[STRERROR_LEN];
1557
0
      failf(data, "Recv failure: %s",
1558
0
            curlx_strerror(sockerr, buffer, sizeof(buffer)));
1559
0
      data->state.os_errno = sockerr;
1560
0
      result = CURLE_RECV_ERROR;
1561
0
    }
1562
0
  }
1563
1564
0
  CURL_TRC_CF(data, cf, "recv(len=%zu) -> %d, %zu", len, result, *pnread);
1565
0
  if(!result && !ctx->got_first_byte) {
1566
0
    ctx->first_byte_at = *Curl_pgrs_now(data);
1567
0
    ctx->got_first_byte = TRUE;
1568
0
  }
1569
0
  return result;
1570
0
}
1571
1572
static void cf_socket_update_data(struct Curl_cfilter *cf,
1573
                                  struct Curl_easy *data)
1574
0
{
1575
  /* Update the IP info held in the transfer, if we have that. */
1576
0
  if(cf->connected && (cf->sockindex == FIRSTSOCKET)) {
1577
0
    struct cf_socket_ctx *ctx = cf->ctx;
1578
0
    data->info.primary = ctx->ip;
1579
    /* not sure if this is redundant... */
1580
0
    data->info.conn_remote_port = cf->conn->remote_port;
1581
0
  }
1582
0
}
1583
1584
static void cf_socket_active(struct Curl_cfilter *cf, struct Curl_easy *data)
1585
0
{
1586
0
  struct cf_socket_ctx *ctx = cf->ctx;
1587
1588
  /* use this socket from now on */
1589
0
  cf->conn->sock[cf->sockindex] = ctx->sock;
1590
0
  set_local_ip(cf, data);
1591
0
#ifdef USE_IPV6
1592
0
  if(cf->sockindex == FIRSTSOCKET)
1593
0
    cf->conn->bits.ipv6 = (ctx->addr.family == AF_INET6);
1594
0
#endif
1595
0
  ctx->active = TRUE;
1596
0
}
1597
1598
static CURLcode cf_socket_cntrl(struct Curl_cfilter *cf,
1599
                                struct Curl_easy *data,
1600
                                int event, int arg1, void *arg2)
1601
0
{
1602
0
  struct cf_socket_ctx *ctx = cf->ctx;
1603
1604
0
  (void)arg1;
1605
0
  (void)arg2;
1606
0
  switch(event) {
1607
0
  case CF_CTRL_CONN_INFO_UPDATE:
1608
0
    cf_socket_active(cf, data);
1609
0
    cf_socket_update_data(cf, data);
1610
0
    break;
1611
0
  case CF_CTRL_DATA_SETUP:
1612
0
    cf_socket_update_data(cf, data);
1613
0
    break;
1614
0
  case CF_CTRL_FORGET_SOCKET:
1615
0
    ctx->sock = CURL_SOCKET_BAD;
1616
0
    break;
1617
0
  }
1618
0
  return CURLE_OK;
1619
0
}
1620
1621
static bool cf_socket_conn_is_alive(struct Curl_cfilter *cf,
1622
                                    struct Curl_easy *data,
1623
                                    bool *input_pending)
1624
0
{
1625
0
  struct cf_socket_ctx *ctx = cf->ctx;
1626
0
  struct pollfd pfd[1];
1627
0
  int r;
1628
1629
0
  *input_pending = FALSE;
1630
0
  (void)data;
1631
0
  if(!ctx || ctx->sock == CURL_SOCKET_BAD)
1632
0
    return FALSE;
1633
1634
  /* Check with 0 timeout if there are any events pending on the socket */
1635
0
  pfd[0].fd = ctx->sock;
1636
0
  pfd[0].events = POLLRDNORM | POLLIN | POLLRDBAND | POLLPRI;
1637
0
  pfd[0].revents = 0;
1638
1639
0
  r = Curl_poll(pfd, 1, 0);
1640
0
  if(r < 0) {
1641
0
    CURL_TRC_CF(data, cf, "is_alive: poll error, assume dead");
1642
0
    return FALSE;
1643
0
  }
1644
0
  else if(r == 0) {
1645
0
    CURL_TRC_CF(data, cf, "is_alive: poll timeout, assume alive");
1646
0
    return TRUE;
1647
0
  }
1648
0
  else if(pfd[0].revents & (POLLERR | POLLHUP | POLLPRI | POLLNVAL)) {
1649
0
    CURL_TRC_CF(data, cf, "is_alive: err/hup/etc events, assume dead");
1650
0
    return FALSE;
1651
0
  }
1652
1653
0
  CURL_TRC_CF(data, cf, "is_alive: valid events, looks alive");
1654
0
  *input_pending = TRUE;
1655
0
  return TRUE;
1656
0
}
1657
1658
static CURLcode cf_socket_query(struct Curl_cfilter *cf,
1659
                                struct Curl_easy *data,
1660
                                int query, int *pres1, void *pres2)
1661
0
{
1662
0
  struct cf_socket_ctx *ctx = cf->ctx;
1663
1664
0
  switch(query) {
1665
0
  case CF_QUERY_SOCKET:
1666
0
    DEBUGASSERT(pres2);
1667
0
    *((curl_socket_t *)pres2) = ctx->sock;
1668
0
    return CURLE_OK;
1669
0
  case CF_QUERY_TRANSPORT:
1670
0
    DEBUGASSERT(pres1);
1671
0
    *pres1 = ctx->transport;
1672
0
    return CURLE_OK;
1673
0
  case CF_QUERY_REMOTE_ADDR:
1674
0
    DEBUGASSERT(pres2);
1675
0
    *((const struct Curl_sockaddr_ex **)pres2) = cf->connected ?
1676
0
                                                 &ctx->addr : NULL;
1677
0
    return CURLE_OK;
1678
0
  case CF_QUERY_CONNECT_REPLY_MS:
1679
0
    if(ctx->got_first_byte) {
1680
0
      timediff_t ms = curlx_ptimediff_ms(&ctx->first_byte_at,
1681
0
                                         &ctx->started_at);
1682
0
      *pres1 = (ms < INT_MAX) ? (int)ms : INT_MAX;
1683
0
    }
1684
0
    else
1685
0
      *pres1 = -1;
1686
0
    return CURLE_OK;
1687
0
  case CF_QUERY_TIMER_CONNECT: {
1688
0
    struct curltime *when = pres2;
1689
0
    switch(ctx->transport) {
1690
0
    case TRNSPRT_UDP:
1691
0
    case TRNSPRT_QUIC:
1692
      /* Since UDP connected sockets work different from TCP, we use the
1693
       * time of the first byte from the peer as the "connect" time. */
1694
0
      if(ctx->got_first_byte) {
1695
0
        *when = ctx->first_byte_at;
1696
0
        break;
1697
0
      }
1698
0
      FALLTHROUGH();
1699
0
    default:
1700
0
      *when = ctx->connected_at;
1701
0
      break;
1702
0
    }
1703
0
    return CURLE_OK;
1704
0
  }
1705
0
  case CF_QUERY_IP_INFO:
1706
0
#ifdef USE_IPV6
1707
0
    *pres1 = (ctx->addr.family == AF_INET6);
1708
#else
1709
    *pres1 = FALSE;
1710
#endif
1711
0
    *(struct ip_quadruple *)pres2 = ctx->ip;
1712
0
    return CURLE_OK;
1713
0
  default:
1714
0
    break;
1715
0
  }
1716
0
  return cf->next ?
1717
0
    cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1718
0
    CURLE_UNKNOWN_OPTION;
1719
0
}
1720
1721
struct Curl_cftype Curl_cft_tcp = {
1722
  "TCP",
1723
  CF_TYPE_IP_CONNECT,
1724
  CURL_LOG_LVL_NONE,
1725
  cf_socket_destroy,
1726
  cf_tcp_connect,
1727
  cf_socket_close,
1728
  cf_socket_shutdown,
1729
  cf_socket_adjust_pollset,
1730
  Curl_cf_def_data_pending,
1731
  cf_socket_send,
1732
  cf_socket_recv,
1733
  cf_socket_cntrl,
1734
  cf_socket_conn_is_alive,
1735
  Curl_cf_def_conn_keep_alive,
1736
  cf_socket_query,
1737
};
1738
1739
CURLcode Curl_cf_tcp_create(struct Curl_cfilter **pcf,
1740
                            struct Curl_easy *data,
1741
                            struct connectdata *conn,
1742
                            const struct Curl_addrinfo *ai,
1743
                            uint8_t transport)
1744
0
{
1745
0
  struct cf_socket_ctx *ctx = NULL;
1746
0
  struct Curl_cfilter *cf = NULL;
1747
0
  CURLcode result;
1748
1749
0
  (void)data;
1750
0
  (void)conn;
1751
0
  DEBUGASSERT(transport == TRNSPRT_TCP);
1752
0
  if(!ai) {
1753
0
    result = CURLE_BAD_FUNCTION_ARGUMENT;
1754
0
    goto out;
1755
0
  }
1756
1757
0
  ctx = curlx_calloc(1, sizeof(*ctx));
1758
0
  if(!ctx) {
1759
0
    result = CURLE_OUT_OF_MEMORY;
1760
0
    goto out;
1761
0
  }
1762
1763
0
  result = cf_socket_ctx_init(ctx, ai, transport);
1764
0
  if(result)
1765
0
    goto out;
1766
1767
0
  result = Curl_cf_create(&cf, &Curl_cft_tcp, ctx);
1768
1769
0
out:
1770
0
  *pcf = (!result) ? cf : NULL;
1771
0
  if(result) {
1772
0
    Curl_safefree(cf);
1773
0
    Curl_safefree(ctx);
1774
0
  }
1775
1776
0
  return result;
1777
0
}
1778
1779
#ifdef __linux__
1780
static void linux_quic_mtu(struct cf_socket_ctx *ctx)
1781
0
{
1782
0
  int val;
1783
0
  switch(ctx->addr.family) {
1784
0
#ifdef IP_MTU_DISCOVER
1785
0
  case AF_INET:
1786
0
    val = IP_PMTUDISC_DO;
1787
0
    (void)setsockopt(ctx->sock, IPPROTO_IP, IP_MTU_DISCOVER, &val,
1788
0
                     sizeof(val));
1789
0
    break;
1790
0
#endif
1791
0
#ifdef IPV6_MTU_DISCOVER
1792
0
  case AF_INET6:
1793
0
    val = IPV6_PMTUDISC_DO;
1794
0
    (void)setsockopt(ctx->sock, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &val,
1795
0
                     sizeof(val));
1796
0
    break;
1797
0
#endif
1798
0
  }
1799
0
}
1800
#else
1801
#define linux_quic_mtu(x)
1802
#endif
1803
1804
#if defined(UDP_GRO) &&                                                 \
1805
  (defined(HAVE_SENDMMSG) || defined(HAVE_SENDMSG)) &&                  \
1806
  ((defined(USE_NGTCP2) && defined(USE_NGHTTP3)) || defined(USE_QUICHE))
1807
static void linux_quic_gro(struct cf_socket_ctx *ctx)
1808
{
1809
  int one = 1;
1810
  (void)setsockopt(ctx->sock, IPPROTO_UDP, UDP_GRO, &one,
1811
                   (socklen_t)sizeof(one));
1812
}
1813
#else
1814
#define linux_quic_gro(x)
1815
#endif
1816
1817
static CURLcode cf_udp_setup_quic(struct Curl_cfilter *cf,
1818
                                  struct Curl_easy *data)
1819
0
{
1820
0
  struct cf_socket_ctx *ctx = cf->ctx;
1821
0
  int rc;
1822
1823
  /* QUIC needs a connected socket, nonblocking */
1824
0
  DEBUGASSERT(ctx->sock != CURL_SOCKET_BAD);
1825
1826
  /* error: The 1st argument to 'connect' is -1 but should be >= 0
1827
     NOLINTNEXTLINE(clang-analyzer-unix.StdCLibraryFunctions) */
1828
0
  rc = connect(ctx->sock, &ctx->addr.curl_sa_addr,
1829
0
               (curl_socklen_t)ctx->addr.addrlen);
1830
0
  if(-1 == rc) {
1831
0
    return socket_connect_result(data, ctx->ip.remote_ip, SOCKERRNO);
1832
0
  }
1833
0
  ctx->sock_connected = TRUE;
1834
0
  set_local_ip(cf, data);
1835
0
  CURL_TRC_CF(data, cf, "%s socket %" FMT_SOCKET_T
1836
0
              " connected: [%s:%d] -> [%s:%d]",
1837
0
              (ctx->transport == TRNSPRT_QUIC) ? "QUIC" : "UDP",
1838
0
              ctx->sock, ctx->ip.local_ip, ctx->ip.local_port,
1839
0
              ctx->ip.remote_ip, ctx->ip.remote_port);
1840
1841
  /* Currently, cf->ctx->sock is always non-blocking because the only
1842
   * caller to cf_udp_setup_quic() is cf_udp_connect() that passes the
1843
   * non-blocking socket created by cf_socket_open() to it. Thus, we
1844
   * do not need to call curlx_nonblock() in cf_udp_setup_quic() anymore.
1845
   */
1846
0
  linux_quic_mtu(ctx);
1847
0
  linux_quic_gro(ctx);
1848
1849
0
  return CURLE_OK;
1850
0
}
1851
1852
static CURLcode cf_udp_connect(struct Curl_cfilter *cf,
1853
                               struct Curl_easy *data,
1854
                               bool *done)
1855
0
{
1856
0
  struct cf_socket_ctx *ctx = cf->ctx;
1857
0
  CURLcode result = CURLE_COULDNT_CONNECT;
1858
1859
0
  if(cf->connected) {
1860
0
    *done = TRUE;
1861
0
    return CURLE_OK;
1862
0
  }
1863
1864
0
  *done = FALSE;
1865
0
  if(ctx->sock == CURL_SOCKET_BAD) {
1866
0
    result = cf_socket_open(cf, data);
1867
0
    if(result) {
1868
0
      CURL_TRC_CF(data, cf, "cf_udp_connect(), open failed -> %d", result);
1869
0
      goto out;
1870
0
    }
1871
1872
0
    if(ctx->transport == TRNSPRT_QUIC) {
1873
0
      result = cf_udp_setup_quic(cf, data);
1874
0
      if(result)
1875
0
        goto out;
1876
0
      CURL_TRC_CF(data, cf, "cf_udp_connect(), opened socket=%"
1877
0
                  FMT_SOCKET_T " (%s:%d)",
1878
0
                  ctx->sock, ctx->ip.local_ip, ctx->ip.local_port);
1879
0
    }
1880
0
    *done = TRUE;
1881
0
    cf->connected = TRUE;
1882
0
  }
1883
0
out:
1884
0
  return result;
1885
0
}
1886
1887
struct Curl_cftype Curl_cft_udp = {
1888
  "UDP",
1889
  CF_TYPE_IP_CONNECT,
1890
  CURL_LOG_LVL_NONE,
1891
  cf_socket_destroy,
1892
  cf_udp_connect,
1893
  cf_socket_close,
1894
  cf_socket_shutdown,
1895
  cf_socket_adjust_pollset,
1896
  Curl_cf_def_data_pending,
1897
  cf_socket_send,
1898
  cf_socket_recv,
1899
  cf_socket_cntrl,
1900
  cf_socket_conn_is_alive,
1901
  Curl_cf_def_conn_keep_alive,
1902
  cf_socket_query,
1903
};
1904
1905
CURLcode Curl_cf_udp_create(struct Curl_cfilter **pcf,
1906
                            struct Curl_easy *data,
1907
                            struct connectdata *conn,
1908
                            const struct Curl_addrinfo *ai,
1909
                            uint8_t transport)
1910
0
{
1911
0
  struct cf_socket_ctx *ctx = NULL;
1912
0
  struct Curl_cfilter *cf = NULL;
1913
0
  CURLcode result;
1914
1915
0
  (void)data;
1916
0
  (void)conn;
1917
0
  DEBUGASSERT(transport == TRNSPRT_UDP || transport == TRNSPRT_QUIC);
1918
0
  ctx = curlx_calloc(1, sizeof(*ctx));
1919
0
  if(!ctx) {
1920
0
    result = CURLE_OUT_OF_MEMORY;
1921
0
    goto out;
1922
0
  }
1923
1924
0
  result = cf_socket_ctx_init(ctx, ai, transport);
1925
0
  if(result)
1926
0
    goto out;
1927
1928
0
  result = Curl_cf_create(&cf, &Curl_cft_udp, ctx);
1929
1930
0
out:
1931
0
  *pcf = (!result) ? cf : NULL;
1932
0
  if(result) {
1933
0
    Curl_safefree(cf);
1934
0
    Curl_safefree(ctx);
1935
0
  }
1936
1937
0
  return result;
1938
0
}
1939
1940
/* this is the TCP filter which can also handle this case */
1941
struct Curl_cftype Curl_cft_unix = {
1942
  "UNIX",
1943
  CF_TYPE_IP_CONNECT,
1944
  CURL_LOG_LVL_NONE,
1945
  cf_socket_destroy,
1946
  cf_tcp_connect,
1947
  cf_socket_close,
1948
  cf_socket_shutdown,
1949
  cf_socket_adjust_pollset,
1950
  Curl_cf_def_data_pending,
1951
  cf_socket_send,
1952
  cf_socket_recv,
1953
  cf_socket_cntrl,
1954
  cf_socket_conn_is_alive,
1955
  Curl_cf_def_conn_keep_alive,
1956
  cf_socket_query,
1957
};
1958
1959
CURLcode Curl_cf_unix_create(struct Curl_cfilter **pcf,
1960
                             struct Curl_easy *data,
1961
                             struct connectdata *conn,
1962
                             const struct Curl_addrinfo *ai,
1963
                             uint8_t transport)
1964
0
{
1965
0
  struct cf_socket_ctx *ctx = NULL;
1966
0
  struct Curl_cfilter *cf = NULL;
1967
0
  CURLcode result;
1968
1969
0
  (void)data;
1970
0
  (void)conn;
1971
0
  DEBUGASSERT(transport == TRNSPRT_UNIX);
1972
0
  ctx = curlx_calloc(1, sizeof(*ctx));
1973
0
  if(!ctx) {
1974
0
    result = CURLE_OUT_OF_MEMORY;
1975
0
    goto out;
1976
0
  }
1977
1978
0
  result = cf_socket_ctx_init(ctx, ai, transport);
1979
0
  if(result)
1980
0
    goto out;
1981
1982
0
  result = Curl_cf_create(&cf, &Curl_cft_unix, ctx);
1983
1984
0
out:
1985
0
  *pcf = (!result) ? cf : NULL;
1986
0
  if(result) {
1987
0
    Curl_safefree(cf);
1988
0
    Curl_safefree(ctx);
1989
0
  }
1990
1991
0
  return result;
1992
0
}
1993
1994
static timediff_t cf_tcp_accept_timeleft(struct Curl_cfilter *cf,
1995
                                         struct Curl_easy *data)
1996
0
{
1997
0
  struct cf_socket_ctx *ctx = cf->ctx;
1998
0
  timediff_t timeout_ms = DEFAULT_ACCEPT_TIMEOUT;
1999
0
  timediff_t other_ms;
2000
2001
0
#ifndef CURL_DISABLE_FTP
2002
0
  if(data->set.accepttimeout > 0)
2003
0
    timeout_ms = data->set.accepttimeout;
2004
0
#endif
2005
2006
  /* check if the generic timeout possibly is set shorter */
2007
0
  other_ms = Curl_timeleft_ms(data, FALSE);
2008
0
  if(other_ms && (other_ms < timeout_ms))
2009
    /* note that this also works fine for when other_ms happens to be negative
2010
       due to it already having elapsed */
2011
0
    timeout_ms = other_ms;
2012
0
  else {
2013
    /* subtract elapsed time */
2014
0
    timeout_ms -= curlx_ptimediff_ms(Curl_pgrs_now(data), &ctx->started_at);
2015
0
    if(!timeout_ms)
2016
      /* avoid returning 0 as that means no timeout! */
2017
0
      timeout_ms = -1;
2018
0
  }
2019
0
  return timeout_ms;
2020
0
}
2021
2022
static void cf_tcp_set_accepted_remote_ip(struct Curl_cfilter *cf,
2023
                                          struct Curl_easy *data)
2024
0
{
2025
0
  struct cf_socket_ctx *ctx = cf->ctx;
2026
0
#ifdef HAVE_GETPEERNAME
2027
0
  char buffer[STRERROR_LEN];
2028
0
  struct Curl_sockaddr_storage ssrem;
2029
0
  curl_socklen_t plen;
2030
2031
0
  ctx->ip.remote_ip[0] = 0;
2032
0
  ctx->ip.remote_port = 0;
2033
0
  plen = sizeof(ssrem);
2034
0
  memset(&ssrem, 0, plen);
2035
0
  if(getpeername(ctx->sock, (struct sockaddr *)&ssrem, &plen)) {
2036
0
    int error = SOCKERRNO;
2037
0
    failf(data, "getpeername() failed with errno %d: %s",
2038
0
          error, curlx_strerror(error, buffer, sizeof(buffer)));
2039
0
    return;
2040
0
  }
2041
0
  if(!Curl_addr2string((struct sockaddr *)&ssrem, plen,
2042
0
                       ctx->ip.remote_ip, &ctx->ip.remote_port)) {
2043
0
    failf(data, "ssrem inet_ntop() failed with errno %d: %s",
2044
0
          errno, curlx_strerror(errno, buffer, sizeof(buffer)));
2045
0
    return;
2046
0
  }
2047
#else
2048
  ctx->ip.remote_ip[0] = 0;
2049
  ctx->ip.remote_port = 0;
2050
  (void)data;
2051
#endif
2052
0
}
2053
2054
static CURLcode cf_tcp_accept_connect(struct Curl_cfilter *cf,
2055
                                      struct Curl_easy *data,
2056
                                      bool *done)
2057
0
{
2058
0
  struct cf_socket_ctx *ctx = cf->ctx;
2059
0
  char errbuf[STRERROR_LEN];
2060
0
#ifdef USE_IPV6
2061
0
  struct Curl_sockaddr_storage add;
2062
#else
2063
  struct sockaddr_in add;
2064
#endif
2065
0
  curl_socklen_t size = (curl_socklen_t)sizeof(add);
2066
0
  curl_socket_t s_accepted = CURL_SOCKET_BAD;
2067
0
  timediff_t timeout_ms;
2068
0
  int socketstate = 0;
2069
0
  bool incoming = FALSE;
2070
2071
  /* we start accepted, if we ever close, we cannot go on */
2072
0
  (void)data;
2073
0
  if(cf->connected) {
2074
0
    *done = TRUE;
2075
0
    return CURLE_OK;
2076
0
  }
2077
2078
0
  *done = FALSE;
2079
0
  timeout_ms = cf_tcp_accept_timeleft(cf, data);
2080
0
  if(timeout_ms < 0) {
2081
    /* if a timeout was already reached, bail out */
2082
0
    failf(data, "Accept timeout occurred while waiting server connect");
2083
0
    return CURLE_FTP_ACCEPT_TIMEOUT;
2084
0
  }
2085
2086
0
  CURL_TRC_CF(data, cf, "Checking for incoming on fd=%" FMT_SOCKET_T
2087
0
              " ip=%s:%d", ctx->sock, ctx->ip.local_ip, ctx->ip.local_port);
2088
0
  socketstate = SOCKET_READABLE(ctx->sock, 0);
2089
0
  CURL_TRC_CF(data, cf, "socket_check -> %x", socketstate);
2090
0
  switch(socketstate) {
2091
0
  case -1: /* error */
2092
    /* let's die here */
2093
0
    failf(data, "Error while waiting for server connect");
2094
0
    return CURLE_FTP_ACCEPT_FAILED;
2095
0
  default:
2096
0
    if(socketstate & CURL_CSELECT_IN) {
2097
0
      infof(data, "Ready to accept data connection from server");
2098
0
      incoming = TRUE;
2099
0
    }
2100
0
    break;
2101
0
  }
2102
2103
0
  if(!incoming) {
2104
0
    CURL_TRC_CF(data, cf, "nothing heard from the server yet");
2105
0
    return CURLE_OK;
2106
0
  }
2107
2108
0
  size = sizeof(add);
2109
0
#ifdef HAVE_ACCEPT4
2110
0
  s_accepted = CURL_ACCEPT4(ctx->sock, (struct sockaddr *)&add, &size,
2111
0
                            SOCK_NONBLOCK | SOCK_CLOEXEC);
2112
#else
2113
  s_accepted = CURL_ACCEPT(ctx->sock, (struct sockaddr *)&add, &size);
2114
#endif
2115
2116
0
  if(CURL_SOCKET_BAD == s_accepted) {
2117
0
    failf(data, "Error accept()ing server connect: %s",
2118
0
          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
2119
0
    return CURLE_FTP_ACCEPT_FAILED;
2120
0
  }
2121
#ifndef HAVE_ACCEPT4
2122
#ifdef HAVE_FCNTL
2123
  if(fcntl(s_accepted, F_SETFD, FD_CLOEXEC) < 0) {
2124
    failf(data, "fcntl set CLOEXEC: %s",
2125
          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
2126
    Curl_socket_close(data, cf->conn, s_accepted);
2127
    return CURLE_FTP_ACCEPT_FAILED;
2128
  }
2129
#endif /* HAVE_FCNTL */
2130
  if(curlx_nonblock(s_accepted, TRUE) < 0) {
2131
    failf(data, "set socket NONBLOCK: %s",
2132
          curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
2133
    Curl_socket_close(data, cf->conn, s_accepted);
2134
    return CURLE_FTP_ACCEPT_FAILED;
2135
  }
2136
#endif /* !HAVE_ACCEPT4 */
2137
0
  infof(data, "Connection accepted from server");
2138
2139
  /* Replace any filter on SECONDARY with one listening on this socket */
2140
0
  ctx->listening = FALSE;
2141
0
  ctx->accepted = TRUE;
2142
0
  socket_close(data, cf->conn, TRUE, ctx->sock);
2143
0
  ctx->sock = s_accepted;
2144
2145
0
  cf->conn->sock[cf->sockindex] = ctx->sock;
2146
0
  cf_tcp_set_accepted_remote_ip(cf, data);
2147
0
  set_local_ip(cf, data);
2148
0
  ctx->active = TRUE;
2149
0
  ctx->connected_at = *Curl_pgrs_now(data);
2150
0
  cf->connected = TRUE;
2151
0
  CURL_TRC_CF(data, cf, "accepted_set(sock=%" FMT_SOCKET_T
2152
0
              ", remote=%s port=%d)",
2153
0
              ctx->sock, ctx->ip.remote_ip, ctx->ip.remote_port);
2154
2155
0
  if(data->set.fsockopt) {
2156
0
    int error = 0;
2157
2158
    /* activate callback for setting socket options */
2159
0
    Curl_set_in_callback(data, true);
2160
0
    error = data->set.fsockopt(data->set.sockopt_client,
2161
0
                               ctx->sock, CURLSOCKTYPE_ACCEPT);
2162
0
    Curl_set_in_callback(data, false);
2163
2164
0
    if(error)
2165
0
      return CURLE_ABORTED_BY_CALLBACK;
2166
0
  }
2167
0
  *done = TRUE;
2168
0
  return CURLE_OK;
2169
0
}
2170
2171
struct Curl_cftype Curl_cft_tcp_accept = {
2172
  "TCP-ACCEPT",
2173
  CF_TYPE_IP_CONNECT,
2174
  CURL_LOG_LVL_NONE,
2175
  cf_socket_destroy,
2176
  cf_tcp_accept_connect,
2177
  cf_socket_close,
2178
  cf_socket_shutdown,
2179
  cf_socket_adjust_pollset,
2180
  Curl_cf_def_data_pending,
2181
  cf_socket_send,
2182
  cf_socket_recv,
2183
  cf_socket_cntrl,
2184
  cf_socket_conn_is_alive,
2185
  Curl_cf_def_conn_keep_alive,
2186
  cf_socket_query,
2187
};
2188
2189
CURLcode Curl_conn_tcp_listen_set(struct Curl_easy *data,
2190
                                  struct connectdata *conn,
2191
                                  int sockindex, curl_socket_t *s)
2192
0
{
2193
0
  CURLcode result;
2194
0
  struct Curl_cfilter *cf = NULL;
2195
0
  struct cf_socket_ctx *ctx = NULL;
2196
2197
  /* replace any existing */
2198
0
  Curl_conn_cf_discard_all(data, conn, sockindex);
2199
0
  DEBUGASSERT(conn->sock[sockindex] == CURL_SOCKET_BAD);
2200
2201
0
  ctx = curlx_calloc(1, sizeof(*ctx));
2202
0
  if(!ctx) {
2203
0
    result = CURLE_OUT_OF_MEMORY;
2204
0
    goto out;
2205
0
  }
2206
0
  ctx->transport = TRNSPRT_TCP;
2207
0
  ctx->sock = *s;
2208
0
  ctx->listening = TRUE;
2209
0
  ctx->accepted = FALSE;
2210
0
  result = Curl_cf_create(&cf, &Curl_cft_tcp_accept, ctx);
2211
0
  if(result)
2212
0
    goto out;
2213
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
2214
2215
0
  ctx->started_at = *Curl_pgrs_now(data);
2216
0
  conn->sock[sockindex] = ctx->sock;
2217
0
  set_local_ip(cf, data);
2218
0
  CURL_TRC_CF(data, cf, "set filter for listen socket fd=%" FMT_SOCKET_T
2219
0
              " ip=%s:%d", ctx->sock,
2220
0
              ctx->ip.local_ip, ctx->ip.local_port);
2221
2222
0
out:
2223
0
  if(result) {
2224
0
    Curl_safefree(cf);
2225
0
    Curl_safefree(ctx);
2226
0
  }
2227
0
  return result;
2228
0
}
2229
2230
bool Curl_conn_is_tcp_listen(struct Curl_easy *data,
2231
                             int sockindex)
2232
0
{
2233
0
  struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
2234
0
  while(cf) {
2235
0
    if(cf->cft == &Curl_cft_tcp_accept)
2236
0
      return TRUE;
2237
0
    cf = cf->next;
2238
0
  }
2239
0
  return FALSE;
2240
0
}
2241
2242
/**
2243
 * Return TRUE iff `cf` is a socket filter.
2244
 */
2245
static bool cf_is_socket(struct Curl_cfilter *cf)
2246
0
{
2247
0
  return cf && (cf->cft == &Curl_cft_tcp ||
2248
0
                cf->cft == &Curl_cft_udp ||
2249
0
                cf->cft == &Curl_cft_unix ||
2250
0
                cf->cft == &Curl_cft_tcp_accept);
2251
0
}
2252
2253
CURLcode Curl_cf_socket_peek(struct Curl_cfilter *cf,
2254
                             struct Curl_easy *data,
2255
                             curl_socket_t *psock,
2256
                             const struct Curl_sockaddr_ex **paddr,
2257
                             struct ip_quadruple *pip)
2258
0
{
2259
0
  (void)data;
2260
0
  if(cf_is_socket(cf) && cf->ctx) {
2261
0
    struct cf_socket_ctx *ctx = cf->ctx;
2262
2263
0
    if(psock)
2264
0
      *psock = ctx->sock;
2265
0
    if(paddr)
2266
0
      *paddr = &ctx->addr;
2267
0
    if(pip)
2268
0
      *pip = ctx->ip;
2269
0
    return CURLE_OK;
2270
0
  }
2271
0
  return CURLE_FAILED_INIT;
2272
0
}