pingpong.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef HEADER_CURL_PINGPONG_H
  2. #define HEADER_CURL_PINGPONG_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_FTP) || \
  28. !defined(CURL_DISABLE_POP3) || !defined(CURL_DISABLE_SMTP)
  29. #define USE_PINGPONG
  30. #endif
  31. /* forward-declaration, this is defined in urldata.h */
  32. struct connectdata;
  33. typedef enum {
  34. PPTRANSFER_BODY, /* yes do transfer a body */
  35. PPTRANSFER_INFO, /* do still go through to get info/headers */
  36. PPTRANSFER_NONE /* do not get anything and do not get info */
  37. } curl_pp_transfer;
  38. /*
  39. * 'pingpong' is the generic struct used for protocols doing server<->client
  40. * conversations in a back-and-forth style such as FTP, IMAP, POP3, SMTP etc.
  41. *
  42. * It holds response cache and non-blocking sending data.
  43. */
  44. struct pingpong {
  45. size_t nread_resp; /* number of bytes currently read of a server response */
  46. bool pending_resp; /* set TRUE when a server response is pending or in
  47. progress, and is cleared once the last response is
  48. read */
  49. char *sendthis; /* pointer to a buffer that is to be sent to the server */
  50. size_t sendleft; /* number of bytes left to send from the sendthis buffer */
  51. size_t sendsize; /* total size of the sendthis buffer */
  52. struct curltime response; /* set to Curl_now() when a command has been sent
  53. off, used to time-out response reading */
  54. timediff_t response_time; /* When no timeout is given, this is the amount of
  55. milliseconds we await for a server response. */
  56. struct dynbuf sendbuf;
  57. struct dynbuf recvbuf;
  58. size_t overflow; /* number of bytes left after a final response line */
  59. size_t nfinal; /* number of bytes in the final response line, which
  60. after a match is first in the receice buffer */
  61. /* Function pointers the protocols MUST implement and provide for the
  62. pingpong layer to function */
  63. CURLcode (*statemachine)(struct Curl_easy *data, struct connectdata *conn);
  64. bool (*endofresp)(struct Curl_easy *data, struct connectdata *conn,
  65. char *ptr, size_t len, int *code);
  66. };
  67. #define PINGPONG_SETUP(pp,s,e) \
  68. do { \
  69. pp->response_time = RESP_TIMEOUT; \
  70. pp->statemachine = s; \
  71. pp->endofresp = e; \
  72. } while(0)
  73. /*
  74. * Curl_pp_statemach()
  75. *
  76. * called repeatedly until done. Set 'wait' to make it wait a while on the
  77. * socket if there is no traffic.
  78. */
  79. CURLcode Curl_pp_statemach(struct Curl_easy *data, struct pingpong *pp,
  80. bool block, bool disconnecting);
  81. /* initialize stuff to prepare for reading a fresh new response */
  82. void Curl_pp_init(struct pingpong *pp);
  83. /* Returns timeout in ms. 0 or negative number means the timeout has already
  84. triggered */
  85. timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
  86. struct pingpong *pp, bool disconnecting);
  87. /***********************************************************************
  88. *
  89. * Curl_pp_sendf()
  90. *
  91. * Send the formatted string as a command to a pingpong server. Note that
  92. * the string should not have any CRLF appended, as this function will
  93. * append the necessary things itself.
  94. *
  95. * made to never block
  96. */
  97. CURLcode Curl_pp_sendf(struct Curl_easy *data,
  98. struct pingpong *pp,
  99. const char *fmt, ...) CURL_PRINTF(3, 4);
  100. /***********************************************************************
  101. *
  102. * Curl_pp_vsendf()
  103. *
  104. * Send the formatted string as a command to a pingpong server. Note that
  105. * the string should not have any CRLF appended, as this function will
  106. * append the necessary things itself.
  107. *
  108. * made to never block
  109. */
  110. CURLcode Curl_pp_vsendf(struct Curl_easy *data,
  111. struct pingpong *pp,
  112. const char *fmt,
  113. va_list args) CURL_PRINTF(3, 0);
  114. /*
  115. * Curl_pp_readresp()
  116. *
  117. * Reads a piece of a server response.
  118. */
  119. CURLcode Curl_pp_readresp(struct Curl_easy *data,
  120. int sockindex,
  121. struct pingpong *pp,
  122. int *code, /* return the server code if done */
  123. size_t *size); /* size of the response */
  124. bool Curl_pp_needs_flush(struct Curl_easy *data,
  125. struct pingpong *pp);
  126. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  127. struct pingpong *pp);
  128. /* call this when a pingpong connection is disconnected */
  129. CURLcode Curl_pp_disconnect(struct pingpong *pp);
  130. int Curl_pp_getsock(struct Curl_easy *data, struct pingpong *pp,
  131. curl_socket_t *socks);
  132. /***********************************************************************
  133. *
  134. * Curl_pp_moredata()
  135. *
  136. * Returns whether there are still more data in the cache and so a call
  137. * to Curl_pp_readresp() will not block.
  138. */
  139. bool Curl_pp_moredata(struct pingpong *pp);
  140. #endif /* HEADER_CURL_PINGPONG_H */