(no commit message)
[oweals/gnunet.git] / src / transport / plugin_transport.h
index 6532357f3159fd86e8a018187d9f04c395ce6c20..d3dc52f3249954e885c81ccbe94117ac830091d6 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
  *        Note that the destructors of transport plugins will
  *        be given the value returned by the constructor
  *        and is expected to return a NULL pointer.
- *
- * TODO:
- * - consider moving DATA message (latency measurement)
- *   to service; avoids encapsulation overheads and
- *   would enable latency measurements for non-bidi
- *   transports.
- * -
- *
  * @author Christian Grothoff
  */
 #ifndef PLUGIN_TRANSPORT_H
 
 #include "gnunet_configuration_lib.h"
 #include "gnunet_scheduler_lib.h"
+#include "gnunet_statistics_service.h"
 #include "gnunet_transport_service.h"
 
+/**
+ * Opaque pointer that plugins can use to distinguish specific
+ * connections to a given peer.  Typically used by stateful plugins to
+ * allow the service to refer to specific streams instead of a more
+ * general notion of "some connection" to the given peer.  This is
+ * useful since sometimes (i.e. for inbound TCP connections) a
+ * connection may not have an address that can be used for meaningful
+ * distinction between sessions to the same peer.
+ */
+struct Session;
+
+/**
+ * Every 'struct Session' must begin with this header.
+ */
+struct SessionHeader
+{
+
+  /**
+   * Cached signature for PONG generation for the session.  Do not use
+   * in the plugin!
+   */
+  struct GNUNET_CRYPTO_RsaSignature pong_signature;
+
+  /**
+   * Expiration time for signature.  Do not use in the plugin!
+   */
+  struct GNUNET_TIME_Absolute pong_sig_expires;
+  
+};
+
+/**
+ * Function that will be called whenever the plugin internally
+ * cleans up a session pointer and hence the service needs to
+ * discard all of those sessions as well.  Plugins that do not
+ * use sessions can simply omit calling this function and always
+ * use NULL wherever a session pointer is needed.
+ * 
+ * @param cls closure
+ * @param peer which peer was the session for 
+ * @param session which session is being destoyed
+ */
+typedef void (*GNUNET_TRANSPORT_SessionEnd) (void *cls,
+                                            const struct GNUNET_PeerIdentity *peer,
+                                            struct Session *session);
+
 
 /**
  * Function called by the transport for each received message.
  * message to signal that the other peer disconnected.
  *
  * @param cls closure
- * @param distance in overlay hops; use 1 unless DV
- * @param sender_address binary address of the sender (if observed)
- * @param sender_address_len number of bytes in sender_address
  * @param peer (claimed) identity of the other peer
- * @param message the message, NULL if peer was disconnected
+ * @param message the message, NULL if we only care about
+ *                learning about the delay until we should receive again -- FIXME!
+ * @param distance in overlay hops; use 1 unless DV (or 0 if message == NULL)
+ * @param session identifier used for this session (NULL for plugins
+ *                that do not offer bi-directional communication to the sender
+ *                using the same "connection")
+ * @param sender_address binary address of the sender (if we established the
+ *                connection or are otherwise sure of it; should be NULL
+ *                for inbound TCP/UDP connections since it it not clear
+ *                that we could establish ourselves a connection to that
+ *                IP address and get the same system)
+ * @param sender_address_len number of bytes in sender_address
+ * @return how long the plugin should wait until receiving more data
+ *         (plugins that do not support this, can ignore the return value)
  */
-typedef void (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
-                                                        unsigned int distance,
-                                                       const char *sender_address,
-                                                       size_t sender_address_len,
-                                                        const struct
-                                                        GNUNET_PeerIdentity *
-                                                        peer,
-                                                        const struct
-                                                        GNUNET_MessageHeader *
-                                                        message);
-
+typedef struct GNUNET_TIME_Relative (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
+                                                                              const struct
+                                                                              GNUNET_PeerIdentity *
+                                                                              peer,
+                                                                              const struct
+                                                                              GNUNET_MessageHeader *
+                                                                              message,
+                                                                              uint32_t distance,
+                                                                              struct Session *session,
+                                                                              const char *sender_address,
+                                                                              uint16_t sender_address_len);
 
 
 /**
@@ -83,12 +131,33 @@ typedef void (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
 typedef void (*GNUNET_TRANSPORT_AddressNotification) (void *cls,
                                                       const char *name,
                                                       const void *addr,
-                                                      size_t addrlen,
+                                                      uint16_t addrlen,
                                                       struct
                                                       GNUNET_TIME_Relative
                                                       expires);
 
 
+/**
+ * Function that will be called whenever the plugin receives data over
+ * the network and wants to determine how long it should wait until
+ * the next time it reads from the given peer.  Note that some plugins
+ * (such as UDP) may not be able to wait (for a particular peer), so
+ * the waiting part is optional.  Plugins that can wait should call
+ * this function, sleep the given amount of time, and call it again
+ * (with zero bytes read) UNTIL it returns zero and only then read.
+ * 
+ * @param cls closure
+ * @param peer which peer did we read data from 
+ * @param amount_recved number of bytes read (can be zero)
+ * @return how long to wait until reading more from this peer
+ *         (to enforce inbound quotas)
+ */
+typedef struct GNUNET_TIME_Relative (*GNUNET_TRANSPORT_TrafficReport) (void *cls,
+                                                                      const struct 
+                                                                      GNUNET_PeerIdentity *peer,
+                                                                      size_t amount_recved);
+
+
 /**
  * The transport service will pass a pointer to a struct
  * of this type as the first and only argument to the
@@ -116,6 +185,11 @@ struct GNUNET_TRANSPORT_PluginEnvironment
    */
   void *cls;
 
+  /**
+   * Handle for reporting statistics.
+   */
+  struct GNUNET_STATISTICS_Handle *stats;
+
   /**
    * Function that should be called by the transport plugin
    * whenever a message is received.
@@ -130,10 +204,16 @@ struct GNUNET_TRANSPORT_PluginEnvironment
   GNUNET_TRANSPORT_AddressNotification notify_address;
 
   /**
-   * What is the default quota (in terms of incoming bytes per
-   * ms) for new connections?
+   * Inform service about traffic received, get information
+   * about when we might be willing to receive more.
+   */
+  GNUNET_TRANSPORT_TrafficReport traffic_report;
+
+  /**
+   * Function that must be called by the plugin when a non-NULL
+   * session handle stops being valid (is destroyed).
    */
-  uint32_t default_quota_in;
+  GNUNET_TRANSPORT_SessionEnd session_end;
 
   /**
    * What is the maximum number of connections that this transport
@@ -172,19 +252,23 @@ typedef void
  *
  * @param cls closure
  * @param target who should receive this message
- * @param msg the message to transmit
+ * @param msgbuf the message to transmit
+ * @param msgbuf_size number of bytes in 'msgbuf'
  * @param priority how important is the message (most plugins will
  *                 ignore message priority and just FIFO)
  * @param timeout how long to wait at most for the transmission (does not
  *                require plugins to discard the message after the timeout,
  *                just advisory for the desired delay; most plugins will ignore
  *                this as well)
+ * @param session which session must be used (or NULL for "any")
  * @param addr the address to use (can be NULL if the plugin
  *                is "on its own" (i.e. re-use existing TCP connection))
  * @param addrlen length of the address in bytes
  * @param force_address GNUNET_YES if the plugin MUST use the given address,
- *                otherwise the plugin may use other addresses or
- *                existing connections (if available)
+ *                GNUNET_NO means the plugin may use any other address and
+ *                GNUNET_SYSERR means that only reliable existing
+ *                bi-directional connections should be used (regardless
+ *                of address)
  * @param cont continuation to call once the message has
  *        been transmitted (or if the transport is ready
  *        for the next transmission call; or if the
@@ -198,9 +282,11 @@ typedef ssize_t
   (*GNUNET_TRANSPORT_TransmitFunction) (void *cls,
                                         const struct GNUNET_PeerIdentity *
                                         target,
-                                        const struct GNUNET_MessageHeader *msg,
-                                        unsigned int priority,
+                                        const char *msgbuf,
+                                        size_t msgbuf_size,
+                                        uint32_t priority,
                                         struct GNUNET_TIME_Relative timeout,
+                                       struct Session *session,
                                         const void *addr,
                                        size_t addrlen,
                                        int force_address,
@@ -268,27 +354,14 @@ typedef void
                                             asc, void *asc_cls);
 
 
-/**
- * Set a quota for receiving data from the given peer; this is a
- * per-transport limit.  The transport should limit its read/select
- * calls to stay below the quota (in terms of incoming data).
- *
- * @param cls closure
- * @param peer the peer for whom the quota is given
- * @param quota_in quota for receiving/sending data in bytes per ms
- */
-typedef void
-  (*GNUNET_TRANSPORT_SetQuota) (void *cls,
-                                const struct GNUNET_PeerIdentity * target,
-                                uint32_t quota_in);
-
-
 /**
  * Another peer has suggested an address for this peer and transport
  * plugin.  Check that this could be a valid address.  This function
  * is not expected to 'validate' the address in the sense of trying to
  * connect to it but simply to see if the binary format is technically
- * legal for establishing a connection.
+ * legal for establishing a connection to this peer (and make sure that
+ * the address really corresponds to our network connection/settings
+ * and not some potential man-in-the-middle).
  *
  * @param addr pointer to the address
  * @param addrlen length of addr
@@ -296,8 +369,25 @@ typedef void
  *         and transport, GNUNET_SYSERR if not
  */
 typedef int
-  (*GNUNET_TRANSPORT_CheckAddress) (void *cls,
-                                   const void *addr, size_t addrlen);
+(*GNUNET_TRANSPORT_CheckAddress) (void *cls,
+                                 const void *addr, size_t addrlen);
+
+
+/**
+ * Function called for a quick conversion of the binary address to
+ * a numeric address.  Note that the caller must not free the 
+ * address and that the next call to this function is allowed
+ * to override the address again.
+ *
+ * @param cls closure
+ * @param addr binary address
+ * @param addr_len length of the address
+ * @return string representing the same address 
+ */
+typedef const char* (*GNUNET_TRANSPORT_AddressToString) (void *cls,
+                                                        const void *addr,
+                                                        size_t addrlen);
+
 
 /**
  * Each plugin is required to return a pointer to a struct of this
@@ -313,7 +403,7 @@ struct GNUNET_TRANSPORT_PluginFunctions
 
   /**
    * Function that the transport service will use to transmit data to
-   * another peer.  May be null for plugins that only support
+   * another peer.  May be NULL for plugins that only support
    * receiving data.  After this call, the plugin call the specified
    * continuation with success or error before notifying us about the
    * target having disconnected.
@@ -336,20 +426,22 @@ struct GNUNET_TRANSPORT_PluginFunctions
    */
   GNUNET_TRANSPORT_AddressPrettyPrinter address_pretty_printer;
 
-  /**
-   * Function that the transport service can use to try to enforce a
-   * quota for the number of bytes received via this transport.
-   * Transports that can not refuse incoming data (such as UDP)
-   * are free to ignore these calls.
-   */
-  GNUNET_TRANSPORT_SetQuota set_receive_quota;
-
   /**
    * Function that will be called to check if a binary address
-   * for this plugin is well-formed.
+   * for this plugin is well-formed and corresponds to an
+   * address for THIS peer (as per our configuration).  Naturally,
+   * if absolutely necessary, plugins can be a bit conservative in
+   * their answer, but in general plugins should make sure that the
+   * address does not redirect traffic to a 3rd party that might
+   * try to man-in-the-middle our traffic.
    */
   GNUNET_TRANSPORT_CheckAddress check_address;
 
+  /**
+   * Function that will be called to convert a binary address
+   * to a string (numeric conversion only).
+   */
+  GNUNET_TRANSPORT_AddressToString address_to_string;
 
 };