pingpong.c 15 KB

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