+#include "silc.h"
+
+/************************** Types and definitions ***************************/
+
+/* Per scheduler (which usually means per thread) data. We put per scheduler
+ data here for accessing without locking. SILC Schedule dictates that
+ tasks are dispatched in one thread, hence the per scheduler context. */
+typedef struct {
+ SilcSchedule schedule; /* The scheduler */
+ SilcPacketEngine engine; /* Packet engine */
+ SilcBufferStruct inbuf; /* Data input buffer */
+ SilcUInt32 stream_count; /* Number of streams using this */
+} *SilcPacketEngineContext;
+
+/* Packet engine */
+struct SilcPacketEngineStruct {
+ SilcMutex lock; /* Engine lock */
+ SilcRng rng; /* RNG for engine */
+ SilcHashTable contexts; /* Per scheduler contexts */
+ SilcPacketCallbacks *callbacks; /* Packet callbacks */
+ void *callback_context; /* Context for callbacks */
+ SilcList streams; /* All streams in engine */
+ SilcList packet_pool; /* Free list for received packets */
+ SilcHashTable udp_remote; /* UDP remote streams, or NULL */
+ unsigned int local_is_router : 1;
+};
+
+/* Packet processor context */
+typedef struct SilcPacketProcessStruct {
+ SilcPacketType *types; /* Packets to process */
+ SilcPacketCallbacks *callbacks; /* Callbacks or NULL */
+ void *callback_context;
+ SilcInt32 priority; /* Priority */
+} *SilcPacketProcess;
+
+/* UDP remote stream tuple */
+typedef struct {
+ char *remote_ip; /* Remote IP address */
+ SilcUInt16 remote_port; /* Remote port */
+} *SilcPacketRemoteUDP;
+
+/* Packet stream */
+struct SilcPacketStreamStruct {
+ struct SilcPacketStreamStruct *next;
+ SilcPacketEngineContext sc; /* Per scheduler context */
+ SilcStream stream; /* Underlaying stream */
+ SilcMutex lock; /* Stream lock */
+ SilcDList process; /* Packet processors, or NULL */
+ SilcPacketRemoteUDP remote_udp; /* UDP remote stream tuple, or NULL */
+ void *stream_context; /* Stream context */
+ SilcBufferStruct outbuf; /* Out buffer */
+ SilcCipher send_key[2]; /* Sending key */
+ SilcHmac send_hmac[2]; /* Sending HMAC */
+ SilcCipher receive_key[2]; /* Receiving key */
+ SilcHmac receive_hmac[2]; /* Receiving HMAC */
+ unsigned char *src_id; /* Source ID */
+ unsigned char *dst_id; /* Destination ID */
+ SilcUInt32 send_psn; /* Sending sequence */
+ SilcUInt32 receive_psn; /* Receiving sequence */
+ SilcAtomic8 refcnt; /* Reference counter */
+ SilcUInt8 sid; /* Security ID, set if IV included */
+ unsigned int src_id_len : 6;
+ unsigned int src_id_type : 2;
+ unsigned int dst_id_len : 6;
+ unsigned int dst_id_type : 2;
+ unsigned int is_router : 1; /* Set if router stream */
+ unsigned int destroyed : 1; /* Set if destroyed */
+ unsigned int iv_included : 1; /* Set if IV included */
+ unsigned int udp : 1; /* UDP remote stream */
+};
+
+/* Initial size of stream buffers */
+#define SILC_PACKET_DEFAULT_SIZE 1024
+
+/* Header length without source and destination ID's. */
+#define SILC_PACKET_HEADER_LEN 10
+
+/* Minimum length of SILC Packet Header. */
+#define SILC_PACKET_MIN_HEADER_LEN 16
+#define SILC_PACKET_MIN_HEADER_LEN_IV 32 + 1
+
+/* Maximum padding length */
+#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)
+
+/* Returns true length of the packet. */
+#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)
+
+/* 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. */
+#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)
+
+/* Calculates the length of the padding in the packet. */
+#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)
+
+/* Returns the length of the padding up to the maximum length, which
+ is 128 bytes.*/
+#define SILC_PACKET_PADLEN_MAX(__packetlen, __blocklen, __padlen) \
+do { \
+ __padlen = (SILC_PACKET_MAX_PADLEN - (__packetlen) % \
+ ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)); \
+} while(0)
+
+/* EOS callback */
+#define SILC_PACKET_CALLBACK_EOS(s) \
+do { \
+ (s)->sc->engine->callbacks->eos((s)->sc->engine, s, \
+ (s)->sc->engine->callback_context, \
+ (s)->stream_context); \
+} while(0)
+
+/* Error callback */
+#define SILC_PACKET_CALLBACK_ERROR(s, err) \
+do { \
+ (s)->sc->engine->callbacks->error((s)->sc->engine, s, err, \
+ (s)->sc->engine->callback_context, \
+ (s)->stream_context); \
+} while(0)
+
+static SilcBool silc_packet_dispatch(SilcPacket packet);
+static void silc_packet_read_process(SilcPacketStream stream);
+static inline SilcBool silc_packet_send_raw(SilcPacketStream stream,
+ SilcPacketType type,
+ SilcPacketFlags flags,
+ SilcIdType src_id_type,
+ unsigned char *src_id,
+ SilcUInt32 src_id_len,
+ SilcIdType dst_id_type,
+ unsigned char *dst_id,
+ SilcUInt32 dst_id_len,
+ const unsigned char *data,
+ SilcUInt32 data_len,
+ SilcCipher cipher,
+ SilcHmac hmac);
+
+/************************ Static utility functions **************************/
+
+/* Injects packet to new stream created with silc_packet_stream_add_remote. */
+
+SILC_TASK_CALLBACK(silc_packet_stream_inject_packet)
+{
+ SilcPacket packet = context;
+ SilcPacketStream stream = packet->stream;