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