pingpong.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. #include "strdup.h"
  38. /* The last 3 #include files should be in this order */
  39. #include "curl_printf.h"
  40. #include "curl_memory.h"
  41. #include "memdebug.h"
  42. #ifdef USE_PINGPONG
  43. /* Returns timeout in ms. 0 or negative number means the timeout has already
  44. triggered */
  45. timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
  46. struct pingpong *pp, bool disconnecting)
  47. {
  48. struct connectdata *conn = data->conn;
  49. timediff_t timeout_ms; /* in milliseconds */
  50. timediff_t response_time = (data->set.server_response_timeout)?
  51. data->set.server_response_timeout: pp->response_time;
  52. /* if CURLOPT_SERVER_RESPONSE_TIMEOUT is set, use that to determine
  53. remaining time, or use pp->response because SERVER_RESPONSE_TIMEOUT is
  54. supposed to govern the response for any given server response, not for
  55. the time from connect to the given server response. */
  56. /* Without a requested timeout, we only wait 'response_time' seconds for the
  57. full response to arrive before we bail out */
  58. timeout_ms = response_time -
  59. Curl_timediff(Curl_now(), pp->response); /* spent time */
  60. if(data->set.timeout && !disconnecting) {
  61. /* if timeout is requested, find out how much remaining time we have */
  62. timediff_t timeout2_ms = data->set.timeout - /* timeout time */
  63. Curl_timediff(Curl_now(), conn->now); /* spent time */
  64. /* pick the lowest number */
  65. timeout_ms = CURLMIN(timeout_ms, timeout2_ms);
  66. }
  67. return timeout_ms;
  68. }
  69. /*
  70. * Curl_pp_statemach()
  71. */
  72. CURLcode Curl_pp_statemach(struct Curl_easy *data,
  73. struct pingpong *pp, bool block,
  74. bool disconnecting)
  75. {
  76. struct connectdata *conn = data->conn;
  77. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  78. int rc;
  79. timediff_t interval_ms;
  80. timediff_t timeout_ms = Curl_pp_state_timeout(data, pp, disconnecting);
  81. CURLcode result = CURLE_OK;
  82. if(timeout_ms <= 0) {
  83. failf(data, "server response timeout");
  84. return CURLE_OPERATION_TIMEDOUT; /* already too little time */
  85. }
  86. if(block) {
  87. interval_ms = 1000; /* use 1 second timeout intervals */
  88. if(timeout_ms < interval_ms)
  89. interval_ms = timeout_ms;
  90. }
  91. else
  92. interval_ms = 0; /* immediate */
  93. if(Curl_conn_data_pending(data, FIRSTSOCKET))
  94. rc = 1;
  95. else if(Curl_pp_moredata(pp))
  96. /* We are receiving and there is data in the cache so just read it */
  97. rc = 1;
  98. else if(!pp->sendleft && Curl_conn_data_pending(data, FIRSTSOCKET))
  99. /* We are receiving and there is data ready in the SSL library */
  100. rc = 1;
  101. else
  102. rc = Curl_socket_check(pp->sendleft?CURL_SOCKET_BAD:sock, /* reading */
  103. CURL_SOCKET_BAD,
  104. pp->sendleft?sock:CURL_SOCKET_BAD, /* writing */
  105. interval_ms);
  106. if(block) {
  107. /* if we didn't wait, we don't have to spend time on this now */
  108. if(Curl_pgrsUpdate(data))
  109. result = CURLE_ABORTED_BY_CALLBACK;
  110. else
  111. result = Curl_speedcheck(data, Curl_now());
  112. if(result)
  113. return result;
  114. }
  115. if(rc == -1) {
  116. failf(data, "select/poll error");
  117. result = CURLE_OUT_OF_MEMORY;
  118. }
  119. else if(rc)
  120. result = pp->statemachine(data, data->conn);
  121. return result;
  122. }
  123. /* initialize stuff to prepare for reading a fresh new response */
  124. void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp)
  125. {
  126. DEBUGASSERT(data);
  127. pp->nread_resp = 0;
  128. pp->linestart_resp = data->state.buffer;
  129. pp->pending_resp = TRUE;
  130. pp->response = Curl_now(); /* start response time-out now! */
  131. }
  132. /* setup for the coming transfer */
  133. void Curl_pp_setup(struct pingpong *pp)
  134. {
  135. Curl_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
  136. }
  137. /***********************************************************************
  138. *
  139. * Curl_pp_vsendf()
  140. *
  141. * Send the formatted string as a command to a pingpong server. Note that
  142. * the string should not have any CRLF appended, as this function will
  143. * append the necessary things itself.
  144. *
  145. * made to never block
  146. */
  147. CURLcode Curl_pp_vsendf(struct Curl_easy *data,
  148. struct pingpong *pp,
  149. const char *fmt,
  150. va_list args)
  151. {
  152. ssize_t bytes_written = 0;
  153. size_t write_len;
  154. char *s;
  155. CURLcode result;
  156. struct connectdata *conn = data->conn;
  157. #ifdef HAVE_GSSAPI
  158. enum protection_level data_sec;
  159. #endif
  160. DEBUGASSERT(pp->sendleft == 0);
  161. DEBUGASSERT(pp->sendsize == 0);
  162. DEBUGASSERT(pp->sendthis == NULL);
  163. if(!conn)
  164. /* can't send without a connection! */
  165. return CURLE_SEND_ERROR;
  166. Curl_dyn_reset(&pp->sendbuf);
  167. result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
  168. if(result)
  169. return result;
  170. /* append CRLF */
  171. result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
  172. if(result)
  173. return result;
  174. write_len = Curl_dyn_len(&pp->sendbuf);
  175. s = Curl_dyn_ptr(&pp->sendbuf);
  176. Curl_pp_init(data, pp);
  177. #ifdef HAVE_GSSAPI
  178. conn->data_prot = PROT_CMD;
  179. #endif
  180. result = Curl_nwrite(data, FIRSTSOCKET, s, write_len, &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 += (unsigned int)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_INFO,
  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 = Curl_memdup(pp->linestart_resp, pp->cache_size);
  375. if(!pp->cache)
  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 Curl_easy *data,
  391. struct pingpong *pp, curl_socket_t *socks)
  392. {
  393. struct connectdata *conn = data->conn;
  394. socks[0] = conn->sock[FIRSTSOCKET];
  395. if(pp->sendleft) {
  396. /* write mode */
  397. return GETSOCK_WRITESOCK(0);
  398. }
  399. /* read mode */
  400. return GETSOCK_READSOCK(0);
  401. }
  402. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  403. struct pingpong *pp)
  404. {
  405. /* we have a piece of a command still left to send */
  406. ssize_t written;
  407. CURLcode result = Curl_nwrite(data, FIRSTSOCKET,
  408. pp->sendthis + pp->sendsize - pp->sendleft,
  409. pp->sendleft, &written);
  410. if(result)
  411. return result;
  412. if(written != (ssize_t)pp->sendleft) {
  413. /* only a fraction was sent */
  414. pp->sendleft -= written;
  415. }
  416. else {
  417. pp->sendthis = NULL;
  418. pp->sendleft = pp->sendsize = 0;
  419. pp->response = Curl_now();
  420. }
  421. return CURLE_OK;
  422. }
  423. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  424. {
  425. Curl_dyn_free(&pp->sendbuf);
  426. Curl_safefree(pp->cache);
  427. return CURLE_OK;
  428. }
  429. bool Curl_pp_moredata(struct pingpong *pp)
  430. {
  431. return (!pp->sendleft && pp->cache && pp->nread_resp < pp->cache_size) ?
  432. TRUE : FALSE;
  433. }
  434. #endif