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 * Options for the stream.
91 enum GNUNET_STREAM_Option
94 * End of the option list.
96 GNUNET_STREAM_OPTION_END = 0,
99 * Option to set the initial retransmission timeout (when do we retransmit
100 * a packet that did not yield an acknowledgement for the first time?).
101 * Repeated retransmissions will then use an exponential back-off.
102 * Takes a 'struct GNUNET_TIME_Relative' as the only argument. A value
103 * of '0' means to use the round-trip time (plus a tiny grace period);
104 * this is also the default.
106 GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT
111 * Tries to open a stream to the target peer
113 * @param cfg configuration to use
114 * @param target the target peer to which the stream has to be opened
115 * @param app_port the application port number which uniquely identifies this
117 * @param open_cb this function will be called after stream has be established
118 * @param open_cb_cls the closure for open_cb
119 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
120 * @return if successful it returns the stream socket; NULL if stream cannot be
123 struct GNUNET_STREAM_Socket *
124 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
125 const struct GNUNET_PeerIdentity *target,
126 GNUNET_MESH_ApplicationType app_port,
127 GNUNET_STREAM_OpenCallback open_cb,
133 * Handle for shutdown
135 struct GNUNET_STREAM_ShutdownHandle;
139 * Completion callback for shutdown
141 * @param cls the closure from GNUNET_STREAM_shutdown call
142 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
145 typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
150 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
152 * @param socket the stream socket
153 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
154 * @param completion_cb the callback that will be called upon successful
155 * shutdown of given operation
156 * @param completion_cls the closure for the completion callback
157 * @return the shutdown handle; NULL in case of any error
159 struct GNUNET_STREAM_ShutdownHandle *
160 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
162 GNUNET_STREAM_ShutdownCompletion completion_cb,
163 void *completion_cls);
167 * Cancels a pending shutdown
169 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
172 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
176 * Closes the stream and frees the associated state. The stream should be
177 * shutdown before closing.
179 * @param socket the stream socket
182 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
186 * Functions of this type are called upon new stream connection from other peers
188 * @param cls the closure from GNUNET_STREAM_listen
189 * @param socket the socket representing the stream
190 * @param initiator the identity of the peer who wants to establish a stream
192 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
193 * stream (the socket will be invalid after the call)
195 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
196 struct GNUNET_STREAM_Socket *socket,
198 GNUNET_PeerIdentity *initiator);
202 * A socket for listening.
204 struct GNUNET_STREAM_ListenSocket;
207 * Listens for stream connections for a specific application ports
209 * @param cfg the configuration to use
210 * @param app_port the application port for which new streams will be accepted
211 * @param listen_cb this function will be called when a peer tries to establish
213 * @param listen_cb_cls closure for listen_cb
214 * @return listen socket, NULL for any error
216 struct GNUNET_STREAM_ListenSocket *
217 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
218 GNUNET_MESH_ApplicationType app_port,
219 GNUNET_STREAM_ListenCallback listen_cb,
220 void *listen_cb_cls);
224 * Closes the listen socket
226 * @param lsocket the listen socket
229 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
233 * Functions of this signature are called whenever writing operations
234 * on a stream are executed
236 * @param cls the closure from GNUNET_STREAM_write
237 * @param status the status of the stream at the time this function is called
238 * @param size the number of bytes written
240 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
241 enum GNUNET_STREAM_Status
247 * Handle to cancel IO write operations.
249 struct GNUNET_STREAM_IOWriteHandle;
253 * Handle to cancel IO read operations.
255 struct GNUNET_STREAM_IOReadHandle;
258 * Tries to write the given data to the stream. The maximum size of data that
259 * can be written as part of a write operation is (64 * (64000 - sizeof (struct
260 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
261 * violation, however only the said number of maximum bytes will be written.
263 * @param socket the socket representing a stream
264 * @param data the data buffer from where the data is written into the stream
265 * @param size the number of bytes to be written from the data buffer
266 * @param timeout the timeout period
267 * @param write_cont the function to call upon writing some bytes into the
269 * @param write_cont_cls the closure
271 * @return handle to cancel the operation; if a previous write is pending or
272 * the stream has been shutdown for this operation then write_cont is
273 * immediately called and NULL is returned.
275 struct GNUNET_STREAM_IOWriteHandle *
276 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
279 struct GNUNET_TIME_Relative timeout,
280 GNUNET_STREAM_CompletionContinuation write_cont,
281 void *write_cont_cls);
285 * Functions of this signature are called whenever data is available from the
288 * @param cls the closure from GNUNET_STREAM_read
289 * @param status the status of the stream at the time this function is called
290 * @param data traffic from the other side
291 * @param size the number of bytes available in data read; will be 0 on timeout
292 * @return number of bytes of processed from 'data' (any data remaining should be
293 * given to the next time the read processor is called).
295 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
296 enum GNUNET_STREAM_Status status,
302 * Tries to read data from the stream.
304 * @param socket the socket representing a stream
305 * @param timeout the timeout period
306 * @param proc function to call with data (once only)
307 * @param proc_cls the closure for proc
309 * @return handle to cancel the operation; if the stream has been shutdown for
310 * this type of opeartion then the DataProcessor is immediately
311 * called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
313 struct GNUNET_STREAM_IOReadHandle *
314 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
315 struct GNUNET_TIME_Relative timeout,
316 GNUNET_STREAM_DataProcessor proc,
321 * Cancels pending write operation. Also cancels packet retransmissions which
322 * may have resulted otherwise.
324 * CAUTION: Normally a write operation is considered successful if the data
325 * given to it is sent and acknowledged by the receiver. As data is divided
326 * into packets, it is possible that not all packets are received by the
327 * receiver. Any missing packets are then retransmitted till the receiver
328 * acknowledges all packets or until a timeout . During this scenario if the
329 * write operation is cancelled all such retransmissions are also
330 * cancelled. This may leave the receiver's receive buffer incompletely filled
331 * as some missing packets are never retransmitted. So this operation should be
332 * used before shutting down transmission from our side or before closing the
335 * @param ioh handle to operation to cancel
338 GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
342 * Cancel pending read operation.
344 * @param ioh handle to operation to cancel
347 GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
357 #endif /* STREAM_PROTOCOL_H */