pingpong.c 16 KB

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