af0b8e5cd8aea0484c7b55da39c96e5a4e830195
[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 target the target peer to which the stream has to be opened
109  * @param app_port the application port number which uniquely identifies this
110  *            stream
111  * @param open_cb this function will be called after stream has be established 
112  * @param open_cb_cls the closure for open_cb
113  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
114  * @return if successful it returns the stream socket; NULL if stream cannot be
115  *         opened 
116  */
117 struct GNUNET_STREAM_Socket *
118 GNUNET_STREAM_open (const struct GNUNET_PeerIdentity *target,
119                     GNUNET_MESH_ApplicationType app_port,
120                     GNUNET_STREAM_OpenCallback open_cb,
121                     void *open_cb_cls,
122                     ...);
123
124
125 /**
126  * Shutdown the stream for reading or writing (man 2 shutdown).
127  *
128  * @param socket the stream socket
129  * @param how SHUT_RD, SHUT_WR or SHUT_RDWR 
130  */
131 void
132 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
133                         int how);
134
135
136 /**
137  * Closes the stream
138  *
139  * @param socket the stream socket
140  */
141 void
142 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
143
144
145 /**
146  * Functions of this type are called upon new stream connection from other peers
147  *
148  * @param cls the closure from GNUNET_STREAM_listen
149  * @param socket the socket representing the stream
150  * @param initiator the identity of the peer who wants to establish a stream
151  *            with us
152  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
153  *             stream (the socket will be invalid after the call)
154  */
155 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
156                                              struct GNUNET_STREAM_Socket *socket,
157                                              const struct 
158                                              GNUNET_PeerIdentity *initiator);
159
160
161 /**
162  * A socket for listening.
163  */
164 struct GNUNET_STREAM_ListenSocket;
165
166 /**
167  * Listens for stream connections for a specific application ports
168  *
169  * @param app_port the application port for which new streams will be accepted
170  * @param listen_cb this function will be called when a peer tries to establish
171  *            a stream with us
172  * @param listen_cb_cls closure for listen_cb
173  * @return listen socket, NULL for any error
174  */
175 struct GNUNET_STREAM_ListenSocket *
176 GNUNET_STREAM_listen (GNUNET_MESH_ApplicationType app_port,
177                       GNUNET_STREAM_ListenCallback listen_cb,
178                       void *listen_cb_cls);
179
180
181 /**
182  * Closes the listen socket
183  *
184  * @param socket the listen socket
185  */
186 void
187 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *socket);
188
189
190 /**
191  * Functions of this signature are called whenever writing operations
192  * on a stream are executed
193  *
194  * @param cls the closure from GNUNET_STREAM_write/read
195  * @param status the status of the stream at the time this function is called
196  * @param size the number of bytes read or written
197  */
198 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
199                                                       enum GNUNET_STREAM_Status
200                                                       status,
201                                                       size_t size);
202
203
204 /**
205  * Handle to cancel IO operations.
206  */
207 struct GNUNET_STREAM_IOHandle;
208
209
210 /**
211  * Tries to write the given data to the stream
212  *
213  * @param socket the socket representing a stream
214  * @param data the data buffer from where the data is written into the stream
215  * @param size the number of bytes to be written from the data buffer
216  * @param timeout the timeout period
217  * @param write_cont the function to call upon writing some bytes into the stream
218  * @param write_cont_cls the closure
219  * @return handle to cancel the operation
220  */
221 struct GNUNET_STREAM_IOHandle *
222 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
223                      const void *data,
224                      size_t size,
225                      struct GNUNET_TIME_Relative timeout,
226                      GNUNET_STREAM_CompletionContinuation write_cont,
227                      void *write_cont_cls);
228
229
230 /**
231  * Functions of this signature are called whenever data is available from the
232  * stream.
233  *
234  * @param cls the closure from GNUNET_STREAM_write/read
235  * @param status the status of the stream at the time this function is called
236  * @param data traffic from the other side
237  * @param size the number of bytes available in data read 
238  * @return number of bytes of processed from 'data' (any data remaining should be
239  *         given to the next time the read processor is called).
240  */
241 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
242                                                enum GNUNET_STREAM_Status status,
243                                                const void *data,
244                                                size_t size);
245
246
247 /**
248  * Tries to read data from the stream
249  *
250  * @param socket the socket representing a stream
251  * @param timeout the timeout period
252  * @param proc function to call with data (once only)
253  * @param proc_cls the closure for proc
254  * @return handle to cancel the operation
255  */
256 struct GNUNET_STREAM_IOHandle *
257 GNUNET_STREAM_read (const struct GNUNET_STREAM_Socket *socket,
258                     struct GNUNET_TIME_Relative timeout,
259                     GNUNET_STREAM_DataProcessor proc,
260                     void *proc_cls);
261
262
263 /**
264  * Cancel pending read or write operation.
265  *
266  * @param ioh handle to operation to cancel
267  */
268 void
269 GNUNET_STREAM_io_cancel (struct GNUNET_STREAM_IOHandle *ioh);
270
271
272 #if 0
273 {
274 #endif
275 #ifdef __cplusplus
276 }
277 #endif
278
279 #endif  /* STREAM_PROTOCOL_H */