1#ifndef HEADER_CURL_CONNECT_H
2#define HEADER_CURL_CONNECT_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
11 *
12 * This software is licensed as described in the file COPYING, which
13 * you should have received as part of this distribution. The terms
14 * are also available at https://curl.se/docs/copyright.html.
15 *
16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17 * copies of the Software, and permit persons to whom the Software is
18 * furnished to do so, under the terms of the COPYING file.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 * SPDX-License-Identifier: curl
24 *
25 ***************************************************************************/
26#include "curl_setup.h"
27
28#include "nonblock.h" /* for curlx_nonblock(), formerly Curl_nonblock() */
29#include "sockaddr.h"
30#include "timeval.h"
31
32CURLcode Curl_is_connected(struct Curl_easy *data,
33 struct connectdata *conn,
34 int sockindex,
35 bool *connected);
36
37CURLcode Curl_connecthost(struct Curl_easy *data,
38 struct connectdata *conn,
39 const struct Curl_dns_entry *host);
40
41/* generic function that returns how much time there's left to run, according
42 to the timeouts set */
43timediff_t Curl_timeleft(struct Curl_easy *data,
44 struct curltime *nowp,
45 bool duringconnect);
46
47#define DEFAULT_CONNECT_TIMEOUT 300000 /* milliseconds == five minutes */
48
49/*
50 * Used to extract socket and connectdata struct for the most recent
51 * transfer on the given Curl_easy.
52 *
53 * The returned socket will be CURL_SOCKET_BAD in case of failure!
54 */
55curl_socket_t Curl_getconnectinfo(struct Curl_easy *data,
56 struct connectdata **connp);
57
58bool Curl_addr2string(struct sockaddr *sa, curl_socklen_t salen,
59 char *addr, int *port);
60
61/*
62 * Check if a connection seems to be alive.
63 */
64bool Curl_connalive(struct connectdata *conn);
65
66#ifdef USE_WINSOCK
67/* When you run a program that uses the Windows Sockets API, you may
68 experience slow performance when you copy data to a TCP server.
69
70 https://support.microsoft.com/kb/823764
71
72 Work-around: Make the Socket Send Buffer Size Larger Than the Program Send
73 Buffer Size
74
75*/
76void Curl_sndbufset(curl_socket_t sockfd);
77#else
78#define Curl_sndbufset(y) Curl_nop_stmt
79#endif
80
81void Curl_updateconninfo(struct Curl_easy *data, struct connectdata *conn,
82 curl_socket_t sockfd);
83void Curl_conninfo_remote(struct Curl_easy *data, struct connectdata *conn,
84 curl_socket_t sockfd);
85void Curl_conninfo_local(struct Curl_easy *data, curl_socket_t sockfd,
86 char *local_ip, int *local_port);
87void Curl_persistconninfo(struct Curl_easy *data, struct connectdata *conn,
88 char *local_ip, int local_port);
89int Curl_closesocket(struct Curl_easy *data, struct connectdata *conn,
90 curl_socket_t sock);
91
92/*
93 * The Curl_sockaddr_ex structure is basically libcurl's external API
94 * curl_sockaddr structure with enough space available to directly hold any
95 * protocol-specific address structures. The variable declared here will be
96 * used to pass / receive data to/from the fopensocket callback if this has
97 * been set, before that, it is initialized from parameters.
98 */
99struct Curl_sockaddr_ex {
100 int family;
101 int socktype;
102 int protocol;
103 unsigned int addrlen;
104 union {
105 struct sockaddr addr;
106 struct Curl_sockaddr_storage buff;
107 } _sa_ex_u;
108};
109#define sa_addr _sa_ex_u.addr
110
111/*
112 * Create a socket based on info from 'conn' and 'ai'.
113 *
114 * Fill in 'addr' and 'sockfd' accordingly if OK is returned. If the open
115 * socket callback is set, used that!
116 *
117 */
118CURLcode Curl_socket(struct Curl_easy *data,
119 const struct Curl_addrinfo *ai,
120 struct Curl_sockaddr_ex *addr,
121 curl_socket_t *sockfd);
122
123/*
124 * Curl_conncontrol() marks the end of a connection/stream. The 'closeit'
125 * argument specifies if it is the end of a connection or a stream.
126 *
127 * For stream-based protocols (such as HTTP/2), a stream close will not cause
128 * a connection close. Other protocols will close the connection for both
129 * cases.
130 *
131 * It sets the bit.close bit to TRUE (with an explanation for debug builds),
132 * when the connection will close.
133 */
134
135#define CONNCTRL_KEEP 0 /* undo a marked closure */
136#define CONNCTRL_CONNECTION 1
137#define CONNCTRL_STREAM 2
138
139void Curl_conncontrol(struct connectdata *conn,
140 int closeit
141#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
142 , const char *reason
143#endif
144 );
145
146#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
147#define streamclose(x,y) Curl_conncontrol(x, CONNCTRL_STREAM, y)
148#define connclose(x,y) Curl_conncontrol(x, CONNCTRL_CONNECTION, y)
149#define connkeep(x,y) Curl_conncontrol(x, CONNCTRL_KEEP, y)
150#else /* if !DEBUGBUILD || CURL_DISABLE_VERBOSE_STRINGS */
151#define streamclose(x,y) Curl_conncontrol(x, CONNCTRL_STREAM)
152#define connclose(x,y) Curl_conncontrol(x, CONNCTRL_CONNECTION)
153#define connkeep(x,y) Curl_conncontrol(x, CONNCTRL_KEEP)
154#endif
155
156bool Curl_conn_data_pending(struct connectdata *conn, int sockindex);
157
158#endif /* HEADER_CURL_CONNECT_H */
159