Debian Codegraph @

extra/curl/curl-8.14.1/lib/pingpong.c

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