Send resume notifications to previous owner server when client resumes.
[silc.git] / apps / silcd / packet_receive.c
index c2c57ed75dee74d14173c59de6f14cb51fc26b0b..ac8156106e02c5616018027769623d1ff4fe015b 100644 (file)
@@ -326,6 +326,7 @@ static void silc_server_notify_process(SilcServer server,
       tmp_len = 128;
 
     /* Update statistics */
+    SILC_VERIFY(server->stat.clients > 0);
     server->stat.clients--;
     if (server->stat.cell_clients)
       server->stat.cell_clients--;
@@ -356,6 +357,8 @@ static void silc_server_notify_process(SilcServer server,
     client->mode = 0;
     client->router = NULL;
     client->connection = NULL;
+    client->data.created = silc_time();
+    silc_dlist_del(server->expired_clients, client);
     silc_dlist_add(server->expired_clients, client);
     break;
 
@@ -1243,6 +1246,7 @@ static void silc_server_notify_process(SilcServer server,
            }
 
            /* Update statistics */
+           SILC_VERIFY(server->stat.clients > 0);
            server->stat.clients--;
            if (server->stat.cell_clients)
              server->stat.cell_clients--;
@@ -1263,6 +1267,7 @@ static void silc_server_notify_process(SilcServer server,
              silc_server_del_from_watcher_list(server, client);
 
            /* Remove the client */
+           silc_dlist_del(server->expired_clients, client);
            silc_idlist_del_data(client);
            silc_idlist_del_client(local ? server->local_list :
                                   server->global_list, client);
@@ -1487,6 +1492,7 @@ static void silc_server_notify_process(SilcServer server,
       }
 
       /* Update statistics */
+      SILC_VERIFY(server->stat.clients > 0);
       server->stat.clients--;
       if (server->stat.cell_clients)
        server->stat.cell_clients--;
@@ -1504,6 +1510,8 @@ static void silc_server_notify_process(SilcServer server,
       client->mode = 0;
       client->router = NULL;
       client->connection = NULL;
+      client->data.created = silc_time();
+      silc_dlist_del(server->expired_clients, client);
       silc_dlist_add(server->expired_clients, client);
       break;
     }
@@ -1664,6 +1672,7 @@ static void silc_server_notify_process(SilcServer server,
 
        silc_server_remove_from_channels(server, NULL, client, TRUE,
                                         NULL, TRUE, FALSE);
+       silc_dlist_del(server->expired_clients, client);
        silc_idlist_del_data(client);
        silc_idlist_del_client(server->global_list, client);
       }
@@ -2145,7 +2154,7 @@ SilcClientEntry silc_server_new_client(SilcServer server,
   if (silc_buffer_unformat(buffer,
                           SILC_STR_UI16_NSTRING_ALLOC(&nickname,
                                                       &nickname_len),
-                          SILC_STR_END)) {
+                          SILC_STR_END) >= 0) {
     if (nickname_len > 128) {
       nickname_len = 128;
       nickname[nickname_len - 1] = '\0';
@@ -3715,6 +3724,7 @@ void silc_server_resume_client(SilcServer server,
     detached_client->data.status &= ~SILC_IDLIST_STATUS_RESUME_RES;
     detached_client->mode &= ~SILC_UMODE_DETACHED;
     server->stat.my_detached--;
+    silc_dlist_del(server->expired_clients, detached_client);
 
     /* We are finished - reset resuming client */
     detached_client->resuming_client = NULL;
@@ -3724,14 +3734,20 @@ void silc_server_resume_client(SilcServer server,
       silc_server_check_watcher_list(server, detached_client, NULL,
                                     SILC_NOTIFY_TYPE_UMODE_CHANGE);
 
-    /* Delete this current client entry since we're resuming to old one. */
-    server->stat.my_clients--;
+    /* Delete this current client entry since we're resuming to old one.
+       We decrement clients/cell_clients as we are getting rid of the
+       current client and replacing it with the detached one.  We keep the
+       server user count as-is (incremented by the current client entry) as
+       we decremented the count already during detach, thus we'd be undoing
+       that operation. */
+    SILC_VERIFY(server->stat.clients > 0);
     server->stat.clients--;
     if (server->stat.cell_clients)
       server->stat.cell_clients--;
     silc_server_remove_from_channels(server, NULL, client, FALSE,
                                     NULL, FALSE, FALSE);
     silc_server_del_from_watcher_list(server, client);
+    silc_dlist_del(server->expired_clients, client);
     if (!silc_idlist_del_client(server->local_list, client))
       silc_idlist_del_client(server->global_list, client);
     client = detached_client;
@@ -3935,6 +3951,23 @@ void silc_server_resume_client(SilcServer server,
                              FALSE, TRUE);
     }
 
+    /* If the client has a locally-connected previous owner, then we
+       will need to notify them that the resume has completed.  Note
+       that if the previous owner was a router, this case is already
+       handled above by the broadcast, so we shouldn't attempt to
+       send another notification in that case.   Additionally, if
+       the previous owner was the server that sent the packet, then
+       we'll not send the notification as it will have already done
+       the necessary work locally. */
+    if (server->server_type == SILC_ROUTER &&
+       idata->conn_type == SILC_CONN_SERVER &&
+       detached_client->router &&
+       SILC_IS_LOCAL(detached_client->router) &&
+       detached_client->router->server_type != SILC_ROUTER)
+      silc_server_packet_send(server, detached_client->router->connection,
+                             SILC_PACKET_RESUME_CLIENT, 0,
+                             buffer->data, silc_buffer_len(buffer));
+
     /* Client is detached, and now it is resumed.  Remove the detached
        mode and mark that it is resumed. */