Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
- Copyright (C) 1997 - 2000 Pekka Riikonen
+ 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
int silc_packet_send(SilcSocketConnection sock, int force_send)
{
+ SILC_LOG_DEBUG(("Sending packet to %s:%d [%s]", sock->hostname,
+ sock->port,
+ (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
+ sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
+ sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
+ "Router")));
+
/* Send now if forced to do so */
if (force_send == TRUE) {
int ret;
SilcBuffer buffer, unsigned int len)
{
unsigned char mac[32];
-
- if (cipher) {
- SILC_LOG_DEBUG(("Encrypting packet, cipher %s, len %d (%d)",
- cipher->cipher->name, len, len - 2));
- }
+ unsigned int mac_len;
/* Compute HMAC. This assumes that HMAC is created from the entire
data area thus this uses the length found in buffer, not the length
sent as argument. */
if (hmac) {
- silc_hmac_make(hmac, buffer->data, buffer->len, mac);
- silc_buffer_put_tail(buffer, mac, hmac->hash->hash->hash_len);
+ silc_hmac_make(hmac, buffer->data, buffer->len, mac, &mac_len);
+ silc_buffer_put_tail(buffer, mac, mac_len);
memset(mac, 0, sizeof(mac));
}
/* Encrypt the data area of the packet. 2 bytes of the packet
are not encrypted. */
- if (cipher)
+ if (cipher) {
+ SILC_LOG_DEBUG(("Encrypting packet, cipher %s, len %d (%d)",
+ cipher->cipher->name, len, len - 2));
cipher->cipher->encrypt(cipher->context, buffer->data + 2,
buffer->data + 2, len - 2, cipher->iv);
+ }
/* Pull the HMAC into the visible data area in the buffer */
if (hmac)
- silc_buffer_pull_tail(buffer, hmac->hash->hash->hash_len);
+ silc_buffer_pull_tail(buffer, mac_len);
}
/* Assembles a new packet to be ready for send out. The buffer sent as
/* 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_global_get_byte();
#else
/* XXX: For testing - to be removed */
memset(tmppad, 65, sizeof(tmppad));
return;
if (hmac)
- mac_len = hmac->hash->hash->hash_len;
+ mac_len = hmac->hmac->len;
/* Parse the packets from the data */
count = 0;
{
int ret;
+ SILC_LOG_DEBUG(("Receiving packet from %s:%d [%s]", sock->hostname,
+ sock->port,
+ (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
+ sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
+ sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
+ "Router")));
+
/* Allocate the incoming data buffer if not done already. */
if (!sock->inbuf)
sock->inbuf = silc_buffer_alloc(SILC_PACKET_DEFAULT_SIZE);
-
+
/* Read some data from connection */
ret = silc_packet_read(sock->sock, sock->inbuf);
- /* Error */
- if (ret == -1) {
- SILC_LOG_ERROR(("Error reading packet, dropped"));
- }
-
return ret;
}
/* Check MAC */
if (hmac) {
unsigned char mac[32];
+ unsigned int mac_len;
SILC_LOG_DEBUG(("Verifying MAC"));
/* Compute HMAC of packet */
memset(mac, 0, sizeof(mac));
- silc_hmac_make(hmac, buffer->data, buffer->len, mac);
+ silc_hmac_make(hmac, buffer->data, buffer->len, mac, &mac_len);
/* Compare the HMAC's (buffer->tail has the packet's HMAC) */
- if (memcmp(mac, buffer->tail, hmac->hash->hash->hash_len)) {
+ if (memcmp(mac, buffer->tail, mac_len)) {
SILC_LOG_DEBUG(("MAC failed"));
return FALSE;
}
/* Pull MAC from packet before decryption */
if (hmac) {
- if ((buffer->len - hmac->hash->hash->hash_len) > SILC_PACKET_MIN_LEN) {
- silc_buffer_push_tail(buffer, hmac->hash->hash->hash_len);
+ if ((buffer->len - hmac->hmac->len) > SILC_PACKET_MIN_LEN) {
+ silc_buffer_push_tail(buffer, hmac->hmac->len);
} else {
SILC_LOG_DEBUG(("Bad MAC length in packet, packet dropped"));
return FALSE;
/* Pull MAC from packet before decryption */
if (hmac) {
- if ((buffer->len - hmac->hash->hash->hash_len) > SILC_PACKET_MIN_LEN) {
- silc_buffer_push_tail(buffer, hmac->hash->hash->hash_len);
+ if ((buffer->len - hmac->hmac->len) > SILC_PACKET_MIN_LEN) {
+ silc_buffer_push_tail(buffer, hmac->hmac->len);
} else {
SILC_LOG_DEBUG(("Bad MAC length in packet, packet dropped"));
return FALSE;
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),
SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx)
{
ctx->users++;
+ SILC_LOG_DEBUG(("Packet context %p refcnt %d->%d", ctx, ctx->users - 1,
+ ctx->users));
return ctx;
}
void silc_packet_context_free(SilcPacketContext *ctx)
{
ctx->users--;
+ SILC_LOG_DEBUG(("Packet context %p refcnt %d->%d", ctx, ctx->users + 1,
+ ctx->users));
if (ctx->users < 1)
{
if (ctx->buffer)