pingpong.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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_write(data, conn->sock[FIRSTSOCKET], s, write_len,
  180. &bytes_written);
  181. if(result)
  182. return result;
  183. #ifdef HAVE_GSSAPI
  184. data_sec = conn->data_prot;
  185. DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
  186. conn->data_prot = (unsigned char)data_sec;
  187. #endif
  188. Curl_debug(data, CURLINFO_HEADER_OUT, s, (size_t)bytes_written);
  189. if(bytes_written != (ssize_t)write_len) {
  190. /* the whole chunk was not sent, keep it around and adjust sizes */
  191. pp->sendthis = s;
  192. pp->sendsize = write_len;
  193. pp->sendleft = write_len - bytes_written;
  194. }
  195. else {
  196. pp->sendthis = NULL;
  197. pp->sendleft = pp->sendsize = 0;
  198. pp->response = Curl_now();
  199. }
  200. return CURLE_OK;
  201. }
  202. /***********************************************************************
  203. *
  204. * Curl_pp_sendf()
  205. *
  206. * Send the formatted 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 Curl_easy *data, 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(data, 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(struct Curl_easy *data,
  228. curl_socket_t sockfd,
  229. struct pingpong *pp,
  230. int *code, /* return the server code if done */
  231. size_t *size) /* size of the response */
  232. {
  233. ssize_t perline; /* count bytes per line */
  234. bool keepon = TRUE;
  235. ssize_t gotbytes;
  236. char *ptr;
  237. struct connectdata *conn = data->conn;
  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 < (size_t)data->set.buffer_size) &&
  246. (keepon && !result)) {
  247. if(pp->cache) {
  248. /* we had data in the "cache", copy that instead of doing an actual
  249. * read
  250. *
  251. * pp->cache_size is cast to ssize_t here. This should be safe, because
  252. * it would have been populated with something of size int to begin
  253. * with, even though its datatype may be larger than an int.
  254. */
  255. if((ptr + pp->cache_size) > (buf + data->set.buffer_size + 1)) {
  256. failf(data, "cached response data too big to handle");
  257. return CURLE_WEIRD_SERVER_REPLY;
  258. }
  259. memcpy(ptr, pp->cache, pp->cache_size);
  260. gotbytes = (ssize_t)pp->cache_size;
  261. free(pp->cache); /* free the cache */
  262. pp->cache = NULL; /* clear the pointer */
  263. pp->cache_size = 0; /* zero the size just in case */
  264. }
  265. else {
  266. #ifdef HAVE_GSSAPI
  267. enum protection_level prot = conn->data_prot;
  268. conn->data_prot = PROT_CLEAR;
  269. #endif
  270. DEBUGASSERT((ptr + data->set.buffer_size - pp->nread_resp) <=
  271. (buf + data->set.buffer_size + 1));
  272. result = Curl_read(data, sockfd, ptr,
  273. data->set.buffer_size - pp->nread_resp,
  274. &gotbytes);
  275. #ifdef HAVE_GSSAPI
  276. DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
  277. conn->data_prot = (unsigned char)prot;
  278. #endif
  279. if(result == CURLE_AGAIN)
  280. return CURLE_OK; /* return */
  281. if(result)
  282. /* Set outer result variable to this error. */
  283. keepon = FALSE;
  284. }
  285. if(!keepon)
  286. ;
  287. else if(gotbytes <= 0) {
  288. keepon = FALSE;
  289. result = CURLE_RECV_ERROR;
  290. failf(data, "response reading failed (errno: %d)", SOCKERRNO);
  291. }
  292. else {
  293. /* we got a whole chunk of data, which can be anything from one
  294. * byte to a set of lines and possible just a piece of the last
  295. * line */
  296. ssize_t i;
  297. ssize_t clipamount = 0;
  298. bool restart = FALSE;
  299. data->req.headerbytecount += (long)gotbytes;
  300. pp->nread_resp += gotbytes;
  301. for(i = 0; i < gotbytes; ptr++, i++) {
  302. perline++;
  303. if(*ptr == '\n') {
  304. /* a newline is CRLF in pp-talk, so the CR is ignored as
  305. the line isn't really terminated until the LF comes */
  306. /* output debug output if that is requested */
  307. #ifdef HAVE_GSSAPI
  308. if(!conn->sec_complete)
  309. #endif
  310. Curl_debug(data, CURLINFO_HEADER_IN,
  311. pp->linestart_resp, (size_t)perline);
  312. /*
  313. * We pass all response-lines to the callback function registered
  314. * for "headers". The response lines can be seen as a kind of
  315. * headers.
  316. */
  317. result = Curl_client_write(data, CLIENTWRITE_HEADER,
  318. pp->linestart_resp, perline);
  319. if(result)
  320. return result;
  321. if(pp->endofresp(data, conn, pp->linestart_resp, perline, code)) {
  322. /* This is the end of the last line, copy the last line to the
  323. start of the buffer and null-terminate, for old times sake */
  324. size_t n = ptr - pp->linestart_resp;
  325. memmove(buf, pp->linestart_resp, n);
  326. buf[n] = 0; /* null-terminate */
  327. keepon = FALSE;
  328. pp->linestart_resp = ptr + 1; /* advance pointer */
  329. i++; /* skip this before getting out */
  330. *size = pp->nread_resp; /* size of the response */
  331. pp->nread_resp = 0; /* restart */
  332. break;
  333. }
  334. perline = 0; /* line starts over here */
  335. pp->linestart_resp = ptr + 1;
  336. }
  337. }
  338. if(!keepon && (i != gotbytes)) {
  339. /* We found the end of the response lines, but we didn't parse the
  340. full chunk of data we have read from the server. We therefore need
  341. to store the rest of the data to be checked on the next invoke as
  342. it may actually contain another end of response already! */
  343. clipamount = gotbytes - i;
  344. restart = TRUE;
  345. DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
  346. "server response left",
  347. (int)clipamount));
  348. }
  349. else if(keepon) {
  350. if((perline == gotbytes) &&
  351. (gotbytes > (ssize_t)data->set.buffer_size/2)) {
  352. /* We got an excessive line without newlines and we need to deal
  353. with it. We keep the first bytes of the line then we throw
  354. away the rest. */
  355. infof(data, "Excessive server response line length received, "
  356. "%zd bytes. Stripping", gotbytes);
  357. restart = TRUE;
  358. /* we keep 40 bytes since all our pingpong protocols are only
  359. interested in the first piece */
  360. clipamount = 40;
  361. }
  362. else if(pp->nread_resp > (size_t)data->set.buffer_size/2) {
  363. /* We got a large chunk of data and there's potentially still
  364. trailing data to take care of, so we put any such part in the
  365. "cache", clear the buffer to make space and restart. */
  366. clipamount = perline;
  367. restart = TRUE;
  368. }
  369. }
  370. else if(i == gotbytes)
  371. restart = TRUE;
  372. if(clipamount) {
  373. pp->cache_size = clipamount;
  374. pp->cache = malloc(pp->cache_size);
  375. if(pp->cache)
  376. memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
  377. else
  378. return CURLE_OUT_OF_MEMORY;
  379. }
  380. if(restart) {
  381. /* now reset a few variables to start over nicely from the start of
  382. the big buffer */
  383. pp->nread_resp = 0; /* start over from scratch in the buffer */
  384. ptr = pp->linestart_resp = buf;
  385. perline = 0;
  386. }
  387. } /* there was data */
  388. } /* while there's buffer left and loop is requested */
  389. pp->pending_resp = FALSE;
  390. return result;
  391. }
  392. int Curl_pp_getsock(struct Curl_easy *data,
  393. struct pingpong *pp, curl_socket_t *socks)
  394. {
  395. struct connectdata *conn = data->conn;
  396. socks[0] = conn->sock[FIRSTSOCKET];
  397. if(pp->sendleft) {
  398. /* write mode */
  399. return GETSOCK_WRITESOCK(0);
  400. }
  401. /* read mode */
  402. return GETSOCK_READSOCK(0);
  403. }
  404. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  405. struct pingpong *pp)
  406. {
  407. /* we have a piece of a command still left to send */
  408. struct connectdata *conn = data->conn;
  409. ssize_t written;
  410. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  411. CURLcode result = Curl_write(data, 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. pp->sendthis = NULL;
  421. pp->sendleft = pp->sendsize = 0;
  422. pp->response = Curl_now();
  423. }
  424. return CURLE_OK;
  425. }
  426. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  427. {
  428. Curl_dyn_free(&pp->sendbuf);
  429. Curl_safefree(pp->cache);
  430. return CURLE_OK;
  431. }
  432. bool Curl_pp_moredata(struct pingpong *pp)
  433. {
  434. return (!pp->sendleft && pp->cache && pp->nread_resp < pp->cache_size) ?
  435. TRUE : FALSE;
  436. }
  437. #endif