updates.
[silc.git] / apps / silcd / idlist.c
index 6323af367cead87a754cc6c4f17318b821ea5709..d8a747fcacd85cd140b87935f223aa62fa1c1027 100644 (file)
@@ -131,6 +131,75 @@ silc_idlist_find_server_by_id(SilcIDList id_list, SilcServerID *id,
   return server;
 }
 
+/* Find server by name */
+
+SilcServerEntry
+silc_idlist_find_server_by_name(SilcIDList id_list, char *name,
+                               SilcIDCacheEntry *ret_entry)
+{
+  SilcIDCacheEntry id_cache = NULL;
+  SilcServerEntry server;
+
+  SILC_LOG_DEBUG(("Server by name `%s'", name));
+
+  if (!silc_idcache_find_by_data_one(id_list->servers, name, &id_cache))
+    return NULL;
+
+  server = (SilcServerEntry)id_cache->context;
+  
+  if (ret_entry)
+    *ret_entry = id_cache;
+
+  return server;
+}
+
+/* Find server by connection parameters, hostname and port */
+
+SilcServerEntry
+silc_idlist_find_server_by_conn(SilcIDList id_list, char *hostname,
+                               int port, SilcIDCacheEntry *ret_entry)
+{
+  SilcIDCacheList list = NULL;
+  SilcIDCacheEntry id_cache = NULL;
+  SilcServerEntry server = NULL;
+  SilcSocketConnection sock;
+  SILC_LOG_DEBUG(("Server by hostname %s and port %d", hostname, port));
+
+  if (!silc_idcache_find_by_id(id_list->servers, SILC_ID_CACHE_ANY, 
+                              SILC_ID_SERVER, &list))
+    return NULL;
+
+  if (!silc_idcache_list_first(list, &id_cache)) {
+    silc_idcache_list_free(list);
+    return NULL;
+  }
+
+  while (id_cache) {
+    server = (SilcServerEntry)id_cache->context;
+    sock = (SilcSocketConnection)server->connection;
+    
+    if (sock && (!strcmp(sock->hostname, hostname) ||
+                !strcmp(sock->ip, hostname)) && sock->port == port)
+      break;
+
+    id_cache = NULL;
+    server = NULL;
+
+    if (!silc_idcache_list_next(list, &id_cache))
+      break;
+  }
+  
+  silc_idcache_list_free(list);
+
+  if (ret_entry)
+    *ret_entry = id_cache;
+
+  SILC_LOG_DEBUG(("Found"));
+
+  return server;
+}
+
 /* Replaces old Server ID with new one */ 
 
 SilcServerEntry
@@ -224,13 +293,14 @@ silc_idlist_add_client(SilcIDList id_list, unsigned char *nickname,
 /* Free client entry. This free's everything and removes the entry
    from ID cache. Call silc_idlist_del_data before calling this one. */
 
-void silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
+int silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
 {
   if (entry) {
     /* Remove from cache */
     if (entry->id)
-      silc_idcache_del_by_id(id_list->clients, SILC_ID_CLIENT, 
-                            (void *)entry->id);
+      if (!silc_idcache_del_by_id(id_list->clients, SILC_ID_CLIENT, 
+                                 (void *)entry->id))
+       return FALSE;
 
     /* Free data */
     if (entry->nickname)
@@ -244,7 +314,11 @@ void silc_idlist_del_client(SilcIDList id_list, SilcClientEntry entry)
 
     memset(entry, 'F', sizeof(*entry));
     silc_free(entry);
+
+    return TRUE;
   }
+
+  return FALSE;
 }
 
 /* Returns all clients matching requested nickname. Number of clients is
@@ -469,6 +543,8 @@ silc_idlist_replace_client_id(SilcIDList id_list, SilcClientID *old_id,
     silc_idcache_sort_by_data(id_list->clients);
   }
 
+  SILC_LOG_DEBUG(("Replaced"));
+
   return client;
 }
 
@@ -534,9 +610,12 @@ int silc_idlist_del_channel(SilcIDList id_list, SilcChannelEntry entry)
       memset(entry->key, 0, entry->key_len / 8);
       silc_free(entry->key);
     }
-    
+
+    /* 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);
     }