+ /* Notify our router about new client on the SILC network */
+ if (!server->standalone)
+ silc_server_send_new_id(server, (SilcSocketConnection)
+ server->router->connection,
+ server->server_type == SILC_ROUTER ? TRUE : FALSE,
+ client->id, SILC_ID_CLIENT, id_len);
+
+ /* Send the new client ID to the client. */
+ id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
+ reply = silc_buffer_alloc(2 + 2 + id_len);
+ silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
+ silc_buffer_format(reply,
+ SILC_STR_UI_SHORT(SILC_ID_CLIENT),
+ SILC_STR_UI_SHORT(id_len),
+ SILC_STR_UI_XNSTRING(id_string, id_len),
+ SILC_STR_END);
+ silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
+ reply->data, reply->len, FALSE);
+ silc_free(id_string);
+ silc_buffer_free(reply);
+
+ /* Send some nice info to the client */
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("Welcome to the SILC Network %s",
+ username));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("Your host is %s, running version %s",
+ server->config->server_info->server_name,
+ server_version));
+ if (server->server_type == SILC_ROUTER) {
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("There are %d clients on %d servers in SILC "
+ "Network", server->stat.clients,
+ server->stat.servers + 1));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("There are %d clients on %d server in our cell",
+ server->stat.cell_clients,
+ server->stat.cell_servers + 1));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("I have %d clients, %d channels, %d servers and "
+ "%d routers",
+ server->stat.my_clients,
+ server->stat.my_channels,
+ server->stat.my_servers,
+ server->stat.my_routers));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("There are %d server operators and %d router "
+ "operators online",
+ server->stat.server_ops,
+ server->stat.router_ops));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("I have %d operators online",
+ server->stat.my_router_ops +
+ server->stat.my_server_ops));
+ } else {
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("I have %d clients and %d channels formed",
+ server->stat.my_clients,
+ server->stat.my_channels));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("%d operators online",
+ server->stat.my_server_ops));
+ }
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("Your connection is secured with %s cipher, "
+ "key length %d bits",
+ idata->send_key->cipher->name,
+ idata->send_key->cipher->key_len));
+ SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
+ ("Your current nickname is %s",
+ client->nickname));
+
+ /* Send motd */
+ silc_server_send_motd(server, sock);
+
+ return client;
+}
+
+/* Create new server. This processes received New Server packet and
+ saves the received Server ID. The server is our locally connected
+ server thus we save all the information and save it to local list.
+ This funtion can be used by both normal server and router server.
+ If normal server uses this it means that its router has connected
+ to the server. If router uses this it means that one of the cell's
+ servers is connected to the router. */
+
+SilcServerEntry silc_server_new_server(SilcServer server,
+ SilcSocketConnection sock,
+ SilcPacketContext *packet)
+{
+ SilcBuffer buffer = packet->buffer;
+ SilcServerEntry new_server, server_entry;
+ SilcServerID *server_id;
+ SilcIDListData idata;
+ unsigned char *server_name, *id_string;
+ uint16 id_len, name_len;
+ int ret;
+ bool local = TRUE;
+
+ SILC_LOG_DEBUG(("Creating new server"));
+
+ if (sock->type != SILC_SOCKET_TYPE_SERVER &&
+ sock->type != SILC_SOCKET_TYPE_ROUTER)
+ return NULL;
+
+ /* Take server entry */
+ new_server = (SilcServerEntry)sock->user_data;
+ idata = (SilcIDListData)new_server;
+
+ /* Remove the old cache entry */
+ if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
+ silc_idcache_del_by_context(server->global_list->servers, new_server);
+ local = FALSE;
+ }
+
+ /* Parse the incoming packet */
+ ret = silc_buffer_unformat(buffer,
+ SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&server_name,
+ &name_len),
+ SILC_STR_END);
+ if (ret == -1) {
+ if (id_string)
+ silc_free(id_string);
+ if (server_name)
+ silc_free(server_name);
+ return NULL;
+ }
+
+ if (id_len > buffer->len) {
+ silc_free(id_string);
+ silc_free(server_name);
+ return NULL;
+ }
+
+ if (name_len > 256)
+ server_name[255] = '\0';
+
+ /* Get Server ID */
+ server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
+ if (!server_id) {
+ silc_free(id_string);
+ silc_free(server_name);
+ return NULL;
+ }
+ silc_free(id_string);
+
+ /* Check that we do not have this ID already */
+ server_entry = silc_idlist_find_server_by_id(server->local_list,
+ server_id, TRUE, NULL);
+ if (server_entry) {
+ silc_idcache_del_by_context(server->local_list->servers, server_entry);
+ } else {
+ server_entry = silc_idlist_find_server_by_id(server->global_list,
+ server_id, TRUE, NULL);
+ if (server_entry)
+ silc_idcache_del_by_context(server->global_list->servers, server_entry);
+ }
+
+ /* Update server entry */
+ idata->status |= SILC_IDLIST_STATUS_REGISTERED;
+ new_server->server_name = server_name;
+ new_server->id = server_id;
+
+ SILC_LOG_DEBUG(("New server id(%s)",
+ silc_id_render(server_id, SILC_ID_SERVER)));
+
+ /* Add again the entry to the ID cache. */
+ silc_idcache_add(local ? server->local_list->servers :
+ server->global_list->servers, server_name, server_id,
+ new_server, 0, NULL);
+
+ /* Distribute the information about new server in the SILC network
+ to our router. If we are normal server we won't send anything
+ since this connection must be our router connection. */
+ if (server->server_type == SILC_ROUTER && !server->standalone &&
+ server->router->connection != sock)
+ silc_server_send_new_id(server, server->router->connection,
+ TRUE, new_server->id, SILC_ID_SERVER,
+ silc_id_get_len(server_id, SILC_ID_SERVER));
+
+ if (server->server_type == SILC_ROUTER)
+ server->stat.cell_servers++;