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