Added silc_server_send_opers[_notify] to send packets to operators.
[silc.git] / apps / silcd / server_util.c
index 713b8528bd336120251b811259921e49218ab921..121bb8d88034f9f361ee54ffbc8846957f0ef76f 100644 (file)
@@ -77,7 +77,7 @@ silc_server_remove_clients_channels(SilcServer server,
     silc_free(chl);
 
     /* Update statistics */
-    if (client->connection)
+    if (SILC_IS_LOCAL(client))
       server->stat.my_chanclients--;
     if (server->server_type == SILC_ROUTER) {
       server->stat.cell_chanclients--;
@@ -287,6 +287,9 @@ bool silc_server_remove_clients_by_server(SilcServer server,
   if (server_signoff) {
     SilcBuffer args, not;
 
+    SILC_LOG_DEBUG(("Sending SERVER_SIGNOFF for %d clients",
+                   argc - 1));
+
     /* Send SERVER_SIGNOFF notify to our primary router */
     if (server->router != entry) {
       args = silc_argument_payload_encode(1, argv, argv_lens,
@@ -459,7 +462,8 @@ silc_server_update_clients_by_real_server(SilcServer server,
    be the new source. This function also removes the clients that are
    *really* originated from `from' if `remove_from' is TRUE. These are
    clients that the `from' owns, and not just clients that are behind
-   the `from'. */
+   the `from'. If `from' is NULL then all non-local clients are switched
+   to `to'. */
 
 void silc_server_update_clients_by_server(SilcServer server, 
                                          SilcServerEntry from,
@@ -474,18 +478,23 @@ void silc_server_update_clients_by_server(SilcServer server,
 
   SILC_LOG_DEBUG(("Start"));
 
-  SILC_LOG_DEBUG(("Updating %s", silc_id_render(from->id,
-                                               SILC_ID_SERVER)));
-  SILC_LOG_DEBUG(("to %s", silc_id_render(to->id,
-                                         SILC_ID_SERVER)));
-
+  if (from)
+    SILC_LOG_DEBUG(("Updating %s", silc_id_render(from->id,
+                                                 SILC_ID_SERVER)));
+  if (to)
+    SILC_LOG_DEBUG(("to %s", silc_id_render(to->id,
+                                           SILC_ID_SERVER)));
 
   local = FALSE;
   if (silc_idcache_get_all(server->global_list->clients, &list)) {
     if (silc_idcache_list_first(list, &id_cache)) {
       while (id_cache) {
        client = (SilcClientEntry)id_cache->context;
-       if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
+
+       /* If entry is disabled skip it.  If entry is local to us, do not
+          switch it to anyone else, it is ours so skip it. */
+       if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
+           SILC_IS_LOCAL(client)) {
          if (!silc_idcache_list_next(list, &id_cache))
            break;
          else
@@ -498,26 +507,31 @@ void silc_server_update_clients_by_server(SilcServer server,
          SILC_LOG_DEBUG(("Client->router (global) %s", 
                          silc_id_render(client->router->id, SILC_ID_SERVER)));
 
-       if (client->router == from) {
-         /* Skip clients that are *really* owned by the `from' */
-         if (remove_from && SILC_ID_COMPARE(from->id, client->id, 
-                                            client->id->ip.data_len)) {
-           SILC_LOG_DEBUG(("Found really owned client, skip it"));
-           if (!silc_idcache_list_next(list, &id_cache))
-             break;
-           else
-             continue;
-         }
-
-         if (resolve_real_server) {
-           client->router = 
-             silc_server_update_clients_by_real_server(server, from, client,
-                                                       local, id_cache);
-           if (!client->router)
+       if (from) {
+         if (client->router == from) {
+           /* Skip clients that are *really* owned by the `from' */
+           if (remove_from && SILC_ID_COMPARE(from->id, client->id, 
+                                              client->id->ip.data_len)) {
+             SILC_LOG_DEBUG(("Found really owned client, skip it"));
+             if (!silc_idcache_list_next(list, &id_cache))
+               break;
+             else
+               continue;
+           }
+
+           if (resolve_real_server) {
+             client->router = 
+               silc_server_update_clients_by_real_server(server, from, client,
+                                                         local, id_cache);
+             if (!client->router)
+               client->router = to;
+           } else {
              client->router = to;
-         } else {
-           client->router = to;
+           }
          }
+       } else {
+         /* All are changed */
+         client->router = to;
        }
 
        if (!silc_idcache_list_next(list, &id_cache))
@@ -532,7 +546,11 @@ void silc_server_update_clients_by_server(SilcServer server,
     if (silc_idcache_list_first(list, &id_cache)) {
       while (id_cache) {
        client = (SilcClientEntry)id_cache->context;
-       if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED)) {
+
+       /* If entry is disabled skip it.  If entry is local to us, do not
+          switch it to anyone else, it is ours so skip it. */
+       if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED) ||
+           SILC_IS_LOCAL(client)) {
          if (!silc_idcache_list_next(list, &id_cache))
            break;
          else
@@ -545,26 +563,31 @@ void silc_server_update_clients_by_server(SilcServer server,
          SILC_LOG_DEBUG(("Client->router (local) %s", 
                          silc_id_render(client->router->id, SILC_ID_SERVER)));
 
-       if (client->router == from) {
-         /* Skip clients that are *really* owned by the `from' */
-         if (remove_from && SILC_ID_COMPARE(from->id, client->id, 
-                                            client->id->ip.data_len)) {
-           SILC_LOG_DEBUG(("Found really owned client, skip it"));
-           if (!silc_idcache_list_next(list, &id_cache))
-             break;
-           else
-             continue;
-         }
-
-         if (resolve_real_server) {
-           client->router = 
-             silc_server_update_clients_by_real_server(server, from, client,
-                                                       local, id_cache);
-           if (!client->router)
-             client->router = from; /* on local list put old from */
-         } else {
-           client->router = to;
+       if (from) {
+         if (client->router == from) {
+           /* Skip clients that are *really* owned by the `from' */
+           if (remove_from && SILC_ID_COMPARE(from->id, client->id, 
+                                              client->id->ip.data_len)) {
+             SILC_LOG_DEBUG(("Found really owned client, skip it"));
+             if (!silc_idcache_list_next(list, &id_cache))
+               break;
+             else
+               continue;
+           }
+
+           if (resolve_real_server) {
+             client->router = 
+               silc_server_update_clients_by_real_server(server, from, client,
+                                                         local, id_cache);
+             if (!client->router)
+               client->router = from; /* on local list put old from */
+           } else {
+             client->router = to;
+           }
          }
+       } else {
+         /* All are changed */
+         client->router = to;
        }
 
        if (!silc_idcache_list_next(list, &id_cache))
@@ -582,26 +605,48 @@ void silc_server_update_clients_by_server(SilcServer server,
 }
 
 /* Updates servers that are from `from' to be originated from `to'.  This
-   will also update the server's connection to `to's connection. */
+   will also update the server's connection to `to's connection. If
+   `local_toggle_enabled' is TRUE then local server's connections are
+   enabled, if FALSE they are disabled. */
 
 void silc_server_update_servers_by_server(SilcServer server, 
                                          SilcServerEntry from,
-                                         SilcServerEntry to)
+                                         SilcServerEntry to,
+                                         bool local_toggle_enabled)
 {
   SilcIDCacheList list = NULL;
   SilcIDCacheEntry id_cache = NULL;
   SilcServerEntry server_entry = NULL;
 
-  SILC_LOG_DEBUG(("Start"));
-
   if (silc_idcache_get_all(server->local_list->servers, &list)) {
     if (silc_idcache_list_first(list, &id_cache)) {
       while (id_cache) {
        server_entry = (SilcServerEntry)id_cache->context;
+
+       if (SILC_IS_LOCAL(server_entry)) {
+         if (server_entry != server->id_entry) {
+           if (local_toggle_enabled)
+             server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
+           else
+             server_entry->data.status |= SILC_IDLIST_STATUS_DISABLED;
+         }
+
+         /* If entry is local to us, do not switch it to any oneelse,
+            it is ours. */
+         if (!silc_idcache_list_next(list, &id_cache))
+           break;
+         else
+           continue;
+       }
+
        if (server_entry->router == from) {
+         SILC_LOG_DEBUG(("Updating server (local) %s",
+                         server_entry->server_name ? 
+                         server_entry->server_name : ""));
          server_entry->router = to;
          server_entry->connection = to->connection;
        }
+
        if (!silc_idcache_list_next(list, &id_cache))
          break;
       }
@@ -613,10 +658,31 @@ void silc_server_update_servers_by_server(SilcServer server,
     if (silc_idcache_list_first(list, &id_cache)) {
       while (id_cache) {
        server_entry = (SilcServerEntry)id_cache->context;
+
+       if (SILC_IS_LOCAL(server_entry)) {
+         if (server_entry != server->id_entry) {
+           if (local_toggle_enabled)
+             server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED;
+           else
+             server_entry->data.status |= SILC_IDLIST_STATUS_DISABLED;
+         }
+
+         /* If entry is local to us, do not switch it to anyone else,
+            it is ours. */
+         if (!silc_idcache_list_next(list, &id_cache))
+           break;
+         else
+           continue;
+       }
+
        if (server_entry->router == from) {
+         SILC_LOG_DEBUG(("Updating server (global) %s",
+                         server_entry->server_name ? 
+                         server_entry->server_name : ""));
          server_entry->router = to;
          server_entry->connection = to->connection;
        }
+
        if (!silc_idcache_list_next(list, &id_cache))
          break;
       }
@@ -762,7 +828,7 @@ bool silc_server_channel_delete(SilcServer server,
     channel->user_count--;
 
     /* Update statistics */
-    if (chl->client->connection)
+    if (SILC_IS_LOCAL(chl->client))
       server->stat.my_chanclients--;
     if (server->server_type == SILC_ROUTER) {
       server->stat.cell_chanclients--;
@@ -948,6 +1014,8 @@ bool silc_server_connection_allowed(SilcServer server,
   SilcUInt32 r_software_version, l_software_version;
   char *r_vendor_version = NULL, *l_vendor_version;
 
+  SILC_LOG_DEBUG(("Checking whether connection is allowed"));
+
   /* Check version */
 
   l_protocol_version = 
@@ -1307,12 +1375,17 @@ void silc_server_kill_client(SilcServer server,
   } else {
     /* Update statistics */
     server->stat.clients--;
-    server->stat.my_clients--;
     if (server->stat.cell_clients)
       server->stat.cell_clients--;
     SILC_OPER_STATS_UPDATE(remote_client, server, SILC_UMODE_SERVER_OPERATOR);
     SILC_OPER_STATS_UPDATE(remote_client, router, SILC_UMODE_ROUTER_OPERATOR);
 
+    if (SILC_IS_LOCAL(remote_client)) {
+      server->stat.my_clients--;
+      silc_schedule_task_del_by_context(server->schedule, remote_client);
+      silc_idlist_del_data(remote_client);
+    }
+
     /* Remove remote client */
     if (!silc_idlist_del_client(server->global_list, remote_client)) {
       /* Remove this client from watcher list if it is */
@@ -1465,3 +1538,25 @@ bool silc_server_force_cumode_change(SilcServer server,
 
   return TRUE;
 }
+
+/* Find active socket connection by the IP address and port indicated by
+   `ip' and `port', and socket connection type of `type'. */
+
+SilcSocketConnection
+silc_server_find_socket_by_host(SilcServer server,
+                               SilcSocketType type,
+                               const char *ip, SilcUInt16 port)
+{
+  int i;
+
+  for (i = 0; i < server->config->param.connections_max; i++) {
+    if (!server->sockets[i])
+      continue;
+    if (!strcmp(server->sockets[i]->ip, ip) &&
+       (!port || server->sockets[i]->port == port) &&
+       server->sockets[i]->type == type)
+      return server->sockets[i];
+  }
+
+  return NULL;
+}