Coverage Report

Created: 2023-06-07 07:02

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