pingpong.c 15 KB

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