Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmcurl/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
  timediff_t xfer_remain_ms;
46
0
  timediff_t remain_ms = data->set.server_response_timeout ?
47
0
    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
0
  remain_ms -= curlx_ptimediff_ms(Curl_pgrs_now(data), &pp->response);
52
  /* transfer remaining time is 0, when it has no timeout. */
53
0
  xfer_remain_ms = Curl_timeleft_ms(data);
54
0
  if(xfer_remain_ms)
55
0
    return CURLMIN(remain_ms, xfer_remain_ms);
56
0
  return remain_ms;
57
0
}
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
0
{
66
0
  struct connectdata *conn = data->conn;
67
0
  curl_socket_t sock = conn->sock[FIRSTSOCKET];
68
0
  int rc;
69
0
  timediff_t interval_ms;
70
0
  timediff_t timeout_ms = Curl_pp_state_timeleft_ms(data, pp);
71
0
  CURLcode result = CURLE_OK;
72
73
0
  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
0
  if(block) {
79
0
    interval_ms = 1000;  /* use 1 second timeout intervals */
80
0
    if(timeout_ms < interval_ms)
81
0
      interval_ms = timeout_ms;
82
0
  }
83
0
  else
84
0
    interval_ms = 0; /* immediate */
85
86
0
  if(Curl_conn_data_pending(data, FIRSTSOCKET))
87
0
    rc = 1;
88
0
  else if(pp->overflow)
89
    /* We are receiving and there is data in the cache so read it */
90
0
    rc = 1;
91
0
  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
0
  else {
95
0
    rc = Curl_socket_check(pp->sendleft ? CURL_SOCKET_BAD : sock, /* reading */
96
0
                           CURL_SOCKET_BAD,
97
0
                           pp->sendleft ? sock : CURL_SOCKET_BAD, /* writing */
98
0
                           interval_ms);
99
0
  }
100
101
0
  if(block) {
102
    /* if we did not wait, we do not have to spend time on this now */
103
0
    result = Curl_pgrsCheck(data);
104
0
    if(result)
105
0
      return result;
106
0
  }
107
108
0
  if(rc == -1) {
109
0
    failf(data, "select/poll error");
110
0
    result = CURLE_OUT_OF_MEMORY;
111
0
  }
112
0
  else if(rc)
113
0
    result = pp->statemachine(data, data->conn);
114
0
  else if(disconnecting)
115
0
    return CURLE_OPERATION_TIMEDOUT;
116
117
0
  return result;
118
0
}
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
0
{
123
0
  DEBUGASSERT(!pp->initialised);
124
0
  pp->nread_resp = 0;
125
0
  pp->response = *pnow; /* start response time-out */
126
0
  pp->pending_resp = TRUE;
127
0
  curlx_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
128
0
  curlx_dyn_init(&pp->recvbuf, DYN_PINGPPONG_CMD);
129
0
  pp->initialised = TRUE;
130
0
}
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
0
{
147
0
  size_t bytes_written = 0;
148
0
  size_t write_len;
149
0
  char *s;
150
0
  CURLcode result;
151
0
  struct connectdata *conn = data->conn;
152
153
0
  DEBUGASSERT(pp->sendleft == 0);
154
0
  DEBUGASSERT(pp->sendsize == 0);
155
0
  DEBUGASSERT(pp->sendthis == NULL);
156
157
0
  if(!conn)
158
    /* cannot send without a connection! */
159
0
    return CURLE_SEND_ERROR;
160
161
0
  curlx_dyn_reset(&pp->sendbuf);
162
0
  result = curlx_dyn_vaddf(&pp->sendbuf, fmt, args);
163
0
  if(result)
164
0
    return result;
165
166
  /* append CRLF */
167
0
  result = curlx_dyn_addn(&pp->sendbuf, "\r\n", 2);
168
0
  if(result)
169
0
    return result;
170
171
0
  pp->pending_resp = TRUE;
172
0
  write_len = curlx_dyn_len(&pp->sendbuf);
173
0
  s = curlx_dyn_ptr(&pp->sendbuf);
174
175
0
  result = Curl_conn_send(data, FIRSTSOCKET, s, write_len, FALSE,
176
0
                          &bytes_written);
177
0
  if(result == CURLE_AGAIN) {
178
0
    bytes_written = 0;
179
0
  }
180
0
  else if(result)
181
0
    return result;
182
183
0
  Curl_debug(data, CURLINFO_HEADER_OUT, s, bytes_written);
184
185
0
  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
0
  else {
192
0
    pp->sendthis = NULL;
193
0
    pp->sendleft = pp->sendsize = 0;
194
0
    pp->response = *Curl_pgrs_now(data);
195
0
  }
196
197
0
  return CURLE_OK;
198
0
}
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
0
{
213
0
  CURLcode result;
214
0
  va_list ap;
215
0
  va_start(ap, fmt);
216
217
0
  result = Curl_pp_vsendf(data, pp, fmt, ap);
218
219
0
  va_end(ap);
220
221
0
  return result;
222
0
}
223
224
static CURLcode pingpong_read(struct Curl_easy *data,
225
                              int sockindex,
226
                              char *buffer,
227
                              size_t buflen,
228
                              size_t *nread)
229
0
{
230
0
  return Curl_conn_recv(data, sockindex, buffer, buflen, nread);
231
0
}
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
                          int sockindex,
240
                          struct pingpong *pp,
241
                          int *code, /* return the server code if done */
242
                          size_t *size) /* size of the response */
243
0
{
244
0
  struct connectdata *conn = data->conn;
245
0
  CURLcode result = CURLE_OK;
246
0
  size_t gotbytes;
247
0
  char buffer[900];
248
249
0
  *code = 0; /* 0 for errors or not done */
250
0
  *size = 0;
251
252
0
  do {
253
0
    gotbytes = 0;
254
0
    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
0
      size_t full = curlx_dyn_len(&pp->recvbuf);
258
259
      /* trim off the "final" leading part */
260
0
      curlx_dyn_tail(&pp->recvbuf, full - pp->nfinal);
261
262
0
      pp->nfinal = 0; /* now gone */
263
0
    }
264
0
    if(!pp->overflow) {
265
0
      result = pingpong_read(data, sockindex, buffer, sizeof(buffer),
266
0
                             &gotbytes);
267
0
      if(result == CURLE_AGAIN)
268
0
        return CURLE_OK;
269
270
0
      if(result)
271
0
        return result;
272
273
0
      if(!gotbytes) {
274
0
        failf(data, "response reading failed (errno: %d)", SOCKERRNO);
275
0
        return CURLE_RECV_ERROR;
276
0
      }
277
278
0
      result = curlx_dyn_addn(&pp->recvbuf, buffer, gotbytes);
279
0
      if(result)
280
0
        return result;
281
282
0
      data->req.headerbytecount += (unsigned int)gotbytes;
283
284
0
      pp->nread_resp += gotbytes;
285
0
    }
286
287
0
    do {
288
0
      const char *line = curlx_dyn_ptr(&pp->recvbuf);
289
0
      const char *nl = memchr(line, '\n', curlx_dyn_len(&pp->recvbuf));
290
0
      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
0
        size_t length = nl - line + 1;
294
295
        /* output debug output if that is requested */
296
0
        Curl_debug(data, CURLINFO_HEADER_IN, line, length);
297
298
        /*
299
         * Pass all response-lines to the callback function registered for
300
         * "headers". The response lines can be seen as a kind of headers.
301
         */
302
0
        result = Curl_client_write(data, CLIENTWRITE_INFO, line, length);
303
0
        if(result)
304
0
          return result;
305
306
0
        if(pp->endofresp(data, conn, line, length, code)) {
307
          /* When at "end of response", keep the endofresp line first in the
308
             buffer since it will be accessed outside (by pingpong
309
             parsers). Store the overflow counter to inform about additional
310
             data in this buffer after the endofresp line. */
311
0
          pp->nfinal = length;
312
0
          if(curlx_dyn_len(&pp->recvbuf) > length)
313
0
            pp->overflow = curlx_dyn_len(&pp->recvbuf) - length;
314
0
          else
315
0
            pp->overflow = 0;
316
0
          *size = pp->nread_resp; /* size of the response */
317
0
          pp->nread_resp = 0; /* restart */
318
0
          gotbytes = 0; /* force break out of outer loop */
319
0
          break;
320
0
        }
321
0
        if(curlx_dyn_len(&pp->recvbuf) > length)
322
          /* keep the remaining piece */
323
0
          curlx_dyn_tail((&pp->recvbuf), curlx_dyn_len(&pp->recvbuf) - length);
324
0
        else
325
0
          curlx_dyn_reset(&pp->recvbuf);
326
0
      }
327
0
      else {
328
        /* without a newline, there is no overflow */
329
0
        pp->overflow = 0;
330
0
        break;
331
0
      }
332
333
0
    } while(1); /* while there is buffer left to scan */
334
335
0
  } while(gotbytes == sizeof(buffer));
336
337
0
  pp->pending_resp = FALSE;
338
339
0
  return result;
340
0
}
341
342
CURLcode Curl_pp_pollset(struct Curl_easy *data,
343
                         struct pingpong *pp,
344
                         struct easy_pollset *ps)
345
0
{
346
0
  int flags = pp->sendleft ? CURL_POLL_OUT : CURL_POLL_IN;
347
0
  return Curl_pollset_change(data, ps, data->conn->sock[FIRSTSOCKET],
348
0
                             flags, 0);
349
0
}
350
351
bool Curl_pp_needs_flush(struct Curl_easy *data,
352
                         struct pingpong *pp)
353
0
{
354
0
  (void)data;
355
0
  return pp->sendleft > 0;
356
0
}
357
358
CURLcode Curl_pp_flushsend(struct Curl_easy *data,
359
                           struct pingpong *pp)
360
0
{
361
  /* we have a piece of a command still left to send */
362
0
  size_t written;
363
0
  CURLcode result;
364
365
0
  if(!Curl_pp_needs_flush(data, pp))
366
0
    return CURLE_OK;
367
368
0
  result = Curl_conn_send(data, FIRSTSOCKET,
369
0
                          pp->sendthis + pp->sendsize - pp->sendleft,
370
0
                          pp->sendleft, FALSE, &written);
371
0
  if(result == CURLE_AGAIN) {
372
0
    result = CURLE_OK;
373
0
    written = 0;
374
0
  }
375
0
  if(result)
376
0
    return result;
377
378
0
  if(written != pp->sendleft) {
379
    /* only a fraction was sent */
380
0
    pp->sendleft -= written;
381
0
  }
382
0
  else {
383
0
    pp->sendthis = NULL;
384
0
    pp->sendleft = pp->sendsize = 0;
385
0
    pp->response = *Curl_pgrs_now(data);
386
0
  }
387
0
  return CURLE_OK;
388
0
}
389
390
CURLcode Curl_pp_disconnect(struct pingpong *pp)
391
0
{
392
0
  if(pp->initialised) {
393
0
    curlx_dyn_free(&pp->sendbuf);
394
0
    curlx_dyn_free(&pp->recvbuf);
395
0
    memset(pp, 0, sizeof(*pp));
396
0
  }
397
0
  return CURLE_OK;
398
0
}
399
400
bool Curl_pp_moredata(struct pingpong *pp)
401
0
{
402
0
  return !pp->sendleft && curlx_dyn_len(&pp->recvbuf) > pp->nfinal;
403
0
}
404
405
#endif