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