updates.
[runtime.git] / apps / silcd / idlist.c
index 86b603179b036ce579ec3329c0d1f5b8dff85fff..b5c17daf5627dd5b9e79f2063538d4085b01a66c 100644 (file)
@@ -36,6 +36,7 @@ void silc_idlist_add_data(void *entry, SilcIDListData idata)
   SilcIDListData data = (SilcIDListData)entry;
   data->send_key = idata->send_key;
   data->receive_key = idata->receive_key;
+  data->hash = idata->hash;
   data->hmac = idata->hmac;
   data->hmac_key = idata->hmac_key;
   data->hmac_key_len = idata->hmac_key_len;
@@ -67,6 +68,21 @@ void silc_idlist_del_data(void *entry)
     silc_pkcs_public_key_free(idata->public_key);
 }
 
+/* Purges ID cache */
+
+SILC_TASK_CALLBACK_GLOBAL(silc_idlist_purge)
+{
+  SilcIDListPurge i = (SilcIDListPurge)context;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  silc_idcache_purge(i->cache);
+  silc_task_register(i->timeout_queue, 0, 
+                    silc_idlist_purge,
+                    (void *)i, 600, 0,
+                    SILC_TASK_TIMEOUT, SILC_TASK_PRI_LOW);
+}
+
 /******************************************************************************
 
                           Server entry functions
@@ -95,8 +111,10 @@ silc_idlist_add_server(SilcIDList id_list,
   server->router = router;
   server->connection = connection;
 
-  if (!silc_idcache_add(id_list->servers, server->server_name, SILC_ID_SERVER,
-                       (void *)server->id, (void *)server, TRUE, FALSE)) {
+  if (!silc_idcache_add(id_list->servers, server->server_name, 
+                       server->server_name ? strlen(server->server_name) : 0,
+                       SILC_ID_SERVER, (void *)server->id, 
+                       (void *)server, TRUE, FALSE)) {
     silc_free(server);
     return NULL;
   }
@@ -150,6 +168,8 @@ silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
   if (ret_entry)
     *ret_entry = id_cache;
 
+  SILC_LOG_DEBUG(("Found"));
+
   return server;
 }
 
@@ -179,8 +199,9 @@ silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
     server = (SilcServerEntry)id_cache->context;
     sock = (SilcSocketConnection)server->connection;
     
-    if (sock && (!strcmp(sock->hostname, hostname) ||
-                !strcmp(sock->ip, hostname)) && sock->port == port)
+    if (sock && ((sock->hostname && !strcmp(sock->hostname, hostname)) ||
+                (sock->ip && !strcmp(sock->ip, hostname)))
+       && sock->port == port)
       break;
 
     id_cache = NULL;
@@ -223,6 +244,8 @@ silc_idlist_replace_server_id(SilcIDList id_list, SilcServerID *old_id,
   server->id = new_id;
   id_cache->id = (void *)new_id;
 
+  SILC_LOG_DEBUG(("Found"));
+
   return server;
 }
 
@@ -264,7 +287,8 @@ void silc_idlist_del_server(SilcIDList id_list, SilcServerEntry entry)
 
 SilcClientEntry
 silc_idlist_add_client(SilcIDList id_list, unsigned char *nickname, 
-                      char *username, char *userinfo, SilcClientID *id, 
+                      unsigned int nickname_len, char *username, 
+                      char *userinfo, SilcClientID *id, 
                       SilcServerEntry router, void *connection)
 {
   SilcClientEntry client;
@@ -281,8 +305,9 @@ silc_idlist_add_client(SilcIDList id_list, unsigned char *nickname,
   silc_list_init(client->channels, struct SilcChannelClientEntryStruct, 
                 client_list);
 
-  if (!silc_idcache_add(id_list->clients, nickname, SILC_ID_CLIENT,
-                       (void *)client->id, (void *)client, TRUE, FALSE)) {
+  if (!silc_idcache_add(id_list->clients, nickname,  nickname_len,
+                       SILC_ID_CLIENT, (void *)client->id, 
+                       (void *)client, TRUE, FALSE)) {
     silc_free(client);
     return NULL;
   }
@@ -333,6 +358,8 @@ silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
   SilcClientEntry *clients;
   int i;
 
+  SILC_LOG_DEBUG(("Start"));
+
   if (!silc_idcache_find_by_data(id_list->clients, nickname, &list))
     return NULL;
 
@@ -355,6 +382,10 @@ silc_idlist_get_clients_by_nickname(SilcIDList id_list, char *nickname,
 
 /* Returns all clients matching requested nickname. Number of clients is
    returned to `clients_count'. Caller must free the returned table. */
+/* XXX This actually checks the data, which can be hash of the nickname
+   but is not if the client is local client. Global client on global
+   list may have hash.  Thus, this is not fully reliable function.
+   Instead this should probably check the hash from the lists client ID's. */
 
 SilcClientEntry *
 silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
@@ -367,6 +398,8 @@ silc_idlist_get_clients_by_hash(SilcIDList id_list, char *nickname,
   unsigned char hash[32];
   int i;
 
+  SILC_LOG_DEBUG(("Start"));
+
   silc_hash_make(md5hash, nickname, strlen(nickname), hash);
 
   if (!silc_idcache_find_by_data(id_list->clients, hash, &list))
@@ -606,8 +639,9 @@ silc_idlist_add_channel(SilcIDList id_list, char *channel_name, int mode,
                 channel_list);
 
   if (!silc_idcache_add(id_list->channels, channel->channel_name, 
-                       SILC_ID_CHANNEL, (void *)channel->id, 
-                       (void *)channel, TRUE, FALSE)) {
+                       channel->channel_name ? strlen(channel->channel_name) :
+                       0, SILC_ID_CHANNEL, 
+                       (void *)channel->id, (void *)channel, TRUE, FALSE)) {
     silc_free(channel);
     return NULL;
   }
@@ -745,5 +779,42 @@ silc_idlist_replace_channel_id(SilcIDList id_list, SilcChannelID *old_id,
   channel->id = new_id;
   id_cache->id = (void *)new_id;
 
+  SILC_LOG_DEBUG(("Replaced"));
+
   return channel;
 }
+
+/* Returns channels from the ID list. If the `channel_id' is NULL then
+   all channels are returned. */
+
+SilcChannelEntry *
+silc_idlist_get_channels(SilcIDList id_list, SilcChannelID *channel_id,
+                        unsigned int *channels_count)
+{
+  SilcIDCacheList list = NULL;
+  SilcIDCacheEntry id_cache = NULL;
+  SilcChannelEntry *channels;
+  int i;
+
+  SILC_LOG_DEBUG(("Start"));
+
+  if (!silc_idcache_find_by_id(id_list->channels, channel_id ? channel_id :
+                              SILC_ID_CACHE_ANY, SILC_ID_CHANNEL, &list))
+    return NULL;
+
+  channels = silc_calloc(silc_idcache_list_count(list), sizeof(*channels));
+
+  i = 0;
+  silc_idcache_list_first(list, &id_cache);
+  channels[i++] = (SilcChannelEntry)id_cache->context;
+
+  while (silc_idcache_list_next(list, &id_cache))
+    channels[i++] = (SilcChannelEntry)id_cache->context;
+  
+  silc_idcache_list_free(list);
+  
+  if (channels_count)
+    *channels_count = i;
+
+  return channels;
+}