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 private message. This resolves the destination of the message
31 and sends the packet. This is used by both server and router. If the
32 destination is our locally connected client this sends the packet to
33 the client. This may also send the message for further routing if
34 the destination is not in our server (or router). */
36 void silc_server_private_message(SilcServer server,
37 SilcSocketConnection sock,
38 SilcPacketContext *packet)
41 SilcServerEntry router;
42 SilcSocketConnection dst_sock;
43 SilcClientEntry client;
46 SILC_LOG_DEBUG(("Start"));
51 /* Decode destination Client ID */
52 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
54 SILC_LOG_ERROR(("Could not decode destination Client ID, dropped"));
58 /* If the destination belongs to our server we don't have to route
59 the message anywhere but to send it to the local destination. */
60 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
62 /* It exists, now deliver the message to the destination */
63 dst_sock = (SilcSocketConnection)client->connection;
65 /* If we are router and the client has router then the client is in
66 our cell but not directly connected to us. */
67 if (server->server_type == SILC_ROUTER && client->router) {
68 /* We are of course in this case the client's router thus the real
69 "router" of the client is the server who owns the client. Thus
70 we will send the packet to that server. */
71 router = (SilcServerEntry)client->router;
72 idata = (SilcIDListData)router;
74 silc_server_send_private_message(server, router->connection,
81 /* Seems that client really is directly connected to us */
82 idata = (SilcIDListData)client;
83 silc_server_send_private_message(server, dst_sock,
89 /* Destination belongs to someone not in this server. If we are normal
90 server our action is to send the packet to our router. */
91 if (server->server_type == SILC_SERVER && !server->standalone) {
92 router = server->router;
94 /* Send to primary route */
96 dst_sock = (SilcSocketConnection)router->connection;
97 idata = (SilcIDListData)router;
98 silc_server_send_private_message(server, dst_sock,
100 idata->hmac, packet);
105 /* We are router and we will perform route lookup for the destination
106 and send the message to fastest route. */
107 if (server->server_type == SILC_ROUTER && !server->standalone) {
108 /* Check first that the ID is valid */
109 client = silc_idlist_find_client_by_id(server->global_list, id, NULL);
111 dst_sock = silc_server_route_get(server, id, SILC_ID_CLIENT);
112 router = (SilcServerEntry)dst_sock->user_data;
113 idata = (SilcIDListData)router;
115 /* Get fastest route and send packet. */
117 silc_server_send_private_message(server, dst_sock,
119 idata->hmac, packet);
125 silc_server_send_error(server, sock,
126 "No such nickname: Private message not sent");
129 /* Processes incoming command reply packet. The command reply packet may
130 be destined to one of our clients or it may directly for us. We will
131 call the command reply routine after processing the packet. */
133 void silc_server_command_reply(SilcServer server,
134 SilcSocketConnection sock,
135 SilcPacketContext *packet)
137 SilcBuffer buffer = packet->buffer;
138 SilcClientEntry client = NULL;
139 SilcSocketConnection dst_sock;
140 SilcIDListData idata;
141 SilcClientID *id = NULL;
143 SILC_LOG_DEBUG(("Start"));
145 /* Source must be server or router */
146 if (packet->src_id_type != SILC_ID_SERVER &&
147 sock->type != SILC_SOCKET_TYPE_ROUTER)
150 if (packet->dst_id_type == SILC_ID_CHANNEL)
153 if (packet->dst_id_type == SILC_ID_CLIENT) {
154 /* Destination must be one of ours */
155 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CLIENT);
158 client = silc_idlist_find_client_by_id(server->local_list, id, NULL);
160 SILC_LOG_ERROR(("Cannot process command reply to unknown client"));
166 if (packet->dst_id_type == SILC_ID_SERVER) {
167 /* For now this must be for us */
168 if (SILC_ID_SERVER_COMPARE(packet->dst_id, server->id_string)) {
169 SILC_LOG_ERROR(("Cannot process command reply to unknown server"));
174 /* Execute command reply locally for the command */
175 silc_server_command_reply_process(server, sock, buffer);
177 if (packet->dst_id_type == SILC_ID_CLIENT && client && id) {
178 /* Relay the packet to the client */
180 dst_sock = (SilcSocketConnection)client->connection;
181 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
182 + packet->dst_id_len + packet->padlen);
184 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
185 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
187 idata = (SilcIDListData)client;
190 silc_packet_encrypt(idata->send_key, idata->hmac, dst_sock->outbuf,
193 /* Send the packet */
194 silc_server_packet_send_real(server, dst_sock, TRUE);
200 /* Process received channel message. The message can be originated from
203 void silc_server_channel_message(SilcServer server,
204 SilcSocketConnection sock,
205 SilcPacketContext *packet)
207 SilcChannelEntry channel = NULL;
208 SilcChannelClientEntry chl;
209 SilcChannelID *id = NULL;
212 SILC_LOG_DEBUG(("Processing channel message"));
215 if (packet->dst_id_type != SILC_ID_CHANNEL) {
216 SILC_LOG_DEBUG(("Received bad message for channel, dropped"));
220 /* Find channel entry */
221 id = silc_id_str2id(packet->dst_id, packet->dst_id_len, SILC_ID_CHANNEL);
224 channel = silc_idlist_find_channel_by_id(server->local_list, id, NULL);
226 channel = silc_idlist_find_channel_by_id(server->global_list, id, NULL);
228 SILC_LOG_DEBUG(("Could not find channel"));
233 /* See that this client is on the channel. If the message is coming
234 from router we won't do the check as the message is from client that
235 we don't know about. Also, if the original sender is not client
236 (as it can be server as well) we don't do the check. */
237 sender = silc_id_str2id(packet->src_id, packet->src_id_len,
238 packet->src_id_type);
241 if (sock->type != SILC_SOCKET_TYPE_ROUTER &&
242 packet->src_id_type == SILC_ID_CLIENT) {
243 silc_list_start(channel->user_list);
244 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
245 if (chl->client && !SILC_ID_CLIENT_COMPARE(chl->client->id, sender))
248 if (chl == SILC_LIST_END)
252 /* Distribute the packet to our local clients. This will send the
253 packet for further routing as well, if needed. */
254 silc_server_packet_relay_to_channel(server, sock, channel, sender,
256 packet->buffer->data,
257 packet->buffer->len, FALSE);
266 /* Received channel key packet. We distribute the key to all of our locally
267 connected clients on the channel. */
269 void silc_server_channel_key(SilcServer server,
270 SilcSocketConnection sock,
271 SilcPacketContext *packet)
273 SilcBuffer buffer = packet->buffer;
274 SilcChannelEntry channel;
276 if (packet->src_id_type != SILC_ID_SERVER)
279 /* Save the channel key */
280 channel = silc_server_save_channel_key(server, buffer, NULL);
284 /* Distribute the key to everybody who is on the channel. If we are router
285 we will also send it to locally connected servers. */
286 silc_server_send_channel_key(server, sock, channel, FALSE);
289 /* Received packet to replace a ID. This checks that the requested ID
290 exists and replaces it with the new one. */
292 void silc_server_replace_id(SilcServer server,
293 SilcSocketConnection sock,
294 SilcPacketContext *packet)
296 SilcBuffer buffer = packet->buffer;
297 unsigned char *old_id = NULL, *new_id = NULL;
298 SilcIdType old_id_type, new_id_type;
299 unsigned short old_id_len, new_id_len;
300 void *id = NULL, *id2 = NULL;
303 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
304 packet->src_id_type == SILC_ID_CLIENT)
307 SILC_LOG_DEBUG(("Replacing ID"));
309 ret = silc_buffer_unformat(buffer,
310 SILC_STR_UI_SHORT(&old_id_type),
311 SILC_STR_UI16_NSTRING_ALLOC(&old_id, &old_id_len),
312 SILC_STR_UI_SHORT(&new_id_type),
313 SILC_STR_UI16_NSTRING_ALLOC(&new_id, &new_id_len),
318 if (old_id_type != new_id_type)
321 if (old_id_len != silc_id_get_len(old_id_type) ||
322 new_id_len != silc_id_get_len(new_id_type))
325 id = silc_id_str2id(old_id, old_id_len, old_id_type);
329 id2 = silc_id_str2id(new_id, new_id_len, new_id_type);
333 /* If we are router and this packet is not already broadcast packet
334 we will broadcast it. The sending socket really cannot be router or
335 the router is buggy. If this packet is coming from router then it must
336 have the broadcast flag set already and we won't do anything. */
337 if (!server->standalone && server->server_type == SILC_ROUTER &&
338 sock->type == SILC_SOCKET_TYPE_SERVER &&
339 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
340 SILC_LOG_DEBUG(("Broadcasting received Replace ID packet"));
341 silc_server_packet_send(server, server->router->connection, packet->type,
342 packet->flags | SILC_PACKET_FLAG_BROADCAST,
343 buffer->data, buffer->len, FALSE);
346 /* Replace the old ID */
347 switch(old_id_type) {
350 SilcBuffer nidp, oidp;
351 SilcClientEntry client = NULL;
353 SILC_LOG_DEBUG(("Old Client ID id(%s)",
354 silc_id_render(id, SILC_ID_CLIENT)));
355 SILC_LOG_DEBUG(("New Client ID id(%s)",
356 silc_id_render(id2, SILC_ID_CLIENT)));
358 if ((client = silc_idlist_replace_client_id(server->local_list,
360 if (server->server_type == SILC_ROUTER)
361 client = silc_idlist_replace_client_id(server->global_list, id, id2);
364 oidp = silc_id_payload_encode(id, SILC_ID_CLIENT);
365 nidp = silc_id_payload_encode(id2, SILC_ID_CLIENT);
367 /* The nickname is not valid anymore, set it NULL. This causes that
368 the nickname will be queried if someone wants to know it. */
369 if (client->nickname)
370 silc_free(client->nickname);
371 client->nickname = NULL;
373 /* Send the NICK_CHANGE notify type to local clients on the channels
374 this client is joined to. */
375 silc_server_send_notify_on_channels(server, client,
376 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
377 oidp->data, oidp->len,
378 nidp->data, nidp->len);
380 silc_buffer_free(nidp);
381 silc_buffer_free(oidp);
387 SILC_LOG_DEBUG(("Old Server ID id(%s)",
388 silc_id_render(id, SILC_ID_SERVER)));
389 SILC_LOG_DEBUG(("New Server ID id(%s)",
390 silc_id_render(id2, SILC_ID_SERVER)));
391 if (silc_idlist_replace_server_id(server->local_list, id, id2) == NULL)
392 if (server->server_type == SILC_ROUTER)
393 silc_idlist_replace_server_id(server->global_list, id, id2);
396 case SILC_ID_CHANNEL:
397 SILC_LOG_DEBUG(("Old Channel ID id(%s)",
398 silc_id_render(id, SILC_ID_CHANNEL)));
399 SILC_LOG_DEBUG(("New Channel ID id(%s)",
400 silc_id_render(id2, SILC_ID_CHANNEL)));
401 if (silc_idlist_replace_channel_id(server->local_list, id, id2) == NULL)
402 silc_idlist_replace_channel_id(server->global_list, id, id2);
420 /* Received New Client packet and processes it. Creates Client ID for the
421 client. Client becomes registered after calling this functions. */
423 SilcClientEntry silc_server_new_client(SilcServer server,
424 SilcSocketConnection sock,
425 SilcPacketContext *packet)
427 SilcBuffer buffer = packet->buffer;
428 SilcClientEntry client;
429 SilcIDCacheEntry cache;
430 SilcClientID *client_id;
432 SilcIDListData idata;
433 char *username = NULL, *realname = NULL, *id_string;
436 SILC_LOG_DEBUG(("Creating new client"));
438 if (sock->type != SILC_SOCKET_TYPE_CLIENT)
441 /* Take client entry */
442 client = (SilcClientEntry)sock->user_data;
443 idata = (SilcIDListData)client;
445 /* Fetch the old client cache entry so that we can update it. */
446 if (!silc_idcache_find_by_context(server->local_list->clients,
447 sock->user_data, &cache)) {
448 SILC_LOG_ERROR(("Lost client's cache entry - bad thing"));
452 /* Parse incoming packet */
453 ret = silc_buffer_unformat(buffer,
454 SILC_STR_UI16_STRING_ALLOC(&username),
455 SILC_STR_UI16_STRING_ALLOC(&realname),
465 /* Create Client ID */
466 silc_id_create_client_id(server->id, server->rng, server->md5hash,
467 username, &client_id);
469 /* Update client entry */
470 idata->registered = TRUE;
471 client->nickname = strdup(username);
472 client->username = username;
473 client->userinfo = realname;
474 client->id = client_id;
476 /* Update the cache entry */
477 cache->id = (void *)client_id;
478 cache->type = SILC_ID_CLIENT;
479 cache->data = username;
480 silc_idcache_sort_by_data(server->local_list->clients);
482 /* Notify our router about new client on the SILC network */
483 if (!server->standalone)
484 silc_server_send_new_id(server, (SilcSocketConnection)
485 server->router->connection,
486 server->server_type == SILC_ROUTER ? TRUE : FALSE,
487 client->id, SILC_ID_CLIENT, SILC_ID_CLIENT_LEN);
489 /* Send the new client ID to the client. */
490 id_string = silc_id_id2str(client->id, SILC_ID_CLIENT);
491 reply = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN);
492 silc_buffer_pull_tail(reply, SILC_BUFFER_END(reply));
493 silc_buffer_format(reply,
494 SILC_STR_UI_SHORT(SILC_ID_CLIENT),
495 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
496 SILC_STR_UI_XNSTRING(id_string, SILC_ID_CLIENT_LEN),
498 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 0,
499 reply->data, reply->len, FALSE);
500 silc_free(id_string);
501 silc_buffer_free(reply);
503 /* Send some nice info to the client */
504 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
505 ("Welcome to the SILC Network %s@%s",
506 username, sock->hostname));
507 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
508 ("Your host is %s, running version %s",
509 server->config->server_info->server_name,
511 if (server->server_type == SILC_ROUTER) {
512 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
513 ("There are %d clients on %d servers in SILC "
514 "Network", server->stat.clients,
515 server->stat.servers + 1));
516 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
517 ("There are %d clients on %d server in our cell",
518 server->stat.cell_clients,
519 server->stat.cell_servers + 1));
520 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
521 ("I have %d clients, %d channels, %d servers and "
523 server->stat.my_clients,
524 server->stat.my_channels,
525 server->stat.my_servers,
526 server->stat.my_routers));
527 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
528 ("%d server operators and %d router operators "
530 server->stat.my_server_ops,
531 server->stat.my_router_ops));
533 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
534 ("I have %d clients and %d channels formed",
535 server->stat.my_clients,
536 server->stat.my_channels));
537 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
538 ("%d operators online",
539 server->stat.my_server_ops));
541 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
542 ("Your connection is secured with %s cipher, "
543 "key length %d bits",
544 idata->send_key->cipher->name,
545 idata->send_key->cipher->key_len));
546 SILC_SERVER_SEND_NOTIFY(server, sock, SILC_NOTIFY_TYPE_NONE,
547 ("Your current nickname is %s",
551 silc_server_send_motd(server, sock);
556 /* Create new server. This processes received New Server packet and
557 saves the received Server ID. The server is our locally connected
558 server thus we save all the information and save it to local list.
559 This funtion can be used by both normal server and router server.
560 If normal server uses this it means that its router has connected
561 to the server. If router uses this it means that one of the cell's
562 servers is connected to the router. */
564 SilcServerEntry silc_server_new_server(SilcServer server,
565 SilcSocketConnection sock,
566 SilcPacketContext *packet)
568 SilcBuffer buffer = packet->buffer;
569 SilcServerEntry new_server;
570 SilcIDCacheEntry cache;
571 SilcServerID *server_id;
572 SilcIDListData idata;
573 unsigned char *server_name, *id_string;
574 unsigned short id_len;
577 SILC_LOG_DEBUG(("Creating new server"));
579 if (sock->type != SILC_SOCKET_TYPE_SERVER &&
580 sock->type != SILC_SOCKET_TYPE_ROUTER)
583 /* Take server entry */
584 new_server = (SilcServerEntry)sock->user_data;
585 idata = (SilcIDListData)new_server;
587 /* Fetch the old server cache entry so that we can update it. */
588 if (!silc_idcache_find_by_context(server->local_list->servers,
589 sock->user_data, &cache)) {
590 SILC_LOG_ERROR(("Lost server's cache entry - bad thing"));
594 /* Parse the incoming packet */
595 ret = silc_buffer_unformat(buffer,
596 SILC_STR_UI16_NSTRING_ALLOC(&id_string, &id_len),
597 SILC_STR_UI16_STRING_ALLOC(&server_name),
601 silc_free(id_string);
603 silc_free(server_name);
607 if (id_len > buffer->len) {
608 silc_free(id_string);
609 silc_free(server_name);
614 server_id = silc_id_str2id(id_string, id_len, SILC_ID_SERVER);
616 silc_free(id_string);
617 silc_free(server_name);
620 silc_free(id_string);
622 /* Update client entry */
623 idata->registered = TRUE;
624 new_server->server_name = server_name;
625 new_server->id = server_id;
627 /* Update the cache entry */
628 cache->id = (void *)server_id;
629 cache->type = SILC_ID_SERVER;
630 cache->data = server_name;
631 silc_idcache_sort_by_data(server->local_list->servers);
633 /* Distribute the information about new server in the SILC network
634 to our router. If we are normal server we won't send anything
635 since this connection must be our router connection. */
636 if (server->server_type == SILC_ROUTER && !server->standalone &&
637 server->router->connection != sock)
638 silc_server_send_new_id(server, server->router->connection,
639 TRUE, new_server->id, SILC_ID_SERVER,
642 if (server->server_type == SILC_ROUTER)
643 server->stat.cell_servers++;
648 /* Processes incoming New ID packet. New ID Payload is used to distribute
649 information about newly registered clients and servers. */
651 static void silc_server_new_id_real(SilcServer server,
652 SilcSocketConnection sock,
653 SilcPacketContext *packet,
656 SilcBuffer buffer = packet->buffer;
658 SilcServerEntry router;
659 SilcSocketConnection router_sock;
662 unsigned char *hash = NULL;
665 SILC_LOG_DEBUG(("Processing new ID"));
667 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
668 server->server_type == SILC_SERVER ||
669 packet->src_id_type != SILC_ID_SERVER)
672 idp = silc_id_payload_parse(buffer);
676 id_type = silc_id_payload_get_type(idp);
678 /* Normal server cannot have other normal server connections */
679 if (id_type == SILC_ID_SERVER && sock->type == SILC_SOCKET_TYPE_SERVER)
682 id = silc_id_payload_get_id(idp);
686 /* If the sender of this packet is server and we are router we need to
687 broadcast this packet to other routers in the network. */
688 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
689 sock->type == SILC_SOCKET_TYPE_SERVER &&
690 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
691 SILC_LOG_DEBUG(("Broadcasting received New ID packet"));
692 silc_server_packet_send(server, server->router->connection,
694 packet->flags | SILC_PACKET_FLAG_BROADCAST,
695 buffer->data, buffer->len, FALSE);
698 if (sock->type == SILC_SOCKET_TYPE_SERVER)
699 id_list = server->local_list;
701 id_list = server->global_list;
704 router = sock->user_data;
709 SilcClientEntry entry;
711 SILC_LOG_DEBUG(("New client id(%s) from [%s] %s",
712 silc_id_render(id, SILC_ID_CLIENT),
713 sock->type == SILC_SOCKET_TYPE_SERVER ?
714 "Server" : "Router", sock->hostname));
716 /* As a router we keep information of all global information in our
717 global list. Cell wide information however is kept in the local
718 list. The client is put to global list and we will take the hash
719 value of the Client ID and save it to the ID Cache system for fast
720 searching in the future. */
721 hash = silc_calloc(sizeof(((SilcClientID *)id)->hash),
722 sizeof(unsigned char));
723 memcpy(hash, ((SilcClientID *)id)->hash,
724 sizeof(((SilcClientID *)id)->hash));
725 entry = silc_idlist_add_client(id_list, hash, NULL, NULL, id,
727 entry->nickname = NULL;
729 if (sock->type == SILC_SOCKET_TYPE_SERVER)
730 server->stat.cell_clients++;
731 server->stat.clients++;
734 /* XXX Adding two ID's with same IP number replaces the old entry thus
735 gives wrong route. Thus, now disabled until figured out a better way
736 to do this or when removed the whole thing. This could be removed
737 because entry->router->connection gives always the most optimal route
738 for the ID anyway (unless new routes (faster perhaps) are established
739 after receiving this ID, this we don't know however). */
740 /* Add route cache for this ID */
741 silc_server_route_add(silc_server_route_hash(
742 ((SilcClientID *)id)->ip.s_addr,
743 server->id->port), ((SilcClientID *)id)->ip.s_addr,
750 SILC_LOG_DEBUG(("New server id(%s) from [%s] %s",
751 silc_id_render(id, SILC_ID_SERVER),
752 sock->type == SILC_SOCKET_TYPE_SERVER ?
753 "Server" : "Router", sock->hostname));
755 /* As a router we keep information of all global information in our global
756 list. Cell wide information however is kept in the local list. */
757 silc_idlist_add_server(id_list, NULL, 0, id, router, router_sock);
759 if (sock->type == SILC_SOCKET_TYPE_SERVER)
760 server->stat.cell_servers++;
761 server->stat.servers++;
764 /* Add route cache for this ID */
765 silc_server_route_add(silc_server_route_hash(
766 ((SilcServerID *)id)->ip.s_addr,
767 ((SilcServerID *)id)->port),
768 ((SilcServerID *)id)->ip.s_addr,
773 case SILC_ID_CHANNEL:
774 SILC_LOG_ERROR(("Channel cannot be registered with NEW_ID packet"));
782 silc_id_payload_free(idp);
786 /* Processes incoming New ID packet. New ID Payload is used to distribute
787 information about newly registered clients and servers. */
789 void silc_server_new_id(SilcServer server, SilcSocketConnection sock,
790 SilcPacketContext *packet)
792 silc_server_new_id_real(server, sock, packet, TRUE);
795 /* Receoved New Id List packet, list of New ID payloads inside one
796 packet. Process the New ID payloads one by one. */
798 void silc_server_new_id_list(SilcServer server, SilcSocketConnection sock,
799 SilcPacketContext *packet)
801 SilcPacketContext *new_id;
803 unsigned short id_len;
805 SILC_LOG_DEBUG(("Processing New ID List"));
807 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
808 packet->src_id_type != SILC_ID_SERVER)
811 /* If the sender of this packet is server and we are router we need to
812 broadcast this packet to other routers in the network. Broadcast
813 this list packet instead of multiple New ID packets. */
814 if (!server->standalone && server->server_type == SILC_ROUTER &&
815 sock->type == SILC_SOCKET_TYPE_SERVER &&
816 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
817 SILC_LOG_DEBUG(("Broadcasting received New ID List packet"));
818 silc_server_packet_send(server, server->router->connection,
820 packet->flags | SILC_PACKET_FLAG_BROADCAST,
821 packet->buffer->data, packet->buffer->len, FALSE);
824 /* Make copy of the original packet context, except for the actual
825 data buffer, which we will here now fetch from the original buffer. */
826 new_id = silc_packet_context_alloc();
827 new_id->type = SILC_PACKET_NEW_ID;
828 new_id->flags = packet->flags;
829 new_id->src_id = packet->src_id;
830 new_id->src_id_len = packet->src_id_len;
831 new_id->src_id_type = packet->src_id_type;
832 new_id->dst_id = packet->dst_id;
833 new_id->dst_id_len = packet->dst_id_len;
834 new_id->dst_id_type = packet->dst_id_type;
836 idp = silc_buffer_alloc(256);
837 new_id->buffer = idp;
839 while (packet->buffer->len) {
840 SILC_GET16_MSB(id_len, packet->buffer->data + 2);
841 if ((id_len > packet->buffer->len) ||
842 (id_len > idp->truelen))
845 silc_buffer_pull_tail(idp, 4 + id_len);
846 silc_buffer_put(idp, packet->buffer->data, 4 + id_len);
848 /* Process the New ID */
849 silc_server_new_id_real(server, sock, new_id, FALSE);
851 silc_buffer_push_tail(idp, 4 + id_len);
852 silc_buffer_pull(packet->buffer, 4 + id_len);
855 silc_buffer_free(idp);
859 /* Received New Channel packet. Information about new channels in the
860 network are distributed using this packet. Save the information about
861 the new channel. This usually comes from router but also normal server
862 can send this to notify channels it has when it connects to us. */
864 void silc_server_new_channel(SilcServer server,
865 SilcSocketConnection sock,
866 SilcPacketContext *packet)
869 SilcChannelID *channel_id;
870 unsigned short channel_id_len;
874 SILC_LOG_DEBUG(("Processing New Channel"));
876 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
877 packet->src_id_type != SILC_ID_SERVER ||
878 server->server_type == SILC_SERVER)
882 ret = silc_buffer_unformat(packet->buffer,
883 SILC_STR_UI16_STRING_ALLOC(&channel_name),
884 SILC_STR_UI16_NSTRING_ALLOC(&id, &channel_id_len),
888 silc_free(channel_name);
894 /* Decode the channel ID */
895 channel_id = silc_id_str2id(id, channel_id_len, SILC_ID_CHANNEL);
899 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
900 /* Add the server to global list as it is coming from router. It
901 cannot be our own channel as it is coming from router. */
903 SILC_LOG_DEBUG(("New channel id(%s) from [Router] %s",
904 silc_id_render(channel_id, SILC_ID_CHANNEL),
907 silc_idlist_add_channel(server->global_list, channel_name, 0, channel_id,
908 server->router->connection, NULL);
910 server->stat.channels++;
912 /* The channel is coming from our server, thus it is in our cell
913 we will add it to our local list. */
914 SilcChannelEntry channel;
917 SILC_LOG_DEBUG(("New channel id(%s) from [Server] %s",
918 silc_id_render(channel_id, SILC_ID_CHANNEL),
921 /* Check that we don't already have this channel */
922 channel = silc_idlist_find_channel_by_name(server->local_list,
925 channel = silc_idlist_find_channel_by_name(server->global_list,
928 /* If the channel does not exist, then create it. We create the channel
929 with the channel ID provided by the server. This creates a new
930 key to the channel as well that we will send to the server. */
932 channel = silc_server_create_new_channel_with_id(server, NULL,
938 /* Send the new channel key to the server */
939 chk = silc_channel_key_payload_encode(channel_id_len, id,
940 strlen(channel->channel_key->
942 channel->channel_key->cipher->name,
943 channel->key_len / 8,
945 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
946 chk->data, chk->len, FALSE);
947 silc_buffer_free(chk);
950 /* The channel exist by that name, check whether the ID's match.
951 If they don't then we'll force the server to use the ID we have.
952 We also create a new key for the channel. */
954 if (SILC_ID_CHANNEL_COMPARE(channel_id, channel->id)) {
955 /* They don't match, send Replace ID packet to the server to
956 force the ID change. */
957 SILC_LOG_DEBUG(("Forcing the server to change Channel ID"));
958 silc_server_send_replace_id(server, sock, FALSE,
959 channel_id, SILC_ID_CHANNEL,
961 channel->id, SILC_ID_CHANNEL,
962 SILC_ID_CHANNEL_LEN);
965 /* Create new key for the channel and send it to the server and
966 everybody else possibly on the channel. */
968 silc_server_create_channel_key(server, channel, 0);
970 /* Send to the channel */
971 silc_server_send_channel_key(server, sock, channel, FALSE);
973 /* Send to the server */
974 chk = silc_channel_key_payload_encode(channel_id_len, id,
975 strlen(channel->channel_key->
977 channel->channel_key->cipher->name,
978 channel->key_len / 8,
980 silc_server_packet_send(server, sock, SILC_PACKET_CHANNEL_KEY, 0,
981 chk->data, chk->len, FALSE);
982 silc_buffer_free(chk);
984 /* Since the channel is coming from server and we also know about it
985 then send the JOIN notify to the server so that it see's our
986 users on the channel "joining" the channel. */
994 /* Received New Channel List packet, list of New Channel List payloads inside
995 one packet. Process the New Channel payloads one by one. */
997 void silc_server_new_channel_list(SilcServer server,
998 SilcSocketConnection sock,
999 SilcPacketContext *packet)
1001 SilcPacketContext *new;
1003 unsigned short len1, len2;
1005 SILC_LOG_DEBUG(("Processing New Channel List"));
1007 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1008 packet->src_id_type != SILC_ID_SERVER ||
1009 server->server_type == SILC_SERVER)
1012 /* If the sender of this packet is server and we are router we need to
1013 broadcast this packet to other routers in the network. Broadcast
1014 this list packet instead of multiple New Channel packets. */
1015 if (!server->standalone && server->server_type == SILC_ROUTER &&
1016 sock->type == SILC_SOCKET_TYPE_SERVER &&
1017 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1018 SILC_LOG_DEBUG(("Broadcasting received New Channel List packet"));
1019 silc_server_packet_send(server, server->router->connection,
1021 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1022 packet->buffer->data, packet->buffer->len, FALSE);
1025 /* Make copy of the original packet context, except for the actual
1026 data buffer, which we will here now fetch from the original buffer. */
1027 new = silc_packet_context_alloc();
1028 new->type = SILC_PACKET_NEW_CHANNEL;
1029 new->flags = packet->flags;
1030 new->src_id = packet->src_id;
1031 new->src_id_len = packet->src_id_len;
1032 new->src_id_type = packet->src_id_type;
1033 new->dst_id = packet->dst_id;
1034 new->dst_id_len = packet->dst_id_len;
1035 new->dst_id_type = packet->dst_id_type;
1037 buffer = silc_buffer_alloc(512);
1038 new->buffer = buffer;
1040 while (packet->buffer->len) {
1041 SILC_GET16_MSB(len1, packet->buffer->data);
1042 if ((len1 > packet->buffer->len) ||
1043 (len1 > buffer->truelen))
1046 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
1047 if ((len2 > packet->buffer->len) ||
1048 (len2 > buffer->truelen))
1051 silc_buffer_pull_tail(buffer, 4 + len1 + len2);
1052 silc_buffer_put(buffer, packet->buffer->data, 4 + len1 + len2);
1054 /* Process the New Channel */
1055 silc_server_new_channel(server, sock, new);
1057 silc_buffer_push_tail(buffer, 4 + len1 + len2);
1058 silc_buffer_pull(packet->buffer, 4 + len1 + len2);
1061 silc_buffer_free(buffer);
1065 /* Received new channel user packet. Information about new users on a
1066 channel are distributed between routers using this packet. The
1067 router receiving this will redistribute it and also sent JOIN notify
1068 to local clients on the same channel. Normal server sends JOIN notify
1069 to its local clients on the channel. */
1071 static void silc_server_new_channel_user_real(SilcServer server,
1072 SilcSocketConnection sock,
1073 SilcPacketContext *packet,
1076 unsigned char *tmpid1, *tmpid2;
1077 SilcClientID *client_id = NULL;
1078 SilcChannelID *channel_id = NULL;
1079 unsigned short channel_id_len;
1080 unsigned short client_id_len;
1081 SilcClientEntry client;
1082 SilcChannelEntry channel;
1083 SilcChannelClientEntry chl;
1087 SILC_LOG_DEBUG(("Start"));
1089 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1090 server->server_type != SILC_ROUTER ||
1091 packet->src_id_type != SILC_ID_SERVER)
1095 ret = silc_buffer_unformat(packet->buffer,
1096 SILC_STR_UI16_NSTRING_ALLOC(&tmpid1,
1098 SILC_STR_UI16_NSTRING_ALLOC(&tmpid2,
1109 /* Decode the channel ID */
1110 channel_id = silc_id_str2id(tmpid1, channel_id_len, SILC_ID_CHANNEL);
1114 /* Decode the client ID */
1115 client_id = silc_id_str2id(tmpid2, client_id_len, SILC_ID_CLIENT);
1119 /* If we are router and this packet is not already broadcast packet
1120 we will broadcast it. */
1121 if (broadcast && !server->standalone && server->server_type == SILC_ROUTER &&
1122 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1123 SILC_LOG_DEBUG(("Broadcasting received New Channel User packet"));
1124 silc_server_packet_send(server, server->router->connection, packet->type,
1125 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1126 packet->buffer->data, packet->buffer->len, FALSE);
1129 /* Find the channel */
1130 channel = silc_idlist_find_channel_by_id(server->local_list,
1133 channel = silc_idlist_find_channel_by_id(server->global_list,
1139 /* Get client entry */
1140 client = silc_idlist_find_client_by_id(server->local_list, client_id, NULL);
1142 client = silc_idlist_find_client_by_id(server->global_list,
1148 /* Join the client to the channel by adding it to channel's user list.
1149 Add also the channel to client entry's channels list for fast cross-
1151 chl = silc_calloc(1, sizeof(*chl));
1152 chl->client = client;
1153 chl->channel = channel;
1154 silc_list_add(channel->user_list, chl);
1155 silc_list_add(client->channels, chl);
1157 server->stat.chanclients++;
1159 /* Send JOIN notify to local clients on the channel. As we are router
1160 it is assured that this is sent only to our local clients and locally
1161 connected servers if needed. */
1162 clidp = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1163 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
1164 SILC_NOTIFY_TYPE_JOIN,
1165 1, clidp->data, clidp->len);
1166 silc_buffer_free(clidp);
1172 silc_free(client_id);
1174 silc_free(channel_id);
1179 /* Received new channel user packet. Information about new users on a
1180 channel are distributed between routers using this packet. The
1181 router receiving this will redistribute it and also sent JOIN notify
1182 to local clients on the same channel. Normal server sends JOIN notify
1183 to its local clients on the channel. */
1185 void silc_server_new_channel_user(SilcServer server,
1186 SilcSocketConnection sock,
1187 SilcPacketContext *packet)
1189 silc_server_new_channel_user_real(server, sock, packet, TRUE);
1192 /* Received New Channel User List packet, list of New Channel User payloads
1193 inside one packet. Process the payloads one by one. */
1195 void silc_server_new_channel_user_list(SilcServer server,
1196 SilcSocketConnection sock,
1197 SilcPacketContext *packet)
1199 SilcPacketContext *new;
1201 unsigned short len1, len2;
1203 SILC_LOG_DEBUG(("Processing New Channel User List"));
1205 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1206 packet->src_id_type != SILC_ID_SERVER ||
1207 server->server_type == SILC_SERVER)
1210 /* If we are router and this packet is not already broadcast packet
1211 we will broadcast it. Brodcast this list packet instead of multiple
1212 New Channel User packets. */
1213 if (!server->standalone && server->server_type == SILC_ROUTER &&
1214 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1215 SILC_LOG_DEBUG(("Broadcasting received New Channel User List packet"));
1216 silc_server_packet_send(server, server->router->connection, packet->type,
1217 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1218 packet->buffer->data, packet->buffer->len, FALSE);
1221 /* Make copy of the original packet context, except for the actual
1222 data buffer, which we will here now fetch from the original buffer. */
1223 new = silc_packet_context_alloc();
1224 new->type = SILC_PACKET_NEW_CHANNEL_USER;
1225 new->flags = packet->flags;
1226 new->src_id = packet->src_id;
1227 new->src_id_len = packet->src_id_len;
1228 new->src_id_type = packet->src_id_type;
1229 new->dst_id = packet->dst_id;
1230 new->dst_id_len = packet->dst_id_len;
1231 new->dst_id_type = packet->dst_id_type;
1233 buffer = silc_buffer_alloc(256);
1234 new->buffer = buffer;
1236 while (packet->buffer->len) {
1237 SILC_GET16_MSB(len1, packet->buffer->data);
1238 if ((len1 > packet->buffer->len) ||
1239 (len1 > buffer->truelen))
1242 SILC_GET16_MSB(len2, packet->buffer->data + 2 + len1);
1243 if ((len2 > packet->buffer->len) ||
1244 (len2 > buffer->truelen))
1247 silc_buffer_pull_tail(buffer, 4 + len1 + len2);
1248 silc_buffer_put(buffer, packet->buffer->data, 4 + len1 + len2);
1250 /* Process the New Channel User */
1251 silc_server_new_channel_user_real(server, sock, new, FALSE);
1253 silc_buffer_push_tail(buffer, 4 + len1 + len2);
1254 silc_buffer_pull(packet->buffer, 4 + len1 + len2);
1257 silc_buffer_free(buffer);
1261 /* Received Remove Channel User packet to remove a user from a channel.
1262 Routers notify other routers that user has left a channel. Client must
1263 not send this packet. Normal server may send this packet but must not
1266 void silc_server_remove_channel_user(SilcServer server,
1267 SilcSocketConnection sock,
1268 SilcPacketContext *packet)
1270 SilcBuffer buffer = packet->buffer;
1271 unsigned char *tmp1 = NULL, *tmp2 = NULL;
1272 unsigned short tmp1_len, tmp2_len;
1273 SilcClientID *client_id = NULL;
1274 SilcChannelID *channel_id = NULL;
1275 SilcChannelEntry channel;
1276 SilcClientEntry client;
1279 SILC_LOG_DEBUG(("Removing user from channel"));
1281 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1282 packet->src_id_type != SILC_ID_SERVER ||
1283 server->server_type == SILC_SERVER)
1286 ret = silc_buffer_unformat(buffer,
1287 SILC_STR_UI16_NSTRING_ALLOC(&tmp1, &tmp1_len),
1288 SILC_STR_UI16_NSTRING_ALLOC(&tmp2, &tmp2_len),
1293 client_id = silc_id_str2id(tmp1, tmp1_len, SILC_ID_CLIENT);
1294 channel_id = silc_id_str2id(tmp2, tmp2_len, SILC_ID_CHANNEL);
1295 if (!client_id || !channel_id)
1298 /* If we are router and this packet is not already broadcast packet
1299 we will broadcast it. The sending socket really cannot be router or
1300 the router is buggy. If this packet is coming from router then it must
1301 have the broadcast flag set already and we won't do anything. */
1302 if (!server->standalone && server->server_type == SILC_ROUTER &&
1303 sock->type == SILC_SOCKET_TYPE_SERVER &&
1304 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1305 SILC_LOG_DEBUG(("Broadcasting received Remove Channel User packet"));
1306 silc_server_packet_send(server, server->router->connection, packet->type,
1307 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1308 buffer->data, buffer->len, FALSE);
1311 /* Get channel entry */
1312 channel = silc_idlist_find_channel_by_id(server->local_list,
1315 channel = silc_idlist_find_channel_by_id(server->global_list,
1321 /* Get client entry */
1322 client = silc_idlist_find_client_by_id(server->local_list, client_id, NULL);
1324 client = silc_idlist_find_client_by_id(server->global_list,
1330 /* Remove user from channel */
1331 silc_server_remove_from_one_channel(server, sock, channel, client, TRUE);
1339 silc_free(client_id);
1341 silc_free(channel_id);
1344 /* Received notify packet. Server can receive notify packets from router.
1345 Server then relays the notify messages to clients if needed. */
1347 void silc_server_notify(SilcServer server,
1348 SilcSocketConnection sock,
1349 SilcPacketContext *packet)
1351 SilcNotifyPayload payload;
1352 SilcNotifyType type;
1353 SilcArgumentPayload args;
1354 SilcChannelID *channel_id;
1355 SilcClientID *client_id, *client_id2;
1356 SilcChannelEntry channel;
1357 SilcClientEntry client;
1359 unsigned int tmp_len;
1361 SILC_LOG_DEBUG(("Start"));
1363 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1364 packet->src_id_type != SILC_ID_SERVER)
1367 /* XXX: For now we expect that the we are normal server and that the
1368 sender is router. Server could send (protocol allows it) notify to
1369 router but we don't support it yet. */
1370 if (server->server_type != SILC_SERVER &&
1371 sock->type != SILC_SOCKET_TYPE_ROUTER)
1374 payload = silc_notify_payload_parse(packet->buffer);
1378 type = silc_notify_get_type(payload);
1379 args = silc_notify_get_args(payload);
1384 case SILC_NOTIFY_TYPE_JOIN:
1386 * Distribute the notify to local clients on the channel
1388 SILC_LOG_DEBUG(("JOIN notify"));
1390 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1391 packet->dst_id_type);
1395 /* Get channel entry */
1396 channel = silc_idlist_find_channel_by_id(server->local_list,
1399 silc_free(channel_id);
1404 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1406 silc_free(channel_id);
1409 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1411 silc_free(channel_id);
1415 /* Send to channel */
1416 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
1417 FALSE, packet->buffer->data,
1418 packet->buffer->len, FALSE);
1420 /* If the the client is not in local list we check global list (ie. the
1421 channel will be global channel) and if it does not exist then create
1422 entry for the client. */
1423 client = silc_idlist_find_client_by_id(server->local_list,
1426 SilcChannelClientEntry chl;
1428 client = silc_idlist_find_client_by_id(server->global_list,
1431 client = silc_idlist_add_client(server->global_list, NULL, NULL, NULL,
1432 client_id, sock->user_data, NULL);
1434 silc_free(channel_id);
1435 silc_free(client_id);
1440 /* The channel is global now */
1441 channel->global_users = TRUE;
1443 /* Now actually JOIN the global client to the channel */
1444 chl = silc_calloc(1, sizeof(*chl));
1445 chl->client = client;
1446 chl->channel = channel;
1447 silc_list_add(channel->user_list, chl);
1448 silc_list_add(client->channels, chl);
1450 silc_free(client_id);
1454 case SILC_NOTIFY_TYPE_LEAVE:
1456 * Distribute the notify to local clients on the channel
1458 SILC_LOG_DEBUG(("LEAVE notify"));
1460 channel_id = silc_id_str2id(packet->dst_id, packet->dst_id_len,
1461 packet->dst_id_type);
1465 /* Get channel entry */
1466 channel = silc_idlist_find_channel_by_id(server->local_list,
1469 silc_free(channel_id);
1474 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1476 silc_free(channel_id);
1479 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1481 silc_free(channel_id);
1485 /* Send to channel */
1486 silc_server_packet_send_to_channel(server, NULL, channel, packet->type,
1487 FALSE, packet->buffer->data,
1488 packet->buffer->len, FALSE);
1490 /* Get client entry */
1491 client = silc_idlist_find_client_by_id(server->global_list,
1494 client = silc_idlist_find_client_by_id(server->local_list,
1497 silc_free(client_id);
1498 silc_free(channel_id);
1502 silc_free(client_id);
1504 /* Remove the user from channel */
1505 silc_server_remove_from_one_channel(server, sock, channel, client, FALSE);
1508 case SILC_NOTIFY_TYPE_SIGNOFF:
1510 * Distribute the notify to local clients on the channel
1512 SILC_LOG_DEBUG(("SIGNOFF notify"));
1515 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1518 client_id = silc_id_payload_parse_id(tmp, tmp_len);
1522 /* Get client entry */
1523 client = silc_idlist_find_client_by_id(server->global_list,
1526 client = silc_idlist_find_client_by_id(server->local_list,
1529 silc_free(client_id);
1533 silc_free(client_id);
1535 /* Remove the client from all channels */
1536 silc_server_remove_from_channels(server, NULL, client);
1538 /* Remove the client entry */
1539 silc_idlist_del_client(server->global_list, client);
1542 case SILC_NOTIFY_TYPE_NICK_CHANGE:
1545 * Distribute the notify to local clients on the channel
1547 unsigned char *id, *id2;
1549 SILC_LOG_DEBUG(("NICK CHANGE notify"));
1551 /* Get old client ID */
1552 id = silc_argument_get_arg_type(args, 1, &tmp_len);
1555 client_id = silc_id_payload_parse_id(id, tmp_len);
1559 /* Get new client ID */
1560 id2 = silc_argument_get_arg_type(args, 2, &tmp_len);
1563 client_id2 = silc_id_payload_parse_id(id2, tmp_len);
1567 SILC_LOG_DEBUG(("Old Client ID id(%s)",
1568 silc_id_render(client_id, SILC_ID_CLIENT)));
1569 SILC_LOG_DEBUG(("New Client ID id(%s)",
1570 silc_id_render(client_id2, SILC_ID_CLIENT)));
1572 /* Replace the Client ID */
1573 client = silc_idlist_replace_client_id(server->global_list, client_id,
1576 client = silc_idlist_replace_client_id(server->local_list, client_id,
1580 /* Send the NICK_CHANGE notify type to local clients on the channels
1581 this client is joined to. */
1582 silc_server_send_notify_on_channels(server, client,
1583 SILC_NOTIFY_TYPE_NICK_CHANGE, 2,
1587 silc_free(client_id);
1589 silc_free(client_id2);
1592 case SILC_NOTIFY_TYPE_TOPIC_SET:
1594 * Distribute the notify to local clients on the channel
1596 SILC_LOG_DEBUG(("TOPIC SET notify (not-impl XXX)"));
1599 case SILC_NOTIFY_TYPE_CMODE_CHANGE:
1601 * Distribute the notify to local clients on the channel
1603 SILC_LOG_DEBUG(("CMODE CHANGE notify (not-impl XXX)"));
1606 case SILC_NOTIFY_TYPE_CUMODE_CHANGE:
1608 * Distribute the notify to local clients on the channel
1610 SILC_LOG_DEBUG(("CUMODE CHANGE notify (not-impl XXX)"));
1613 /* Ignore rest notify types for now */
1614 case SILC_NOTIFY_TYPE_NONE:
1615 case SILC_NOTIFY_TYPE_INVITE:
1616 case SILC_NOTIFY_TYPE_MOTD:
1622 silc_notify_payload_free(payload);
1625 /* Processes incoming REMOVE_ID packet. The packet is used to notify routers
1626 that certain ID should be removed. After that the ID will become invalid. */
1628 void silc_server_remove_id(SilcServer server,
1629 SilcSocketConnection sock,
1630 SilcPacketContext *packet)
1635 void *id, *id_entry;
1637 SILC_LOG_DEBUG(("Start"));
1639 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1640 server->server_type == SILC_SERVER ||
1641 packet->src_id_type != SILC_ID_SERVER)
1644 idp = silc_id_payload_parse(packet->buffer);
1648 id_type = silc_id_payload_get_type(idp);
1650 id = silc_id_payload_get_id(idp);
1654 /* If the sender of this packet is server and we are router we need to
1655 broadcast this packet to other routers in the network. */
1656 if (!server->standalone && server->server_type == SILC_ROUTER &&
1657 sock->type == SILC_SOCKET_TYPE_SERVER &&
1658 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1659 SILC_LOG_DEBUG(("Broadcasting received Remove ID packet"));
1660 silc_server_packet_send(server, server->router->connection,
1662 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1663 packet->buffer->data, packet->buffer->len, FALSE);
1666 if (sock->type == SILC_SOCKET_TYPE_SERVER)
1667 id_list = server->local_list;
1669 id_list = server->global_list;
1673 case SILC_ID_CLIENT:
1674 id_entry = silc_idlist_find_client_by_id(id_list, (SilcClientID *)id,
1677 /* Remove from channels */
1678 silc_server_remove_from_channels(server, NULL, id_entry);
1680 /* Remove the client entry */
1681 silc_idlist_del_client(id_list, (SilcClientEntry)id_entry);
1682 server->stat.clients--;
1683 if (sock->type == SILC_SOCKET_TYPE_SERVER &&
1684 server->server_type == SILC_ROUTER)
1685 server->stat.cell_clients--;
1687 SILC_LOG_DEBUG(("Removed client id(%s) from [%s] %s",
1688 silc_id_render(id, SILC_ID_CLIENT),
1689 sock->type == SILC_SOCKET_TYPE_SERVER ?
1690 "Server" : "Router", sock->hostname));
1694 case SILC_ID_SERVER:
1695 id_entry = silc_idlist_find_server_by_id(id_list, (SilcServerID *)id,
1698 silc_idlist_del_server(id_list, (SilcServerEntry)id_entry);
1699 server->stat.servers--;
1700 if (sock->type == SILC_SOCKET_TYPE_SERVER &&
1701 server->server_type == SILC_ROUTER)
1702 server->stat.cell_servers--;
1704 SILC_LOG_DEBUG(("Removed server id(%s) from [%s] %s",
1705 silc_id_render(id, SILC_ID_SERVER),
1706 sock->type == SILC_SOCKET_TYPE_SERVER ?
1707 "Server" : "Router", sock->hostname));
1711 case SILC_ID_CHANNEL:
1712 id_entry = silc_idlist_find_channel_by_id(id_list, (SilcChannelID *)id,
1715 silc_idlist_del_channel(id_list, (SilcChannelEntry)id_entry);
1716 server->stat.channels--;
1717 if (sock->type == SILC_SOCKET_TYPE_SERVER &&
1718 server->server_type == SILC_ROUTER)
1719 server->stat.cell_channels--;
1721 SILC_LOG_DEBUG(("Removed channel id(%s) from [%s] %s",
1722 silc_id_render(id, SILC_ID_CHANNEL),
1723 sock->type == SILC_SOCKET_TYPE_SERVER ?
1724 "Server" : "Router", sock->hostname));
1733 silc_id_payload_free(idp);
1736 /* Processes received SET_MODE packet. The packet is used to distribute
1737 the information about changed channel's or client's channel modes. */
1739 void silc_server_set_mode(SilcServer server,
1740 SilcSocketConnection sock,
1741 SilcPacketContext *packet)
1743 SilcSetModePayload payload = NULL;
1744 SilcArgumentPayload args = NULL;
1745 unsigned short mode_type;
1746 unsigned int mode_mask;
1747 unsigned char *tmp, *tmp2;
1748 unsigned int tmp_len, tmp_len2;
1749 unsigned char mode[4];
1750 SilcClientID *client_id;
1751 SilcChannelID *channel_id = NULL;
1752 SilcClientEntry client;
1753 SilcChannelEntry channel;
1754 SilcChannelClientEntry chl;
1756 if (sock->type == SILC_SOCKET_TYPE_CLIENT ||
1757 packet->src_id_type == SILC_ID_CLIENT)
1760 SILC_LOG_DEBUG(("Start"));
1762 /* If we are router and this packet is not already broadcast packet
1763 we will broadcast it. The sending socket really cannot be router or
1764 the router is buggy. If this packet is coming from router then it must
1765 have the broadcast flag set already and we won't do anything. */
1766 if (!server->standalone && server->server_type == SILC_ROUTER &&
1767 sock->type == SILC_SOCKET_TYPE_SERVER &&
1768 !(packet->flags & SILC_PACKET_FLAG_BROADCAST)) {
1769 SILC_LOG_DEBUG(("Broadcasting received Set Mode packet"));
1770 silc_server_packet_send(server, server->router->connection, packet->type,
1771 packet->flags | SILC_PACKET_FLAG_BROADCAST,
1772 packet->buffer->data, packet->buffer->len, FALSE);
1775 /* Parse Set Mode payload */
1776 payload = silc_set_mode_payload_parse(packet->buffer);
1780 mode_type = silc_set_mode_get_type(payload);
1781 args = silc_set_mode_get_args(payload);
1785 mode_mask = silc_set_mode_get_mode(payload);
1786 SILC_PUT32_MSB(mode_mask, mode);
1788 switch (mode_type) {
1789 case SILC_MODE_TYPE_CHANNEL:
1790 /* Get Channel ID */
1791 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1794 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1798 /* Get channel entry */
1799 channel = silc_idlist_find_channel_by_id(server->local_list,
1802 channel = silc_idlist_find_channel_by_id(server->global_list,
1808 /* Get Client ID payload */
1809 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1813 /* Send CMODE_CHANGE notify to local channel */
1814 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
1815 SILC_NOTIFY_TYPE_CMODE_CHANGE,
1817 mode, sizeof(mode));
1819 /* Change the mode */
1820 channel->mode = mode_mask;
1823 case SILC_MODE_TYPE_UCHANNEL:
1824 /* Get Channel ID */
1825 tmp = silc_argument_get_arg_type(args, 1, &tmp_len);
1828 channel_id = silc_id_payload_parse_id(tmp, tmp_len);
1832 /* Get channel entry */
1833 channel = silc_idlist_find_channel_by_id(server->local_list,
1836 channel = silc_idlist_find_channel_by_id(server->global_list,
1842 /* Get Client ID payload */
1843 tmp = silc_argument_get_arg_type(args, 2, &tmp_len);
1847 /* Get target Client ID */
1848 tmp2 = silc_argument_get_arg_type(args, 3, &tmp_len2);
1851 client_id = silc_id_payload_parse_id(tmp2, tmp_len2);
1855 /* Get target client entry */
1856 client = silc_idlist_find_client_by_id(server->global_list,
1859 client = silc_idlist_find_client_by_id(server->local_list,
1862 silc_free(client_id);
1866 silc_free(client_id);
1868 /* Send CUMODE_CHANGE notify to local channel */
1869 silc_server_send_notify_to_channel(server, sock, channel, FALSE,
1870 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 2,
1875 /* Get entry to the channel user list */
1876 silc_list_start(channel->user_list);
1877 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END)
1878 if (chl->client == client) {
1879 /* Change the mode */
1880 chl->mode = mode_mask;
1892 silc_free(channel_id);
1894 silc_argument_payload_free(args);
1896 silc_set_mode_payload_free(payload);