pingpong.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef HEADER_CURL_PINGPONG_H
  2. #define HEADER_CURL_PINGPONG_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2022, 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 /* don't get anything and don't 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. char *cache; /* data cache between getresponse()-calls */
  46. size_t cache_size; /* size of cache in bytes */
  47. size_t nread_resp; /* number of bytes currently read of a server response */
  48. char *linestart_resp; /* line start pointer for the server response
  49. reader function */
  50. bool pending_resp; /* set TRUE when a server response is pending or in
  51. progress, and is cleared once the last response is
  52. read */
  53. char *sendthis; /* allocated pointer to a buffer that is to be sent to the
  54. server */
  55. size_t sendleft; /* number of bytes left to send from the sendthis buffer */
  56. size_t sendsize; /* total size of the sendthis buffer */
  57. struct curltime response; /* set to Curl_now() when a command has been sent
  58. off, used to time-out response reading */
  59. timediff_t response_time; /* When no timeout is given, this is the amount of
  60. milliseconds we await for a server response. */
  61. struct dynbuf sendbuf;
  62. /* Function pointers the protocols MUST implement and provide for the
  63. pingpong layer to function */
  64. CURLcode (*statemachine)(struct Curl_easy *data, struct connectdata *conn);
  65. bool (*endofresp)(struct Curl_easy *data, struct connectdata *conn,
  66. char *ptr, size_t len, int *code);
  67. };
  68. #define PINGPONG_SETUP(pp,s,e) \
  69. do { \
  70. pp->response_time = RESP_TIMEOUT; \
  71. pp->statemachine = s; \
  72. pp->endofresp = e; \
  73. } while(0)
  74. /*
  75. * Curl_pp_statemach()
  76. *
  77. * called repeatedly until done. Set 'wait' to make it wait a while on the
  78. * socket if there's no traffic.
  79. */
  80. CURLcode Curl_pp_statemach(struct Curl_easy *data, struct pingpong *pp,
  81. bool block, bool disconnecting);
  82. /* initialize stuff to prepare for reading a fresh new response */
  83. void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp);
  84. /* setup for the transfer */
  85. void Curl_pp_setup(struct pingpong *pp);
  86. /* Returns timeout in ms. 0 or negative number means the timeout has already
  87. triggered */
  88. timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
  89. struct pingpong *pp, bool disconnecting);
  90. /***********************************************************************
  91. *
  92. * Curl_pp_sendf()
  93. *
  94. * Send the formatted string as a command to a pingpong server. Note that
  95. * the string should not have any CRLF appended, as this function will
  96. * append the necessary things itself.
  97. *
  98. * made to never block
  99. */
  100. CURLcode Curl_pp_sendf(struct Curl_easy *data,
  101. struct pingpong *pp,
  102. const char *fmt, ...);
  103. /***********************************************************************
  104. *
  105. * Curl_pp_vsendf()
  106. *
  107. * Send the formatted string as a command to a pingpong server. Note that
  108. * the string should not have any CRLF appended, as this function will
  109. * append the necessary things itself.
  110. *
  111. * made to never block
  112. */
  113. CURLcode Curl_pp_vsendf(struct Curl_easy *data,
  114. struct pingpong *pp,
  115. const char *fmt,
  116. va_list args);
  117. /*
  118. * Curl_pp_readresp()
  119. *
  120. * Reads a piece of a server response.
  121. */
  122. CURLcode Curl_pp_readresp(struct Curl_easy *data,
  123. curl_socket_t sockfd,
  124. struct pingpong *pp,
  125. int *code, /* return the server code if done */
  126. size_t *size); /* size of the response */
  127. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  128. struct pingpong *pp);
  129. /* call this when a pingpong connection is disconnected */
  130. CURLcode Curl_pp_disconnect(struct pingpong *pp);
  131. int Curl_pp_getsock(struct Curl_easy *data, struct pingpong *pp,
  132. curl_socket_t *socks);
  133. /***********************************************************************
  134. *
  135. * Curl_pp_moredata()
  136. *
  137. * Returns whether there are still more data in the cache and so a call
  138. * to Curl_pp_readresp() will not block.
  139. */
  140. bool Curl_pp_moredata(struct pingpong *pp);
  141. #endif /* HEADER_CURL_PINGPONG_H */