+
+/****f* silccore/SilcPacketAPI/silc_packet_send
+ *
+ * SYNOPSIS
+ *
+ * int silc_packet_send(SilcSocketConnection sock, bool force_send);
+ *
+ * DESCRIPTION
+ *
+ * Actually sends the packet. This flushes the connections outgoing data
+ * buffer. If data is sent directly to the network this returns the bytes
+ * written, if error occured this returns -1 and if the data could not
+ * be written directly to the network at this time this returns -2, in
+ * which case the data should be queued by the caller and sent at some
+ * later time. If `force_send' is TRUE this attempts to write the data
+ * directly to the network, if FALSE, this returns -2.
+ *
+ ***/
+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, SilcUInt32 len);
+ *
+ * DESCRIPTION
+ *
+ * Encrypts a packet. This also creates HMAC of the packet before
+ * encryption and adds the HMAC at the end of the buffer. This assumes
+ * that there is enough free space at the end of the buffer to add the
+ * computed HMAC. This is the normal way of encrypting packets, if some
+ * other process of HMAC computing and encryption is needed this function
+ * cannot be used.
+ *
+ ***/
+void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, SilcUInt32 sequence,
+ SilcBuffer buffer, SilcUInt32 len);
+
+/****f* silccore/SilcPacketAPI/silc_packet_assemble
+ *
+ * SYNOPSIS
+ *
+ * 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 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.
+ *
+ ***/
+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
+ *
+ * bool silc_packet_send_prepare(SilcSocketConnection sock,
+ * SilcUInt32 header_len,
+ * SilcUInt32 pad_len,
+ * SilcUInt32 data_len,
+ * SilcHmac hmac,
+ * const SilcBuffer packet);
+ *
+ * DESCRIPTION
+ *
+ * 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.
+ *
+ * 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.
+ *
+ ***/
+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
+ *
+ * SYNOPSIS
+ *
+ * int silc_packet_receive(SilcSocketConnection sock);
+ *
+ * DESCRIPTION
+ *
+ * Receives packet from network and reads the data into connection's
+ * incoming data buffer. If the data was read directly this returns the
+ * read bytes, if error occured this returns -1, if the data could not
+ * be read directly at this time this returns -2 in which case the data
+ * should be read again at some later time, or If EOF occured this returns
+ * 0.
+ *
+ ***/
+int silc_packet_receive(SilcSocketConnection sock);
+
+/****f* silccore/SilcPacketAPI/silc_packet_receive_process
+ *
+ * SYNOPSIS
+ *
+ * bool silc_packet_receive_process(SilcSocketConnection sock,
+ * bool local_is_router,
+ * SilcCipher cipher, SilcHmac hmac,
+ * SilcPacketParserCallback parser,
+ * void *parser_context);
+ *
+ * DESCRIPTION
+ *
+ * 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.
+ *
+ ***/
+bool silc_packet_receive_process(SilcSocketConnection sock,
+ bool local_is_router,
+ SilcCipher cipher, SilcHmac hmac,
+ SilcUInt32 sequence,
+ SilcPacketParserCallback parser,
+ void *parser_context);
+
+/****f* silccore/SilcPacketAPI/silc_packet_parse
+ *
+ * SYNOPSIS
+ *
+ * SilcPacketType silc_packet_parse(SilcPacketContext *ctx);
+ *
+ * DESCRIPTION
+ *
+ * Parses the packet. This is called when a whole packet is ready to be
+ * parsed. The buffer sent must be already decrypted before calling this
+ * function. The len argument must be the true length of the packet. This
+ * function returns the type of the packet. The data section of the
+ * buffer is parsed, not head or tail sections.
+ *
+ ***/
+SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher);
+
+/****f* silccore/SilcPacketAPI/silc_packet_parse_special
+ *
+ * SYNOPSIS
+ *
+ * SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx);
+ *
+ * DESCRIPTION
+ *
+ * Perform special SILC Packet header parsing. This is required to some
+ * packet types that have the data payload encrypted with different key
+ * than the header area plus padding of the packet. Hence, this parses
+ * the header in a way that it does not take the data area into account
+ * and parses the header and padding area only.
+ *
+ ***/
+SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx,
+ SilcCipher cipher);
+
+/****f* silccore/SilcPacketAPI/silc_packet_context_alloc
+ *
+ * SYNOPSIS
+ *
+ * SilcPacketContext *silc_packet_context_alloc();
+ *
+ * DESCRIPTION
+ *
+ * Allocates a packet context. Packet contexts are used when
+ * packets are assembled and parsed. The context is freed by the
+ * silc_packet_context_free function.
+ *
+ ***/
+SilcPacketContext *silc_packet_context_alloc(void);
+
+/****f* silccore/SilcPacketAPI/silc_packet_context_dup
+ *
+ * SYNOPSIS
+ *
+ * SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx);
+ *
+ * DESCRIPTION
+ *
+ * Duplicates the packet context. It actually does not duplicate
+ * any data, instead a reference counter is increased.
+ *
+ ***/
+SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx);
+
+/****f* silccore/SilcPacketAPI/silc_packet_context_free
+ *
+ * SYNOPSIS
+ *
+ * void silc_packet_context_free(SilcPacketContext *ctx);
+ *
+ * DESCRIPTION
+ *
+ * Frees the packet context. The context is actually freed when the
+ * reference counter hits zero.
+ *
+ ***/
+void silc_packet_context_free(SilcPacketContext *ctx);