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