pingpong.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 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 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. /* can't 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, &bytes_written);
  176. if(result == CURLE_AGAIN) {
  177. bytes_written = 0;
  178. }
  179. else if(result)
  180. return result;
  181. #ifdef HAVE_GSSAPI
  182. data_sec = conn->data_prot;
  183. DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
  184. conn->data_prot = (unsigned char)data_sec;
  185. #endif
  186. Curl_debug(data, CURLINFO_HEADER_OUT, s, bytes_written);
  187. if(bytes_written != write_len) {
  188. /* the whole chunk was not sent, keep it around and adjust sizes */
  189. pp->sendthis = s;
  190. pp->sendsize = write_len;
  191. pp->sendleft = write_len - bytes_written;
  192. }
  193. else {
  194. pp->sendthis = NULL;
  195. pp->sendleft = pp->sendsize = 0;
  196. pp->response = Curl_now();
  197. }
  198. return CURLE_OK;
  199. }
  200. /***********************************************************************
  201. *
  202. * Curl_pp_sendf()
  203. *
  204. * Send the formatted string as a command to a pingpong server. Note that
  205. * the string should not have any CRLF appended, as this function will
  206. * append the necessary things itself.
  207. *
  208. * made to never block
  209. */
  210. CURLcode Curl_pp_sendf(struct Curl_easy *data, struct pingpong *pp,
  211. const char *fmt, ...)
  212. {
  213. CURLcode result;
  214. va_list ap;
  215. va_start(ap, fmt);
  216. result = Curl_pp_vsendf(data, pp, fmt, ap);
  217. va_end(ap);
  218. return result;
  219. }
  220. static CURLcode pingpong_read(struct Curl_easy *data,
  221. int sockindex,
  222. char *buffer,
  223. size_t buflen,
  224. ssize_t *nread)
  225. {
  226. CURLcode result;
  227. #ifdef HAVE_GSSAPI
  228. enum protection_level prot = data->conn->data_prot;
  229. data->conn->data_prot = PROT_CLEAR;
  230. #endif
  231. result = Curl_conn_recv(data, sockindex, buffer, buflen, nread);
  232. #ifdef HAVE_GSSAPI
  233. DEBUGASSERT(prot > PROT_NONE && prot < PROT_LAST);
  234. data->conn->data_prot = (unsigned char)prot;
  235. #endif
  236. return result;
  237. }
  238. /*
  239. * Curl_pp_readresp()
  240. *
  241. * Reads a piece of a server response.
  242. */
  243. CURLcode Curl_pp_readresp(struct Curl_easy *data,
  244. int sockindex,
  245. struct pingpong *pp,
  246. int *code, /* return the server code if done */
  247. size_t *size) /* size of the response */
  248. {
  249. struct connectdata *conn = data->conn;
  250. CURLcode result = CURLE_OK;
  251. *code = 0; /* 0 for errors or not done */
  252. *size = 0;
  253. if(pp->nfinal) {
  254. /* a previous call left this many bytes in the beginning of the buffer as
  255. that was the final line; now ditch that */
  256. size_t full = Curl_dyn_len(&pp->recvbuf);
  257. /* trim off the "final" leading part */
  258. Curl_dyn_tail(&pp->recvbuf, full - pp->nfinal);
  259. pp->nfinal = 0; /* now gone */
  260. }
  261. if(!pp->overflow) {
  262. ssize_t gotbytes = 0;
  263. char buffer[900];
  264. result = pingpong_read(data, sockindex, buffer, sizeof(buffer), &gotbytes);
  265. if(result == CURLE_AGAIN)
  266. return CURLE_OK;
  267. if(result)
  268. return result;
  269. if(gotbytes <= 0) {
  270. failf(data, "response reading failed (errno: %d)", SOCKERRNO);
  271. return CURLE_RECV_ERROR;
  272. }
  273. result = Curl_dyn_addn(&pp->recvbuf, buffer, gotbytes);
  274. if(result)
  275. return result;
  276. data->req.headerbytecount += (unsigned int)gotbytes;
  277. pp->nread_resp += gotbytes;
  278. }
  279. do {
  280. char *line = Curl_dyn_ptr(&pp->recvbuf);
  281. char *nl = memchr(line, '\n', Curl_dyn_len(&pp->recvbuf));
  282. if(nl) {
  283. /* a newline is CRLF in pp-talk, so the CR is ignored as
  284. the line isn't really terminated until the LF comes */
  285. size_t length = nl - line + 1;
  286. /* output debug output if that is requested */
  287. #ifdef HAVE_GSSAPI
  288. if(!conn->sec_complete)
  289. #endif
  290. Curl_debug(data, CURLINFO_HEADER_IN, line, length);
  291. /*
  292. * Pass all response-lines to the callback function registered for
  293. * "headers". The response lines can be seen as a kind of headers.
  294. */
  295. result = Curl_client_write(data, CLIENTWRITE_INFO, line, length);
  296. if(result)
  297. return result;
  298. if(pp->endofresp(data, conn, line, length, code)) {
  299. /* When at "end of response", keep the endofresp line first in the
  300. buffer since it will be accessed outside (by pingpong
  301. parsers). Store the overflow counter to inform about additional
  302. data in this buffer after the endofresp line. */
  303. pp->nfinal = length;
  304. if(Curl_dyn_len(&pp->recvbuf) > length)
  305. pp->overflow = Curl_dyn_len(&pp->recvbuf) - length;
  306. else
  307. pp->overflow = 0;
  308. *size = pp->nread_resp; /* size of the response */
  309. pp->nread_resp = 0; /* restart */
  310. break;
  311. }
  312. if(Curl_dyn_len(&pp->recvbuf) > length)
  313. /* keep the remaining piece */
  314. Curl_dyn_tail((&pp->recvbuf), Curl_dyn_len(&pp->recvbuf) - length);
  315. else
  316. Curl_dyn_reset(&pp->recvbuf);
  317. }
  318. else {
  319. /* without a newline, there is no overflow */
  320. pp->overflow = 0;
  321. break;
  322. }
  323. } while(1); /* while there's buffer left to scan */
  324. pp->pending_resp = FALSE;
  325. return result;
  326. }
  327. int Curl_pp_getsock(struct Curl_easy *data,
  328. struct pingpong *pp, curl_socket_t *socks)
  329. {
  330. struct connectdata *conn = data->conn;
  331. socks[0] = conn->sock[FIRSTSOCKET];
  332. if(pp->sendleft) {
  333. /* write mode */
  334. return GETSOCK_WRITESOCK(0);
  335. }
  336. /* read mode */
  337. return GETSOCK_READSOCK(0);
  338. }
  339. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  340. struct pingpong *pp)
  341. {
  342. /* we have a piece of a command still left to send */
  343. size_t written;
  344. CURLcode result;
  345. result = Curl_conn_send(data, FIRSTSOCKET,
  346. pp->sendthis + pp->sendsize - pp->sendleft,
  347. pp->sendleft, &written);
  348. if(result == CURLE_AGAIN) {
  349. result = CURLE_OK;
  350. written = 0;
  351. }
  352. if(result)
  353. return result;
  354. if(written != pp->sendleft) {
  355. /* only a fraction was sent */
  356. pp->sendleft -= written;
  357. }
  358. else {
  359. pp->sendthis = NULL;
  360. pp->sendleft = pp->sendsize = 0;
  361. pp->response = Curl_now();
  362. }
  363. return CURLE_OK;
  364. }
  365. CURLcode Curl_pp_disconnect(struct pingpong *pp)
  366. {
  367. Curl_dyn_free(&pp->sendbuf);
  368. Curl_dyn_free(&pp->recvbuf);
  369. return CURLE_OK;
  370. }
  371. bool Curl_pp_moredata(struct pingpong *pp)
  372. {
  373. return (!pp->sendleft && Curl_dyn_len(&pp->recvbuf) > pp->nfinal);
  374. }
  375. #endif