2
0

pingpong.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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(pp->overflow)
  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 did not wait, we do not 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 pingpong *pp)
  125. {
  126. pp->nread_resp = 0;
  127. pp->response = Curl_now(); /* start response time-out now! */
  128. pp->pending_resp = TRUE;
  129. Curl_dyn_init(&pp->sendbuf, DYN_PINGPPONG_CMD);
  130. Curl_dyn_init(&pp->recvbuf, DYN_PINGPPONG_CMD);
  131. }
  132. /***********************************************************************
  133. *
  134. * Curl_pp_vsendf()
  135. *
  136. * Send the formatted string as a command to a pingpong server. Note that
  137. * the string should not have any CRLF appended, as this function will
  138. * append the necessary things itself.
  139. *
  140. * made to never block
  141. */
  142. CURLcode Curl_pp_vsendf(struct Curl_easy *data,
  143. struct pingpong *pp,
  144. const char *fmt,
  145. va_list args)
  146. {
  147. size_t bytes_written = 0;
  148. size_t write_len;
  149. char *s;
  150. CURLcode result;
  151. struct connectdata *conn = data->conn;
  152. #ifdef HAVE_GSSAPI
  153. enum protection_level data_sec;
  154. #endif
  155. DEBUGASSERT(pp->sendleft == 0);
  156. DEBUGASSERT(pp->sendsize == 0);
  157. DEBUGASSERT(pp->sendthis == NULL);
  158. if(!conn)
  159. /* cannot send without a connection! */
  160. return CURLE_SEND_ERROR;
  161. Curl_dyn_reset(&pp->sendbuf);
  162. result = Curl_dyn_vaddf(&pp->sendbuf, fmt, args);
  163. if(result)
  164. return result;
  165. /* append CRLF */
  166. result = Curl_dyn_addn(&pp->sendbuf, "\r\n", 2);
  167. if(result)
  168. return result;
  169. pp->pending_resp = TRUE;
  170. write_len = Curl_dyn_len(&pp->sendbuf);
  171. s = Curl_dyn_ptr(&pp->sendbuf);
  172. #ifdef HAVE_GSSAPI
  173. conn->data_prot = PROT_CMD;
  174. #endif
  175. result = Curl_conn_send(data, FIRSTSOCKET, s, write_len, FALSE,
  176. &bytes_written);
  177. if(result == CURLE_AGAIN) {
  178. bytes_written = 0;
  179. }
  180. else 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, bytes_written);
  188. if(bytes_written != 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. static CURLcode pingpong_read(struct Curl_easy *data,
  222. int sockindex,
  223. char *buffer,
  224. size_t buflen,
  225. ssize_t *nread)
  226. {
  227. CURLcode result;
  228. #ifdef HAVE_GSSAPI
  229. enum protection_level prot = data->conn->data_prot;
  230. data->conn->data_prot = PROT_CLEAR;
  231. #endif
  232. result = Curl_conn_recv(data, sockindex, buffer, buflen, nread);
  233. #ifdef HAVE_GSSAPI
  234. DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
  235. data->conn->data_prot = (unsigned char)prot;
  236. #endif
  237. return result;
  238. }
  239. /*
  240. * Curl_pp_readresp()
  241. *
  242. * Reads a piece of a server response.
  243. */
  244. CURLcode Curl_pp_readresp(struct Curl_easy *data,
  245. int sockindex,
  246. struct pingpong *pp,
  247. int *code, /* return the server code if done */
  248. size_t *size) /* size of the response */
  249. {
  250. struct connectdata *conn = data->conn;
  251. CURLcode result = CURLE_OK;
  252. ssize_t gotbytes;
  253. char buffer[900];
  254. *code = 0; /* 0 for errors or not done */
  255. *size = 0;
  256. do {
  257. gotbytes = 0;
  258. if(pp->nfinal) {
  259. /* a previous call left this many bytes in the beginning of the buffer as
  260. that was the final line; now ditch that */
  261. size_t full = Curl_dyn_len(&pp->recvbuf);
  262. /* trim off the "final" leading part */
  263. Curl_dyn_tail(&pp->recvbuf, full - pp->nfinal);
  264. pp->nfinal = 0; /* now gone */
  265. }
  266. if(!pp->overflow) {
  267. result = pingpong_read(data, sockindex, buffer, sizeof(buffer),
  268. &gotbytes);
  269. if(result == CURLE_AGAIN)
  270. return CURLE_OK;
  271. if(result)
  272. return result;
  273. if(gotbytes <= 0) {
  274. failf(data, "response reading failed (errno: %d)", SOCKERRNO);
  275. return CURLE_RECV_ERROR;
  276. }
  277. result = Curl_dyn_addn(&pp->recvbuf, buffer, gotbytes);
  278. if(result)
  279. return result;
  280. data->req.headerbytecount += (unsigned int)gotbytes;
  281. pp->nread_resp += gotbytes;
  282. }
  283. do {
  284. char *line = Curl_dyn_ptr(&pp->recvbuf);
  285. char *nl = memchr(line, '\n', Curl_dyn_len(&pp->recvbuf));
  286. if(nl) {
  287. /* a newline is CRLF in pp-talk, so the CR is ignored as
  288. the line is not really terminated until the LF comes */
  289. size_t length = nl - line + 1;
  290. /* output debug output if that is requested */
  291. #ifdef HAVE_GSSAPI
  292. if(!conn->sec_complete)
  293. #endif
  294. Curl_debug(data, CURLINFO_HEADER_IN, line, length);
  295. /*
  296. * Pass all response-lines to the callback function registered for
  297. * "headers". The response lines can be seen as a kind of headers.
  298. */
  299. result = Curl_client_write(data, CLIENTWRITE_INFO, line, length);
  300. if(result)
  301. return result;
  302. if(pp->endofresp(data, conn, line, length, code)) {
  303. /* When at "end of response", keep the endofresp line first in the
  304. buffer since it will be accessed outside (by pingpong
  305. parsers). Store the overflow counter to inform about additional
  306. data in this buffer after the endofresp line. */
  307. pp->nfinal = length;
  308. if(Curl_dyn_len(&pp->recvbuf) > length)
  309. pp->overflow = Curl_dyn_len(&pp->recvbuf) - length;
  310. else
  311. pp->overflow = 0;
  312. *size = pp->nread_resp; /* size of the response */
  313. pp->nread_resp = 0; /* restart */
  314. gotbytes = 0; /* force break out of outer loop */
  315. break;
  316. }
  317. if(Curl_dyn_len(&pp->recvbuf) > length)
  318. /* keep the remaining piece */
  319. Curl_dyn_tail((&pp->recvbuf), Curl_dyn_len(&pp->recvbuf) - length);
  320. else
  321. Curl_dyn_reset(&pp->recvbuf);
  322. }
  323. else {
  324. /* without a newline, there is no overflow */
  325. pp->overflow = 0;
  326. break;
  327. }
  328. } while(1); /* while there is buffer left to scan */
  329. } while(gotbytes == sizeof(buffer));
  330. pp->pending_resp = FALSE;
  331. return result;
  332. }
  333. int Curl_pp_getsock(struct Curl_easy *data,
  334. struct pingpong *pp, curl_socket_t *socks)
  335. {
  336. struct connectdata *conn = data->conn;
  337. socks[0] = conn->sock[FIRSTSOCKET];
  338. if(pp->sendleft) {
  339. /* write mode */
  340. return GETSOCK_WRITESOCK(0);
  341. }
  342. /* read mode */
  343. return GETSOCK_READSOCK(0);
  344. }
  345. bool Curl_pp_needs_flush(struct Curl_easy *data,
  346. struct pingpong *pp)
  347. {
  348. (void)data;
  349. return pp->sendleft > 0;
  350. }
  351. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  352. struct pingpong *pp)
  353. {
  354. /* we have a piece of a command still left to send */
  355. size_t written;
  356. CURLcode result;
  357. if(!Curl_pp_needs_flush(data, pp))
  358. return CURLE_OK;
  359. result = Curl_conn_send(data, FIRSTSOCKET,
  360. pp->sendthis + pp->sendsize - pp->sendleft,
  361. pp->sendleft, FALSE, &written);
  362. if(result == CURLE_AGAIN) {
  363. result = CURLE_OK;
  364. written = 0;
  365. }
  366. if(result)
  367. return result;
  368. if(written != pp->sendleft) {
  369. /* only a fraction was sent */
  370. pp->sendleft -= written;
  371. }
  372. else {
  373. pp->sendthis = NULL;
  374. pp->sendleft = pp->sendsize = 0;
  375. pp->response = Curl_now();
  376. }
  377. return CURLE_OK;
  378. }
  379. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  380. {
  381. Curl_dyn_free(&pp->sendbuf);
  382. Curl_dyn_free(&pp->recvbuf);
  383. return CURLE_OK;
  384. }
  385. bool Curl_pp_moredata(struct pingpong *pp)
  386. {
  387. return (!pp->sendleft && Curl_dyn_len(&pp->recvbuf) > pp->nfinal);
  388. }
  389. #endif