Added sort-of Quality of Service (QoS) support to the
[silc.git] / lib / silcutil / silcsockconn.h
index 89de51aace822bc5f023e9f77a4013172b85100c..e8f3356cdae300e1c8ae6ea39c0f51461a8808c8 100644 (file)
@@ -18,7 +18,7 @@
 
 */
 
-/****h* silcutil/SilcSocketConnectionAPI
+/****h* silcutil/SILC Socket Interface
  *
  * DESCRIPTION
  *
@@ -67,6 +67,31 @@ typedef struct SilcSocketConnectionStruct *SilcSocketConnection;
  ***/
 typedef struct SilcSocketConnectionHBStruct *SilcSocketConnectionHB;
 
+/****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionQos
+ *
+ * NAME
+ * 
+ *    typedef struct SilcSocketConnectionQosStruct *SilcSocketConnectionQos;
+ *
+ * DESCRIPTION
+ *
+ *    This structure is "Quality of Service" structure for the socket
+ *    connection and is set with silc_socket_set_qos function for a
+ *    socket context.
+ *
+ ***/
+typedef struct SilcSocketConnectionQosStruct {
+  SilcUInt16 read_limit_bytes;     /* Max read bytes */
+  SilcUInt16 read_rate;                    /* Max read rate/second */
+  SilcUInt16 limit_sec;                    /* Limit seconds */
+  SilcUInt32 limit_usec;           /* Limit microseconds */
+  SilcSchedule schedule;
+  struct timeval next_limit;
+  unsigned int cur_rate : 31;
+  unsigned int applied  : 1;
+  SilcUInt32 data_len;
+} *SilcSocketConnectionQos;
+
 /****d* silcutil/SilcSocketConnectionAPI/SilcSocketType
  *
  * NAME
@@ -94,11 +119,14 @@ typedef enum {
 
 /* Socket flags */
 #define SILC_SF_NONE             0
-#define SILC_SF_INBUF_PENDING    1
-#define SILC_SF_OUTBUF_PENDING   2
-#define SILC_SF_DISCONNECTING    3
-#define SILC_SF_DISCONNECTED     4
-#define SILC_SF_HOST_LOOKUP      5
+#define SILC_SF_INBUF_PENDING    1 /* data in inbound buffer */
+#define SILC_SF_OUTBUF_PENDING   2 /* data in outbound buffer */
+#define SILC_SF_DISCONNECTING    3 /* socket disconnecting */
+#define SILC_SF_DISCONNECTED     4 /* socket disconnected */
+#define SILC_SF_HOST_LOOKUP      5 /* performing host lookup for socket */
+#define SILC_SF_DISABLED         6 /* socket connection is disabled,
+                                     no data is sent or received. */
+#define SILC_SF_LISTENER         7
 
 /****s* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionStruct
  *
@@ -140,7 +168,7 @@ typedef enum {
  *      Protocol object for the socket. Currently only one protocol can be
  *      executing at a time for a particular socket.
  *
- *    uint32 flags
+ *    SilcUInt32 flags
  *
  *      Socket flags that indicate the status of the socket. This can
  *      indicate several different status that can affect the use of the
@@ -151,12 +179,9 @@ typedef enum {
  *      Reference counter. When allocated it is set to one (1) and it won't
  *      be freed until it hits zero (0).
  *
- *    char *hostname
- *    char *ip
- *    uint16 port
+ *    SilcSocketConnectionHB hb
  *
- *      Resolved hostname, IP address and port of the connection who owns
- *      this object.
+ *      The heartbeat context.  If NULL, heartbeat is not performed.
  *
  *    SilcBuffer inbuf
  *    SilcBuffer outbuf
@@ -166,9 +191,12 @@ typedef enum {
  *      inbuf buffer and outgoing data after encryption is put to the outbuf
  *      buffer.
  *
- *    SilcSocketConnectionHB hb
+ *    char *hostname
+ *    char *ip
+ *    SilcUInt16 port
  *
- *      The heartbeat context.  If NULL, heartbeat is not performed.
+ *      Resolved hostname, IP address and port of the connection who owns
+ *      this object.
  *
  ***/
 struct SilcSocketConnectionStruct {
@@ -176,21 +204,27 @@ struct SilcSocketConnectionStruct {
   SilcSocketType type;
   void *user_data;
   SilcProtocol protocol;
-  uint32 flags;
+  SilcUInt32 flags;
   int users;
 
-  char *hostname;
-  char *ip;
-  uint16 port;
+  SilcSocketConnectionHB hb;
+  SilcSocketConnectionQos qos;
 
   SilcBuffer inbuf;
   SilcBuffer outbuf;
 
-  SilcSocketConnectionHB hb;
+  char *hostname;
+  char *ip;
+  SilcUInt16 port;
+  SilcUInt8 sock_error;
+  SilcUInt8 version;
 };
 
 /* Macros */
 
+/* Check for specific protocol version */
+#define SILC_PROTOCOL_VERSION(s, maj, min) (s->version == maj##min)
+
 /* Amount of bytes to be read from the socket connection at once. */
 #define SILC_SOCKET_READ_SIZE 16384
 
@@ -208,11 +242,15 @@ struct SilcSocketConnectionStruct {
 #define SILC_SET_DISCONNECTING(x) SF_SET((x), SILC_SF_DISCONNECTING)
 #define SILC_SET_DISCONNECTED(x) SF_SET((x), SILC_SF_DISCONNECTED)
 #define SILC_SET_HOST_LOOKUP(x) SF_SET((x), SILC_SF_HOST_LOOKUP)
+#define SILC_SET_DISABLED(x) SF_SET((x), SILC_SF_DISABLED)
+#define SILC_SET_LISTENER(x) SF_SET((x), SILC_SF_LISTENER)
 #define SILC_UNSET_OUTBUF_PENDING(x) SF_UNSET((x), SILC_SF_OUTBUF_PENDING)
 #define SILC_UNSET_INBUF_PENDING(x) SF_UNSET((x), SILC_SF_INBUF_PENDING)
 #define SILC_UNSET_DISCONNECTING(x) SF_UNSET((x), SILC_SF_DISCONNECTING)
 #define SILC_UNSET_DISCONNECTED(x) SF_UNSET((x), SILC_SF_DISCONNECTED)
 #define SILC_UNSET_HOST_LOOKUP(x) SF_UNSET((x), SILC_SF_HOST_LOOKUP)
+#define SILC_UNSET_DISABLED(x) SF_UNSET((x), SILC_SF_DISABLED)
+#define SILC_UNSET_LISTENER(x) SF_UNSET((x), SILC_SF_LISTENER)
 
 /* Checking for flags */
 #define SILC_IS_OUTBUF_PENDING(x) SF_IS((x), SILC_SF_OUTBUF_PENDING)
@@ -220,6 +258,8 @@ struct SilcSocketConnectionStruct {
 #define SILC_IS_DISCONNECTING(x) SF_IS((x), SILC_SF_DISCONNECTING)
 #define SILC_IS_DISCONNECTED(x) SF_IS((x), SILC_SF_DISCONNECTED)
 #define SILC_IS_HOST_LOOKUP(x) SF_IS((x), SILC_SF_HOST_LOOKUP)
+#define SILC_IS_DISABLED(x) SF_IS((x), SILC_SF_DISABLED)
+#define SILC_IS_LISTENER(x) SF_IS((x), SILC_SF_LISTENER)
 
 /* Prototypes */
 
@@ -294,7 +334,7 @@ int silc_socket_read(SilcSocketConnection sock);
  *
  * SYNOPSIS
  *
- *    int silc_socket_read(SilcSocketConnection sock);
+ *    int silc_socket_write(SilcSocketConnection sock);
  *
  * DESCRIPTION
  *
@@ -308,6 +348,23 @@ int silc_socket_read(SilcSocketConnection sock);
  ***/
 int silc_socket_write(SilcSocketConnection sock);
 
+/****f* silcutil/SilcSocketConnectionAPI/silc_socket_get_error
+ *
+ * SYNOPSIS
+ *
+ *    bool silc_socket_get_error(SilcSocketConnection sock, char *error,
+ *                               SilcUInt32 error_len);
+ *
+ * DESCRIPTION
+ *
+ *    Returns human readable error message into the `error' buffer if
+ *    the socket is in error status.  Returns TRUE if error message was
+ *    written into the buffer and FALSE if there is not socket error.
+ *
+ ***/
+bool silc_socket_get_error(SilcSocketConnection sock, char *error,
+                          SilcUInt32 error_len);
+
 /****f* silcutil/SilcSocketConnectionAPI/SilcSocketConnectionHBCb
  *
  * SYNOPSIS
@@ -330,7 +387,7 @@ typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
  * SYNOPSIS
  *
  *    void silc_socket_set_heartbeat(SilcSocketConnection sock, 
- *                                   uint32 heartbeat,
+ *                                   SilcUInt32 heartbeat,
  *                                   void *hb_context,
  *                                   SilcSocketConnectionHBCb hb_callback,
  *                                   SilcSchedule schedule);
@@ -342,16 +399,48 @@ typedef void (*SilcSocketConnectionHBCb)(SilcSocketConnection sock,
  *    allocated by the application and will be sent as argument to the
  *    `hb_callback' function that is called when the `heartbeat' timeout
  *    expires.  The callback `hb_context' won't be touched by the library
- *    but will be freed automatically when calling silc_socket_free.  The
- *    `schedule' is the application's scheduler.
+ *    but and must be freed by the application.  The `schedule' is the
+ *    application's scheduler.
  *
  ***/
 void silc_socket_set_heartbeat(SilcSocketConnection sock, 
-                              uint32 heartbeat,
+                              SilcUInt32 heartbeat,
                               void *hb_context,
                               SilcSocketConnectionHBCb hb_callback,
                               SilcSchedule schedule);
 
+/****f* silcutil/SilcSocketConnectionAPI/silc_socket_set_qos
+ *
+ * SYNOPSIS
+ *
+ *    void silc_socket_set_qos(SilcSocketConnection sock, 
+ *                             SilcUInt32 read_rate,
+ *                             SilcUInt32 read_limit_bytes,
+ *                             SilcUInt32 limit_sec,
+ *                             SilcUInt32 limit_usec,
+ *                             SilcSchedule schedule)
+ *
+ * DESCRIPTION
+ *
+ *    Sets a "Quality of Service" settings for socket connection `sock'.
+ *    The `read_rate' specifies the maximum read operations per second.
+ *    If more read operations are executed the limit will be applied for
+ *    the reading.  The `read_limit_bytes' specifies the maximum data
+ *    that is read.  It is guaranteed that silc_socket_read never returns
+ *    more that `read_limit_bytes' of data.  If more is read the limit
+ *    will be applied for the reading.  The `limit_sec' and `limit_usec'
+ *    specifies the limit that is applied if `read_rate' and/or 
+ *    `read_limit_bytes' is reached.  The `schedule' is the application's
+ *    scheduler.
+ *
+ ***/
+void silc_socket_set_qos(SilcSocketConnection sock, 
+                        SilcUInt32 read_rate,
+                        SilcUInt32 read_limit_bytes,
+                        SilcUInt32 limit_sec,
+                        SilcUInt32 limit_usec,
+                        SilcSchedule schedule);
+
 /****f* silcutil/SilcSocketConnectionAPI/SilcSocketHostLookupCb
  *
  * SYNOPSIS