pingpong.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2009, 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 http://curl.haxx.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. * 'pingpong' is for generic back-and-forth support functions used by FTP,
  22. * IMAP, POP3, SMTP and whatever more that likes them.
  23. *
  24. * $Id$
  25. ***************************************************************************/
  26. #include "setup.h"
  27. #include "urldata.h"
  28. #include "sendf.h"
  29. #include "select.h"
  30. #include "progress.h"
  31. #include "speedcheck.h"
  32. #include "pingpong.h"
  33. #include "multiif.h"
  34. #define _MPRINTF_REPLACE /* use our functions only */
  35. #include <curl/mprintf.h>
  36. #include "curl_memory.h"
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. #ifdef USE_PINGPONG
  40. /* Returns timeout in ms. 0 or negative number means the timeout has already
  41. triggered */
  42. long Curl_pp_state_timeout(struct pingpong *pp)
  43. {
  44. struct connectdata *conn = pp->conn;
  45. struct SessionHandle *data=conn->data;
  46. long timeout_ms=360000; /* in milliseconds */
  47. if(data->set.server_response_timeout )
  48. /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
  49. remaining time. Also, use pp->response because SERVER_RESPONSE_TIMEOUT
  50. is supposed to govern the response for any given server response, not
  51. for the time from connect to the given server response. */
  52. timeout_ms = data->set.server_response_timeout - /* timeout time */
  53. Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */
  54. else if(data->set.timeout)
  55. /* if timeout is requested, find out how much remaining time we have */
  56. timeout_ms = data->set.timeout - /* timeout time */
  57. Curl_tvdiff(Curl_tvnow(), conn->now); /* spent time */
  58. else
  59. /* Without a requested timeout, we only wait 'response_time' seconds for
  60. the full response to arrive before we bail out */
  61. timeout_ms = pp->response_time -
  62. Curl_tvdiff(Curl_tvnow(), pp->response); /* spent time */
  63. return timeout_ms;
  64. }
  65. /*
  66. * Curl_pp_multi_statemach()
  67. *
  68. * called repeatedly until done when the multi interface is used.
  69. */
  70. CURLcode Curl_pp_multi_statemach(struct pingpong *pp)
  71. {
  72. struct connectdata *conn = pp->conn;
  73. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  74. int rc;
  75. struct SessionHandle *data=conn->data;
  76. CURLcode result = CURLE_OK;
  77. long timeout_ms = Curl_pp_state_timeout(pp);
  78. if(timeout_ms <= 0) {
  79. failf(data, "server response timeout");
  80. return CURLE_OPERATION_TIMEDOUT;
  81. }
  82. rc = Curl_socket_ready(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
  83. pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
  84. 0);
  85. if(rc == -1) {
  86. failf(data, "select/poll error");
  87. return CURLE_OUT_OF_MEMORY;
  88. }
  89. else if(rc != 0)
  90. result = pp->statemach_act(conn);
  91. /* if rc == 0, then select() timed out */
  92. return result;
  93. }
  94. /*
  95. * Curl_pp_easy_statemach()
  96. *
  97. * called repeatedly until done when the easy interface is used.
  98. */
  99. CURLcode Curl_pp_easy_statemach(struct pingpong *pp)
  100. {
  101. struct connectdata *conn = pp->conn;
  102. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  103. int rc;
  104. long interval_ms;
  105. long timeout_ms = Curl_pp_state_timeout(pp);
  106. struct SessionHandle *data=conn->data;
  107. CURLcode result;
  108. if(timeout_ms <=0 ) {
  109. failf(data, "server response timeout");
  110. return CURLE_OPERATION_TIMEDOUT; /* already too little time */
  111. }
  112. interval_ms = 1000; /* use 1 second timeout intervals */
  113. if(timeout_ms < interval_ms)
  114. interval_ms = timeout_ms;
  115. rc = Curl_socket_ready(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
  116. pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
  117. (int)interval_ms);
  118. if(Curl_pgrsUpdate(conn))
  119. result = CURLE_ABORTED_BY_CALLBACK;
  120. else
  121. result = Curl_speedcheck(data, Curl_tvnow());
  122. if(result)
  123. ;
  124. else if(rc == -1) {
  125. failf(data, "select/poll error");
  126. result = CURLE_OUT_OF_MEMORY;
  127. }
  128. else if(rc)
  129. result = pp->statemach_act(conn);
  130. return result;
  131. }
  132. /* initialize stuff to prepare for reading a fresh new response */
  133. void Curl_pp_init(struct pingpong *pp)
  134. {
  135. struct connectdata *conn = pp->conn;
  136. pp->nread_resp = 0;
  137. pp->linestart_resp = conn->data->state.buffer;
  138. pp->pending_resp = TRUE;
  139. pp->response = Curl_tvnow(); /* start response time-out now! */
  140. }
  141. /***********************************************************************
  142. *
  143. * Curl_pp_sendfv()
  144. *
  145. * Send the formated string as a command to a pingpong server. Note that
  146. * the string should not have any CRLF appended, as this function will
  147. * append the necessary things itself.
  148. *
  149. * NOTE: we build the command in a fixed-length buffer, which sets length
  150. * restrictions on the command!
  151. *
  152. * made to never block
  153. */
  154. CURLcode Curl_pp_vsendf(struct pingpong *pp,
  155. const char *fmt,
  156. va_list args)
  157. {
  158. ssize_t bytes_written;
  159. /* may still not be big enough for some krb5 tokens */
  160. #define SBUF_SIZE 1024
  161. char s[SBUF_SIZE];
  162. size_t write_len;
  163. char *sptr=s;
  164. CURLcode res = CURLE_OK;
  165. struct connectdata *conn = pp->conn;
  166. struct SessionHandle *data = conn->data;
  167. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  168. enum protection_level data_sec = conn->data_prot;
  169. #endif
  170. vsnprintf(s, SBUF_SIZE-3, fmt, args);
  171. strcat(s, "\r\n"); /* append a trailing CRLF */
  172. bytes_written=0;
  173. write_len = strlen(s);
  174. Curl_pp_init(pp);
  175. #ifdef CURL_DOES_CONVERSIONS
  176. res = Curl_convert_to_network(data, s, write_len);
  177. /* Curl_convert_to_network calls failf if unsuccessful */
  178. if(res != CURLE_OK) {
  179. return res;
  180. }
  181. #endif /* CURL_DOES_CONVERSIONS */
  182. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  183. conn->data_prot = prot_cmd;
  184. #endif
  185. res = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
  186. &bytes_written);
  187. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  188. conn->data_prot = data_sec;
  189. #endif
  190. if(CURLE_OK != res)
  191. return res;
  192. if(conn->data->set.verbose)
  193. Curl_debug(conn->data, CURLINFO_HEADER_OUT,
  194. sptr, (size_t)bytes_written, conn);
  195. if(bytes_written != (ssize_t)write_len) {
  196. /* the whole chunk was not sent, store the rest of the data */
  197. write_len -= bytes_written;
  198. sptr += bytes_written;
  199. pp->sendthis = malloc(write_len);
  200. if(pp->sendthis) {
  201. memcpy(pp->sendthis, sptr, write_len);
  202. pp->sendsize = pp->sendleft = write_len;
  203. }
  204. else {
  205. failf(data, "out of memory");
  206. res = CURLE_OUT_OF_MEMORY;
  207. }
  208. }
  209. else
  210. pp->response = Curl_tvnow();
  211. return res;
  212. }
  213. /***********************************************************************
  214. *
  215. * Curl_pp_sendf()
  216. *
  217. * Send the formated string as a command to a pingpong server. Note that
  218. * the string should not have any CRLF appended, as this function will
  219. * append the necessary things itself.
  220. *
  221. * NOTE: we build the command in a fixed-length buffer, which sets length
  222. * restrictions on the command!
  223. *
  224. * made to never block
  225. */
  226. CURLcode Curl_pp_sendf(struct pingpong *pp,
  227. const char *fmt, ...)
  228. {
  229. CURLcode res;
  230. va_list ap;
  231. va_start(ap, fmt);
  232. res = Curl_pp_vsendf(pp, fmt, ap);
  233. va_end(ap);
  234. return res;
  235. }
  236. /*
  237. * Curl_pp_readresp()
  238. *
  239. * Reads a piece of a server response.
  240. */
  241. CURLcode Curl_pp_readresp(curl_socket_t sockfd,
  242. struct pingpong *pp,
  243. int *code, /* return the server code if done */
  244. size_t *size) /* size of the response */
  245. {
  246. ssize_t perline; /* count bytes per line */
  247. bool keepon=TRUE;
  248. ssize_t gotbytes;
  249. char *ptr;
  250. struct connectdata *conn = pp->conn;
  251. struct SessionHandle *data = conn->data;
  252. char * const buf = data->state.buffer;
  253. CURLcode result = CURLE_OK;
  254. *code = 0; /* 0 for errors or not done */
  255. *size = 0;
  256. ptr=buf + pp->nread_resp;
  257. /* number of bytes in the current line, so far */
  258. perline = (ssize_t)(ptr-pp->linestart_resp);
  259. keepon=TRUE;
  260. while((pp->nread_resp<BUFSIZE) && (keepon && !result)) {
  261. if(pp->cache) {
  262. /* we had data in the "cache", copy that instead of doing an actual
  263. * read
  264. *
  265. * ftp->cache_size is cast to int here. This should be safe,
  266. * because it would have been populated with something of size
  267. * int to begin with, even though its datatype may be larger
  268. * than an int.
  269. */
  270. DEBUGASSERT((ptr+pp->cache_size) <= (buf+BUFSIZE+1));
  271. memcpy(ptr, pp->cache, pp->cache_size);
  272. gotbytes = pp->cache_size;
  273. free(pp->cache); /* free the cache */
  274. pp->cache = NULL; /* clear the pointer */
  275. pp->cache_size = 0; /* zero the size just in case */
  276. }
  277. else {
  278. int res;
  279. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  280. enum protection_level prot = conn->data_prot;
  281. conn->data_prot = 0;
  282. #endif
  283. DEBUGASSERT((ptr+BUFSIZE-pp->nread_resp) <= (buf+BUFSIZE+1));
  284. res = Curl_read(conn, sockfd, ptr, BUFSIZE-pp->nread_resp,
  285. &gotbytes);
  286. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  287. conn->data_prot = prot;
  288. #endif
  289. if(res < 0)
  290. /* EWOULDBLOCK */
  291. return CURLE_OK; /* return */
  292. #ifdef CURL_DOES_CONVERSIONS
  293. if((res == CURLE_OK) && (gotbytes > 0)) {
  294. /* convert from the network encoding */
  295. res = Curl_convert_from_network(data, ptr, gotbytes);
  296. /* Curl_convert_from_network calls failf if unsuccessful */
  297. }
  298. #endif /* CURL_DOES_CONVERSIONS */
  299. if(CURLE_OK != res) {
  300. result = (CURLcode)res; /* Set outer result variable to this error. */
  301. keepon = FALSE;
  302. }
  303. }
  304. if(!keepon)
  305. ;
  306. else if(gotbytes <= 0) {
  307. keepon = FALSE;
  308. result = CURLE_RECV_ERROR;
  309. failf(data, "FTP response reading failed");
  310. }
  311. else {
  312. /* we got a whole chunk of data, which can be anything from one
  313. * byte to a set of lines and possible just a piece of the last
  314. * line */
  315. ssize_t i;
  316. ssize_t clipamount = 0;
  317. bool restart = FALSE;
  318. data->req.headerbytecount += (long)gotbytes;
  319. pp->nread_resp += gotbytes;
  320. for(i = 0; i < gotbytes; ptr++, i++) {
  321. perline++;
  322. if(*ptr=='\n') {
  323. /* a newline is CRLF in ftp-talk, so the CR is ignored as
  324. the line isn't really terminated until the LF comes */
  325. /* output debug output if that is requested */
  326. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  327. if(!conn->sec_complete)
  328. #endif
  329. if(data->set.verbose)
  330. Curl_debug(data, CURLINFO_HEADER_IN,
  331. pp->linestart_resp, (size_t)perline, conn);
  332. /*
  333. * We pass all response-lines to the callback function registered
  334. * for "headers". The response lines can be seen as a kind of
  335. * headers.
  336. */
  337. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  338. pp->linestart_resp, perline);
  339. if(result)
  340. return result;
  341. if(pp->endofresp(pp, code)) {
  342. /* This is the end of the last line, copy the last line to the
  343. start of the buffer and zero terminate, for old times sake (and
  344. krb4)! */
  345. char *meow;
  346. int n;
  347. for(meow=pp->linestart_resp, n=0; meow<ptr; meow++, n++)
  348. buf[n] = *meow;
  349. *meow=0; /* zero terminate */
  350. keepon=FALSE;
  351. pp->linestart_resp = ptr+1; /* advance pointer */
  352. i++; /* skip this before getting out */
  353. *size = pp->nread_resp; /* size of the response */
  354. pp->nread_resp = 0; /* restart */
  355. break;
  356. }
  357. perline=0; /* line starts over here */
  358. pp->linestart_resp = ptr+1;
  359. }
  360. }
  361. if(!keepon && (i != gotbytes)) {
  362. /* We found the end of the response lines, but we didn't parse the
  363. full chunk of data we have read from the server. We therefore need
  364. to store the rest of the data to be checked on the next invoke as
  365. it may actually contain another end of response already! */
  366. clipamount = gotbytes - i;
  367. restart = TRUE;
  368. }
  369. else if(keepon) {
  370. if((perline == gotbytes) && (gotbytes > BUFSIZE/2)) {
  371. /* We got an excessive line without newlines and we need to deal
  372. with it. We keep the first bytes of the line then we throw
  373. away the rest. */
  374. infof(data, "Excessive server response line length received, %zd bytes."
  375. " Stripping\n", gotbytes);
  376. restart = TRUE;
  377. /* we keep 40 bytes since all our pingpong protocols are only
  378. interested in the first piece */
  379. clipamount = 40;
  380. }
  381. else if(pp->nread_resp > BUFSIZE/2) {
  382. /* We got a large chunk of data and there's potentially still trailing
  383. data to take care of, so we put any such part in the "cache", clear
  384. the buffer to make space and restart. */
  385. clipamount = perline;
  386. restart = TRUE;
  387. }
  388. }
  389. else if(i == gotbytes)
  390. restart = TRUE;
  391. if(clipamount) {
  392. pp->cache_size = clipamount;
  393. pp->cache = malloc(pp->cache_size);
  394. if(pp->cache)
  395. memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
  396. else
  397. return CURLE_OUT_OF_MEMORY;
  398. }
  399. if(restart) {
  400. /* now reset a few variables to start over nicely from the start of
  401. the big buffer */
  402. pp->nread_resp = 0; /* start over from scratch in the buffer */
  403. ptr = pp->linestart_resp = buf;
  404. perline = 0;
  405. }
  406. } /* there was data */
  407. } /* while there's buffer left and loop is requested */
  408. pp->pending_resp = FALSE;
  409. return result;
  410. }
  411. int Curl_pp_getsock(struct pingpong *pp,
  412. curl_socket_t *socks,
  413. int numsocks)
  414. {
  415. struct connectdata *conn = pp->conn;
  416. if(!numsocks)
  417. return GETSOCK_BLANK;
  418. socks[0] = conn->sock[FIRSTSOCKET];
  419. if(pp->sendleft) {
  420. /* write mode */
  421. return GETSOCK_WRITESOCK(0);
  422. }
  423. /* read mode */
  424. return GETSOCK_READSOCK(0);
  425. }
  426. CURLcode Curl_pp_flushsend(struct pingpong *pp)
  427. {
  428. /* we have a piece of a command still left to send */
  429. struct connectdata *conn = pp->conn;
  430. ssize_t written;
  431. CURLcode result = CURLE_OK;
  432. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  433. result = Curl_write(conn, sock, pp->sendthis + pp->sendsize -
  434. pp->sendleft, pp->sendleft, &written);
  435. if(result)
  436. return result;
  437. if(written != (ssize_t)pp->sendleft) {
  438. /* only a fraction was sent */
  439. pp->sendleft -= written;
  440. }
  441. else {
  442. free(pp->sendthis);
  443. pp->sendthis=NULL;
  444. pp->sendleft = pp->sendsize = 0;
  445. pp->response = Curl_tvnow();
  446. }
  447. return CURLE_OK;
  448. }
  449. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  450. {
  451. if(pp->cache) {
  452. free(pp->cache);
  453. pp->cache = NULL;
  454. }
  455. return CURLE_OK;
  456. }
  457. #endif