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