Coverage Report

Created: 2026-12-01 06:27

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