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)
58 /* If the packet is destined directly to a client then relay the packet
59 before processing it. */
60 if (packet->dst_id_type == SILC_ID_CLIENT) {
62 SilcSocketConnection dst_sock;
64 /* Get the route to the client */
65 dst_sock = silc_server_get_client_route(server, packet->dst_id,
66 packet->dst_id_len, &idata);
68 /* Relay the packet */
69 silc_server_relay_packet(server, dst_sock, idata->send_key,
70 idata->hmac, packet, TRUE);
73 /* If we are router and this packet is not already broadcast packet
74 we will broadcast it. The sending socket really cannot be router or
75 the router is buggy. If this packet is coming from router then it must
76 have the broadcast flag set already and we won't do anything. */
77 if (!server->standalone && server->server_type == SILC_ROUTER &&
78 sock->type == SILC_SOCKET_TYPE_SERVER &&
79 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
80 SILC_LOG_DEBUG(("Broadcasting received Notify packet"));
81 silc_server_packet_send(server, server->router->connection, packet->type,
82 packet->flags | SILC_PACKET_FLAG_BROADCAST,
83 packet->buffer->data, packet->buffer->len, FALSE);
86 payload = silc_notify_payload_parse(packet->buffer);
90 type = silc_notify_get_type(payload);
91 args = silc_notify_get_args(payload);
96 case SILC_NOTIFY_TYPE_JOIN:
98 * Distribute the notify to local clients on the channel
100 SILC_LOG_DEBUG(("JOIN notify"));
103 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
106 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
110 /* Get channel entry */
111 channel = silc_idlist_find_channel_by_id(server->local_list,
114 channel = silc_idlist_find_channel_by_id(server->global_list,
117 silc_free(channel_id);
121 silc_free(channel_id);
124 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
127 client_id = silc_id_payload_parse_id(tmp, tmp_len);
131 /* Send to channel */
132 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
133 FALSE, packet->buffer->data,
134 packet->buffer->len, FALSE);
136 /* If the the client is not in local list we check global list (ie. the
137 channel will be global channel) and if it does not exist then create
138 entry for the client. */
139 client = silc_idlist_find_client_by_id(server->local_list,
142 client = silc_idlist_find_client_by_id(server->global_list,
145 /* If router did not find the client the it is bogus */
146 if (server->server_type == SILC_ROUTER)
150 silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
151 silc_id_dup(client_id, SILC_ID_CLIENT),
152 sock->user_data, NULL);
154 silc_free(client_id);
160 /* Do not add client to channel if it is there already */
161 if (silc_server_client_on_channel(client, channel))
164 if (server->server_type == SILC_SERVER &&
165 sock->type == SILC_SOCKET_TYPE_ROUTER)
166 /* The channel is global now */
167 channel->global_users = TRUE;
169 /* JOIN the global client to the channel (local clients (if router
170 created the channel) is joined in the pending JOIN command). */
171 chl = silc_calloc(1, sizeof(*chl));
172 chl->client = client;
173 chl->channel = channel;
174 silc_list_add(channel->user_list, chl);
175 silc_list_add(client->channels, chl);
176 silc_free(client_id);
180 case SILC_NOTIFY_TYPE_LEAVE:
182 * Distribute the notify to local clients on the channel
184 SILC_LOG_DEBUG(("LEAVE notify"));
186 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
187 packet->dst_id_type);
191 /* Get channel entry */
192 channel = silc_idlist_find_channel_by_id(server->local_list,
195 channel = silc_idlist_find_channel_by_id(server->global_list,
198 silc_free(channel_id);
204 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
206 silc_free(channel_id);
209 client_id = silc_id_payload_parse_id(tmp, tmp_len);
211 silc_free(channel_id);
215 /* Send to channel */
216 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
217 FALSE, packet->buffer->data,
218 packet->buffer->len, FALSE);
220 /* Get client entry */
221 client = silc_idlist_find_client_by_id(server->global_list,
224 client = silc_idlist_find_client_by_id(server->local_list,
227 silc_free(client_id);
228 silc_free(channel_id);
232 silc_free(client_id);
234 /* Remove the user from channel */
235 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
238 case SILC_NOTIFY_TYPE_SIGNOFF:
240 * Distribute the notify to local clients on the channel
242 SILC_LOG_DEBUG(("SIGNOFF notify"));
245 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
248 client_id = silc_id_payload_parse_id(tmp, tmp_len);
252 /* Get client entry */
253 client = silc_idlist_find_client_by_id(server->global_list,
256 client = silc_idlist_find_client_by_id(server->local_list,
259 silc_free(client_id);
263 silc_free(client_id);
265 /* Get signoff message */
266 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
270 /* Remove the client from all channels */
271 silc_server_remove_from_channels(server, NULL, client, TRUE, tmp, TRUE);
273 /* Remove the client entry */
274 if (!silc_idlist_del_client(server->global_list, client))
275 silc_idlist_del_client(server->local_list, client);
278 case SILC_NOTIFY_TYPE_TOPIC_SET:
280 * Distribute the notify to local clients on the channel
283 SILC_LOG_DEBUG(("TOPIC SET notify"));
285 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
286 packet->dst_id_type);
290 /* Get channel entry */
291 channel = silc_idlist_find_channel_by_id(server->local_list,
294 channel = silc_idlist_find_channel_by_id(server->global_list,
297 silc_free(channel_id);
303 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
305 silc_free(channel_id);
310 silc_free(channel->topic);
311 channel->topic = silc_calloc(tmp_len, sizeof(*channel->topic));
312 memcpy(channel->topic, tmp, tmp_len);
314 /* Send the same notify to the channel */
315 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
316 FALSE, packet->buffer->data,
317 packet->buffer->len, FALSE);
318 silc_free(channel_id);
321 case SILC_NOTIFY_TYPE_NICK_CHANGE:
324 * Distribute the notify to local clients on the channel
326 unsigned char *id, *id2;
328 SILC_LOG_DEBUG(("NICK CHANGE notify"));
330 /* Get old client ID */
331 id = silc_argument_get_arg_type(args, 1, &tmp_len);
334 client_id = silc_id_payload_parse_id(id, tmp_len);
338 /* Get new client ID */
339 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
342 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
346 SILC_LOG_DEBUG(("Old Client ID id(%s)",
347 silc_id_render(client_id, SILC_ID_CLIENT)));
348 SILC_LOG_DEBUG(("New Client ID id(%s)",
349 silc_id_render(client_id2, SILC_ID_CLIENT)));
351 /* Replace the Client ID */
352 client = silc_idlist_replace_client_id(server->global_list, client_id,
355 client = silc_idlist_replace_client_id(server->local_list, client_id,
359 /* The nickname is not valid anymore, set it NULL. This causes that
360 the nickname will be queried if someone wants to know it. */
361 if (client->nickname)
362 silc_free(client->nickname);
363 client->nickname = NULL;
365 /* Send the NICK_CHANGE notify type to local clients on the channels
366 this client is joined to. */
367 silc_server_send_notify_on_channels(server, NULL, client,
368 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
373 silc_free(client_id);
375 silc_free(client_id2);
379 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
381 * Distribute the notify to local clients on the channel
384 SILC_LOG_DEBUG(("CMODE CHANGE notify"));
386 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
387 packet->dst_id_type);
391 /* Get channel entry */
392 channel = silc_idlist_find_channel_by_id(server->local_list,
395 channel = silc_idlist_find_channel_by_id(server->global_list,
398 silc_free(channel_id);
403 /* Send the same notify to the channel */
404 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
405 FALSE, packet->buffer->data,
406 packet->buffer->len, FALSE);
409 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
411 silc_free(channel_id);
415 SILC_GET32_MSB(mode, tmp);
418 channel->mode = mode;
419 silc_free(channel_id);
422 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
424 * Distribute the notify to local clients on the channel
427 SILC_LOG_DEBUG(("CUMODE CHANGE notify"));
429 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
430 packet->dst_id_type);
434 /* Get channel entry */
435 channel = silc_idlist_find_channel_by_id(server->local_list,
438 channel = silc_idlist_find_channel_by_id(server->global_list,
441 silc_free(channel_id);
447 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
449 silc_free(channel_id);
453 SILC_GET32_MSB(mode, tmp);
455 /* Get target client */
456 tmp = silc_argument_get_arg_type(args, 3, &tmp_len);
459 client_id = silc_id_payload_parse_id(tmp, tmp_len);
463 /* Get client entry */
464 client = silc_idlist_find_client_by_id(server->global_list,
467 client = silc_idlist_find_client_by_id(server->local_list,
470 silc_free(client_id);
474 silc_free(client_id);
476 /* Get entry to the channel user list */
477 silc_list_start(channel->user_list);
478 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
479 if (chl->client == client) {
480 /* Change the mode */
485 /* Send the same notify to the channel */
486 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
487 FALSE, packet->buffer->data,
488 packet->buffer->len, FALSE);
489 silc_free(channel_id);
492 case SILC_NOTIFY_TYPE_INVITE:
493 SILC_LOG_DEBUG(("INVITE notify (not-impl XXX)"));
496 case SILC_NOTIFY_TYPE_CHANNEL_CHANGE:
497 SILC_LOG_DEBUG(("CHANNEL CHANGE notify (not-impl XXX)"));
500 case SILC_NOTIFY_TYPE_SERVER_SIGNOFF:
501 SILC_LOG_DEBUG(("SERVER SIGNOFF notify (not-impl XXX)"));
504 case SILC_NOTIFY_TYPE_KICKED:
506 * Distribute the notify to local clients on the channel
509 SILC_LOG_DEBUG(("KICKED notify"));
511 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
512 packet->dst_id_type);
516 /* Get channel entry */
517 channel = silc_idlist_find_channel_by_id(server->local_list,
520 channel = silc_idlist_find_channel_by_id(server->global_list,
523 silc_free(channel_id);
527 silc_free(channel_id);
530 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
533 client_id = silc_id_payload_parse_id(tmp, tmp_len);
537 /* Send to channel */
538 silc_server_packet_send_to_channel(server, sock, channel, packet->type,
539 FALSE, packet->buffer->data,
540 packet->buffer->len, FALSE);
542 /* If the the client is not in local list we check global list */
543 client = silc_idlist_find_client_by_id(server->local_list,
546 client = silc_idlist_find_client_by_id(server->global_list,
549 silc_free(client_id);
554 /* Remove the client from channel */
555 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
559 case SILC_NOTIFY_TYPE_KILLED:
562 * Distribute the notify to local clients on channels
567 SILC_LOG_DEBUG(("KILLED notify"));
570 id = silc_argument_get_arg_type(args, 1, &id_len);
573 client_id = silc_id_payload_parse_id(id, id_len);
577 /* If the the client is not in local list we check global list */
578 client = silc_idlist_find_client_by_id(server->local_list,
581 client = silc_idlist_find_client_by_id(server->global_list,
584 silc_free(client_id);
588 silc_free(client_id);
590 /* If the client is one of ours, then close the connection to the
591 client now. This removes the client from all channels as well. */
592 if (packet->dst_id_type == SILC_ID_CLIENT && client->data.registered &&
593 client->connection) {
594 sock = client->connection;
595 silc_server_free_client_data(server, NULL, client, FALSE, NULL);
596 silc_server_close_connection(server, sock);
601 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
605 /* Send the notify to local clients on the channels except to the
606 client who is killed. */
607 silc_server_send_notify_on_channels(server, client, client,
608 SILC_NOTIFY_TYPE_KILLED,
613 /* Remove the client from all channels */
614 silc_server_remove_from_channels(server, NULL, client, FALSE, NULL,
620 /* Ignore rest of the notify types for now */
621 case SILC_NOTIFY_TYPE_NONE:
622 case SILC_NOTIFY_TYPE_MOTD:
629 silc_notify_payload_free(payload);
632 void silc_server_notify_list(SilcServer server,
633 SilcSocketConnection sock,
634 SilcPacketContext *packet)
636 SilcPacketContext *new;
640 SILC_LOG_DEBUG(("Processing New Notify List"));
642 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
643 packet->src_id_type != SILC_ID_SERVER)
646 /* Make copy of the original packet context, except for the actual
647 data buffer, which we will here now fetch from the original buffer. */
648 new = silc_packet_context_alloc();
649 new->type = SILC_PACKET_NOTIFY;
650 new->flags = packet->flags;
651 new->src_id = packet->src_id;
652 new->src_id_len = packet->src_id_len;
653 new->src_id_type = packet->src_id_type;
654 new->dst_id = packet->dst_id;
655 new->dst_id_len = packet->dst_id_len;
656 new->dst_id_type = packet->dst_id_type;
658 buffer = silc_buffer_alloc(1024);
659 new->buffer = buffer;
661 while (packet->buffer->len) {
662 SILC_GET16_MSB(len, packet->buffer->data + 2);
663 if (len > packet->buffer->len)
666 if (len > buffer->truelen) {
667 silc_buffer_free(buffer);
668 buffer = silc_buffer_alloc(1024 + len);
671 silc_buffer_pull_tail(buffer, len);
672 silc_buffer_put(buffer, packet->buffer->data, len);
674 /* Process the Notify */
675 silc_server_notify(server, sock, new);
677 silc_buffer_push_tail(buffer, len);
678 silc_buffer_pull(packet->buffer, len);
681 silc_buffer_free(buffer);
685 /* Received private message. This resolves the destination of the message
686 and sends the packet. This is used by both server and router. If the
687 destination is our locally connected client this sends the packet to
688 the client. This may also send the message for further routing if
689 the destination is not in our server (or router). */
691 void silc_server_private_message(SilcServer server,
692 SilcSocketConnection sock,
693 SilcPacketContext *packet)
695 SilcSocketConnection dst_sock;
696 SilcIDListData idata;
698 SILC_LOG_DEBUG(("Start"));
700 if (packet->src_id_type != SILC_ID_CLIENT ||
701 packet->dst_id_type != SILC_ID_CLIENT)
707 /* Get the route to the client */
708 dst_sock = silc_server_get_client_route(server, packet->dst_id,
709 packet->dst_id_len, &idata);
713 /* Send the private message */
714 silc_server_send_private_message(server, dst_sock, idata->send_key,
715 idata->hmac, packet);
718 /* Received private message key packet.. This packet is never for us. It is to
719 the client in the packet's destination ID. Sending of this sort of packet
720 equals sending private message, ie. it is sent point to point from
721 one client to another. */
723 void silc_server_private_message_key(SilcServer server,
724 SilcSocketConnection sock,
725 SilcPacketContext *packet)
727 SilcSocketConnection dst_sock;
728 SilcIDListData idata;
730 SILC_LOG_DEBUG(("Start"));
732 if (packet->src_id_type != SILC_ID_CLIENT ||
733 packet->dst_id_type != SILC_ID_CLIENT)
739 /* Get the route to the client */
740 dst_sock = silc_server_get_client_route(server, packet->dst_id,
741 packet->dst_id_len, &idata);
745 /* Relay the packet */
746 silc_server_relay_packet(server, dst_sock, idata->send_key,
747 idata->hmac, packet, FALSE);
750 /* Processes incoming command reply packet. The command reply packet may
751 be destined to one of our clients or it may directly for us. We will
752 call the command reply routine after processing the packet. */
754 void silc_server_command_reply(SilcServer server,
755 SilcSocketConnection sock,
756 SilcPacketContext *packet)
758 SilcBuffer buffer = packet->buffer;
759 SilcClientEntry client = NULL;
760 SilcSocketConnection dst_sock;
761 SilcIDListData idata;
762 SilcClientID *id = NULL;
764 SILC_LOG_DEBUG(("Start"));
766 /* Source must be server or router */
767 if (packet->src_id_type != SILC_ID_SERVER &&
768 sock->type != SILC_SOCKET_TYPE_ROUTER)
771 if (packet->dst_id_type == SILC_ID_CHANNEL)
774 if (packet->dst_id_type == SILC_ID_CLIENT) {
775 /* Destination must be one of ours */
776 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
779 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
781 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
787 if (packet->dst_id_type == SILC_ID_SERVER) {
788 /* For now this must be for us */
789 if (SILC_ID_SERVER_COMPARE(packet->dst_id, server->id_string)) {
790 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
795 /* Execute command reply locally for the command */
796 silc_server_command_reply_process(server, sock, buffer);
798 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
799 /* Relay the packet to the client */
801 dst_sock = (SilcSocketConnection)client->connection;
802 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
803 + packet->dst_id_len + packet->padlen);
805 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
806 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
808 idata = (SilcIDListData)client;
811 silc_packet_encrypt(idata->send_key, idata->hmac, dst_sock->outbuf,
814 /* Send the packet */
815 silc_server_packet_send_real(server, dst_sock, TRUE);
821 /* Process received channel message. The message can be originated from
824 void silc_server_channel_message(SilcServer server,
825 SilcSocketConnection sock,
826 SilcPacketContext *packet)
828 SilcChannelEntry channel = NULL;
829 SilcChannelClientEntry chl;
830 SilcChannelID *id = NULL;
833 SILC_LOG_DEBUG(("Processing channel message"));
836 if (packet->dst_id_type != SILC_ID_CHANNEL) {
837 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
841 /* Find channel entry */
842 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
845 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
847 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
849 SILC_LOG_DEBUG(("Could not find channel"));
854 /* See that this client is on the channel. If the message is coming
855 from router we won't do the check as the message is from client that
856 we don't know about. Also, if the original sender is not client
857 (as it can be server as well) we don't do the check. */
858 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
859 packet->src_id_type);
862 if (sock->type != SILC_SOCKET_TYPE_ROUTER &&
863 packet->src_id_type == SILC_ID_CLIENT) {
864 silc_list_start(channel->user_list);
865 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
866 if (chl->client && !SILC_ID_CLIENT_COMPARE(chl->client->id, sender))
869 if (chl == SILC_LIST_END) {
870 SILC_LOG_DEBUG(("Client not on channel"));
875 /* If we are router and the packet came from router and private key
876 has not been set for the channel then we must encrypt the packet
877 as it was decrypted with the session key shared between us and the
878 router which sent it. This is so, because cells does not share the
880 if (server->server_type == SILC_ROUTER &&
881 sock->type == SILC_SOCKET_TYPE_ROUTER &&
882 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
884 unsigned int iv_len, i, data_len;
886 iv_len = silc_cipher_get_block_len(channel->channel_key);
887 if (channel->iv[0] == '\0')
888 for (i = 0; i < iv_len; i++) channel->iv[i] =
889 silc_rng_get_byte(server->rng);
891 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
893 /* Encode new payload. This encrypts it also. */
894 SILC_GET16_MSB(data_len, packet->buffer->data);
895 chp = silc_channel_payload_encode(data_len, packet->buffer->data + 2,
897 channel->channel_key,
898 channel->hmac, server->rng);
899 silc_buffer_put(packet->buffer, chp->data, chp->len);
900 silc_buffer_free(chp);
903 /* Distribute the packet to our local clients. This will send the
904 packet for further routing as well, if needed. */
905 silc_server_packet_relay_to_channel(server, sock, channel, sender,
907 packet->buffer->data,
908 packet->buffer->len, FALSE);
917 /* Received channel key packet. We distribute the key to all of our locally
918 connected clients on the channel. */
920 void silc_server_channel_key(SilcServer server,
921 SilcSocketConnection sock,
922 SilcPacketContext *packet)
924 SilcBuffer buffer = packet->buffer;
925 SilcChannelEntry channel;
927 if (packet->src_id_type != SILC_ID_SERVER)
930 /* Save the channel key */
931 channel = silc_server_save_channel_key(server, buffer, NULL);
935 /* Distribute the key to everybody who is on the channel. If we are router
936 we will also send it to locally connected servers. */
937 silc_server_send_channel_key(server, sock, channel, FALSE);
940 /* Received New Client packet and processes it. Creates Client ID for the
941 client. Client becomes registered after calling this functions. */
943 SilcClientEntry silc_server_new_client(SilcServer server,
944 SilcSocketConnection sock,
945 SilcPacketContext *packet)
947 SilcBuffer buffer = packet->buffer;
948 SilcClientEntry client;
949 SilcIDCacheEntry cache;
950 SilcClientID *client_id;
952 SilcIDListData idata;
953 char *username = NULL, *realname = NULL, *id_string;
956 SILC_LOG_DEBUG(("Creating new client"));
958 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
961 /* Take client entry */
962 client = (SilcClientEntry)sock->user_data;
963 idata = (SilcIDListData)client;
965 /* Fetch the old client cache entry so that we can update it. */
966 if (!silc_idcache_find_by_context(server->local_list->clients,
967 sock->user_data, &cache)) {
968 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
972 /* Parse incoming packet */
973 ret = silc_buffer_unformat(buffer,
974 SILC_STR_UI16_STRING_ALLOC(&username),
975 SILC_STR_UI16_STRING_ALLOC(&realname),
989 silc_server_disconnect_remote(server, sock, "Server closed connection: "
990 "Incomplete client information");
994 /* Create Client ID */
995 silc_id_create_client_id(server->id, server->rng, server->md5hash,
996 username, &client_id);
998 if (strlen(username) > 128)
999 username[127] = '\0';
1001 /* Update client entry */
1002 idata->registered = TRUE;
1003 client->nickname = strdup(username);
1004 client->username = username;
1005 client->userinfo = realname ? realname : strdup(" ");
1006 client->id = client_id;
1008 /* Update the cache entry */
1009 cache->id = (void *)client_id;
1010 cache->type = SILC_ID_CLIENT;
1011 cache->data = username;
1012 silc_idcache_sort_by_data(server->local_list->clients);
1014 /* Notify our router about new client on the SILC network */
1015 if (!server->standalone)
1016 silc_server_send_new_id(server, (SilcSocketConnection)
1017 server->router->connection,
1018 server->server_type == SILC_ROUTER ? TRUE : FALSE,
1019 client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
1021 /* Send the new client ID to the client. */
1022 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
1023 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
1024 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
1025 silc_buffer_format(reply,
1026 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
1027 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
1028 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
1030 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
1031 reply->data, reply->len, FALSE);
1032 silc_free(id_string);
1033 silc_buffer_free(reply);
1035 /* Send some nice info to the client */
1036 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1037 ("Welcome to the SILC Network %s@%s",
1038 username, sock->hostname));
1039 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1040 ("Your host is %s, running version %s",
1041 server->config->server_info->server_name,
1043 if (server->server_type == SILC_ROUTER) {
1044 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1045 ("There are %d clients on %d servers in SILC "
1046 "Network", server->stat.clients,
1047 server->stat.servers + 1));
1048 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1049 ("There are %d clients on %d server in our cell",
1050 server->stat.cell_clients,
1051 server->stat.cell_servers + 1));
1052 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1053 ("I have %d clients, %d channels, %d servers and "
1055 server->stat.my_clients,
1056 server->stat.my_channels,
1057 server->stat.my_servers,
1058 server->stat.my_routers));
1059 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1060 ("%d server operators and %d router operators "
1062 server->stat.my_server_ops,
1063 server->stat.my_router_ops));
1065 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1066 ("I have %d clients and %d channels formed",
1067 server->stat.my_clients,
1068 server->stat.my_channels));
1069 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1070 ("%d operators online",
1071 server->stat.my_server_ops));
1073 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1074 ("Your connection is secured with %s cipher, "
1075 "key length %d bits",
1076 idata->send_key->cipher->name,
1077 idata->send_key->cipher->key_len));
1078 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
1079 ("Your current nickname is %s",
1083 silc_server_send_motd(server, sock);
1088 /* Create new server. This processes received New Server packet and
1089 saves the received Server ID. The server is our locally connected
1090 server thus we save all the information and save it to local list.
1091 This funtion can be used by both normal server and router server.
1092 If normal server uses this it means that its router has connected
1093 to the server. If router uses this it means that one of the cell's
1094 servers is connected to the router. */
1096 SilcServerEntry silc_server_new_server(SilcServer server,
1097 SilcSocketConnection sock,
1098 SilcPacketContext *packet)
1100 SilcBuffer buffer = packet->buffer;
1101 SilcServerEntry new_server;
1102 SilcIDCacheEntry cache;
1103 SilcServerID *server_id;
1104 SilcIDListData idata;
1105 unsigned char *server_name, *id_string;
1106 unsigned short id_len;
1109 SILC_LOG_DEBUG(("Creating new server"));
1111 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
1112 sock->type != SILC_SOCKET_TYPE_ROUTER)
1115 /* Take server entry */
1116 new_server = (SilcServerEntry)sock->user_data;
1117 idata = (SilcIDListData)new_server;
1119 /* Fetch the old server cache entry so that we can update it. */
1120 if (!silc_idcache_find_by_context(server->local_list->servers,
1121 sock->user_data, &cache)) {
1122 SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
1126 /* Parse the incoming packet */
1127 ret = silc_buffer_unformat(buffer,
1128 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
1129 SILC_STR_UI16_STRING_ALLOC(&server_name),
1133 silc_free(id_string);
1135 silc_free(server_name);
1139 if (id_len > buffer->len) {
1140 silc_free(id_string);
1141 silc_free(server_name);
1146 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
1148 silc_free(id_string);
1149 silc_free(server_name);
1152 silc_free(id_string);
1154 /* Update client entry */
1155 idata->registered = TRUE;
1156 new_server->server_name = server_name;
1157 new_server->id = server_id;
1159 /* Update the cache entry */
1160 cache->id = (void *)server_id;
1161 cache->type = SILC_ID_SERVER;
1162 cache->data = server_name;
1163 silc_idcache_sort_by_data(server->local_list->servers);
1165 /* Distribute the information about new server in the SILC network
1166 to our router. If we are normal server we won't send anything
1167 since this connection must be our router connection. */
1168 if (server->server_type == SILC_ROUTER && !server->standalone &&
1169 server->router->connection != sock)
1170 silc_server_send_new_id(server, server->router->connection,
1171 TRUE, new_server->id, SILC_ID_SERVER,
1172 SILC_ID_SERVER_LEN);
1174 if (server->server_type == SILC_ROUTER)
1175 server->stat.cell_servers++;
1180 /* Processes incoming New ID packet. New ID Payload is used to distribute
1181 information about newly registered clients and servers. */
1183 static void silc_server_new_id_real(SilcServer server,
1184 SilcSocketConnection sock,
1185 SilcPacketContext *packet,
1188 SilcBuffer buffer = packet->buffer;
1190 SilcServerEntry router;
1191 SilcSocketConnection router_sock;
1194 unsigned char *hash = NULL;
1197 SILC_LOG_DEBUG(("Processing new ID"));
1199 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1200 server->server_type == SILC_SERVER ||
1201 packet->src_id_type != SILC_ID_SERVER)
1204 idp = silc_id_payload_parse(buffer);
1208 id_type = silc_id_payload_get_type(idp);
1210 /* Normal server cannot have other normal server connections */
1211 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
1214 id = silc_id_payload_get_id(idp);
1218 /* If the sender of this packet is server and we are router we need to
1219 broadcast this packet to other routers in the network. */
1220 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1221 sock->type == SILC_SOCKET_TYPE_SERVER &&
1222 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1223 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
1224 silc_server_packet_send(server, server->router->connection,
1226 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1227 buffer->data, buffer->len, FALSE);
1230 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1231 id_list = server->local_list;
1233 id_list = server->global_list;
1236 router = sock->user_data;
1239 case SILC_ID_CLIENT:
1241 SilcClientEntry entry;
1243 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
1244 silc_id_render(id, SILC_ID_CLIENT),
1245 sock->type == SILC_SOCKET_TYPE_SERVER ?
1246 "Server" : "Router", sock->hostname));
1248 /* As a router we keep information of all global information in our
1249 global list. Cell wide information however is kept in the local
1250 list. The client is put to global list and we will take the hash
1251 value of the Client ID and save it to the ID Cache system for fast
1252 searching in the future. */
1253 hash = silc_calloc(sizeof(((SilcClientID *)id)->hash),
1254 sizeof(unsigned char));
1255 memcpy(hash, ((SilcClientID *)id)->hash,
1256 sizeof(((SilcClientID *)id)->hash));
1257 entry = silc_idlist_add_client(id_list, hash, NULL, NULL, id,
1259 entry->nickname = NULL;
1261 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1262 server->stat.cell_clients++;
1263 server->stat.clients++;
1266 /* XXX Adding two ID's with same IP number replaces the old entry thus
1267 gives wrong route. Thus, now disabled until figured out a better way
1268 to do this or when removed the whole thing. This could be removed
1269 because entry->router->connection gives always the most optimal route
1270 for the ID anyway (unless new routes (faster perhaps) are established
1271 after receiving this ID, this we don't know however). */
1272 /* Add route cache for this ID */
1273 silc_server_route_add(silc_server_route_hash(
1274 ((SilcClientID *)id)->ip.s_addr,
1275 server->id->port), ((SilcClientID *)id)->ip.s_addr,
1281 case SILC_ID_SERVER:
1282 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
1283 silc_id_render(id, SILC_ID_SERVER),
1284 sock->type == SILC_SOCKET_TYPE_SERVER ?
1285 "Server" : "Router", sock->hostname));
1287 /* As a router we keep information of all global information in our global
1288 list. Cell wide information however is kept in the local list. */
1289 silc_idlist_add_server(id_list, NULL, 0, id, router, router_sock);
1291 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1292 server->stat.cell_servers++;
1293 server->stat.servers++;
1296 /* Add route cache for this ID */
1297 silc_server_route_add(silc_server_route_hash(
1298 ((SilcServerID *)id)->ip.s_addr,
1299 ((SilcServerID *)id)->port),
1300 ((SilcServerID *)id)->ip.s_addr,
1305 case SILC_ID_CHANNEL:
1306 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
1314 silc_id_payload_free(idp);
1318 /* Processes incoming New ID packet. New ID Payload is used to distribute
1319 information about newly registered clients and servers. */
1321 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
1322 SilcPacketContext *packet)
1324 silc_server_new_id_real(server, sock, packet, TRUE);
1327 /* Receoved New Id List packet, list of New ID payloads inside one
1328 packet. Process the New ID payloads one by one. */
1330 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
1331 SilcPacketContext *packet)
1333 SilcPacketContext *new_id;
1335 unsigned short id_len;
1337 SILC_LOG_DEBUG(("Processing New ID List"));
1339 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1340 packet->src_id_type != SILC_ID_SERVER)
1343 /* If the sender of this packet is server and we are router we need to
1344 broadcast this packet to other routers in the network. Broadcast
1345 this list packet instead of multiple New ID packets. */
1346 if (!server->standalone && server->server_type == SILC_ROUTER &&
1347 sock->type == SILC_SOCKET_TYPE_SERVER &&
1348 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1349 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
1350 silc_server_packet_send(server, server->router->connection,
1352 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1353 packet->buffer->data, packet->buffer->len, FALSE);
1356 /* Make copy of the original packet context, except for the actual
1357 data buffer, which we will here now fetch from the original buffer. */
1358 new_id = silc_packet_context_alloc();
1359 new_id->type = SILC_PACKET_NEW_ID;
1360 new_id->flags = packet->flags;
1361 new_id->src_id = packet->src_id;
1362 new_id->src_id_len = packet->src_id_len;
1363 new_id->src_id_type = packet->src_id_type;
1364 new_id->dst_id = packet->dst_id;
1365 new_id->dst_id_len = packet->dst_id_len;
1366 new_id->dst_id_type = packet->dst_id_type;
1368 idp = silc_buffer_alloc(256);
1369 new_id->buffer = idp;
1371 while (packet->buffer->len) {
1372 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
1373 if ((id_len > packet->buffer->len) ||
1374 (id_len > idp->truelen))
1377 silc_buffer_pull_tail(idp, 4 + id_len);
1378 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
1380 /* Process the New ID */
1381 silc_server_new_id_real(server, sock, new_id, FALSE);
1383 silc_buffer_push_tail(idp, 4 + id_len);
1384 silc_buffer_pull(packet->buffer, 4 + id_len);
1387 silc_buffer_free(idp);
1391 /* Received New Channel packet. Information about new channels in the
1392 network are distributed using this packet. Save the information about
1393 the new channel. This usually comes from router but also normal server
1394 can send this to notify channels it has when it connects to us. */
1396 void silc_server_new_channel(SilcServer server,
1397 SilcSocketConnection sock,
1398 SilcPacketContext *packet)
1401 SilcChannelID *channel_id;
1402 unsigned short channel_id_len;
1406 SILC_LOG_DEBUG(("Processing New Channel"));
1408 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1409 packet->src_id_type != SILC_ID_SERVER ||
1410 server->server_type == SILC_SERVER)
1414 ret = silc_buffer_unformat(packet->buffer,
1415 SILC_STR_UI16_STRING_ALLOC(&channel_name),
1416 SILC_STR_UI16_NSTRING_ALLOC(&id, &channel_id_len),
1420 silc_free(channel_name);
1426 /* Decode the channel ID */
1427 channel_id = silc_id_str2id(id, channel_id_len, SILC_ID_CHANNEL);
1431 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
1432 /* Add the server to global list as it is coming from router. It
1433 cannot be our own channel as it is coming from router. */
1435 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
1436 silc_id_render(channel_id, SILC_ID_CHANNEL),
1439 silc_idlist_add_channel(server->global_list, channel_name, 0, channel_id,
1440 server->router->connection, NULL, NULL);
1442 server->stat.channels++;
1444 /* The channel is coming from our server, thus it is in our cell
1445 we will add it to our local list. */
1446 SilcChannelEntry channel;
1449 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
1450 silc_id_render(channel_id, SILC_ID_CHANNEL),
1453 /* Check that we don't already have this channel */
1454 channel = silc_idlist_find_channel_by_name(server->local_list,
1455 channel_name, NULL);
1457 channel = silc_idlist_find_channel_by_name(server->global_list,
1458 channel_name, NULL);
1460 /* If the channel does not exist, then create it. We create the channel
1461 with the channel ID provided by the server. This creates a new
1462 key to the channel as well that we will send to the server. */
1464 channel = silc_server_create_new_channel_with_id(server, NULL, NULL,
1470 /* Send the new channel key to the server */
1471 chk = silc_channel_key_payload_encode(channel_id_len, id,
1472 strlen(channel->channel_key->
1474 channel->channel_key->cipher->name,
1475 channel->key_len / 8,
1477 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1478 chk->data, chk->len, FALSE);
1479 silc_buffer_free(chk);
1482 /* The channel exist by that name, check whether the ID's match.
1483 If they don't then we'll force the server to use the ID we have.
1484 We also create a new key for the channel. */
1486 if (SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
1487 /* They don't match, send CHANNEL_CHANGE notify to the server to
1488 force the ID change. */
1489 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
1490 silc_server_send_notify_channel_change(server, sock, FALSE,
1493 SILC_ID_CHANNEL_LEN);
1496 /* Create new key for the channel and send it to the server and
1497 everybody else possibly on the channel. */
1499 silc_server_create_channel_key(server, channel, 0);
1501 /* Send to the channel */
1502 silc_server_send_channel_key(server, sock, channel, FALSE);
1504 /* Send to the server */
1505 chk = silc_channel_key_payload_encode(channel_id_len, id,
1506 strlen(channel->channel_key->
1508 channel->channel_key->cipher->name,
1509 channel->key_len / 8,
1511 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
1512 chk->data, chk->len, FALSE);
1513 silc_buffer_free(chk);
1515 /* Since the channel is coming from server and we also know about it
1516 then send the JOIN notify to the server so that it see's our
1517 users on the channel "joining" the channel. */
1525 /* Received New Channel List packet, list of New Channel List payloads inside
1526 one packet. Process the New Channel payloads one by one. */
1528 void silc_server_new_channel_list(SilcServer server,
1529 SilcSocketConnection sock,
1530 SilcPacketContext *packet)
1532 SilcPacketContext *new;
1534 unsigned short len1, len2;
1536 SILC_LOG_DEBUG(("Processing New Channel List"));
1538 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1539 packet->src_id_type != SILC_ID_SERVER ||
1540 server->server_type == SILC_SERVER)
1543 /* If the sender of this packet is server and we are router we need to
1544 broadcast this packet to other routers in the network. Broadcast
1545 this list packet instead of multiple New Channel packets. */
1546 if (!server->standalone && server->server_type == SILC_ROUTER &&
1547 sock->type == SILC_SOCKET_TYPE_SERVER &&
1548 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1549 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
1550 silc_server_packet_send(server, server->router->connection,
1552 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1553 packet->buffer->data, packet->buffer->len, FALSE);
1556 /* Make copy of the original packet context, except for the actual
1557 data buffer, which we will here now fetch from the original buffer. */
1558 new = silc_packet_context_alloc();
1559 new->type = SILC_PACKET_NEW_CHANNEL;
1560 new->flags = packet->flags;
1561 new->src_id = packet->src_id;
1562 new->src_id_len = packet->src_id_len;
1563 new->src_id_type = packet->src_id_type;
1564 new->dst_id = packet->dst_id;
1565 new->dst_id_len = packet->dst_id_len;
1566 new->dst_id_type = packet->dst_id_type;
1568 buffer = silc_buffer_alloc(512);
1569 new->buffer = buffer;
1571 while (packet->buffer->len) {
1572 SILC_GET16_MSB(len1, packet->buffer->data);
1573 if ((len1 > packet->buffer->len) ||
1574 (len1 > buffer->truelen))
1577 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
1578 if ((len2 > packet->buffer->len) ||
1579 (len2 > buffer->truelen))
1582 silc_buffer_pull_tail(buffer, 4 + len1 + len2);
1583 silc_buffer_put(buffer, packet->buffer->data, 4 + len1 + len2);
1585 /* Process the New Channel */
1586 silc_server_new_channel(server, sock, new);
1588 silc_buffer_push_tail(buffer, 4 + len1 + len2);
1589 silc_buffer_pull(packet->buffer, 4 + len1 + len2);
1592 silc_buffer_free(buffer);
1596 /* Received key agreement packet. This packet is never for us. It is to
1597 the client in the packet's destination ID. Sending of this sort of packet
1598 equals sending private message, ie. it is sent point to point from
1599 one client to another. */
1601 void silc_server_key_agreement(SilcServer server,
1602 SilcSocketConnection sock,
1603 SilcPacketContext *packet)
1605 SilcSocketConnection dst_sock;
1606 SilcIDListData idata;
1608 SILC_LOG_DEBUG(("Start"));
1610 if (packet->src_id_type != SILC_ID_CLIENT ||
1611 packet->dst_id_type != SILC_ID_CLIENT)
1614 if (!packet->dst_id)
1617 /* Get the route to the client */
1618 dst_sock = silc_server_get_client_route(server, packet->dst_id,
1619 packet->dst_id_len, &idata);
1623 /* Relay the packet */
1624 silc_server_relay_packet(server, dst_sock, idata->send_key,
1625 idata->hmac, packet, FALSE);