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
74 * Opaque handler for stream
76 struct GNUNET_STREAM_Socket;
79 * Functions of this type will be called when a stream is established
81 * @param cls the closure from GNUNET_STREAM_open
82 * @param socket socket to use to communicate with the other side (read/write)
84 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
85 struct GNUNET_STREAM_Socket *socket);
89 * Callback for signalling stream listen success; See
90 * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
92 typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void);
96 * Options for the stream.
98 enum GNUNET_STREAM_Option
101 * End of the option list.
103 GNUNET_STREAM_OPTION_END = 0,
106 * Option to set the initial retransmission timeout (when do we retransmit
107 * a packet that did not yield an acknowledgement for the first time?).
108 * Repeated retransmissions will then use an exponential back-off.
109 * Takes a 'struct GNUNET_TIME_Relative' as the only argument. A value
110 * of '0' means to use the round-trip time (plus a tiny grace period);
111 * this is also the default.
113 GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
116 * Option to set the write sequence number. Takes a uint32_t as parameter
117 * to set the value of the write sequence number
119 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
122 * Listen socket timeout in milliseconds given as uint32_t
124 GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
127 * Option to register a callback when stream listening is
128 * successfull. Takes parameter of the form
129 * GNUNET_STREAM_ListenSuccessCallback. The callback is only called if
130 * listen 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
203 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
206 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
210 * Closes the stream and frees the associated state. The stream should be
211 * shutdown before closing.
213 * @param socket the stream socket
216 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
220 * Functions of this type are called upon new stream connection from other peers
221 * or upon binding error which happen when the app_port given in
222 * GNUNET_STREAM_listen() is already taken.
224 * @param cls the closure from GNUNET_STREAM_listen
225 * @param socket the socket representing the stream; NULL on binding error
226 * @param initiator the identity of the peer who wants to establish a stream
227 * with us; NULL on binding error
228 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
229 * stream (the socket will be invalid after the call)
231 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
232 struct GNUNET_STREAM_Socket *socket,
234 GNUNET_PeerIdentity *initiator);
238 * A socket for listening.
240 struct GNUNET_STREAM_ListenSocket;
243 * Listens for stream connections for a specific application ports
245 * @param cfg the configuration to use
247 * @param app_port the application port for which new streams will be
248 * accepted. If another stream is listening on the same port the
249 * listen_cb will be called to signal binding error and the returned
250 * ListenSocket will be invalidated.
252 * @param listen_cb this function will be called when a peer tries to establish
254 * @param listen_cb_cls closure for listen_cb
255 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
256 * @return listen socket, NULL for any error
258 struct GNUNET_STREAM_ListenSocket *
259 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
260 GNUNET_MESH_ApplicationType app_port,
261 GNUNET_STREAM_ListenCallback listen_cb,
267 * Closes the listen socket
269 * @param lsocket the listen socket
272 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
276 * Functions of this signature are called whenever writing operations
277 * on a stream are executed
279 * @param cls the closure from GNUNET_STREAM_write
280 * @param status the status of the stream at the time this function is called
281 * @param size the number of bytes written
283 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
284 enum GNUNET_STREAM_Status
290 * Handle to cancel IO write operations.
292 struct GNUNET_STREAM_IOWriteHandle;
296 * Handle to cancel IO read operations.
298 struct GNUNET_STREAM_IOReadHandle;
301 * Tries to write the given data to the stream. The maximum size of data that
302 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
303 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
304 * violation, however only the said number of maximum bytes will be written.
306 * @param socket the socket representing a stream
307 * @param data the data buffer from where the data is written into the stream
308 * @param size the number of bytes to be written from the data buffer
309 * @param timeout the timeout period
310 * @param write_cont the function to call upon writing some bytes into the
312 * @param write_cont_cls the closure
314 * @return handle to cancel the operation; if a previous write is pending or
315 * the stream has been shutdown for this operation then write_cont is
316 * immediately called and NULL is returned.
318 struct GNUNET_STREAM_IOWriteHandle *
319 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
322 struct GNUNET_TIME_Relative timeout,
323 GNUNET_STREAM_CompletionContinuation write_cont,
324 void *write_cont_cls);
328 * Functions of this signature are called whenever data is available from the
331 * @param cls the closure from GNUNET_STREAM_read
332 * @param status the status of the stream at the time this function is called
333 * @param data traffic from the other side
334 * @param size the number of bytes available in data read; will be 0 on timeout
335 * @return number of bytes of processed from 'data' (any data remaining should be
336 * given to the next time the read processor is called).
338 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
339 enum GNUNET_STREAM_Status status,
345 * Tries to read data from the stream.
347 * @param socket the socket representing a stream
348 * @param timeout the timeout period
349 * @param proc function to call with data (once only)
350 * @param proc_cls the closure for proc
352 * @return handle to cancel the operation; NULL is returned if: the stream has
353 * been shutdown for this type of opeartion (the DataProcessor is
354 * immediately called with GNUNET_STREAM_SHUTDOWN as status) OR another
355 * read handle is present (only one read handle per socket is present
358 struct GNUNET_STREAM_IOReadHandle *
359 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
360 struct GNUNET_TIME_Relative timeout,
361 GNUNET_STREAM_DataProcessor proc,
366 * Cancels pending write operation. Also cancels packet retransmissions which
367 * may have resulted otherwise.
369 * CAUTION: Normally a write operation is considered successful if the data
370 * given to it is sent and acknowledged by the receiver. As data is divided
371 * into packets, it is possible that not all packets are received by the
372 * receiver. Any missing packets are then retransmitted till the receiver
373 * acknowledges all packets or until a timeout . During this scenario if the
374 * write operation is cancelled all such retransmissions are also
375 * cancelled. This may leave the receiver's receive buffer incompletely filled
376 * as some missing packets are never retransmitted. So this operation should be
377 * used before shutting down transmission from our side or before closing the
380 * @param ioh handle to operation to cancel
383 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
387 * Cancel pending read operation.
389 * @param ioh handle to operation to cancel
392 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
402 #endif /* STREAM_PROTOCOL_H */