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
69 * Opaque handler for stream
71 struct GNUNET_STREAM_Socket;
74 * Functions of this type will be called when a stream is established
76 * @param cls the closure from GNUNET_STREAM_open
77 * @param socket socket to use to communicate with the other side (read/write)
79 typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
80 struct GNUNET_STREAM_Socket *socket);
84 * Options for the stream.
86 enum GNUNET_STREAM_Option
89 * End of the option list.
91 GNUNET_STREAM_OPTION_END = 0,
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.
101 GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT
106 * Tries to open a stream to the target peer
108 * @param cfg configuration to use
109 * @param target the target peer to which the stream has to be opened
110 * @param app_port the application port number which uniquely identifies this
112 * @param open_cb this function will be called after stream has be established
113 * @param open_cb_cls the closure for open_cb
114 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
115 * @return if successful it returns the stream socket; NULL if stream cannot be
118 struct GNUNET_STREAM_Socket *
119 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
120 const struct GNUNET_PeerIdentity *target,
121 GNUNET_MESH_ApplicationType app_port,
122 GNUNET_STREAM_OpenCallback open_cb,
128 * Handle for shutdown
130 struct GNUNET_STREAM_ShutdownHandle;
134 * Completion callback for shutdown
136 * @param cls the closure from GNUNET_STREAM_shutdown call
137 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
140 typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
145 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
147 * @param socket the stream socket
148 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
149 * @param completion_cb the callback that will be called upon successful
150 * shutdown of given operation
151 * @param completion_cls the closure for the completion callback
152 * @return the shutdown handle; NULL in case of any error
154 struct GNUNET_STREAM_ShutdownHandle *
155 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
157 GNUNET_STREAM_ShutdownCompletion completion_cb,
158 void *completion_cls);
162 * Cancels a pending shutdown
164 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
167 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
171 * Closes the stream and frees the associated state. The stream should be
172 * shutdown before closing.
174 * @param socket the stream socket
177 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
181 * Functions of this type are called upon new stream connection from other peers
183 * @param cls the closure from GNUNET_STREAM_listen
184 * @param socket the socket representing the stream
185 * @param initiator the identity of the peer who wants to establish a stream
187 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
188 * stream (the socket will be invalid after the call)
190 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
191 struct GNUNET_STREAM_Socket *socket,
193 GNUNET_PeerIdentity *initiator);
197 * A socket for listening.
199 struct GNUNET_STREAM_ListenSocket;
202 * Listens for stream connections for a specific application ports
204 * @param cfg the configuration to use
205 * @param app_port the application port for which new streams will be accepted
206 * @param listen_cb this function will be called when a peer tries to establish
208 * @param listen_cb_cls closure for listen_cb
209 * @return listen socket, NULL for any error
211 struct GNUNET_STREAM_ListenSocket *
212 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
213 GNUNET_MESH_ApplicationType app_port,
214 GNUNET_STREAM_ListenCallback listen_cb,
215 void *listen_cb_cls);
219 * Closes the listen socket
221 * @param lsocket the listen socket
224 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
228 * Functions of this signature are called whenever writing operations
229 * on a stream are executed
231 * @param cls the closure from GNUNET_STREAM_write
232 * @param status the status of the stream at the time this function is called
233 * @param size the number of bytes written
235 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
236 enum GNUNET_STREAM_Status
242 * Handle to cancel IO write operations.
244 struct GNUNET_STREAM_IOWriteHandle;
248 * Handle to cancel IO read operations.
250 struct GNUNET_STREAM_IOReadHandle;
253 * Tries to write the given data to the stream. The maximum size of data that
254 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
255 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
256 * violation, however only the said number of maximum bytes will be written.
258 * @param socket the socket representing a stream
259 * @param data the data buffer from where the data is written into the stream
260 * @param size the number of bytes to be written from the data buffer
261 * @param timeout the timeout period
262 * @param write_cont the function to call upon writing some bytes into the
264 * @param write_cont_cls the closure
266 * @return handle to cancel the operation; if a previous write is pending or
267 * the stream has been shutdown for this operation then write_cont is
268 * immediately called and NULL is returned.
270 struct GNUNET_STREAM_IOWriteHandle *
271 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
274 struct GNUNET_TIME_Relative timeout,
275 GNUNET_STREAM_CompletionContinuation write_cont,
276 void *write_cont_cls);
280 * Functions of this signature are called whenever data is available from the
283 * @param cls the closure from GNUNET_STREAM_read
284 * @param status the status of the stream at the time this function is called
285 * @param data traffic from the other side
286 * @param size the number of bytes available in data read; will be 0 on timeout
287 * @return number of bytes of processed from 'data' (any data remaining should be
288 * given to the next time the read processor is called).
290 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
291 enum GNUNET_STREAM_Status status,
297 * Tries to read data from the stream.
299 * @param socket the socket representing a stream
300 * @param timeout the timeout period
301 * @param proc function to call with data (once only)
302 * @param proc_cls the closure for proc
304 * @return handle to cancel the operation; if the stream has been shutdown for
305 * this type of opeartion then the DataProcessor is immediately
306 * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
308 struct GNUNET_STREAM_IOReadHandle *
309 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
310 struct GNUNET_TIME_Relative timeout,
311 GNUNET_STREAM_DataProcessor proc,
316 * Cancels pending write operation. Also cancels packet retransmissions which
317 * may have resulted otherwise.
319 * CAUTION: Normally a write operation is considered successful if the data
320 * given to it is sent and acknowledged by the receiver. As data is divided
321 * into packets, it is possible that not all packets are received by the
322 * receiver. Any missing packets are then retransmitted till the receiver
323 * acknowledges all packets or until a timeout . During this scenario if the
324 * write operation is cancelled all such retransmissions are also
325 * cancelled. This may leave the receiver's receive buffer incompletely filled
326 * as some missing packets are never retransmitted. So this operation should be
327 * used before shutting down transmission from our side or before closing the
330 * @param ioh handle to operation to cancel
333 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
337 * Cancel pending read operation.
339 * @param ioh handle to operation to cancel
342 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
352 #endif /* STREAM_PROTOCOL_H */