5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to handle received packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 extern char *server_version;
30 /* Received notify packet. Server can receive notify packets from router.
31 Server then relays the notify messages to clients if needed. */
33 void silc_server_notify(SilcServer server,
34 SilcSocketConnection sock,
35 SilcPacketContext *packet)
37 SilcNotifyPayload payload;
39 SilcArgumentPayload args;
40 SilcChannelID *channel_id = NULL, *channel_id2;
41 SilcClientID *client_id, *client_id2;
42 SilcServerID *server_id;
43 SilcChannelEntry channel;
44 SilcClientEntry client;
45 SilcServerEntry server_entry;
46 SilcChannelClientEntry chl;
47 SilcIDCacheEntry cache;
48 SilcHashTableList htl;
53 SILC_LOG_DEBUG(("Start"));
55 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
56 packet->src_id_type != SILC_ID_SERVER)
62 /* If the packet is destined directly to a client then relay the packet
63 before processing it. */
64 if (packet->dst_id_type == SILC_ID_CLIENT) {
66 SilcSocketConnection dst_sock;
68 /* Get the route to the client */
69 dst_sock = silc_server_get_client_route(server, packet->dst_id,
70 packet->dst_id_len, NULL, &idata);
72 /* Relay the packet */
73 silc_server_relay_packet(server, dst_sock, idata->send_key,
74 idata->hmac_receive, idata->psn_send++,
78 /* Parse the Notify Payload */
79 payload = silc_notify_payload_parse(packet->buffer->data,
84 /* If we are router and this packet is not already broadcast packet
85 we will broadcast it. The sending socket really cannot be router or
86 the router is buggy. If this packet is coming from router then it must
87 have the broadcast flag set already and we won't do anything. */
88 if (!server->standalone && server->server_type == SILC_ROUTER &&
89 sock->type == SILC_SOCKET_TYPE_SERVER &&
90 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
91 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
92 if (packet->dst_id_type == SILC_ID_CHANNEL) {
93 /* Packet is destined to channel */
94 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
99 silc_server_packet_send_dest(server, server->router->connection,
101 packet->flags | SILC_PACKET_FLAG_BROADCAST,
102 channel_id, SILC_ID_CHANNEL,
103 packet->buffer->data, packet->buffer->len,
105 silc_server_backup_send_dest(server, (SilcServerEntry)sock->user_data,
106 packet->type, packet->flags,
107 channel_id, SILC_ID_CHANNEL,
108 packet->buffer->data, packet->buffer->len,
111 /* Packet is destined to client or server */
112 silc_server_packet_send(server, server->router->connection,
114 packet->flags | SILC_PACKET_FLAG_BROADCAST,
115 packet->buffer->data, packet->buffer->len,
117 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
118 packet->type, packet->flags,
119 packet->buffer->data, packet->buffer->len,
124 type = silc_notify_get_type(payload);
125 args = silc_notify_get_args(payload);
130 case SILC_NOTIFY_TYPE_JOIN:
132 * Distribute the notify to local clients on the channel
134 SILC_LOG_DEBUG(("JOIN notify"));
137 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
140 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
144 /* Get channel entry */
145 channel = silc_idlist_find_channel_by_id(server->global_list,
148 channel = silc_idlist_find_channel_by_id(server->local_list,
151 silc_free(channel_id);
155 silc_free(channel_id);
158 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
161 client_id = silc_id_payload_parse_id(tmp, tmp_len);
165 /* If the the client is not in local list we check global list (ie. the
166 channel will be global channel) and if it does not exist then create
167 entry for the client. */
168 client = silc_idlist_find_client_by_id(server->global_list,
169 client_id, server->server_type,
172 client = silc_idlist_find_client_by_id(server->local_list,
173 client_id, server->server_type,
176 /* If router did not find the client the it is bogus */
177 if (server->server_type != SILC_SERVER)
181 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
182 silc_id_dup(client_id, SILC_ID_CLIENT),
183 sock->user_data, NULL, 0);
185 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
186 silc_free(client_id);
190 client->data.status |= SILC_IDLIST_STATUS_REGISTERED;
194 /* Do not process the notify if the client is not registered */
195 if (!(client->data.status & SILC_IDLIST_STATUS_REGISTERED))
198 /* Do not add client to channel if it is there already */
199 if (silc_server_client_on_channel(client, channel)) {
200 SILC_LOG_DEBUG(("Client already on channel"));
204 /* Send to channel */
205 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
206 FALSE, packet->buffer->data,
207 packet->buffer->len, FALSE);
209 if (server->server_type != SILC_ROUTER &&
210 sock->type == SILC_SOCKET_TYPE_ROUTER)
211 /* The channel is global now */
212 channel->global_users = TRUE;
214 /* JOIN the global client to the channel (local clients (if router
215 created the channel) is joined in the pending JOIN command). */
216 chl = silc_calloc(1, sizeof(*chl));
217 chl->client = client;
218 chl->channel = channel;
220 /* If this is the first one on the channel then it is the founder of
222 if (!silc_hash_table_count(channel->user_list))
223 chl->mode = (SILC_CHANNEL_UMODE_CHANOP | SILC_CHANNEL_UMODE_CHANFO);
225 silc_hash_table_add(channel->user_list, client, chl);
226 silc_hash_table_add(client->channels, channel, chl);
227 silc_free(client_id);
231 case SILC_NOTIFY_TYPE_LEAVE:
233 * Distribute the notify to local clients on the channel
235 SILC_LOG_DEBUG(("LEAVE notify"));
238 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
239 packet->dst_id_type);
244 /* Get channel entry */
245 channel = silc_idlist_find_channel_by_id(server->global_list,
248 channel = silc_idlist_find_channel_by_id(server->local_list,
251 silc_free(channel_id);
257 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
259 silc_free(channel_id);
262 client_id = silc_id_payload_parse_id(tmp, tmp_len);
264 silc_free(channel_id);
268 /* Get client entry */
269 client = silc_idlist_find_client_by_id(server->global_list,
270 client_id, TRUE, NULL);
272 client = silc_idlist_find_client_by_id(server->local_list,
273 client_id, TRUE, NULL);
275 silc_free(client_id);
276 silc_free(channel_id);
280 silc_free(client_id);
282 /* Check if on channel */
283 if (!silc_server_client_on_channel(client, channel))
286 /* Send the leave notify to channel */
287 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
288 FALSE, packet->buffer->data,
289 packet->buffer->len, FALSE);
291 /* Remove the user from channel */
292 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
295 case SILC_NOTIFY_TYPE_SIGNOFF:
297 * Distribute the notify to local clients on the channel
299 SILC_LOG_DEBUG(("SIGNOFF notify"));
302 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
305 client_id = silc_id_payload_parse_id(tmp, tmp_len);
309 /* Get client entry */
310 client = silc_idlist_find_client_by_id(server->global_list,
311 client_id, TRUE, &cache);
313 client = silc_idlist_find_client_by_id(server->local_list,
314 client_id, TRUE, &cache);
316 silc_free(client_id);
320 silc_free(client_id);
322 /* Get signoff message */
323 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
327 /* Remove the client from all channels. */
328 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, FALSE);
330 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
331 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
332 server->stat.clients--;
333 if (server->server_type == SILC_ROUTER)
334 server->stat.cell_clients--;
337 case SILC_NOTIFY_TYPE_TOPIC_SET:
339 * Distribute the notify to local clients on the channel
342 SILC_LOG_DEBUG(("TOPIC SET notify"));
345 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
346 packet->dst_id_type);
351 /* Get channel entry */
352 channel = silc_idlist_find_channel_by_id(server->global_list,
355 channel = silc_idlist_find_channel_by_id(server->local_list,
358 silc_free(channel_id);
364 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
366 silc_free(channel_id);
370 silc_free(channel->topic);
371 channel->topic = strdup(tmp);
373 /* Send the same notify to the channel */
374 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
375 FALSE, packet->buffer->data,
376 packet->buffer->len, FALSE);
377 silc_free(channel_id);
380 case SILC_NOTIFY_TYPE_NICK_CHANGE:
383 * Distribute the notify to local clients on the channel
385 unsigned char *id, *id2;
387 SILC_LOG_DEBUG(("NICK CHANGE notify"));
389 /* Get old client ID */
390 id = silc_argument_get_arg_type(args, 1, &tmp_len);
393 client_id = silc_id_payload_parse_id(id, tmp_len);
397 /* Get new client ID */
398 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
401 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
405 SILC_LOG_DEBUG(("Old Client ID id(%s)",
406 silc_id_render(client_id, SILC_ID_CLIENT)));
407 SILC_LOG_DEBUG(("New Client ID id(%s)",
408 silc_id_render(client_id2, SILC_ID_CLIENT)));
410 /* Replace the Client ID */
411 client = silc_idlist_replace_client_id(server->global_list, client_id,
414 client = silc_idlist_replace_client_id(server->local_list, client_id,
418 /* The nickname is not valid anymore, set it NULL. This causes that
419 the nickname will be queried if someone wants to know it. */
420 if (client->nickname)
421 silc_free(client->nickname);
422 client->nickname = NULL;
424 /* Send the NICK_CHANGE notify type to local clients on the channels
425 this client is joined to. */
426 silc_server_send_notify_on_channels(server, NULL, client,
427 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
432 silc_free(client_id);
434 silc_free(client_id2);
438 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
440 * Distribute the notify to local clients on the channel
443 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
446 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
447 packet->dst_id_type);
452 /* Get channel entry */
453 channel = silc_idlist_find_channel_by_id(server->global_list,
456 channel = silc_idlist_find_channel_by_id(server->local_list,
459 silc_free(channel_id);
465 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
467 silc_free(channel_id);
471 SILC_GET32_MSB(mode, tmp);
473 /* Check if mode changed */
474 if (channel->mode == mode)
477 /* Send the same notify to the channel */
478 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
479 FALSE, packet->buffer->data,
480 packet->buffer->len, FALSE);
482 /* If the channel had private keys set and the mode was removed then
483 we must re-generate and re-distribute a new channel key */
484 if (channel->mode & SILC_CHANNEL_MODE_PRIVKEY &&
485 !(mode & SILC_CHANNEL_MODE_PRIVKEY)) {
486 /* Re-generate channel key */
487 if (!silc_server_create_channel_key(server, channel, 0))
490 /* Send the channel key. This sends it to our local clients and if
491 we are normal server to our router as well. */
492 silc_server_send_channel_key(server, NULL, channel,
493 server->server_type == SILC_ROUTER ?
494 FALSE : !server->standalone);
498 channel->mode = mode;
499 silc_free(channel_id);
502 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
504 unsigned char hash[32];
507 silc_hmac_free(channel->hmac);
508 if (!silc_hmac_alloc(tmp, NULL, &channel->hmac))
511 /* Set the HMAC key out of current channel key. The client must do
513 silc_hash_make(silc_hmac_get_hash(channel->hmac), channel->key,
514 channel->key_len / 8,
516 silc_hmac_set_key(channel->hmac, hash,
517 silc_hash_len(silc_hmac_get_hash(channel->hmac)));
518 memset(hash, 0, sizeof(hash));
521 /* Get the passphrase */
522 tmp = silc_argument_get_arg_type(args, 5, &tmp_len);
524 silc_free(channel->passphrase);
525 channel->passphrase = strdup(tmp);
530 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
533 * Distribute the notify to local clients on the channel
535 SilcChannelClientEntry chl2 = NULL;
536 bool notify_sent = FALSE;
538 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
541 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
542 packet->dst_id_type);
547 /* Get channel entry */
548 channel = silc_idlist_find_channel_by_id(server->global_list,
551 channel = silc_idlist_find_channel_by_id(server->local_list,
554 silc_free(channel_id);
560 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
562 silc_free(channel_id);
566 SILC_GET32_MSB(mode, tmp);
568 /* Get target client */
569 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
572 client_id = silc_id_payload_parse_id(tmp, tmp_len);
576 /* Get client entry */
577 client = silc_idlist_find_client_by_id(server->global_list,
578 client_id, TRUE, NULL);
580 client = silc_idlist_find_client_by_id(server->local_list,
581 client_id, TRUE, NULL);
583 silc_free(client_id);
587 silc_free(client_id);
589 /* Get entry to the channel user list */
590 silc_hash_table_list(channel->user_list, &htl);
591 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
592 /* If the mode is channel founder and we already find a client
593 to have that mode on the channel we will enforce the sender
594 to change the channel founder mode away. There can be only one
595 channel founder on the channel. */
596 if (server->server_type == SILC_ROUTER &&
597 mode & SILC_CHANNEL_UMODE_CHANFO &&
598 chl->mode & SILC_CHANNEL_UMODE_CHANFO) {
600 unsigned char cumode[4];
602 if (chl->client == client && chl->mode == mode) {
607 mode &= ~SILC_CHANNEL_UMODE_CHANFO;
608 silc_server_send_notify_cumode(server, sock, FALSE, channel, mode,
609 client->id, SILC_ID_CLIENT,
612 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
613 SILC_PUT32_MSB(mode, cumode);
614 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
615 SILC_NOTIFY_TYPE_CUMODE_CHANGE,
616 3, idp->data, idp->len,
618 idp->data, idp->len);
619 silc_buffer_free(idp);
622 /* Force the mode change if we alredy set the mode */
625 silc_free(channel_id);
630 if (chl->client == client) {
631 if (chl->mode == mode) {
636 /* Change the mode */
638 if (!(mode & SILC_CHANNEL_UMODE_CHANFO))
645 /* Send the same notify to the channel */
647 silc_server_packet_send_to_channel(server, sock, channel,
649 FALSE, packet->buffer->data,
650 packet->buffer->len, FALSE);
652 silc_free(channel_id);
656 case SILC_NOTIFY_TYPE_INVITE:
658 if (packet->dst_id_type == SILC_ID_CLIENT)
661 SILC_LOG_DEBUG(("INVITE notify"));
664 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
667 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
671 /* Get channel entry */
672 channel = silc_idlist_find_channel_by_id(server->global_list,
675 channel = silc_idlist_find_channel_by_id(server->local_list,
678 silc_free(channel_id);
682 silc_free(channel_id);
684 /* Get the added invite */
685 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
687 if (!channel->invite_list)
688 channel->invite_list = silc_calloc(tmp_len + 2,
689 sizeof(*channel->invite_list));
691 channel->invite_list = silc_realloc(channel->invite_list,
692 sizeof(*channel->invite_list) *
694 strlen(channel->invite_list) +
696 if (tmp[tmp_len - 1] == ',')
697 tmp[tmp_len - 1] = '\0';
699 strncat(channel->invite_list, tmp, tmp_len);
700 strncat(channel->invite_list, ",", 1);
703 /* Get the deleted invite */
704 tmp = silc_argument_get_arg_type(args, 4, &tmp_len);
705 if (tmp && channel->invite_list) {
706 char *start, *end, *n;
708 if (!strncmp(channel->invite_list, tmp,
709 strlen(channel->invite_list) - 1)) {
710 silc_free(channel->invite_list);
711 channel->invite_list = NULL;
713 start = strstr(channel->invite_list, tmp);
714 if (start && strlen(start) >= tmp_len) {
715 end = start + tmp_len;
716 n = silc_calloc(strlen(channel->invite_list) - tmp_len, sizeof(*n));
717 strncat(n, channel->invite_list, start - channel->invite_list);
718 strncat(n, end + 1, ((channel->invite_list +
719 strlen(channel->invite_list)) - end) - 1);
720 silc_free(channel->invite_list);
721 channel->invite_list = n;
728 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
730 * Distribute to the local clients on the channel and change the
734 SILC_LOG_DEBUG(("CHANNEL CHANGE"));
736 if (sock->type != SILC_SOCKET_TYPE_ROUTER)
739 /* Get the old Channel ID */
740 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
743 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
747 /* Get the channel entry */
748 channel = silc_idlist_find_channel_by_id(server->global_list,
751 channel = silc_idlist_find_channel_by_id(server->local_list,
754 silc_free(channel_id);
759 /* Send the notify to the channel */
760 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
761 FALSE, packet->buffer->data,
762 packet->buffer->len, FALSE);
764 /* Get the new Channel ID */
765 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
768 channel_id2 = silc_id_payload_parse_id(tmp, tmp_len);
772 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
773 silc_id_render(channel_id, SILC_ID_CHANNEL)));
774 SILC_LOG_DEBUG(("New Channel ID id(%s)",
775 silc_id_render(channel_id2, SILC_ID_CHANNEL)));
777 /* Replace the Channel ID */
778 if (!silc_idlist_replace_channel_id(server->global_list, channel_id,
780 if (!silc_idlist_replace_channel_id(server->local_list, channel_id,
782 silc_free(channel_id2);
787 SilcBuffer users = NULL, users_modes = NULL;
789 /* Re-announce our clients on the channel as the ID has changed now */
790 silc_server_announce_get_channel_users(server, channel, &users,
793 silc_buffer_push(users, users->data - users->head);
794 silc_server_packet_send(server, sock,
795 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
796 users->data, users->len, FALSE);
797 silc_buffer_free(users);
800 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
801 silc_server_packet_send_dest(server, sock,
802 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
803 channel->id, SILC_ID_CHANNEL,
805 users_modes->len, FALSE);
806 silc_buffer_free(users_modes);
809 /* Re-announce channel's topic */
810 if (channel->topic) {
811 silc_server_send_notify_topic_set(server, sock,
812 server->server_type == SILC_ROUTER ?
813 TRUE : FALSE, channel,
814 channel->id, SILC_ID_CHANNEL,
819 silc_free(channel_id);
823 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
825 * Remove the server entry and all clients that this server owns.
828 SILC_LOG_DEBUG(("SERVER SIGNOFF notify"));
831 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
834 server_id = silc_id_payload_parse_id(tmp, tmp_len);
838 /* Get server entry */
839 server_entry = silc_idlist_find_server_by_id(server->global_list,
840 server_id, TRUE, NULL);
842 server_entry = silc_idlist_find_server_by_id(server->local_list,
843 server_id, TRUE, NULL);
845 /* If we are normal server then we might not have the server. Check
846 whether router was kind enough to send the list of all clients
847 that actually was to be removed. Remove them if the list is
849 if (server->server_type != SILC_ROUTER &&
850 silc_argument_get_arg_num(args) > 1) {
853 for (i = 1; i < silc_argument_get_arg_num(args); i++) {
855 tmp = silc_argument_get_arg_type(args, i + 1, &tmp_len);
858 client_id = silc_id_payload_parse_id(tmp, tmp_len);
862 /* Get client entry */
863 client = silc_idlist_find_client_by_id(server->global_list,
864 client_id, TRUE, &cache);
866 client = silc_idlist_find_client_by_id(server->local_list,
867 client_id, TRUE, &cache);
869 silc_free(client_id);
873 silc_free(client_id);
875 /* Remove the client from all channels. */
876 silc_server_remove_from_channels(server, NULL, client,
879 client->data.status &= ~SILC_IDLIST_STATUS_REGISTERED;
880 cache->expire = SILC_ID_CACHE_EXPIRE_DEF;
881 server->stat.clients--;
882 if (server->server_type == SILC_ROUTER)
883 server->stat.cell_clients--;
887 silc_free(server_id);
891 silc_free(server_id);
893 /* Free all client entries that this server owns as they will
894 become invalid now as well. */
895 silc_server_remove_clients_by_server(server, server_entry, TRUE);
897 /* Remove the server entry */
898 if (!silc_idlist_del_server(server->global_list, server_entry))
899 silc_idlist_del_server(server->local_list, server_entry);
901 /* XXX update statistics */
905 case SILC_NOTIFY_TYPE_KICKED:
907 * Distribute the notify to local clients on the channel
910 SILC_LOG_DEBUG(("KICKED notify"));
913 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
914 packet->dst_id_type);
919 /* Get channel entry */
920 channel = silc_idlist_find_channel_by_id(server->global_list,
923 channel = silc_idlist_find_channel_by_id(server->local_list,
926 silc_free(channel_id);
930 silc_free(channel_id);
933 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
936 client_id = silc_id_payload_parse_id(tmp, tmp_len);
940 /* If the the client is not in local list we check global list */
941 client = silc_idlist_find_client_by_id(server->global_list,
942 client_id, TRUE, NULL);
944 client = silc_idlist_find_client_by_id(server->local_list,
945 client_id, TRUE, NULL);
947 silc_free(client_id);
952 /* Send to channel */
953 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
954 FALSE, packet->buffer->data,
955 packet->buffer->len, FALSE);
957 /* Remove the client from channel */
958 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
962 case SILC_NOTIFY_TYPE_KILLED:
965 * Distribute the notify to local clients on channels
970 SILC_LOG_DEBUG(("KILLED notify"));
973 id = silc_argument_get_arg_type(args, 1, &id_len);
976 client_id = silc_id_payload_parse_id(id, id_len);
980 /* If the the client is not in local list we check global list */
981 client = silc_idlist_find_client_by_id(server->global_list,
982 client_id, TRUE, NULL);
984 client = silc_idlist_find_client_by_id(server->local_list,
985 client_id, TRUE, NULL);
987 silc_free(client_id);
991 silc_free(client_id);
993 /* If the client is one of ours, then close the connection to the
994 client now. This removes the client from all channels as well. */
995 if (packet->dst_id_type == SILC_ID_CLIENT && client->connection) {
996 sock = client->connection;
997 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
998 silc_server_close_connection(server, sock);
1003 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1007 /* Send the notify to local clients on the channels except to the
1008 client who is killed. */
1009 silc_server_send_notify_on_channels(server, client, client,
1010 SILC_NOTIFY_TYPE_KILLED,
1015 /* Remove the client from all channels */
1016 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
1022 case SILC_NOTIFY_TYPE_UMODE_CHANGE:
1024 * Save the mode of the client.
1027 SILC_LOG_DEBUG(("UMODE_CHANGE notify"));
1030 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1033 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1037 /* Get client entry */
1038 client = silc_idlist_find_client_by_id(server->global_list,
1039 client_id, TRUE, NULL);
1041 client = silc_idlist_find_client_by_id(server->local_list,
1042 client_id, TRUE, NULL);
1044 silc_free(client_id);
1048 silc_free(client_id);
1051 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1056 SILC_GET32_MSB(client->mode, tmp);
1060 case SILC_NOTIFY_TYPE_BAN:
1065 SILC_LOG_DEBUG(("BAN notify"));
1067 /* Get Channel ID */
1068 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1071 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1075 /* Get channel entry */
1076 channel = silc_idlist_find_channel_by_id(server->global_list,
1079 channel = silc_idlist_find_channel_by_id(server->local_list,
1082 silc_free(channel_id);
1086 silc_free(channel_id);
1088 /* Get the new ban and add it to the ban list */
1089 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1091 if (!channel->ban_list)
1092 channel->ban_list = silc_calloc(tmp_len + 2,
1093 sizeof(*channel->ban_list));
1095 channel->ban_list = silc_realloc(channel->ban_list,
1096 sizeof(*channel->ban_list) *
1098 strlen(channel->ban_list) + 2));
1099 strncat(channel->ban_list, tmp, tmp_len);
1100 strncat(channel->ban_list, ",", 1);
1103 /* Get the ban to be removed and remove it from the list */
1104 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
1105 if (tmp && channel->ban_list) {
1106 char *start, *end, *n;
1108 if (!strcmp(channel->ban_list, tmp)) {
1109 silc_free(channel->ban_list);
1110 channel->ban_list = NULL;
1112 start = strstr(channel->ban_list, tmp);
1113 if (start && strlen(start) >= tmp_len) {
1114 end = start + tmp_len;
1115 n = silc_calloc(strlen(channel->ban_list) - tmp_len, sizeof(*n));
1116 strncat(n, channel->ban_list, start - channel->ban_list);
1117 strncat(n, end + 1, ((channel->ban_list +
1118 strlen(channel->ban_list)) - end) - 1);
1119 silc_free(channel->ban_list);
1120 channel->ban_list = n;
1127 /* Ignore rest of the notify types for now */
1128 case SILC_NOTIFY_TYPE_NONE:
1129 case SILC_NOTIFY_TYPE_MOTD:
1136 silc_notify_payload_free(payload);
1139 void silc_server_notify_list(SilcServer server,
1140 SilcSocketConnection sock,
1141 SilcPacketContext *packet)
1143 SilcPacketContext *new;
1147 SILC_LOG_DEBUG(("Processing Notify List"));
1149 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1150 packet->src_id_type != SILC_ID_SERVER)
1153 /* Make copy of the original packet context, except for the actual
1154 data buffer, which we will here now fetch from the original buffer. */
1155 new = silc_packet_context_alloc();
1156 new->type = SILC_PACKET_NOTIFY;
1157 new->flags = packet->flags;
1158 new->src_id = packet->src_id;
1159 new->src_id_len = packet->src_id_len;
1160 new->src_id_type = packet->src_id_type;
1161 new->dst_id = packet->dst_id;
1162 new->dst_id_len = packet->dst_id_len;
1163 new->dst_id_type = packet->dst_id_type;
1165 buffer = silc_buffer_alloc(1024);
1166 new->buffer = buffer;
1168 while (packet->buffer->len) {
1169 SILC_GET16_MSB(len, packet->buffer->data + 2);
1170 if (len > packet->buffer->len)
1173 if (len > buffer->truelen) {
1174 silc_buffer_free(buffer);
1175 buffer = silc_buffer_alloc(1024 + len);
1178 silc_buffer_pull_tail(buffer, len);
1179 silc_buffer_put(buffer, packet->buffer->data, len);
1181 /* Process the Notify */
1182 silc_server_notify(server, sock, new);
1184 silc_buffer_push_tail(buffer, len);
1185 silc_buffer_pull(packet->buffer, len);
1188 silc_buffer_free(buffer);
1192 /* Received private message. This resolves the destination of the message
1193 and sends the packet. This is used by both server and router. If the
1194 destination is our locally connected client this sends the packet to
1195 the client. This may also send the message for further routing if
1196 the destination is not in our server (or router). */
1198 void silc_server_private_message(SilcServer server,
1199 SilcSocketConnection sock,
1200 SilcPacketContext *packet)
1202 SilcSocketConnection dst_sock;
1203 SilcIDListData idata;
1205 SILC_LOG_DEBUG(("Start"));
1207 if (packet->src_id_type != SILC_ID_CLIENT ||
1208 packet->dst_id_type != SILC_ID_CLIENT || !packet->dst_id)
1211 /* Get the route to the client */
1212 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1213 packet->dst_id_len, NULL, &idata);
1215 /* Send IDENTIFY command reply with error status to indicate that
1216 such destination ID does not exist or is invalid */
1217 SilcBuffer idp = silc_id_payload_encode_data(packet->dst_id,
1219 packet->dst_id_type);
1223 if (packet->src_id_type == SILC_ID_CLIENT) {
1224 SilcClientID *client_id = silc_id_str2id(packet->src_id,
1226 packet->src_id_type);
1227 silc_server_send_dest_command_reply(server, sock,
1228 client_id, SILC_ID_CLIENT,
1229 SILC_COMMAND_IDENTIFY,
1230 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1231 0, 1, 2, idp->data, idp->len);
1232 silc_free(client_id);
1234 silc_server_send_command_reply(server, sock, SILC_COMMAND_IDENTIFY,
1235 SILC_STATUS_ERR_NO_SUCH_CLIENT_ID,
1236 0, 1, 2, idp->data, idp->len);
1239 silc_buffer_free(idp);
1243 /* Send the private message */
1244 silc_server_send_private_message(server, dst_sock, idata->send_key,
1245 idata->hmac_send, idata->psn_send++,
1249 /* Received private message key packet.. This packet is never for us. It is to
1250 the client in the packet's destination ID. Sending of this sort of packet
1251 equals sending private message, ie. it is sent point to point from
1252 one client to another. */
1254 void silc_server_private_message_key(SilcServer server,
1255 SilcSocketConnection sock,
1256 SilcPacketContext *packet)
1258 SilcSocketConnection dst_sock;
1259 SilcIDListData idata;
1261 SILC_LOG_DEBUG(("Start"));
1263 if (packet->src_id_type != SILC_ID_CLIENT ||
1264 packet->dst_id_type != SILC_ID_CLIENT)
1267 if (!packet->dst_id)
1270 /* Get the route to the client */
1271 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1272 packet->dst_id_len, NULL, &idata);
1276 /* Relay the packet */
1277 silc_server_relay_packet(server, dst_sock, idata->send_key,
1278 idata->hmac_send, idata->psn_send++, packet, FALSE);
1281 /* Processes incoming command reply packet. The command reply packet may
1282 be destined to one of our clients or it may directly for us. We will
1283 call the command reply routine after processing the packet. */
1285 void silc_server_command_reply(SilcServer server,
1286 SilcSocketConnection sock,
1287 SilcPacketContext *packet)
1289 SilcBuffer buffer = packet->buffer;
1290 SilcClientEntry client = NULL;
1291 SilcSocketConnection dst_sock;
1292 SilcIDListData idata;
1293 SilcClientID *id = NULL;
1295 SILC_LOG_DEBUG(("Start"));
1297 /* Source must be server or router */
1298 if (packet->src_id_type != SILC_ID_SERVER &&
1299 sock->type != SILC_SOCKET_TYPE_ROUTER)
1302 if (packet->dst_id_type == SILC_ID_CHANNEL)
1305 if (packet->dst_id_type == SILC_ID_CLIENT) {
1306 /* Destination must be one of ours */
1307 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1310 client = silc_idlist_find_client_by_id(server->local_list, id, TRUE, NULL);
1312 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
1318 if (packet->dst_id_type == SILC_ID_SERVER) {
1319 /* For now this must be for us */
1320 if (memcmp(packet->dst_id, server->id_string, server->id_string_len)) {
1321 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
1326 /* Execute command reply locally for the command */
1327 silc_server_command_reply_process(server, sock, buffer);
1329 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
1330 /* Relay the packet to the client */
1332 dst_sock = (SilcSocketConnection)client->connection;
1333 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1334 + packet->dst_id_len + packet->padlen);
1336 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1337 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1339 idata = (SilcIDListData)client;
1341 /* Encrypt packet */
1342 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
1343 dst_sock->outbuf, buffer->len);
1345 /* Send the packet */
1346 silc_server_packet_send_real(server, dst_sock, TRUE);
1352 /* Process received channel message. The message can be originated from
1353 client or server. */
1355 void silc_server_channel_message(SilcServer server,
1356 SilcSocketConnection sock,
1357 SilcPacketContext *packet)
1359 SilcChannelEntry channel = NULL;
1360 SilcChannelID *id = NULL;
1361 void *sender = NULL;
1362 void *sender_entry = NULL;
1365 SILC_LOG_DEBUG(("Processing channel message"));
1368 if (packet->dst_id_type != SILC_ID_CHANNEL) {
1369 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
1373 /* Find channel entry */
1374 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
1377 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
1379 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
1381 SILC_LOG_DEBUG(("Could not find channel"));
1386 /* See that this client is on the channel. If the original sender is
1387 not client (as it can be server as well) we don't do the check. */
1388 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
1389 packet->src_id_type);
1392 if (packet->src_id_type == SILC_ID_CLIENT) {
1393 sender_entry = silc_idlist_find_client_by_id(server->local_list,
1394 sender, TRUE, NULL);
1395 if (!sender_entry) {
1397 sender_entry = silc_idlist_find_client_by_id(server->global_list,
1398 sender, TRUE, NULL);
1400 if (!sender_entry || !silc_server_client_on_channel(sender_entry,
1402 SILC_LOG_DEBUG(("Client not on channel"));
1406 /* If the packet is coming from router, but the client entry is
1407 local entry to us then some router is rerouting this to us and it is
1409 if (server->server_type == SILC_ROUTER &&
1410 sock->type == SILC_SOCKET_TYPE_ROUTER && local) {
1411 SILC_LOG_DEBUG(("Channel message rerouted to the sender, drop it"));
1416 /* Distribute the packet to our local clients. This will send the
1417 packet for further routing as well, if needed. */
1418 silc_server_packet_relay_to_channel(server, sock, channel, sender,
1419 packet->src_id_type, sender_entry,
1420 packet->buffer->data,
1421 packet->buffer->len, FALSE);
1430 /* Received channel key packet. We distribute the key to all of our locally
1431 connected clients on the channel. */
1433 void silc_server_channel_key(SilcServer server,
1434 SilcSocketConnection sock,
1435 SilcPacketContext *packet)
1437 SilcBuffer buffer = packet->buffer;
1438 SilcChannelEntry channel;
1440 if (packet->src_id_type != SILC_ID_SERVER ||
1441 (server->server_type == SILC_ROUTER &&
1442 sock->type == SILC_SOCKET_TYPE_ROUTER))
1445 /* Save the channel key */
1446 channel = silc_server_save_channel_key(server, buffer, NULL);
1450 /* Distribute the key to everybody who is on the channel. If we are router
1451 we will also send it to locally connected servers. */
1452 silc_server_send_channel_key(server, sock, channel, FALSE);
1454 if (server->server_type != SILC_BACKUP_ROUTER) {
1455 /* Distribute to local cell backup routers. */
1456 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
1457 SILC_PACKET_CHANNEL_KEY, 0,
1458 buffer->data, buffer->len, FALSE, TRUE);
1462 /* Received New Client packet and processes it. Creates Client ID for the
1463 client. Client becomes registered after calling this functions. */
1465 SilcClientEntry silc_server_new_client(SilcServer server,
1466 SilcSocketConnection sock,
1467 SilcPacketContext *packet)
1469 SilcBuffer buffer = packet->buffer;
1470 SilcClientEntry client;
1471 SilcClientID *client_id;
1473 SilcIDListData idata;
1474 char *username = NULL, *realname = NULL, *id_string;
1475 uint16 username_len;
1478 char *hostname, *nickname;
1481 SILC_LOG_DEBUG(("Creating new client"));
1483 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1486 /* Take client entry */
1487 client = (SilcClientEntry)sock->user_data;
1488 idata = (SilcIDListData)client;
1490 /* Remove the old cache entry. */
1491 if (!silc_idcache_del_by_context(server->local_list->clients, client)) {
1492 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1493 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1498 /* Parse incoming packet */
1499 ret = silc_buffer_unformat(buffer,
1500 SILC_STR_UI16_NSTRING_ALLOC(&username,
1502 SILC_STR_UI16_STRING_ALLOC(&realname),
1505 silc_free(username);
1506 silc_free(realname);
1507 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1508 "Incomplete client information");
1513 silc_free(username);
1514 silc_free(realname);
1515 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1516 "Incomplete client information");
1520 if (username_len > 128)
1521 username[128] = '\0';
1523 /* Check for bad characters for nickname, and modify the nickname if
1524 it includes those. */
1525 if (silc_server_name_bad_chars(username, username_len)) {
1526 nickname = silc_server_name_modify_bad(username, username_len);
1528 nickname = strdup(username);
1531 /* Make sanity checks for the hostname of the client. If the hostname
1532 is provided in the `username' check that it is the same than the
1533 resolved hostname, or if not resolved the hostname that appears in
1534 the client's public key. If the hostname is not present then put
1535 it from the resolved name or from the public key. */
1536 if (strchr(username, '@')) {
1537 SilcPublicKeyIdentifier pident;
1538 int tlen = strcspn(username, "@");
1539 char *phostname = NULL;
1541 hostname = silc_calloc((strlen(username) - tlen) + 1, sizeof(char));
1542 memcpy(hostname, username + tlen + 1, strlen(username) - tlen - 1);
1544 if (strcmp(sock->hostname, sock->ip) &&
1545 strcmp(sock->hostname, hostname)) {
1546 silc_free(username);
1547 silc_free(hostname);
1548 silc_free(realname);
1549 silc_server_disconnect_remote(server, sock,
1550 "Server closed connection: "
1551 "Incomplete client information");
1555 pident = silc_pkcs_decode_identifier(client->data.public_key->identifier);
1557 phostname = strdup(pident->host);
1558 silc_pkcs_free_identifier(pident);
1561 if (!strcmp(sock->hostname, sock->ip) &&
1562 phostname && strcmp(phostname, hostname)) {
1563 silc_free(username);
1564 silc_free(hostname);
1565 silc_free(phostname);
1566 silc_free(realname);
1567 silc_server_disconnect_remote(server, sock,
1568 "Server closed connection: "
1569 "Incomplete client information");
1573 silc_free(phostname);
1575 /* The hostname is not present, add it. */
1577 /* XXX For now we cannot take the host name from the public key since
1578 they are not trusted or we cannot verify them as trusted. Just take
1579 what the resolved name or address is. */
1581 if (strcmp(sock->hostname, sock->ip)) {
1583 newusername = silc_calloc(strlen(username) +
1584 strlen(sock->hostname) + 2,
1585 sizeof(*newusername));
1586 strncat(newusername, username, strlen(username));
1587 strncat(newusername, "@", 1);
1588 strncat(newusername, sock->hostname, strlen(sock->hostname));
1589 silc_free(username);
1590 username = newusername;
1593 SilcPublicKeyIdentifier pident =
1594 silc_pkcs_decode_identifier(client->data.public_key->identifier);
1597 newusername = silc_calloc(strlen(username) +
1598 strlen(pident->host) + 2,
1599 sizeof(*newusername));
1600 strncat(newusername, username, strlen(username));
1601 strncat(newusername, "@", 1);
1602 strncat(newusername, pident->host, strlen(pident->host));
1603 silc_free(username);
1604 username = newusername;
1605 silc_pkcs_free_identifier(pident);
1611 /* Create Client ID */
1612 while (!silc_id_create_client_id(server, server->id, server->rng,
1613 server->md5hash, nickname, &client_id)) {
1615 snprintf(&nickname[strlen(nickname) - 1], 1, "%d", nickfail);
1618 /* Update client entry */
1619 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1620 client->nickname = nickname;
1621 client->username = username;
1622 client->userinfo = realname ? realname : strdup(" ");
1623 client->id = client_id;
1624 id_len = silc_id_get_len(client_id, SILC_ID_CLIENT);
1626 /* Add the client again to the ID cache */
1627 silc_idcache_add(server->local_list->clients, client->nickname,
1628 client_id, client, 0, NULL);
1630 /* Notify our router about new client on the SILC network */
1631 if (!server->standalone)
1632 silc_server_send_new_id(server, (SilcSocketConnection)
1633 server->router->connection,
1634 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1635 client->id, SILC_ID_CLIENT, id_len);
1637 /* Send the new client ID to the client. */
1638 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1639 reply = silc_buffer_alloc(2 + 2 + id_len);
1640 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1641 silc_buffer_format(reply,
1642 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1643 SILC_STR_UI_SHORT(id_len),
1644 SILC_STR_UI_XNSTRING(id_string, id_len),
1646 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1647 reply->data, reply->len, FALSE);
1648 silc_free(id_string);
1649 silc_buffer_free(reply);
1651 /* Send some nice info to the client */
1652 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1653 ("Welcome to the SILC Network %s",
1655 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1656 ("Your host is %s, running version %s",
1657 server->config->server_info->server_name,
1659 if (server->server_type == SILC_ROUTER) {
1660 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1661 ("There are %d clients on %d servers in SILC "
1662 "Network", server->stat.clients,
1663 server->stat.servers + 1));
1664 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1665 ("There are %d clients on %d server in our cell",
1666 server->stat.cell_clients,
1667 server->stat.cell_servers + 1));
1668 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1669 ("I have %d clients, %d channels, %d servers and "
1671 server->stat.my_clients,
1672 server->stat.my_channels,
1673 server->stat.my_servers,
1674 server->stat.my_routers));
1675 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1676 ("%d server operators and %d router operators "
1678 server->stat.my_server_ops,
1679 server->stat.my_router_ops));
1681 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1682 ("I have %d clients and %d channels formed",
1683 server->stat.my_clients,
1684 server->stat.my_channels));
1685 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1686 ("%d operators online",
1687 server->stat.my_server_ops));
1689 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1690 ("Your connection is secured with %s cipher, "
1691 "key length %d bits",
1692 idata->send_key->cipher->name,
1693 idata->send_key->cipher->key_len));
1694 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1695 ("Your current nickname is %s",
1699 silc_server_send_motd(server, sock);
1704 /* Create new server. This processes received New Server packet and
1705 saves the received Server ID. The server is our locally connected
1706 server thus we save all the information and save it to local list.
1707 This funtion can be used by both normal server and router server.
1708 If normal server uses this it means that its router has connected
1709 to the server. If router uses this it means that one of the cell's
1710 servers is connected to the router. */
1712 SilcServerEntry silc_server_new_server(SilcServer server,
1713 SilcSocketConnection sock,
1714 SilcPacketContext *packet)
1716 SilcBuffer buffer = packet->buffer;
1717 SilcServerEntry new_server, server_entry;
1718 SilcServerID *server_id;
1719 SilcIDListData idata;
1720 unsigned char *server_name, *id_string;
1721 uint16 id_len, name_len;
1725 SILC_LOG_DEBUG(("Creating new server"));
1727 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1728 sock->type != SILC_SOCKET_TYPE_ROUTER)
1731 /* Take server entry */
1732 new_server = (SilcServerEntry)sock->user_data;
1733 idata = (SilcIDListData)new_server;
1735 /* Remove the old cache entry */
1736 if (!silc_idcache_del_by_context(server->local_list->servers, new_server)) {
1737 silc_idcache_del_by_context(server->global_list->servers, new_server);
1741 /* Parse the incoming packet */
1742 ret = silc_buffer_unformat(buffer,
1743 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1744 SILC_STR_UI16_NSTRING_ALLOC(&server_name,
1749 silc_free(id_string);
1751 silc_free(server_name);
1755 if (id_len > buffer->len) {
1756 silc_free(id_string);
1757 silc_free(server_name);
1762 server_name[255] = '\0';
1765 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1767 silc_free(id_string);
1768 silc_free(server_name);
1771 silc_free(id_string);
1773 /* Check that we do not have this ID already */
1774 server_entry = silc_idlist_find_server_by_id(server->local_list,
1775 server_id, TRUE, NULL);
1777 silc_idcache_del_by_context(server->local_list->servers, server_entry);
1779 server_entry = silc_idlist_find_server_by_id(server->global_list,
1780 server_id, TRUE, NULL);
1782 silc_idcache_del_by_context(server->global_list->servers, server_entry);
1785 /* Update server entry */
1786 idata->status |= SILC_IDLIST_STATUS_REGISTERED;
1787 new_server->server_name = server_name;
1788 new_server->id = server_id;
1790 SILC_LOG_DEBUG(("New server id(%s)",
1791 silc_id_render(server_id, SILC_ID_SERVER)));
1793 /* Add again the entry to the ID cache. */
1794 silc_idcache_add(local ? server->local_list->servers :
1795 server->global_list->servers, server_name, server_id,
1796 new_server, 0, NULL);
1798 /* Distribute the information about new server in the SILC network
1799 to our router. If we are normal server we won't send anything
1800 since this connection must be our router connection. */
1801 if (server->server_type == SILC_ROUTER && !server->standalone &&
1802 server->router->connection != sock)
1803 silc_server_send_new_id(server, server->router->connection,
1804 TRUE, new_server->id, SILC_ID_SERVER,
1805 silc_id_get_len(server_id, SILC_ID_SERVER));
1807 if (server->server_type == SILC_ROUTER)
1808 server->stat.cell_servers++;
1810 /* Check whether this router connection has been replaced by an
1811 backup router. If it has been then we'll disable the server and will
1812 ignore everything it will send until the backup router resuming
1813 protocol has been completed. */
1814 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1815 silc_server_backup_replaced_get(server, server_id, NULL)) {
1816 /* Send packet to the server indicating that it cannot use this
1817 connection as it has been replaced by backup router. */
1818 SilcBuffer packet = silc_buffer_alloc(2);
1819 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1820 silc_buffer_format(packet,
1821 SILC_STR_UI_CHAR(SILC_SERVER_BACKUP_REPLACED),
1822 SILC_STR_UI_CHAR(0),
1824 silc_server_packet_send(server, sock,
1825 SILC_PACKET_RESUME_ROUTER, 0,
1826 packet->data, packet->len, TRUE);
1827 silc_buffer_free(packet);
1829 /* Mark the router disabled. The data sent earlier will go but nothing
1830 after this does not go to this connection. */
1831 idata->status |= SILC_IDLIST_STATUS_DISABLED;
1833 /* If it is router announce our stuff to it. */
1834 if (sock->type == SILC_SOCKET_TYPE_ROUTER &&
1835 server->server_type == SILC_ROUTER) {
1836 silc_server_announce_servers(server, FALSE, 0, sock);
1837 silc_server_announce_clients(server, 0, sock);
1838 silc_server_announce_channels(server, 0, sock);
1845 /* Processes incoming New ID packet. New ID Payload is used to distribute
1846 information about newly registered clients and servers. */
1848 static void silc_server_new_id_real(SilcServer server,
1849 SilcSocketConnection sock,
1850 SilcPacketContext *packet,
1853 SilcBuffer buffer = packet->buffer;
1855 SilcServerEntry router, server_entry;
1856 SilcSocketConnection router_sock;
1861 SILC_LOG_DEBUG(("Processing new ID"));
1863 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1864 server->server_type == SILC_SERVER ||
1865 packet->src_id_type != SILC_ID_SERVER)
1868 idp = silc_id_payload_parse(buffer->data, buffer->len);
1872 id_type = silc_id_payload_get_type(idp);
1874 /* Normal server cannot have other normal server connections */
1875 server_entry = (SilcServerEntry)sock->user_data;
1876 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER &&
1877 server_entry->server_type == SILC_SERVER)
1880 id = silc_id_payload_get_id(idp);
1884 /* If the packet is coming from server then use the sender as the
1885 origin of the the packet. If it came from router then check the real
1886 sender of the packet and use that as the origin. */
1887 if (sock->type == SILC_SOCKET_TYPE_SERVER) {
1888 id_list = server->local_list;
1890 router = sock->user_data;
1892 /* If the sender is backup router and ID is server (and we are not
1893 backup router) then switch the entry to global list. */
1894 if (server_entry->server_type == SILC_BACKUP_ROUTER &&
1895 id_type == SILC_ID_SERVER &&
1896 server->id_entry->server_type != SILC_BACKUP_ROUTER) {
1897 id_list = server->global_list;
1898 router_sock = server->router ? server->router->connection : sock;
1901 void *sender_id = silc_id_str2id(packet->src_id, packet->src_id_len,
1902 packet->src_id_type);
1903 router = silc_idlist_find_server_by_id(server->global_list,
1904 sender_id, TRUE, NULL);
1906 router = silc_idlist_find_server_by_id(server->local_list,
1907 sender_id, TRUE, NULL);
1908 silc_free(sender_id);
1912 id_list = server->global_list;
1916 case SILC_ID_CLIENT:
1918 SilcClientEntry entry;
1920 /* Check that we do not have this client already */
1921 entry = silc_idlist_find_client_by_id(server->global_list,
1922 id, server->server_type,
1925 entry = silc_idlist_find_client_by_id(server->local_list,
1926 id, server->server_type,
1929 SILC_LOG_DEBUG(("Ignoring client that we already have"));
1933 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1934 silc_id_render(id, SILC_ID_CLIENT),
1935 sock->type == SILC_SOCKET_TYPE_SERVER ?
1936 "Server" : "Router", sock->hostname));
1938 /* As a router we keep information of all global information in our
1939 global list. Cell wide information however is kept in the local
1941 entry = silc_idlist_add_client(id_list, NULL, NULL, NULL,
1942 id, router, NULL, 0);
1944 SILC_LOG_ERROR(("Could not add new client to the ID Cache"));
1946 /* Inform the sender that the ID is not usable */
1947 silc_server_send_notify_signoff(server, sock, FALSE, id, NULL);
1950 entry->nickname = NULL;
1951 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
1953 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1954 server->stat.cell_clients++;
1955 server->stat.clients++;
1959 case SILC_ID_SERVER:
1961 SilcServerEntry entry;
1963 /* If the ID is mine, ignore it. */
1964 if (SILC_ID_SERVER_COMPARE(id, server->id)) {
1965 SILC_LOG_DEBUG(("Ignoring my own ID as new ID"));
1969 /* If the ID is the sender's ID, ignore it (we have it already) */
1970 if (SILC_ID_SERVER_COMPARE(id, router->id)) {
1971 SILC_LOG_DEBUG(("Ignoring sender's own ID"));
1975 /* Check that we do not have this server already */
1976 entry = silc_idlist_find_server_by_id(server->global_list,
1977 id, server->server_type,
1980 entry = silc_idlist_find_server_by_id(server->local_list,
1981 id, server->server_type,
1984 SILC_LOG_DEBUG(("Ignoring server that we already have"));
1988 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1989 silc_id_render(id, SILC_ID_SERVER),
1990 sock->type == SILC_SOCKET_TYPE_SERVER ?
1991 "Server" : "Router", sock->hostname));
1993 /* As a router we keep information of all global information in our
1994 global list. Cell wide information however is kept in the local
1996 entry = silc_idlist_add_server(id_list, NULL, 0, id, router,
1999 SILC_LOG_ERROR(("Could not add new server to the ID Cache"));
2002 entry->data.status |= SILC_IDLIST_STATUS_REGISTERED;
2004 if (sock->type == SILC_SOCKET_TYPE_SERVER)
2005 server->stat.cell_servers++;
2006 server->stat.servers++;
2010 case SILC_ID_CHANNEL:
2011 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
2020 /* If the sender of this packet is server and we are router we need to
2021 broadcast this packet to other routers in the network. */
2022 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
2023 sock->type == SILC_SOCKET_TYPE_SERVER &&
2024 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2025 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
2026 silc_server_packet_send(server, server->router->connection,
2028 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2029 buffer->data, buffer->len, FALSE);
2030 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2031 packet->type, packet->flags,
2032 packet->buffer->data, packet->buffer->len,
2037 silc_id_payload_free(idp);
2041 /* Processes incoming New ID packet. New ID Payload is used to distribute
2042 information about newly registered clients and servers. */
2044 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
2045 SilcPacketContext *packet)
2047 silc_server_new_id_real(server, sock, packet, TRUE);
2050 /* Receoved New Id List packet, list of New ID payloads inside one
2051 packet. Process the New ID payloads one by one. */
2053 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
2054 SilcPacketContext *packet)
2056 SilcPacketContext *new_id;
2060 SILC_LOG_DEBUG(("Processing New ID List"));
2062 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2063 packet->src_id_type != SILC_ID_SERVER)
2066 /* If the sender of this packet is server and we are router we need to
2067 broadcast this packet to other routers in the network. Broadcast
2068 this list packet instead of multiple New ID packets. */
2069 if (!server->standalone && server->server_type == SILC_ROUTER &&
2070 sock->type == SILC_SOCKET_TYPE_SERVER &&
2071 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2072 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
2073 silc_server_packet_send(server, server->router->connection,
2075 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2076 packet->buffer->data, packet->buffer->len, FALSE);
2077 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2078 packet->type, packet->flags,
2079 packet->buffer->data, packet->buffer->len,
2083 /* Make copy of the original packet context, except for the actual
2084 data buffer, which we will here now fetch from the original buffer. */
2085 new_id = silc_packet_context_alloc();
2086 new_id->type = SILC_PACKET_NEW_ID;
2087 new_id->flags = packet->flags;
2088 new_id->src_id = packet->src_id;
2089 new_id->src_id_len = packet->src_id_len;
2090 new_id->src_id_type = packet->src_id_type;
2091 new_id->dst_id = packet->dst_id;
2092 new_id->dst_id_len = packet->dst_id_len;
2093 new_id->dst_id_type = packet->dst_id_type;
2095 idp = silc_buffer_alloc(256);
2096 new_id->buffer = idp;
2098 while (packet->buffer->len) {
2099 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
2100 if ((id_len > packet->buffer->len) ||
2101 (id_len > idp->truelen))
2104 silc_buffer_pull_tail(idp, 4 + id_len);
2105 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
2107 /* Process the New ID */
2108 silc_server_new_id_real(server, sock, new_id, FALSE);
2110 silc_buffer_push_tail(idp, 4 + id_len);
2111 silc_buffer_pull(packet->buffer, 4 + id_len);
2114 silc_buffer_free(idp);
2118 /* Received New Channel packet. Information about new channels in the
2119 network are distributed using this packet. Save the information about
2120 the new channel. This usually comes from router but also normal server
2121 can send this to notify channels it has when it connects to us. */
2123 void silc_server_new_channel(SilcServer server,
2124 SilcSocketConnection sock,
2125 SilcPacketContext *packet)
2127 SilcChannelPayload payload;
2128 SilcChannelID *channel_id;
2134 SilcServerEntry server_entry;
2135 SilcChannelEntry channel;
2137 SILC_LOG_DEBUG(("Processing New Channel"));
2139 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2140 packet->src_id_type != SILC_ID_SERVER ||
2141 server->server_type == SILC_SERVER)
2144 /* Parse the channel payload */
2145 payload = silc_channel_payload_parse(packet->buffer->data,
2146 packet->buffer->len);
2150 /* Get the channel ID */
2151 channel_id = silc_channel_get_id_parse(payload);
2153 silc_channel_payload_free(payload);
2157 channel_name = silc_channel_get_name(payload, &name_len);
2159 channel_name[255] = '\0';
2161 id = silc_channel_get_id(payload, &id_len);
2163 server_entry = (SilcServerEntry)sock->user_data;
2165 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2166 /* Add the channel to global list as it is coming from router. It
2167 cannot be our own channel as it is coming from router. */
2169 /* Check that we don't already have this channel */
2170 channel = silc_idlist_find_channel_by_name(server->local_list,
2171 channel_name, NULL);
2173 channel = silc_idlist_find_channel_by_name(server->global_list,
2174 channel_name, NULL);
2176 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
2177 silc_id_render(channel_id, SILC_ID_CHANNEL),
2180 silc_idlist_add_channel(server->global_list, strdup(channel_name),
2181 0, channel_id, sock->user_data, NULL, NULL, 0);
2182 server->stat.channels++;
2185 /* The channel is coming from our server, thus it is in our cell
2186 we will add it to our local list. */
2189 SILC_LOG_DEBUG(("Channel id(%s) from [Server] %s",
2190 silc_id_render(channel_id, SILC_ID_CHANNEL),
2193 /* Check that we don't already have this channel */
2194 channel = silc_idlist_find_channel_by_name(server->local_list,
2195 channel_name, NULL);
2197 channel = silc_idlist_find_channel_by_name(server->global_list,
2198 channel_name, NULL);
2200 /* If the channel does not exist, then create it. This creates a new
2201 key to the channel as well that we will send to the server. */
2203 /* The protocol says that the Channel ID's IP address must be based
2204 on the router's IP address. Check whether the ID is based in our
2205 IP and if it is not then create a new ID and enforce the server
2206 to switch the ID. */
2207 if (server_entry->server_type != SILC_BACKUP_ROUTER &&
2208 !SILC_ID_COMPARE(channel_id, server->id, server->id->ip.data_len)) {
2210 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2212 if (silc_id_create_channel_id(server, server->id, server->rng, &tmp)) {
2213 silc_server_send_notify_channel_change(server, sock, FALSE,
2215 silc_free(channel_id);
2220 /* Create the channel with the provided Channel ID */
2221 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
2225 silc_channel_payload_free(payload);
2226 silc_free(channel_id);
2230 /* Get the mode and set it to the channel */
2231 channel->mode = silc_channel_get_mode(payload);
2233 /* Send the new channel key to the server */
2234 chk = silc_channel_key_payload_encode(id_len, id,
2235 strlen(channel->channel_key->
2237 channel->channel_key->cipher->name,
2238 channel->key_len / 8,
2240 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2241 chk->data, chk->len, FALSE);
2242 silc_buffer_free(chk);
2245 /* The channel exist by that name, check whether the ID's match.
2246 If they don't then we'll force the server to use the ID we have.
2247 We also create a new key for the channel. */
2248 SilcBuffer users = NULL, users_modes = NULL;
2250 if (!SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
2251 /* They don't match, send CHANNEL_CHANGE notify to the server to
2252 force the ID change. */
2253 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
2254 silc_server_send_notify_channel_change(server, sock, FALSE,
2255 channel_id, channel->id);
2258 /* If the mode is different from what we have then enforce the
2260 mode = silc_channel_get_mode(payload);
2261 if (channel->mode != mode) {
2262 SILC_LOG_DEBUG(("Forcing the server to change channel mode"));
2263 silc_server_send_notify_cmode(server, sock, FALSE, channel,
2264 channel->mode, server->id,
2266 channel->cipher, channel->hmac_name,
2267 channel->passphrase);
2270 /* Create new key for the channel and send it to the server and
2271 everybody else possibly on the channel. */
2273 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
2274 if (!silc_server_create_channel_key(server, channel, 0))
2277 /* Send to the channel */
2278 silc_server_send_channel_key(server, sock, channel, FALSE);
2279 id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
2280 id_len = SILC_ID_CHANNEL_LEN;
2282 /* Send to the server */
2283 chk = silc_channel_key_payload_encode(id_len, id,
2284 strlen(channel->channel_key->
2286 channel->channel_key->
2288 channel->key_len / 8,
2290 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
2291 chk->data, chk->len, FALSE);
2292 silc_buffer_free(chk);
2296 silc_free(channel_id);
2298 /* Since the channel is coming from server and we also know about it
2299 then send the JOIN notify to the server so that it see's our
2300 users on the channel "joining" the channel. */
2301 silc_server_announce_get_channel_users(server, channel, &users,
2304 silc_buffer_push(users, users->data - users->head);
2305 silc_server_packet_send(server, sock,
2306 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2307 users->data, users->len, FALSE);
2308 silc_buffer_free(users);
2311 silc_buffer_push(users_modes, users_modes->data - users_modes->head);
2312 silc_server_packet_send_dest(server, sock,
2313 SILC_PACKET_NOTIFY, SILC_PACKET_FLAG_LIST,
2314 channel->id, SILC_ID_CHANNEL,
2316 users_modes->len, FALSE);
2317 silc_buffer_free(users_modes);
2322 silc_channel_payload_free(payload);
2325 /* Received New Channel List packet, list of New Channel List payloads inside
2326 one packet. Process the New Channel payloads one by one. */
2328 void silc_server_new_channel_list(SilcServer server,
2329 SilcSocketConnection sock,
2330 SilcPacketContext *packet)
2332 SilcPacketContext *new;
2336 SILC_LOG_DEBUG(("Processing New Channel List"));
2338 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
2339 packet->src_id_type != SILC_ID_SERVER ||
2340 server->server_type == SILC_SERVER)
2343 /* If the sender of this packet is server and we are router we need to
2344 broadcast this packet to other routers in the network. Broadcast
2345 this list packet instead of multiple New Channel packets. */
2346 if (!server->standalone && server->server_type == SILC_ROUTER &&
2347 sock->type == SILC_SOCKET_TYPE_SERVER &&
2348 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
2349 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
2350 silc_server_packet_send(server, server->router->connection,
2352 packet->flags | SILC_PACKET_FLAG_BROADCAST,
2353 packet->buffer->data, packet->buffer->len, FALSE);
2354 silc_server_backup_send(server, (SilcServerEntry)sock->user_data,
2355 packet->type, packet->flags,
2356 packet->buffer->data, packet->buffer->len,
2360 /* Make copy of the original packet context, except for the actual
2361 data buffer, which we will here now fetch from the original buffer. */
2362 new = silc_packet_context_alloc();
2363 new->type = SILC_PACKET_NEW_CHANNEL;
2364 new->flags = packet->flags;
2365 new->src_id = packet->src_id;
2366 new->src_id_len = packet->src_id_len;
2367 new->src_id_type = packet->src_id_type;
2368 new->dst_id = packet->dst_id;
2369 new->dst_id_len = packet->dst_id_len;
2370 new->dst_id_type = packet->dst_id_type;
2372 buffer = silc_buffer_alloc(512);
2373 new->buffer = buffer;
2375 while (packet->buffer->len) {
2376 SILC_GET16_MSB(len1, packet->buffer->data);
2377 if ((len1 > packet->buffer->len) ||
2378 (len1 > buffer->truelen))
2381 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
2382 if ((len2 > packet->buffer->len) ||
2383 (len2 > buffer->truelen))
2386 silc_buffer_pull_tail(buffer, 8 + len1 + len2);
2387 silc_buffer_put(buffer, packet->buffer->data, 8 + len1 + len2);
2389 /* Process the New Channel */
2390 silc_server_new_channel(server, sock, new);
2392 silc_buffer_push_tail(buffer, 8 + len1 + len2);
2393 silc_buffer_pull(packet->buffer, 8 + len1 + len2);
2396 silc_buffer_free(buffer);
2400 /* Received key agreement packet. This packet is never for us. It is to
2401 the client in the packet's destination ID. Sending of this sort of packet
2402 equals sending private message, ie. it is sent point to point from
2403 one client to another. */
2405 void silc_server_key_agreement(SilcServer server,
2406 SilcSocketConnection sock,
2407 SilcPacketContext *packet)
2409 SilcSocketConnection dst_sock;
2410 SilcIDListData idata;
2412 SILC_LOG_DEBUG(("Start"));
2414 if (packet->src_id_type != SILC_ID_CLIENT ||
2415 packet->dst_id_type != SILC_ID_CLIENT)
2418 if (!packet->dst_id)
2421 /* Get the route to the client */
2422 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2423 packet->dst_id_len, NULL, &idata);
2427 /* Relay the packet */
2428 silc_server_relay_packet(server, dst_sock, idata->send_key,
2429 idata->hmac_send, idata->psn_send++,
2433 /* Received connection auth request packet that is used during connection
2434 phase to resolve the mandatory authentication method. This packet can
2435 actually be received at anytime but usually it is used only during
2436 the connection authentication phase. Now, protocol says that this packet
2437 can come from client or server, however, we support only this coming
2438 from client and expect that server always knows what authentication
2441 void silc_server_connection_auth_request(SilcServer server,
2442 SilcSocketConnection sock,
2443 SilcPacketContext *packet)
2445 SilcServerConfigSectionClientConnection *client = NULL;
2448 SilcAuthMethod auth_meth;
2450 SILC_LOG_DEBUG(("Start"));
2452 if (packet->src_id_type && packet->src_id_type != SILC_ID_CLIENT)
2455 /* Parse the payload */
2456 ret = silc_buffer_unformat(packet->buffer,
2457 SILC_STR_UI_SHORT(&conn_type),
2458 SILC_STR_UI_SHORT(NULL),
2463 if (conn_type != SILC_SOCKET_TYPE_CLIENT)
2466 /* Get the authentication method for the client */
2467 auth_meth = SILC_AUTH_NONE;
2468 port = server->sockets[server->sock]->port; /* Listenning port */
2469 client = silc_server_config_find_client_conn(server->config,
2473 client = silc_server_config_find_client_conn(server->config,
2477 auth_meth = client->auth_meth;
2479 /* Send it back to the client */
2480 silc_server_send_connection_auth_request(server, sock,
2485 /* Received REKEY packet. The sender of the packet wants to regenerate
2486 its session keys. This starts the REKEY protocol. */
2488 void silc_server_rekey(SilcServer server,
2489 SilcSocketConnection sock,
2490 SilcPacketContext *packet)
2492 SilcProtocol protocol;
2493 SilcServerRekeyInternalContext *proto_ctx;
2494 SilcIDListData idata = (SilcIDListData)sock->user_data;
2496 SILC_LOG_DEBUG(("Start"));
2498 /* Allocate internal protocol context. This is sent as context
2500 proto_ctx = silc_calloc(1, sizeof(*proto_ctx));
2501 proto_ctx->server = (void *)server;
2502 proto_ctx->sock = sock;
2503 proto_ctx->responder = TRUE;
2504 proto_ctx->pfs = idata->rekey->pfs;
2506 /* Perform rekey protocol. Will call the final callback after the
2507 protocol is over. */
2508 silc_protocol_alloc(SILC_PROTOCOL_SERVER_REKEY,
2509 &protocol, proto_ctx, silc_server_rekey_final);
2510 sock->protocol = protocol;
2512 if (proto_ctx->pfs == FALSE)
2513 /* Run the protocol */
2514 silc_protocol_execute(protocol, server->schedule, 0, 0);
2517 /* Received file transger packet. This packet is never for us. It is to
2518 the client in the packet's destination ID. Sending of this sort of packet
2519 equals sending private message, ie. it is sent point to point from
2520 one client to another. */
2522 void silc_server_ftp(SilcServer server,
2523 SilcSocketConnection sock,
2524 SilcPacketContext *packet)
2526 SilcSocketConnection dst_sock;
2527 SilcIDListData idata;
2529 SILC_LOG_DEBUG(("Start"));
2531 if (packet->src_id_type != SILC_ID_CLIENT ||
2532 packet->dst_id_type != SILC_ID_CLIENT)
2535 if (!packet->dst_id)
2538 /* Get the route to the client */
2539 dst_sock = silc_server_get_client_route(server, packet->dst_id,
2540 packet->dst_id_len, NULL, &idata);
2544 /* Relay the packet */
2545 silc_server_relay_packet(server, dst_sock, idata->send_key,
2546 idata->hmac_send, idata->psn_send++,