-stream listen timeout option
[oweals/gnunet.git] / src / include / gnunet_stream_lib.h
1 /*
2      This file is part of GNUnet.
3      (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file include/gnunet_stream_lib.h
23  * @brief stream handling using mesh API
24  * @author Sree Harsha Totakura
25  */
26
27 #ifndef GNUNET_STREAM_LIB_H
28 #define GNUNET_STREAM_LIB_H
29
30 #ifdef __cplusplus
31 extern "C" 
32 {
33 #if 0
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_mesh_service.h"
40
41 /**
42  * Stream status 
43  */
44 enum GNUNET_STREAM_Status
45   {
46     /**
47      * All previous read/write operations are successfully done
48      */
49     GNUNET_STREAM_OK = 0,
50
51     /**
52      * A timeout occured while reading/writing the stream
53      */
54     GNUNET_STREAM_TIMEOUT = 1,
55
56     /**
57      * Other side has shutdown the socket for this type of operation
58      * (reading/writing)
59      */
60     GNUNET_STREAM_SHUTDOWN = 2,
61
62     /**
63      * A serious error occured while operating on this stream
64      */
65     GNUNET_STREAM_SYSERR = 3,
66     
67     /**
68      * An error resulted in an unusable stream
69      */
70     GNUNET_STREAM_BROKEN
71   };
72
73 /**
74  * Opaque handler for stream
75  */
76 struct GNUNET_STREAM_Socket;
77
78 /**
79  * Functions of this type will be called when a stream is established
80  *
81  * @param cls the closure from GNUNET_STREAM_open
82  * @param socket socket to use to communicate with the other side (read/write)
83  */
84 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
85                                             struct GNUNET_STREAM_Socket *socket);
86
87
88 /**
89  * Options for the stream.
90  */
91 enum GNUNET_STREAM_Option
92   {
93     /**
94      * End of the option list.
95      */
96     GNUNET_STREAM_OPTION_END = 0,
97
98     /**
99      * Option to set the initial retransmission timeout (when do we retransmit
100      * a packet that did not yield an acknowledgement for the first time?).  
101      * Repeated retransmissions will then use an exponential back-off.
102      * Takes a 'struct GNUNET_TIME_Relative' as the only argument.  A value
103      * of '0' means to use the round-trip time (plus a tiny grace period);
104      * this is also the default.
105      */
106     GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
107
108     /**
109      * Option to set the write sequence number. Takes a uint32_t as parameter
110      * to set the value of the write sequence number
111      */
112     GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
113
114     /**
115      * Listen socket timeout in milliseconds
116      */
117     GNUNET_STREAM_OPTION_LISTEN_TIMEOUT
118   };
119
120
121 /**
122  * Tries to open a stream to the target peer
123  *
124  * @param cfg configuration to use
125  * @param target the target peer to which the stream has to be opened
126  * @param app_port the application port number which uniquely identifies this
127  *            stream
128  * @param open_cb this function will be called after stream has be established 
129  * @param open_cb_cls the closure for open_cb
130  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
131  * @return if successful it returns the stream socket; NULL if stream cannot be
132  *         opened 
133  */
134 struct GNUNET_STREAM_Socket *
135 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
136                     const struct GNUNET_PeerIdentity *target,
137                     GNUNET_MESH_ApplicationType app_port,
138                     GNUNET_STREAM_OpenCallback open_cb,
139                     void *open_cb_cls,
140                     ...);
141
142
143 /**
144  * Handle for shutdown
145  */
146 struct GNUNET_STREAM_ShutdownHandle;
147
148
149 /**
150  * Completion callback for shutdown
151  *
152  * @param cls the closure from GNUNET_STREAM_shutdown call
153  * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
154  *          SHUT_RDWR) 
155  */
156 typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
157                                                   int operation);
158
159
160 /**
161  * Shutdown the stream for reading or writing (similar to man 2 shutdown).
162  *
163  * @param socket the stream socket
164  * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
165  * @param completion_cb the callback that will be called upon successful
166  *          shutdown of given operation
167  * @param completion_cls the closure for the completion callback
168  * @return the shutdown handle; NULL in case of any error
169  */
170 struct GNUNET_STREAM_ShutdownHandle *
171 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
172                         int operation,
173                         GNUNET_STREAM_ShutdownCompletion completion_cb,
174                         void *completion_cls);
175
176
177 /**
178  * Cancels a pending shutdown
179  *
180  * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
181  */
182 void
183 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
184
185
186 /**
187  * Closes the stream and frees the associated state. The stream should be
188  * shutdown before closing.
189  *
190  * @param socket the stream socket
191  */
192 void
193 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
194
195
196 /**
197  * Functions of this type are called upon new stream connection from other peers
198  *
199  * @param cls the closure from GNUNET_STREAM_listen
200  * @param socket the socket representing the stream; NULL on binding error
201  * @param initiator the identity of the peer who wants to establish a stream
202  *            with us; NULL on binding error
203  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
204  *             stream (the socket will be invalid after the call)
205  */
206 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
207                                              struct GNUNET_STREAM_Socket *socket,
208                                              const struct 
209                                              GNUNET_PeerIdentity *initiator);
210
211
212 /**
213  * A socket for listening.
214  */
215 struct GNUNET_STREAM_ListenSocket;
216
217 /**
218  * Listens for stream connections for a specific application ports
219  *
220  * @param cfg the configuration to use
221  *
222  * @param app_port the application port for which new streams will be
223  *         accepted. If another stream is listening on the same port the
224  *         listen_cb will be called to signal binding error and the returned
225  *         ListenSocket will be invalidated.
226  *
227  * @param listen_cb this function will be called when a peer tries to establish
228  *            a stream with us
229  * @param listen_cb_cls closure for listen_cb
230  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
231  * @return listen socket, NULL for any error
232  */
233 struct GNUNET_STREAM_ListenSocket *
234 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
235                       GNUNET_MESH_ApplicationType app_port,
236                       GNUNET_STREAM_ListenCallback listen_cb,
237                       void *listen_cb_cls,
238                       ...);
239
240
241 /**
242  * Closes the listen socket
243  *
244  * @param lsocket the listen socket
245  */
246 void
247 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
248
249
250 /**
251  * Functions of this signature are called whenever writing operations
252  * on a stream are executed
253  *
254  * @param cls the closure from GNUNET_STREAM_write
255  * @param status the status of the stream at the time this function is called
256  * @param size the number of bytes written
257  */
258 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
259                                                       enum GNUNET_STREAM_Status
260                                                       status,
261                                                       size_t size);
262
263
264 /**
265  * Handle to cancel IO write operations.
266  */
267 struct GNUNET_STREAM_IOWriteHandle;
268
269
270 /**
271  * Handle to cancel IO read operations.
272  */
273 struct GNUNET_STREAM_IOReadHandle;
274
275 /**
276  * Tries to write the given data to the stream. The maximum size of data that
277  * can be written as part of a write operation is (64 * (64000 - sizeof (struct
278  * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
279  * violation, however only the said number of maximum bytes will be written.
280  *
281  * @param socket the socket representing a stream
282  * @param data the data buffer from where the data is written into the stream
283  * @param size the number of bytes to be written from the data buffer
284  * @param timeout the timeout period
285  * @param write_cont the function to call upon writing some bytes into the
286  *          stream 
287  * @param write_cont_cls the closure
288  *
289  * @return handle to cancel the operation; if a previous write is pending or
290  *           the stream has been shutdown for this operation then write_cont is
291  *           immediately called and NULL is returned.
292  */
293 struct GNUNET_STREAM_IOWriteHandle *
294 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
295                      const void *data,
296                      size_t size,
297                      struct GNUNET_TIME_Relative timeout,
298                      GNUNET_STREAM_CompletionContinuation write_cont,
299                      void *write_cont_cls);
300
301
302 /**
303  * Functions of this signature are called whenever data is available from the
304  * stream.
305  *
306  * @param cls the closure from GNUNET_STREAM_read
307  * @param status the status of the stream at the time this function is called
308  * @param data traffic from the other side
309  * @param size the number of bytes available in data read; will be 0 on timeout 
310  * @return number of bytes of processed from 'data' (any data remaining should be
311  *         given to the next time the read processor is called).
312  */
313 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
314                                                enum GNUNET_STREAM_Status status,
315                                                const void *data,
316                                                size_t size);
317
318
319 /**
320  * Tries to read data from the stream.
321  *
322  * @param socket the socket representing a stream
323  * @param timeout the timeout period
324  * @param proc function to call with data (once only)
325  * @param proc_cls the closure for proc
326  *
327  * @return handle to cancel the operation; if the stream has been shutdown for
328  *           this type of opeartion then the DataProcessor is immediately
329  *           called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
330  */
331 struct GNUNET_STREAM_IOReadHandle *
332 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
333                     struct GNUNET_TIME_Relative timeout,
334                     GNUNET_STREAM_DataProcessor proc,
335                     void *proc_cls);
336
337
338 /**
339  * Cancels pending write operation. Also cancels packet retransmissions which
340  * may have resulted otherwise.
341  *
342  * CAUTION: Normally a write operation is considered successful if the data
343  * given to it is sent and acknowledged by the receiver. As data is divided
344  * into packets, it is possible that not all packets are received by the
345  * receiver. Any missing packets are then retransmitted till the receiver
346  * acknowledges all packets or until a timeout . During this scenario if the
347  * write operation is cancelled all such retransmissions are also
348  * cancelled. This may leave the receiver's receive buffer incompletely filled
349  * as some missing packets are never retransmitted. So this operation should be
350  * used before shutting down transmission from our side or before closing the
351  * socket.
352  *
353  * @param ioh handle to operation to cancel
354  */
355 void
356 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
357
358
359 /**
360  * Cancel pending read operation.
361  *
362  * @param ioh handle to operation to cancel
363  */
364 void
365 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
366
367
368 #if 0
369 {
370 #endif
371 #ifdef __cplusplus
372 }
373 #endif
374
375 #endif  /* STREAM_PROTOCOL_H */