Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | #ifndef HEADER_CURL_PINGPONG_H |
| 2 | #define HEADER_CURL_PINGPONG_H |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 3 | /*************************************************************************** |
| 4 | * _ _ ____ _ |
| 5 | * Project ___| | | | _ \| | |
| 6 | * / __| | | | |_) | | |
| 7 | * | (__| |_| | _ <| |___ |
| 8 | * \___|\___/|_| \_\_____| |
| 9 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 10 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 14 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 25 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 26 | |
| 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 | |
| 32 | /* forward-declaration, this is defined in urldata.h */ |
| 33 | struct connectdata; |
| 34 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 35 | typedef enum { |
| 36 | FTPTRANSFER_BODY, /* yes do transfer a body */ |
| 37 | FTPTRANSFER_INFO, /* do still go through to get info/headers */ |
| 38 | FTPTRANSFER_NONE, /* don't get anything and don't get info */ |
| 39 | FTPTRANSFER_LAST /* end of list marker, never used */ |
| 40 | } curl_pp_transfer; |
| 41 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 42 | /* |
| 43 | * 'pingpong' is the generic struct used for protocols doing server<->client |
| 44 | * conversations in a back-and-forth style such as FTP, IMAP, POP3, SMTP etc. |
| 45 | * |
| 46 | * It holds response cache and non-blocking sending data. |
| 47 | */ |
| 48 | struct pingpong { |
| 49 | char *cache; /* data cache between getresponse()-calls */ |
| 50 | size_t cache_size; /* size of cache in bytes */ |
| 51 | size_t nread_resp; /* number of bytes currently read of a server response */ |
| 52 | char *linestart_resp; /* line start pointer for the server response |
| 53 | reader function */ |
| 54 | bool pending_resp; /* set TRUE when a server response is pending or in |
| 55 | progress, and is cleared once the last response is |
| 56 | read */ |
| 57 | char *sendthis; /* allocated pointer to a buffer that is to be sent to the |
| 58 | server */ |
| 59 | size_t sendleft; /* number of bytes left to send from the sendthis buffer */ |
| 60 | size_t sendsize; /* total size of the sendthis buffer */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 61 | struct curltime response; /* set to Curl_now() when a command has been sent |
| 62 | off, used to time-out response reading */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 63 | long response_time; /* When no timeout is given, this is the amount of |
Lucas Eckels | 9bd90e6 | 2012-08-06 15:07:02 -0700 | [diff] [blame] | 64 | milliseconds we await for a server response. */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 65 | |
| 66 | struct connectdata *conn; /* points to the connectdata struct that this |
| 67 | belongs to */ |
| 68 | |
| 69 | /* Function pointers the protocols MUST implement and provide for the |
| 70 | pingpong layer to function */ |
| 71 | |
| 72 | CURLcode (*statemach_act)(struct connectdata *conn); |
| 73 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 74 | bool (*endofresp)(struct connectdata *conn, char *ptr, size_t len, |
| 75 | int *code); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | /* |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 79 | * Curl_pp_statemach() |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 80 | * |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 81 | * called repeatedly until done. Set 'wait' to make it wait a while on the |
| 82 | * socket if there's no traffic. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 83 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 84 | CURLcode Curl_pp_statemach(struct pingpong *pp, bool block); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 85 | |
| 86 | /* initialize stuff to prepare for reading a fresh new response */ |
| 87 | void Curl_pp_init(struct pingpong *pp); |
| 88 | |
| 89 | /* Returns timeout in ms. 0 or negative number means the timeout has already |
| 90 | triggered */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 91 | time_t Curl_pp_state_timeout(struct pingpong *pp); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 92 | |
| 93 | |
| 94 | /*********************************************************************** |
| 95 | * |
| 96 | * Curl_pp_sendf() |
| 97 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 98 | * Send the formatted string as a command to a pingpong server. Note that |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 99 | * the string should not have any CRLF appended, as this function will |
| 100 | * append the necessary things itself. |
| 101 | * |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 102 | * made to never block |
| 103 | */ |
| 104 | CURLcode Curl_pp_sendf(struct pingpong *pp, |
| 105 | const char *fmt, ...); |
| 106 | |
| 107 | /*********************************************************************** |
| 108 | * |
| 109 | * Curl_pp_vsendf() |
| 110 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 111 | * Send the formatted string as a command to a pingpong server. Note that |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 112 | * the string should not have any CRLF appended, as this function will |
| 113 | * append the necessary things itself. |
| 114 | * |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 115 | * made to never block |
| 116 | */ |
| 117 | CURLcode Curl_pp_vsendf(struct pingpong *pp, |
| 118 | const char *fmt, |
| 119 | va_list args); |
| 120 | |
| 121 | /* |
| 122 | * Curl_pp_readresp() |
| 123 | * |
| 124 | * Reads a piece of a server response. |
| 125 | */ |
| 126 | CURLcode Curl_pp_readresp(curl_socket_t sockfd, |
| 127 | struct pingpong *pp, |
| 128 | int *code, /* return the server code if done */ |
| 129 | size_t *size); /* size of the response */ |
| 130 | |
| 131 | |
| 132 | CURLcode Curl_pp_flushsend(struct pingpong *pp); |
| 133 | |
| 134 | /* call this when a pingpong connection is disconnected */ |
| 135 | CURLcode Curl_pp_disconnect(struct pingpong *pp); |
| 136 | |
| 137 | int Curl_pp_getsock(struct pingpong *pp, curl_socket_t *socks, |
| 138 | int numsocks); |
| 139 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 140 | |
| 141 | /*********************************************************************** |
| 142 | * |
| 143 | * Curl_pp_moredata() |
| 144 | * |
| 145 | * Returns whether there are still more data in the cache and so a call |
| 146 | * to Curl_pp_readresp() will not block. |
| 147 | */ |
| 148 | bool Curl_pp_moredata(struct pingpong *pp); |
| 149 | |
| 150 | #endif /* HEADER_CURL_PINGPONG_H */ |