Added SILC Server library.
[silc.git] / lib / silccore / silcid.c
index 870400638b721c0fdc712e5ccd983163b7768153..1e5727bc215bd96d94831b3aa7119e9e5b71ff97 100644 (file)
@@ -2,15 +2,14 @@
 
   id.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2005 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.
-  
+  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
@@ -19,7 +18,7 @@
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 #include "silcid.h"
 
 /* ID lengths (in bytes) without the IP address part */
@@ -48,8 +47,6 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
   SilcIDPayload newp;
   int ret;
 
-  SILC_LOG_DEBUG(("Parsing ID payload"));
-
   silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
   newp = silc_calloc(1, sizeof(*newp));
   if (!newp)
@@ -62,9 +59,13 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
   if (ret == -1)
     goto err;
 
+  if (newp->type > SILC_ID_CHANNEL)
+    goto err;
+
   silc_buffer_pull(&buffer, 4);
 
-  if (newp->len > buffer.len)
+  if (newp->len > silc_buffer_len(&buffer) ||
+      newp->len > SILC_PACKET_MAX_ID_LEN)
     goto err;
 
   ret = silc_buffer_unformat(&buffer,
@@ -73,25 +74,25 @@ SilcIDPayload silc_id_payload_parse(const unsigned char *payload,
   if (ret == -1)
     goto err;
 
-  silc_buffer_push(&buffer, 4);
-
   return newp;
 
  err:
+  SILC_LOG_DEBUG(("Error parsing ID payload"));
   silc_free(newp);
   return NULL;
 }
 
 /* Return the ID directly from the raw payload data. */
 
-void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
+SilcBool silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len,
+                                 SilcIdType *ret_type, void *ret_id,
+                                 SilcUInt32 ret_id_size)
 {
   SilcBufferStruct buffer;
   SilcIdType type;
   SilcUInt16 idlen;
-  unsigned char *id_data = NULL;
+  unsigned char *id_data;
   int ret;
-  void *id;
 
   silc_buffer_set(&buffer, (unsigned char *)data, len);
   ret = silc_buffer_unformat(&buffer,
@@ -101,23 +102,31 @@ void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
   if (ret == -1)
     goto err;
 
+  if (type > SILC_ID_CHANNEL)
+    goto err;
+
   silc_buffer_pull(&buffer, 4);
 
-  if (idlen > buffer.len)
+  if (idlen > silc_buffer_len(&buffer) || idlen > SILC_PACKET_MAX_ID_LEN)
     goto err;
 
   ret = silc_buffer_unformat(&buffer,
-                            SILC_STR_UI_XNSTRING_ALLOC(&id_data, idlen),
+                            SILC_STR_UI_XNSTRING(&id_data, idlen),
                             SILC_STR_END);
   if (ret == -1)
     goto err;
 
-  id = silc_id_str2id(id_data, idlen, type);
-  silc_free(id_data);
-  return id;
+  if (!silc_id_str2id(id_data, idlen, type, ret_id, ret_id_size))
+    goto err;
+
+  if (ret_type)
+    *ret_type = type;
+
+  return TRUE;
 
  err:
-  return NULL;
+  SILC_LOG_DEBUG(("Error parsing ID payload"));
+  return FALSE;
 }
 
 /* Encodes ID Payload */
@@ -125,14 +134,13 @@ void *silc_id_payload_parse_id(const unsigned char *data, SilcUInt32 len)
 SilcBuffer silc_id_payload_encode(const void *id, SilcIdType type)
 {
   SilcBuffer buffer;
-  unsigned char *id_data;
+  unsigned char id_data[32];
   SilcUInt32 len;
 
-  id_data = silc_id_id2str(id, type);
-  len = silc_id_get_len(id, type);
+  if (!silc_id_id2str(id, type, id_data, sizeof(id_data), &len))
+    return NULL;
   buffer = silc_id_payload_encode_data((const unsigned char *)id_data,
                                       len, type);
-  silc_free(id_data);
   return buffer;
 }
 
@@ -141,10 +149,6 @@ SilcBuffer silc_id_payload_encode_data(const unsigned char *id,
 {
   SilcBuffer buffer;
 
-  SILC_LOG_DEBUG(("Encoding %s ID payload",
-                 type == SILC_ID_CLIENT ? "Client" :
-                 type == SILC_ID_SERVER ? "Server" : "Channel"));
-
   buffer = silc_buffer_alloc_size(4 + id_len);
   if (!buffer)
     return NULL;
@@ -175,10 +179,13 @@ SilcIdType silc_id_payload_get_type(SilcIDPayload payload)
 
 /* Get ID */
 
-void *silc_id_payload_get_id(SilcIDPayload payload)
+SilcBool silc_id_payload_get_id(SilcIDPayload payload, void *ret_id,
+                               SilcUInt32 ret_id_len)
 {
-  return payload ? silc_id_str2id(payload->id, payload->len,
-                                  payload->type) : NULL;
+  if (!payload)
+    return FALSE;
+  return silc_id_str2id(payload->id, payload->len, payload->type,
+                       ret_id, ret_id_len);
 }
 
 /* Get raw ID data. Data is duplicated. */
@@ -200,116 +207,125 @@ SilcUInt32 silc_id_payload_get_len(SilcIDPayload payload)
 
 /* Converts ID to string. */
 
-unsigned char *silc_id_id2str(const void *id, SilcIdType type)
+SilcBool silc_id_id2str(const void *id, SilcIdType type,
+                       unsigned char *ret_id, SilcUInt32 ret_id_size,
+                       SilcUInt32 *ret_id_len)
 {
-  unsigned char *ret_id;
   SilcServerID *server_id;
   SilcClientID *client_id;
   SilcChannelID *channel_id;
   SilcUInt32 id_len = silc_id_get_len(id, type);
 
+  if (id_len > ret_id_size)
+    return FALSE;
+
+  if (ret_id_len)
+    *ret_id_len = id_len;
+
+  if (id_len > SILC_PACKET_MAX_ID_LEN)
+    return FALSE;
+
   switch(type) {
   case SILC_ID_SERVER:
     server_id = (SilcServerID *)id;
-    ret_id = silc_calloc(id_len, sizeof(unsigned char));
-    if (!ret_id)
-      return NULL;
     memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
-    SILC_PUT16_MSB(server_id->port, &ret_id[4]);
-    SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
-    return ret_id;
+    SILC_PUT16_MSB(server_id->port, &ret_id[server_id->ip.data_len]);
+    SILC_PUT16_MSB(server_id->rnd, &ret_id[server_id->ip.data_len + 2]);
+    return TRUE;
     break;
   case SILC_ID_CLIENT:
     client_id = (SilcClientID *)id;
-    ret_id = silc_calloc(id_len, sizeof(unsigned char));
-    if (!ret_id)
-      return NULL;
     memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
-    ret_id[4] = client_id->rnd;
-    memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
-    return ret_id;
+    ret_id[client_id->ip.data_len] = client_id->rnd;
+    memcpy(&ret_id[client_id->ip.data_len + 1], client_id->hash,
+          CLIENTID_HASH_LEN);
+    return TRUE;
     break;
   case SILC_ID_CHANNEL:
     channel_id = (SilcChannelID *)id;
-    ret_id = silc_calloc(id_len, sizeof(unsigned char));
-    if (!ret_id)
-      return NULL;
     memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
-    SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
-    SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
-    return ret_id;
+    SILC_PUT16_MSB(channel_id->port, &ret_id[channel_id->ip.data_len]);
+    SILC_PUT16_MSB(channel_id->rnd, &ret_id[channel_id->ip.data_len + 2]);
+    return TRUE;
     break;
   }
 
-  return NULL;
+  return FALSE;
 }
 
 /* Converts string to a ID */
 
-void *silc_id_str2id(const unsigned char *id, SilcUInt32 id_len, SilcIdType type)
+SilcBool silc_id_str2id(const unsigned char *id, SilcUInt32 id_len,
+                       SilcIdType type, void *ret_id, SilcUInt32 ret_id_size)
 {
+  if (id_len > SILC_PACKET_MAX_ID_LEN)
+    return FALSE;
 
   switch(type) {
   case SILC_ID_SERVER:
     {
-      SilcServerID *server_id;
+      SilcServerID *server_id = ret_id;
 
       if (id_len != ID_SERVER_LEN_PART + 4 &&
          id_len != ID_SERVER_LEN_PART + 16)
-       return NULL;
+       return FALSE;
+
+      if (ret_id_size < sizeof(SilcServerID))
+       return FALSE;
 
-      server_id = silc_calloc(1, sizeof(*server_id));
-      if (!server_id)
-       return NULL;
+      memset(ret_id, 0, ret_id_size);
       memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
                                      16 : 4));
       server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
-      SILC_GET16_MSB(server_id->port, &id[4]);
-      SILC_GET16_MSB(server_id->rnd, &id[6]);
-      return server_id;
+      SILC_GET16_MSB(server_id->port, &id[server_id->ip.data_len]);
+      SILC_GET16_MSB(server_id->rnd, &id[server_id->ip.data_len + 2]);
+      return TRUE;
     }
     break;
   case SILC_ID_CLIENT:
     {
-      SilcClientID *client_id;
+      SilcClientID *client_id = ret_id;
 
       if (id_len != ID_CLIENT_LEN_PART + 4 &&
          id_len != ID_CLIENT_LEN_PART + 16)
-       return NULL;
+       return FALSE;
 
-      client_id = silc_calloc(1, sizeof(*client_id));
-      if (!client_id)
-       return NULL;
+      if (ret_id_size < sizeof(SilcClientID))
+       return FALSE;
+
+      memset(ret_id, 0, ret_id_size);
       memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
                                      16 : 4));
       client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
-      client_id->rnd = id[4];
-      memcpy(client_id->hash, &id[5], CLIENTID_HASH_LEN);
-      return client_id;
+      client_id->rnd = id[client_id->ip.data_len];
+      memcpy(client_id->hash, &id[client_id->ip.data_len + 1],
+            CLIENTID_HASH_LEN);
+      return TRUE;
     }
     break;
   case SILC_ID_CHANNEL:
     {
-      SilcChannelID *channel_id;
+      SilcChannelID *channel_id = ret_id;
 
       if (id_len != ID_CHANNEL_LEN_PART + 4 &&
          id_len != ID_CHANNEL_LEN_PART + 16)
-       return NULL;
+       return FALSE;
+
+      if (ret_id_size < sizeof(SilcChannelID))
+       return FALSE;
 
-      channel_id = silc_calloc(1, sizeof(*channel_id));
-      if (!channel_id)
-       return NULL;
+      memset(ret_id, 0, ret_id_size);
       memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
                                       16 : 4));
       channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
-      SILC_GET16_MSB(channel_id->port, &id[4]);
-      SILC_GET16_MSB(channel_id->rnd, &id[6]);
-      return channel_id;
+      SILC_GET16_MSB(channel_id->port, &id[channel_id->ip.data_len]);
+      SILC_GET16_MSB(channel_id->rnd, &id[channel_id->ip.data_len + 2]);
+      return TRUE;
     }
     break;
   }
 
-  return NULL;
+  return FALSE;
 }
 
 /* Returns length of the ID */