1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * 'pingpong' is for generic back-and-forth support functions used by FTP,
22 * IMAP, POP3, SMTP and whatever more that likes them.
24 ***************************************************************************/
32 #include "speedcheck.h"
36 #define _MPRINTF_REPLACE /* use our functions only */
37 #include <curl/mprintf.h>
39 #include "curl_memory.h"
40 /* The last #include file should be: */
45 /* Returns timeout in ms. 0 or negative number means the timeout has already
47 long Curl_pp_state_timeout(struct pingpong *pp)
49 struct connectdata *conn = pp->conn;
50 struct SessionHandle *data=conn->data;
51 long timeout_ms; /* in milliseconds */
52 long timeout2_ms; /* in milliseconds */
53 long response_time= (data->set.server_response_timeout)?
54 data->set.server_response_timeout: pp->response_time;
56 /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
57 remaining time, or use pp->response because SERVER_RESPONSE_TIMEOUT is
58 supposed to govern the response for any given server response, not for
59 the time from connect to the given server response. */
61 /* Without a requested timeout, we only wait 'response_time' seconds for the
62 full response to arrive before we bail out */
63 timeout_ms = response_time -
64 Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */
66 if(data->set.timeout) {
67 /* if timeout is requested, find out how much remaining time we have */
68 timeout2_ms = data->set.timeout - /* timeout time */
69 Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */
71 /* pick the lowest number */
72 timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
80 * Curl_pp_multi_statemach()
82 * called repeatedly until done when the multi interface is used.
84 CURLcode Curl_pp_multi_statemach(struct pingpong *pp)
86 struct connectdata *conn = pp->conn;
87 curl_socket_t sock = conn->sock[FIRSTSOCKET];
89 struct SessionHandle *data=conn->data;
90 CURLcode result = CURLE_OK;
91 long timeout_ms = Curl_pp_state_timeout(pp);
94 failf(data, "server response timeout");
95 return CURLE_OPERATION_TIMEDOUT;
98 rc = Curl_socket_ready(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
99 pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
103 failf(data, "select/poll error");
104 return CURLE_OUT_OF_MEMORY;
107 result = pp->statemach_act(conn);
109 /* if rc == 0, then select() timed out */
115 * Curl_pp_easy_statemach()
117 * called repeatedly until done when the easy interface is used.
119 CURLcode Curl_pp_easy_statemach(struct pingpong *pp)
121 struct connectdata *conn = pp->conn;
122 curl_socket_t sock = conn->sock[FIRSTSOCKET];
125 long timeout_ms = Curl_pp_state_timeout(pp);
126 struct SessionHandle *data=conn->data;
129 if(timeout_ms <=0 ) {
130 failf(data, "server response timeout");
131 return CURLE_OPERATION_TIMEDOUT; /* already too little time */
134 interval_ms = 1000; /* use 1 second timeout intervals */
135 if(timeout_ms < interval_ms)
136 interval_ms = timeout_ms;
138 rc = Curl_socket_ready(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
139 pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
142 if(Curl_pgrsUpdate(conn))
143 result = CURLE_ABORTED_BY_CALLBACK;
145 result = Curl_speedcheck(data, Curl_tvnow());
150 failf(data, "select/poll error");
151 result = CURLE_OUT_OF_MEMORY;
154 result = pp->statemach_act(conn);
159 /* initialize stuff to prepare for reading a fresh new response */
160 void Curl_pp_init(struct pingpong *pp)
162 struct connectdata *conn = pp->conn;
164 pp->linestart_resp = conn->data->state.buffer;
165 pp->pending_resp = TRUE;
166 pp->response = Curl_tvnow(); /* start response time-out now! */
171 /***********************************************************************
175 * Send the formated string as a command to a pingpong server. Note that
176 * the string should not have any CRLF appended, as this function will
177 * append the necessary things itself.
179 * NOTE: we build the command in a fixed-length buffer, which sets length
180 * restrictions on the command!
182 * made to never block
184 CURLcode Curl_pp_vsendf(struct pingpong *pp,
188 ssize_t bytes_written;
189 /* may still not be big enough for some krb5 tokens */
190 #define SBUF_SIZE 1024
194 CURLcode res = CURLE_OK;
195 struct connectdata *conn = pp->conn;
196 struct SessionHandle *data = conn->data;
198 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
199 enum protection_level data_sec = conn->data_prot;
202 vsnprintf(s, SBUF_SIZE-3, fmt, args);
204 strcat(s, "\r\n"); /* append a trailing CRLF */
207 write_len = strlen(s);
211 #ifdef CURL_DOES_CONVERSIONS
212 res = Curl_convert_to_network(data, s, write_len);
213 /* Curl_convert_to_network calls failf if unsuccessful */
214 if(res != CURLE_OK) {
217 #endif /* CURL_DOES_CONVERSIONS */
219 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
220 conn->data_prot = prot_cmd;
222 res = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
224 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
225 conn->data_prot = data_sec;
231 if(conn->data->set.verbose)
232 Curl_debug(conn->data, CURLINFO_HEADER_OUT,
233 sptr, (size_t)bytes_written, conn);
235 if(bytes_written != (ssize_t)write_len) {
236 /* the whole chunk was not sent, store the rest of the data */
237 write_len -= bytes_written;
238 sptr += bytes_written;
239 pp->sendthis = malloc(write_len);
241 memcpy(pp->sendthis, sptr, write_len);
242 pp->sendsize = pp->sendleft = write_len;
245 failf(data, "out of memory");
246 res = CURLE_OUT_OF_MEMORY;
250 pp->response = Curl_tvnow();
256 /***********************************************************************
260 * Send the formated string as a command to a pingpong server. Note that
261 * the string should not have any CRLF appended, as this function will
262 * append the necessary things itself.
264 * NOTE: we build the command in a fixed-length buffer, which sets length
265 * restrictions on the command!
267 * made to never block
269 CURLcode Curl_pp_sendf(struct pingpong *pp,
270 const char *fmt, ...)
276 res = Curl_pp_vsendf(pp, fmt, ap);
286 * Reads a piece of a server response.
288 CURLcode Curl_pp_readresp(curl_socket_t sockfd,
290 int *code, /* return the server code if done */
291 size_t *size) /* size of the response */
293 ssize_t perline; /* count bytes per line */
297 struct connectdata *conn = pp->conn;
298 struct SessionHandle *data = conn->data;
299 char * const buf = data->state.buffer;
300 CURLcode result = CURLE_OK;
302 *code = 0; /* 0 for errors or not done */
305 ptr=buf + pp->nread_resp;
307 /* number of bytes in the current line, so far */
308 perline = (ssize_t)(ptr-pp->linestart_resp);
312 while((pp->nread_resp<BUFSIZE) && (keepon && !result)) {
315 /* we had data in the "cache", copy that instead of doing an actual
318 * ftp->cache_size is cast to int here. This should be safe,
319 * because it would have been populated with something of size
320 * int to begin with, even though its datatype may be larger
323 DEBUGASSERT((ptr+pp->cache_size) <= (buf+BUFSIZE+1));
324 memcpy(ptr, pp->cache, pp->cache_size);
325 gotbytes = (ssize_t)pp->cache_size;
326 free(pp->cache); /* free the cache */
327 pp->cache = NULL; /* clear the pointer */
328 pp->cache_size = 0; /* zero the size just in case */
332 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
333 enum protection_level prot = conn->data_prot;
337 DEBUGASSERT((ptr+BUFSIZE-pp->nread_resp) <= (buf+BUFSIZE+1));
338 res = Curl_read(conn, sockfd, ptr, BUFSIZE-pp->nread_resp,
340 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
341 conn->data_prot = prot;
343 if(res == CURLE_AGAIN)
344 return CURLE_OK; /* return */
346 #ifdef CURL_DOES_CONVERSIONS
347 if((res == CURLE_OK) && (gotbytes > 0)) {
348 /* convert from the network encoding */
349 res = Curl_convert_from_network(data, ptr, gotbytes);
350 /* Curl_convert_from_network calls failf if unsuccessful */
352 #endif /* CURL_DOES_CONVERSIONS */
354 if(CURLE_OK != res) {
355 result = (CURLcode)res; /* Set outer result variable to this error. */
362 else if(gotbytes <= 0) {
364 result = CURLE_RECV_ERROR;
365 failf(data, "FTP response reading failed");
368 /* we got a whole chunk of data, which can be anything from one
369 * byte to a set of lines and possible just a piece of the last
372 ssize_t clipamount = 0;
373 bool restart = FALSE;
375 data->req.headerbytecount += (long)gotbytes;
377 pp->nread_resp += gotbytes;
378 for(i = 0; i < gotbytes; ptr++, i++) {
381 /* a newline is CRLF in ftp-talk, so the CR is ignored as
382 the line isn't really terminated until the LF comes */
384 /* output debug output if that is requested */
385 #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
386 if(!conn->sec_complete)
388 if(data->set.verbose)
389 Curl_debug(data, CURLINFO_HEADER_IN,
390 pp->linestart_resp, (size_t)perline, conn);
393 * We pass all response-lines to the callback function registered
394 * for "headers". The response lines can be seen as a kind of
397 result = Curl_client_write(conn, CLIENTWRITE_HEADER,
398 pp->linestart_resp, perline);
402 if(pp->endofresp(pp, code)) {
403 /* This is the end of the last line, copy the last line to the
404 start of the buffer and zero terminate, for old times sake (and
408 for(meow=pp->linestart_resp, n=0; meow<ptr; meow++, n++)
410 *meow=0; /* zero terminate */
412 pp->linestart_resp = ptr+1; /* advance pointer */
413 i++; /* skip this before getting out */
415 *size = pp->nread_resp; /* size of the response */
416 pp->nread_resp = 0; /* restart */
419 perline=0; /* line starts over here */
420 pp->linestart_resp = ptr+1;
424 if(!keepon && (i != gotbytes)) {
425 /* We found the end of the response lines, but we didn't parse the
426 full chunk of data we have read from the server. We therefore need
427 to store the rest of the data to be checked on the next invoke as
428 it may actually contain another end of response already! */
429 clipamount = gotbytes - i;
434 if((perline == gotbytes) && (gotbytes > BUFSIZE/2)) {
435 /* We got an excessive line without newlines and we need to deal
436 with it. We keep the first bytes of the line then we throw
438 infof(data, "Excessive server response line length received, %zd bytes."
439 " Stripping\n", gotbytes);
442 /* we keep 40 bytes since all our pingpong protocols are only
443 interested in the first piece */
446 else if(pp->nread_resp > BUFSIZE/2) {
447 /* We got a large chunk of data and there's potentially still trailing
448 data to take care of, so we put any such part in the "cache", clear
449 the buffer to make space and restart. */
450 clipamount = perline;
454 else if(i == gotbytes)
458 pp->cache_size = clipamount;
459 pp->cache = malloc(pp->cache_size);
461 memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
463 return CURLE_OUT_OF_MEMORY;
466 /* now reset a few variables to start over nicely from the start of
468 pp->nread_resp = 0; /* start over from scratch in the buffer */
469 ptr = pp->linestart_resp = buf;
473 } /* there was data */
475 } /* while there's buffer left and loop is requested */
477 pp->pending_resp = FALSE;
482 int Curl_pp_getsock(struct pingpong *pp,
483 curl_socket_t *socks,
486 struct connectdata *conn = pp->conn;
489 return GETSOCK_BLANK;
491 socks[0] = conn->sock[FIRSTSOCKET];
495 return GETSOCK_WRITESOCK(0);
499 return GETSOCK_READSOCK(0);
502 CURLcode Curl_pp_flushsend(struct pingpong *pp)
504 /* we have a piece of a command still left to send */
505 struct connectdata *conn = pp->conn;
507 CURLcode result = CURLE_OK;
508 curl_socket_t sock = conn->sock[FIRSTSOCKET];
510 result = Curl_write(conn, sock, pp->sendthis + pp->sendsize -
511 pp->sendleft, pp->sendleft, &written);
515 if(written != (ssize_t)pp->sendleft) {
516 /* only a fraction was sent */
517 pp->sendleft -= written;
522 pp->sendleft = pp->sendsize = 0;
523 pp->response = Curl_tvnow();
528 CURLcode Curl_pp_disconnect(struct pingpong *pp)