pingpong.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. * 'pingpong' is for generic back-and-forth support functions used by FTP,
  24. * IMAP, POP3, SMTP and whatever more that likes them.
  25. *
  26. ***************************************************************************/
  27. #include "curl_setup.h"
  28. #include "urldata.h"
  29. #include "cfilters.h"
  30. #include "sendf.h"
  31. #include "select.h"
  32. #include "progress.h"
  33. #include "speedcheck.h"
  34. #include "pingpong.h"
  35. #include "multiif.h"
  36. #include "vtls/vtls.h"
  37. /* The last 3 #include files should be in this order */
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. #ifdef USE_PINGPONG
  42. /* Returns timeout in ms. 0 or negative number means the timeout has already
  43. triggered */
  44. timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
  45. struct pingpong *pp, bool disconnecting)
  46. {
  47. struct connectdata *conn = data->conn;
  48. timediff_t timeout_ms; /* in milliseconds */
  49. timediff_t 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_timediff(Curl_now(), pp->response); /* spent time */
  59. if(data->set.timeout && !disconnecting) {
  60. /* if timeout is requested, find out how much remaining time we have */
  61. timediff_t timeout2_ms = data->set.timeout - /* timeout time */
  62. Curl_timediff(Curl_now(), 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 Curl_easy *data,
  72. struct pingpong *pp, bool block,
  73. bool disconnecting)
  74. {
  75. struct connectdata *conn = data->conn;
  76. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  77. int rc;
  78. timediff_t interval_ms;
  79. timediff_t timeout_ms = Curl_pp_state_timeout(data, pp, disconnecting);
  80. CURLcode result = CURLE_OK;
  81. if(timeout_ms <= 0) {
  82. failf(data, "server response timeout");
  83. return CURLE_OPERATION_TIMEDOUT; /* already too little time */
  84. }
  85. if(block) {
  86. interval_ms = 1000; /* use 1 second timeout intervals */
  87. if(timeout_ms < interval_ms)
  88. interval_ms = timeout_ms;
  89. }
  90. else
  91. interval_ms = 0; /* immediate */
  92. if(Curl_conn_data_pending(data, FIRSTSOCKET))
  93. rc = 1;
  94. else if(Curl_pp_moredata(pp))
  95. /* We are receiving and there is data in the cache so just read it */
  96. rc = 1;
  97. else if(!pp->sendleft && Curl_conn_data_pending(data, FIRSTSOCKET))
  98. /* We are receiving and there is data ready in the SSL library */
  99. rc = 1;
  100. else
  101. rc = Curl_socket_check(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
  102. CURL_SOCKET_BAD,
  103. pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
  104. interval_ms);
  105. if(block) {
  106. /* if we didn't wait, we don't have to spend time on this now */
  107. if(Curl_pgrsUpdate(data))
  108. result = CURLE_ABORTED_BY_CALLBACK;
  109. else
  110. result = Curl_speedcheck(data, Curl_now());
  111. if(result)
  112. return result;
  113. }
  114. if(rc == -1) {
  115. failf(data, "select/poll error");
  116. result = CURLE_OUT_OF_MEMORY;
  117. }
  118. else if(rc)
  119. result = pp->statemachine(data, data->conn);
  120. return result;
  121. }
  122. /* initialize stuff to prepare for reading a fresh new response */
  123. void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp)
  124. {
  125. DEBUGASSERT(data);
  126. pp->nread_resp = 0;
  127. pp->linestart_resp = data->state.buffer;
  128. pp->pending_resp = TRUE;
  129. pp->response = Curl_now(); /* start response time-out now! */
  130. }
  131. /* setup for the coming transfer */
  132. void Curl_pp_setup(struct pingpong *pp)
  133. {
  134. Curl_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
  135. }
  136. /***********************************************************************
  137. *
  138. * Curl_pp_vsendf()
  139. *
  140. * Send the formatted string as a command to a pingpong server. Note that
  141. * the string should not have any CRLF appended, as this function will
  142. * append the necessary things itself.
  143. *
  144. * made to never block
  145. */
  146. CURLcode Curl_pp_vsendf(struct Curl_easy *data,
  147. struct pingpong *pp,
  148. const char *fmt,
  149. va_list args)
  150. {
  151. ssize_t bytes_written = 0;
  152. size_t write_len;
  153. char *s;
  154. CURLcode result;
  155. struct connectdata *conn = data->conn;
  156. #ifdef HAVE_GSSAPI
  157. enum protection_level data_sec;
  158. #endif
  159. DEBUGASSERT(pp->sendleft == 0);
  160. DEBUGASSERT(pp->sendsize == 0);
  161. DEBUGASSERT(pp->sendthis == NULL);
  162. if(!conn)
  163. /* can't send without a connection! */
  164. return CURLE_SEND_ERROR;
  165. Curl_dyn_reset(&pp->sendbuf);
  166. result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
  167. if(result)
  168. return result;
  169. /* append CRLF */
  170. result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
  171. if(result)
  172. return result;
  173. write_len = Curl_dyn_len(&pp->sendbuf);
  174. s = Curl_dyn_ptr(&pp->sendbuf);
  175. Curl_pp_init(data, pp);
  176. #ifdef HAVE_GSSAPI
  177. conn->data_prot = PROT_CMD;
  178. #endif
  179. result = Curl_nwrite(data, FIRSTSOCKET, s, write_len, &bytes_written);
  180. if(result)
  181. return result;
  182. #ifdef HAVE_GSSAPI
  183. data_sec = conn->data_prot;
  184. DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
  185. conn->data_prot = (unsigned char)data_sec;
  186. #endif
  187. Curl_debug(data, CURLINFO_HEADER_OUT, s, (size_t)bytes_written);
  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. pp->sendthis = NULL;
  196. pp->sendleft = pp->sendsize = 0;
  197. pp->response = Curl_now();
  198. }
  199. return CURLE_OK;
  200. }
  201. /***********************************************************************
  202. *
  203. * Curl_pp_sendf()
  204. *
  205. * Send the formatted string as a command to a pingpong server. Note that
  206. * the string should not have any CRLF appended, as this function will
  207. * append the necessary things itself.
  208. *
  209. * made to never block
  210. */
  211. CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
  212. const char *fmt, ...)
  213. {
  214. CURLcode result;
  215. va_list ap;
  216. va_start(ap, fmt);
  217. result = Curl_pp_vsendf(data, pp, fmt, ap);
  218. va_end(ap);
  219. return result;
  220. }
  221. /*
  222. * Curl_pp_readresp()
  223. *
  224. * Reads a piece of a server response.
  225. */
  226. CURLcode Curl_pp_readresp(struct Curl_easy *data,
  227. 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 = data->conn;
  237. char * const buf = data->state.buffer;
  238. CURLcode result = CURLE_OK;
  239. *code = 0; /* 0 for errors or not done */
  240. *size = 0;
  241. ptr = buf + pp->nread_resp;
  242. /* number of bytes in the current line, so far */
  243. perline = (ssize_t)(ptr-pp->linestart_resp);
  244. while((pp->nread_resp < (size_t)data->set.buffer_size) &&
  245. (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. if((ptr + pp->cache_size) > (buf + data->set.buffer_size + 1)) {
  255. failf(data, "cached response data too big to handle");
  256. return CURLE_WEIRD_SERVER_REPLY;
  257. }
  258. memcpy(ptr, pp->cache, pp->cache_size);
  259. gotbytes = (ssize_t)pp->cache_size;
  260. free(pp->cache); /* free the cache */
  261. pp->cache = NULL; /* clear the pointer */
  262. pp->cache_size = 0; /* zero the size just in case */
  263. }
  264. else {
  265. #ifdef HAVE_GSSAPI
  266. enum protection_level prot = conn->data_prot;
  267. conn->data_prot = PROT_CLEAR;
  268. #endif
  269. DEBUGASSERT((ptr + data->set.buffer_size - pp->nread_resp) <=
  270. (buf + data->set.buffer_size + 1));
  271. result = Curl_read(data, sockfd, ptr,
  272. data->set.buffer_size - pp->nread_resp,
  273. &gotbytes);
  274. #ifdef HAVE_GSSAPI
  275. DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
  276. conn->data_prot = (unsigned char)prot;
  277. #endif
  278. if(result == CURLE_AGAIN)
  279. return CURLE_OK; /* return */
  280. if(result)
  281. /* Set outer result variable to this error. */
  282. keepon = FALSE;
  283. }
  284. if(!keepon)
  285. ;
  286. else if(gotbytes <= 0) {
  287. keepon = FALSE;
  288. result = CURLE_RECV_ERROR;
  289. failf(data, "response reading failed (errno: %d)", SOCKERRNO);
  290. }
  291. else {
  292. /* we got a whole chunk of data, which can be anything from one
  293. * byte to a set of lines and possible just a piece of the last
  294. * line */
  295. ssize_t i;
  296. ssize_t clipamount = 0;
  297. bool restart = FALSE;
  298. data->req.headerbytecount += (unsigned int)gotbytes;
  299. pp->nread_resp += gotbytes;
  300. for(i = 0; i < gotbytes; ptr++, i++) {
  301. perline++;
  302. if(*ptr == '\n') {
  303. /* a newline is CRLF in pp-talk, so the CR is ignored as
  304. the line isn't really terminated until the LF comes */
  305. /* output debug output if that is requested */
  306. #ifdef HAVE_GSSAPI
  307. if(!conn->sec_complete)
  308. #endif
  309. Curl_debug(data, CURLINFO_HEADER_IN,
  310. pp->linestart_resp, (size_t)perline);
  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(data, CLIENTWRITE_INFO,
  317. pp->linestart_resp, perline);
  318. if(result)
  319. return result;
  320. if(pp->endofresp(data, 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 null-terminate, for old times sake */
  323. size_t n = ptr - pp->linestart_resp;
  324. memmove(buf, pp->linestart_resp, n);
  325. buf[n] = 0; /* null-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",
  346. (int)clipamount));
  347. }
  348. else if(keepon) {
  349. if((perline == gotbytes) &&
  350. (gotbytes > (ssize_t)data->set.buffer_size/2)) {
  351. /* We got an excessive line without newlines and we need to deal
  352. with it. We keep the first bytes of the line then we throw
  353. away the rest. */
  354. infof(data, "Excessive server response line length received, "
  355. "%zd bytes. Stripping", gotbytes);
  356. restart = TRUE;
  357. /* we keep 40 bytes since all our pingpong protocols are only
  358. interested in the first piece */
  359. clipamount = 40;
  360. }
  361. else if(pp->nread_resp > (size_t)data->set.buffer_size/2) {
  362. /* We got a large chunk of data and there's potentially still
  363. trailing data to take care of, so we put any such part in the
  364. "cache", clear the buffer to make space and restart. */
  365. clipamount = perline;
  366. restart = TRUE;
  367. }
  368. }
  369. else if(i == gotbytes)
  370. restart = TRUE;
  371. if(clipamount) {
  372. pp->cache_size = clipamount;
  373. pp->cache = malloc(pp->cache_size);
  374. if(pp->cache)
  375. memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
  376. else
  377. return CURLE_OUT_OF_MEMORY;
  378. }
  379. if(restart) {
  380. /* now reset a few variables to start over nicely from the start of
  381. the big buffer */
  382. pp->nread_resp = 0; /* start over from scratch in the buffer */
  383. ptr = pp->linestart_resp = buf;
  384. perline = 0;
  385. }
  386. } /* there was data */
  387. } /* while there's buffer left and loop is requested */
  388. pp->pending_resp = FALSE;
  389. return result;
  390. }
  391. int Curl_pp_getsock(struct Curl_easy *data,
  392. struct pingpong *pp, curl_socket_t *socks)
  393. {
  394. struct connectdata *conn = data->conn;
  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 Curl_easy *data,
  404. struct pingpong *pp)
  405. {
  406. /* we have a piece of a command still left to send */
  407. ssize_t written;
  408. CURLcode result = Curl_nwrite(data, FIRSTSOCKET,
  409. pp->sendthis + pp->sendsize - pp->sendleft,
  410. 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. pp->sendthis = NULL;
  419. pp->sendleft = pp->sendsize = 0;
  420. pp->response = Curl_now();
  421. }
  422. return CURLE_OK;
  423. }
  424. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  425. {
  426. Curl_dyn_free(&pp->sendbuf);
  427. Curl_safefree(pp->cache);
  428. return CURLE_OK;
  429. }
  430. bool Curl_pp_moredata(struct pingpong *pp)
  431. {
  432. return (!pp->sendleft && pp->cache && pp->nread_resp < pp->cache_size) ?
  433. TRUE : FALSE;
  434. }
  435. #endif