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,
57 * Other side has shutdown the socket for this type of operation
60 GNUNET_STREAM_SHUTDOWN,
63 * A serious error occured while operating on this stream
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 * Callback for signalling stream listen success; See
85 * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
87 typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void);
91 * Options for the stream.
93 enum GNUNET_STREAM_Option
96 * End of the option list.
98 GNUNET_STREAM_OPTION_END = 0,
101 * Option to set the initial retransmission timeout (when do we retransmit
102 * a packet that did not yield an acknowledgement for the first time?).
103 * Repeated retransmissions will then use an exponential back-off.
104 * Takes a 'struct GNUNET_TIME_Relative' as the only argument. A value
105 * of '0' means to use the round-trip time (plus a tiny grace period);
106 * this is also the default.
108 GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
111 * Option to set the write sequence number. Takes a uint32_t as parameter
112 * to set the value of the write sequence number
114 GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
117 * Listen socket timeout in milliseconds given as uint32_t
119 GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
122 * Option to register a callback when stream listening is successfull. Takes
123 * parameter of the form GNUNET_STREAM_ListenSuccessCallback. The callback
124 * is only called if listening is successful
126 GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
129 * Option to set the maximum payload size in bytes of a stream data
130 * packets. Takes an uint16_t as argument. Note that this should be less
131 * than 64000 and cannot be zero. Default is 64000 bytes.
133 GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE
138 * Tries to open a stream to the target peer
140 * @param cfg configuration to use
141 * @param target the target peer to which the stream has to be opened
142 * @param app_port the application port number which uniquely identifies this
144 * @param open_cb this function will be called after stream has be established;
146 * @param open_cb_cls the closure for open_cb
147 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
148 * @return if successful it returns the stream socket; NULL if stream cannot be
151 struct GNUNET_STREAM_Socket *
152 GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
153 const struct GNUNET_PeerIdentity *target,
155 GNUNET_STREAM_OpenCallback open_cb,
161 * Handle for shutdown
163 struct GNUNET_STREAM_ShutdownHandle;
167 * Completion callback for shutdown
169 * @param cls the closure from GNUNET_STREAM_shutdown call
170 * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
173 typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
178 * Shutdown the stream for reading or writing (similar to man 2 shutdown).
180 * @param socket the stream socket
181 * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
182 * @param completion_cb the callback that will be called upon successful
183 * shutdown of given operation
184 * @param completion_cls the closure for the completion callback
185 * @return the shutdown handle; NULL in case of any error
187 struct GNUNET_STREAM_ShutdownHandle *
188 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
190 GNUNET_STREAM_ShutdownCompletion completion_cb,
191 void *completion_cls);
195 * Cancels a pending shutdown. Note that the shutdown messages may already
196 * be sent and the stream is shutdown already for the operation given to
197 * GNUNET_STREAM_shutdown(). This function only clears up any retranmissions of
198 * shutdown messages and frees the shutdown handle.
200 * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
203 GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
207 * Closes the stream and frees the associated state. The stream should be
208 * shutdown for both reading and writing before closing.
210 * @param socket the stream socket
213 GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
217 * Functions of this type are called upon new stream connection from other peers
218 * or upon binding error which happen when the app_port given in
219 * GNUNET_STREAM_listen() is already taken.
221 * @param cls the closure from GNUNET_STREAM_listen
222 * @param socket the socket representing the stream; NULL on binding error
223 * @param initiator the identity of the peer who wants to establish a stream
224 * with us; NULL on binding error
225 * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
226 * stream (the socket will be invalid after the call)
228 typedef int (*GNUNET_STREAM_ListenCallback) (void *cls,
229 struct GNUNET_STREAM_Socket *socket,
231 GNUNET_PeerIdentity *initiator);
235 * A socket for listening.
237 struct GNUNET_STREAM_ListenSocket;
240 * Listens for stream connections for a specific application ports
242 * @param cfg the configuration to use
243 * @param app_port the application port for which new streams will be
244 * accepted. If another stream is listening on the same port the
245 * listen_cb will be called to signal binding error and the returned
246 * ListenSocket will be invalidated.
247 * @param listen_cb this function will be called when a peer tries to establish
249 * @param listen_cb_cls closure for listen_cb
250 * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
251 * @return listen socket, NULL for any error
253 struct GNUNET_STREAM_ListenSocket *
254 GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
256 GNUNET_STREAM_ListenCallback listen_cb,
262 * Closes the listen socket
264 * @param lsocket the listen socket
267 GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
271 * Functions of this signature are called whenever writing operations
272 * on a stream are executed
274 * @param cls the closure from GNUNET_STREAM_write
275 * @param status the status of the stream at the time this function is called;
276 * GNUNET_STREAM_OK if writing to stream was completed successfully;
277 * GNUNET_STREAM_TIMEOUT if the given data is not sent successfully
278 * (this doesn't mean that the data is never sent, the receiver may
279 * have read the data but its ACKs may have been lost);
280 * GNUNET_STREAM_SHUTDOWN if the stream is shutdown for writing in the
281 * mean time; GNUNET_STREAM_SYSERR if the stream is broken and cannot
283 * @param size the number of bytes written
285 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
286 enum GNUNET_STREAM_Status
292 * Handle to cancel IO write operations.
294 struct GNUNET_STREAM_WriteHandle;
298 * Handle to cancel IO read operations.
300 struct GNUNET_STREAM_ReadHandle;
303 * Tries to write the given data to the stream. The maximum size of data that
304 * can be written per a write operation is ~ 4MB (64 * (64000 - sizeof (struct
305 * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
306 * violation, however only the said number of maximum bytes will be written.
308 * @param socket the socket representing a stream
309 * @param data the data buffer from where the data is written into the stream
310 * @param size the number of bytes to be written from the data buffer
311 * @param timeout the timeout period
312 * @param write_cont the function to call upon writing some bytes into the
314 * @param write_cont_cls the closure
316 * @return handle to cancel the operation; if a previous write is pending NULL
317 * is returned. If the stream has been shutdown for this operation or
318 * is broken then write_cont is immediately called and NULL is
321 struct GNUNET_STREAM_WriteHandle *
322 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
325 struct GNUNET_TIME_Relative timeout,
326 GNUNET_STREAM_CompletionContinuation write_cont,
327 void *write_cont_cls);
331 * Functions of this signature are called whenever data is available from the
334 * @param cls the closure from GNUNET_STREAM_read
335 * @param status the status of the stream at the time this function is called
336 * @param data traffic from the other side
337 * @param size the number of bytes available in data read; will be 0 on timeout
338 * @return number of bytes of processed from 'data' (any data remaining should be
339 * given to the next time the read processor is called).
341 typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
342 enum GNUNET_STREAM_Status status,
348 * Tries to read data from the stream. Should not be called when another read
349 * handle is present; the existing read handle should be canceled with
350 * GNUNET_STREAM_read_cancel(). Only one read handle per socket is present at
353 * @param socket the socket representing a stream
354 * @param timeout the timeout period
355 * @param proc function to call with data (once only)
356 * @param proc_cls the closure for proc
357 * @return handle to cancel the operation; NULL is returned if the stream has
358 * been shutdown for this type of opeartion (the DataProcessor is
359 * immediately called with GNUNET_STREAM_SHUTDOWN as status)
361 struct GNUNET_STREAM_ReadHandle *
362 GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
363 struct GNUNET_TIME_Relative timeout,
364 GNUNET_STREAM_DataProcessor proc,
369 * Cancels pending write operation. Also cancels packet retransmissions which
370 * may have resulted otherwise.
372 * CAUTION: Normally a write operation is considered successful if the data
373 * given to it is sent and acknowledged by the receiver. As data is divided
374 * into packets, it is possible that not all packets are received by the
375 * receiver. Any missing packets are then retransmitted till the receiver
376 * acknowledges all packets or until a timeout . During this scenario if the
377 * write operation is cancelled all such retransmissions are also
378 * cancelled. This may leave the receiver's receive buffer incompletely filled
379 * as some missing packets are never retransmitted. So this operation should be
380 * used before shutting down transmission from our side or before closing the
383 * @param wh write operation handle to cancel
386 GNUNET_STREAM_write_cancel (struct GNUNET_STREAM_WriteHandle *wh);
390 * Cancel pending read operation.
392 * @param rh read operation handle to cancel
395 GNUNET_STREAM_read_cancel (struct GNUNET_STREAM_ReadHandle *rh);
399 * Create a message queue for a stream socket.
401 * @param socket the socket to read/write in the message queue
402 * @param msg_handlers message handler array
403 * @param error_handler callback for errors
404 * @param cls closure for message handlers and error handler
405 * @return the message queue for the socket
407 struct GNUNET_MQ_Handle *
408 GNUNET_STREAM_mq_create (struct GNUNET_STREAM_Socket *socket,
409 const struct GNUNET_MQ_MessageHandler *msg_handlers,
410 GNUNET_MQ_ErrorHandler error_handler,
421 #endif /* STREAM_PROTOCOL_H */