Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / pingpong.h
1 #ifndef HEADER_CURL_PINGPONG_H
2 #define HEADER_CURL_PINGPONG_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, 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
27 #include "curl_setup.h"
28
29 #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_FTP) || \
30   !defined(CURL_DISABLE_POP3) || !defined(CURL_DISABLE_SMTP)
31 #define USE_PINGPONG
32 #endif
33
34 /* forward-declaration, this is defined in urldata.h */
35 struct connectdata;
36
37 typedef enum {
38   PPTRANSFER_BODY, /* yes do transfer a body */
39   PPTRANSFER_INFO, /* do still go through to get info/headers */
40   PPTRANSFER_NONE  /* don't get anything and don't get info */
41 } curl_pp_transfer;
42
43 /*
44  * 'pingpong' is the generic struct used for protocols doing server<->client
45  * conversations in a back-and-forth style such as FTP, IMAP, POP3, SMTP etc.
46  *
47  * It holds response cache and non-blocking sending data.
48  */
49 struct pingpong {
50   char *cache;     /* data cache between getresponse()-calls */
51   size_t cache_size;  /* size of cache in bytes */
52   size_t nread_resp;  /* number of bytes currently read of a server response */
53   char *linestart_resp; /* line start pointer for the server response
54                            reader function */
55   bool pending_resp;  /* set TRUE when a server response is pending or in
56                          progress, and is cleared once the last response is
57                          read */
58   char *sendthis; /* allocated pointer to a buffer that is to be sent to the
59                      server */
60   size_t sendleft; /* number of bytes left to send from the sendthis buffer */
61   size_t sendsize; /* total size of the sendthis buffer */
62   struct curltime response; /* set to Curl_now() when a command has been sent
63                                off, used to time-out response reading */
64   timediff_t response_time; /* When no timeout is given, this is the amount of
65                                milliseconds we await for a server response. */
66   struct dynbuf sendbuf;
67
68   /* Function pointers the protocols MUST implement and provide for the
69      pingpong layer to function */
70
71   CURLcode (*statemachine)(struct Curl_easy *data, struct connectdata *conn);
72   bool (*endofresp)(struct Curl_easy *data, struct connectdata *conn,
73                     char *ptr, size_t len, int *code);
74 };
75
76 #define PINGPONG_SETUP(pp,s,e)                   \
77   do {                                           \
78     pp->response_time = RESP_TIMEOUT;            \
79     pp->statemachine = s;                        \
80     pp->endofresp = e;                           \
81   } while(0)
82
83 /*
84  * Curl_pp_statemach()
85  *
86  * called repeatedly until done. Set 'wait' to make it wait a while on the
87  * socket if there's no traffic.
88  */
89 CURLcode Curl_pp_statemach(struct Curl_easy *data, struct pingpong *pp,
90                            bool block, bool disconnecting);
91
92 /* initialize stuff to prepare for reading a fresh new response */
93 void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp);
94
95 /* setup for the transfer */
96 void Curl_pp_setup(struct pingpong *pp);
97
98 /* Returns timeout in ms. 0 or negative number means the timeout has already
99    triggered */
100 timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
101                                  struct pingpong *pp, bool disconnecting);
102
103
104 /***********************************************************************
105  *
106  * Curl_pp_sendf()
107  *
108  * Send the formatted string as a command to a pingpong server. Note that
109  * the string should not have any CRLF appended, as this function will
110  * append the necessary things itself.
111  *
112  * made to never block
113  */
114 CURLcode Curl_pp_sendf(struct Curl_easy *data,
115                        struct pingpong *pp,
116                        const char *fmt, ...);
117
118 /***********************************************************************
119  *
120  * Curl_pp_vsendf()
121  *
122  * Send the formatted string as a command to a pingpong server. Note that
123  * the string should not have any CRLF appended, as this function will
124  * append the necessary things itself.
125  *
126  * made to never block
127  */
128 CURLcode Curl_pp_vsendf(struct Curl_easy *data,
129                         struct pingpong *pp,
130                         const char *fmt,
131                         va_list args);
132
133 /*
134  * Curl_pp_readresp()
135  *
136  * Reads a piece of a server response.
137  */
138 CURLcode Curl_pp_readresp(struct Curl_easy *data,
139                           curl_socket_t sockfd,
140                           struct pingpong *pp,
141                           int *code, /* return the server code if done */
142                           size_t *size); /* size of the response */
143
144
145 CURLcode Curl_pp_flushsend(struct Curl_easy *data,
146                            struct pingpong *pp);
147
148 /* call this when a pingpong connection is disconnected */
149 CURLcode Curl_pp_disconnect(struct pingpong *pp);
150
151 int Curl_pp_getsock(struct Curl_easy *data, struct pingpong *pp,
152                     curl_socket_t *socks);
153
154
155 /***********************************************************************
156  *
157  * Curl_pp_moredata()
158  *
159  * Returns whether there are still more data in the cache and so a call
160  * to Curl_pp_readresp() will not block.
161  */
162 bool Curl_pp_moredata(struct pingpong *pp);
163
164 #endif /* HEADER_CURL_PINGPONG_H */