-ensure labels are less than 64 chars, add test for full DNS names
[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      * FIXME: status code unused?
70      */
71     GNUNET_STREAM_BROKEN
72   };
73
74 /**
75  * Opaque handler for stream
76  */
77 struct GNUNET_STREAM_Socket;
78
79 /**
80  * Functions of this type will be called when a stream is established
81  *
82  * @param cls the closure from GNUNET_STREAM_open
83  * @param socket socket to use to communicate with the other side (read/write)
84  */
85 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
86                                             struct GNUNET_STREAM_Socket *socket);
87
88
89 /**
90  * Callback for signalling stream listen success; See
91  * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
92  */
93 typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void);
94
95
96 /**
97  * Options for the stream.
98  */
99 enum GNUNET_STREAM_Option
100   {
101     /**
102      * End of the option list.
103      */
104     GNUNET_STREAM_OPTION_END = 0,
105
106     /**
107      * Option to set the initial retransmission timeout (when do we retransmit
108      * a packet that did not yield an acknowledgement for the first time?).  
109      * Repeated retransmissions will then use an exponential back-off.
110      * Takes a 'struct GNUNET_TIME_Relative' as the only argument.  A value
111      * of '0' means to use the round-trip time (plus a tiny grace period);
112      * this is also the default.
113      */
114     GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
115
116     /**
117      * Option to set the write sequence number. Takes a uint32_t as parameter
118      * to set the value of the write sequence number
119      */
120     GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
121
122     /**
123      * Listen socket timeout in milliseconds given as uint32_t
124      */
125     GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
126
127     /**
128      * Option to register a callback when stream listening is successfull. Takes
129      * parameter of the form GNUNET_STREAM_ListenSuccessCallback. The callback
130      * is only called if listening 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. Note that the shutdown messages may already
202  * be sent and the stream is shutdown already for the operation given to
203  * GNUNET_STREAM_shutdown(). This function only clears up any retranmissions of
204  * shutdown messages and frees the shutdown handle.
205  *
206  * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
207  */
208 void
209 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
210
211
212 /**
213  * Closes the stream and frees the associated state. The stream should be
214  * shutdown for both reading and writing before closing.
215  *
216  * @param socket the stream socket
217  */
218 void
219 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
220
221
222 /**
223  * Functions of this type are called upon new stream connection from other peers
224  * or upon binding error which happen when the app_port given in
225  * GNUNET_STREAM_listen() is already taken.
226  *
227  * @param cls the closure from GNUNET_STREAM_listen
228  * @param socket the socket representing the stream; NULL on binding error
229  * @param initiator the identity of the peer who wants to establish a stream
230  *            with us; NULL on binding error
231  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
232  *             stream (the socket will be invalid after the call)
233  */
234 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
235                                              struct GNUNET_STREAM_Socket *socket,
236                                              const struct 
237                                              GNUNET_PeerIdentity *initiator);
238
239
240 /**
241  * A socket for listening.
242  */
243 struct GNUNET_STREAM_ListenSocket;
244
245 /**
246  * Listens for stream connections for a specific application ports
247  *
248  * @param cfg the configuration to use
249  * @param app_port the application port for which new streams will be
250  *         accepted. If another stream is listening on the same port the
251  *         listen_cb will be called to signal binding error and the returned
252  *         ListenSocket will be invalidated.
253  * @param listen_cb this function will be called when a peer tries to establish
254  *            a stream with us
255  * @param listen_cb_cls closure for listen_cb
256  * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
257  * @return listen socket, NULL for any error
258  */
259 struct GNUNET_STREAM_ListenSocket *
260 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
261                       GNUNET_MESH_ApplicationType app_port,
262                       GNUNET_STREAM_ListenCallback listen_cb,
263                       void *listen_cb_cls,
264                       ...);
265
266
267 /**
268  * Closes the listen socket
269  *
270  * @param lsocket the listen socket
271  */
272 void
273 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
274
275
276 /**
277  * Functions of this signature are called whenever writing operations
278  * on a stream are executed
279  *
280  * @param cls the closure from GNUNET_STREAM_write
281  * @param status the status of the stream at the time this function is called;
282  *          GNUNET_OK if writing to stream was completed successfully,
283  *          GNUNET_STREAM_SHUTDOWN if the stream is shutdown for writing in the
284  *          mean time.
285  * @param size the number of bytes written
286  */
287 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
288                                                       enum GNUNET_STREAM_Status
289                                                       status,
290                                                       size_t size);
291
292
293 /**
294  * Handle to cancel IO write operations.
295  */
296 struct GNUNET_STREAM_IOWriteHandle;
297
298
299 /**
300  * Handle to cancel IO read operations.
301  */
302 struct GNUNET_STREAM_IOReadHandle;
303
304 /**
305  * Tries to write the given data to the stream. The maximum size of data that
306  * can be written as part of a write operation is (64 * (64000 - sizeof (struct
307  * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
308  * violation, however only the said number of maximum bytes will be written.
309  *
310  * @param socket the socket representing a stream
311  * @param data the data buffer from where the data is written into the stream
312  * @param size the number of bytes to be written from the data buffer
313  * @param timeout the timeout period
314  * @param write_cont the function to call upon writing some bytes into the
315  *          stream 
316  * @param write_cont_cls the closure
317  *
318  * @return handle to cancel the operation; if a previous write is pending or
319  *           the stream has been shutdown for this operation then write_cont is
320  *           immediately called and NULL is returned.
321  */
322 struct GNUNET_STREAM_IOWriteHandle *
323 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
324                      const void *data,
325                      size_t size,
326                      struct GNUNET_TIME_Relative timeout,
327                      GNUNET_STREAM_CompletionContinuation write_cont,
328                      void *write_cont_cls);
329
330
331 /**
332  * Functions of this signature are called whenever data is available from the
333  * stream.
334  *
335  * @param cls the closure from GNUNET_STREAM_read
336  * @param status the status of the stream at the time this function is called
337  * @param data traffic from the other side
338  * @param size the number of bytes available in data read; will be 0 on timeout 
339  * @return number of bytes of processed from 'data' (any data remaining should be
340  *         given to the next time the read processor is called).
341  */
342 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
343                                                enum GNUNET_STREAM_Status status,
344                                                const void *data,
345                                                size_t size);
346
347
348 /**
349  * Tries to read data from the stream.
350  *
351  * @param socket the socket representing a stream
352  * @param timeout the timeout period
353  * @param proc function to call with data (once only)
354  * @param proc_cls the closure for proc
355  *
356  * @return handle to cancel the operation; NULL is returned if: the stream has
357  *           been shutdown for this type of opeartion (the DataProcessor is
358  *           immediately called with GNUNET_STREAM_SHUTDOWN as status) OR another
359  *           read handle is present (only one read handle per socket is present
360  *           at any time)
361  */
362 struct GNUNET_STREAM_IOReadHandle *
363 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
364                     struct GNUNET_TIME_Relative timeout,
365                     GNUNET_STREAM_DataProcessor proc,
366                     void *proc_cls);
367
368
369 /**
370  * Cancels pending write operation. Also cancels packet retransmissions which
371  * may have resulted otherwise.
372  *
373  * CAUTION: Normally a write operation is considered successful if the data
374  * given to it is sent and acknowledged by the receiver. As data is divided
375  * into packets, it is possible that not all packets are received by the
376  * receiver. Any missing packets are then retransmitted till the receiver
377  * acknowledges all packets or until a timeout . During this scenario if the
378  * write operation is cancelled all such retransmissions are also
379  * cancelled. This may leave the receiver's receive buffer incompletely filled
380  * as some missing packets are never retransmitted. So this operation should be
381  * used before shutting down transmission from our side or before closing the
382  * socket.
383  *
384  * @param ioh handle to operation to cancel
385  */
386 void
387 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
388
389
390 /**
391  * Cancel pending read operation.
392  *
393  * @param ioh handle to operation to cancel
394  */
395 void
396 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
397
398
399 #if 0
400 {
401 #endif
402 #ifdef __cplusplus
403 }
404 #endif
405
406 #endif  /* STREAM_PROTOCOL_H */