2
0

pingpong.c 15 KB

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