pingpong.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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 pingpong *pp, bool disconnecting)
  43. {
  44. struct connectdata *conn = pp->conn;
  45. struct Curl_easy *data = conn->data;
  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 pingpong *pp, bool block,
  70. bool disconnecting)
  71. {
  72. struct connectdata *conn = pp->conn;
  73. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  74. int rc;
  75. timediff_t interval_ms;
  76. timediff_t timeout_ms = Curl_pp_state_timeout(pp, disconnecting);
  77. struct Curl_easy *data = conn->data;
  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(conn))
  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->statemach_act(conn);
  118. return result;
  119. }
  120. /* initialize stuff to prepare for reading a fresh new response */
  121. void Curl_pp_init(struct pingpong *pp)
  122. {
  123. struct connectdata *conn = pp->conn;
  124. pp->nread_resp = 0;
  125. pp->linestart_resp = conn->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 pingpong *pp,
  145. const char *fmt,
  146. va_list args)
  147. {
  148. ssize_t bytes_written = 0;
  149. size_t write_len;
  150. char *s;
  151. CURLcode result;
  152. struct connectdata *conn = pp->conn;
  153. struct Curl_easy *data;
  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. data = conn->data;
  164. Curl_dyn_reset(&pp->sendbuf);
  165. result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
  166. if(result)
  167. return result;
  168. /* append CRLF */
  169. result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
  170. if(result)
  171. return result;
  172. write_len = Curl_dyn_len(&pp->sendbuf);
  173. s = Curl_dyn_ptr(&pp->sendbuf);
  174. Curl_pp_init(pp);
  175. result = Curl_convert_to_network(data, s, write_len);
  176. /* Curl_convert_to_network calls failf if unsuccessful */
  177. if(result)
  178. return result;
  179. #ifdef HAVE_GSSAPI
  180. conn->data_prot = PROT_CMD;
  181. #endif
  182. result = Curl_write(conn, conn->sock[FIRSTSOCKET], s, write_len,
  183. &bytes_written);
  184. if(result)
  185. return result;
  186. #ifdef HAVE_GSSAPI
  187. data_sec = conn->data_prot;
  188. DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
  189. conn->data_prot = data_sec;
  190. #endif
  191. Curl_debug(data, CURLINFO_HEADER_OUT, s, (size_t)bytes_written);
  192. if(bytes_written != (ssize_t)write_len) {
  193. /* the whole chunk was not sent, keep it around and adjust sizes */
  194. pp->sendthis = s;
  195. pp->sendsize = write_len;
  196. pp->sendleft = write_len - bytes_written;
  197. }
  198. else {
  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. Curl_debug(data, CURLINFO_HEADER_IN,
  318. pp->linestart_resp, (size_t)perline);
  319. /*
  320. * We pass all response-lines to the callback function registered
  321. * for "headers". The response lines can be seen as a kind of
  322. * headers.
  323. */
  324. result = Curl_client_write(conn, CLIENTWRITE_HEADER,
  325. pp->linestart_resp, perline);
  326. if(result)
  327. return result;
  328. if(pp->endofresp(conn, pp->linestart_resp, perline, code)) {
  329. /* This is the end of the last line, copy the last line to the
  330. start of the buffer and null-terminate, for old times sake */
  331. size_t n = ptr - pp->linestart_resp;
  332. memmove(buf, pp->linestart_resp, n);
  333. buf[n] = 0; /* null-terminate */
  334. keepon = FALSE;
  335. pp->linestart_resp = ptr + 1; /* advance pointer */
  336. i++; /* skip this before getting out */
  337. *size = pp->nread_resp; /* size of the response */
  338. pp->nread_resp = 0; /* restart */
  339. break;
  340. }
  341. perline = 0; /* line starts over here */
  342. pp->linestart_resp = ptr + 1;
  343. }
  344. }
  345. if(!keepon && (i != gotbytes)) {
  346. /* We found the end of the response lines, but we didn't parse the
  347. full chunk of data we have read from the server. We therefore need
  348. to store the rest of the data to be checked on the next invoke as
  349. it may actually contain another end of response already! */
  350. clipamount = gotbytes - i;
  351. restart = TRUE;
  352. DEBUGF(infof(data, "Curl_pp_readresp_ %d bytes of trailing "
  353. "server response left\n",
  354. (int)clipamount));
  355. }
  356. else if(keepon) {
  357. if((perline == gotbytes) && (gotbytes > data->set.buffer_size/2)) {
  358. /* We got an excessive line without newlines and we need to deal
  359. with it. We keep the first bytes of the line then we throw
  360. away the rest. */
  361. infof(data, "Excessive server response line length received, "
  362. "%zd bytes. Stripping\n", gotbytes);
  363. restart = TRUE;
  364. /* we keep 40 bytes since all our pingpong protocols are only
  365. interested in the first piece */
  366. clipamount = 40;
  367. }
  368. else if(pp->nread_resp > (size_t)data->set.buffer_size/2) {
  369. /* We got a large chunk of data and there's potentially still
  370. trailing data to take care of, so we put any such part in the
  371. "cache", clear the buffer to make space and restart. */
  372. clipamount = perline;
  373. restart = TRUE;
  374. }
  375. }
  376. else if(i == gotbytes)
  377. restart = TRUE;
  378. if(clipamount) {
  379. pp->cache_size = clipamount;
  380. pp->cache = malloc(pp->cache_size);
  381. if(pp->cache)
  382. memcpy(pp->cache, pp->linestart_resp, pp->cache_size);
  383. else
  384. return CURLE_OUT_OF_MEMORY;
  385. }
  386. if(restart) {
  387. /* now reset a few variables to start over nicely from the start of
  388. the big buffer */
  389. pp->nread_resp = 0; /* start over from scratch in the buffer */
  390. ptr = pp->linestart_resp = buf;
  391. perline = 0;
  392. }
  393. } /* there was data */
  394. } /* while there's buffer left and loop is requested */
  395. pp->pending_resp = FALSE;
  396. return result;
  397. }
  398. int Curl_pp_getsock(struct pingpong *pp,
  399. curl_socket_t *socks)
  400. {
  401. struct connectdata *conn = pp->conn;
  402. socks[0] = conn->sock[FIRSTSOCKET];
  403. if(pp->sendleft) {
  404. /* write mode */
  405. return GETSOCK_WRITESOCK(0);
  406. }
  407. /* read mode */
  408. return GETSOCK_READSOCK(0);
  409. }
  410. CURLcode Curl_pp_flushsend(struct pingpong *pp)
  411. {
  412. /* we have a piece of a command still left to send */
  413. struct connectdata *conn = pp->conn;
  414. ssize_t written;
  415. curl_socket_t sock = conn->sock[FIRSTSOCKET];
  416. CURLcode result = Curl_write(conn, 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