• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 #include "curl_setup.h"
28 
29 #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_FTP) || \
30   !defined(CURL_DISABLE_POP3) || !defined(CURL_DISABLE_SMTP)
31 #define USE_PINGPONG
32 #endif
33 
34 /* forward-declaration, this is defined in urldata.h */
35 struct connectdata;
36 
37 typedef enum {
38   PPTRANSFER_BODY, /* yes do transfer a body */
39   PPTRANSFER_INFO, /* do still go through to get info/headers */
40   PPTRANSFER_NONE  /* don't get anything and don't get info */
41 } curl_pp_transfer;
42 
43 /*
44  * 'pingpong' is the generic struct used for protocols doing server<->client
45  * conversations in a back-and-forth style such as FTP, IMAP, POP3, SMTP etc.
46  *
47  * It holds response cache and non-blocking sending data.
48  */
49 struct pingpong {
50   size_t nread_resp;  /* number of bytes currently read of a server response */
51   bool pending_resp;  /* set TRUE when a server response is pending or in
52                          progress, and is cleared once the last response is
53                          read */
54   char *sendthis; /* pointer to a buffer that is to be sent to the 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   struct dynbuf recvbuf;
63   size_t overflow; /* number of bytes left after a final response line */
64   size_t nfinal;   /* number of bytes in the final response line, which
65                       after a match is first in the receice buffer */
66 
67   /* Function pointers the protocols MUST implement and provide for the
68      pingpong layer to function */
69 
70   CURLcode (*statemachine)(struct Curl_easy *data, struct connectdata *conn);
71   bool (*endofresp)(struct Curl_easy *data, struct connectdata *conn,
72                     char *ptr, size_t len, int *code);
73 };
74 
75 #define PINGPONG_SETUP(pp,s,e)                   \
76   do {                                           \
77     pp->response_time = RESP_TIMEOUT;            \
78     pp->statemachine = s;                        \
79     pp->endofresp = e;                           \
80   } while(0)
81 
82 /*
83  * Curl_pp_statemach()
84  *
85  * called repeatedly until done. Set 'wait' to make it wait a while on the
86  * socket if there's no traffic.
87  */
88 CURLcode Curl_pp_statemach(struct Curl_easy *data, struct pingpong *pp,
89                            bool block, bool disconnecting);
90 
91 /* initialize stuff to prepare for reading a fresh new response */
92 void Curl_pp_init(struct pingpong *pp);
93 
94 /* Returns timeout in ms. 0 or negative number means the timeout has already
95    triggered */
96 timediff_t Curl_pp_state_timeout(struct Curl_easy *data,
97                                  struct pingpong *pp, bool disconnecting);
98 
99 
100 /***********************************************************************
101  *
102  * Curl_pp_sendf()
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_sendf(struct Curl_easy *data,
111                        struct pingpong *pp,
112                        const char *fmt, ...) CURL_PRINTF(3, 4);
113 
114 /***********************************************************************
115  *
116  * Curl_pp_vsendf()
117  *
118  * Send the formatted string as a command to a pingpong server. Note that
119  * the string should not have any CRLF appended, as this function will
120  * append the necessary things itself.
121  *
122  * made to never block
123  */
124 CURLcode Curl_pp_vsendf(struct Curl_easy *data,
125                         struct pingpong *pp,
126                         const char *fmt,
127                         va_list args) CURL_PRINTF(3, 0);
128 
129 /*
130  * Curl_pp_readresp()
131  *
132  * Reads a piece of a server response.
133  */
134 CURLcode Curl_pp_readresp(struct Curl_easy *data,
135                           curl_socket_t sockfd,
136                           struct pingpong *pp,
137                           int *code, /* return the server code if done */
138                           size_t *size); /* size of the response */
139 
140 
141 CURLcode Curl_pp_flushsend(struct Curl_easy *data,
142                            struct pingpong *pp);
143 
144 /* call this when a pingpong connection is disconnected */
145 CURLcode Curl_pp_disconnect(struct pingpong *pp);
146 
147 int Curl_pp_getsock(struct Curl_easy *data, struct pingpong *pp,
148                     curl_socket_t *socks);
149 
150 
151 /***********************************************************************
152  *
153  * Curl_pp_moredata()
154  *
155  * Returns whether there are still more data in the cache and so a call
156  * to Curl_pp_readresp() will not block.
157  */
158 bool Curl_pp_moredata(struct pingpong *pp);
159 
160 #endif /* HEADER_CURL_PINGPONG_H */
161