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