-change vpn wire data format
[oweals/gnunet.git] / src / include / gnunet_stream_lib.h
index f0110bbb36fb2e821d64c1b7f365f650bdd90c2d..a134470c75b7ccca763fcdb0133e065f79afdd7c 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2011, Christian Grothoff (and other contributing authors)
+     (C) 2011, 2012 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -24,8 +24,8 @@
  * @author Sree Harsha Totakura
  */
 
-#ifndef GNUNET_STREAM_LIB_H_
-#define GNUNET_STREAM_LIB_H_
+#ifndef GNUNET_STREAM_LIB_H
+#define GNUNET_STREAM_LIB_H
 
 #ifdef __cplusplus
 extern "C" 
@@ -60,9 +60,14 @@ enum GNUNET_STREAM_Status
     GNUNET_STREAM_SHUTDOWN = 2,
 
     /**
-     * A serious error occured while operating of this stream
+     * A serious error occured while operating on this stream
      */
-    GNUNET_STREAM_SYSERR = 3
+    GNUNET_STREAM_SYSERR = 3,
+    
+    /**
+     * An error resulted in an unusable stream
+     */
+    GNUNET_STREAM_BROKEN
   };
 
 /**
@@ -80,37 +85,123 @@ typedef void (*GNUNET_STREAM_OpenCallback) (void *cls,
                                            struct GNUNET_STREAM_Socket *socket);
 
 
+/**
+ * Callback for signalling stream listen success; See
+ * GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
+ */
+typedef void (*GNUNET_STREAM_ListenSuccessCallback) (void);
+
+
+/**
+ * Options for the stream.
+ */
+enum GNUNET_STREAM_Option
+  {
+    /**
+     * End of the option list.
+     */
+    GNUNET_STREAM_OPTION_END = 0,
+
+    /**
+     * Option to set the initial retransmission timeout (when do we retransmit
+     * a packet that did not yield an acknowledgement for the first time?).  
+     * Repeated retransmissions will then use an exponential back-off.
+     * Takes a 'struct GNUNET_TIME_Relative' as the only argument.  A value
+     * of '0' means to use the round-trip time (plus a tiny grace period);
+     * this is also the default.
+     */
+    GNUNET_STREAM_OPTION_INITIAL_RETRANSMIT_TIMEOUT,
+
+    /**
+     * Option to set the write sequence number. Takes a uint32_t as parameter
+     * to set the value of the write sequence number
+     */
+    GNUNET_STREAM_OPTION_TESTING_SET_WRITE_SEQUENCE_NUMBER,
+
+    /**
+     * Listen socket timeout in milliseconds given as uint32_t
+     */
+    GNUNET_STREAM_OPTION_LISTEN_TIMEOUT,
+
+    /**
+     * Option to register a callback when stream listening is
+     * successfull. Takes parameter of the form
+     * GNUNET_STREAM_ListenSuccessCallback. The callback is only called if
+     * listen is successful
+     */
+    GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS
+  };
+
+
 /**
  * Tries to open a stream to the target peer
  *
+ * @param cfg configuration to use
  * @param target the target peer to which the stream has to be opened
  * @param app_port the application port number which uniquely identifies this
  *            stream
- * @param open_cb this function will be called after stream has be established 
+ * @param open_cb this function will be called after stream has be established;
+ *          cannot be NULL
  * @param open_cb_cls the closure for open_cb
+ * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
  * @return if successful it returns the stream socket; NULL if stream cannot be
  *         opened 
  */
 struct GNUNET_STREAM_Socket *
-GNUNET_STREAM_open (const struct GNUNET_PeerIdentity *target,
+GNUNET_STREAM_open (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                    const struct GNUNET_PeerIdentity *target,
                     GNUNET_MESH_ApplicationType app_port,
                     GNUNET_STREAM_OpenCallback open_cb,
-                   void *open_cb_cls);
+                   void *open_cb_cls,
+                   ...);
+
+
+/**
+ * Handle for shutdown
+ */
+struct GNUNET_STREAM_ShutdownHandle;
+
+
+/**
+ * Completion callback for shutdown
+ *
+ * @param cls the closure from GNUNET_STREAM_shutdown call
+ * @param operation the operation that was shutdown (SHUT_RD, SHUT_WR,
+ *          SHUT_RDWR) 
+ */
+typedef void (*GNUNET_STREAM_ShutdownCompletion) (void *cls,
+                                                  int operation);
 
 
 /**
- * Shutdown the stream for reading or writing (man 2 shutdown).
+ * Shutdown the stream for reading or writing (similar to man 2 shutdown).
  *
  * @param socket the stream socket
- * @param how SHUT_RD, SHUT_WR or SHUT_RDWR 
+ * @param operation SHUT_RD, SHUT_WR or SHUT_RDWR
+ * @param completion_cb the callback that will be called upon successful
+ *          shutdown of given operation
+ * @param completion_cls the closure for the completion callback
+ * @return the shutdown handle; NULL in case of any error
  */
-void
+struct GNUNET_STREAM_ShutdownHandle *
 GNUNET_STREAM_shutdown (struct GNUNET_STREAM_Socket *socket,
-                       int how);
+                       int operation,
+                        GNUNET_STREAM_ShutdownCompletion completion_cb,
+                        void *completion_cls);
 
 
 /**
- * Closes the stream
+ * Cancels a pending shutdown
+ *
+ * @param handle the shutdown handle returned from GNUNET_STREAM_shutdown
+ */
+void
+GNUNET_STREAM_shutdown_cancel (struct GNUNET_STREAM_ShutdownHandle *handle);
+
+
+/**
+ * Closes the stream and frees the associated state. The stream should be
+ * shutdown before closing.
  *
  * @param socket the stream socket
  */
@@ -122,9 +213,9 @@ GNUNET_STREAM_close (struct GNUNET_STREAM_Socket *socket);
  * Functions of this type are called upon new stream connection from other peers
  *
  * @param cls the closure from GNUNET_STREAM_listen
- * @param socket the socket representing the stream
+ * @param socket the socket representing the stream; NULL on binding error
  * @param initiator the identity of the peer who wants to establish a stream
- *            with us
+ *            with us; NULL on binding error
  * @return GNUNET_OK to keep the socket open, GNUNET_SYSERR to close the
  *             stream (the socket will be invalid after the call)
  */
@@ -142,34 +233,43 @@ struct GNUNET_STREAM_ListenSocket;
 /**
  * Listens for stream connections for a specific application ports
  *
- * @param app_port the application port for which new streams will be accepted
+ * @param cfg the configuration to use
+ *
+ * @param app_port the application port for which new streams will be
+ *         accepted. If another stream is listening on the same port the
+ *         listen_cb will be called to signal binding error and the returned
+ *         ListenSocket will be invalidated.
+ *
  * @param listen_cb this function will be called when a peer tries to establish
  *            a stream with us
  * @param listen_cb_cls closure for listen_cb
+ * @param ... options to the stream, terminated by GNUNET_STREAM_OPTION_END
  * @return listen socket, NULL for any error
  */
 struct GNUNET_STREAM_ListenSocket *
-GNUNET_STREAM_listen (GNUNET_MESH_ApplicationType app_port,
+GNUNET_STREAM_listen (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                      GNUNET_MESH_ApplicationType app_port,
                       GNUNET_STREAM_ListenCallback listen_cb,
-                      void *listen_cb_cls);
+                      void *listen_cb_cls,
+                      ...);
 
 
 /**
  * Closes the listen socket
  *
- * @param socket the listen socket
+ * @param lsocket the listen socket
  */
 void
-GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *socket);
+GNUNET_STREAM_listen_close (struct GNUNET_STREAM_ListenSocket *lsocket);
 
 
 /**
  * Functions of this signature are called whenever writing operations
  * on a stream are executed
  *
- * @param cls the closure from GNUNET_STREAM_write/read
+ * @param cls the closure from GNUNET_STREAM_write
  * @param status the status of the stream at the time this function is called
- * @param size the number of bytes read or written
+ * @param size the number of bytes written
  */
 typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
                                                      enum GNUNET_STREAM_Status
@@ -178,23 +278,35 @@ typedef void (*GNUNET_STREAM_CompletionContinuation) (void *cls,
 
 
 /**
- * Handle to cancel IO operations.
+ * Handle to cancel IO write operations.
  */
-struct GNUNET_STREAM_IOHandle;
+struct GNUNET_STREAM_IOWriteHandle;
 
 
 /**
- * Tries to write the given data to the stream
+ * Handle to cancel IO read operations.
+ */
+struct GNUNET_STREAM_IOReadHandle;
+
+/**
+ * Tries to write the given data to the stream. The maximum size of data that
+ * can be written as part of a write operation is (64 * (64000 - sizeof (struct
+ * GNUNET_STREAM_DataMessage))). If size is greater than this it is not an API
+ * violation, however only the said number of maximum bytes will be written.
  *
  * @param socket the socket representing a stream
  * @param data the data buffer from where the data is written into the stream
  * @param size the number of bytes to be written from the data buffer
  * @param timeout the timeout period
- * @param write_cont the function to call upon writing some bytes into the stream
+ * @param write_cont the function to call upon writing some bytes into the
+ *          stream 
  * @param write_cont_cls the closure
- * @return handle to cancel the operation
+ *
+ * @return handle to cancel the operation; if a previous write is pending or
+ *           the stream has been shutdown for this operation then write_cont is
+ *           immediately called and NULL is returned.
  */
-struct GNUNET_STREAM_IOHandle *
+struct GNUNET_STREAM_IOWriteHandle *
 GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
                      const void *data,
                      size_t size,
@@ -207,40 +319,66 @@ GNUNET_STREAM_write (struct GNUNET_STREAM_Socket *socket,
  * Functions of this signature are called whenever data is available from the
  * stream.
  *
- * @param cls the closure from GNUNET_STREAM_write/read
+ * @param cls the closure from GNUNET_STREAM_read
  * @param status the status of the stream at the time this function is called
  * @param data traffic from the other side
- * @param size the number of bytes available in data read 
+ * @param size the number of bytes available in data read; will be 0 on timeout 
+ * @return number of bytes of processed from 'data' (any data remaining should be
+ *         given to the next time the read processor is called).
  */
-typedef void (*GNUNET_STREAM_DataProcessor) (void *cls,
-                                            enum GNUNET_STREAM_Status status,
-                                            const char *data,
-                                            size_t size);
+typedef size_t (*GNUNET_STREAM_DataProcessor) (void *cls,
+                                               enum GNUNET_STREAM_Status status,
+                                               const void *data,
+                                               size_t size);
 
 
 /**
- * Tries to read data from the stream
+ * Tries to read data from the stream.
  *
  * @param socket the socket representing a stream
  * @param timeout the timeout period
- * @param proc function to call with data
+ * @param proc function to call with data (once only)
  * @param proc_cls the closure for proc
- * @return handle to cancel the operation
+ *
+ * @return handle to cancel the operation; if the stream has been shutdown for
+ *           this type of opeartion then the DataProcessor is immediately
+ *           called with GNUNET_STREAM_SHUTDOWN as status and NULL if returned
  */
-struct GNUNET_STREAM_IOHandle *
-GNUNET_STREAM_read (const struct GNUNET_STREAM_Socket *socket,
+struct GNUNET_STREAM_IOReadHandle *
+GNUNET_STREAM_read (struct GNUNET_STREAM_Socket *socket,
                     struct GNUNET_TIME_Relative timeout,
                    GNUNET_STREAM_DataProcessor proc,
                    void *proc_cls);
 
 
 /**
- * Cancel pending read or write operation.
+ * Cancels pending write operation. Also cancels packet retransmissions which
+ * may have resulted otherwise.
+ *
+ * CAUTION: Normally a write operation is considered successful if the data
+ * given to it is sent and acknowledged by the receiver. As data is divided
+ * into packets, it is possible that not all packets are received by the
+ * receiver. Any missing packets are then retransmitted till the receiver
+ * acknowledges all packets or until a timeout . During this scenario if the
+ * write operation is cancelled all such retransmissions are also
+ * cancelled. This may leave the receiver's receive buffer incompletely filled
+ * as some missing packets are never retransmitted. So this operation should be
+ * used before shutting down transmission from our side or before closing the
+ * socket.
+ *
+ * @param ioh handle to operation to cancel
+ */
+void
+GNUNET_STREAM_io_write_cancel (struct GNUNET_STREAM_IOWriteHandle *iowh);
+
+
+/**
+ * Cancel pending read operation.
  *
  * @param ioh handle to operation to cancel
  */
 void
-GNUNET_STREAM_io_cancel (struct GNUNET_STREAM_IOHandle *ioh);
+GNUNET_STREAM_io_read_cancel (struct GNUNET_STREAM_IOReadHandle *iorh);
 
 
 #if 0
@@ -250,4 +388,4 @@ GNUNET_STREAM_io_cancel (struct GNUNET_STREAM_IOHandle *ioh);
 }
 #endif
 
-#endif
+#endif  /* STREAM_PROTOCOL_H */