PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/curl-7.28.0/lib/pingpong.c

https://bitbucket.org/vlaznev/curl
C | 535 lines | 337 code | 82 blank | 116 comment | 80 complexity | 6c7856b640cb52f8712b3fa70ae40158 MD5 | raw file
  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. * pp->cache_size is cast to ssize_t here. This should be safe, because
  266. * it would have been populated with something of size int to begin
  267. * with, even though its datatype may be larger than an int.
  268. */
  269. DEBUGASSERT((ptr+pp->cache_size) <= (buf+BUFSIZE+1));
  270. memcpy(ptr, pp->cache, pp->cache_size);
  271. gotbytes = (ssize_t)pp->cache_size;
  272. free(pp->cache); /* free the cache */
  273. pp->cache = NULL; /* clear the pointer */
  274. pp->cache_size = 0; /* zero the size just in case */
  275. }
  276. else {
  277. int res;
  278. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  279. enum protection_level prot = conn->data_prot;
  280. conn->data_prot = PROT_CLEAR;
  281. #endif
  282. DEBUGASSERT((ptr+BUFSIZE-pp->nread_resp) <= (buf+BUFSIZE+1));
  283. res = Curl_read(conn, sockfd, ptr, BUFSIZE-pp->nread_resp,
  284. &gotbytes);
  285. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  286. DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
  287. conn->data_prot = prot;
  288. #endif
  289. if(res == CURLE_AGAIN)
  290. return CURLE_OK; /* return */
  291. if((res == CURLE_OK) && (gotbytes > 0))
  292. /* convert from the network encoding */
  293. res = Curl_convert_from_network(data, ptr, gotbytes);
  294. /* Curl_convert_from_network calls failf if unsuccessful */
  295. if(CURLE_OK != res) {
  296. result = (CURLcode)res; /* Set outer result variable to this error. */
  297. keepon = FALSE;
  298. }
  299. }
  300. if(!keepon)
  301. ;
  302. else if(gotbytes <= 0) {
  303. keepon = FALSE;
  304. result = CURLE_RECV_ERROR;
  305. failf(data, "response reading failed");
  306. }
  307. else {
  308. /* we got a whole chunk of data, which can be anything from one
  309. * byte to a set of lines and possible just a piece of the last
  310. * line */
  311. ssize_t i;
  312. ssize_t clipamount = 0;
  313. bool restart = FALSE;
  314. data->req.headerbytecount += (long)gotbytes;
  315. pp->nread_resp += gotbytes;
  316. for(i = 0; i < gotbytes; ptr++, i++) {
  317. perline++;
  318. if(*ptr=='\n') {
  319. /* a newline is CRLF in pp-talk, so the CR is ignored as
  320. the line isn't really terminated until the LF comes */
  321. /* output debug output if that is requested */
  322. #if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
  323. if(!conn->sec_complete)
  324. #endif
  325. if(data->set.verbose)
  326. Curl_debug(data, CURLINFO_HEADER_IN,
  327. pp->linestart_resp, (size_t)perline, conn);
  328. /*
  329. * We pass all response-lines to the callback function registered
  330. * for "headers". The response lines can be seen as a kind of
  331. * headers.
  332. */
  333. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  334. pp->linestart_resp, perline);
  335. if(result)
  336. return result;
  337. if(pp->endofresp(pp, code)) {
  338. /* This is the end of the last line, copy the last line to the
  339. start of the buffer and zero terminate, for old times sake (and
  340. krb4)! */
  341. char *meow;
  342. int n;
  343. for(meow=pp->linestart_resp, n=0; meow<ptr; meow++, n++)
  344. buf[n] = *meow;
  345. *meow=0; /* zero terminate */
  346. keepon=FALSE;
  347. pp->linestart_resp = ptr+1; /* advance pointer */
  348. i++; /* skip this before getting out */
  349. *size = pp->nread_resp; /* size of the response */
  350. pp->nread_resp = 0; /* restart */
  351. break;
  352. }
  353. perline=0; /* line starts over here */
  354. pp->linestart_resp = ptr+1;
  355. }
  356. }
  357. if(!keepon && (i != gotbytes)) {
  358. /* We found the end of the response lines, but we didn't parse the
  359. full chunk of data we have read from the server. We therefore need
  360. to store the rest of the data to be checked on the next invoke as
  361. it may actually contain another end of response already! */
  362. clipamount = gotbytes - i;
  363. restart = TRUE;
  364. }
  365. else if(keepon) {
  366. if((perline == gotbytes) && (gotbytes > BUFSIZE/2)) {
  367. /* We got an excessive line without newlines and we need to deal
  368. with it. We keep the first bytes of the line then we throw
  369. away the rest. */
  370. infof(data, "Excessive server response line length received, "
  371. "%zd bytes. Stripping\n", gotbytes);
  372. restart = TRUE;
  373. /* we keep 40 bytes since all our pingpong protocols are only
  374. interested in the first piece */
  375. clipamount = 40;
  376. }
  377. else if(pp->nread_resp > BUFSIZE/2) {
  378. /* We got a large chunk of data and there's potentially still
  379. trailing data to take care of, so we put any such part in the
  380. "cache", clear the buffer to make space and restart. */
  381. clipamount = perline;
  382. restart = TRUE;
  383. }
  384. }
  385. else if(i == gotbytes)
  386. restart = TRUE;
  387. if(clipamount) {
  388. pp->cache_size = clipamount;
  389. pp->cache = malloc(pp->cache_size);
  390. if(pp->cache)
  391. memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
  392. else
  393. return CURLE_OUT_OF_MEMORY;
  394. }
  395. if(restart) {
  396. /* now reset a few variables to start over nicely from the start of
  397. the big buffer */
  398. pp->nread_resp = 0; /* start over from scratch in the buffer */
  399. ptr = pp->linestart_resp = buf;
  400. perline = 0;
  401. }
  402. } /* there was data */
  403. } /* while there's buffer left and loop is requested */
  404. pp->pending_resp = FALSE;
  405. return result;
  406. }
  407. int Curl_pp_getsock(struct pingpong *pp,
  408. curl_socket_t *socks,
  409. int numsocks)
  410. {
  411. struct connectdata *conn = pp->conn;
  412. if(!numsocks)
  413. return GETSOCK_BLANK;
  414. socks[0] = conn->sock[FIRSTSOCKET];
  415. if(pp->sendleft) {
  416. /* write mode */
  417. return GETSOCK_WRITESOCK(0);
  418. }
  419. /* read mode */
  420. return GETSOCK_READSOCK(0);
  421. }
  422. CURLcode Curl_pp_flushsend(struct pingpong *pp)
  423. {
  424. /* we have a piece of a command still left to send */
  425. struct connectdata *conn = pp->conn;
  426. ssize_t written;
  427. CURLcode result = CURLE_OK;
  428. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  429. result = Curl_write(conn, sock, pp->sendthis + pp->sendsize -
  430. pp->sendleft, pp->sendleft, &written);
  431. if(result)
  432. return result;
  433. if(written != (ssize_t)pp->sendleft) {
  434. /* only a fraction was sent */
  435. pp->sendleft -= written;
  436. }
  437. else {
  438. free(pp->sendthis);
  439. pp->sendthis=NULL;
  440. pp->sendleft = pp->sendsize = 0;
  441. pp->response = Curl_tvnow();
  442. }
  443. return CURLE_OK;
  444. }
  445. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  446. {
  447. if(pp->cache) {
  448. free(pp->cache);
  449. pp->cache = NULL;
  450. }
  451. return CURLE_OK;
  452. }
  453. #endif