Coverage Report

Created: 2025-07-11 07:03

/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 "connect.h"
33
#include "sendf.h"
34
#include "select.h"
35
#include "progress.h"
36
#include "speedcheck.h"
37
#include "pingpong.h"
38
#include "multiif.h"
39
#include "vtls/vtls.h"
40
#include "strdup.h"
41
42
/* The last 3 #include files should be in this order */
43
#include "curl_printf.h"
44
#include "curl_memory.h"
45
#include "memdebug.h"
46
47
#ifdef USE_PINGPONG
48
49
/* Returns timeout in ms. 0 or negative number means the timeout has already
50
   triggered */
51
timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
52
                                 struct pingpong *pp, bool disconnecting)
53
10.2M
{
54
10.2M
  timediff_t timeout_ms; /* in milliseconds */
55
10.2M
  timediff_t response_time = (data->set.server_response_timeout > 0) ?
56
10.2M
    data->set.server_response_timeout : pp->response_time;
57
10.2M
  struct curltime now = curlx_now();
58
59
  /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
60
     remaining time, or use pp->response because SERVER_RESPONSE_TIMEOUT is
61
     supposed to govern the response for any given server response, not for
62
     the time from connect to the given server response. */
63
64
  /* Without a requested timeout, we only wait 'response_time' seconds for the
65
     full response to arrive before we bail out */
66
10.2M
  timeout_ms = response_time - curlx_timediff(now, pp->response);
67
68
10.2M
  if((data->set.timeout > 0) && !disconnecting) {
69
    /* if timeout is requested, find out how much overall remains */
70
10.2M
    timediff_t timeout2_ms = Curl_timeleft(data, &now, FALSE);
71
    /* pick the lowest number */
72
10.2M
    timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
73
10.2M
  }
74
75
10.2M
  if(disconnecting) {
76
3.19k
    timediff_t total_left_ms = Curl_timeleft(data, NULL, FALSE);
77
3.19k
    timeout_ms = CURLMIN(timeout_ms, CURLMAX(total_left_ms, 0));
78
3.19k
  }
79
80
10.2M
  return timeout_ms;
81
10.2M
}
82
83
/*
84
 * Curl_pp_statemach()
85
 */
86
CURLcode Curl_pp_statemach(struct Curl_easy *data,
87
                           struct pingpong *pp, bool block,
88
                           bool disconnecting)
89
10.2M
{
90
10.2M
  struct connectdata *conn = data->conn;
91
10.2M
  curl_socket_t sock = conn->sock[FIRSTSOCKET];
92
10.2M
  int rc;
93
10.2M
  timediff_t interval_ms;
94
10.2M
  timediff_t timeout_ms = Curl_pp_state_timeout(data, pp, disconnecting);
95
10.2M
  CURLcode result = CURLE_OK;
96
97
10.2M
  if(timeout_ms <= 0) {
98
128
    failf(data, "server response timeout");
99
128
    return CURLE_OPERATION_TIMEDOUT; /* already too little time */
100
128
  }
101
102
10.2M
  DEBUGF(infof(data, "pp_statematch, timeout=%" FMT_TIMEDIFF_T, timeout_ms));
103
10.2M
  if(block) {
104
10.1M
    interval_ms = 1000;  /* use 1 second timeout intervals */
105
10.1M
    if(timeout_ms < interval_ms)
106
10.1M
      interval_ms = timeout_ms;
107
10.1M
  }
108
89.8k
  else
109
89.8k
    interval_ms = 0; /* immediate */
110
111
10.2M
  if(Curl_conn_data_pending(data, FIRSTSOCKET))
112
0
    rc = 1;
113
10.2M
  else if(pp->overflow)
114
    /* We are receiving and there is data in the cache so just read it */
115
10.2M
    rc = 1;
116
23.9k
  else if(!pp->sendleft && Curl_conn_data_pending(data, FIRSTSOCKET))
117
    /* We are receiving and there is data ready in the SSL library */
118
0
    rc = 1;
119
23.9k
  else {
120
23.9k
    DEBUGF(infof(data, "pp_statematch, select, timeout=%" FMT_TIMEDIFF_T
121
23.9k
           ", sendleft=%zu",
122
23.9k
           timeout_ms, pp->sendleft));
123
23.9k
    rc = Curl_socket_check(pp->sendleft ? CURL_SOCKET_BAD : sock, /* reading */
124
23.9k
                           CURL_SOCKET_BAD,
125
23.9k
                           pp->sendleft ? sock : CURL_SOCKET_BAD, /* writing */
126
23.9k
                           interval_ms);
127
23.9k
  }
128
129
10.2M
  if(block) {
130
    /* if we did not wait, we do not have to spend time on this now */
131
10.1M
    if(Curl_pgrsUpdate(data))
132
0
      result = CURLE_ABORTED_BY_CALLBACK;
133
10.1M
    else
134
10.1M
      result = Curl_speedcheck(data, curlx_now());
135
136
10.1M
    if(result)
137
0
      return result;
138
10.1M
  }
139
140
10.2M
  if(rc == -1) {
141
0
    failf(data, "select/poll error");
142
0
    result = CURLE_OUT_OF_MEMORY;
143
0
  }
144
10.2M
  else if(rc)
145
10.2M
    result = pp->statemachine(data, data->conn);
146
1.82k
  else if(disconnecting)
147
76
    return CURLE_OPERATION_TIMEDOUT;
148
149
10.2M
  return result;
150
10.2M
}
151
152
/* initialize stuff to prepare for reading a fresh new response */
153
void Curl_pp_init(struct pingpong *pp)
154
12.7k
{
155
12.7k
  DEBUGASSERT(!pp->initialised);
156
12.7k
  pp->nread_resp = 0;
157
12.7k
  pp->response = curlx_now(); /* start response time-out now! */
158
12.7k
  pp->pending_resp = TRUE;
159
12.7k
  curlx_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
160
12.7k
  curlx_dyn_init(&pp->recvbuf, DYN_PINGPPONG_CMD);
161
12.7k
  pp->initialised = TRUE;
162
12.7k
}
163
164
/***********************************************************************
165
 *
166
 * Curl_pp_vsendf()
167
 *
168
 * Send the formatted string as a command to a pingpong server. Note that
169
 * the string should not have any CRLF appended, as this function will
170
 * append the necessary things itself.
171
 *
172
 * made to never block
173
 */
174
CURLcode Curl_pp_vsendf(struct Curl_easy *data,
175
                        struct pingpong *pp,
176
                        const char *fmt,
177
                        va_list args)
178
22.5k
{
179
22.5k
  size_t bytes_written = 0;
180
22.5k
  size_t write_len;
181
22.5k
  char *s;
182
22.5k
  CURLcode result;
183
22.5k
  struct connectdata *conn = data->conn;
184
185
#ifdef HAVE_GSSAPI
186
  enum protection_level data_sec;
187
#endif
188
189
22.5k
  DEBUGASSERT(pp->sendleft == 0);
190
22.5k
  DEBUGASSERT(pp->sendsize == 0);
191
22.5k
  DEBUGASSERT(pp->sendthis == NULL);
192
193
22.5k
  if(!conn)
194
    /* cannot send without a connection! */
195
0
    return CURLE_SEND_ERROR;
196
197
22.5k
  curlx_dyn_reset(&pp->sendbuf);
198
22.5k
  result = curlx_dyn_vaddf(&pp->sendbuf, fmt, args);
199
22.5k
  if(result)
200
2
    return result;
201
202
  /* append CRLF */
203
22.5k
  result = curlx_dyn_addn(&pp->sendbuf, "\r\n", 2);
204
22.5k
  if(result)
205
0
    return result;
206
207
22.5k
  pp->pending_resp = TRUE;
208
22.5k
  write_len = curlx_dyn_len(&pp->sendbuf);
209
22.5k
  s = curlx_dyn_ptr(&pp->sendbuf);
210
211
#ifdef HAVE_GSSAPI
212
  conn->data_prot = PROT_CMD;
213
#endif
214
22.5k
  result = Curl_conn_send(data, FIRSTSOCKET, s, write_len, FALSE,
215
22.5k
                          &bytes_written);
216
22.5k
  if(result == CURLE_AGAIN) {
217
44
    bytes_written = 0;
218
44
  }
219
22.4k
  else if(result)
220
0
    return result;
221
#ifdef HAVE_GSSAPI
222
  data_sec = conn->data_prot;
223
  DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
224
  conn->data_prot = (unsigned char)data_sec;
225
#endif
226
227
22.5k
  Curl_debug(data, CURLINFO_HEADER_OUT, s, bytes_written);
228
229
22.5k
  if(bytes_written != write_len) {
230
    /* the whole chunk was not sent, keep it around and adjust sizes */
231
44
    pp->sendthis = s;
232
44
    pp->sendsize = write_len;
233
44
    pp->sendleft = write_len - bytes_written;
234
44
  }
235
22.4k
  else {
236
22.4k
    pp->sendthis = NULL;
237
22.4k
    pp->sendleft = pp->sendsize = 0;
238
22.4k
    pp->response = curlx_now();
239
22.4k
  }
240
241
22.5k
  return CURLE_OK;
242
22.5k
}
243
244
245
/***********************************************************************
246
 *
247
 * Curl_pp_sendf()
248
 *
249
 * Send the formatted string as a command to a pingpong server. Note that
250
 * the string should not have any CRLF appended, as this function will
251
 * append the necessary things itself.
252
 *
253
 * made to never block
254
 */
255
CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
256
                       const char *fmt, ...)
257
17.4k
{
258
17.4k
  CURLcode result;
259
17.4k
  va_list ap;
260
17.4k
  va_start(ap, fmt);
261
262
17.4k
  result = Curl_pp_vsendf(data, pp, fmt, ap);
263
264
17.4k
  va_end(ap);
265
266
17.4k
  return result;
267
17.4k
}
268
269
static CURLcode pingpong_read(struct Curl_easy *data,
270
                              int sockindex,
271
                              char *buffer,
272
                              size_t buflen,
273
                              size_t *nread)
274
27.8k
{
275
27.8k
  CURLcode result;
276
#ifdef HAVE_GSSAPI
277
  enum protection_level prot = data->conn->data_prot;
278
  data->conn->data_prot = PROT_CLEAR;
279
#endif
280
27.8k
  result = Curl_conn_recv(data, sockindex, buffer, buflen, nread);
281
#ifdef HAVE_GSSAPI
282
  DEBUGASSERT(prot  > PROT_NONE && prot < PROT_LAST);
283
  data->conn->data_prot = (unsigned char)prot;
284
#endif
285
27.8k
  return result;
286
27.8k
}
287
288
/*
289
 * Curl_pp_readresp()
290
 *
291
 * Reads a piece of a server response.
292
 */
293
CURLcode Curl_pp_readresp(struct Curl_easy *data,
294
                          int sockindex,
295
                          struct pingpong *pp,
296
                          int *code, /* return the server code if done */
297
                          size_t *size) /* size of the response */
298
628k
{
299
628k
  struct connectdata *conn = data->conn;
300
628k
  CURLcode result = CURLE_OK;
301
628k
  size_t gotbytes;
302
628k
  char buffer[900];
303
304
628k
  *code = 0; /* 0 for errors or not done */
305
628k
  *size = 0;
306
307
635k
  do {
308
635k
    gotbytes = 0;
309
635k
    if(pp->nfinal) {
310
      /* a previous call left this many bytes in the beginning of the buffer as
311
         that was the final line; now ditch that */
312
610k
      size_t full = curlx_dyn_len(&pp->recvbuf);
313
314
      /* trim off the "final" leading part */
315
610k
      curlx_dyn_tail(&pp->recvbuf, full -  pp->nfinal);
316
317
610k
      pp->nfinal = 0; /* now gone */
318
610k
    }
319
635k
    if(!pp->overflow) {
320
27.8k
      result = pingpong_read(data, sockindex, buffer, sizeof(buffer),
321
27.8k
                             &gotbytes);
322
27.8k
      if(result == CURLE_AGAIN)
323
5
        return CURLE_OK;
324
325
27.8k
      if(result)
326
0
        return result;
327
328
27.8k
      if(!gotbytes) {
329
6.20k
        failf(data, "response reading failed (errno: %d)", SOCKERRNO);
330
6.20k
        return CURLE_RECV_ERROR;
331
6.20k
      }
332
333
21.5k
      result = curlx_dyn_addn(&pp->recvbuf, buffer, gotbytes);
334
21.5k
      if(result)
335
4
        return result;
336
337
21.5k
      data->req.headerbytecount += (unsigned int)gotbytes;
338
339
21.5k
      pp->nread_resp += gotbytes;
340
21.5k
    }
341
342
1.66M
    do {
343
1.66M
      char *line = curlx_dyn_ptr(&pp->recvbuf);
344
1.66M
      char *nl = memchr(line, '\n', curlx_dyn_len(&pp->recvbuf));
345
1.66M
      if(nl) {
346
        /* a newline is CRLF in pp-talk, so the CR is ignored as
347
           the line is not really terminated until the LF comes */
348
1.64M
        size_t length = nl - line + 1;
349
350
        /* output debug output if that is requested */
351
#ifdef HAVE_GSSAPI
352
        if(!conn->sec_complete)
353
#endif
354
1.64M
          Curl_debug(data, CURLINFO_HEADER_IN, line, length);
355
356
        /*
357
         * Pass all response-lines to the callback function registered for
358
         * "headers". The response lines can be seen as a kind of headers.
359
         */
360
1.64M
        result = Curl_client_write(data, CLIENTWRITE_INFO, line, length);
361
1.64M
        if(result)
362
0
          return result;
363
364
1.64M
        if(pp->endofresp(data, conn, line, length, code)) {
365
          /* When at "end of response", keep the endofresp line first in the
366
             buffer since it will be accessed outside (by pingpong
367
             parsers). Store the overflow counter to inform about additional
368
             data in this buffer after the endofresp line. */
369
613k
          pp->nfinal = length;
370
613k
          if(curlx_dyn_len(&pp->recvbuf) > length)
371
610k
            pp->overflow = curlx_dyn_len(&pp->recvbuf) - length;
372
3.19k
          else
373
3.19k
            pp->overflow = 0;
374
613k
          *size = pp->nread_resp; /* size of the response */
375
613k
          pp->nread_resp = 0; /* restart */
376
613k
          gotbytes = 0; /* force break out of outer loop */
377
613k
          break;
378
613k
        }
379
1.03M
        if(curlx_dyn_len(&pp->recvbuf) > length)
380
          /* keep the remaining piece */
381
1.03M
          curlx_dyn_tail((&pp->recvbuf), curlx_dyn_len(&pp->recvbuf) - length);
382
1.64k
        else
383
1.64k
          curlx_dyn_reset(&pp->recvbuf);
384
1.03M
      }
385
16.3k
      else {
386
        /* without a newline, there is no overflow */
387
16.3k
        pp->overflow = 0;
388
16.3k
        break;
389
16.3k
      }
390
391
1.66M
    } while(1); /* while there is buffer left to scan */
392
393
629k
  } while(gotbytes == sizeof(buffer));
394
395
622k
  pp->pending_resp = FALSE;
396
397
622k
  return result;
398
628k
}
399
400
int Curl_pp_getsock(struct Curl_easy *data,
401
                    struct pingpong *pp, curl_socket_t *socks)
402
69.5k
{
403
69.5k
  struct connectdata *conn = data->conn;
404
69.5k
  socks[0] = conn->sock[FIRSTSOCKET];
405
406
69.5k
  if(pp->sendleft) {
407
    /* write mode */
408
0
    return GETSOCK_WRITESOCK(0);
409
0
  }
410
411
  /* read mode */
412
69.5k
  return GETSOCK_READSOCK(0);
413
69.5k
}
414
415
bool Curl_pp_needs_flush(struct Curl_easy *data,
416
                         struct pingpong *pp)
417
10.2M
{
418
10.2M
  (void)data;
419
10.2M
  return pp->sendleft > 0;
420
10.2M
}
421
422
CURLcode Curl_pp_flushsend(struct Curl_easy *data,
423
                           struct pingpong *pp)
424
10.1M
{
425
  /* we have a piece of a command still left to send */
426
10.1M
  size_t written;
427
10.1M
  CURLcode result;
428
429
10.1M
  if(!Curl_pp_needs_flush(data, pp))
430
0
    return CURLE_OK;
431
432
10.1M
  result = Curl_conn_send(data, FIRSTSOCKET,
433
10.1M
                          pp->sendthis + pp->sendsize - pp->sendleft,
434
10.1M
                          pp->sendleft, FALSE, &written);
435
10.1M
  if(result == CURLE_AGAIN) {
436
10.1M
    result = CURLE_OK;
437
10.1M
    written = 0;
438
10.1M
  }
439
10.1M
  if(result)
440
0
    return result;
441
442
10.1M
  if(written != pp->sendleft) {
443
    /* only a fraction was sent */
444
10.1M
    pp->sendleft -= written;
445
10.1M
  }
446
0
  else {
447
0
    pp->sendthis = NULL;
448
0
    pp->sendleft = pp->sendsize = 0;
449
0
    pp->response = curlx_now();
450
0
  }
451
10.1M
  return CURLE_OK;
452
10.1M
}
453
454
CURLcode Curl_pp_disconnect(struct pingpong *pp)
455
29.3k
{
456
29.3k
  if(pp->initialised) {
457
12.7k
    curlx_dyn_free(&pp->sendbuf);
458
12.7k
    curlx_dyn_free(&pp->recvbuf);
459
12.7k
    memset(pp, 0, sizeof(*pp));
460
12.7k
  }
461
29.3k
  return CURLE_OK;
462
29.3k
}
463
464
bool Curl_pp_moredata(struct pingpong *pp)
465
516k
{
466
516k
  return !pp->sendleft && curlx_dyn_len(&pp->recvbuf) > pp->nfinal;
467
516k
}
468
469
#endif