2
0

pingpong.c 15 KB

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