Coverage Report

Created: 2026-09-14 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/pingpong.c
Line
Count
Source
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
#include "curl_setup.h"
28
29
#include "urldata.h"
30
#include "pingpong.h"
31
32
#ifdef USE_PINGPONG
33
34
#include "cfilters.h"
35
#include "connect.h"
36
#include "multiif.h"
37
#include "sendf.h"
38
#include "curl_trc.h"
39
#include "select.h"
40
#include "progress.h"
41
42
timediff_t Curl_pp_state_timeleft_ms(struct Curl_easy *data,
43
                                     struct pingpong *pp)
44
3.88k
{
45
3.88k
  timediff_t xfer_remain_ms;
46
3.88k
  timediff_t remain_ms = data->set.server_response_timeout ?
47
3.88k
    data->set.server_response_timeout : PINGPONG_TIMEOUT_MS;
48
49
  /* If the overall transfer has less time remaining than pingpong
50
   * has otherwise for the state, return that. */
51
3.88k
  remain_ms -= curlx_ptimediff_ms(Curl_pgrs_now(data), &pp->response);
52
  /* transfer remaining time is 0, when it has no timeout. */
53
3.88k
  xfer_remain_ms = Curl_timeleft_ms(data);
54
3.88k
  if(xfer_remain_ms)
55
3.87k
    return CURLMIN(remain_ms, xfer_remain_ms);
56
11
  return remain_ms;
57
3.88k
}
58
59
/*
60
 * Curl_pp_statemach()
61
 */
62
CURLcode Curl_pp_statemach(struct Curl_easy *data,
63
                           struct pingpong *pp, bool block,
64
                           bool disconnecting)
65
3.88k
{
66
3.88k
  struct connectdata *conn = data->conn;
67
3.88k
  curl_socket_t sock = conn->sock[FIRSTSOCKET];
68
3.88k
  int rc;
69
3.88k
  timediff_t interval_ms;
70
3.88k
  timediff_t timeout_ms = Curl_pp_state_timeleft_ms(data, pp);
71
3.88k
  CURLcode result = CURLE_OK;
72
73
3.88k
  if(timeout_ms <= 0) {
74
0
    failf(data, "server response timeout");
75
0
    return CURLE_OPERATION_TIMEDOUT; /* already too little time */
76
0
  }
77
78
3.88k
  if(block) {
79
580
    interval_ms = 1000;  /* use 1 second timeout intervals */
80
580
    if(timeout_ms < interval_ms)
81
0
      interval_ms = timeout_ms;
82
580
  }
83
3.30k
  else
84
3.30k
    interval_ms = 0; /* immediate */
85
86
3.88k
  if(Curl_conn_data_pending(data, FIRSTSOCKET))
87
0
    rc = 1;
88
3.88k
  else if(pp->overflow)
89
    /* We are receiving and there is data in the cache so read it */
90
672
    rc = 1;
91
3.21k
  else if(!pp->sendleft && Curl_conn_data_pending(data, FIRSTSOCKET))
92
    /* We are receiving and there is data ready in the SSL library */
93
0
    rc = 1;
94
3.21k
  else {
95
3.21k
    rc = Curl_socket_check(pp->sendleft ? CURL_SOCKET_BAD : sock, /* reading */
96
3.21k
                           CURL_SOCKET_BAD,
97
3.21k
                           pp->sendleft ? sock : CURL_SOCKET_BAD, /* writing */
98
3.21k
                           interval_ms);
99
3.21k
  }
100
101
3.88k
  if(block) {
102
    /* if we did not wait, we do not have to spend time on this now */
103
580
    result = Curl_pgrsCheck(data);
104
580
    if(result)
105
0
      return result;
106
580
  }
107
108
3.88k
  if(rc == -1) {
109
0
    failf(data, "select/poll error");
110
0
    result = CURLE_OUT_OF_MEMORY;
111
0
  }
112
3.88k
  else if(rc)
113
3.60k
    result = pp->statemachine(data, data->conn);
114
279
  else if(disconnecting)
115
14
    return CURLE_OPERATION_TIMEDOUT;
116
117
3.87k
  return result;
118
3.88k
}
119
120
/* initialize stuff to prepare for reading a fresh new response */
121
void Curl_pp_init(struct pingpong *pp, const struct curltime *pnow)
122
1.80k
{
123
1.80k
  DEBUGASSERT(!pp->initialized);
124
1.80k
  pp->nread_resp = 0;
125
1.80k
  pp->response = *pnow; /* start response time-out */
126
1.80k
  pp->pending_resp = TRUE;
127
1.80k
  curlx_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
128
1.80k
  curlx_dyn_init(&pp->recvbuf, DYN_PINGPPONG_CMD);
129
1.80k
  pp->initialized = TRUE;
130
1.80k
}
131
132
/***********************************************************************
133
 *
134
 * Curl_pp_vsendf()
135
 *
136
 * Send the formatted string as a command to a pingpong server. Note that
137
 * the string should not have any CRLF appended, as this function will
138
 * append the necessary things itself.
139
 *
140
 * made to never block
141
 */
142
CURLcode Curl_pp_vsendf(struct Curl_easy *data,
143
                        struct pingpong *pp,
144
                        const char *fmt,
145
                        va_list args)
146
2.60k
{
147
2.60k
  size_t bytes_written = 0;
148
2.60k
  size_t write_len;
149
2.60k
  char *s;
150
2.60k
  CURLcode result;
151
2.60k
  struct connectdata *conn = data->conn;
152
153
2.60k
  DEBUGASSERT(pp->sendleft == 0);
154
2.60k
  DEBUGASSERT(pp->sendsize == 0);
155
2.60k
  DEBUGASSERT(!pp->sendthis);
156
157
2.60k
  if(!conn)
158
    /* cannot send without a connection! */
159
0
    return CURLE_SEND_ERROR;
160
161
2.60k
  curlx_dyn_reset(&pp->sendbuf);
162
2.60k
  result = curlx_dyn_vaddf(&pp->sendbuf, fmt, args);
163
2.60k
  if(result)
164
3
    return result;
165
166
  /* append CRLF */
167
2.60k
  result = curlx_dyn_addn(&pp->sendbuf, "\r\n", 2);
168
2.60k
  if(result)
169
0
    return result;
170
171
2.60k
  pp->pending_resp = TRUE;
172
2.60k
  write_len = curlx_dyn_len(&pp->sendbuf);
173
2.60k
  s = curlx_dyn_ptr(&pp->sendbuf);
174
175
2.60k
  result = Curl_conn_send(data, FIRSTSOCKET, s, write_len, FALSE,
176
2.60k
                          &bytes_written);
177
2.60k
  if(result == CURLE_AGAIN) {
178
0
    bytes_written = 0;
179
0
  }
180
2.60k
  else if(result)
181
0
    return result;
182
183
2.60k
  Curl_debug(data, CURLINFO_HEADER_OUT, s, bytes_written);
184
185
2.60k
  if(bytes_written != write_len) {
186
    /* the whole chunk was not sent, keep it around and adjust sizes */
187
0
    pp->sendthis = s;
188
0
    pp->sendsize = write_len;
189
0
    pp->sendleft = write_len - bytes_written;
190
0
  }
191
2.60k
  else {
192
2.60k
    pp->sendthis = NULL;
193
2.60k
    pp->sendleft = pp->sendsize = 0;
194
2.60k
    pp->response = *Curl_pgrs_now(data);
195
2.60k
  }
196
197
2.60k
  return CURLE_OK;
198
2.60k
}
199
200
/***********************************************************************
201
 *
202
 * Curl_pp_sendf()
203
 *
204
 * Send the formatted string as a command to a pingpong server. Note that
205
 * the string should not have any CRLF appended, as this function will
206
 * append the necessary things itself.
207
 *
208
 * made to never block
209
 */
210
CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
211
                       const char *fmt, ...)
212
2.60k
{
213
2.60k
  CURLcode result;
214
2.60k
  va_list ap;
215
2.60k
  va_start(ap, fmt);
216
217
2.60k
  result = Curl_pp_vsendf(data, pp, fmt, ap);
218
219
2.60k
  va_end(ap);
220
221
2.60k
  return result;
222
2.60k
}
223
224
static CURLcode pingpong_read(struct Curl_easy *data,
225
                              int8_t sockindex,
226
                              char *buffer,
227
                              size_t buflen,
228
                              size_t *nread)
229
3.20k
{
230
3.20k
  return Curl_conn_recv(data, sockindex, buffer, buflen, nread);
231
3.20k
}
232
233
/*
234
 * Curl_pp_readresp()
235
 *
236
 * Reads a piece of a server response.
237
 */
238
CURLcode Curl_pp_readresp(struct Curl_easy *data,
239
                          int8_t sockindex,
240
                          struct pingpong *pp,
241
                          int *code, /* return the server code if done */
242
                          size_t *size) /* size of the response */
243
12.4k
{
244
12.4k
  struct connectdata *conn = data->conn;
245
12.4k
  CURLcode result = CURLE_OK;
246
12.4k
  size_t gotbytes;
247
12.4k
  char buffer[900];
248
249
12.4k
  *code = 0; /* 0 for errors or not done */
250
12.4k
  *size = 0;
251
252
12.7k
  do {
253
12.7k
    gotbytes = 0;
254
12.7k
    if(pp->nfinal) {
255
      /* a previous call left this many bytes in the beginning of the buffer as
256
         that was the final line; now ditch that */
257
9.95k
      size_t full = curlx_dyn_len(&pp->recvbuf);
258
259
      /* trim off the "final" leading part */
260
9.95k
      curlx_dyn_tail(&pp->recvbuf, full - pp->nfinal);
261
262
9.95k
      pp->nfinal = 0; /* now gone */
263
9.95k
    }
264
12.7k
    if(!pp->overflow) {
265
3.20k
      result = pingpong_read(data, sockindex, buffer, sizeof(buffer),
266
3.20k
                             &gotbytes);
267
3.20k
      if(result == CURLE_AGAIN)
268
1
        return CURLE_OK;
269
270
3.20k
      if(result)
271
0
        return result;
272
273
3.20k
      if(!gotbytes) {
274
1.54k
        failf(data, "response reading failed (errno: %d)", SOCKERRNO);
275
1.54k
        return CURLE_RECV_ERROR;
276
1.54k
      }
277
278
1.66k
      result = curlx_dyn_addn(&pp->recvbuf, buffer, gotbytes);
279
1.66k
      if(result)
280
1
        return result;
281
282
1.65k
      data->req.headerbytecount += (unsigned int)gotbytes;
283
284
1.65k
      pp->nread_resp += gotbytes;
285
1.65k
    }
286
287
12.4k
    do {
288
12.4k
      const char *line = curlx_dyn_ptr(&pp->recvbuf);
289
12.4k
      const char *nl = memchr(line, '\n', curlx_dyn_len(&pp->recvbuf));
290
12.4k
      if(nl) {
291
        /* a newline is CRLF in pp-talk, so the CR is ignored as
292
           the line is not really terminated until the LF comes */
293
11.7k
        size_t length = nl - line + 1;
294
295
11.7k
        if(memchr(line, 0, length)) {
296
          /* The response line is passed on as a "header" below, so reject an
297
             embedded nul the same way verify_header() does for HTTP. */
298
12
          failf(data, "Nul byte in server response line");
299
12
          return CURLE_WEIRD_SERVER_REPLY;
300
12
        }
301
302
        /* output debug output if that is requested */
303
11.7k
        Curl_debug(data, CURLINFO_HEADER_IN, line, length);
304
305
        /*
306
         * Pass all response-lines to the callback function registered for
307
         * "headers". The response lines can be seen as a kind of headers.
308
         */
309
11.7k
        result = Curl_client_write(data, CLIENTWRITE_INFO, line, length);
310
11.7k
        if(result)
311
0
          return result;
312
313
11.7k
        if(pp->endofresp(data, conn, line, length, code)) {
314
          /* When at "end of response", keep the endofresp line first in the
315
             buffer since it will be accessed outside (by pingpong
316
             parsers). Store the overflow counter to inform about additional
317
             data in this buffer after the endofresp line. */
318
10.4k
          pp->nfinal = length;
319
10.4k
          if(curlx_dyn_len(&pp->recvbuf) > length)
320
9.96k
            pp->overflow = curlx_dyn_len(&pp->recvbuf) - length;
321
504
          else
322
504
            pp->overflow = 0;
323
10.4k
          *size = pp->nread_resp; /* size of the response */
324
10.4k
          pp->nread_resp = 0; /* restart */
325
10.4k
          gotbytes = 0; /* force break out of outer loop */
326
10.4k
          break;
327
10.4k
        }
328
1.29k
        if(curlx_dyn_len(&pp->recvbuf) > length)
329
          /* keep the remaining piece */
330
1.20k
          curlx_dyn_tail((&pp->recvbuf), curlx_dyn_len(&pp->recvbuf) - length);
331
89
        else
332
89
          curlx_dyn_reset(&pp->recvbuf);
333
1.29k
      }
334
719
      else {
335
        /* without a newline, there is no overflow */
336
719
        pp->overflow = 0;
337
719
        break;
338
719
      }
339
340
12.4k
    } while(1); /* while there is buffer left to scan */
341
342
11.1k
  } while(gotbytes == sizeof(buffer));
343
344
10.9k
  pp->pending_resp = FALSE;
345
346
10.9k
  return result;
347
12.4k
}
348
349
CURLcode Curl_pp_pollset(struct Curl_easy *data,
350
                         struct pingpong *pp,
351
                         struct easy_pollset *ps)
352
314
{
353
314
  int flags = pp->sendleft ? CURL_POLL_OUT : CURL_POLL_IN;
354
314
  return Curl_pollset_change(data, ps, data->conn->sock[FIRSTSOCKET],
355
314
                             flags, 0);
356
314
}
357
358
bool Curl_pp_needs_flush(struct Curl_easy *data,
359
                         struct pingpong *pp)
360
552
{
361
552
  (void)data;
362
552
  return pp->sendleft > 0;
363
552
}
364
365
CURLcode Curl_pp_flushsend(struct Curl_easy *data,
366
                           struct pingpong *pp)
367
0
{
368
  /* we have a piece of a command still left to send */
369
0
  size_t written;
370
0
  CURLcode result;
371
372
0
  if(!Curl_pp_needs_flush(data, pp))
373
0
    return CURLE_OK;
374
375
0
  result = Curl_conn_send(data, FIRSTSOCKET,
376
0
                          pp->sendthis + pp->sendsize - pp->sendleft,
377
0
                          pp->sendleft, FALSE, &written);
378
0
  if(result == CURLE_AGAIN) {
379
0
    result = CURLE_OK;
380
0
    written = 0;
381
0
  }
382
0
  if(result)
383
0
    return result;
384
385
0
  if(written != pp->sendleft) {
386
    /* only a fraction was sent */
387
0
    pp->sendleft -= written;
388
0
  }
389
0
  else {
390
0
    pp->sendthis = NULL;
391
0
    pp->sendleft = pp->sendsize = 0;
392
0
    pp->response = *Curl_pgrs_now(data);
393
0
  }
394
0
  return CURLE_OK;
395
0
}
396
397
CURLcode Curl_pp_disconnect(struct pingpong *pp)
398
13.9k
{
399
13.9k
  if(pp->initialized) {
400
1.80k
    curlx_dyn_free(&pp->sendbuf);
401
1.80k
    curlx_dyn_free(&pp->recvbuf);
402
1.80k
    memset(pp, 0, sizeof(*pp));
403
1.80k
  }
404
13.9k
  return CURLE_OK;
405
13.9k
}
406
407
bool Curl_pp_moredata(struct pingpong *pp)
408
9.11k
{
409
9.11k
  return !pp->sendleft && curlx_dyn_len(&pp->recvbuf) > pp->nfinal;
410
9.11k
}
411
412
#endif