From 320bf875dcc0933966191c51c7a4f38f77a1c22a Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Sun, 14 Oct 2001 20:52:37 +0000 Subject: [PATCH] updates. --- CHANGES | 15 ++ TODO | 5 + apps/silcd/command.c | 7 +- apps/silcd/packet_receive.c | 48 ++++-- apps/silcd/packet_send.c | 297 ++++++++++++++++---------------- apps/silcd/server.c | 33 ++-- apps/silcd/server_backup.c | 137 +++++++++++++-- apps/silcd/server_util.c | 74 +++++++- doc/examples/cell1_server1.conf | 12 +- doc/examples/cell1_server2.conf | 12 +- doc/examples/cell2_server1.conf | 10 +- doc/examples/cell2_server2.conf | 10 +- doc/examples/cell3_server1.conf | 10 +- doc/examples/cell3_server2.conf | 10 +- 14 files changed, 452 insertions(+), 228 deletions(-) diff --git a/CHANGES b/CHANGES index e4f88dbb..a655b2a7 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,18 @@ +Sun Oct 14 18:28:22 EDT 2001 Pekka Riikonen + + * Assure that router cannot reroute the same channel message + to the sender. Affected file silcd/packet_receive.c. + +Sat Oct 13 12:46:18 EDT 2001 Pekka Riikonen + + * Made better checks that the channel message is not sent + to the router it came from. Affected file is + silcd/packet_send.c. Fixed memory leak too. + + * Announce informations for incoming router connection, but + only after checking if it is replaced by backup router. + Affected file silcd/packet_receive.c. + Fri Oct 12 18:37:24 EDT 2001 Pekka Riikonen * Fixed the backup resuming protocol to work in multiple diff --git a/TODO b/TODO index fa210ce6..0a37eb5d 100644 --- a/TODO +++ b/TODO @@ -46,6 +46,8 @@ TODO/bugs In SILC Client Library TODO/bugs In SILC Server ======================== + o Server signoff notifys does not go to normal servers from routers. + o On normal server the channel count can go negative (like -3 channels). o Change the sever to connect to another server from low ports (706) @@ -82,6 +84,9 @@ TODO/bugs In SILC Server returns only the channels the server knows about. The protocol spec does not prohibit of sending the LIST to the router. + o Announcements are incomplete: channel topics are not announced, + user modes (UMODE) are not announced. + o Incomplete IPv6 support: o silcd/serverid.c and its routines supports only IPv4. diff --git a/apps/silcd/command.c b/apps/silcd/command.c index 3e94f304..0eae9abc 100644 --- a/apps/silcd/command.c +++ b/apps/silcd/command.c @@ -1341,7 +1341,7 @@ silc_server_command_identify_parse(SilcServerCommandContext cmd, entry = silc_idlist_find_server_by_name(server->local_list, tmp, TRUE, NULL); if (!entry && check_global) - entry = silc_idlist_find_server_by_name(server->local_list, + entry = silc_idlist_find_server_by_name(server->global_list, tmp, TRUE, NULL); if (entry) { *servers = silc_realloc(*servers, sizeof(**servers) * @@ -1363,7 +1363,7 @@ silc_server_command_identify_parse(SilcServerCommandContext cmd, entry = silc_idlist_find_channel_by_name(server->local_list, tmp, NULL); if (!entry && check_global) - entry = silc_idlist_find_channel_by_name(server->local_list, + entry = silc_idlist_find_channel_by_name(server->global_list, tmp, NULL); if (entry) { *channels = silc_realloc(*channels, sizeof(**channels) * @@ -4811,6 +4811,9 @@ SILC_SERVER_CMD_FUNC(close) server_entry = silc_idlist_find_server_by_conn(server->local_list, name, port, FALSE, NULL); + if (!server_entry) + server_entry = silc_idlist_find_server_by_conn(server->global_list, + name, port, FALSE, NULL); if (!server_entry) { silc_server_command_send_status_reply(cmd, SILC_COMMAND_CLOSE, SILC_STATUS_ERR_NO_SERVER_ID); diff --git a/apps/silcd/packet_receive.c b/apps/silcd/packet_receive.c index 044bb49c..06dc814c 100644 --- a/apps/silcd/packet_receive.c +++ b/apps/silcd/packet_receive.c @@ -1277,6 +1277,7 @@ void silc_server_channel_message(SilcServer server, SilcChannelID *id = NULL; void *sender = NULL; void *sender_entry = NULL; + bool local = TRUE; SILC_LOG_DEBUG(("Processing channel message")); @@ -1308,14 +1309,25 @@ void silc_server_channel_message(SilcServer server, if (packet->src_id_type == SILC_ID_CLIENT) { sender_entry = silc_idlist_find_client_by_id(server->local_list, sender, TRUE, NULL); - if (!sender_entry) + if (!sender_entry) { + local = FALSE; sender_entry = silc_idlist_find_client_by_id(server->global_list, sender, TRUE, NULL); + } if (!sender_entry || !silc_server_client_on_channel(sender_entry, channel)) { SILC_LOG_DEBUG(("Client not on channel")); goto out; } + + /* If the packet is coming from router, but the client entry is + local entry to us then some router is rerouting this to us and it is + not allowed. */ + if (server->server_type == SILC_ROUTER && + sock->type == SILC_SOCKET_TYPE_ROUTER && local) { + SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it")); + goto out; + } } /* Distribute the packet to our local clients. This will send the @@ -1624,6 +1636,7 @@ SilcServerEntry silc_server_new_server(SilcServer server, unsigned char *server_name, *id_string; uint16 id_len, name_len; int ret; + bool local = TRUE; SILC_LOG_DEBUG(("Creating new server")); @@ -1636,7 +1649,10 @@ SilcServerEntry silc_server_new_server(SilcServer server, idata = (SilcIDListData)new_server; /* Remove the old cache entry */ - silc_idcache_del_by_context(server->local_list->servers, new_server); + 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, @@ -1691,7 +1707,8 @@ SilcServerEntry silc_server_new_server(SilcServer server, silc_id_render(server_id, SILC_ID_SERVER))); /* Add again the entry to the ID cache. */ - silc_idcache_add(server->local_list->servers, server_name, server_id, + silc_idcache_add(local ? server->local_list->servers : + server->global_list->servers, server_name, server_id, new_server, FALSE); /* Distribute the information about new server in the SILC network @@ -1728,6 +1745,14 @@ SilcServerEntry silc_server_new_server(SilcServer server, /* Mark the router disabled. The data sent earlier will go but nothing after this does not go to this connection. */ idata->status |= SILC_IDLIST_STATUS_DISABLED; + } else { + /* If it is router announce our stuff to it. */ + if (sock->type == SILC_SOCKET_TYPE_ROUTER && + server->server_type == SILC_ROUTER) { + silc_server_announce_servers(server, FALSE, 0, sock); + silc_server_announce_clients(server, 0, sock); + silc_server_announce_channels(server, 0, sock); + } } return new_server; @@ -1743,7 +1768,7 @@ static void silc_server_new_id_real(SilcServer server, { SilcBuffer buffer = packet->buffer; SilcIDList id_list; - SilcServerEntry router; + SilcServerEntry router, server_entry; SilcSocketConnection router_sock; SilcIDPayload idp; SilcIdType id_type; @@ -1763,24 +1788,26 @@ static void silc_server_new_id_real(SilcServer server, id_type = silc_id_payload_get_type(idp); /* Normal server cannot have other normal server connections */ - if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER) + server_entry = (SilcServerEntry)sock->user_data; + if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER && + server_entry->server_type == SILC_SERVER) goto out; id = silc_id_payload_get_id(idp); if (!id) goto out; - if (sock->type == SILC_SOCKET_TYPE_SERVER) - id_list = server->local_list; - else - id_list = server->global_list; - /* If the packet is coming from server then use the sender as the origin of the the packet. If it came from router then check the real sender of the packet and use that as the origin. */ if (sock->type == SILC_SOCKET_TYPE_SERVER) { + id_list = server->local_list; router_sock = sock; router = sock->user_data; + + if (server_entry->server_type == SILC_BACKUP_ROUTER && + id_type == SILC_ID_SERVER) + id_list = server->global_list; } else { void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type); @@ -1793,6 +1820,7 @@ static void silc_server_new_id_real(SilcServer server, if (!router) goto out; router_sock = sock; + id_list = server->global_list; } switch(id_type) { diff --git a/apps/silcd/packet_send.c b/apps/silcd/packet_send.c index c6fcdf1a..16020908 100644 --- a/apps/silcd/packet_send.c +++ b/apps/silcd/packet_send.c @@ -449,6 +449,7 @@ void silc_server_packet_send_to_channel(SilcServer server, SilcIDListData idata; uint32 routed_count = 0; bool gone = FALSE; + int k; /* This doesn't send channel message packets */ assert(type != SILC_PACKET_CHANNEL_MESSAGE); @@ -490,17 +491,21 @@ void silc_server_packet_send_to_channel(SilcServer server, } } + routed = silc_calloc(silc_hash_table_count(channel->user_list), + sizeof(*routed)); + /* Send the message to clients on the channel's client list. */ silc_hash_table_list(channel->user_list, &htl); while (silc_hash_table_get(&htl, NULL, (void *)&chl)) { client = chl->client; + if (!client) + continue; /* If client has router set it is not locally connected client and we will route the message to the router set in the client. Though, send locally connected server in all cases. */ - if (server->server_type == SILC_ROUTER && client && client->router && + if (server->server_type == SILC_ROUTER && client->router && ((!route && client->router->router == server->id_entry) || route)) { - int k; /* Check if we have sent the packet to this route already */ for (k = 0; k < routed_count; k++) @@ -530,40 +535,32 @@ void silc_server_packet_send_to_channel(SilcServer server, data, data_len, FALSE, force_send); - /* We want to make sure that the packet is routed to same router - only once. Mark this route as sent route. */ - k = routed_count; - routed = silc_realloc(routed, sizeof(*routed) * (k + 1)); - routed[k] = client->router; - routed_count++; - + /* Mark this route routed already */ + routed[routed_count++] = client->router; continue; } - if (client && client->router) + if (client->router) continue; /* Send to locally connected client */ - if (client) { - - /* Get data used in packet header encryption, keys and stuff. */ - sock = (SilcSocketConnection)client->connection; - idata = (SilcIDListData)client; - if (sender && sock == sender) - continue; + /* Get data used in packet header encryption, keys and stuff. */ + sock = (SilcSocketConnection)client->connection; + idata = (SilcIDListData)client; + + if (sender && sock == sender) + continue; /* Send the packet */ - silc_server_packet_send_to_channel_real(server, sock, &packetdata, - idata->send_key, - idata->hmac_send, - data, data_len, FALSE, - force_send); - } + silc_server_packet_send_to_channel_real(server, sock, &packetdata, + idata->send_key, + idata->hmac_send, + data, data_len, FALSE, + force_send); } - if (routed_count) - silc_free(routed); + silc_free(routed); silc_free(packetdata.src_id); silc_free(packetdata.dst_id); } @@ -648,6 +645,7 @@ void silc_server_packet_relay_to_channel(SilcServer server, SilcIDListData idata; SilcHashTableList htl; bool gone = FALSE; + int k; SILC_LOG_DEBUG(("Relaying packet to channel")); @@ -677,9 +675,7 @@ void silc_server_packet_relay_to_channel(SilcServer server, first to our router for further routing. */ if (server->server_type != SILC_ROUTER && !server->standalone && channel->global_users) { - SilcServerEntry router; - - router = server->router; + SilcServerEntry router = server->router; /* Check that the sender is not our router. */ if (sender_sock != (SilcSocketConnection)router->connection) { @@ -698,148 +694,155 @@ void silc_server_packet_relay_to_channel(SilcServer server, } } + routed = silc_calloc(silc_hash_table_count(channel->user_list), + sizeof(*routed)); + + /* Mark that to the route the original sender if from is not routed */ + if (sender_type == SILC_ID_CLIENT) { + client = (SilcClientEntry)sender_entry; + if (client->router) { + routed[routed_count++] = client->router; + SILC_LOG_DEBUG(("************* router %s", + silc_id_render(client->router->id, SILC_ID_SERVER))); + } + } + /* Send the message to clients on the channel's client list. */ silc_hash_table_list(channel->user_list, &htl); while (silc_hash_table_get(&htl, NULL, (void *)&chl)) { client = chl->client; + if (!client) + continue; + + /* Do not send to the sender */ + if (!found && client == sender_entry) { + found = TRUE; + continue; + } - if (client) { + /* If the client has set router it means that it is not locally + connected client and we will route the packet further. */ + if (server->server_type == SILC_ROUTER && client->router) { - /* If sender is one on the channel do not send it the packet. */ - if (!found && sender_type == SILC_ID_CLIENT && - SILC_ID_CLIENT_COMPARE(client->id, sender)) { + /* Sender maybe server as well so we want to make sure that + we won't send the message to the server it came from. */ + if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) { found = TRUE; + routed[routed_count++] = client->router; continue; } - /* If the client has set router it means that it is not locally - connected client and we will route the packet further. */ - if (server->server_type == SILC_ROUTER && client->router) { - int k; + /* Check if we have sent the packet to this route already */ + for (k = 0; k < routed_count; k++) + if (routed[k] == client->router) + break; + if (k < routed_count) + continue; + + /* Get data used in packet header encryption, keys and stuff. */ + sock = (SilcSocketConnection)client->router->connection; + idata = (SilcIDListData)client->router; - /* Sender maybe server as well so we want to make sure that - we won't send the message to the server it came from. */ - if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) { - found = TRUE; - continue; - } + /* Do not send to the sender. Check first whether the true + sender's router is same as this client's router. Also check + if the sender socket is the same as this client's router + socket. */ + if (sender_entry && + ((SilcClientEntry)sender_entry)->router == client->router) + continue; + if (sender_sock && sock == sender_sock) + continue; - /* Check if we have sent the packet to this route already */ - for (k = 0; k < routed_count; k++) - if (routed[k] == client->router) - break; - if (k < routed_count) - continue; + SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)", + silc_id_render(client->id, SILC_ID_CLIENT), + sock->hostname, sock->ip)); + + /* Mark this route routed already. */ + routed[routed_count++] = client->router; - /* Get data used in packet header encryption, keys and stuff. */ - sock = (SilcSocketConnection)client->router->connection; - idata = (SilcIDListData)client->router; - - /* Do not send to the sender. Check first whether the true - sender's router is same as this client's router. Also check - if the sender socket is the same as this client's router - socket. */ - if (sender_entry && - ((SilcClientEntry)sender_entry)->router == client->router) - continue; - if (sender_sock && sock == sender_sock) + /* If the remote connection is router then we'll decrypt the + channel message and re-encrypt it with the session key shared + between us and the remote router. This is done because the + channel keys are cell specific and we have different channel + key than the remote router has. */ + if (sock->type == SILC_SOCKET_TYPE_ROUTER) { + if (gone) continue; - SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)", - silc_id_render(client->id, SILC_ID_CLIENT), - sock->hostname, sock->ip)); - - /* We want to make sure that the packet is routed to same router - only once. Mark this route as sent route. */ - k = routed_count; - routed = silc_realloc(routed, sizeof(*routed) * (k + 1)); - routed[k] = client->router; - routed_count++; - - /* If the remote connection is router then we'll decrypt the - channel message and re-encrypt it with the session key shared - between us and the remote router. This is done because the - channel keys are cell specific and we have different channel - key than the remote router has. */ - if (sock->type == SILC_SOCKET_TYPE_ROUTER) { - - if (gone) - continue; + SILC_LOG_DEBUG(("Remote is router, encrypt with session key")); + gone = TRUE; - SILC_LOG_DEBUG(("Remote is router, encrypt with session key")); - gone = TRUE; - - /* If private key mode is not set then decrypt the packet - and re-encrypt it */ - if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) { - unsigned char *tmp = silc_calloc(data_len, sizeof(*data)); - memcpy(tmp, data, data_len); - - /* Decrypt the channel message (we don't check the MAC) */ - if (channel->channel_key && - !silc_channel_message_payload_decrypt(tmp, data_len, - channel->channel_key, - NULL)) { - memset(tmp, 0, data_len); - silc_free(tmp); - continue; - } - - /* Now re-encrypt and send it to the router */ - silc_server_packet_send_srcdest(server, sock, - SILC_PACKET_CHANNEL_MESSAGE, 0, - sender, sender_type, - channel->id, SILC_ID_CHANNEL, - tmp, data_len, force_send); - - /* Free the copy of the channel message */ + /* If private key mode is not set then decrypt the packet + and re-encrypt it */ + if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) { + unsigned char *tmp = silc_calloc(data_len, sizeof(*data)); + memcpy(tmp, data, data_len); + + /* Decrypt the channel message (we don't check the MAC) */ + if (channel->channel_key && + !silc_channel_message_payload_decrypt(tmp, data_len, + channel->channel_key, + NULL)) { memset(tmp, 0, data_len); silc_free(tmp); - } else { - /* Private key mode is set, we don't have the channel key, so - just re-encrypt the entire packet and send it to the router. */ - silc_server_packet_send_srcdest(server, sock, - SILC_PACKET_CHANNEL_MESSAGE, 0, - sender, sender_type, - channel->id, SILC_ID_CHANNEL, - data, data_len, force_send); + continue; } - continue; - } - - /* Send the packet (to normal server) */ - silc_server_packet_send_to_channel_real(server, sock, &packetdata, - idata->send_key, - idata->hmac_send, - data, data_len, TRUE, - force_send); + /* Now re-encrypt and send it to the router */ + silc_server_packet_send_srcdest(server, sock, + SILC_PACKET_CHANNEL_MESSAGE, 0, + sender, sender_type, + channel->id, SILC_ID_CHANNEL, + tmp, data_len, force_send); + + /* Free the copy of the channel message */ + memset(tmp, 0, data_len); + silc_free(tmp); + } else { + /* Private key mode is set, we don't have the channel key, so + just re-encrypt the entire packet and send it to the router. */ + silc_server_packet_send_srcdest(server, sock, + SILC_PACKET_CHANNEL_MESSAGE, 0, + sender, sender_type, + channel->id, SILC_ID_CHANNEL, + data, data_len, force_send); + } continue; } - if (client && client->router) - continue; - - /* Get data used in packet header encryption, keys and stuff. */ - sock = (SilcSocketConnection)client->connection; - idata = (SilcIDListData)client; - - if (sender_sock && sock == sender_sock) - continue; - - SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)", - silc_id_render(client->id, SILC_ID_CLIENT), - sock->hostname, sock->ip)); - - /* Send the packet */ + /* Send the packet (to normal server) */ silc_server_packet_send_to_channel_real(server, sock, &packetdata, idata->send_key, idata->hmac_send, data, data_len, TRUE, force_send); + + continue; } + + if (client->router) + continue; + + /* Get data used in packet header encryption, keys and stuff. */ + sock = (SilcSocketConnection)client->connection; + idata = (SilcIDListData)client; + + if (sender_sock && sock == sender_sock) + continue; + + SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)", + silc_id_render(client->id, SILC_ID_CLIENT), + sock->hostname, sock->ip)); + + /* Send the packet */ + silc_server_packet_send_to_channel_real(server, sock, &packetdata, + idata->send_key, + idata->hmac_send, + data, data_len, TRUE, + force_send); } + silc_free(routed); silc_free(packetdata.src_id); silc_free(packetdata.dst_id); } @@ -1458,11 +1461,8 @@ void silc_server_send_notify_on_channels(SilcServer server, /* We want to make sure that the packet is routed to same router only once. Mark this route as sent route. */ - k = routed_count; - routed = silc_realloc(routed, sizeof(*routed) * (k + 1)); - routed[k] = c->router; - routed_count++; - + routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1)); + routed[routed_count++] = c->router; continue; } @@ -1495,16 +1495,13 @@ void silc_server_send_notify_on_channels(SilcServer server, /* Make sure that we send the notify only once per client. */ sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) * (sent_clients_count + 1)); - sent_clients[sent_clients_count] = c; - sent_clients_count++; + sent_clients[sent_clients_count++] = c; } } } - if (routed_count) - silc_free(routed); - if (sent_clients_count) - silc_free(sent_clients); + silc_free(routed); + silc_free(sent_clients); silc_free(packetdata.src_id); va_end(ap); } diff --git a/apps/silcd/server.c b/apps/silcd/server.c index dfc40225..ae8592c4 100644 --- a/apps/silcd/server.c +++ b/apps/silcd/server.c @@ -891,8 +891,9 @@ SILC_TASK_CALLBACK(silc_server_connect_to_router_final) SILC_LOG_DEBUG(("New server id(%s)", silc_id_render(ctx->dest_id, SILC_ID_SERVER))); - /* Add the connected router to local server list */ - id_entry = silc_idlist_add_server(server->local_list, strdup(sock->hostname), + /* Add the connected router to global server list */ + id_entry = silc_idlist_add_server(server->global_list, + strdup(sock->hostname), SILC_ROUTER, ctx->dest_id, NULL, sock); if (!id_entry) { silc_free(ctx->dest_id); @@ -1336,13 +1337,18 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_final) server. We mark ourselves as router for this server if we really are router. */ new_server = - silc_idlist_add_server(server->local_list, NULL, - ctx->conn_type == SILC_SOCKET_TYPE_SERVER ? - SILC_SERVER : SILC_ROUTER, NULL, - ctx->conn_type == SILC_SOCKET_TYPE_SERVER ? - server->id_entry : - (conn->backup_router ? server->id_entry : - NULL), sock); + silc_idlist_add_server((ctx->conn_type == SILC_SOCKET_TYPE_SERVER ? + server->local_list : (conn->backup_router ? + server->local_list : + server->global_list)), + NULL, + (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ? + SILC_SERVER : SILC_ROUTER), + NULL, + (ctx->conn_type == SILC_SOCKET_TYPE_SERVER ? + server->id_entry : (conn->backup_router ? + server->id_entry : NULL)), + sock); if (!new_server) { SILC_LOG_ERROR(("Could not add new server to cache")); silc_free(sock->user_data); @@ -1369,10 +1375,10 @@ SILC_TASK_CALLBACK(silc_server_accept_new_connection_final) conn->backup_replace_port, conn->backup_local); /* Change it back to SERVER type since that's what it really is. */ - if (conn->backup_local) { + if (conn->backup_local) ctx->conn_type = SILC_SOCKET_TYPE_SERVER; - new_server->server_type = SILC_BACKUP_ROUTER; - } + + new_server->server_type = SILC_BACKUP_ROUTER; } /* Check whether this connection is to be our primary router connection @@ -2399,7 +2405,8 @@ void silc_server_free_sock_user_data(SilcServer server, silc_server_backup_del(server, user_data); silc_server_backup_replaced_del(server, user_data); silc_idlist_del_data(user_data); - silc_idlist_del_server(server->local_list, user_data); + if (!silc_idlist_del_server(server->local_list, user_data)) + silc_idlist_del_server(server->global_list, user_data); server->stat.my_servers--; server->stat.servers--; if (server->server_type == SILC_ROUTER) diff --git a/apps/silcd/server_backup.c b/apps/silcd/server_backup.c index 8f345508..d040d454 100644 --- a/apps/silcd/server_backup.c +++ b/apps/silcd/server_backup.c @@ -741,8 +741,9 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) if (silc_idcache_list_first(list, &id_cache)) { while (id_cache) { server_entry = (SilcServerEntry)id_cache->context; - if ((server_entry == server->id_entry) || - !server_entry->connection) { + if (!server_entry || (server_entry == server->id_entry) || + !server_entry->connection || !server_entry->data.send_key || + (server_entry->data.status & SILC_IDLIST_STATUS_DISABLED)) { if (!silc_idcache_list_next(list, &id_cache)) break; else @@ -757,7 +758,54 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) ctx->sessions[ctx->sessions_count].server_entry = server_entry; SILC_LOG_DEBUG(("********************************")); - SILC_LOG_DEBUG(("START for session %d", ctx->sessions_count)); + SILC_LOG_DEBUG(("START (local) for session %d", + ctx->sessions_count)); + + /* This connection is performing this protocol too now */ + ((SilcSocketConnection)server_entry->connection)->protocol = + protocol; + + if (server_entry->server_type == SILC_ROUTER) + packet->data[0] = SILC_SERVER_BACKUP_START; + else + packet->data[0] = SILC_SERVER_BACKUP_START_GLOBAL; + packet->data[1] = ctx->sessions_count; + silc_server_packet_send(server, server_entry->connection, + SILC_PACKET_RESUME_ROUTER, 0, + packet->data, packet->len, FALSE); + ctx->sessions_count++; + + if (!silc_idcache_list_next(list, &id_cache)) + break; + } + } + + silc_idcache_list_free(list); + } + + if (silc_idcache_get_all(server->global_list->servers, &list)) { + if (silc_idcache_list_first(list, &id_cache)) { + while (id_cache) { + server_entry = (SilcServerEntry)id_cache->context; + if (!server_entry || (server_entry == server->id_entry) || + !server_entry->connection || !server_entry->data.send_key || + (server_entry->data.status & SILC_IDLIST_STATUS_DISABLED)) { + if (!silc_idcache_list_next(list, &id_cache)) + break; + else + continue; + } + + ctx->sessions = silc_realloc(ctx->sessions, + sizeof(*ctx->sessions) * + (ctx->sessions_count + 1)); + ctx->sessions[ctx->sessions_count].session = ctx->sessions_count; + ctx->sessions[ctx->sessions_count].connected = FALSE; + ctx->sessions[ctx->sessions_count].server_entry = server_entry; + + SILC_LOG_DEBUG(("********************************")); + SILC_LOG_DEBUG(("START (global) for session %d", + ctx->sessions_count)); /* This connection is performing this protocol too now */ ((SilcSocketConnection)server_entry->connection)->protocol = @@ -783,9 +831,8 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) silc_buffer_free(packet); - /* If we are router then announce our possible servers. */ - if (server->server_type == SILC_ROUTER) - silc_server_announce_servers(server, FALSE, 0, ctx->sock); + /* Announce all of our information */ + silc_server_announce_servers(server, TRUE, 0, ctx->sock); silc_server_announce_clients(server, 0, ctx->sock); silc_server_announce_channels(server, 0, ctx->sock); @@ -918,10 +965,10 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) break; } - /* Switch announced informations to our entry instead of using the + /* Switch announced informations to our primary router of using the backup router. */ silc_server_update_clients_by_server(server, ctx->sock->user_data, - server->id_entry, TRUE, FALSE); + server->router, TRUE, FALSE); packet = silc_buffer_alloc(2); silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet)); @@ -931,8 +978,45 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) if (silc_idcache_list_first(list, &id_cache)) { while (id_cache) { server_entry = (SilcServerEntry)id_cache->context; - if ((server_entry == server->id_entry) || - !server_entry->connection) { + if (!server_entry || (server_entry == server->id_entry) || + !server_entry->connection || !server_entry->data.send_key) { + if (!silc_idcache_list_next(list, &id_cache)) + break; + else + continue; + } + + SILC_LOG_DEBUG(("********************************")); + SILC_LOG_DEBUG(("RESUMED packet (local)")); + + server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED; + + /* This connection is performing this protocol too now */ + ((SilcSocketConnection)server_entry->connection)->protocol = + protocol; + + if (server_entry->server_type == SILC_ROUTER) + packet->data[0] = SILC_SERVER_BACKUP_RESUMED; + else + packet->data[0] = SILC_SERVER_BACKUP_RESUMED_GLOBAL; + silc_server_packet_send(server, server_entry->connection, + SILC_PACKET_RESUME_ROUTER, 0, + packet->data, packet->len, FALSE); + + if (!silc_idcache_list_next(list, &id_cache)) + break; + } + } + + silc_idcache_list_free(list); + } + + if (silc_idcache_get_all(server->global_list->servers, &list)) { + if (silc_idcache_list_first(list, &id_cache)) { + while (id_cache) { + server_entry = (SilcServerEntry)id_cache->context; + if (!server_entry || (server_entry == server->id_entry) || + !server_entry->connection || !server_entry->data.send_key) { if (!silc_idcache_list_next(list, &id_cache)) break; else @@ -940,7 +1024,7 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) } SILC_LOG_DEBUG(("********************************")); - SILC_LOG_DEBUG(("RESUMED packet")); + SILC_LOG_DEBUG(("RESUMED packet (global)")); server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED; @@ -1001,12 +1085,12 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) &backup_router)) { if (backup_router == server->router) { + server->id_entry->router = router; + server->router = router; SILC_LOG_INFO(("Switching back to primary router %s", server->router->server_name)); SILC_LOG_DEBUG(("Switching back to primary router %s", server->router->server_name)); - server->id_entry->router = router; - server->router = router; } else { SILC_LOG_INFO(("Resuming the use of router %s", router->server_name)); @@ -1017,9 +1101,10 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) idata = (SilcIDListData)server->router; idata->status &= ~SILC_IDLIST_STATUS_DISABLED; - /* Update the client entries of the backup router to the new router */ + /* Update the client entries of the backup router to the new + router */ silc_server_update_clients_by_server(server, backup_router, - router, FALSE, FALSE); + router, TRUE, FALSE); silc_server_backup_replaced_del(server, backup_router); silc_server_backup_add(server, backup_router, ctx->sock->ip, ctx->sock->port, @@ -1029,7 +1114,7 @@ SILC_TASK_CALLBACK_GLOBAL(silc_server_protocol_backup) /* Announce all of our information to the router. */ if (server->server_type == SILC_ROUTER) silc_server_announce_servers(server, FALSE, 0, router->connection); - + /* Announce our clients and channels to the router */ silc_server_announce_clients(server, 0, router->connection); silc_server_announce_channels(server, 0, router->connection); @@ -1102,6 +1187,26 @@ SILC_TASK_CALLBACK(silc_server_protocol_backup_done) silc_idcache_list_free(list); } + if (silc_idcache_get_all(server->global_list->servers, &list)) { + if (silc_idcache_list_first(list, &id_cache)) { + while (id_cache) { + server_entry = (SilcServerEntry)id_cache->context; + sock = (SilcSocketConnection)server_entry->connection; + + if (sock->protocol == protocol) { + sock->protocol = NULL; + + if (server_entry->data.status & SILC_IDLIST_STATUS_DISABLED) + server_entry->data.status &= ~SILC_IDLIST_STATUS_DISABLED; + } + + if (!silc_idcache_list_next(list, &id_cache)) + break; + } + } + silc_idcache_list_free(list); + } + if (ctx->sock->protocol) ctx->sock->protocol = NULL; silc_protocol_free(protocol); diff --git a/apps/silcd/server_util.c b/apps/silcd/server_util.c index 7c6c1b3d..26c33fc6 100644 --- a/apps/silcd/server_util.c +++ b/apps/silcd/server_util.c @@ -330,7 +330,10 @@ bool silc_server_remove_clients_by_server(SilcServer server, static SilcServerEntry silc_server_update_clients_by_real_server(SilcServer server, - SilcClientEntry client) + SilcServerEntry from, + SilcClientEntry client, + bool local, + SilcIDCacheEntry client_cache) { SilcServerEntry server_entry; SilcIDCacheEntry id_cache = NULL; @@ -342,9 +345,55 @@ silc_server_update_clients_by_real_server(SilcServer server, if (silc_idcache_list_first(list, &id_cache)) { while (id_cache) { server_entry = (SilcServerEntry)id_cache->context; - if (SILC_ID_COMPARE(server_entry->id, client->id, + if (server_entry != from && + SILC_ID_COMPARE(server_entry->id, client->id, + client->id->ip.data_len)) { + SILC_LOG_DEBUG(("Found (local) %s", + silc_id_render(server_entry->id, SILC_ID_SERVER))); + + /* If the client is not marked as local then move it to local list + since the server is local. */ + if (server_entry->server_type != SILC_BACKUP_ROUTER && !local) { + SILC_LOG_DEBUG(("Moving client to local list")); + silc_idcache_add(server->local_list->clients, client_cache->name, + client_cache->id, client_cache->context, + client_cache->expire); + silc_idcache_del_by_context(server->global_list->clients, client); + } + + silc_idcache_list_free(list); + return server_entry; + } + + if (!silc_idcache_list_next(list, &id_cache)) + break; + } + } + + silc_idcache_list_free(list); + + if (!silc_idcache_get_all(server->global_list->servers, &list)) + return NULL; + + if (silc_idcache_list_first(list, &id_cache)) { + while (id_cache) { + server_entry = (SilcServerEntry)id_cache->context; + if (server_entry != from && + SILC_ID_COMPARE(server_entry->id, client->id, client->id->ip.data_len)) { - SILC_LOG_DEBUG(("Found")); + SILC_LOG_DEBUG(("Found (global) %s", + silc_id_render(server_entry->id, SILC_ID_SERVER))); + + /* If the client is marked as local then move it to global list + since the server is global. */ + if (server_entry->server_type != SILC_BACKUP_ROUTER && local) { + SILC_LOG_DEBUG(("Moving client to global list")); + silc_idcache_add(server->global_list->clients, client_cache->name, + client_cache->id, client_cache->context, + client_cache->expire); + silc_idcache_del_by_context(server->local_list->clients, client); + } + silc_idcache_list_free(list); return server_entry; } @@ -378,6 +427,7 @@ void silc_server_update_clients_by_server(SilcServer server, SilcIDCacheList list = NULL; SilcIDCacheEntry id_cache = NULL; SilcClientEntry client = NULL; + bool local; SILC_LOG_DEBUG(("Start")); @@ -386,10 +436,13 @@ void silc_server_update_clients_by_server(SilcServer server, SILC_LOG_DEBUG(("to %s", silc_id_render(to->id, SILC_ID_SERVER))); + + local = TRUE; if (silc_idcache_get_all(server->local_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 (!silc_idcache_list_next(list, &id_cache)) break; @@ -397,6 +450,9 @@ void silc_server_update_clients_by_server(SilcServer server, continue; } + SILC_LOG_DEBUG(("Client (local) %s", + silc_id_render(client->id, SILC_ID_CLIENT))); + if (client->router == from) { /* Skip clients that are *really* owned by the `from' */ if (remove_from && SILC_ID_COMPARE(from->id, client->id, @@ -410,7 +466,8 @@ void silc_server_update_clients_by_server(SilcServer server, if (resolve_real_server) { client->router = - silc_server_update_clients_by_real_server(server, client); + silc_server_update_clients_by_real_server(server, from, client, + local, id_cache); if (!client->router) client->router = to; } else { @@ -425,10 +482,13 @@ void silc_server_update_clients_by_server(SilcServer server, silc_idcache_list_free(list); } + 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 (!silc_idcache_list_next(list, &id_cache)) break; @@ -436,6 +496,9 @@ void silc_server_update_clients_by_server(SilcServer server, continue; } + SILC_LOG_DEBUG(("Client (global) %s", + silc_id_render(client->id, SILC_ID_CLIENT))); + if (client->router == from) { /* Skip clients that are *really* owned by the `from' */ if (remove_from && SILC_ID_COMPARE(from->id, client->id, @@ -449,7 +512,8 @@ void silc_server_update_clients_by_server(SilcServer server, if (resolve_real_server) { client->router = - silc_server_update_clients_by_real_server(server, client); + silc_server_update_clients_by_real_server(server, from, client, + local, id_cache); if (!client->router) client->router = to; } else { diff --git a/doc/examples/cell1_server1.conf b/doc/examples/cell1_server1.conf index e91bc547..7f0019ba 100644 --- a/doc/examples/cell1_server1.conf +++ b/doc/examples/cell1_server1.conf @@ -34,10 +34,10 @@ server1.cell1.com:212.146.42.101:Kuopio, Finland:706 212.146.42.101:212.146.42.101:706 [Logging] -#infologfile:silcd2.log:10000 -#warninglogfile:/var/log/silcd_warning.log:10000 -#errorlogfile:silcd2.log:10000 -#fatallogfile:/var/log/silcd_error.log: +infologfile:cell1_server1.log: +warninglogfile:cell1_server1.log: +errorlogfile:cell1_server1.log: +fatallogfile:cell1_server1.log: [ConnectionClass] 1:100:100:100 @@ -53,8 +53,8 @@ server1.cell1.com:212.146.42.101:Kuopio, Finland:706 [RouterConnection] # my primary -212.146.42.250:passwd:priikone:706:1:1:1:0 +212.146.42.250:passwd:priikone:706:1:1:1 # our backup router in the cell -212.146.42.100:passwd:priikone:706:1:1:1:1 +212.146.42.100:passwd:priikone:706:1:1:1:212.146.42.250:706:1 [DenyConnection] diff --git a/doc/examples/cell1_server2.conf b/doc/examples/cell1_server2.conf index d5ad67d4..6037c841 100644 --- a/doc/examples/cell1_server2.conf +++ b/doc/examples/cell1_server2.conf @@ -34,10 +34,10 @@ server2.cell1.com:212.146.42.102:Kuopio, Finland:706 212.146.42.102:212.146.42.102:706 [Logging] -#infologfile:silcd2.log:10000 -#warninglogfile:/var/log/silcd_warning.log:10000 -#errorlogfile:silcd2.log:10000 -#fatallogfile:/var/log/silcd_error.log: +infologfile:cell1_server2.log: +warninglogfile:cell1_server2.log: +errorlogfile:cell1_server2.log: +fatallogfile:cell1_server2.log: [ConnectionClass] 1:100:100:100 @@ -53,8 +53,8 @@ server2.cell1.com:212.146.42.102:Kuopio, Finland:706 [RouterConnection] # my primary -212.146.42.250:passwd:priikone:706:1:1:1:0 +212.146.42.250:passwd:priikone:706:1:1:1 # our backup router in the cell -212.146.42.100:passwd:priikone:706:1:1:1:1 +212.146.42.100:passwd:priikone:706:1:1:1:212.146.42.250:706:1 [DenyConnection] diff --git a/doc/examples/cell2_server1.conf b/doc/examples/cell2_server1.conf index f0a5af3d..76726f0f 100644 --- a/doc/examples/cell2_server1.conf +++ b/doc/examples/cell2_server1.conf @@ -34,10 +34,10 @@ server1.cell2.com:212.146.42.130:Kuopio, Finland:706 212.146.42.130:212.146.42.130:706 [Logging] -#infologfile:silcd2.log:10000 -#warninglogfile:/var/log/silcd_warning.log:10000 -#errorlogfile:silcd2.log:10000 -#fatallogfile:/var/log/silcd_error.log: +infologfile:cell2_server1.log: +warninglogfile:cell2_server1.log: +errorlogfile:cell2_server1.log: +fatallogfile:cell2_server1.log: [ConnectionClass] 1:100:100:100 @@ -53,6 +53,6 @@ server1.cell2.com:212.146.42.130:Kuopio, Finland:706 [RouterConnection] # my primary -212.146.42.251:passwd:priikone:706:1:1:1:0 +212.146.42.251:passwd:priikone:706:1:1:1 [DenyConnection] diff --git a/doc/examples/cell2_server2.conf b/doc/examples/cell2_server2.conf index 59f7824e..9c013268 100644 --- a/doc/examples/cell2_server2.conf +++ b/doc/examples/cell2_server2.conf @@ -34,10 +34,10 @@ server2.cell2.com:212.146.42.131:Kuopio, Finland:706 212.146.42.131:212.146.42.131:706 [Logging] -#infologfile:silcd2.log:10000 -#warninglogfile:/var/log/silcd_warning.log:10000 -#errorlogfile:silcd2.log:10000 -#fatallogfile:/var/log/silcd_error.log: +infologfile:cell2_server2.log: +warninglogfile:cell2_server2.log: +errorlogfile:cell2_server2.log: +fatallogfile:cell2_server2.log: [ConnectionClass] 1:100:100:100 @@ -53,6 +53,6 @@ server2.cell2.com:212.146.42.131:Kuopio, Finland:706 [RouterConnection] # my primary -212.146.42.251:passwd:priikone:706:1:1:1:0 +212.146.42.251:passwd:priikone:706:1:1:1 [DenyConnection] diff --git a/doc/examples/cell3_server1.conf b/doc/examples/cell3_server1.conf index de10c60f..69eb1a52 100644 --- a/doc/examples/cell3_server1.conf +++ b/doc/examples/cell3_server1.conf @@ -34,10 +34,10 @@ server1.cell3.com:212.146.42.150:Kuopio, Finland:706 212.146.42.150:212.146.42.150:706 [Logging] -#infologfile:silcd2.log:10000 -#warninglogfile:/var/log/silcd_warning.log:10000 -#errorlogfile:silcd2.log:10000 -#fatallogfile:/var/log/silcd_error.log: +infologfile:cell3_server1.log: +warninglogfile:cell3_server1.log: +errorlogfile:cell3_server1.log: +fatallogfile:cell3_server1.log: [ConnectionClass] 1:100:100:100 @@ -53,6 +53,6 @@ server1.cell3.com:212.146.42.150:Kuopio, Finland:706 [RouterConnection] # my primary -212.146.42.252:passwd:priikone:706:1:1:1:0 +212.146.42.252:passwd:priikone:706:1:1:1 [DenyConnection] diff --git a/doc/examples/cell3_server2.conf b/doc/examples/cell3_server2.conf index 289334da..7f219286 100644 --- a/doc/examples/cell3_server2.conf +++ b/doc/examples/cell3_server2.conf @@ -34,10 +34,10 @@ server2.cell3.com:212.146.42.151:Kuopio, Finland:706 212.146.42.151:212.146.42.151:706 [Logging] -#infologfile:silcd2.log:10000 -#warninglogfile:/var/log/silcd_warning.log:10000 -#errorlogfile:silcd2.log:10000 -#fatallogfile:/var/log/silcd_error.log: +infologfile:cell3_server2.log: +warninglogfile:cell3_server2.log: +errorlogfile:cell3_server2.log: +fatallogfile:cell3_server2.log: [ConnectionClass] 1:100:100:100 @@ -53,6 +53,6 @@ server2.cell3.com:212.146.42.151:Kuopio, Finland:706 [RouterConnection] # my primary -212.146.42.252:passwd:priikone:706:1:1:1:0 +212.146.42.252:passwd:priikone:706:1:1:1 [DenyConnection] -- 2.24.0