Merged from silc_1_0_branch.
[silc.git] / lib / silccore / silcpacket.h
index 2d5ba45dd5e1b00badfc3cf1204b02afe71f0813..b2c9dd2790dc0b121ffc10c5f3919e452aa3dd01 100644 (file)
@@ -1,24 +1,23 @@
-/****h* silccore/silcpacket.h
- *
- * NAME
- *
- * silcpacket.h
- *
- * COPYRIGHT
- *
- * Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
- *
- * Copyright (C) 1997 - 2001 Pekka Riikonen
- *
- * This program 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 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+/*
+
+  silcpacket.h 
+
+  Author: Pekka Riikonen <priikone@silcnet.org>
+
+  Copyright (C) 1997 - 2001 Pekka Riikonen
+
+  This program 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; version 2 of the License.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+
+/****h* silccore/Packet Protocol Interface
  *
  * DESCRIPTION
  *
 #ifndef SILCPACKET_H
 #define SILCPACKET_H
 
-/* Amount of bytes to be read from the socket connection at once. */
-#define SILC_PACKET_READ_SIZE 16384
-
-/* Default byte size of the packet. This can be set larger if this
-   is not enough, we shall see. */
-#define SILC_PACKET_DEFAULT_SIZE 2048
+/* Default byte size of the packet. */
+#define SILC_PACKET_DEFAULT_SIZE SILC_SOCKET_BUF_SIZE
 
 /* Header length without source and destination ID's. */
-#define SILC_PACKET_HEADER_LEN 8 + 2
+#define SILC_PACKET_HEADER_LEN 10
 
 /* Minimum length of SILC Packet Header. This much is decrypted always
    when packet is received to be able to get all the relevant data out
    from the header. */
-#define SILC_PACKET_MIN_HEADER_LEN 16 + 2
+#define SILC_PACKET_MIN_HEADER_LEN 16
 
 /* Maximum padding length */
-#define SILC_PACKET_MAX_PADLEN 16
+#define SILC_PACKET_MAX_PADLEN 128
+
+/* Default padding length */
+#define SILC_PACKET_DEFAULT_PADLEN 16
 
 /* Minimum packet length */
 #define SILC_PACKET_MIN_LEN (SILC_PACKET_HEADER_LEN + 1)
 
+/* Maximum packet length */
+#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
  *
@@ -68,7 +69,7 @@
  *
  * SOURCE
  */
-typedef unsigned char SilcPacketType;
+typedef SilcUInt8 SilcPacketType;
 
 /* SILC Packet types. */
 #define SILC_PACKET_NONE                0       /* NULL, never sent */
@@ -97,7 +98,9 @@ typedef unsigned char SilcPacketType;
 #define SILC_PACKET_REKEY_DONE           23      /* Re-key done */
 #define SILC_PACKET_HEARTBEAT            24      /* Heartbeat */
 #define SILC_PACKET_KEY_AGREEMENT        25      /* Key Agreement request */
-#define SILC_PACKET_CELL_ROUTERS         26      /* Cell routers backup */
+#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 */
@@ -107,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
  *
@@ -128,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
@@ -162,69 +165,81 @@ typedef unsigned char SilcPacketFlags;
  *
  *    Short description of the fields following:
  *
- *    SilcBuffer buffer
- *
- *      The data buffer.
+ *    SilcUInt16 truelen
  *
- *    SilcPacketType type
- *
- *      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
- *    uint16 src_id_len
- *    unsigned char src_id_type
+ *    SilcUInt8 src_id_len
+ *    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;
- *    uint16 dst_id_len;
- *    unsigned char src_id_type;
+ *    SilcUInt8 dst_id_len;
+ *    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.
  *
- *    uint16 truelen
- *    uint16 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.
  *
- *    in 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.  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;
-  SilcPacketType type;
+  SilcUInt16 truelen;
   SilcPacketFlags flags;
+  SilcPacketType type;
 
   unsigned char *src_id;
-  uint16 src_id_len;
-  unsigned char src_id_type;
-
   unsigned char *dst_id;
-  uint16 dst_id_len;
-  unsigned char dst_id_type;
-
-  uint16 truelen;
-  uint16 padlen;
-
-  /* Back pointers */
-  void *context;
-  SilcSocketConnection sock;
-
-  int users;
+  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
@@ -235,10 +250,11 @@ typedef struct {
  *
  * DESCRIPTION
  *
- *    This context is used in packet reception when silc_packet_receive_process
- *    function calls parser callback that performs the actual packet decryption
- *    and parsing. This context is sent as argument to the parser function.
- *    This context must be free'd by the parser callback function.
+ *    This context is used in packet reception when the function
+ *    silc_packet_receive_process calls parser callback that performs
+ *    the actual packet decryption and parsing. This context is sent as
+ *    argument to the parser function. This context must be free'd by
+ *    the parser callback function.
  *
  *    Following description of the fields:
  *
@@ -248,6 +264,11 @@ typedef struct {
  *      context is not parsed, only the packet->buffer is allocated and
  *      it includes the raw packet data, which is encrypted.
  *
+ *    bool normal
+ *
+ *      Indicates whether the received packet is normal or special packet.
+ *      If special the parsing process is special also.
+ *
  *    SilcSocketConnection sock
  *
  *      The associated connection.
@@ -261,6 +282,7 @@ typedef struct {
  ***/
 typedef struct {
   SilcPacketContext *packet;
+  bool normal;
   SilcSocketConnection sock;
   void *context;
 } SilcPacketParserContext;
@@ -269,7 +291,7 @@ typedef struct {
  *
  * SYNOPSIS
  *
- *    typedef void (*SilcPacketParserCallback)(SilcPacketParserContext 
+ *    typedef bool (*SilcPacketParserCallback)(SilcPacketParserContext 
  *                                             *parse_context);
  *
  * DESCRIPTION
@@ -282,43 +304,17 @@ typedef struct {
  *    context. The application receiving the SilcPacketParserContext
  *    must free it.
  *
- ***/
-typedef void (*SilcPacketParserCallback)(SilcPacketParserContext 
-                                        *parse_context);
-
-/****f* silccore/SilcPacketAPI/SilcPacketCheckDecrypt
- *
- * SYNOPSIS
- *
- *    typedef int (*SilcPacketCheckDecrypt)(SilcPacketType packet_type,
- *                                          SilcBuffer buffer,
- *                                          SilcPacketContext *packet,
- *                                          void *context);
- *
- * DESCRIPTION
- *
- *    This callback function relates to the checking whether the packet is
- *    normal packet or special packet and how it should be processed.  If
- *    the callback returns TRUE the packet is normal and FALSE if the packet
- *    is special and requires special procesing. Some of the packets in
- *    SILC are special (like channel message packets that are encrypted
- *    using channel specific keys) and requires special processing. That
- *    is the reason for this callback function.
- *
- *    The library will call this function if provided for the
- *    silc_packet_decrypt function. The `packet_type' is the type of
- *    packet received (this is also actually the first time application
- *    receives information of the received packet, next time it receives
- *    it is when the SilcPacketParserCallback function is called),
- *    the `buffer' is the raw packet data the `packet' the allocated
- *    SilcPacketContext that is filled when parsing the packet and `context'
- *    is application specific user context.
+ *    This returns TRUE if the library should continue packet processing
+ *    (assuming there is more data to be processed), and FALSE if the
+ *    upper layer does not want the library to continue but to leave the
+ *    rest of the data is the packet queue untouched.  Application may
+ *    want to do this for example if the cipher is not ready before 
+ *    processing a certain packet.  In this case the application wants
+ *    to recall the processing function with the correct cipher.
  *
  ***/
-typedef int (*SilcPacketCheckDecrypt)(SilcPacketType packet_type,
-                                     SilcBuffer buffer,
-                                     SilcPacketContext *packet,
-                                     void *context);
+typedef bool (*SilcPacketParserCallback)(SilcPacketParserContext 
+                                        *parse_context, void *context);
 
 /* Macros */
 
@@ -330,20 +326,42 @@ typedef int (*SilcPacketCheckDecrypt)(SilcPacketType packet_type,
  *
  * DESCRIPTION
  *
- *    Returns true length of the packet and padded length of the packet.
- *    This is primarily used by the libary in packet parsing phase but
- *    the application may use it as well if needed.
+ *    Returns true length of the packet. This is primarily used by the
+ *    libary in packet parsing phase but the application may use it as
+ *    well if needed.
  *
  * SOURCE
  */
-#define SILC_PACKET_LENGTH(__packet, __ret_truelen, __ret_padlen)           \
-do {                                                                        \
-  SILC_GET16_MSB((__ret_truelen), (__packet)->data);                        \
-  (__ret_padlen) = (((__ret_truelen) - 2) +                                 \
-                   SILC_PACKET_MAX_PADLEN) & ~(SILC_PACKET_MAX_PADLEN - 1); \
+#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)
 /***/
 
+/****d* silccore/SilcPacketAPI/SILC_PACKET_DATALEN
+ *
+ * NAME
+ * 
+ *    #define SILC_PACKET_DATALEN ...
+ *
+ * DESCRIPTION
+ *
+ *    Calculates the data length with given header length.  This macro
+ *    can be used to check whether the data_len with header_len exceeds
+ *    SILC_PACKET_MAX_LEN.  If it does, this returns the new data_len
+ *    so that the SILC_PACKET_MAX_LEN is not exceeded.  If the data_len
+ *    plus header_len fits SILC_PACKET_MAX_LEN the returned data length
+ *    is the data_len given as argument.  This macro can be used when
+ *    assembling packet.
+ *
+ * SOURCE
+ */
+#define SILC_PACKET_DATALEN(data_len, header_len)                        \
+  ((data_len + header_len) > SILC_PACKET_MAX_LEN ?                       \
+   data_len - ((data_len + header_len) - SILC_PACKET_MAX_LEN) : data_len)
+/***/
+
 /****d* silccore/SilcPacketAPI/SILC_PACKET_PADLEN
  *
  * NAME
@@ -352,39 +370,48 @@ 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)                                         \
-  SILC_PACKET_MAX_PADLEN - ((__packetlen) - 2) % SILC_PACKET_MAX_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)
 /***/
 
-/* Prototypes */
-
-/****f* silccore/SilcPacketAPI/silc_packet_write
- *
- * SYNOPSIS
+/****d* silccore/SilcPacketAPI/SILC_PACKET_PADLEN_MAX
  *
- *    int silc_packet_write(int sock, SilcBuffer src);
+ * NAME
+ * 
+ *    #define SILC_PACKET_PADLEN_MAX ...
  *
  * DESCRIPTION
  *
- *    Writes data from encrypted buffer to the socket connection. If the
- *    data cannot be written at once, it will be written later with a timeout. 
- *    The data is written from the data section of the buffer, not from head
- *    or tail section. This automatically pulls the data section towards end
- *    after writing the data.
+ *    Returns the length of the padding up to the maximum length, which
+ *    is 128 bytes. This is used by various library routines to determine
+ *    needed padding length.
  *
- ***/
-int silc_packet_write(int sock, SilcBuffer src);
+ * SOURCE
+ */
+#define SILC_PACKET_PADLEN_MAX(__packetlen, __blocklen, __padlen)         \
+do {                                                                      \
+  __padlen = (SILC_PACKET_MAX_PADLEN - (__packetlen) %                            \
+             ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)); \
+} while(0)
+/***/
+
+/* Prototypes */
 
 /****f* silccore/SilcPacketAPI/silc_packet_send
  *
  * SYNOPSIS
  *
- *    int silc_packet_send(SilcSocketConnection sock, int force_send);
+ *    int silc_packet_send(SilcSocketConnection sock, bool force_send);
  *
  * DESCRIPTION
  *
@@ -397,14 +424,14 @@ int silc_packet_write(int sock, SilcBuffer src);
  *    directly to the network, if FALSE, this returns -2.
  *
  ***/
-int silc_packet_send(SilcSocketConnection sock, int force_send);
+int silc_packet_send(SilcSocketConnection sock, bool force_send);
 
 /****f* silccore/SilcPacketAPI/silc_packet_encrypt
  *
  * SYNOPSIS
  *
  *    void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, 
- *                             SilcBuffer buffer, uint32 len);
+ *                             SilcBuffer buffer, SilcUInt32 len);
  *
  * DESCRIPTION
  *
@@ -416,110 +443,73 @@ int silc_packet_send(SilcSocketConnection sock, int force_send);
  *    cannot be used. 
  *
  ***/
-void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, 
-                        SilcBuffer buffer, uint32 len);
+void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, SilcUInt32 sequence,
+                        SilcBuffer buffer, SilcUInt32 len);
 
 /****f* silccore/SilcPacketAPI/silc_packet_assemble
  *
  * SYNOPSIS
  *
- *    void silc_packet_assemble(SilcPacketContext *ctx);
+ *    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);
  *
  * DESCRIPTION
  *
- *    Assembles a new packet to be ready for send out. The buffer sent as
- *    argument must include the data to be sent and it must not be encrypted. 
- *    The packet also must have enough free space so that the SILC header
- *    and padding maybe added to the packet. The packet is encrypted after 
- *    this function has returned.
- *
- *    The buffer sent as argument should be something like following:
- *
- *    --------------------------------------------
- *    | head             | data           | tail |
- *    --------------------------------------------
- *    ^                  ^
- *    58 bytes           x bytes
- *
- *    So that the SILC header and 1 - 16 bytes of padding can fit to
- *    the buffer. After assembly the buffer might look like this:
- *
- *    --------------------------------------------
- *    | data                              |      |
- *    --------------------------------------------
- *    ^                                   ^
- *    Start of assembled packet
- *
- *    Packet construct is as follows (* = won't be encrypted):
- *
- *    n bytes       SILC Header
- *      2 bytes     Payload length  (*)
- *      1 byte      Flags
- *      1 byte      Packet type
- *      2 bytes     Source ID Length
- *      2 bytes     Destination ID Length
- *      1 byte      Source ID Type
- *      n bytes     Source ID
- *      1 byte      Destination ID Type
- *      n bytes     Destination ID
- *
- *    1 - 16 bytes    Padding
- *
- *    n bytes        Data payload
- *
- *    All fields in the packet will be authenticated by MAC. The MAC is
- *    not computed here, it must be computed separately before encrypting
- *    the packet.
+ *    Assembles new packet to be ready for encrypting and sending out.
+ *    The `packet' is filled by caller to include the packet header specific
+ *    values.  This prepares the socket connection's `sock' outoing buffer
+ *    for sending data, and returns the assembled packet to the 
+ *    `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.  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.
  *
  ***/
-void silc_packet_assemble(SilcPacketContext *ctx);
+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);
 
 /****f* silccore/SilcPacketAPI/silc_packet_send_prepare
  *
  * SYNOPSIS
  *
- *    void silc_packet_send_prepare(SilcSocketConnection sock,
- *                                  uint32 header_len,
- *                                  uint32 padlen,
- *                                  uint32 data_len);
- *
- * DESCRIPTION
- *
- *    Prepare outgoing data buffer for packet sending. This moves the data
- *    area so that new packet may be added into it. If needed this allocates
- *    more space to the buffer. This handles directly the connection's
- *    outgoing buffer in SilcSocketConnection object.
- *
- ***/
-void silc_packet_send_prepare(SilcSocketConnection sock,
-                             uint32 header_len,
-                             uint32 padlen,
-                             uint32 data_len);
-
-/****f* silccore/SilcPacketAPI/silc_packet_read
- *
- * SYNOPSIS
- *
- *    int silc_packet_read(int sock, SilcBuffer dest);
+ *    bool silc_packet_send_prepare(SilcSocketConnection sock,
+ *                                  SilcUInt32 header_len,
+ *                                  SilcUInt32 pad_len,
+ *                                  SilcUInt32 data_len,
+ *                                  SilcHmac hmac,
+ *                                  const SilcBuffer packet);
  *
  * DESCRIPTION
  *
- *    Reads data from the socket connection into the incoming data buffer.
- *    However, this does not parse the packet, it only reads some amount from
- *    the network. If there are more data available that can be read at a time
- *    the rest of the data will be read later with a timeout and only after
- *    that the packet is ready to be parsed. 
+ *    This function can be used to prepare the outgoing data buffer in
+ *    the socket connection specified by `sock' for packet sending.
+ *    This is used internally by packet sending routines, but application
+ *    may call this if it doesn't call silc_packet_assemble function.
+ *    If that function is called then application must not call this since
+ *    that function calls this internally.
  *
- *    The destination buffer sent as argument must be initialized before 
- *    calling this function, and, the data section and the start of the tail
- *    section must be same. Ie. we add the read data to the tail section of
- *    the buffer hence the data section is the start of the buffer.
- *
- *    This returns amount of bytes read or -1 on error or -2 on case where
- *    all of the data could not be read at once.
+ *    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.  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.
  *
  ***/
-int silc_packet_read(int sock, SilcBuffer dest);
+bool silc_packet_send_prepare(SilcSocketConnection sock,
+                             SilcUInt32 header_len,
+                             SilcUInt32 pad_len,
+                             SilcUInt32 data_len,
+                             SilcHmac hmac,
+                             const SilcBuffer packet);
 
 /****f* silccore/SilcPacketAPI/silc_packet_receive
  *
@@ -539,60 +529,38 @@ int silc_packet_read(int sock, SilcBuffer dest);
  ***/
 int silc_packet_receive(SilcSocketConnection sock);
 
-/****f* silccore/SilcPacketAPI/silc_packet_decrypt
- *
- * SYNOPSIS
- *
- *    int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
- *                            SilcBuffer buffer, SilcPacketContext *packet,
- *                            SilcPacketCheckDecrypt check_packet,
- *                            void *context);
- *
- * DESCRIPTION
- *
- *    Decrypts a packet. This assumes that typical SILC packet is the
- *    packet to be decrypted and thus checks for normal and special SILC
- *    packets and can handle both of them. This also computes and checks
- *    the HMAC of the packet. If any other special or customized decryption
- *    processing is required this function cannot be used. This returns
- *    -1 on error, 0 when packet is normal packet and 1 when the packet
- *    is special and requires special processing. 
- *
- *    The `check_packet' is a callback funtion that this function will 
- *    call.  The callback relates to the checking whether the packet is
- *    normal packet or special packet and how it should be processed.  If
- *    the callback return TRUE the packet is normal and FALSE if the packet
- *    is special and requires special procesing.
- *
- ***/
-int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
-                       SilcBuffer buffer, SilcPacketContext *packet,
-                       SilcPacketCheckDecrypt check_packet,
-                       void *context);
-
 /****f* silccore/SilcPacketAPI/silc_packet_receive_process
  *
  * SYNOPSIS
  *
- *    void silc_packet_receive_process(SilcSocketConnection sock,
+ *    bool silc_packet_receive_process(SilcSocketConnection sock,
+ *                                     bool local_is_router,
  *                                     SilcCipher cipher, SilcHmac hmac,
  *                                     SilcPacketParserCallback parser,
- *                                     void *context);
+ *                                     void *parser_context);
  *
  * DESCRIPTION
  *
- *    Processes the received data. This checks the received data and 
- *    calls parser callback that handles the actual packet decryption
- *    and parsing. If more than one packet was received this calls the
- *    parser multiple times. The parser callback will get context
- *    SilcPacketParserContext that includes the packet and the `context'
- *    sent to this function.
+ *    Processes and decrypts the incoming data, and calls parser callback
+ *    for each received packet that will handle the actual packet parsing.
+ *    If more than one packet was received this calls the parser multiple
+ *    times.  The parser callback will get context SilcPacketParserContext
+ *    that includes the packet and the `parser_context' sent to this
+ *    function. 
+ *
+ *    The `local_is_router' indicates whether the caller is router server
+ *    in which case the receiving process of a certain packet types may
+ *    be special.  Normal server and client must set it to FALSE.  The
+ *    SilcPacketParserContext will indicate also whether the received
+ *    packet was normal or special packet.
  *
  ***/
-void silc_packet_receive_process(SilcSocketConnection sock,
+bool silc_packet_receive_process(SilcSocketConnection sock,
+                                bool local_is_router,
                                 SilcCipher cipher, SilcHmac hmac,
+                                SilcUInt32 sequence,
                                 SilcPacketParserCallback parser,
-                                void *context);
+                                void *parser_context);
 
 /****f* silccore/SilcPacketAPI/silc_packet_parse
  *
@@ -609,7 +577,7 @@ void silc_packet_receive_process(SilcSocketConnection sock,
  *    buffer is parsed, not head or tail sections.
  *
  ***/
-SilcPacketType silc_packet_parse(SilcPacketContext *ctx);
+SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher);
 
 /****f* silccore/SilcPacketAPI/silc_packet_parse_special
  *
@@ -626,7 +594,8 @@ SilcPacketType silc_packet_parse(SilcPacketContext *ctx);
  *    and parses the header and padding area only.
  *
  ***/
-SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx);
+SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx,
+                                        SilcCipher cipher);
 
 /****f* silccore/SilcPacketAPI/silc_packet_context_alloc
  *
@@ -641,7 +610,7 @@ SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx);
  *    silc_packet_context_free function.
  *
  ***/
-SilcPacketContext *silc_packet_context_alloc();
+SilcPacketContext *silc_packet_context_alloc(void);
 
 /****f* silccore/SilcPacketAPI/silc_packet_context_dup
  *