Merged from silc_1_0_branch.
[silc.git] / lib / silccore / silcpacket.h
index d4f28ededd3fc87a0661f2c6b73ae2d7af564ebd..b2c9dd2790dc0b121ffc10c5f3919e452aa3dd01 100644 (file)
 #define SILC_PACKET_MAX_LEN 0xffff
 
 /* Maximum length of ID */
-#define SILC_PACKET_MAX_ID_LEN 16
+#define SILC_PACKET_MAX_ID_LEN 28
 
 /****d* silccore/SilcPacketAPI/SilcPacketType
  *
  * NAME
  * 
- *    typedef unsigned char SilcPacketType;
+ *    typedef SilcUInt8 SilcPacketType;
  *
  * DESCRIPTION
  *
@@ -69,7 +69,7 @@
  *
  * SOURCE
  */
-typedef unsigned char SilcPacketType;
+typedef SilcUInt8 SilcPacketType;
 
 /* SILC Packet types. */
 #define SILC_PACKET_NONE                0       /* NULL, never sent */
@@ -100,6 +100,7 @@ typedef unsigned char SilcPacketType;
 #define SILC_PACKET_KEY_AGREEMENT        25      /* Key Agreement request */
 #define SILC_PACKET_RESUME_ROUTER        26      /* Backup router resume */
 #define SILC_PACKET_FTP                  27      /* File Transfer */
+#define SILC_PACKET_RESUME_CLIENT        28      /* Client resume */
 
 #define SILC_PACKET_PRIVATE              200     /* Private range start  */
 #define SILC_PACKET_MAX                  255     /* RESERVED */
@@ -109,20 +110,20 @@ typedef unsigned char SilcPacketType;
  *
  * NAME
  * 
- *    typedef unsigned char SilcPacketVersion;
+ *    typedef SilcUInt8 SilcPacketVersion;
  *
  * DESCRIPTION
  *
  *    SILC packet version type definition.
  *
  ***/
-typedef unsigned char SilcPacketVersion;
+typedef SilcUInt8 SilcPacketVersion;
 
 /****d* silccore/SilcPacketAPI/SilcPacketFlags
  *
  * NAME
  * 
- *    typedef unsigned char SilcPacketFlags;
+ *    typedef SilcUInt8 SilcPacketFlags;
  *
  * DESCRIPTION
  *
@@ -130,17 +131,17 @@ typedef unsigned char SilcPacketVersion;
  *
  * SOURCE
  */
-typedef unsigned char SilcPacketFlags;
+typedef SilcUInt8 SilcPacketFlags;
 
 /* All defined packet flags */
 #define SILC_PACKET_FLAG_NONE             0x00    /* No flags */
 #define SILC_PACKET_FLAG_PRIVMSG_KEY      0x01   /* Private message key */
 #define SILC_PACKET_FLAG_LIST             0x02   /* Packet is a list */
 #define SILC_PACKET_FLAG_BROADCAST        0x04   /* Packet is a broadcast */
+#define SILC_PACKET_FLAG_COMPRESSED       0x08    /* Payload is compressed */
 /***/
 
 /* Rest of flags still available
-#define SILC_PACKET_FLAG_XXX              0x08
 #define SILC_PACKET_FLAG_XXX              0x10
 #define SILC_PACKET_FLAG_XXX              0x20
 #define SILC_PACKET_FLAG_XXX              0x40
@@ -164,73 +165,81 @@ typedef unsigned char SilcPacketFlags;
  *
  *    Short description of the fields following:
  *
- *    SilcBuffer buffer
- *
- *      The data buffer.
- *
- *    SilcPacketType type
+ *    SilcUInt16 truelen
  *
- *      Type of the packet. Types are defined below.
+ *      True length of the packet.  This may be set by the caller before
+ *      calling any of the silc_packet_* routines.  If not provided the
+ *      library will calculate the values.
  *
  *    SilcPacketFlags flags
  *
  *      Packet flags. Flags are defined above.
  *
+ *    SilcPacketType type
+ *
+ *      Type of the packet. Types are defined below.
+ *
  *    unsigned char *src_id
  *    SilcUInt8 src_id_len
- *    unsigned char src_id_type
+ *    SilcUInt8 src_id_type
  *
  *      Source ID, its length and type. On packet reception retuned ID's
  *      are always the hash values of the ID's from the packet.
  *
  *    unsigned char *dst_id;
  *    SilcUInt8 dst_id_len;
- *    unsigned char src_id_type;
+ *    SilcUInt8 src_id_type;
  *
  *      Destination ID, its length and type. On packet reception retuned
  *      ID's are always the hash values of the ID's from the packet.
  *
- *    SilcUInt16 truelen
- *    SilcUInt8 padlen
- *
- *      The true lenght of the packet and the padded length of the packet.
- *      These may be set by the caller before calling any of the 
- *      silc_packet_* routines. If not provided the library will calculate
- *      the values.
+ *    bool long_pad
+ * 
+ *      If set to TRUE the packet will include the maximum padding allowed
+ *      in SILC packet, which is 128 bytes.  If FALSE only the amount of
+ *      padding needed will be applied.
  *
- *    int users;
+ *    SilcUInt16 users;
  *
  *      Reference counter for this context. The context is freed only 
  *      after the reference counter hits zero. The counter is added
  *      calling silc_packet_context_dup and decreased by calling the
  *      silc_packet_context_free.
  *
+ *    SilcUInt8 padlen
+ *
+ *      The padded length of the packet.  This may be set by the caller
+ *      before calling any of the silc_packet_* routines. If not provided
+ *      the library will calculate the values.
+ *
  *    SilcUInt32 sequence;
  *
- *      Packet sequence number.
+ *      Packet sequence number.  Set only when this context is a parsed
+ *      packet.
+ *
+ *    SilcBuffer buffer
+ *
+ *      The actual packet data.  Set only when this context is a parsed
+ *      packet.
  *
  ***/
 typedef struct {
-  SilcBuffer buffer;
-
   SilcUInt16 truelen;
   SilcPacketFlags flags;
   SilcPacketType type;
-  SilcUInt8 padlen;
 
   unsigned char *src_id;
-  SilcUInt8 src_id_len;
-  SilcUInt8 src_id_type;
-
   unsigned char *dst_id;
-  SilcUInt8 dst_id_len;
-  SilcUInt8 dst_id_type;
-
-  int users;
-  bool long_pad;               /* Set to TRUE to use maximum padding
-                                  in packet (up to 256 bytes). */
+  unsigned int src_id_len : 5;
+  unsigned int src_id_type : 2;
+  unsigned int dst_id_len : 5;
+  unsigned int dst_id_type : 2;
+  unsigned int long_pad : 1;     /* Set when maximum padding in packet */
+  unsigned int users : 9;        /* Reference counter */
+  unsigned int padlen : 8;
 
   SilcUInt32 sequence;
+  SilcBuffer buffer;
 } SilcPacketContext;
 
 /****s* silccore/SilcPacketAPI/SilcPacketParserContext
@@ -323,10 +332,10 @@ typedef bool (*SilcPacketParserCallback)(SilcPacketParserContext
  *
  * SOURCE
  */
-#define SILC_PACKET_LENGTH(__packet, __ret_truelen, __ret_paddedlen)   \
-do {                                                                   \
-  SILC_GET16_MSB((__ret_truelen), (__packet)->data);                   \
-  (__ret_paddedlen) = (__ret_truelen) + (__packet)->data[4];           \
+#define SILC_PACKET_LENGTH(__packetdata, __ret_truelen, __ret_paddedlen) \
+do {                                                                    \
+  SILC_GET16_MSB((__ret_truelen), (__packetdata));                      \
+  (__ret_paddedlen) = (__ret_truelen) + (SilcUInt8)(__packetdata)[4];   \
 } while(0)
 /***/
 
@@ -361,14 +370,18 @@ do {                                                                      \
  *
  * DESCRIPTION
  *
- *    Returns the length of the padding in the packet. This is used
+ *    Calculates the length of the padding in the packet. This is used
  *    by various library routines to determine needed padding length.
  *
  * SOURCE
  */
-#define SILC_PACKET_PADLEN(__packetlen, __blocklen)            \
-  SILC_PACKET_DEFAULT_PADLEN - (__packetlen) %                 \
-    ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)
+#define SILC_PACKET_PADLEN(__packetlen, __blocklen, __padlen)              \
+do {                                                                       \
+  __padlen = (SILC_PACKET_DEFAULT_PADLEN - (__packetlen) %                 \
+             ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN));  \
+  if (__padlen < 8)                                                        \
+    __padlen += ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN); \
+} while(0)
 /***/
 
 /****d* silccore/SilcPacketAPI/SILC_PACKET_PADLEN_MAX
@@ -380,13 +393,16 @@ do {                                                                      \
  * DESCRIPTION
  *
  *    Returns the length of the padding up to the maximum length, which
- *    is 128 butes. This is used by various library routines to determine
+ *    is 128 bytes. This is used by various library routines to determine
  *    needed padding length.
  *
  * SOURCE
  */
-#define SILC_PACKET_PADLEN_MAX(__packetlen)                            \
-  SILC_PACKET_MAX_PADLEN - (__packetlen) % SILC_PACKET_MAX_PADLEN
+#define SILC_PACKET_PADLEN_MAX(__packetlen, __blocklen, __padlen)         \
+do {                                                                      \
+  __padlen = (SILC_PACKET_MAX_PADLEN - (__packetlen) %                            \
+             ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)); \
+} while(0)
 /***/
 
 /* Prototypes */
@@ -449,14 +465,16 @@ void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, SilcUInt32 sequence,
  *    `assembled_packet' pointer sent by the caller.  The `assembled_packet'
  *    is a reference to the socket connection's outgoing buffer.  The
  *    returned packet can be encrypted, and then sent to network by calling
- *    silc_packet_send function.
+ *    silc_packet_send function.  The `assembled_packet' may be freely
+ *    modified (like encrypted etc.) but it must not be freed, since it is
+ *    reference from `sock' outgoing buffer, and it is const.
  *
  ***/
 bool silc_packet_assemble(SilcPacketContext *packet, SilcRng rng,
-                          SilcCipher cipher, SilcHmac hmac,
-                          SilcSocketConnection sock,
-                          const unsigned char *data, SilcUInt32 data_len,
-                          const SilcBuffer assembled_packet);
+                         SilcCipher cipher, SilcHmac hmac,
+                         SilcSocketConnection sock,
+                         const unsigned char *data, SilcUInt32 data_len,
+                         const SilcBuffer assembled_packet);
 
 /****f* silccore/SilcPacketAPI/silc_packet_send_prepare
  *
@@ -481,15 +499,17 @@ bool silc_packet_assemble(SilcPacketContext *packet, SilcRng rng,
  *    This returns the prepared data area into the `packet' pointer provided
  *    caller, which can be used then to add data to it, and later encrypt
  *    it.  The `packet' includes reference to the socket connection's
- *    outgoing buffer.
+ *    outgoing buffer.  The `packet' may be freely modified (like 
+ *    encrypted etc.) but it must not be freed, since it is reference from 
+ *    `sock' outgoing buffer, and it is const.
  *
  ***/
 bool silc_packet_send_prepare(SilcSocketConnection sock,
-                              SilcUInt32 header_len,
-                              SilcUInt32 pad_len,
-                              SilcUInt32 data_len,
-                              SilcHmac hmac,
-                              const SilcBuffer packet);
+                             SilcUInt32 header_len,
+                             SilcUInt32 pad_len,
+                             SilcUInt32 data_len,
+                             SilcHmac hmac,
+                             const SilcBuffer packet);
 
 /****f* silccore/SilcPacketAPI/silc_packet_receive
  *