pingpong.c 15 KB

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