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