updates.
[silc.git] / apps / silcd / idlist.c
index 8da1366274fc7cfd2f0841a95c8945de835615c8..9cdbc223487148a4f8ab7a050fe8ec7aa453faba 100644 (file)
@@ -307,11 +307,12 @@ silc_idlist_add_client(SilcIDList id_list, char *nickname, char *username,
   client->id = id;
   client->router = router;
   client->connection = connection;
-  silc_list_init(client->channels, struct SilcChannelClientEntryStruct, 
-                client_list);
+  client->channels = silc_hash_table_alloc(3, silc_hash_ptr, NULL,
+                                          NULL, NULL, NULL, NULL, TRUE);
 
   if (!silc_idcache_add(id_list->clients, nickname, (void *)client->id, 
                        (void *)client, FALSE)) {
+    silc_hash_table_free(client->channels);
     silc_free(client);
     return NULL;
   }
@@ -383,6 +384,8 @@ int silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
   
   *clients_count += i;
 
+  SILC_LOG_DEBUG(("Found %d clients", *clients_count));
+
   return TRUE;
 }
 
@@ -404,6 +407,10 @@ int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
 
   silc_hash_make(md5hash, nickname, strlen(nickname), hash);
 
+  /* As the Client ID is hashed in the ID cache by hashing only the hash
+     from the Client ID, we can do a lookup with only the hash not the
+     other parts of the ID and get all the clients with that hash, ie.
+     with that nickname, as the hash is from the nickname. */
   memset(&client_id, 0, sizeof(client_id));
   memcpy(&client_id.hash, hash, sizeof(client_id.hash));
   if (!silc_idcache_find_by_id(id_list->clients, &client_id, &list))
@@ -424,6 +431,8 @@ int silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
   
   *clients_count += i;
 
+  SILC_LOG_DEBUG(("Found %d clients", *clients_count));
+
   return TRUE;
 }
 
@@ -554,11 +563,13 @@ silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
       return NULL;
     }
 
-  silc_list_init(channel->user_list, struct SilcChannelClientEntryStruct, 
-                channel_list);
+  channel->user_list = silc_hash_table_alloc(3, silc_hash_ptr, NULL, NULL,
+                                            NULL, NULL, NULL, TRUE);
 
   if (!silc_idcache_add(id_list->channels, channel->channel_name, 
                        (void *)channel->id, (void *)channel, FALSE)) {
+    silc_hmac_free(channel->hmac);
+    silc_hash_table_free(channel->user_list);
     silc_free(channel);
     return NULL;
   }
@@ -566,6 +577,20 @@ silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
   return channel;
 }
 
+/* Foreach callbcak to free all users from the channel when deleting a
+   channel entry. */
+
+static void silc_idlist_del_channel_foreach(void *key, void *context,
+                                           void *user_context)
+{
+  SilcChannelClientEntry chl = (SilcChannelClientEntry)context;
+
+  /* Remove the context from the client's channel hash table as that
+     table and channel's user_list hash table share this same context. */
+  silc_hash_table_del_by_context(chl->client->channels, chl->channel, chl);
+  silc_free(chl);
+}
+
 /* Free channel entry.  This free's everything. */
 
 int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
@@ -573,8 +598,6 @@ int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
   SILC_LOG_DEBUG(("Start"));
 
   if (entry) {
-    SilcChannelClientEntry chl;
-
     /* Remove from cache */
     if (entry->id)
       if (!silc_idcache_del_by_id(id_list->channels, (void *)entry->id))
@@ -600,14 +623,12 @@ int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
     if (entry->rekey)
       silc_free(entry->rekey);
 
-    /* Free all data, free also any reference from the client's channel
-       list since they share the same memory. */
-    silc_list_start(entry->user_list);
-    while ((chl = silc_list_get(entry->user_list)) != SILC_LIST_END) {
-      silc_list_del(chl->client->channels, chl);
-      silc_list_del(entry->user_list, chl);
-      silc_free(chl);
-    }
+    /* Free all client entrys from the users list. The silc_hash_table_free
+       will free all the entries so they are not freed at the foreach 
+       callback. */
+    silc_hash_table_foreach(entry->user_list, silc_idlist_del_channel_foreach,
+                           NULL);
+    silc_hash_table_free(entry->user_list);
 
     memset(entry, 'F', sizeof(*entry));
     silc_free(entry);