pingpong.c 15 KB

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