5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2002 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 send packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Routine that sends packet or marks packet to be sent. This is used
29 directly only in special cases. Normal cases should use
30 silc_server_packet_send. Returns < 0 error. */
32 int silc_server_packet_send_real(SilcServer server,
33 SilcSocketConnection sock,
38 /* If disconnecting, ignore the data */
39 if (SILC_IS_DISCONNECTING(sock))
42 /* If rekey protocol is active we must assure that all packets are
43 sent through packet queue. */
44 if (SILC_SERVER_IS_REKEY(sock))
47 /* If outbound data is already pending do not force send */
48 if (SILC_IS_OUTBUF_PENDING(sock))
52 ret = silc_packet_send(sock, force_send);
54 server->stat.packets_sent++;
58 /* Mark that there is some outgoing data available for this connection.
59 This call sets the connection both for input and output (the input
60 is set always and this call keeps the input setting, actually).
61 Actual data sending is performed by silc_server_packet_process. */
62 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
64 /* Mark to socket that data is pending in outgoing buffer. This flag
65 is needed if new data is added to the buffer before the earlier
66 put data is sent to the network. */
67 SILC_SET_OUTBUF_PENDING(sock);
72 /* Assembles a new packet to be sent out to network. This doesn't actually
73 send the packet but creates the packet and fills the outgoing data
74 buffer and marks the packet ready to be sent to network. However, If
75 argument force_send is TRUE the packet is sent immediately and not put
76 to queue. Normal case is that the packet is not sent immediately. */
78 void silc_server_packet_send(SilcServer server,
79 SilcSocketConnection sock,
81 SilcPacketFlags flags,
87 SilcIdType dst_id_type = SILC_ID_NONE;
93 idata = (SilcIDListData)sock->user_data;
95 /* If disconnecting, ignore the data */
96 if (SILC_IS_DISCONNECTING(sock))
99 /* If entry is disabled do not sent anything. */
100 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED &&
101 type != SILC_PACKET_HEARTBEAT) ||
102 sock->user_data == server->id_entry) {
103 SILC_LOG_DEBUG(("Connection is disabled"));
107 /* Get data used in the packet sending, keys and stuff */
109 case SILC_SOCKET_TYPE_CLIENT:
110 if (sock->user_data) {
111 dst_id = ((SilcClientEntry)sock->user_data)->id;
112 dst_id_type = SILC_ID_CLIENT;
115 case SILC_SOCKET_TYPE_SERVER:
116 case SILC_SOCKET_TYPE_ROUTER:
117 if (sock->user_data) {
118 dst_id = ((SilcServerEntry)sock->user_data)->id;
119 dst_id_type = SILC_ID_SERVER;
126 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
127 dst_id_type, data, data_len, force_send);
130 /* Assembles a new packet to be sent out to network. This doesn't actually
131 send the packet but creates the packet and fills the outgoing data
132 buffer and marks the packet ready to be sent to network. However, If
133 argument force_send is TRUE the packet is sent immediately and not put
134 to queue. Normal case is that the packet is not sent immediately.
135 Destination information is sent as argument for this function. */
137 void silc_server_packet_send_dest(SilcServer server,
138 SilcSocketConnection sock,
140 SilcPacketFlags flags,
142 SilcIdType dst_id_type,
147 SilcPacketContext packetdata;
148 const SilcBufferStruct packet;
149 SilcIDListData idata;
150 SilcCipher cipher = NULL;
151 SilcHmac hmac = NULL;
152 SilcUInt32 sequence = 0;
153 unsigned char *dst_id_data = NULL;
154 SilcUInt32 dst_id_len = 0;
157 /* If disconnecting, ignore the data */
158 if (!sock || SILC_IS_DISCONNECTING(sock))
161 idata = (SilcIDListData)sock->user_data;
163 /* If entry is disabled do not sent anything. */
164 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED) ||
165 sock->user_data == server->id_entry) {
166 SILC_LOG_DEBUG(("Connection is disabled"));
170 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
173 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
174 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
178 cipher = idata->send_key;
179 hmac = idata->hmac_send;
180 sequence = idata->psn_send++;
181 block_len = silc_cipher_get_block_len(cipher);
184 /* Set the packet context pointers */
185 packetdata.type = type;
186 packetdata.flags = flags;
187 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
188 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
189 packetdata.src_id_type = SILC_ID_SERVER;
190 packetdata.dst_id = dst_id_data;
191 packetdata.dst_id_len = dst_id_len;
192 packetdata.dst_id_type = dst_id_type;
193 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
194 packetdata.src_id_len +
195 packetdata.dst_id_len));
196 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
197 packetdata.src_id_len + dst_id_len;
198 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
200 /* Create the outgoing packet */
201 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock,
202 data, data_len, (const SilcBuffer)&packet)) {
203 SILC_LOG_ERROR(("Cannot assemble packet"));
207 /* Encrypt the packet */
208 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
210 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
211 packet.data, packet.len);
213 /* Now actually send the packet */
214 silc_server_packet_send_real(server, sock, force_send);
217 if (packetdata.src_id)
218 silc_free(packetdata.src_id);
219 if (packetdata.dst_id)
220 silc_free(packetdata.dst_id);
223 /* Assembles a new packet to be sent out to network. This doesn't actually
224 send the packet but creates the packet and fills the outgoing data
225 buffer and marks the packet ready to be sent to network. However, If
226 argument force_send is TRUE the packet is sent immediately and not put
227 to queue. Normal case is that the packet is not sent immediately.
228 The source and destination information is sent as argument for this
231 void silc_server_packet_send_srcdest(SilcServer server,
232 SilcSocketConnection sock,
234 SilcPacketFlags flags,
236 SilcIdType src_id_type,
238 SilcIdType dst_id_type,
243 SilcPacketContext packetdata;
244 const SilcBufferStruct packet;
245 SilcIDListData idata;
246 SilcCipher cipher = NULL;
247 SilcHmac hmac = NULL;
248 SilcUInt32 sequence = 0;
249 unsigned char *dst_id_data = NULL;
250 SilcUInt32 dst_id_len = 0;
251 unsigned char *src_id_data = NULL;
252 SilcUInt32 src_id_len = 0;
255 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
260 /* Get data used in the packet sending, keys and stuff */
261 idata = (SilcIDListData)sock->user_data;
263 /* If entry is disabled do not sent anything. */
264 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED) ||
265 sock->user_data == server->id_entry) {
266 SILC_LOG_DEBUG(("Connection is disabled"));
271 cipher = idata->send_key;
272 hmac = idata->hmac_send;
273 sequence = idata->psn_send++;
274 block_len = silc_cipher_get_block_len(cipher);
278 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
279 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
283 src_id_data = silc_id_id2str(src_id, src_id_type);
284 src_id_len = silc_id_get_len(src_id, src_id_type);
287 /* Set the packet context pointers */
288 packetdata.type = type;
289 packetdata.flags = flags;
290 packetdata.src_id = src_id_data;
291 packetdata.src_id_len = src_id_len;
292 packetdata.src_id_type = src_id_type;
293 packetdata.dst_id = dst_id_data;
294 packetdata.dst_id_len = dst_id_len;
295 packetdata.dst_id_type = dst_id_type;
296 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
297 packetdata.src_id_len +
299 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
300 packetdata.src_id_len + dst_id_len;
301 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
303 /* Create the outgoing packet */
304 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock, data,
305 data_len, (const SilcBuffer)&packet)) {
306 SILC_LOG_ERROR(("Cannot assemble packe"));
310 /* Encrypt the packet */
311 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
313 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
314 packet.data, packet.len);
316 /* Now actually send the packet */
317 silc_server_packet_send_real(server, sock, force_send);
320 if (packetdata.src_id)
321 silc_free(packetdata.src_id);
322 if (packetdata.dst_id)
323 silc_free(packetdata.dst_id);
326 /* Broadcast received packet to our primary route. This function is used
327 by router to further route received broadcast packet. It is expected
328 that the broadcast flag from the packet is checked before calling this
329 function. This does not test or set the broadcast flag. */
331 void silc_server_packet_broadcast(SilcServer server,
332 SilcSocketConnection sock,
333 SilcPacketContext *packet)
335 SilcBuffer buffer = packet->buffer;
336 SilcIDListData idata;
342 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
344 /* If the packet is originated from our primary route we are
345 not allowed to send the packet. */
346 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
347 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
348 const SilcBufferStruct p;
350 idata = (SilcIDListData)sock->user_data;
352 silc_buffer_push(buffer, buffer->data - buffer->head);
353 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
354 (const SilcBuffer)&p)) {
355 SILC_LOG_ERROR(("Cannot send packet"));
359 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
360 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
361 (SilcBuffer)&p, p.len);
363 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
364 p.len), p.data, p.len);
366 /* Now actually send the packet */
367 silc_server_packet_send_real(server, sock, TRUE);
372 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
373 "original sender of this packet"));
377 /* Routes received packet to `sock'. This is used to route the packets that
378 router receives but are not destined to it. */
380 void silc_server_packet_route(SilcServer server,
381 SilcSocketConnection sock,
382 SilcPacketContext *packet)
384 SilcBuffer buffer = packet->buffer;
385 const SilcBufferStruct p;
386 SilcIDListData idata;
388 SILC_LOG_DEBUG(("Routing received packet"));
390 idata = (SilcIDListData)sock->user_data;
392 silc_buffer_push(buffer, buffer->data - buffer->head);
393 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
394 (const SilcBuffer)&p)) {
395 SILC_LOG_ERROR(("Cannot send packet"));
398 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
399 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
400 (SilcBuffer)&p, p.len);
402 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
403 p.len), p.data, p.len);
405 /* Now actually send the packet */
406 silc_server_packet_send_real(server, sock, TRUE);
409 /* This routine can be used to send a packet to table of clients provided
410 in `clients'. If `route' is FALSE the packet is routed only to local
411 clients (for server locally connected, and for router local cell). */
413 void silc_server_packet_send_clients(SilcServer server,
414 SilcHashTable clients,
416 SilcPacketFlags flags,
422 SilcSocketConnection sock = NULL;
423 SilcHashTableList htl;
424 SilcClientEntry client = NULL;
425 SilcServerEntry *routed = NULL;
426 SilcUInt32 routed_count = 0;
430 if (!silc_hash_table_count(clients))
433 SILC_LOG_DEBUG(("Sending packet to %d clients",
434 silc_hash_table_count(clients)));
436 /* Send to all clients in table */
437 silc_hash_table_list(clients, &htl);
438 while (silc_hash_table_get(&htl, NULL, (void **)&client)) {
439 /* If client has router set it is not locally connected client and
440 we will route the message to the router set in the client. Though,
441 send locally connected server in all cases. */
442 if (server->server_type == SILC_ROUTER && client->router &&
443 ((!route && client->router->router == server->id_entry) || route)) {
445 /* Check if we have sent the packet to this route already */
446 for (k = 0; k < routed_count; k++)
447 if (routed[k] == client->router)
449 if (k < routed_count)
452 /* Route only once to router */
453 sock = (SilcSocketConnection)client->router->connection;
454 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
460 /* Send the packet */
461 silc_server_packet_send_dest(server, sock, type, flags,
462 client->router->id, SILC_ID_SERVER,
463 data, data_len, force_send);
465 /* Mark this route routed already */
466 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
467 routed[routed_count++] = client->router;
474 /* Send to locally connected client */
475 sock = (SilcSocketConnection)client->connection;
479 silc_server_packet_send_dest(server, sock, type, flags,
480 client->id, SILC_ID_CLIENT,
481 data, data_len, force_send);
483 silc_hash_table_list_reset(&htl);
487 /* Internal routine to actually create the channel packet and send it
488 to network. This is common function in channel message sending. If
489 `channel_message' is TRUE this encrypts the message as it is strictly
490 a channel message. If FALSE normal encryption process is used. */
493 silc_server_packet_send_to_channel_real(SilcServer server,
494 SilcSocketConnection sock,
495 SilcPacketContext *packet,
501 bool channel_message,
505 const SilcBufferStruct p;
510 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
512 packet->dst_id_len));
513 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
514 packet->src_id_len + packet->dst_id_len;
516 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
518 packet->padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
520 packet->dst_id_len), block_len);
522 packet->padlen = SILC_PACKET_PADLEN(packet->truelen, block_len);
524 /* Put the data to buffer, assemble and encrypt the packet. The packet
525 is encrypted with normal session key shared with the client, unless
526 the `channel_message' is TRUE. */
527 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
528 data_len, (const SilcBuffer)&p)) {
529 SILC_LOG_ERROR(("Cannot assemble packet"));
534 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
535 SILC_PACKET_HEADER_LEN + packet->src_id_len +
536 packet->dst_id_len + packet->padlen);
538 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
540 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
543 /* Now actually send the packet */
544 silc_server_packet_send_real(server, sock, force_send);
547 /* This routine is used by the server to send packets to channel. The
548 packet sent with this function is distributed to all clients on
549 the channel. Usually this is used to send notify messages to the
550 channel, things like notify about new user joining to the channel.
551 If `route' is FALSE then the packet is sent only locally and will not
552 be routed anywhere (for router locally means cell wide). If `sender'
553 is provided then the packet is not sent to that connection since it
554 originally came from it. If `send_to_clients' is FALSE then the
555 packet is not sent clients, only servers. */
557 void silc_server_packet_send_to_channel(SilcServer server,
558 SilcSocketConnection sender,
559 SilcChannelEntry channel,
566 SilcSocketConnection sock = NULL;
567 SilcPacketContext packetdata;
568 SilcClientEntry client = NULL;
569 SilcServerEntry *routed = NULL;
570 SilcChannelClientEntry chl;
571 SilcHashTableList htl;
572 SilcIDListData idata;
573 SilcUInt32 routed_count = 0;
577 /* This doesn't send channel message packets */
578 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
580 /* Set the packet context pointers. */
581 packetdata.flags = 0;
582 packetdata.type = type;
583 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
584 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
585 packetdata.src_id_type = SILC_ID_SERVER;
586 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
587 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
588 packetdata.dst_id_type = SILC_ID_CHANNEL;
590 /* If there are global users in the channel we will send the message
591 first to our router for further routing. */
592 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
593 channel->global_users) {
594 SilcServerEntry router;
596 /* Get data used in packet header encryption, keys and stuff. */
597 router = server->router;
598 sock = (SilcSocketConnection)router->connection;
599 idata = (SilcIDListData)router;
601 if (sock != sender) {
602 SILC_LOG_DEBUG(("Sending packet to router for routing"));
603 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
607 data, data_len, FALSE,
612 if (!silc_hash_table_count(channel->user_list)) {
613 SILC_LOG_DEBUG(("Channel %s is empty", channel->channel_name));
617 SILC_LOG_DEBUG(("Sending %s to channel %s",
618 silc_get_packet_name(type), channel->channel_name));
620 routed = silc_calloc(silc_hash_table_count(channel->user_list),
623 /* Send the message to clients on the channel's client list. */
624 silc_hash_table_list(channel->user_list, &htl);
625 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
626 client = chl->client;
630 /* If client has router set it is not locally connected client and
631 we will route the message to the router set in the client. Though,
632 send locally connected server in all cases. */
633 if (server->server_type == SILC_ROUTER && client->router &&
634 ((!route && client->router->router == server->id_entry) || route)) {
636 /* Check if we have sent the packet to this route already */
637 for (k = 0; k < routed_count; k++)
638 if (routed[k] == client->router)
640 if (k < routed_count)
643 /* Get data used in packet header encryption, keys and stuff. */
644 sock = (SilcSocketConnection)client->router->connection;
645 idata = (SilcIDListData)client->router;
647 if (sender && sock == sender)
650 /* Route only once to router. Protocol prohibits sending channel
651 messages to more than one router. */
652 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
658 SILC_LOG_DEBUG(("Sending packet to client %s",
659 client->nickname ? client->nickname :
660 (unsigned char *)""));
662 /* Send the packet */
663 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
667 data, data_len, FALSE,
670 /* Mark this route routed already */
671 routed[routed_count++] = client->router;
678 /* Send to locally connected client */
680 /* Get data used in packet header encryption, keys and stuff. */
681 sock = (SilcSocketConnection)client->connection;
682 idata = (SilcIDListData)client;
684 if (!sock || (sender && sock == sender))
687 SILC_LOG_DEBUG(("Sending packet to client %s",
688 client->nickname ? client->nickname :
689 (unsigned char *)""));
691 /* Send the packet */
692 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
696 data, data_len, FALSE,
699 silc_hash_table_list_reset(&htl);
703 silc_free(packetdata.src_id);
704 silc_free(packetdata.dst_id);
707 /* This checks whether the relayed packet came from router. If it did
708 then we'll need to encrypt it with the channel key. This is called
709 from the silc_server_packet_relay_to_channel. */
712 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
713 SilcSocketConnection sock,
714 SilcChannelEntry channel,
716 unsigned int data_len)
718 /* If we are router and the packet came from router and private key
719 has not been set for the channel then we must encrypt the packet
720 as it was decrypted with the session key shared between us and the
721 router which sent it. This is so, because cells does not share the
723 if (server->server_type == SILC_ROUTER &&
724 sock->type == SILC_SOCKET_TYPE_ROUTER &&
725 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
726 channel->channel_key) {
727 SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
728 SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
729 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
731 if (data_len <= mac_len + iv_len) {
732 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
736 memcpy(iv, data + (data_len - iv_len), iv_len);
737 silc_channel_message_payload_encrypt(data, data_len - iv_len - mac_len,
738 data_len, iv, iv_len,
739 channel->channel_key, channel->hmac);
745 /* This routine is explicitly used to relay messages to some channel.
746 Packets sent with this function we have received earlier and are
747 totally encrypted. This just sends the packet to all clients on
748 the channel. If the sender of the packet is someone on the channel
749 the message will not be sent to that client. The SILC Packet header
750 is encrypted with the session key shared between us and the client.
751 MAC is also computed before encrypting the header. Rest of the
752 packet will be untouched. */
754 void silc_server_packet_relay_to_channel(SilcServer server,
755 SilcSocketConnection sender_sock,
756 SilcChannelEntry channel,
758 SilcIdType sender_type,
759 SilcClientEntry sender_entry,
764 SilcSocketConnection sock = NULL;
765 SilcPacketContext packetdata;
766 SilcClientEntry client = NULL;
767 SilcServerEntry *routed = NULL;
768 SilcChannelClientEntry chl, chl_sender;
769 SilcUInt32 routed_count = 0;
770 SilcIDListData idata;
771 SilcHashTableList htl;
775 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
778 SILC_LOG_DEBUG(("Relaying packet to channel %s", channel->channel_name));
780 /* This encrypts the packet, if needed. It will be encrypted if
781 it came from the router thus it needs to be encrypted with the
782 channel key. If the channel key does not exist, then we know we
783 don't have a single local user on the channel. */
784 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
789 /* Set the packet context pointers. */
790 packetdata.flags = 0;
791 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
792 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
793 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
794 packetdata.src_id_type = sender_type;
795 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
796 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
797 packetdata.dst_id_type = SILC_ID_CHANNEL;
799 /* If there are global users in the channel we will send the message
800 first to our router for further routing. */
801 if (server->server_type != SILC_ROUTER && !server->standalone &&
802 channel->global_users) {
803 SilcServerEntry router = server->router;
805 /* Check that the sender is not our router. */
806 if (sender_sock != (SilcSocketConnection)router->connection) {
808 /* Get data used in packet header encryption, keys and stuff. */
809 sock = (SilcSocketConnection)router->connection;
810 idata = (SilcIDListData)router;
812 SILC_LOG_DEBUG(("Sending message to router for routing"));
814 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
818 data, data_len, TRUE,
823 routed = silc_calloc(silc_hash_table_count(channel->user_list),
826 /* Assure we won't route the message back to the sender's way. */
827 if (sender_entry->router)
828 routed[routed_count++] = sender_entry->router;
830 /* Send the message to clients on the channel's client list. */
831 silc_hash_table_list(channel->user_list, &htl);
832 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
833 client = chl->client;
834 if (!client || client == sender_entry)
837 /* Check whether message sending is blocked */
838 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
840 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
841 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
842 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
844 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
845 sender_entry->mode & SILC_UMODE_ROBOT)
848 /* If the client has set router it means that it is not locally
849 connected client and we will route the packet further. */
850 if (server->server_type == SILC_ROUTER && client->router) {
852 /* Check if we have sent the packet to this route already */
853 for (k = 0; k < routed_count; k++)
854 if (routed[k] == client->router)
856 if (k < routed_count)
859 /* Get data used in packet header encryption, keys and stuff. */
860 sock = (SilcSocketConnection)client->router->connection;
861 idata = (SilcIDListData)client->router;
863 /* Check if the sender socket is the same as this client's router
865 if (sender_sock && sock == sender_sock)
868 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
869 silc_id_render(client->id, SILC_ID_CLIENT),
870 sock->hostname, sock->ip));
872 /* Mark this route routed already. */
873 routed[routed_count++] = client->router;
875 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
876 /* The remote connection is router then we'll decrypt the
877 channel message and re-encrypt it with the session key shared
878 between us and the remote router. This is done because the
879 channel keys are cell specific and we have different channel
880 key than the remote router has. */
882 /* Route only once to router. Protocol prohibits sending channel
883 messages to more than one router. */
888 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
890 /* If private key mode is not set then decrypt the packet
892 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
893 channel->channel_key) {
894 unsigned char tmp[SILC_PACKET_MAX_LEN];
896 if (data_len > SILC_PACKET_MAX_LEN)
897 data_len = SILC_PACKET_MAX_LEN;
898 memcpy(tmp, data, data_len);
900 /* Decrypt the channel message (we don't check the MAC) */
901 silc_channel_message_payload_decrypt(tmp, data_len,
902 channel->channel_key, NULL);
904 /* Now re-encrypt and send it to the router */
905 silc_server_packet_send_srcdest(server, sock,
906 SILC_PACKET_CHANNEL_MESSAGE, 0,
907 sender_id, sender_type,
908 channel->id, SILC_ID_CHANNEL,
909 tmp, data_len, force_send);
911 /* Private key mode is set, we don't have the channel key, so
912 just re-encrypt the entire packet and send it to the router. */
913 silc_server_packet_send_srcdest(server, sock,
914 SILC_PACKET_CHANNEL_MESSAGE, 0,
915 sender_id, sender_type,
916 channel->id, SILC_ID_CHANNEL,
917 data, data_len, force_send);
920 /* Send the packet to normal server */
921 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
925 data, data_len, TRUE,
935 /* Get data used in packet header encryption, keys and stuff. */
936 sock = (SilcSocketConnection)client->connection;
937 idata = (SilcIDListData)client;
939 if (!sock || (sender_sock && sock == sender_sock))
942 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
943 silc_id_render(client->id, SILC_ID_CLIENT),
944 sock->hostname, sock->ip));
946 /* Send the packet */
947 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
951 data, data_len, TRUE,
955 silc_hash_table_list_reset(&htl);
957 silc_free(packetdata.src_id);
958 silc_free(packetdata.dst_id);
961 /* This function is used to send packets strictly to all local clients
962 on a particular channel. This is used for example to distribute new
963 channel key to all our locally connected clients on the channel.
964 The packets are always encrypted with the session key shared between
965 the client, this means these are not _to the channel_ but _to the client_
968 void silc_server_packet_send_local_channel(SilcServer server,
969 SilcChannelEntry channel,
971 SilcPacketFlags flags,
976 SilcChannelClientEntry chl;
977 SilcHashTableList htl;
978 SilcSocketConnection sock = NULL;
980 SILC_LOG_DEBUG(("Send packet to local clients on channel %s",
981 channel->channel_name));
983 /* Send the message to clients on the channel's client list. */
984 silc_hash_table_list(channel->user_list, &htl);
985 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
986 if (chl->client && !chl->client->router) {
987 sock = (SilcSocketConnection)chl->client->connection;
989 /* Send the packet to the client */
990 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
991 SILC_ID_CLIENT, data, data_len,
995 silc_hash_table_list_reset(&htl);
998 /* Routine used to send (relay, route) private messages to some destination.
999 If the private message key does not exist then the message is re-encrypted,
1000 otherwise we just pass it along. This really is not used to send new
1001 private messages (as server does not send them) but to relay received
1002 private messages. */
1004 void silc_server_send_private_message(SilcServer server,
1005 SilcSocketConnection dst_sock,
1008 SilcUInt32 sequence,
1009 SilcPacketContext *packet)
1011 SilcBuffer buffer = packet->buffer;
1012 const SilcBufferStruct p;
1014 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1015 + packet->dst_id_len + packet->padlen);
1016 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
1017 (const SilcBuffer)&p)) {
1018 SILC_LOG_ERROR(("Cannot send packet"));
1021 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1023 /* Re-encrypt and send if private messge key does not exist */
1024 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
1025 /* Re-encrypt packet */
1026 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
1028 /* Key exist so encrypt just header and send it */
1029 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
1030 SILC_PACKET_HEADER_LEN + packet->src_id_len +
1031 packet->dst_id_len + packet->padlen);
1034 /* Send the packet */
1035 silc_server_packet_send_real(server, dst_sock, FALSE);
1038 /* Sends current motd to client */
1040 void silc_server_send_motd(SilcServer server,
1041 SilcSocketConnection sock)
1043 char *motd, *motd_file = NULL;
1044 SilcUInt32 motd_len;
1047 motd_file = server->config->server_info->motd_file;
1050 motd = silc_file_readfile(motd_file, &motd_len);
1054 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1060 /* Sends error message. Error messages may or may not have any
1063 void silc_server_send_error(SilcServer server,
1064 SilcSocketConnection sock,
1065 const char *fmt, ...)
1068 unsigned char buf[4096];
1070 memset(buf, 0, sizeof(buf));
1072 vsprintf(buf, fmt, ap);
1075 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1076 buf, strlen(buf), FALSE);
1079 /* Sends notify message. If format is TRUE the variable arguments are
1080 formatted and the formatted string is sent as argument payload. If it is
1081 FALSE then each argument is sent as separate argument and their format
1082 in the argument list must be { argument data, argument length }. */
1084 void silc_server_send_notify(SilcServer server,
1085 SilcSocketConnection sock,
1087 SilcNotifyType type,
1088 SilcUInt32 argc, ...)
1095 packet = silc_notify_payload_encode(type, argc, ap);
1096 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1097 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1098 packet->data, packet->len, FALSE);
1100 /* Send to backup routers if this is being broadcasted to primary
1101 router. The silc_server_backup_send checks further whether to
1102 actually send it or not. */
1103 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1104 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1105 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1106 packet->data, packet->len, FALSE, TRUE);
1108 silc_buffer_free(packet);
1112 /* Sends notify message and gets the arguments from the `args' Argument
1115 void silc_server_send_notify_args(SilcServer server,
1116 SilcSocketConnection sock,
1118 SilcNotifyType type,
1124 packet = silc_notify_payload_encode_args(type, argc, args);
1125 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1126 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1127 packet->data, packet->len, FALSE);
1128 silc_buffer_free(packet);
1131 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1132 `old_id' with the `new_id'. */
1134 void silc_server_send_notify_channel_change(SilcServer server,
1135 SilcSocketConnection sock,
1137 SilcChannelID *old_id,
1138 SilcChannelID *new_id)
1140 SilcBuffer idp1, idp2;
1142 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1143 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1145 silc_server_send_notify(server, sock, broadcast,
1146 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1147 2, idp1->data, idp1->len, idp2->data, idp2->len);
1148 silc_buffer_free(idp1);
1149 silc_buffer_free(idp2);
1152 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1153 `old_id' with the `new_id'. */
1155 void silc_server_send_notify_nick_change(SilcServer server,
1156 SilcSocketConnection sock,
1158 SilcClientID *old_id,
1159 SilcClientID *new_id,
1160 const char *nickname)
1162 SilcBuffer idp1, idp2;
1164 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1165 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1167 silc_server_send_notify(server, sock, broadcast,
1168 SILC_NOTIFY_TYPE_NICK_CHANGE,
1169 3, idp1->data, idp1->len, idp2->data, idp2->len,
1170 nickname, nickname ? strlen(nickname) : 0);
1171 silc_buffer_free(idp1);
1172 silc_buffer_free(idp2);
1175 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1176 has joined to the `channel'. */
1178 void silc_server_send_notify_join(SilcServer server,
1179 SilcSocketConnection sock,
1181 SilcChannelEntry channel,
1182 SilcClientID *client_id)
1184 SilcBuffer idp1, idp2;
1186 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1187 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1188 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1189 2, idp1->data, idp1->len,
1190 idp2->data, idp2->len);
1191 silc_buffer_free(idp1);
1192 silc_buffer_free(idp2);
1195 /* Sends LEAVE notify type. This tells that `client_id' has left the
1196 `channel'. The Notify packet is always destined to the channel. */
1198 void silc_server_send_notify_leave(SilcServer server,
1199 SilcSocketConnection sock,
1201 SilcChannelEntry channel,
1202 SilcClientID *client_id)
1206 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1207 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1208 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1209 1, idp->data, idp->len);
1210 silc_buffer_free(idp);
1213 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1214 `channel' mode to `mode. The Notify packet is always destined to
1217 void silc_server_send_notify_cmode(SilcServer server,
1218 SilcSocketConnection sock,
1220 SilcChannelEntry channel,
1221 SilcUInt32 mode_mask,
1222 void *id, SilcIdType id_type,
1223 const char *cipher, const char *hmac,
1224 const char *passphrase,
1225 SilcPublicKey founder_key)
1228 unsigned char mode[4], *key = NULL;
1229 SilcUInt32 key_len = 0;
1231 idp = silc_id_payload_encode((void *)id, id_type);
1232 SILC_PUT32_MSB(mode_mask, mode);
1234 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1236 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1237 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1238 6, idp->data, idp->len,
1240 cipher, cipher ? strlen(cipher) : 0,
1241 hmac, hmac ? strlen(hmac) : 0,
1242 passphrase, passphrase ?
1243 strlen(passphrase) : 0,
1246 silc_buffer_free(idp);
1249 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1250 `target' client's mode on `channel'. The notify packet is always
1251 destined to the channel. */
1253 void silc_server_send_notify_cumode(SilcServer server,
1254 SilcSocketConnection sock,
1256 SilcChannelEntry channel,
1257 SilcUInt32 mode_mask,
1258 void *id, SilcIdType id_type,
1259 SilcClientID *target,
1260 SilcPublicKey founder_key)
1262 SilcBuffer idp1, idp2;
1263 unsigned char mode[4], *key = NULL;
1264 SilcUInt32 key_len = 0;
1266 idp1 = silc_id_payload_encode((void *)id, id_type);
1267 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1268 SILC_PUT32_MSB(mode_mask, mode);
1270 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1272 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1274 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
1275 idp1->data, idp1->len,
1277 idp2->data, idp2->len,
1280 silc_buffer_free(idp1);
1281 silc_buffer_free(idp2);
1284 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1285 left SILC network. This function is used only between server and router
1286 traffic. This is not used to send the notify to the channel for
1287 client. The `message may be NULL. */
1289 void silc_server_send_notify_signoff(SilcServer server,
1290 SilcSocketConnection sock,
1292 SilcClientID *client_id,
1293 const char *message)
1297 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1298 silc_server_send_notify(server, sock, broadcast,
1299 SILC_NOTIFY_TYPE_SIGNOFF,
1300 message ? 2 : 1, idp->data, idp->len,
1301 message, message ? strlen(message): 0);
1302 silc_buffer_free(idp);
1305 /* Sends TOPIC_SET notify type. This tells that `id' changed
1306 the `channel's topic to `topic'. The Notify packet is always destined
1307 to the channel. This function is used to send the topic set notifies
1310 void silc_server_send_notify_topic_set(SilcServer server,
1311 SilcSocketConnection sock,
1313 SilcChannelEntry channel,
1314 void *id, SilcIdType id_type,
1319 idp = silc_id_payload_encode(id, id_type);
1320 silc_server_send_notify_dest(server, sock, broadcast,
1321 (void *)channel->id, SILC_ID_CHANNEL,
1322 SILC_NOTIFY_TYPE_TOPIC_SET,
1324 idp->data, idp->len,
1325 topic, topic ? strlen(topic) : 0);
1326 silc_buffer_free(idp);
1329 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1330 was kicked off the channel. The `comment' may indicate the reason
1331 for the kicking. This function is used only between server and router
1334 void silc_server_send_notify_kicked(SilcServer server,
1335 SilcSocketConnection sock,
1337 SilcChannelEntry channel,
1338 SilcClientID *client_id,
1339 SilcClientID *kicker,
1345 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1346 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1347 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1348 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1349 idp1->data, idp1->len,
1350 comment, comment ? strlen(comment) : 0,
1351 idp2->data, idp2->len);
1352 silc_buffer_free(idp1);
1353 silc_buffer_free(idp2);
1356 /* Send KILLED notify type. This tells that the `client_id' client was
1357 killed from the network. The `comment' may indicate the reason
1360 void silc_server_send_notify_killed(SilcServer server,
1361 SilcSocketConnection sock,
1363 SilcClientID *client_id,
1364 const char *comment,
1365 void *killer, SilcIdType killer_type)
1370 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1371 idp2 = silc_id_payload_encode(killer, killer_type);
1372 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1373 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1374 3, idp1->data, idp1->len,
1375 comment, comment ? strlen(comment) : 0,
1376 idp2->data, idp2->len);
1377 silc_buffer_free(idp1);
1378 silc_buffer_free(idp2);
1381 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1382 user mode in the SILC Network was changed. This function is used to
1383 send the packet between routers as broadcast packet. */
1385 void silc_server_send_notify_umode(SilcServer server,
1386 SilcSocketConnection sock,
1388 SilcClientID *client_id,
1389 SilcUInt32 mode_mask)
1392 unsigned char mode[4];
1394 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1395 SILC_PUT32_MSB(mode_mask, mode);
1397 silc_server_send_notify(server, sock, broadcast,
1398 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1399 idp->data, idp->len,
1401 silc_buffer_free(idp);
1404 /* Sends BAN notify type. This tells that ban has been either `add'ed
1405 or `del'eted on the `channel. This function is used to send the packet
1406 between routers as broadcast packet. */
1408 void silc_server_send_notify_ban(SilcServer server,
1409 SilcSocketConnection sock,
1411 SilcChannelEntry channel,
1412 char *add, char *del)
1416 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1417 silc_server_send_notify(server, sock, broadcast,
1418 SILC_NOTIFY_TYPE_BAN, 3,
1419 idp->data, idp->len,
1420 add, add ? strlen(add) : 0,
1421 del, del ? strlen(del) : 0);
1422 silc_buffer_free(idp);
1425 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1426 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1427 This function is used to send the packet between routers as broadcast
1430 void silc_server_send_notify_invite(SilcServer server,
1431 SilcSocketConnection sock,
1433 SilcChannelEntry channel,
1434 SilcClientID *client_id,
1435 char *add, char *del)
1437 SilcBuffer idp, idp2;
1439 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1440 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1441 silc_server_send_notify(server, sock, broadcast,
1442 SILC_NOTIFY_TYPE_INVITE, 5,
1443 idp->data, idp->len,
1444 channel->channel_name, strlen(channel->channel_name),
1445 idp2->data, idp2->len,
1446 add, add ? strlen(add) : 0,
1447 del, del ? strlen(del) : 0);
1448 silc_buffer_free(idp);
1449 silc_buffer_free(idp2);
1452 /* Sends WATCH notify type. This tells that the `client' was watched and
1453 its status in the network has changed. */
1455 void silc_server_send_notify_watch(SilcServer server,
1456 SilcSocketConnection sock,
1457 SilcClientEntry watcher,
1458 SilcClientEntry client,
1459 const char *nickname,
1460 SilcNotifyType type)
1463 unsigned char mode[4], n[2];
1465 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1466 SILC_PUT16_MSB(type, n);
1467 SILC_PUT32_MSB(client->mode, mode);
1468 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1469 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1470 4, idp->data, idp->len,
1471 nickname, nickname ? strlen(nickname) : 0,
1473 type != SILC_NOTIFY_TYPE_NONE ?
1474 n : NULL, sizeof(n));
1475 silc_buffer_free(idp);
1478 /* Sends notify message destined to specific entity. */
1480 void silc_server_send_notify_dest(SilcServer server,
1481 SilcSocketConnection sock,
1484 SilcIdType dest_id_type,
1485 SilcNotifyType type,
1486 SilcUInt32 argc, ...)
1493 packet = silc_notify_payload_encode(type, argc, ap);
1494 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1495 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1496 dest_id, dest_id_type,
1497 packet->data, packet->len, FALSE);
1499 /* Send to backup routers if this is being broadcasted to primary
1500 router. The silc_server_backup_send checks further whether to
1501 actually send it or not. */
1502 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1503 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1504 silc_server_backup_send_dest(server, NULL, SILC_PACKET_NOTIFY, 0,
1505 dest_id, dest_id_type,
1506 packet->data, packet->len, FALSE, TRUE);
1508 silc_buffer_free(packet);
1512 /* Sends notify message to a channel. The notify message sent is
1513 distributed to all clients on the channel. If `route_notify' is TRUE
1514 then the notify may be routed to primary route or to some other routers.
1515 If FALSE it is assured that the notify is sent only locally. If `sender'
1516 is provided then the packet is not sent to that connection since it
1517 originally came from it. */
1519 void silc_server_send_notify_to_channel(SilcServer server,
1520 SilcSocketConnection sender,
1521 SilcChannelEntry channel,
1523 SilcNotifyType type,
1524 SilcUInt32 argc, ...)
1531 packet = silc_notify_payload_encode(type, argc, ap);
1532 silc_server_packet_send_to_channel(server, sender, channel,
1533 SILC_PACKET_NOTIFY, route_notify,
1534 packet->data, packet->len, FALSE);
1535 silc_buffer_free(packet);
1539 /* Send notify message to all channels the client has joined. It is quaranteed
1540 that the message is sent only once to a client (ie. if a client is joined
1541 on two same channel it will receive only one notify message). Also, this
1542 sends only to local clients (locally connected if we are server, and to
1543 local servers if we are router). If `sender' is provided the packet is
1544 not sent to that client at all. */
1546 void silc_server_send_notify_on_channels(SilcServer server,
1547 SilcClientEntry sender,
1548 SilcClientEntry client,
1549 SilcNotifyType type,
1550 SilcUInt32 argc, ...)
1553 SilcSocketConnection sock = NULL;
1554 SilcPacketContext packetdata;
1556 SilcClientEntry *sent_clients = NULL;
1557 SilcUInt32 sent_clients_count = 0;
1558 SilcServerEntry *routed = NULL;
1559 SilcUInt32 routed_count = 0;
1560 SilcHashTableList htl, htl2;
1561 SilcChannelEntry channel;
1562 SilcChannelClientEntry chl, chl2;
1563 SilcIDListData idata;
1565 unsigned char *data;
1566 SilcUInt32 data_len;
1567 bool force_send = FALSE;
1570 if (!silc_hash_table_count(client->channels)) {
1571 SILC_LOG_DEBUG(("Client is not joined to any channels"));
1575 SILC_LOG_DEBUG(("Sending notify to joined channels"));
1578 packet = silc_notify_payload_encode(type, argc, ap);
1579 data = packet->data;
1580 data_len = packet->len;
1582 /* Set the packet context pointers. */
1583 packetdata.flags = 0;
1584 packetdata.type = SILC_PACKET_NOTIFY;
1585 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1586 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1587 packetdata.src_id_type = SILC_ID_SERVER;
1589 silc_hash_table_list(client->channels, &htl);
1590 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
1591 channel = chl->channel;
1593 /* Send the message to all clients on the channel's client list. */
1594 silc_hash_table_list(channel->user_list, &htl2);
1595 while (silc_hash_table_get(&htl2, NULL, (void **)&chl2)) {
1598 if (sender && c == sender)
1601 /* Check if we have sent the packet to this client already */
1602 for (k = 0; k < sent_clients_count; k++)
1603 if (sent_clients[k] == c)
1605 if (k < sent_clients_count)
1608 /* If we are router and if this client has router set it is not
1609 locally connected client and we will route the message to the
1610 router set in the client. */
1611 if (c && c->router && server->server_type == SILC_ROUTER) {
1612 /* Check if we have sent the packet to this route already */
1613 for (k = 0; k < routed_count; k++)
1614 if (routed[k] == c->router)
1616 if (k < routed_count)
1619 /* Get data used in packet header encryption, keys and stuff. */
1620 sock = (SilcSocketConnection)c->router->connection;
1621 idata = (SilcIDListData)c->router;
1624 SILC_LOG_DEBUG(("*****************"));
1625 SILC_LOG_DEBUG(("client->router->id %s",
1626 silc_id_render(c->router->id, SILC_ID_SERVER)));
1627 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1628 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1631 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1632 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1633 packetdata.dst_id_type = SILC_ID_SERVER;
1635 /* Send the packet */
1636 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1640 data, data_len, FALSE,
1643 silc_free(packetdata.dst_id);
1645 /* We want to make sure that the packet is routed to same router
1646 only once. Mark this route as sent route. */
1647 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1648 routed[routed_count++] = c->router;
1655 /* Send to locally connected client */
1658 /* Get data used in packet header encryption, keys and stuff. */
1659 sock = (SilcSocketConnection)c->connection;
1660 idata = (SilcIDListData)c;
1665 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1666 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1667 packetdata.dst_id_type = SILC_ID_CLIENT;
1669 /* Send the packet */
1670 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1674 data, data_len, FALSE,
1677 silc_free(packetdata.dst_id);
1679 /* Make sure that we send the notify only once per client. */
1680 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1681 (sent_clients_count + 1));
1682 sent_clients[sent_clients_count++] = c;
1685 silc_hash_table_list_reset(&htl2);
1688 silc_hash_table_list_reset(&htl);
1690 silc_free(sent_clients);
1691 silc_free(packetdata.src_id);
1692 silc_buffer_free(packet);
1696 /* Sends New ID Payload to remote end. The packet is used to distribute
1697 information about new registered clients, servers, channel etc. usually
1698 to routers so that they can keep these information up to date.
1699 If the argument `broadcast' is TRUE then the packet is sent as
1700 broadcast packet. */
1702 void silc_server_send_new_id(SilcServer server,
1703 SilcSocketConnection sock,
1705 void *id, SilcIdType id_type,
1710 SILC_LOG_DEBUG(("Sending new ID"));
1712 idp = silc_id_payload_encode(id, id_type);
1713 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1714 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1715 idp->data, idp->len, FALSE);
1716 silc_buffer_free(idp);
1719 /* Send New Channel Payload to notify about newly created channel in the
1720 SILC network. Router uses this to notify other routers in the network
1721 about new channel. This packet is broadcasted by router. */
1723 void silc_server_send_new_channel(SilcServer server,
1724 SilcSocketConnection sock,
1728 SilcUInt32 channel_id_len,
1733 SilcUInt32 name_len = strlen(channel_name);
1735 SILC_LOG_DEBUG(("Sending new channel"));
1737 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1741 /* Encode the channel payload */
1742 packet = silc_channel_payload_encode(channel_name, name_len,
1743 cid, channel_id_len, mode);
1745 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1746 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1747 packet->data, packet->len, FALSE);
1750 silc_buffer_free(packet);
1753 /* Send Channel Key payload to distribute the new channel key. Normal server
1754 sends this to router when new client joins to existing channel. Router
1755 sends this to the local server who sent the join command in case where
1756 the channel did not exist yet. Both normal and router servers uses this
1757 also to send this to locally connected clients on the channel. This
1758 must not be broadcasted packet. Routers do not send this to each other.
1759 If `sender is provided then the packet is not sent to that connection since
1760 it originally came from it. */
1762 void silc_server_send_channel_key(SilcServer server,
1763 SilcSocketConnection sender,
1764 SilcChannelEntry channel,
1765 unsigned char route)
1768 unsigned char *chid;
1771 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1773 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1777 /* Encode channel key packet */
1778 tmp_len = strlen(channel->channel_key->cipher->name);
1779 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1782 channel->channel_key->cipher->name,
1783 channel->key_len / 8, channel->key);
1784 silc_server_packet_send_to_channel(server, sender, channel,
1785 SILC_PACKET_CHANNEL_KEY,
1786 route, packet->data, packet->len,
1788 silc_buffer_free(packet);
1792 /* Generic function to send any command. The arguments must be sent already
1793 encoded into correct form in correct order. */
1795 void silc_server_send_command(SilcServer server,
1796 SilcSocketConnection sock,
1797 SilcCommand command,
1799 SilcUInt32 argc, ...)
1806 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1807 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1808 packet->data, packet->len, TRUE);
1809 silc_buffer_free(packet);
1813 /* Generic function to send any command reply. The arguments must be sent
1814 already encoded into correct form in correct order. */
1816 void silc_server_send_command_reply(SilcServer server,
1817 SilcSocketConnection sock,
1818 SilcCommand command,
1822 SilcUInt32 argc, ...)
1829 packet = silc_command_reply_payload_encode_vap(command, status, error,
1831 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1832 packet->data, packet->len, TRUE);
1833 silc_buffer_free(packet);
1837 /* Generic function to send any command reply. The arguments must be sent
1838 already encoded into correct form in correct order. */
1840 void silc_server_send_dest_command_reply(SilcServer server,
1841 SilcSocketConnection sock,
1843 SilcIdType dst_id_type,
1844 SilcCommand command,
1848 SilcUInt32 argc, ...)
1855 packet = silc_command_reply_payload_encode_vap(command, status, error,
1857 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1858 dst_id, dst_id_type, packet->data,
1860 silc_buffer_free(packet);
1864 /* Send the heartbeat packet. */
1866 void silc_server_send_heartbeat(SilcServer server,
1867 SilcSocketConnection sock)
1869 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1873 /* Generic function to relay packet we've received. This is used to relay
1874 packets to a client but generally can be used to other purposes as well. */
1876 void silc_server_relay_packet(SilcServer server,
1877 SilcSocketConnection dst_sock,
1880 SilcUInt32 sequence,
1881 SilcPacketContext *packet,
1884 const SilcBufferStruct p;
1886 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1887 + packet->dst_id_len + packet->padlen);
1888 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1889 (const SilcBuffer)&p)) {
1890 SILC_LOG_ERROR(("Cannot send packet"));
1893 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1895 /* Re-encrypt packet */
1896 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1898 /* Send the packet */
1899 silc_server_packet_send_real(server, dst_sock, force_send);
1901 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1902 + packet->dst_id_len + packet->padlen);
1905 /* Routine used to send the connection authentication packet. */
1907 void silc_server_send_connection_auth_request(SilcServer server,
1908 SilcSocketConnection sock,
1909 SilcUInt16 conn_type,
1910 SilcAuthMethod auth_meth)
1914 packet = silc_buffer_alloc(4);
1915 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1916 silc_buffer_format(packet,
1917 SILC_STR_UI_SHORT(conn_type),
1918 SILC_STR_UI_SHORT(auth_meth),
1921 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1922 0, packet->data, packet->len, FALSE);
1923 silc_buffer_free(packet);
1926 /* Purge the outgoing packet queue to the network if there is data. This
1927 function can be used to empty the packet queue. It is guaranteed that
1928 after this function returns the outgoing data queue is empty. */
1930 void silc_server_packet_queue_purge(SilcServer server,
1931 SilcSocketConnection sock)
1933 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1934 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1935 server->stat.packets_sent++;
1936 silc_packet_send(sock, TRUE);
1937 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1938 SILC_UNSET_OUTBUF_PENDING(sock);
1939 silc_buffer_clear(sock->outbuf);
1943 /* Send packet to clients that are known to be operators. If server
1944 is router and `route' is TRUE then the packet would go to all operators
1945 in the SILC network. If `route' is FALSE then only local operators
1946 (local for server and cell wide for router). If `local' is TRUE then
1947 only locally connected operators receive the packet. If `local' is
1948 TRUE then `route' is ignored. If server is normal server and `route'
1949 is FALSE it is equivalent to `local' being TRUE. */
1951 void silc_server_send_opers(SilcServer server,
1952 SilcPacketType type,
1953 SilcPacketFlags flags,
1954 bool route, bool local,
1955 unsigned char *data,
1956 SilcUInt32 data_len,
1959 SilcIDCacheList list = NULL;
1960 SilcIDCacheEntry id_cache = NULL;
1961 SilcClientEntry client = NULL;
1962 SilcSocketConnection sock;
1963 SilcServerEntry *routed = NULL;
1964 SilcUInt32 routed_count = 0;
1968 SILC_LOG_DEBUG(("Sending %s packet to operators",
1969 silc_get_packet_name(type)));
1971 /* If local was requested send only locally connected operators. */
1972 if (local || (server->server_type == SILC_SERVER && !route)) {
1973 if (!silc_idcache_get_all(server->local_list->clients, &list) ||
1974 !silc_idcache_list_first(list, &id_cache))
1977 client = (SilcClientEntry)id_cache->context;
1978 if (!client->router && SILC_IS_LOCAL(client) &&
1979 (client->mode & SILC_UMODE_SERVER_OPERATOR ||
1980 client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
1982 /* Send the packet to locally connected operator */
1983 silc_server_packet_send_dest(server, client->connection, type, flags,
1984 client->id, SILC_ID_CLIENT,
1985 data, data_len, force_send);
1988 if (!silc_idcache_list_next(list, &id_cache))
1991 silc_idcache_list_free(list);
1995 if (!silc_idcache_get_all(server->local_list->clients, &list) ||
1996 !silc_idcache_list_first(list, &id_cache))
1999 client = (SilcClientEntry)id_cache->context;
2000 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
2001 !(client->mode & SILC_UMODE_ROUTER_OPERATOR))
2004 if (server->server_type != SILC_SERVER && client->router &&
2005 ((!route && client->router->router == server->id_entry) || route)) {
2007 /* Check if we have sent the packet to this route already */
2008 for (k = 0; k < routed_count; k++)
2009 if (routed[k] == client->router)
2011 if (k < routed_count)
2014 /* Route only once to router */
2015 sock = (SilcSocketConnection)client->router->connection;
2016 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2022 /* Send the packet */
2023 silc_server_packet_send_dest(server, sock, type, flags,
2024 client->id, SILC_ID_CLIENT,
2025 data, data_len, force_send);
2027 /* Mark this route routed already */
2028 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
2029 routed[routed_count++] = client->router;
2033 if (client->router || !client->connection)
2036 /* Send to locally connected client */
2037 sock = (SilcSocketConnection)client->connection;
2038 silc_server_packet_send_dest(server, sock, type, flags,
2039 client->id, SILC_ID_CLIENT,
2040 data, data_len, force_send);
2043 if (!silc_idcache_list_next(list, &id_cache))
2046 silc_idcache_list_free(list);
2048 if (!silc_idcache_get_all(server->global_list->clients, &list) ||
2049 !silc_idcache_list_first(list, &id_cache))
2052 client = (SilcClientEntry)id_cache->context;
2053 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
2054 !(client->mode & SILC_UMODE_ROUTER_OPERATOR))
2057 if (server->server_type != SILC_SERVER && client->router &&
2058 ((!route && client->router->router == server->id_entry) || route)) {
2060 /* Check if we have sent the packet to this route already */
2061 for (k = 0; k < routed_count; k++)
2062 if (routed[k] == client->router)
2064 if (k < routed_count)
2067 /* Route only once to router */
2068 sock = (SilcSocketConnection)client->router->connection;
2069 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2075 /* Send the packet */
2076 silc_server_packet_send_dest(server, sock, type, flags,
2077 client->id, SILC_ID_CLIENT,
2078 data, data_len, force_send);
2080 /* Mark this route routed already */
2081 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
2082 routed[routed_count++] = client->router;
2086 if (client->router || !client->connection)
2089 /* Send to locally connected client */
2090 sock = (SilcSocketConnection)client->connection;
2091 silc_server_packet_send_dest(server, sock, type, flags,
2092 client->id, SILC_ID_CLIENT,
2093 data, data_len, force_send);
2096 if (!silc_idcache_list_next(list, &id_cache))
2099 silc_idcache_list_free(list);
2103 /* Send a notify packet to operators */
2105 void silc_server_send_opers_notify(SilcServer server,
2108 SilcNotifyType type,
2109 SilcUInt32 argc, ...)
2115 packet = silc_notify_payload_encode(type, argc, ap);
2116 silc_server_send_opers(server, SILC_PACKET_NOTIFY, 0,
2117 route, local, packet->data, packet->len,
2119 silc_buffer_free(packet);