-control retransmission for HELLO and HELLO_ACK
[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  * Callback for signalling stream listen success; See
90  * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
91  */
92 typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void);
93
94
95 /**
96  * Options for the stream.
97  */
98 enum GNUNET_STREAM_Option
99   {
100     /**
101      * End of the option list.
102      */
103     GNUNET_STREAM_OPTION_END = 0,
104
105     /**
106      * Option to set the initial retransmission timeout (when do we retransmit
107      * a packet that did not yield an acknowledgement for the first time?).  
108      * Repeated retransmissions will then use an exponential back-off.
109      * Takes a 'struct GNUNET_TIME_Relative' as the only argument.  A value
110      * of '0' means to use the round-trip time (plus a tiny grace period);
111      * this is also the default.
112      */
113     GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
114
115     /**
116      * Option to set the write sequence number. Takes a uint32_t as parameter
117      * to set the value of the write sequence number
118      */
119     GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
120
121     /**
122      * Listen socket timeout in milliseconds given as uint32_t
123      */
124     GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
125
126     /**
127      * Option to register a callback when stream listening is
128      * successfull. Takes parameter of the form
129      * GNUNET_STREAM_ListenSuccessCallback. The callback is only called if
130      * listen is successful
131      */
132     GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
133   };
134
135
136 /**
137  * Tries to open a stream to the target peer
138  *
139  * @param cfg configuration to use
140  * @param target the target peer to which the stream has to be opened
141  * @param app_port the application port number which uniquely identifies this
142  *            stream
143  * @param open_cb this function will be called after stream has be established;
144  *          cannot be NULL
145  * @param open_cb_cls the closure for open_cb
146  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
147  * @return if successful it returns the stream socket; NULL if stream cannot be
148  *         opened 
149  */
150 struct GNUNET_STREAM_Socket *
151 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
152                     const struct GNUNET_PeerIdentity *target,
153                     GNUNET_MESH_ApplicationType app_port,
154                     GNUNET_STREAM_OpenCallback open_cb,
155                     void *open_cb_cls,
156                     ...);
157
158
159 /**
160  * Handle for shutdown
161  */
162 struct GNUNET_STREAM_ShutdownHandle;
163
164
165 /**
166  * Completion callback for shutdown
167  *
168  * @param cls the closure from GNUNET_STREAM_shutdown call
169  * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
170  *          SHUT_RDWR) 
171  */
172 typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
173                                                   int operation);
174
175
176 /**
177  * Shutdown the stream for reading or writing (similar to man 2 shutdown).
178  *
179  * @param socket the stream socket
180  * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
181  * @param completion_cb the callback that will be called upon successful
182  *          shutdown of given operation
183  * @param completion_cls the closure for the completion callback
184  * @return the shutdown handle; NULL in case of any error
185  */
186 struct GNUNET_STREAM_ShutdownHandle *
187 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
188                         int operation,
189                         GNUNET_STREAM_ShutdownCompletion completion_cb,
190                         void *completion_cls);
191
192
193 /**
194  * Cancels a pending shutdown
195  *
196  * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
197  */
198 void
199 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
200
201
202 /**
203  * Closes the stream and frees the associated state. The stream should be
204  * shutdown before closing.
205  *
206  * @param socket the stream socket
207  */
208 void
209 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
210
211
212 /**
213  * Functions of this type are called upon new stream connection from other peers
214  *
215  * @param cls the closure from GNUNET_STREAM_listen
216  * @param socket the socket representing the stream; NULL on binding error
217  * @param initiator the identity of the peer who wants to establish a stream
218  *            with us; NULL on binding error
219  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
220  *             stream (the socket will be invalid after the call)
221  */
222 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
223                                              struct GNUNET_STREAM_Socket *socket,
224                                              const struct 
225                                              GNUNET_PeerIdentity *initiator);
226
227
228 /**
229  * A socket for listening.
230  */
231 struct GNUNET_STREAM_ListenSocket;
232
233 /**
234  * Listens for stream connections for a specific application ports
235  *
236  * @param cfg the configuration to use
237  *
238  * @param app_port the application port for which new streams will be
239  *         accepted. If another stream is listening on the same port the
240  *         listen_cb will be called to signal binding error and the returned
241  *         ListenSocket will be invalidated.
242  *
243  * @param listen_cb this function will be called when a peer tries to establish
244  *            a stream with us
245  * @param listen_cb_cls closure for listen_cb
246  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
247  * @return listen socket, NULL for any error
248  */
249 struct GNUNET_STREAM_ListenSocket *
250 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
251                       GNUNET_MESH_ApplicationType app_port,
252                       GNUNET_STREAM_ListenCallback listen_cb,
253                       void *listen_cb_cls,
254                       ...);
255
256
257 /**
258  * Closes the listen socket
259  *
260  * @param lsocket the listen socket
261  */
262 void
263 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
264
265
266 /**
267  * Functions of this signature are called whenever writing operations
268  * on a stream are executed
269  *
270  * @param cls the closure from GNUNET_STREAM_write
271  * @param status the status of the stream at the time this function is called
272  * @param size the number of bytes written
273  */
274 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
275                                                       enum GNUNET_STREAM_Status
276                                                       status,
277                                                       size_t size);
278
279
280 /**
281  * Handle to cancel IO write operations.
282  */
283 struct GNUNET_STREAM_IOWriteHandle;
284
285
286 /**
287  * Handle to cancel IO read operations.
288  */
289 struct GNUNET_STREAM_IOReadHandle;
290
291 /**
292  * Tries to write the given data to the stream. The maximum size of data that
293  * can be written as part of a write operation is (64 * (64000 - sizeof (struct
294  * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
295  * violation, however only the said number of maximum bytes will be written.
296  *
297  * @param socket the socket representing a stream
298  * @param data the data buffer from where the data is written into the stream
299  * @param size the number of bytes to be written from the data buffer
300  * @param timeout the timeout period
301  * @param write_cont the function to call upon writing some bytes into the
302  *          stream 
303  * @param write_cont_cls the closure
304  *
305  * @return handle to cancel the operation; if a previous write is pending or
306  *           the stream has been shutdown for this operation then write_cont is
307  *           immediately called and NULL is returned.
308  */
309 struct GNUNET_STREAM_IOWriteHandle *
310 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
311                      const void *data,
312                      size_t size,
313                      struct GNUNET_TIME_Relative timeout,
314                      GNUNET_STREAM_CompletionContinuation write_cont,
315                      void *write_cont_cls);
316
317
318 /**
319  * Functions of this signature are called whenever data is available from the
320  * stream.
321  *
322  * @param cls the closure from GNUNET_STREAM_read
323  * @param status the status of the stream at the time this function is called
324  * @param data traffic from the other side
325  * @param size the number of bytes available in data read; will be 0 on timeout 
326  * @return number of bytes of processed from 'data' (any data remaining should be
327  *         given to the next time the read processor is called).
328  */
329 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
330                                                enum GNUNET_STREAM_Status status,
331                                                const void *data,
332                                                size_t size);
333
334
335 /**
336  * Tries to read data from the stream.
337  *
338  * @param socket the socket representing a stream
339  * @param timeout the timeout period
340  * @param proc function to call with data (once only)
341  * @param proc_cls the closure for proc
342  *
343  * @return handle to cancel the operation; if the stream has been shutdown for
344  *           this type of opeartion then the DataProcessor is immediately
345  *           called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
346  */
347 struct GNUNET_STREAM_IOReadHandle *
348 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
349                     struct GNUNET_TIME_Relative timeout,
350                     GNUNET_STREAM_DataProcessor proc,
351                     void *proc_cls);
352
353
354 /**
355  * Cancels pending write operation. Also cancels packet retransmissions which
356  * may have resulted otherwise.
357  *
358  * CAUTION: Normally a write operation is considered successful if the data
359  * given to it is sent and acknowledged by the receiver. As data is divided
360  * into packets, it is possible that not all packets are received by the
361  * receiver. Any missing packets are then retransmitted till the receiver
362  * acknowledges all packets or until a timeout . During this scenario if the
363  * write operation is cancelled all such retransmissions are also
364  * cancelled. This may leave the receiver's receive buffer incompletely filled
365  * as some missing packets are never retransmitted. So this operation should be
366  * used before shutting down transmission from our side or before closing the
367  * socket.
368  *
369  * @param ioh handle to operation to cancel
370  */
371 void
372 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
373
374
375 /**
376  * Cancel pending read operation.
377  *
378  * @param ioh handle to operation to cancel
379  */
380 void
381 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
382
383
384 #if 0
385 {
386 #endif
387 #ifdef __cplusplus
388 }
389 #endif
390
391 #endif  /* STREAM_PROTOCOL_H */