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