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