Coverage Report

Created: 2025-12-14 06:23

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