113498266Sopenharmony_ci#ifndef HEADER_CURL_CF_SOCKET_H
213498266Sopenharmony_ci#define HEADER_CURL_CF_SOCKET_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci#include "curl_setup.h"
2713498266Sopenharmony_ci
2813498266Sopenharmony_ci#include "nonblock.h" /* for curlx_nonblock(), formerly Curl_nonblock() */
2913498266Sopenharmony_ci#include "sockaddr.h"
3013498266Sopenharmony_ci
3113498266Sopenharmony_cistruct Curl_addrinfo;
3213498266Sopenharmony_cistruct Curl_cfilter;
3313498266Sopenharmony_cistruct Curl_easy;
3413498266Sopenharmony_cistruct connectdata;
3513498266Sopenharmony_cistruct Curl_sockaddr_ex;
3613498266Sopenharmony_ci
3713498266Sopenharmony_ci/*
3813498266Sopenharmony_ci * The Curl_sockaddr_ex structure is basically libcurl's external API
3913498266Sopenharmony_ci * curl_sockaddr structure with enough space available to directly hold any
4013498266Sopenharmony_ci * protocol-specific address structures. The variable declared here will be
4113498266Sopenharmony_ci * used to pass / receive data to/from the fopensocket callback if this has
4213498266Sopenharmony_ci * been set, before that, it is initialized from parameters.
4313498266Sopenharmony_ci */
4413498266Sopenharmony_cistruct Curl_sockaddr_ex {
4513498266Sopenharmony_ci  int family;
4613498266Sopenharmony_ci  int socktype;
4713498266Sopenharmony_ci  int protocol;
4813498266Sopenharmony_ci  unsigned int addrlen;
4913498266Sopenharmony_ci  union {
5013498266Sopenharmony_ci    struct sockaddr addr;
5113498266Sopenharmony_ci    struct Curl_sockaddr_storage buff;
5213498266Sopenharmony_ci  } _sa_ex_u;
5313498266Sopenharmony_ci};
5413498266Sopenharmony_ci#define sa_addr _sa_ex_u.addr
5513498266Sopenharmony_ci
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci/*
5813498266Sopenharmony_ci * Create a socket based on info from 'conn' and 'ai'.
5913498266Sopenharmony_ci *
6013498266Sopenharmony_ci * Fill in 'addr' and 'sockfd' accordingly if OK is returned. If the open
6113498266Sopenharmony_ci * socket callback is set, used that!
6213498266Sopenharmony_ci *
6313498266Sopenharmony_ci */
6413498266Sopenharmony_ciCURLcode Curl_socket_open(struct Curl_easy *data,
6513498266Sopenharmony_ci                            const struct Curl_addrinfo *ai,
6613498266Sopenharmony_ci                            struct Curl_sockaddr_ex *addr,
6713498266Sopenharmony_ci                            int transport,
6813498266Sopenharmony_ci                            curl_socket_t *sockfd);
6913498266Sopenharmony_ci
7013498266Sopenharmony_ciint Curl_socket_close(struct Curl_easy *data, struct connectdata *conn,
7113498266Sopenharmony_ci                      curl_socket_t sock);
7213498266Sopenharmony_ci
7313498266Sopenharmony_ci#ifdef USE_WINSOCK
7413498266Sopenharmony_ci/* When you run a program that uses the Windows Sockets API, you may
7513498266Sopenharmony_ci   experience slow performance when you copy data to a TCP server.
7613498266Sopenharmony_ci
7713498266Sopenharmony_ci   https://support.microsoft.com/kb/823764
7813498266Sopenharmony_ci
7913498266Sopenharmony_ci   Work-around: Make the Socket Send Buffer Size Larger Than the Program Send
8013498266Sopenharmony_ci   Buffer Size
8113498266Sopenharmony_ci
8213498266Sopenharmony_ci*/
8313498266Sopenharmony_civoid Curl_sndbufset(curl_socket_t sockfd);
8413498266Sopenharmony_ci#else
8513498266Sopenharmony_ci#define Curl_sndbufset(y) Curl_nop_stmt
8613498266Sopenharmony_ci#endif
8713498266Sopenharmony_ci
8813498266Sopenharmony_ci/**
8913498266Sopenharmony_ci * Assign the address `ai` to the Curl_sockaddr_ex `dest` and
9013498266Sopenharmony_ci * set the transport used.
9113498266Sopenharmony_ci */
9213498266Sopenharmony_civoid Curl_sock_assign_addr(struct Curl_sockaddr_ex *dest,
9313498266Sopenharmony_ci                           const struct Curl_addrinfo *ai,
9413498266Sopenharmony_ci                           int transport);
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci/**
9713498266Sopenharmony_ci * Creates a cfilter that opens a TCP socket to the given address
9813498266Sopenharmony_ci * when calling its `connect` implementation.
9913498266Sopenharmony_ci * The filter will not touch any connection/data flags and can be
10013498266Sopenharmony_ci * used in happy eyeballing. Once selected for use, its `_active()`
10113498266Sopenharmony_ci * method needs to be called.
10213498266Sopenharmony_ci */
10313498266Sopenharmony_ciCURLcode Curl_cf_tcp_create(struct Curl_cfilter **pcf,
10413498266Sopenharmony_ci                            struct Curl_easy *data,
10513498266Sopenharmony_ci                            struct connectdata *conn,
10613498266Sopenharmony_ci                            const struct Curl_addrinfo *ai,
10713498266Sopenharmony_ci                            int transport);
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci/**
11013498266Sopenharmony_ci * Creates a cfilter that opens a UDP socket to the given address
11113498266Sopenharmony_ci * when calling its `connect` implementation.
11213498266Sopenharmony_ci * The filter will not touch any connection/data flags and can be
11313498266Sopenharmony_ci * used in happy eyeballing. Once selected for use, its `_active()`
11413498266Sopenharmony_ci * method needs to be called.
11513498266Sopenharmony_ci */
11613498266Sopenharmony_ciCURLcode Curl_cf_udp_create(struct Curl_cfilter **pcf,
11713498266Sopenharmony_ci                            struct Curl_easy *data,
11813498266Sopenharmony_ci                            struct connectdata *conn,
11913498266Sopenharmony_ci                            const struct Curl_addrinfo *ai,
12013498266Sopenharmony_ci                            int transport);
12113498266Sopenharmony_ci
12213498266Sopenharmony_ci/**
12313498266Sopenharmony_ci * Creates a cfilter that opens a UNIX socket to the given address
12413498266Sopenharmony_ci * when calling its `connect` implementation.
12513498266Sopenharmony_ci * The filter will not touch any connection/data flags and can be
12613498266Sopenharmony_ci * used in happy eyeballing. Once selected for use, its `_active()`
12713498266Sopenharmony_ci * method needs to be called.
12813498266Sopenharmony_ci */
12913498266Sopenharmony_ciCURLcode Curl_cf_unix_create(struct Curl_cfilter **pcf,
13013498266Sopenharmony_ci                             struct Curl_easy *data,
13113498266Sopenharmony_ci                             struct connectdata *conn,
13213498266Sopenharmony_ci                             const struct Curl_addrinfo *ai,
13313498266Sopenharmony_ci                             int transport);
13413498266Sopenharmony_ci
13513498266Sopenharmony_ci/**
13613498266Sopenharmony_ci * Creates a cfilter that keeps a listening socket.
13713498266Sopenharmony_ci */
13813498266Sopenharmony_ciCURLcode Curl_conn_tcp_listen_set(struct Curl_easy *data,
13913498266Sopenharmony_ci                                  struct connectdata *conn,
14013498266Sopenharmony_ci                                  int sockindex,
14113498266Sopenharmony_ci                                  curl_socket_t *s);
14213498266Sopenharmony_ci
14313498266Sopenharmony_ci/**
14413498266Sopenharmony_ci * Replace the listen socket with the accept()ed one.
14513498266Sopenharmony_ci */
14613498266Sopenharmony_ciCURLcode Curl_conn_tcp_accepted_set(struct Curl_easy *data,
14713498266Sopenharmony_ci                                    struct connectdata *conn,
14813498266Sopenharmony_ci                                    int sockindex,
14913498266Sopenharmony_ci                                    curl_socket_t *s);
15013498266Sopenharmony_ci
15113498266Sopenharmony_ci/**
15213498266Sopenharmony_ci * Peek at the socket and remote ip/port the socket filter is using.
15313498266Sopenharmony_ci * The filter owns all returned values.
15413498266Sopenharmony_ci * @param psock             pointer to hold socket descriptor or NULL
15513498266Sopenharmony_ci * @param paddr             pointer to hold addr reference or NULL
15613498266Sopenharmony_ci * @param pr_ip_str         pointer to hold remote addr as string or NULL
15713498266Sopenharmony_ci * @param pr_port           pointer to hold remote port number or NULL
15813498266Sopenharmony_ci * @param pl_ip_str         pointer to hold local addr as string or NULL
15913498266Sopenharmony_ci * @param pl_port           pointer to hold local port number or NULL
16013498266Sopenharmony_ci * Returns error if the filter is of invalid type.
16113498266Sopenharmony_ci */
16213498266Sopenharmony_ciCURLcode Curl_cf_socket_peek(struct Curl_cfilter *cf,
16313498266Sopenharmony_ci                             struct Curl_easy *data,
16413498266Sopenharmony_ci                             curl_socket_t *psock,
16513498266Sopenharmony_ci                             const struct Curl_sockaddr_ex **paddr,
16613498266Sopenharmony_ci                             const char **pr_ip_str, int *pr_port,
16713498266Sopenharmony_ci                             const char **pl_ip_str, int *pl_port);
16813498266Sopenharmony_ci
16913498266Sopenharmony_ciextern struct Curl_cftype Curl_cft_tcp;
17013498266Sopenharmony_ciextern struct Curl_cftype Curl_cft_udp;
17113498266Sopenharmony_ciextern struct Curl_cftype Curl_cft_unix;
17213498266Sopenharmony_ciextern struct Curl_cftype Curl_cft_tcp_accept;
17313498266Sopenharmony_ci
17413498266Sopenharmony_ci#endif /* HEADER_CURL_CF_SOCKET_H */
175