2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_stream_lib.h
23 * @brief stream handling using mesh API
24 * @author Sree Harsha Totakura
27 #ifndef GNUNET_STREAM_LIB_H
28 #define GNUNET_STREAM_LIB_H
38 #include "gnunet_util_lib.h"
39 #include "gnunet_mesh_service.h"
44 enum GNUNET_STREAM_Status
47 * All previous read/write operations are successfully done
52 * A timeout occured while reading/writing the stream
54 GNUNET_STREAM_TIMEOUT = 1,
57 * Other side has shutdown the socket for this type of operation
60 GNUNET_STREAM_SHUTDOWN = 2,
63 * A serious error occured while operating on this stream
65 GNUNET_STREAM_SYSERR = 3,
68 * An error resulted in an unusable stream
69 * FIXME: status code unused?
75 * Opaque handler for stream
77 struct GNUNET_STREAM_Socket;
80 * Functions of this type will be called when a stream is established
82 * @param cls the closure from GNUNET_STREAM_open
83 * @param socket socket to use to communicate with the other side (read/write)
85 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
86 struct GNUNET_STREAM_Socket *socket);
90 * Callback for signalling stream listen success; See
91 * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
93 typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void);
97 * Options for the stream.
99 enum GNUNET_STREAM_Option
102 * End of the option list.
104 GNUNET_STREAM_OPTION_END = 0,
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.
114 GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
117 * Option to set the write sequence number. Takes a uint32_t as parameter
118 * to set the value of the write sequence number
120 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
123 * Listen socket timeout in milliseconds given as uint32_t
125 GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
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
132 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
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.
139 GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE
144 * Tries to open a stream to the target peer
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
150 * @param open_cb this function will be called after stream has be established;
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
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,
167 * Handle for shutdown
169 struct GNUNET_STREAM_ShutdownHandle;
173 * Completion callback for shutdown
175 * @param cls the closure from GNUNET_STREAM_shutdown call
176 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
179 typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
184 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
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
193 struct GNUNET_STREAM_ShutdownHandle *
194 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
196 GNUNET_STREAM_ShutdownCompletion completion_cb,
197 void *completion_cls);
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.
206 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
209 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
213 * Closes the stream and frees the associated state. The stream should be
214 * shutdown for both reading and writing before closing.
216 * @param socket the stream socket
219 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
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.
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)
234 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
235 struct GNUNET_STREAM_Socket *socket,
237 GNUNET_PeerIdentity *initiator);
241 * A socket for listening.
243 struct GNUNET_STREAM_ListenSocket;
246 * Listens for stream connections for a specific application ports
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
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
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,
268 * Closes the listen socket
270 * @param lsocket the listen socket
273 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
277 * Functions of this signature are called whenever writing operations
278 * on a stream are executed
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
285 * @param size the number of bytes written
287 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
288 enum GNUNET_STREAM_Status
294 * Handle to cancel IO write operations.
296 struct GNUNET_STREAM_IOWriteHandle;
300 * Handle to cancel IO read operations.
302 struct GNUNET_STREAM_IOReadHandle;
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.
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
316 * @param write_cont_cls the closure
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.
322 struct GNUNET_STREAM_IOWriteHandle *
323 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
326 struct GNUNET_TIME_Relative timeout,
327 GNUNET_STREAM_CompletionContinuation write_cont,
328 void *write_cont_cls);
332 * Functions of this signature are called whenever data is available from the
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).
342 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
343 enum GNUNET_STREAM_Status status,
349 * Tries to read data from the stream.
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
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
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,
370 * Cancels pending write operation. Also cancels packet retransmissions which
371 * may have resulted otherwise.
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
384 * @param ioh handle to operation to cancel
387 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
391 * Cancel pending read operation.
393 * @param ioh handle to operation to cancel
396 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
406 #endif /* STREAM_PROTOCOL_H */