1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * 'pingpong' is for generic back-and-forth support functions used by FTP,
22 * IMAP, POP3, SMTP and whatever more that likes them.
24 ***************************************************************************/
26 #include "curl_setup.h"
32 #include "speedcheck.h"
35 #include "non-ascii.h"
36 #include "vtls/vtls.h"
38 /* The last 3 #include files should be in this order */
39 #include "curl_printf.h"
40 #include "curl_memory.h"
45 /* Returns timeout in ms. 0 or negative number means the timeout has already
47 timediff_t Curl_pp_state_timeout(struct pingpong *pp, bool disconnecting)
49 struct connectdata *conn = pp->conn;
50 struct Curl_easy *data = conn->data;
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;
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. */
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 */
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 */
70 /* pick the lowest number */
71 timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
80 CURLcode Curl_pp_statemach(struct pingpong *pp, bool block,
83 struct connectdata *conn = pp->conn;
84 curl_socket_t sock = conn->sock[FIRSTSOCKET];
86 timediff_t interval_ms;
87 timediff_t timeout_ms = Curl_pp_state_timeout(pp, disconnecting);
88 struct Curl_easy *data = conn->data;
89 CURLcode result = CURLE_OK;
92 failf(data, "server response timeout");
93 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
97 interval_ms = 1000; /* use 1 second timeout intervals */
98 if(timeout_ms < interval_ms)
99 interval_ms = timeout_ms;
102 interval_ms = 0; /* immediate */
104 if(Curl_ssl_data_pending(conn, FIRSTSOCKET))
106 else if(Curl_pp_moredata(pp))
107 /* We are receiving and there is data in the cache so just read it */
109 else if(!pp->sendleft && Curl_ssl_data_pending(conn, FIRSTSOCKET))
110 /* We are receiving and there is data ready in the SSL library */
113 rc = Curl_socket_check(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
115 pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
119 /* if we didn't wait, we don't have to spend time on this now */
120 if(Curl_pgrsUpdate(conn))
121 result = CURLE_ABORTED_BY_CALLBACK;
123 result = Curl_speedcheck(data, Curl_now());
130 failf(data, "select/poll error");
131 result = CURLE_OUT_OF_MEMORY;
134 result = pp->statemach_act(conn);
139 /* initialize stuff to prepare for reading a fresh new response */
140 void Curl_pp_init(struct pingpong *pp)
142 struct connectdata *conn = pp->conn;
144 pp->linestart_resp = conn->data->state.buffer;
145 pp->pending_resp = TRUE;
146 pp->response = Curl_now(); /* start response time-out now! */
149 /* setup for the coming transfer */
150 void Curl_pp_setup(struct pingpong *pp)
152 Curl_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
155 /***********************************************************************
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.
163 * made to never block
165 CURLcode Curl_pp_vsendf(struct pingpong *pp,
169 ssize_t bytes_written = 0;
173 struct connectdata *conn = pp->conn;
174 struct Curl_easy *data;
177 enum protection_level data_sec;
180 DEBUGASSERT(pp->sendleft == 0);
181 DEBUGASSERT(pp->sendsize == 0);
182 DEBUGASSERT(pp->sendthis == NULL);
185 /* can't send without a connection! */
186 return CURLE_SEND_ERROR;
189 Curl_dyn_reset(&pp->sendbuf);
190 result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
195 result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
199 write_len = Curl_dyn_len(&pp->sendbuf);
200 s = Curl_dyn_ptr(&pp->sendbuf);
203 result = Curl_convert_to_network(data, s, write_len);
204 /* Curl_convert_to_network calls failf if unsuccessful */
209 conn->data_prot = PROT_CMD;
211 result = Curl_write(conn, conn->sock[FIRSTSOCKET], s, write_len,
216 data_sec = conn->data_prot;
217 DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
218 conn->data_prot = data_sec;
221 if(data->set.verbose)
222 Curl_debug(data, CURLINFO_HEADER_OUT, s, (size_t)bytes_written);
224 if(bytes_written != (ssize_t)write_len) {
225 /* the whole chunk was not sent, keep it around and adjust sizes */
227 pp->sendsize = write_len;
228 pp->sendleft = write_len - bytes_written;
232 pp->sendleft = pp->sendsize = 0;
233 pp->response = Curl_now();
240 /***********************************************************************
244 * Send the formatted string as a command to a pingpong server. Note that
245 * the string should not have any CRLF appended, as this function will
246 * append the necessary things itself.
248 * made to never block
250 CURLcode Curl_pp_sendf(struct pingpong *pp,
251 const char *fmt, ...)
257 result = Curl_pp_vsendf(pp, fmt, ap);
267 * Reads a piece of a server response.
269 CURLcode Curl_pp_readresp(curl_socket_t sockfd,
271 int *code, /* return the server code if done */
272 size_t *size) /* size of the response */
274 ssize_t perline; /* count bytes per line */
278 struct connectdata *conn = pp->conn;
279 struct Curl_easy *data = conn->data;
280 char * const buf = data->state.buffer;
281 CURLcode result = CURLE_OK;
283 *code = 0; /* 0 for errors or not done */
286 ptr = buf + pp->nread_resp;
288 /* number of bytes in the current line, so far */
289 perline = (ssize_t)(ptr-pp->linestart_resp);
291 while((pp->nread_resp < (size_t)data->set.buffer_size) &&
292 (keepon && !result)) {
295 /* we had data in the "cache", copy that instead of doing an actual
298 * pp->cache_size is cast to ssize_t here. This should be safe, because
299 * it would have been populated with something of size int to begin
300 * with, even though its datatype may be larger than an int.
302 if((ptr + pp->cache_size) > (buf + data->set.buffer_size + 1)) {
303 failf(data, "cached response data too big to handle");
304 return CURLE_RECV_ERROR;
306 memcpy(ptr, pp->cache, pp->cache_size);
307 gotbytes = (ssize_t)pp->cache_size;
308 free(pp->cache); /* free the cache */
309 pp->cache = NULL; /* clear the pointer */
310 pp->cache_size = 0; /* zero the size just in case */
314 enum protection_level prot = conn->data_prot;
315 conn->data_prot = PROT_CLEAR;
317 DEBUGASSERT((ptr + data->set.buffer_size - pp->nread_resp) <=
318 (buf + data->set.buffer_size + 1));
319 result = Curl_read(conn, sockfd, ptr,
320 data->set.buffer_size - pp->nread_resp,
323 DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
324 conn->data_prot = prot;
326 if(result == CURLE_AGAIN)
327 return CURLE_OK; /* return */
329 if(!result && (gotbytes > 0))
330 /* convert from the network encoding */
331 result = Curl_convert_from_network(data, ptr, gotbytes);
332 /* Curl_convert_from_network calls failf if unsuccessful */
335 /* Set outer result variable to this error. */
341 else if(gotbytes <= 0) {
343 result = CURLE_RECV_ERROR;
344 failf(data, "response reading failed");
347 /* we got a whole chunk of data, which can be anything from one
348 * byte to a set of lines and possible just a piece of the last
351 ssize_t clipamount = 0;
352 bool restart = FALSE;
354 data->req.headerbytecount += (long)gotbytes;
356 pp->nread_resp += gotbytes;
357 for(i = 0; i < gotbytes; ptr++, i++) {
360 /* a newline is CRLF in pp-talk, so the CR is ignored as
361 the line isn't really terminated until the LF comes */
363 /* output debug output if that is requested */
365 if(!conn->sec_complete)
367 if(data->set.verbose)
368 Curl_debug(data, CURLINFO_HEADER_IN,
369 pp->linestart_resp, (size_t)perline);
372 * We pass all response-lines to the callback function registered
373 * for "headers". The response lines can be seen as a kind of
376 result = Curl_client_write(conn, CLIENTWRITE_HEADER,
377 pp->linestart_resp, perline);
381 if(pp->endofresp(conn, pp->linestart_resp, perline, code)) {
382 /* This is the end of the last line, copy the last line to the
383 start of the buffer and null-terminate, for old times sake */
384 size_t n = ptr - pp->linestart_resp;
385 memmove(buf, pp->linestart_resp, n);
386 buf[n] = 0; /* null-terminate */
388 pp->linestart_resp = ptr + 1; /* advance pointer */
389 i++; /* skip this before getting out */
391 *size = pp->nread_resp; /* size of the response */
392 pp->nread_resp = 0; /* restart */
395 perline = 0; /* line starts over here */
396 pp->linestart_resp = ptr + 1;
400 if(!keepon && (i != gotbytes)) {
401 /* We found the end of the response lines, but we didn't parse the
402 full chunk of data we have read from the server. We therefore need
403 to store the rest of the data to be checked on the next invoke as
404 it may actually contain another end of response already! */
405 clipamount = gotbytes - i;
407 DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
408 "server response left\n",
413 if((perline == gotbytes) && (gotbytes > data->set.buffer_size/2)) {
414 /* We got an excessive line without newlines and we need to deal
415 with it. We keep the first bytes of the line then we throw
417 infof(data, "Excessive server response line length received, "
418 "%zd bytes. Stripping\n", gotbytes);
421 /* we keep 40 bytes since all our pingpong protocols are only
422 interested in the first piece */
425 else if(pp->nread_resp > (size_t)data->set.buffer_size/2) {
426 /* We got a large chunk of data and there's potentially still
427 trailing data to take care of, so we put any such part in the
428 "cache", clear the buffer to make space and restart. */
429 clipamount = perline;
433 else if(i == gotbytes)
437 pp->cache_size = clipamount;
438 pp->cache = malloc(pp->cache_size);
440 memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
442 return CURLE_OUT_OF_MEMORY;
445 /* now reset a few variables to start over nicely from the start of
447 pp->nread_resp = 0; /* start over from scratch in the buffer */
448 ptr = pp->linestart_resp = buf;
452 } /* there was data */
454 } /* while there's buffer left and loop is requested */
456 pp->pending_resp = FALSE;
461 int Curl_pp_getsock(struct pingpong *pp,
462 curl_socket_t *socks)
464 struct connectdata *conn = pp->conn;
465 socks[0] = conn->sock[FIRSTSOCKET];
469 return GETSOCK_WRITESOCK(0);
473 return GETSOCK_READSOCK(0);
476 CURLcode Curl_pp_flushsend(struct pingpong *pp)
478 /* we have a piece of a command still left to send */
479 struct connectdata *conn = pp->conn;
481 curl_socket_t sock = conn->sock[FIRSTSOCKET];
482 CURLcode result = Curl_write(conn, sock, pp->sendthis + pp->sendsize -
483 pp->sendleft, pp->sendleft, &written);
487 if(written != (ssize_t)pp->sendleft) {
488 /* only a fraction was sent */
489 pp->sendleft -= written;
493 pp->sendleft = pp->sendsize = 0;
494 pp->response = Curl_now();
499 CURLcode Curl_pp_disconnect(struct pingpong *pp)
501 Curl_dyn_free(&pp->sendbuf);
502 Curl_safefree(pp->cache);
506 bool Curl_pp_moredata(struct pingpong *pp)
508 return (!pp->sendleft && pp->cache && pp->nread_resp < pp->cache_size) ?