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