pingpong.h 5.9 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) 1998 - 2021, 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. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_FTP) || \
  26. !defined(CURL_DISABLE_POP3) || !defined(CURL_DISABLE_SMTP)
  27. #define USE_PINGPONG
  28. #endif
  29. /* forward-declaration, this is defined in urldata.h */
  30. struct connectdata;
  31. typedef enum {
  32. PPTRANSFER_BODY, /* yes do transfer a body */
  33. PPTRANSFER_INFO, /* do still go through to get info/headers */
  34. PPTRANSFER_NONE /* don't get anything and don't get info */
  35. } curl_pp_transfer;
  36. /*
  37. * 'pingpong' is the generic struct used for protocols doing server<->client
  38. * conversations in a back-and-forth style such as FTP, IMAP, POP3, SMTP etc.
  39. *
  40. * It holds response cache and non-blocking sending data.
  41. */
  42. struct pingpong {
  43. char *cache; /* data cache between getresponse()-calls */
  44. size_t cache_size; /* size of cache in bytes */
  45. size_t nread_resp; /* number of bytes currently read of a server response */
  46. char *linestart_resp; /* line start pointer for the server response
  47. reader function */
  48. bool pending_resp; /* set TRUE when a server response is pending or in
  49. progress, and is cleared once the last response is
  50. read */
  51. char *sendthis; /* allocated pointer to a buffer that is to be sent to the
  52. server */
  53. size_t sendleft; /* number of bytes left to send from the sendthis buffer */
  54. size_t sendsize; /* total size of the sendthis buffer */
  55. struct curltime response; /* set to Curl_now() when a command has been sent
  56. off, used to time-out response reading */
  57. timediff_t response_time; /* When no timeout is given, this is the amount of
  58. milliseconds we await for a server response. */
  59. struct dynbuf sendbuf;
  60. /* Function pointers the protocols MUST implement and provide for the
  61. pingpong layer to function */
  62. CURLcode (*statemachine)(struct Curl_easy *data, struct connectdata *conn);
  63. bool (*endofresp)(struct Curl_easy *data, struct connectdata *conn,
  64. char *ptr, size_t len, int *code);
  65. };
  66. #define PINGPONG_SETUP(pp,s,e) \
  67. do { \
  68. pp->response_time = RESP_TIMEOUT; \
  69. pp->statemachine = s; \
  70. pp->endofresp = e; \
  71. } while(0)
  72. /*
  73. * Curl_pp_statemach()
  74. *
  75. * called repeatedly until done. Set 'wait' to make it wait a while on the
  76. * socket if there's no traffic.
  77. */
  78. CURLcode Curl_pp_statemach(struct Curl_easy *data, struct pingpong *pp,
  79. bool block, bool disconnecting);
  80. /* initialize stuff to prepare for reading a fresh new response */
  81. void Curl_pp_init(struct Curl_easy *data, struct pingpong *pp);
  82. /* setup for the transfer */
  83. void Curl_pp_setup(struct pingpong *pp);
  84. /* Returns timeout in ms. 0 or negative number means the timeout has already
  85. triggered */
  86. timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
  87. struct pingpong *pp, bool disconnecting);
  88. /***********************************************************************
  89. *
  90. * Curl_pp_sendf()
  91. *
  92. * Send the formatted string as a command to a pingpong server. Note that
  93. * the string should not have any CRLF appended, as this function will
  94. * append the necessary things itself.
  95. *
  96. * made to never block
  97. */
  98. CURLcode Curl_pp_sendf(struct Curl_easy *data,
  99. struct pingpong *pp,
  100. const char *fmt, ...);
  101. /***********************************************************************
  102. *
  103. * Curl_pp_vsendf()
  104. *
  105. * Send the formatted string as a command to a pingpong server. Note that
  106. * the string should not have any CRLF appended, as this function will
  107. * append the necessary things itself.
  108. *
  109. * made to never block
  110. */
  111. CURLcode Curl_pp_vsendf(struct Curl_easy *data,
  112. struct pingpong *pp,
  113. const char *fmt,
  114. va_list args);
  115. /*
  116. * Curl_pp_readresp()
  117. *
  118. * Reads a piece of a server response.
  119. */
  120. CURLcode Curl_pp_readresp(struct Curl_easy *data,
  121. curl_socket_t sockfd,
  122. struct pingpong *pp,
  123. int *code, /* return the server code if done */
  124. size_t *size); /* size of the response */
  125. CURLcode Curl_pp_flushsend(struct Curl_easy *data,
  126. struct pingpong *pp);
  127. /* call this when a pingpong connection is disconnected */
  128. CURLcode Curl_pp_disconnect(struct pingpong *pp);
  129. int Curl_pp_getsock(struct Curl_easy *data, struct pingpong *pp,
  130. curl_socket_t *socks);
  131. /***********************************************************************
  132. *
  133. * Curl_pp_moredata()
  134. *
  135. * Returns whether there are still more data in the cache and so a call
  136. * to Curl_pp_readresp() will not block.
  137. */
  138. bool Curl_pp_moredata(struct pingpong *pp);
  139. #endif /* HEADER_CURL_PINGPONG_H */