Mon Jun 18 22:21:37 CEST 2007 Jochen Eisinger <coffee@silcnet.org>
[silc.git] / lib / silcclient / client.c
index 33a975496864aadfe251a29323790ece27454dd4..154de47819ff7dcbca86f7051f663dd3af400718 100644 (file)
@@ -227,12 +227,6 @@ void silc_client_run_one(SilcClient client)
   silc_schedule_one(client->schedule, 0);
 }
 
-static void silc_client_entry_destructor(SilcIDCache cache,
-                                        SilcIDCacheEntry entry)
-{
-  silc_free(entry->name);
-}
-
 /* Allocates and adds new connection to the client. This adds the allocated
    connection to the connection table and returns a pointer to it. A client
    can have multiple connections to multiple servers. Every connection must
@@ -260,12 +254,11 @@ silc_client_add_connection(SilcClient client,
   conn->remote_port = port;
   conn->context = context;
   conn->internal->client_cache =
-    silc_idcache_alloc(0, SILC_ID_CLIENT, silc_client_entry_destructor,
-                      FALSE, FALSE);
+    silc_idcache_alloc(0, SILC_ID_CLIENT, NULL, NULL, FALSE, TRUE);
   conn->internal->channel_cache = silc_idcache_alloc(0, SILC_ID_CHANNEL, NULL,
-                                                    FALSE, FALSE);
+                                                    NULL, FALSE, TRUE);
   conn->internal->server_cache = silc_idcache_alloc(0, SILC_ID_SERVER, NULL,
-                                                   FALSE, FALSE);
+                                                   NULL, FALSE, TRUE);
   conn->internal->pending_commands = silc_dlist_init();
   conn->internal->ftp_sessions = silc_dlist_init();
 
@@ -1425,6 +1418,7 @@ void silc_client_packet_send(SilcClient client,
   const SilcBufferStruct packet;
   int block_len;
   SilcUInt32 sequence = 0;
+  int src_id_allocated = FALSE;
 
   if (!sock)
     return;
@@ -1468,6 +1462,7 @@ void silc_client_packet_send(SilcClient client,
   } else {
     packetdata.src_id = silc_calloc(SILC_ID_CLIENT_LEN, sizeof(unsigned char));
     packetdata.src_id_len = SILC_ID_CLIENT_LEN;
+    src_id_allocated = TRUE;
   }
   packetdata.src_id_type = SILC_ID_CLIENT;
   if (dst_id) {
@@ -1493,7 +1488,7 @@ void silc_client_packet_send(SilcClient client,
   if (!silc_packet_assemble(&packetdata, client->rng, cipher, hmac, sock,
                             data, data_len, (const SilcBuffer)&packet)) {
     SILC_LOG_ERROR(("Error assembling packet"));
-    return;
+    goto out;
   }
 
   /* Encrypt the packet */
@@ -1506,6 +1501,12 @@ void silc_client_packet_send(SilcClient client,
 
   /* Now actually send the packet */
   silc_client_packet_send_real(client, sock, force_send);
+
+ out:
+  if (src_id_allocated && packetdata.src_id)
+    silc_free(packetdata.src_id);
+  if (packetdata.dst_id)
+    silc_free(packetdata.dst_id);
 }
 
 /* Packet sending routine for application.  This is the only routine that
@@ -1745,6 +1746,7 @@ void silc_client_receive_new_id(SilcClient client,
   SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   int connecting = FALSE;
   SilcClientID *client_id = silc_id_payload_get_id(idp);
+  char *nickname;
 
   if (!conn->local_entry)
     connecting = TRUE;
@@ -1787,9 +1789,14 @@ void silc_client_receive_new_id(SilcClient client,
                                                        NULL, NULL, NULL,
                                                        TRUE);
 
-  /* Put it to the ID cache */
-  silc_idcache_add(conn->internal->client_cache,
-                  strdup(conn->nickname), conn->local_id,
+  /* Normalize nickname */
+  nickname = silc_identifier_check(conn->nickname, strlen(conn->nickname),
+                                  SILC_STRING_UTF8, 128, NULL);
+  if (!nickname)
+    return;
+
+    /* Put it to the ID cache */
+  silc_idcache_add(conn->internal->client_cache, nickname, conn->local_id,
                   (void *)conn->local_entry, 0, NULL);
 
   if (connecting) {