1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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 * 'pingpong' is for generic back-and-forth support functions used by FTP,
24 * IMAP, POP3, SMTP and whatever more that likes them.
25 *
26 ***************************************************************************/
27
28#include "curl_setup.h"
29
30#include "urldata.h"
31#include "sendf.h"
32#include "select.h"
33#include "progress.h"
34#include "speedcheck.h"
35#include "pingpong.h"
36#include "multiif.h"
37#include "vtls/vtls.h"
38
39/* The last 3 #include files should be in this order */
40#include "curl_printf.h"
41#include "curl_memory.h"
42#include "memdebug.h"
43
44#ifdef USE_PINGPONG
45
46/* Returns timeout in ms. 0 or negative number means the timeout has already
47 triggered */
48timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
49 struct pingpong *pp, bool disconnecting)
50{
51 struct connectdata *conn = data->conn;
52 timediff_t timeout_ms; /* in milliseconds */
53 timediff_t response_time = (data->set.server_response_timeout)?
54 data->set.server_response_timeout: pp->response_time;
55
56 /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
57 remaining time, or use pp->response because SERVER_RESPONSE_TIMEOUT is
58 supposed to govern the response for any given server response, not for
59 the time from connect to the given server response. */
60
61 /* Without a requested timeout, we only wait 'response_time' seconds for the
62 full response to arrive before we bail out */
63 timeout_ms = response_time -
64 Curl_timediff(Curl_now(), pp->response); /* spent time */
65
66 if(data->set.timeout && !disconnecting) {
67 /* if timeout is requested, find out how much remaining time we have */
68 timediff_t timeout2_ms = data->set.timeout - /* timeout time */
69 Curl_timediff(Curl_now(), conn->now); /* spent time */
70
71 /* pick the lowest number */
72 timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
73 }
74
75 return timeout_ms;
76}
77
78/*
79 * Curl_pp_statemach()
80 */
81CURLcode Curl_pp_statemach(struct Curl_easy *data,
82 struct pingpong *pp, bool block,
83 bool disconnecting)
84{
85 struct connectdata *conn = data->conn;
86 curl_socket_t sock = conn->sock[FIRSTSOCKET];
87 int rc;
88 timediff_t interval_ms;
89 timediff_t timeout_ms = Curl_pp_state_timeout(data, pp, disconnecting);
90 CURLcode result = CURLE_OK;
91
92 if(timeout_ms <= 0) {
93 failf(data, "server response timeout");
94 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
95 }
96
97 if(block) {
98 interval_ms = 1000; /* use 1 second timeout intervals */
99 if(timeout_ms < interval_ms)
100 interval_ms = timeout_ms;
101 }
102 else
103 interval_ms = 0; /* immediate */
104
105 if(Curl_ssl_data_pending(conn, FIRSTSOCKET))
106 rc = 1;
107 else if(Curl_pp_moredata(pp))
108 /* We are receiving and there is data in the cache so just read it */
109 rc = 1;
110 else if(!pp->sendleft && Curl_ssl_data_pending(conn, FIRSTSOCKET))
111 /* We are receiving and there is data ready in the SSL library */
112 rc = 1;
113 else
114 rc = Curl_socket_check(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
115 CURL_SOCKET_BAD,
116 pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
117 interval_ms);
118
119 if(block) {
120 /* if we didn't wait, we don't have to spend time on this now */
121 if(Curl_pgrsUpdate(data))
122 result = CURLE_ABORTED_BY_CALLBACK;
123 else
124 result = Curl_speedcheck(data, Curl_now());
125
126 if(result)
127 return result;
128 }
129
130 if(rc == -1) {
131 failf(data, "select/poll error");
132 result = CURLE_OUT_OF_MEMORY;
133 }
134 else if(rc)
135 result = pp->statemachine(data, data->conn);
136
137 return result;
138}
139
140/* initialize stuff to prepare for reading a fresh new response */
141void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp)
142{
143 DEBUGASSERT(data);
144 pp->nread_resp = 0;
145 pp->linestart_resp = data->state.buffer;
146 pp->pending_resp = TRUE;
147 pp->response = Curl_now(); /* start response time-out now! */
148}
149
150/* setup for the coming transfer */
151void Curl_pp_setup(struct pingpong *pp)
152{
153 Curl_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
154}
155
156/***********************************************************************
157 *
158 * Curl_pp_vsendf()
159 *
160 * Send the formatted string as a command to a pingpong server. Note that
161 * the string should not have any CRLF appended, as this function will
162 * append the necessary things itself.
163 *
164 * made to never block
165 */
166CURLcode Curl_pp_vsendf(struct Curl_easy *data,
167 struct pingpong *pp,
168 const char *fmt,
169 va_list args)
170{
171 ssize_t bytes_written = 0;
172 size_t write_len;
173 char *s;
174 CURLcode result;
175 struct connectdata *conn = data->conn;
176
177#ifdef HAVE_GSSAPI
178 enum protection_level data_sec;
179#endif
180
181 DEBUGASSERT(pp->sendleft == 0);
182 DEBUGASSERT(pp->sendsize == 0);
183 DEBUGASSERT(pp->sendthis == NULL);
184
185 if(!conn)
186 /* can't send without a connection! */
187 return CURLE_SEND_ERROR;
188
189 Curl_dyn_reset(&pp->sendbuf);
190 result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
191 if(result)
192 return result;
193
194 /* append CRLF */
195 result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
196 if(result)
197 return result;
198
199 write_len = Curl_dyn_len(&pp->sendbuf);
200 s = Curl_dyn_ptr(&pp->sendbuf);
201 Curl_pp_init(data, pp);
202
203#ifdef HAVE_GSSAPI
204 conn->data_prot = PROT_CMD;
205#endif
206 result = Curl_write(data, conn->sock[FIRSTSOCKET], s, write_len,
207 &bytes_written);
208 if(result)
209 return result;
210#ifdef HAVE_GSSAPI
211 data_sec = conn->data_prot;
212 DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
213 conn->data_prot = data_sec;
214#endif
215
216 Curl_debug(data, CURLINFO_HEADER_OUT, s, (size_t)bytes_written);
217
218 if(bytes_written != (ssize_t)write_len) {
219 /* the whole chunk was not sent, keep it around and adjust sizes */
220 pp->sendthis = s;
221 pp->sendsize = write_len;
222 pp->sendleft = write_len - bytes_written;
223 }
224 else {
225 pp->sendthis = NULL;
226 pp->sendleft = pp->sendsize = 0;
227 pp->response = Curl_now();
228 }
229
230 return CURLE_OK;
231}
232
233
234/***********************************************************************
235 *
236 * Curl_pp_sendf()
237 *
238 * Send the formatted string as a command to a pingpong server. Note that
239 * the string should not have any CRLF appended, as this function will
240 * append the necessary things itself.
241 *
242 * made to never block
243 */
244CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
245 const char *fmt, ...)
246{
247 CURLcode result;
248 va_list ap;
249 va_start(ap, fmt);
250
251 result = Curl_pp_vsendf(data, pp, fmt, ap);
252
253 va_end(ap);
254
255 return result;
256}
257
258/*
259 * Curl_pp_readresp()
260 *
261 * Reads a piece of a server response.
262 */
263CURLcode Curl_pp_readresp(struct Curl_easy *data,
264 curl_socket_t sockfd,
265 struct pingpong *pp,
266 int *code, /* return the server code if done */
267 size_t *size) /* size of the response */
268{
269 ssize_t perline; /* count bytes per line */
270 bool keepon = TRUE;
271 ssize_t gotbytes;
272 char *ptr;
273 struct connectdata *conn = data->conn;
274 char * const buf = data->state.buffer;
275 CURLcode result = CURLE_OK;
276
277 *code = 0; /* 0 for errors or not done */
278 *size = 0;
279
280 ptr = buf + pp->nread_resp;
281
282 /* number of bytes in the current line, so far */
283 perline = (ssize_t)(ptr-pp->linestart_resp);
284
285 while((pp->nread_resp < (size_t)data->set.buffer_size) &&
286 (keepon && !result)) {
287
288 if(pp->cache) {
289 /* we had data in the "cache", copy that instead of doing an actual
290 * read
291 *
292 * pp->cache_size is cast to ssize_t here. This should be safe, because
293 * it would have been populated with something of size int to begin
294 * with, even though its datatype may be larger than an int.
295 */
296 if((ptr + pp->cache_size) > (buf + data->set.buffer_size + 1)) {
297 failf(data, "cached response data too big to handle");
298 return CURLE_WEIRD_SERVER_REPLY;
299 }
300 memcpy(ptr, pp->cache, pp->cache_size);
301 gotbytes = (ssize_t)pp->cache_size;
302 free(pp->cache); /* free the cache */
303 pp->cache = NULL; /* clear the pointer */
304 pp->cache_size = 0; /* zero the size just in case */
305 }
306 else {
307#ifdef HAVE_GSSAPI
308 enum protection_level prot = conn->data_prot;
309 conn->data_prot = PROT_CLEAR;
310#endif
311 DEBUGASSERT((ptr + data->set.buffer_size - pp->nread_resp) <=
312 (buf + data->set.buffer_size + 1));
313 result = Curl_read(data, sockfd, ptr,
314 data->set.buffer_size - pp->nread_resp,
315 &gotbytes);
316#ifdef HAVE_GSSAPI
317 DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
318 conn->data_prot = prot;
319#endif
320 if(result == CURLE_AGAIN)
321 return CURLE_OK; /* return */
322
323 if(result)
324 /* Set outer result variable to this error. */
325 keepon = FALSE;
326 }
327
328 if(!keepon)
329 ;
330 else if(gotbytes <= 0) {
331 keepon = FALSE;
332 result = CURLE_RECV_ERROR;
333 failf(data, "response reading failed");
334 }
335 else {
336 /* we got a whole chunk of data, which can be anything from one
337 * byte to a set of lines and possible just a piece of the last
338 * line */
339 ssize_t i;
340 ssize_t clipamount = 0;
341 bool restart = FALSE;
342
343 data->req.headerbytecount += (long)gotbytes;
344
345 pp->nread_resp += gotbytes;
346 for(i = 0; i < gotbytes; ptr++, i++) {
347 perline++;
348 if(*ptr == '\n') {
349 /* a newline is CRLF in pp-talk, so the CR is ignored as
350 the line isn't really terminated until the LF comes */
351
352 /* output debug output if that is requested */
353#ifdef HAVE_GSSAPI
354 if(!conn->sec_complete)
355#endif
356 Curl_debug(data, CURLINFO_HEADER_IN,
357 pp->linestart_resp, (size_t)perline);
358
359 /*
360 * We pass all response-lines to the callback function registered
361 * for "headers". The response lines can be seen as a kind of
362 * headers.
363 */
364 result = Curl_client_write(data, CLIENTWRITE_HEADER,
365 pp->linestart_resp, perline);
366 if(result)
367 return result;
368
369 if(pp->endofresp(data, conn, pp->linestart_resp, perline, code)) {
370 /* This is the end of the last line, copy the last line to the
371 start of the buffer and null-terminate, for old times sake */
372 size_t n = ptr - pp->linestart_resp;
373 memmove(buf, pp->linestart_resp, n);
374 buf[n] = 0; /* null-terminate */
375 keepon = FALSE;
376 pp->linestart_resp = ptr + 1; /* advance pointer */
377 i++; /* skip this before getting out */
378
379 *size = pp->nread_resp; /* size of the response */
380 pp->nread_resp = 0; /* restart */
381 break;
382 }
383 perline = 0; /* line starts over here */
384 pp->linestart_resp = ptr + 1;
385 }
386 }
387
388 if(!keepon && (i != gotbytes)) {
389 /* We found the end of the response lines, but we didn't parse the
390 full chunk of data we have read from the server. We therefore need
391 to store the rest of the data to be checked on the next invoke as
392 it may actually contain another end of response already! */
393 clipamount = gotbytes - i;
394 restart = TRUE;
395 DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
396 "server response left",
397 (int)clipamount));
398 }
399 else if(keepon) {
400
401 if((perline == gotbytes) &&
402 (gotbytes > (ssize_t)data->set.buffer_size/2)) {
403 /* We got an excessive line without newlines and we need to deal
404 with it. We keep the first bytes of the line then we throw
405 away the rest. */
406 infof(data, "Excessive server response line length received, "
407 "%zd bytes. Stripping", gotbytes);
408 restart = TRUE;
409
410 /* we keep 40 bytes since all our pingpong protocols are only
411 interested in the first piece */
412 clipamount = 40;
413 }
414 else if(pp->nread_resp > (size_t)data->set.buffer_size/2) {
415 /* We got a large chunk of data and there's potentially still
416 trailing data to take care of, so we put any such part in the
417 "cache", clear the buffer to make space and restart. */
418 clipamount = perline;
419 restart = TRUE;
420 }
421 }
422 else if(i == gotbytes)
423 restart = TRUE;
424
425 if(clipamount) {
426 pp->cache_size = clipamount;
427 pp->cache = malloc(pp->cache_size);
428 if(pp->cache)
429 memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
430 else
431 return CURLE_OUT_OF_MEMORY;
432 }
433 if(restart) {
434 /* now reset a few variables to start over nicely from the start of
435 the big buffer */
436 pp->nread_resp = 0; /* start over from scratch in the buffer */
437 ptr = pp->linestart_resp = buf;
438 perline = 0;
439 }
440
441 } /* there was data */
442
443 } /* while there's buffer left and loop is requested */
444
445 pp->pending_resp = FALSE;
446
447 return result;
448}
449
450int Curl_pp_getsock(struct Curl_easy *data,
451 struct pingpong *pp, curl_socket_t *socks)
452{
453 struct connectdata *conn = data->conn;
454 socks[0] = conn->sock[FIRSTSOCKET];
455
456 if(pp->sendleft) {
457 /* write mode */
458 return GETSOCK_WRITESOCK(0);
459 }
460
461 /* read mode */
462 return GETSOCK_READSOCK(0);
463}
464
465CURLcode Curl_pp_flushsend(struct Curl_easy *data,
466 struct pingpong *pp)
467{
468 /* we have a piece of a command still left to send */
469 struct connectdata *conn = data->conn;
470 ssize_t written;
471 curl_socket_t sock = conn->sock[FIRSTSOCKET];
472 CURLcode result = Curl_write(data, sock, pp->sendthis + pp->sendsize -
473 pp->sendleft, pp->sendleft, &written);
474 if(result)
475 return result;
476
477 if(written != (ssize_t)pp->sendleft) {
478 /* only a fraction was sent */
479 pp->sendleft -= written;
480 }
481 else {
482 pp->sendthis = NULL;
483 pp->sendleft = pp->sendsize = 0;
484 pp->response = Curl_now();
485 }
486 return CURLE_OK;
487}
488
489CURLcode Curl_pp_disconnect(struct pingpong *pp)
490{
491 Curl_dyn_free(&pp->sendbuf);
492 Curl_safefree(pp->cache);
493 return CURLE_OK;
494}
495
496bool Curl_pp_moredata(struct pingpong *pp)
497{
498 return (!pp->sendleft && pp->cache && pp->nread_resp < pp->cache_size) ?
499 TRUE : FALSE;
500}
501
502#endif
503