/* Get random padding */
#if 1
- for (i = 0; i < ctx->padlen; i++)
- tmppad[i] = silc_rng_get_byte(ctx->rng);
+ for (i = 0; i < ctx->padlen; i++) tmppad[i] = silc_rng_get_byte(ctx->rng);
#else
/* XXX: For testing - to be removed */
memset(tmppad, 65, sizeof(tmppad));
}
parse_ctx = silc_calloc(1, sizeof(*parse_ctx));
- parse_ctx->packet = silc_calloc(1, sizeof(*parse_ctx->packet));
+ parse_ctx->packet = silc_packet_context_alloc();
parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
parse_ctx->sock = sock;
parse_ctx->cipher = cipher;
int silc_packet_decrypt(SilcCipher cipher, SilcHmac hmac,
SilcBuffer buffer, SilcPacketContext *packet)
{
-#if 0
- SILC_LOG_DEBUG(("Decrypting packet, cipher %s, len %d (%d)",
- cipher->cipher->name, len, len - 2));
-#endif
/* Decrypt start of the packet header */
if (cipher)
} else {
/* Packet requires special handling, decrypt rest of the header.
This only decrypts. */
- silc_packet_decrypt_rest_special(cipher, hmac, buffer);
+ if (!silc_packet_decrypt_rest_special(cipher, hmac, buffer))
+ return -1;
/* Check MAC */
if (!silc_packet_check_mac(hmac, buffer))
SilcPacketType silc_packet_parse(SilcPacketContext *ctx)
{
SilcBuffer buffer = ctx->buffer;
- int len;
+ int len, ret;
SILC_LOG_DEBUG(("Parsing incoming packet"));
SILC_STR_UI_SHORT(&ctx->dst_id_len),
SILC_STR_UI_CHAR(&ctx->src_id_type),
SILC_STR_END);
+ if (len == -1)
+ return SILC_PACKET_NONE;
if (ctx->src_id_len > SILC_PACKET_MAX_ID_LEN ||
ctx->dst_id_len > SILC_PACKET_MAX_ID_LEN) {
ctx->padlen = SILC_PACKET_PADLEN(ctx->truelen);
silc_buffer_pull(buffer, len);
- silc_buffer_unformat(buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id,
- ctx->src_id_len),
- SILC_STR_UI_CHAR(&ctx->dst_id_type),
- SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id,
- ctx->dst_id_len),
- SILC_STR_UI_XNSTRING(NULL, ctx->padlen),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id,
+ ctx->src_id_len),
+ SILC_STR_UI_CHAR(&ctx->dst_id_type),
+ SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id,
+ ctx->dst_id_len),
+ SILC_STR_UI_XNSTRING(NULL, ctx->padlen),
+ SILC_STR_END);
+ if (ret == -1)
+ return SILC_PACKET_NONE;
+
silc_buffer_push(buffer, len);
SILC_LOG_HEXDUMP(("parsed packet, len %d", ctx->buffer->len),
SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx)
{
SilcBuffer buffer = ctx->buffer;
- int len, tmplen;
+ int len, tmplen, ret;
SILC_LOG_DEBUG(("Parsing incoming packet"));
SILC_STR_UI_SHORT(&ctx->dst_id_len),
SILC_STR_UI_CHAR(&ctx->src_id_type),
SILC_STR_END);
+ if (len == -1)
+ return SILC_PACKET_NONE;
if (ctx->src_id_len > SILC_PACKET_MAX_ID_LEN ||
ctx->dst_id_len > SILC_PACKET_MAX_ID_LEN) {
ctx->padlen = SILC_PACKET_PADLEN(tmplen);
silc_buffer_pull(buffer, len);
- silc_buffer_unformat(buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id,
- ctx->src_id_len),
- SILC_STR_UI_CHAR(&ctx->dst_id_type),
- SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id,
- ctx->dst_id_len),
- SILC_STR_UI_XNSTRING(NULL, ctx->padlen),
- SILC_STR_END);
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI_XNSTRING_ALLOC(&ctx->src_id,
+ ctx->src_id_len),
+ SILC_STR_UI_CHAR(&ctx->dst_id_type),
+ SILC_STR_UI_XNSTRING_ALLOC(&ctx->dst_id,
+ ctx->dst_id_len),
+ SILC_STR_UI_XNSTRING(NULL, ctx->padlen),
+ SILC_STR_END);
+ if (ret == -1)
+ return SILC_PACKET_NONE;
+
silc_buffer_push(buffer, len);
SILC_LOG_HEXDUMP(("parsed packet, len %d", ctx->buffer->len),
return ctx->type;
}
+
+/* Allocate packet context */
+
+SilcPacketContext *silc_packet_context_alloc()
+{
+ SilcPacketContext *ctx = silc_calloc(1, sizeof(*ctx));
+ ctx->users++;
+ return ctx;
+}
+
+/* Increse the reference count of the packet context. */
+
+SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx)
+{
+ ctx->users++;
+ SILC_LOG_DEBUG(("Packet context %p rfcnt %d->%d", ctx, ctx->users - 1,
+ ctx->users));
+ return ctx;
+}
+
+/* Decrese the reference count of the packet context and free it only if
+ it is zero. */
+
+void silc_packet_context_free(SilcPacketContext *ctx)
+{
+ ctx->users--;
+ SILC_LOG_DEBUG(("Packet context %p rfcnt %d->%d", ctx, ctx->users + 1,
+ ctx->users));
+ if (ctx->users < 1)
+ {
+ if (ctx->buffer)
+ silc_buffer_free(ctx->buffer);
+ if (ctx->src_id)
+ silc_free(ctx->src_id);
+ if (ctx->dst_id)
+ silc_free(ctx->dst_id);
+ silc_free(ctx);
+ }
+}