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