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;
41 SilcClientID *client_id, *client_id2;
42 SilcChannelEntry channel;
43 SilcClientEntry client;
44 SilcChannelClientEntry chl;
49 SILC_LOG_DEBUG(("Start"));
51 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
52 packet->src_id_type != SILC_ID_SERVER)
55 /* If we are router and this packet is not already broadcast packet
56 we will broadcast it. The sending socket really cannot be router or
57 the router is buggy. If this packet is coming from router then it must
58 have the broadcast flag set already and we won't do anything. */
59 if (!server->standalone && server->server_type == SILC_ROUTER &&
60 sock->type == SILC_SOCKET_TYPE_SERVER &&
61 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
62 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
63 silc_server_packet_send(server, server->router->connection, packet->type,
64 packet->flags | SILC_PACKET_FLAG_BROADCAST,
65 packet->buffer->data, packet->buffer->len, FALSE);
68 payload = silc_notify_payload_parse(packet->buffer);
72 type = silc_notify_get_type(payload);
73 args = silc_notify_get_args(payload);
78 case SILC_NOTIFY_TYPE_JOIN:
80 * Distribute the notify to local clients on the channel
82 SILC_LOG_DEBUG(("JOIN notify"));
85 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
88 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
92 /* Get channel entry */
93 channel = silc_idlist_find_channel_by_id(server->local_list,
96 channel = silc_idlist_find_channel_by_id(server->global_list,
99 silc_free(channel_id);
103 silc_free(channel_id);
106 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
109 client_id = silc_id_payload_parse_id(tmp, tmp_len);
113 /* Send to channel */
114 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
115 FALSE, packet->buffer->data,
116 packet->buffer->len, FALSE);
118 /* If the the client is not in local list we check global list (ie. the
119 channel will be global channel) and if it does not exist then create
120 entry for the client. */
121 client = silc_idlist_find_client_by_id(server->local_list,
124 client = silc_idlist_find_client_by_id(server->global_list,
127 /* If router did not find the client the it is bogus */
128 if (server->server_type == SILC_ROUTER)
132 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
133 silc_id_dup(client_id, SILC_ID_CLIENT),
134 sock->user_data, NULL);
136 silc_free(client_id);
142 /* Do not add client to channel if it is there already */
143 if (silc_server_client_on_channel(client, channel))
146 if (server->server_type == SILC_SERVER &&
147 sock->type == SILC_SOCKET_TYPE_ROUTER)
148 /* The channel is global now */
149 channel->global_users = TRUE;
151 /* JOIN the global client to the channel (local clients (if router
152 created the channel) is joined in the pending JOIN command). */
153 chl = silc_calloc(1, sizeof(*chl));
154 chl->client = client;
155 chl->channel = channel;
156 silc_list_add(channel->user_list, chl);
157 silc_list_add(client->channels, chl);
158 silc_free(client_id);
162 case SILC_NOTIFY_TYPE_LEAVE:
164 * Distribute the notify to local clients on the channel
166 SILC_LOG_DEBUG(("LEAVE notify"));
168 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
169 packet->dst_id_type);
173 /* Get channel entry */
174 channel = silc_idlist_find_channel_by_id(server->local_list,
177 channel = silc_idlist_find_channel_by_id(server->global_list,
180 silc_free(channel_id);
186 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
188 silc_free(channel_id);
191 client_id = silc_id_payload_parse_id(tmp, tmp_len);
193 silc_free(channel_id);
197 /* Send to channel */
198 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
199 FALSE, packet->buffer->data,
200 packet->buffer->len, FALSE);
202 /* Get client entry */
203 client = silc_idlist_find_client_by_id(server->global_list,
206 client = silc_idlist_find_client_by_id(server->local_list,
209 silc_free(client_id);
210 silc_free(channel_id);
214 silc_free(client_id);
216 /* Remove the user from channel */
217 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
220 case SILC_NOTIFY_TYPE_SIGNOFF:
222 * Distribute the notify to local clients on the channel
224 SILC_LOG_DEBUG(("SIGNOFF notify"));
227 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
230 client_id = silc_id_payload_parse_id(tmp, tmp_len);
234 /* Get client entry */
235 client = silc_idlist_find_client_by_id(server->global_list,
238 client = silc_idlist_find_client_by_id(server->local_list,
241 silc_free(client_id);
245 silc_free(client_id);
247 /* Get signoff message */
248 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
252 /* Remove the client from all channels */
253 silc_server_remove_from_channels(server, NULL, client, tmp);
255 /* Remove the client entry */
256 if (!silc_idlist_del_client(server->global_list, client))
257 silc_idlist_del_client(server->local_list, client);
260 case SILC_NOTIFY_TYPE_TOPIC_SET:
262 * Distribute the notify to local clients on the channel
265 SILC_LOG_DEBUG(("TOPIC SET notify"));
267 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
268 packet->dst_id_type);
272 /* Get channel entry */
273 channel = silc_idlist_find_channel_by_id(server->local_list,
276 channel = silc_idlist_find_channel_by_id(server->global_list,
279 silc_free(channel_id);
285 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
287 silc_free(channel_id);
292 silc_free(channel->topic);
293 channel->topic = silc_calloc(tmp_len, sizeof(*channel->topic));
294 memcpy(channel->topic, tmp, tmp_len);
296 /* Send the same notify to the channel */
297 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
298 FALSE, packet->buffer->data,
299 packet->buffer->len, FALSE);
300 silc_free(channel_id);
303 case SILC_NOTIFY_TYPE_NICK_CHANGE:
306 * Distribute the notify to local clients on the channel
308 unsigned char *id, *id2;
310 SILC_LOG_DEBUG(("NICK CHANGE notify"));
312 /* Get old client ID */
313 id = silc_argument_get_arg_type(args, 1, &tmp_len);
316 client_id = silc_id_payload_parse_id(id, tmp_len);
320 /* Get new client ID */
321 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
324 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
328 SILC_LOG_DEBUG(("Old Client ID id(%s)",
329 silc_id_render(client_id, SILC_ID_CLIENT)));
330 SILC_LOG_DEBUG(("New Client ID id(%s)",
331 silc_id_render(client_id2, SILC_ID_CLIENT)));
333 /* Replace the Client ID */
334 client = silc_idlist_replace_client_id(server->global_list, client_id,
337 client = silc_idlist_replace_client_id(server->local_list, client_id,
341 /* The nickname is not valid anymore, set it NULL. This causes that
342 the nickname will be queried if someone wants to know it. */
343 if (client->nickname)
344 silc_free(client->nickname);
345 client->nickname = NULL;
347 /* Send the NICK_CHANGE notify type to local clients on the channels
348 this client is joined to. */
349 silc_server_send_notify_on_channels(server, client,
350 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
355 silc_free(client_id);
357 silc_free(client_id2);
361 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
363 * Distribute the notify to local clients on the channel
366 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
368 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
369 packet->dst_id_type);
373 /* Get channel entry */
374 channel = silc_idlist_find_channel_by_id(server->local_list,
377 channel = silc_idlist_find_channel_by_id(server->global_list,
380 silc_free(channel_id);
385 /* Send the same notify to the channel */
386 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
387 FALSE, packet->buffer->data,
388 packet->buffer->len, FALSE);
391 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
393 silc_free(channel_id);
397 SILC_GET32_MSB(mode, tmp);
400 channel->mode = mode;
401 silc_free(channel_id);
404 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
406 * Distribute the notify to local clients on the channel
409 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
411 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
412 packet->dst_id_type);
416 /* Get channel entry */
417 channel = silc_idlist_find_channel_by_id(server->local_list,
420 channel = silc_idlist_find_channel_by_id(server->global_list,
423 silc_free(channel_id);
429 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
431 silc_free(channel_id);
435 SILC_GET32_MSB(mode, tmp);
437 /* Get target client */
438 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
441 client_id = silc_id_payload_parse_id(tmp, tmp_len);
445 /* Get client entry */
446 client = silc_idlist_find_client_by_id(server->global_list,
449 client = silc_idlist_find_client_by_id(server->local_list,
452 silc_free(client_id);
456 silc_free(client_id);
458 /* Get entry to the channel user list */
459 silc_list_start(channel->user_list);
460 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
461 if (chl->client == client) {
462 /* Change the mode */
467 /* Send the same notify to the channel */
468 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
469 FALSE, packet->buffer->data,
470 packet->buffer->len, FALSE);
471 silc_free(channel_id);
474 case SILC_NOTIFY_TYPE_INVITE:
475 SILC_LOG_DEBUG(("INVITE notify (not-impl XXX)"));
478 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
479 SILC_LOG_DEBUG(("CHANNEL CHANGE notify (not-impl XXX)"));
482 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
483 SILC_LOG_DEBUG(("SERVER SIGNOFF notify (not-impl XXX)"));
486 case SILC_NOTIFY_TYPE_KICKED:
488 * Distribute the notify to local clients on the channel
491 SILC_LOG_DEBUG(("KICKED notify"));
493 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
494 packet->dst_id_type);
498 /* Get channel entry */
499 channel = silc_idlist_find_channel_by_id(server->local_list,
502 channel = silc_idlist_find_channel_by_id(server->global_list,
505 silc_free(channel_id);
509 silc_free(channel_id);
512 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
515 client_id = silc_id_payload_parse_id(tmp, tmp_len);
519 /* Send to channel */
520 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
521 FALSE, packet->buffer->data,
522 packet->buffer->len, FALSE);
524 /* If the the client is not in local list we check global list */
525 client = silc_idlist_find_client_by_id(server->local_list,
528 client = silc_idlist_find_client_by_id(server->global_list,
531 silc_free(client_id);
536 /* Remove the client from channel */
537 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
541 /* Ignore rest of the notify types for now */
542 case SILC_NOTIFY_TYPE_NONE:
543 case SILC_NOTIFY_TYPE_MOTD:
550 silc_notify_payload_free(payload);
553 void silc_server_notify_list(SilcServer server,
554 SilcSocketConnection sock,
555 SilcPacketContext *packet)
557 SilcPacketContext *new;
561 SILC_LOG_DEBUG(("Processing New Notify List"));
563 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
564 packet->src_id_type != SILC_ID_SERVER)
567 /* Make copy of the original packet context, except for the actual
568 data buffer, which we will here now fetch from the original buffer. */
569 new = silc_packet_context_alloc();
570 new->type = SILC_PACKET_NOTIFY;
571 new->flags = packet->flags;
572 new->src_id = packet->src_id;
573 new->src_id_len = packet->src_id_len;
574 new->src_id_type = packet->src_id_type;
575 new->dst_id = packet->dst_id;
576 new->dst_id_len = packet->dst_id_len;
577 new->dst_id_type = packet->dst_id_type;
579 buffer = silc_buffer_alloc(1024);
580 new->buffer = buffer;
582 while (packet->buffer->len) {
583 SILC_GET16_MSB(len, packet->buffer->data + 2);
584 if (len > packet->buffer->len)
587 if (len > buffer->truelen) {
588 silc_buffer_free(buffer);
589 buffer = silc_buffer_alloc(1024 + len);
592 silc_buffer_pull_tail(buffer, len);
593 silc_buffer_put(buffer, packet->buffer->data, len);
595 /* Process the Notify */
596 silc_server_notify(server, sock, new);
598 silc_buffer_push_tail(buffer, len);
599 silc_buffer_pull(packet->buffer, len);
602 silc_buffer_free(buffer);
606 /* Received private message. This resolves the destination of the message
607 and sends the packet. This is used by both server and router. If the
608 destination is our locally connected client this sends the packet to
609 the client. This may also send the message for further routing if
610 the destination is not in our server (or router). */
612 void silc_server_private_message(SilcServer server,
613 SilcSocketConnection sock,
614 SilcPacketContext *packet)
617 SilcServerEntry router;
618 SilcSocketConnection dst_sock;
619 SilcClientEntry client;
620 SilcIDListData idata;
622 SILC_LOG_DEBUG(("Start"));
624 if (packet->src_id_type != SILC_ID_CLIENT ||
625 packet->dst_id_type != SILC_ID_CLIENT)
631 /* Decode destination Client ID */
632 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
634 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
638 /* If the destination belongs to our server we don't have to route
639 the message anywhere but to send it to the local destination. */
640 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
642 /* It exists, now deliver the message to the destination */
643 dst_sock = (SilcSocketConnection)client->connection;
645 /* If we are router and the client has router then the client is in
646 our cell but not directly connected to us. */
647 if (server->server_type == SILC_ROUTER && client->router) {
648 /* We are of course in this case the client's router thus the real
649 "router" of the client is the server who owns the client. Thus
650 we will send the packet to that server. */
651 router = (SilcServerEntry)client->router;
652 idata = (SilcIDListData)router;
654 silc_server_send_private_message(server, router->connection,
661 /* Seems that client really is directly connected to us */
662 idata = (SilcIDListData)client;
663 silc_server_send_private_message(server, dst_sock,
665 idata->hmac, packet);
669 /* Destination belongs to someone not in this server. If we are normal
670 server our action is to send the packet to our router. */
671 if (server->server_type == SILC_SERVER && !server->standalone) {
672 router = server->router;
674 /* Send to primary route */
676 dst_sock = (SilcSocketConnection)router->connection;
677 idata = (SilcIDListData)router;
678 silc_server_send_private_message(server, dst_sock,
680 idata->hmac, packet);
685 /* We are router and we will perform route lookup for the destination
686 and send the message to fastest route. */
687 if (server->server_type == SILC_ROUTER && !server->standalone) {
688 /* Check first that the ID is valid */
689 client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
691 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
692 router = (SilcServerEntry)dst_sock->user_data;
693 idata = (SilcIDListData)router;
695 /* Get fastest route and send packet. */
697 silc_server_send_private_message(server, dst_sock,
699 idata->hmac, packet);
705 /* Received private message key packet.. This packet is never for us. It is to
706 the client in the packet's destination ID. Sending of this sort of packet
707 equals sending private message, ie. it is sent point to point from
708 one client to another. */
710 void silc_server_private_message_key(SilcServer server,
711 SilcSocketConnection sock,
712 SilcPacketContext *packet)
715 SilcServerEntry router;
716 SilcSocketConnection dst_sock;
717 SilcClientEntry client;
718 SilcIDListData idata;
720 SILC_LOG_DEBUG(("Start"));
722 if (packet->src_id_type != SILC_ID_CLIENT ||
723 packet->dst_id_type != SILC_ID_CLIENT)
729 /* Decode destination Client ID */
730 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
732 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
736 /* If the destination belongs to our server we don't have to route
737 the message anywhere but to send it to the local destination. */
738 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
740 /* It exists, now deliver the message to the destination */
741 dst_sock = (SilcSocketConnection)client->connection;
743 /* If we are router and the client has router then the client is in
744 our cell but not directly connected to us. */
745 if (server->server_type == SILC_ROUTER && client->router) {
746 /* We are of course in this case the client's router thus the real
747 "router" of the client is the server who owns the client. Thus
748 we will send the packet to that server. */
749 router = (SilcServerEntry)client->router;
750 idata = (SilcIDListData)router;
751 silc_server_send_private_message_key(server, router->connection,
758 /* Seems that client really is directly connected to us */
759 idata = (SilcIDListData)client;
760 silc_server_send_private_message_key(server, dst_sock,
762 idata->hmac, packet);
766 /* Destination belongs to someone not in this server. If we are normal
767 server our action is to send the packet to our router. */
768 if (server->server_type == SILC_SERVER && !server->standalone) {
769 router = server->router;
771 /* Send to primary route */
773 dst_sock = (SilcSocketConnection)router->connection;
774 idata = (SilcIDListData)router;
775 silc_server_send_private_message_key(server, dst_sock,
777 idata->hmac, packet);
782 /* We are router and we will perform route lookup for the destination
783 and send the packet to fastest route. */
784 if (server->server_type == SILC_ROUTER && !server->standalone) {
785 /* Check first that the ID is valid */
786 client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
788 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
789 router = (SilcServerEntry)dst_sock->user_data;
790 idata = (SilcIDListData)router;
792 /* Get fastest route and send packet. */
794 silc_server_send_private_message_key(server, dst_sock,
796 idata->hmac, packet);
802 /* Processes incoming command reply packet. The command reply packet may
803 be destined to one of our clients or it may directly for us. We will
804 call the command reply routine after processing the packet. */
806 void silc_server_command_reply(SilcServer server,
807 SilcSocketConnection sock,
808 SilcPacketContext *packet)
810 SilcBuffer buffer = packet->buffer;
811 SilcClientEntry client = NULL;
812 SilcSocketConnection dst_sock;
813 SilcIDListData idata;
814 SilcClientID *id = NULL;
816 SILC_LOG_DEBUG(("Start"));
818 /* Source must be server or router */
819 if (packet->src_id_type != SILC_ID_SERVER &&
820 sock->type != SILC_SOCKET_TYPE_ROUTER)
823 if (packet->dst_id_type == SILC_ID_CHANNEL)
826 if (packet->dst_id_type == SILC_ID_CLIENT) {
827 /* Destination must be one of ours */
828 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
831 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
833 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
839 if (packet->dst_id_type == SILC_ID_SERVER) {
840 /* For now this must be for us */
841 if (SILC_ID_SERVER_COMPARE(packet->dst_id, server->id_string)) {
842 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
847 /* Execute command reply locally for the command */
848 silc_server_command_reply_process(server, sock, buffer);
850 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
851 /* Relay the packet to the client */
853 dst_sock = (SilcSocketConnection)client->connection;
854 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
855 + packet->dst_id_len + packet->padlen);
857 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
858 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
860 idata = (SilcIDListData)client;
863 silc_packet_encrypt(idata->send_key, idata->hmac, dst_sock->outbuf,
866 /* Send the packet */
867 silc_server_packet_send_real(server, dst_sock, TRUE);
873 /* Process received channel message. The message can be originated from
876 void silc_server_channel_message(SilcServer server,
877 SilcSocketConnection sock,
878 SilcPacketContext *packet)
880 SilcChannelEntry channel = NULL;
881 SilcChannelClientEntry chl;
882 SilcChannelID *id = NULL;
885 SILC_LOG_DEBUG(("Processing channel message"));
888 if (packet->dst_id_type != SILC_ID_CHANNEL) {
889 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
893 /* Find channel entry */
894 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
897 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
899 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
901 SILC_LOG_DEBUG(("Could not find channel"));
906 /* See that this client is on the channel. If the message is coming
907 from router we won't do the check as the message is from client that
908 we don't know about. Also, if the original sender is not client
909 (as it can be server as well) we don't do the check. */
910 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
911 packet->src_id_type);
914 if (sock->type != SILC_SOCKET_TYPE_ROUTER &&
915 packet->src_id_type == SILC_ID_CLIENT) {
916 silc_list_start(channel->user_list);
917 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
918 if (chl->client && !SILC_ID_CLIENT_COMPARE(chl->client->id, sender))
921 if (chl == SILC_LIST_END) {
922 SILC_LOG_DEBUG(("Client not on channel"));
927 /* If we are router and the packet came from router and private key
928 has not been set for the channel then we must encrypt the packet
929 as it was decrypted with the session key shared between us and the
930 router which sent it. This is so, because cells does not share the
932 if (server->server_type == SILC_ROUTER &&
933 sock->type == SILC_SOCKET_TYPE_ROUTER &&
934 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
936 unsigned int iv_len, i, data_len;
938 iv_len = silc_cipher_get_block_len(channel->channel_key);
939 if (channel->iv[0] == '\0')
940 for (i = 0; i < iv_len; i++) channel->iv[i] =
941 silc_rng_get_byte(server->rng);
943 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
945 /* Encode new payload. This encrypts it also. */
946 SILC_GET16_MSB(data_len, packet->buffer->data);
947 chp = silc_channel_payload_encode(data_len, packet->buffer->data + 2,
949 channel->channel_key,
950 channel->hmac, server->rng);
951 silc_buffer_put(packet->buffer, chp->data, chp->len);
952 silc_buffer_free(chp);
955 /* Distribute the packet to our local clients. This will send the
956 packet for further routing as well, if needed. */
957 silc_server_packet_relay_to_channel(server, sock, channel, sender,
959 packet->buffer->data,
960 packet->buffer->len, FALSE);
969 /* Received channel key packet. We distribute the key to all of our locally
970 connected clients on the channel. */
972 void silc_server_channel_key(SilcServer server,
973 SilcSocketConnection sock,
974 SilcPacketContext *packet)
976 SilcBuffer buffer = packet->buffer;
977 SilcChannelEntry channel;
979 if (packet->src_id_type != SILC_ID_SERVER)
982 /* Save the channel key */
983 channel = silc_server_save_channel_key(server, buffer, NULL);
987 /* Distribute the key to everybody who is on the channel. If we are router
988 we will also send it to locally connected servers. */
989 silc_server_send_channel_key(server, sock, channel, FALSE);
992 /* Received New Client packet and processes it. Creates Client ID for the
993 client. Client becomes registered after calling this functions. */
995 SilcClientEntry silc_server_new_client(SilcServer server,
996 SilcSocketConnection sock,
997 SilcPacketContext *packet)
999 SilcBuffer buffer = packet->buffer;
1000 SilcClientEntry client;
1001 SilcIDCacheEntry cache;
1002 SilcClientID *client_id;
1004 SilcIDListData idata;
1005 char *username = NULL, *realname = NULL, *id_string;
1008 SILC_LOG_DEBUG(("Creating new client"));
1010 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
1013 /* Take client entry */
1014 client = (SilcClientEntry)sock->user_data;
1015 idata = (SilcIDListData)client;
1017 /* Fetch the old client cache entry so that we can update it. */
1018 if (!silc_idcache_find_by_context(server->local_list->clients,
1019 sock->user_data, &cache)) {
1020 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
1024 /* Parse incoming packet */
1025 ret = silc_buffer_unformat(buffer,
1026 SILC_STR_UI16_STRING_ALLOC(&username),
1027 SILC_STR_UI16_STRING_ALLOC(&realname),
1031 silc_free(username);
1033 silc_free(realname);
1038 silc_free(username);
1040 silc_free(realname);
1041 silc_server_disconnect_remote(server, sock, "Server closed connection: "
1042 "Incomplete client information");
1046 /* Create Client ID */
1047 silc_id_create_client_id(server->id, server->rng, server->md5hash,
1048 username, &client_id);
1050 /* Update client entry */
1051 idata->registered = TRUE;
1052 client->nickname = strdup(username);
1053 client->username = username;
1054 client->userinfo = realname ? realname : strdup(" ");
1055 client->id = client_id;
1057 /* Update the cache entry */
1058 cache->id = (void *)client_id;
1059 cache->type = SILC_ID_CLIENT;
1060 cache->data = username;
1061 silc_idcache_sort_by_data(server->local_list->clients);
1063 /* Notify our router about new client on the SILC network */
1064 if (!server->standalone)
1065 silc_server_send_new_id(server, (SilcSocketConnection)
1066 server->router->connection,
1067 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1068 client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
1070 /* Send the new client ID to the client. */
1071 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1072 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
1073 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1074 silc_buffer_format(reply,
1075 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1076 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
1077 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
1079 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1080 reply->data, reply->len, FALSE);
1081 silc_free(id_string);
1082 silc_buffer_free(reply);
1084 /* Send some nice info to the client */
1085 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1086 ("Welcome to the SILC Network %s@%s",
1087 username, sock->hostname));
1088 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1089 ("Your host is %s, running version %s",
1090 server->config->server_info->server_name,
1092 if (server->server_type == SILC_ROUTER) {
1093 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1094 ("There are %d clients on %d servers in SILC "
1095 "Network", server->stat.clients,
1096 server->stat.servers + 1));
1097 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1098 ("There are %d clients on %d server in our cell",
1099 server->stat.cell_clients,
1100 server->stat.cell_servers + 1));
1101 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1102 ("I have %d clients, %d channels, %d servers and "
1104 server->stat.my_clients,
1105 server->stat.my_channels,
1106 server->stat.my_servers,
1107 server->stat.my_routers));
1108 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1109 ("%d server operators and %d router operators "
1111 server->stat.my_server_ops,
1112 server->stat.my_router_ops));
1114 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1115 ("I have %d clients and %d channels formed",
1116 server->stat.my_clients,
1117 server->stat.my_channels));
1118 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1119 ("%d operators online",
1120 server->stat.my_server_ops));
1122 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1123 ("Your connection is secured with %s cipher, "
1124 "key length %d bits",
1125 idata->send_key->cipher->name,
1126 idata->send_key->cipher->key_len));
1127 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1128 ("Your current nickname is %s",
1132 silc_server_send_motd(server, sock);
1137 /* Create new server. This processes received New Server packet and
1138 saves the received Server ID. The server is our locally connected
1139 server thus we save all the information and save it to local list.
1140 This funtion can be used by both normal server and router server.
1141 If normal server uses this it means that its router has connected
1142 to the server. If router uses this it means that one of the cell's
1143 servers is connected to the router. */
1145 SilcServerEntry silc_server_new_server(SilcServer server,
1146 SilcSocketConnection sock,
1147 SilcPacketContext *packet)
1149 SilcBuffer buffer = packet->buffer;
1150 SilcServerEntry new_server;
1151 SilcIDCacheEntry cache;
1152 SilcServerID *server_id;
1153 SilcIDListData idata;
1154 unsigned char *server_name, *id_string;
1155 unsigned short id_len;
1158 SILC_LOG_DEBUG(("Creating new server"));
1160 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1161 sock->type != SILC_SOCKET_TYPE_ROUTER)
1164 /* Take server entry */
1165 new_server = (SilcServerEntry)sock->user_data;
1166 idata = (SilcIDListData)new_server;
1168 /* Fetch the old server cache entry so that we can update it. */
1169 if (!silc_idcache_find_by_context(server->local_list->servers,
1170 sock->user_data, &cache)) {
1171 SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
1175 /* Parse the incoming packet */
1176 ret = silc_buffer_unformat(buffer,
1177 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1178 SILC_STR_UI16_STRING_ALLOC(&server_name),
1182 silc_free(id_string);
1184 silc_free(server_name);
1188 if (id_len > buffer->len) {
1189 silc_free(id_string);
1190 silc_free(server_name);
1195 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1197 silc_free(id_string);
1198 silc_free(server_name);
1201 silc_free(id_string);
1203 /* Update client entry */
1204 idata->registered = TRUE;
1205 new_server->server_name = server_name;
1206 new_server->id = server_id;
1208 /* Update the cache entry */
1209 cache->id = (void *)server_id;
1210 cache->type = SILC_ID_SERVER;
1211 cache->data = server_name;
1212 silc_idcache_sort_by_data(server->local_list->servers);
1214 /* Distribute the information about new server in the SILC network
1215 to our router. If we are normal server we won't send anything
1216 since this connection must be our router connection. */
1217 if (server->server_type == SILC_ROUTER && !server->standalone &&
1218 server->router->connection != sock)
1219 silc_server_send_new_id(server, server->router->connection,
1220 TRUE, new_server->id, SILC_ID_SERVER,
1221 SILC_ID_SERVER_LEN);
1223 if (server->server_type == SILC_ROUTER)
1224 server->stat.cell_servers++;
1229 /* Processes incoming New ID packet. New ID Payload is used to distribute
1230 information about newly registered clients and servers. */
1232 static void silc_server_new_id_real(SilcServer server,
1233 SilcSocketConnection sock,
1234 SilcPacketContext *packet,
1237 SilcBuffer buffer = packet->buffer;
1239 SilcServerEntry router;
1240 SilcSocketConnection router_sock;
1243 unsigned char *hash = NULL;
1246 SILC_LOG_DEBUG(("Processing new ID"));
1248 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1249 server->server_type == SILC_SERVER ||
1250 packet->src_id_type != SILC_ID_SERVER)
1253 idp = silc_id_payload_parse(buffer);
1257 id_type = silc_id_payload_get_type(idp);
1259 /* Normal server cannot have other normal server connections */
1260 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1263 id = silc_id_payload_get_id(idp);
1267 /* If the sender of this packet is server and we are router we need to
1268 broadcast this packet to other routers in the network. */
1269 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1270 sock->type == SILC_SOCKET_TYPE_SERVER &&
1271 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1272 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1273 silc_server_packet_send(server, server->router->connection,
1275 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1276 buffer->data, buffer->len, FALSE);
1279 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1280 id_list = server->local_list;
1282 id_list = server->global_list;
1285 router = sock->user_data;
1288 case SILC_ID_CLIENT:
1290 SilcClientEntry entry;
1292 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1293 silc_id_render(id, SILC_ID_CLIENT),
1294 sock->type == SILC_SOCKET_TYPE_SERVER ?
1295 "Server" : "Router", sock->hostname));
1297 /* As a router we keep information of all global information in our
1298 global list. Cell wide information however is kept in the local
1299 list. The client is put to global list and we will take the hash
1300 value of the Client ID and save it to the ID Cache system for fast
1301 searching in the future. */
1302 hash = silc_calloc(sizeof(((SilcClientID *)id)->hash),
1303 sizeof(unsigned char));
1304 memcpy(hash, ((SilcClientID *)id)->hash,
1305 sizeof(((SilcClientID *)id)->hash));
1306 entry = silc_idlist_add_client(id_list, hash, NULL, NULL, id,
1308 entry->nickname = NULL;
1310 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1311 server->stat.cell_clients++;
1312 server->stat.clients++;
1315 /* XXX Adding two ID's with same IP number replaces the old entry thus
1316 gives wrong route. Thus, now disabled until figured out a better way
1317 to do this or when removed the whole thing. This could be removed
1318 because entry->router->connection gives always the most optimal route
1319 for the ID anyway (unless new routes (faster perhaps) are established
1320 after receiving this ID, this we don't know however). */
1321 /* Add route cache for this ID */
1322 silc_server_route_add(silc_server_route_hash(
1323 ((SilcClientID *)id)->ip.s_addr,
1324 server->id->port), ((SilcClientID *)id)->ip.s_addr,
1330 case SILC_ID_SERVER:
1331 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1332 silc_id_render(id, SILC_ID_SERVER),
1333 sock->type == SILC_SOCKET_TYPE_SERVER ?
1334 "Server" : "Router", sock->hostname));
1336 /* As a router we keep information of all global information in our global
1337 list. Cell wide information however is kept in the local list. */
1338 silc_idlist_add_server(id_list, NULL, 0, id, router, router_sock);
1340 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1341 server->stat.cell_servers++;
1342 server->stat.servers++;
1345 /* Add route cache for this ID */
1346 silc_server_route_add(silc_server_route_hash(
1347 ((SilcServerID *)id)->ip.s_addr,
1348 ((SilcServerID *)id)->port),
1349 ((SilcServerID *)id)->ip.s_addr,
1354 case SILC_ID_CHANNEL:
1355 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1363 silc_id_payload_free(idp);
1367 /* Processes incoming New ID packet. New ID Payload is used to distribute
1368 information about newly registered clients and servers. */
1370 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1371 SilcPacketContext *packet)
1373 silc_server_new_id_real(server, sock, packet, TRUE);
1376 /* Receoved New Id List packet, list of New ID payloads inside one
1377 packet. Process the New ID payloads one by one. */
1379 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1380 SilcPacketContext *packet)
1382 SilcPacketContext *new_id;
1384 unsigned short id_len;
1386 SILC_LOG_DEBUG(("Processing New ID List"));
1388 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1389 packet->src_id_type != SILC_ID_SERVER)
1392 /* If the sender of this packet is server and we are router we need to
1393 broadcast this packet to other routers in the network. Broadcast
1394 this list packet instead of multiple New ID packets. */
1395 if (!server->standalone && server->server_type == SILC_ROUTER &&
1396 sock->type == SILC_SOCKET_TYPE_SERVER &&
1397 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1398 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1399 silc_server_packet_send(server, server->router->connection,
1401 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1402 packet->buffer->data, packet->buffer->len, FALSE);
1405 /* Make copy of the original packet context, except for the actual
1406 data buffer, which we will here now fetch from the original buffer. */
1407 new_id = silc_packet_context_alloc();
1408 new_id->type = SILC_PACKET_NEW_ID;
1409 new_id->flags = packet->flags;
1410 new_id->src_id = packet->src_id;
1411 new_id->src_id_len = packet->src_id_len;
1412 new_id->src_id_type = packet->src_id_type;
1413 new_id->dst_id = packet->dst_id;
1414 new_id->dst_id_len = packet->dst_id_len;
1415 new_id->dst_id_type = packet->dst_id_type;
1417 idp = silc_buffer_alloc(256);
1418 new_id->buffer = idp;
1420 while (packet->buffer->len) {
1421 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1422 if ((id_len > packet->buffer->len) ||
1423 (id_len > idp->truelen))
1426 silc_buffer_pull_tail(idp, 4 + id_len);
1427 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1429 /* Process the New ID */
1430 silc_server_new_id_real(server, sock, new_id, FALSE);
1432 silc_buffer_push_tail(idp, 4 + id_len);
1433 silc_buffer_pull(packet->buffer, 4 + id_len);
1436 silc_buffer_free(idp);
1440 /* Received New Channel packet. Information about new channels in the
1441 network are distributed using this packet. Save the information about
1442 the new channel. This usually comes from router but also normal server
1443 can send this to notify channels it has when it connects to us. */
1445 void silc_server_new_channel(SilcServer server,
1446 SilcSocketConnection sock,
1447 SilcPacketContext *packet)
1450 SilcChannelID *channel_id;
1451 unsigned short channel_id_len;
1455 SILC_LOG_DEBUG(("Processing New Channel"));
1457 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1458 packet->src_id_type != SILC_ID_SERVER ||
1459 server->server_type == SILC_SERVER)
1463 ret = silc_buffer_unformat(packet->buffer,
1464 SILC_STR_UI16_STRING_ALLOC(&channel_name),
1465 SILC_STR_UI16_NSTRING_ALLOC(&id, &channel_id_len),
1469 silc_free(channel_name);
1475 /* Decode the channel ID */
1476 channel_id = silc_id_str2id(id, channel_id_len, SILC_ID_CHANNEL);
1480 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1481 /* Add the server to global list as it is coming from router. It
1482 cannot be our own channel as it is coming from router. */
1484 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1485 silc_id_render(channel_id, SILC_ID_CHANNEL),
1488 silc_idlist_add_channel(server->global_list, channel_name, 0, channel_id,
1489 server->router->connection, NULL, NULL);
1491 server->stat.channels++;
1493 /* The channel is coming from our server, thus it is in our cell
1494 we will add it to our local list. */
1495 SilcChannelEntry channel;
1498 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1499 silc_id_render(channel_id, SILC_ID_CHANNEL),
1502 /* Check that we don't already have this channel */
1503 channel = silc_idlist_find_channel_by_name(server->local_list,
1504 channel_name, NULL);
1506 channel = silc_idlist_find_channel_by_name(server->global_list,
1507 channel_name, NULL);
1509 /* If the channel does not exist, then create it. We create the channel
1510 with the channel ID provided by the server. This creates a new
1511 key to the channel as well that we will send to the server. */
1513 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1519 /* Send the new channel key to the server */
1520 chk = silc_channel_key_payload_encode(channel_id_len, id,
1521 strlen(channel->channel_key->
1523 channel->channel_key->cipher->name,
1524 channel->key_len / 8,
1526 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1527 chk->data, chk->len, FALSE);
1528 silc_buffer_free(chk);
1531 /* The channel exist by that name, check whether the ID's match.
1532 If they don't then we'll force the server to use the ID we have.
1533 We also create a new key for the channel. */
1535 if (SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1536 /* They don't match, send CHANNEL_CHANGE notify to the server to
1537 force the ID change. */
1538 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1539 silc_server_send_notify_channel_change(server, sock, FALSE,
1542 SILC_ID_CHANNEL_LEN);
1545 /* Create new key for the channel and send it to the server and
1546 everybody else possibly on the channel. */
1548 silc_server_create_channel_key(server, channel, 0);
1550 /* Send to the channel */
1551 silc_server_send_channel_key(server, sock, channel, FALSE);
1553 /* Send to the server */
1554 chk = silc_channel_key_payload_encode(channel_id_len, id,
1555 strlen(channel->channel_key->
1557 channel->channel_key->cipher->name,
1558 channel->key_len / 8,
1560 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1561 chk->data, chk->len, FALSE);
1562 silc_buffer_free(chk);
1564 /* Since the channel is coming from server and we also know about it
1565 then send the JOIN notify to the server so that it see's our
1566 users on the channel "joining" the channel. */
1574 /* Received New Channel List packet, list of New Channel List payloads inside
1575 one packet. Process the New Channel payloads one by one. */
1577 void silc_server_new_channel_list(SilcServer server,
1578 SilcSocketConnection sock,
1579 SilcPacketContext *packet)
1581 SilcPacketContext *new;
1583 unsigned short len1, len2;
1585 SILC_LOG_DEBUG(("Processing New Channel List"));
1587 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1588 packet->src_id_type != SILC_ID_SERVER ||
1589 server->server_type == SILC_SERVER)
1592 /* If the sender of this packet is server and we are router we need to
1593 broadcast this packet to other routers in the network. Broadcast
1594 this list packet instead of multiple New Channel packets. */
1595 if (!server->standalone && server->server_type == SILC_ROUTER &&
1596 sock->type == SILC_SOCKET_TYPE_SERVER &&
1597 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1598 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
1599 silc_server_packet_send(server, server->router->connection,
1601 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1602 packet->buffer->data, packet->buffer->len, FALSE);
1605 /* Make copy of the original packet context, except for the actual
1606 data buffer, which we will here now fetch from the original buffer. */
1607 new = silc_packet_context_alloc();
1608 new->type = SILC_PACKET_NEW_CHANNEL;
1609 new->flags = packet->flags;
1610 new->src_id = packet->src_id;
1611 new->src_id_len = packet->src_id_len;
1612 new->src_id_type = packet->src_id_type;
1613 new->dst_id = packet->dst_id;
1614 new->dst_id_len = packet->dst_id_len;
1615 new->dst_id_type = packet->dst_id_type;
1617 buffer = silc_buffer_alloc(512);
1618 new->buffer = buffer;
1620 while (packet->buffer->len) {
1621 SILC_GET16_MSB(len1, packet->buffer->data);
1622 if ((len1 > packet->buffer->len) ||
1623 (len1 > buffer->truelen))
1626 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
1627 if ((len2 > packet->buffer->len) ||
1628 (len2 > buffer->truelen))
1631 silc_buffer_pull_tail(buffer, 4 + len1 + len2);
1632 silc_buffer_put(buffer, packet->buffer->data, 4 + len1 + len2);
1634 /* Process the New Channel */
1635 silc_server_new_channel(server, sock, new);
1637 silc_buffer_push_tail(buffer, 4 + len1 + len2);
1638 silc_buffer_pull(packet->buffer, 4 + len1 + len2);
1641 silc_buffer_free(buffer);
1645 /* Received key agreement packet. This packet is never for us. It is to
1646 the client in the packet's destination ID. Sending of this sort of packet
1647 equals sending private message, ie. it is sent point to point from
1648 one client to another. */
1650 void silc_server_key_agreement(SilcServer server,
1651 SilcSocketConnection sock,
1652 SilcPacketContext *packet)
1655 SilcServerEntry router;
1656 SilcSocketConnection dst_sock;
1657 SilcClientEntry client;
1658 SilcIDListData idata;
1660 SILC_LOG_DEBUG(("Start"));
1662 if (packet->src_id_type != SILC_ID_CLIENT ||
1663 packet->dst_id_type != SILC_ID_CLIENT)
1666 if (!packet->dst_id)
1669 /* Decode destination Client ID */
1670 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
1672 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
1676 /* If the destination belongs to our server we don't have to route
1677 the message anywhere but to send it to the local destination. */
1678 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
1680 /* It exists, now deliver the message to the destination */
1681 dst_sock = (SilcSocketConnection)client->connection;
1683 /* If we are router and the client has router then the client is in
1684 our cell but not directly connected to us. */
1685 if (server->server_type == SILC_ROUTER && client->router) {
1686 /* We are of course in this case the client's router thus the real
1687 "router" of the client is the server who owns the client. Thus
1688 we will send the packet to that server. */
1689 router = (SilcServerEntry)client->router;
1690 idata = (SilcIDListData)router;
1691 silc_server_send_key_agreement(server, router->connection,
1698 /* Seems that client really is directly connected to us */
1699 idata = (SilcIDListData)client;
1700 silc_server_send_key_agreement(server, dst_sock,
1702 idata->hmac, packet);
1706 /* Destination belongs to someone not in this server. If we are normal
1707 server our action is to send the packet to our router. */
1708 if (server->server_type == SILC_SERVER && !server->standalone) {
1709 router = server->router;
1711 /* Send to primary route */
1713 dst_sock = (SilcSocketConnection)router->connection;
1714 idata = (SilcIDListData)router;
1715 silc_server_send_key_agreement(server, dst_sock,
1717 idata->hmac, packet);
1722 /* We are router and we will perform route lookup for the destination
1723 and send the packet to fastest route. */
1724 if (server->server_type == SILC_ROUTER && !server->standalone) {
1725 /* Check first that the ID is valid */
1726 client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
1728 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
1729 router = (SilcServerEntry)dst_sock->user_data;
1730 idata = (SilcIDListData)router;
1732 /* Get fastest route and send packet. */
1734 silc_server_send_key_agreement(server, dst_sock,
1736 idata->hmac, packet);