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)
103 /* Get data used in the packet sending, keys and stuff */
105 case SILC_SOCKET_TYPE_CLIENT:
106 if (sock->user_data) {
107 dst_id = ((SilcClientEntry)sock->user_data)->id;
108 dst_id_type = SILC_ID_CLIENT;
111 case SILC_SOCKET_TYPE_SERVER:
112 case SILC_SOCKET_TYPE_ROUTER:
113 if (sock->user_data) {
114 dst_id = ((SilcServerEntry)sock->user_data)->id;
115 dst_id_type = SILC_ID_SERVER;
122 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
123 dst_id_type, data, data_len, force_send);
126 /* Assembles a new packet to be sent out to network. This doesn't actually
127 send the packet but creates the packet and fills the outgoing data
128 buffer and marks the packet ready to be sent to network. However, If
129 argument force_send is TRUE the packet is sent immediately and not put
130 to queue. Normal case is that the packet is not sent immediately.
131 Destination information is sent as argument for this function. */
133 void silc_server_packet_send_dest(SilcServer server,
134 SilcSocketConnection sock,
136 SilcPacketFlags flags,
138 SilcIdType dst_id_type,
143 SilcPacketContext packetdata;
144 const SilcBufferStruct packet;
145 SilcIDListData idata;
146 SilcCipher cipher = NULL;
147 SilcHmac hmac = NULL;
148 SilcUInt32 sequence = 0;
149 unsigned char *dst_id_data = NULL;
150 SilcUInt32 dst_id_len = 0;
153 /* If disconnecting, ignore the data */
154 if (!sock || SILC_IS_DISCONNECTING(sock))
157 idata = (SilcIDListData)sock->user_data;
159 /* If entry is disabled do not sent anything. */
160 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
163 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
166 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
167 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
171 cipher = idata->send_key;
172 hmac = idata->hmac_send;
173 sequence = idata->psn_send++;
174 block_len = silc_cipher_get_block_len(cipher);
177 /* Set the packet context pointers */
178 packetdata.type = type;
179 packetdata.flags = flags;
180 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
181 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
182 packetdata.src_id_type = SILC_ID_SERVER;
183 packetdata.dst_id = dst_id_data;
184 packetdata.dst_id_len = dst_id_len;
185 packetdata.dst_id_type = dst_id_type;
186 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
187 packetdata.src_id_len +
188 packetdata.dst_id_len));
189 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
190 packetdata.src_id_len + dst_id_len;
191 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
193 /* Create the outgoing packet */
194 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock,
195 data, data_len, (const SilcBuffer)&packet)) {
196 SILC_LOG_ERROR(("Cannot assemble packet"));
200 /* Encrypt the packet */
201 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
203 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
204 packet.data, packet.len);
206 /* Now actually send the packet */
207 silc_server_packet_send_real(server, sock, force_send);
210 if (packetdata.src_id)
211 silc_free(packetdata.src_id);
212 if (packetdata.dst_id)
213 silc_free(packetdata.dst_id);
216 /* Assembles a new packet to be sent out to network. This doesn't actually
217 send the packet but creates the packet and fills the outgoing data
218 buffer and marks the packet ready to be sent to network. However, If
219 argument force_send is TRUE the packet is sent immediately and not put
220 to queue. Normal case is that the packet is not sent immediately.
221 The source and destination information is sent as argument for this
224 void silc_server_packet_send_srcdest(SilcServer server,
225 SilcSocketConnection sock,
227 SilcPacketFlags flags,
229 SilcIdType src_id_type,
231 SilcIdType dst_id_type,
236 SilcPacketContext packetdata;
237 const SilcBufferStruct packet;
238 SilcIDListData idata;
239 SilcCipher cipher = NULL;
240 SilcHmac hmac = NULL;
241 SilcUInt32 sequence = 0;
242 unsigned char *dst_id_data = NULL;
243 SilcUInt32 dst_id_len = 0;
244 unsigned char *src_id_data = NULL;
245 SilcUInt32 src_id_len = 0;
248 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
253 /* Get data used in the packet sending, keys and stuff */
254 idata = (SilcIDListData)sock->user_data;
257 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
258 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
262 src_id_data = silc_id_id2str(src_id, src_id_type);
263 src_id_len = silc_id_get_len(src_id, src_id_type);
267 cipher = idata->send_key;
268 hmac = idata->hmac_send;
269 sequence = idata->psn_send++;
270 block_len = silc_cipher_get_block_len(cipher);
273 /* Set the packet context pointers */
274 packetdata.type = type;
275 packetdata.flags = flags;
276 packetdata.src_id = src_id_data;
277 packetdata.src_id_len = src_id_len;
278 packetdata.src_id_type = src_id_type;
279 packetdata.dst_id = dst_id_data;
280 packetdata.dst_id_len = dst_id_len;
281 packetdata.dst_id_type = dst_id_type;
282 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
283 packetdata.src_id_len +
285 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
286 packetdata.src_id_len + dst_id_len;
287 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
289 /* Create the outgoing packet */
290 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock, data,
291 data_len, (const SilcBuffer)&packet)) {
292 SILC_LOG_ERROR(("Cannot assemble packe"));
296 /* Encrypt the packet */
297 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
299 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
300 packet.data, packet.len);
302 /* Now actually send the packet */
303 silc_server_packet_send_real(server, sock, force_send);
306 if (packetdata.src_id)
307 silc_free(packetdata.src_id);
308 if (packetdata.dst_id)
309 silc_free(packetdata.dst_id);
312 /* Broadcast received packet to our primary route. This function is used
313 by router to further route received broadcast packet. It is expected
314 that the broadcast flag from the packet is checked before calling this
315 function. This does not test or set the broadcast flag. */
317 void silc_server_packet_broadcast(SilcServer server,
318 SilcSocketConnection sock,
319 SilcPacketContext *packet)
321 SilcBuffer buffer = packet->buffer;
322 SilcIDListData idata;
328 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
330 /* If the packet is originated from our primary route we are
331 not allowed to send the packet. */
332 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
333 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
334 const SilcBufferStruct p;
336 idata = (SilcIDListData)sock->user_data;
338 silc_buffer_push(buffer, buffer->data - buffer->head);
339 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
340 (const SilcBuffer)&p)) {
341 SILC_LOG_ERROR(("Cannot send packet"));
345 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
346 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
347 (SilcBuffer)&p, p.len);
349 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
350 p.len), p.data, p.len);
352 /* Now actually send the packet */
353 silc_server_packet_send_real(server, sock, TRUE);
358 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
359 "original sender of this packet"));
363 /* Routes received packet to `sock'. This is used to route the packets that
364 router receives but are not destined to it. */
366 void silc_server_packet_route(SilcServer server,
367 SilcSocketConnection sock,
368 SilcPacketContext *packet)
370 SilcBuffer buffer = packet->buffer;
371 const SilcBufferStruct p;
372 SilcIDListData idata;
374 SILC_LOG_DEBUG(("Routing received packet"));
376 idata = (SilcIDListData)sock->user_data;
378 silc_buffer_push(buffer, buffer->data - buffer->head);
379 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
380 (const SilcBuffer)&p)) {
381 SILC_LOG_ERROR(("Cannot send packet"));
384 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
385 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
386 (SilcBuffer)&p, p.len);
388 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
389 p.len), p.data, p.len);
391 /* Now actually send the packet */
392 silc_server_packet_send_real(server, sock, TRUE);
395 /* This routine can be used to send a packet to table of clients provided
396 in `clients'. If `route' is FALSE the packet is routed only to local
397 clients (for server locally connected, and for router local cell). */
399 void silc_server_packet_send_clients(SilcServer server,
400 SilcHashTable clients,
402 SilcPacketFlags flags,
408 SilcSocketConnection sock = NULL;
409 SilcHashTableList htl;
410 SilcClientEntry client = NULL;
411 SilcServerEntry *routed = NULL;
412 SilcUInt32 routed_count = 0;
416 SILC_LOG_DEBUG(("Sending packet to list of clients"));
418 /* Send to all clients in table */
419 silc_hash_table_list(clients, &htl);
420 while (silc_hash_table_get(&htl, NULL, (void **)&client)) {
421 /* If client has router set it is not locally connected client and
422 we will route the message to the router set in the client. Though,
423 send locally connected server in all cases. */
424 if (server->server_type == SILC_ROUTER && client->router &&
425 ((!route && client->router->router == server->id_entry) || route)) {
427 /* Check if we have sent the packet to this route already */
428 for (k = 0; k < routed_count; k++)
429 if (routed[k] == client->router)
431 if (k < routed_count)
434 /* Route only once to router */
435 sock = (SilcSocketConnection)client->router->connection;
436 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
442 /* Send the packet */
443 silc_server_packet_send_dest(server, sock, type, flags,
444 client->router->id, SILC_ID_SERVER,
445 data, data_len, force_send);
447 /* Mark this route routed already */
448 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
449 routed[routed_count++] = client->router;
456 /* Send to locally connected client */
457 sock = (SilcSocketConnection)client->connection;
461 silc_server_packet_send_dest(server, sock, type, flags,
462 client->id, SILC_ID_CLIENT,
463 data, data_len, force_send);
465 silc_hash_table_list_reset(&htl);
469 /* Internal routine to actually create the channel packet and send it
470 to network. This is common function in channel message sending. If
471 `channel_message' is TRUE this encrypts the message as it is strictly
472 a channel message. If FALSE normal encryption process is used. */
475 silc_server_packet_send_to_channel_real(SilcServer server,
476 SilcSocketConnection sock,
477 SilcPacketContext *packet,
483 bool channel_message,
487 const SilcBufferStruct p;
492 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
494 packet->dst_id_len));
495 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
496 packet->src_id_len + packet->dst_id_len;
498 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
500 packet->padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
502 packet->dst_id_len), block_len);
504 packet->padlen = SILC_PACKET_PADLEN(packet->truelen, block_len);
506 /* Put the data to buffer, assemble and encrypt the packet. The packet
507 is encrypted with normal session key shared with the client, unless
508 the `channel_message' is TRUE. */
509 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
510 data_len, (const SilcBuffer)&p)) {
511 SILC_LOG_ERROR(("Cannot assemble packet"));
516 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
517 SILC_PACKET_HEADER_LEN + packet->src_id_len +
518 packet->dst_id_len + packet->padlen);
520 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
522 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
525 /* Now actually send the packet */
526 silc_server_packet_send_real(server, sock, force_send);
529 /* This routine is used by the server to send packets to channel. The
530 packet sent with this function is distributed to all clients on
531 the channel. Usually this is used to send notify messages to the
532 channel, things like notify about new user joining to the channel.
533 If `route' is FALSE then the packet is sent only locally and will not
534 be routed anywhere (for router locally means cell wide). If `sender'
535 is provided then the packet is not sent to that connection since it
536 originally came from it. If `send_to_clients' is FALSE then the
537 packet is not sent clients, only servers. */
539 void silc_server_packet_send_to_channel(SilcServer server,
540 SilcSocketConnection sender,
541 SilcChannelEntry channel,
548 SilcSocketConnection sock = NULL;
549 SilcPacketContext packetdata;
550 SilcClientEntry client = NULL;
551 SilcServerEntry *routed = NULL;
552 SilcChannelClientEntry chl;
553 SilcHashTableList htl;
554 SilcIDListData idata;
555 SilcUInt32 routed_count = 0;
559 /* This doesn't send channel message packets */
560 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
562 /* Set the packet context pointers. */
563 packetdata.flags = 0;
564 packetdata.type = type;
565 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
566 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
567 packetdata.src_id_type = SILC_ID_SERVER;
568 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
569 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
570 packetdata.dst_id_type = SILC_ID_CHANNEL;
572 /* If there are global users in the channel we will send the message
573 first to our router for further routing. */
574 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
575 channel->global_users) {
576 SilcServerEntry router;
578 /* Get data used in packet header encryption, keys and stuff. */
579 router = server->router;
580 sock = (SilcSocketConnection)router->connection;
581 idata = (SilcIDListData)router;
583 if (sock != sender) {
584 SILC_LOG_DEBUG(("Sending packet to router for routing"));
585 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
589 data, data_len, FALSE,
594 if (!silc_hash_table_count(channel->user_list)) {
595 SILC_LOG_DEBUG(("Channel %s is empty", channel->channel_name));
599 SILC_LOG_DEBUG(("Sending packet to channel %s", channel->channel_name));
601 routed = silc_calloc(silc_hash_table_count(channel->user_list),
604 /* Send the message to clients on the channel's client list. */
605 silc_hash_table_list(channel->user_list, &htl);
606 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
607 client = chl->client;
611 /* If client has router set it is not locally connected client and
612 we will route the message to the router set in the client. Though,
613 send locally connected server in all cases. */
614 if (server->server_type == SILC_ROUTER && client->router &&
615 ((!route && client->router->router == server->id_entry) || route)) {
617 /* Check if we have sent the packet to this route already */
618 for (k = 0; k < routed_count; k++)
619 if (routed[k] == client->router)
621 if (k < routed_count)
624 /* Get data used in packet header encryption, keys and stuff. */
625 sock = (SilcSocketConnection)client->router->connection;
626 idata = (SilcIDListData)client->router;
628 if (sender && sock == sender)
631 /* Route only once to router. Protocol prohibits sending channel
632 messages to more than one router. */
633 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
639 /* Send the packet */
640 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
644 data, data_len, FALSE,
647 /* Mark this route routed already */
648 routed[routed_count++] = client->router;
655 /* Send to locally connected client */
657 /* Get data used in packet header encryption, keys and stuff. */
658 sock = (SilcSocketConnection)client->connection;
659 idata = (SilcIDListData)client;
661 if (!sock || (sender && sock == sender))
664 /* Send the packet */
665 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
669 data, data_len, FALSE,
672 silc_hash_table_list_reset(&htl);
676 silc_free(packetdata.src_id);
677 silc_free(packetdata.dst_id);
680 /* This checks whether the relayed packet came from router. If it did
681 then we'll need to encrypt it with the channel key. This is called
682 from the silc_server_packet_relay_to_channel. */
685 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
686 SilcSocketConnection sock,
687 SilcChannelEntry channel,
689 unsigned int data_len)
691 /* If we are router and the packet came from router and private key
692 has not been set for the channel then we must encrypt the packet
693 as it was decrypted with the session key shared between us and the
694 router which sent it. This is so, because cells does not share the
696 if (server->server_type == SILC_ROUTER &&
697 sock->type == SILC_SOCKET_TYPE_ROUTER &&
698 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
699 channel->channel_key) {
700 SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
701 SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
702 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
704 if (data_len <= mac_len + iv_len) {
705 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
709 memcpy(iv, data + (data_len - iv_len), iv_len);
710 silc_channel_message_payload_encrypt(data, data_len - iv_len - mac_len,
711 data_len, iv, iv_len,
712 channel->channel_key, channel->hmac);
718 /* This routine is explicitly used to relay messages to some channel.
719 Packets sent with this function we have received earlier and are
720 totally encrypted. This just sends the packet to all clients on
721 the channel. If the sender of the packet is someone on the channel
722 the message will not be sent to that client. The SILC Packet header
723 is encrypted with the session key shared between us and the client.
724 MAC is also computed before encrypting the header. Rest of the
725 packet will be untouched. */
727 void silc_server_packet_relay_to_channel(SilcServer server,
728 SilcSocketConnection sender_sock,
729 SilcChannelEntry channel,
731 SilcIdType sender_type,
732 SilcClientEntry sender_entry,
737 SilcSocketConnection sock = NULL;
738 SilcPacketContext packetdata;
739 SilcClientEntry client = NULL;
740 SilcServerEntry *routed = NULL;
741 SilcChannelClientEntry chl, chl_sender;
742 SilcUInt32 routed_count = 0;
743 SilcIDListData idata;
744 SilcHashTableList htl;
748 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
751 SILC_LOG_DEBUG(("Relaying packet to channel %s", channel->channel_name));
753 /* This encrypts the packet, if needed. It will be encrypted if
754 it came from the router thus it needs to be encrypted with the
755 channel key. If the channel key does not exist, then we know we
756 don't have a single local user on the channel. */
757 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
762 /* Set the packet context pointers. */
763 packetdata.flags = 0;
764 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
765 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
766 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
767 packetdata.src_id_type = sender_type;
768 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
769 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
770 packetdata.dst_id_type = SILC_ID_CHANNEL;
772 /* If there are global users in the channel we will send the message
773 first to our router for further routing. */
774 if (server->server_type != SILC_ROUTER && !server->standalone &&
775 channel->global_users) {
776 SilcServerEntry router = server->router;
778 /* Check that the sender is not our router. */
779 if (sender_sock != (SilcSocketConnection)router->connection) {
781 /* Get data used in packet header encryption, keys and stuff. */
782 sock = (SilcSocketConnection)router->connection;
783 idata = (SilcIDListData)router;
785 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
787 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
791 data, data_len, TRUE,
796 routed = silc_calloc(silc_hash_table_count(channel->user_list),
799 /* Assure we won't route the message back to the sender's way. */
800 if (sender_entry->router)
801 routed[routed_count++] = sender_entry->router;
803 /* Send the message to clients on the channel's client list. */
804 silc_hash_table_list(channel->user_list, &htl);
805 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
806 client = chl->client;
807 if (!client || client == sender_entry)
810 /* Check whether message sending is blocked */
811 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
813 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
814 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
815 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
817 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
818 sender_entry->mode & SILC_UMODE_ROBOT)
821 /* If the client has set router it means that it is not locally
822 connected client and we will route the packet further. */
823 if (server->server_type == SILC_ROUTER && client->router) {
825 /* Check if we have sent the packet to this route already */
826 for (k = 0; k < routed_count; k++)
827 if (routed[k] == client->router)
829 if (k < routed_count)
832 /* Get data used in packet header encryption, keys and stuff. */
833 sock = (SilcSocketConnection)client->router->connection;
834 idata = (SilcIDListData)client->router;
836 /* Check if the sender socket is the same as this client's router
838 if (sender_sock && sock == sender_sock)
841 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
842 silc_id_render(client->id, SILC_ID_CLIENT),
843 sock->hostname, sock->ip));
845 /* Mark this route routed already. */
846 routed[routed_count++] = client->router;
848 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
849 /* The remote connection is router then we'll decrypt the
850 channel message and re-encrypt it with the session key shared
851 between us and the remote router. This is done because the
852 channel keys are cell specific and we have different channel
853 key than the remote router has. */
855 /* Route only once to router. Protocol prohibits sending channel
856 messages to more than one router. */
861 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
863 /* If private key mode is not set then decrypt the packet
865 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
866 channel->channel_key) {
867 unsigned char tmp[SILC_PACKET_MAX_LEN];
869 if (data_len > SILC_PACKET_MAX_LEN)
870 data_len = SILC_PACKET_MAX_LEN;
871 memcpy(tmp, data, data_len);
873 /* Decrypt the channel message (we don't check the MAC) */
874 silc_channel_message_payload_decrypt(tmp, data_len,
875 channel->channel_key, NULL);
877 /* Now re-encrypt and send it to the router */
878 silc_server_packet_send_srcdest(server, sock,
879 SILC_PACKET_CHANNEL_MESSAGE, 0,
880 sender_id, sender_type,
881 channel->id, SILC_ID_CHANNEL,
882 tmp, data_len, force_send);
884 /* Private key mode is set, we don't have the channel key, so
885 just re-encrypt the entire packet and send it to the router. */
886 silc_server_packet_send_srcdest(server, sock,
887 SILC_PACKET_CHANNEL_MESSAGE, 0,
888 sender_id, sender_type,
889 channel->id, SILC_ID_CHANNEL,
890 data, data_len, force_send);
893 /* Send the packet to normal server */
894 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
898 data, data_len, TRUE,
908 /* Get data used in packet header encryption, keys and stuff. */
909 sock = (SilcSocketConnection)client->connection;
910 idata = (SilcIDListData)client;
912 if (!sock || (sender_sock && sock == sender_sock))
915 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
916 silc_id_render(client->id, SILC_ID_CLIENT),
917 sock->hostname, sock->ip));
919 /* Send the packet */
920 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
924 data, data_len, TRUE,
928 silc_hash_table_list_reset(&htl);
930 silc_free(packetdata.src_id);
931 silc_free(packetdata.dst_id);
934 /* This function is used to send packets strictly to all local clients
935 on a particular channel. This is used for example to distribute new
936 channel key to all our locally connected clients on the channel.
937 The packets are always encrypted with the session key shared between
938 the client, this means these are not _to the channel_ but _to the client_
941 void silc_server_packet_send_local_channel(SilcServer server,
942 SilcChannelEntry channel,
944 SilcPacketFlags flags,
949 SilcChannelClientEntry chl;
950 SilcHashTableList htl;
951 SilcSocketConnection sock = NULL;
953 SILC_LOG_DEBUG(("Send packet to local clients on channel %s",
954 channel->channel_name));
956 /* Send the message to clients on the channel's client list. */
957 silc_hash_table_list(channel->user_list, &htl);
958 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
959 if (chl->client && !chl->client->router) {
960 sock = (SilcSocketConnection)chl->client->connection;
962 /* Send the packet to the client */
963 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
964 SILC_ID_CLIENT, data, data_len,
968 silc_hash_table_list_reset(&htl);
971 /* Routine used to send (relay, route) private messages to some destination.
972 If the private message key does not exist then the message is re-encrypted,
973 otherwise we just pass it along. This really is not used to send new
974 private messages (as server does not send them) but to relay received
977 void silc_server_send_private_message(SilcServer server,
978 SilcSocketConnection dst_sock,
982 SilcPacketContext *packet)
984 SilcBuffer buffer = packet->buffer;
985 const SilcBufferStruct p;
987 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
988 + packet->dst_id_len + packet->padlen);
989 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
990 (const SilcBuffer)&p)) {
991 SILC_LOG_ERROR(("Cannot send packet"));
994 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
996 /* Re-encrypt and send if private messge key does not exist */
997 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
998 /* Re-encrypt packet */
999 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
1001 /* Key exist so encrypt just header and send it */
1002 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
1003 SILC_PACKET_HEADER_LEN + packet->src_id_len +
1004 packet->dst_id_len + packet->padlen);
1007 /* Send the packet */
1008 silc_server_packet_send_real(server, dst_sock, FALSE);
1011 /* Sends current motd to client */
1013 void silc_server_send_motd(SilcServer server,
1014 SilcSocketConnection sock)
1016 char *motd, *motd_file = NULL;
1017 SilcUInt32 motd_len;
1020 motd_file = server->config->server_info->motd_file;
1023 motd = silc_file_readfile(motd_file, &motd_len);
1027 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1033 /* Sends error message. Error messages may or may not have any
1036 void silc_server_send_error(SilcServer server,
1037 SilcSocketConnection sock,
1038 const char *fmt, ...)
1041 unsigned char buf[4096];
1043 memset(buf, 0, sizeof(buf));
1045 vsprintf(buf, fmt, ap);
1048 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1049 buf, strlen(buf), FALSE);
1052 /* Sends notify message. If format is TRUE the variable arguments are
1053 formatted and the formatted string is sent as argument payload. If it is
1054 FALSE then each argument is sent as separate argument and their format
1055 in the argument list must be { argument data, argument length }. */
1057 void silc_server_send_notify(SilcServer server,
1058 SilcSocketConnection sock,
1060 SilcNotifyType type,
1061 SilcUInt32 argc, ...)
1068 packet = silc_notify_payload_encode(type, argc, ap);
1069 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1070 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1071 packet->data, packet->len, FALSE);
1073 /* Send to backup routers if this is being broadcasted to primary
1074 router. The silc_server_backup_send checks further whether to
1075 actually send it or not. */
1076 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1077 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1078 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1079 packet->data, packet->len, FALSE, TRUE);
1081 silc_buffer_free(packet);
1085 /* Sends notify message and gets the arguments from the `args' Argument
1088 void silc_server_send_notify_args(SilcServer server,
1089 SilcSocketConnection sock,
1091 SilcNotifyType type,
1097 packet = silc_notify_payload_encode_args(type, argc, args);
1098 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1099 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1100 packet->data, packet->len, FALSE);
1101 silc_buffer_free(packet);
1104 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1105 `old_id' with the `new_id'. */
1107 void silc_server_send_notify_channel_change(SilcServer server,
1108 SilcSocketConnection sock,
1110 SilcChannelID *old_id,
1111 SilcChannelID *new_id)
1113 SilcBuffer idp1, idp2;
1115 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1116 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1118 silc_server_send_notify(server, sock, broadcast,
1119 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1120 2, idp1->data, idp1->len, idp2->data, idp2->len);
1121 silc_buffer_free(idp1);
1122 silc_buffer_free(idp2);
1125 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1126 `old_id' with the `new_id'. */
1128 void silc_server_send_notify_nick_change(SilcServer server,
1129 SilcSocketConnection sock,
1131 SilcClientID *old_id,
1132 SilcClientID *new_id,
1133 const char *nickname)
1135 SilcBuffer idp1, idp2;
1137 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1138 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1140 silc_server_send_notify(server, sock, broadcast,
1141 SILC_NOTIFY_TYPE_NICK_CHANGE,
1142 3, idp1->data, idp1->len, idp2->data, idp2->len,
1143 nickname, nickname ? strlen(nickname) : 0);
1144 silc_buffer_free(idp1);
1145 silc_buffer_free(idp2);
1148 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1149 has joined to the `channel'. */
1151 void silc_server_send_notify_join(SilcServer server,
1152 SilcSocketConnection sock,
1154 SilcChannelEntry channel,
1155 SilcClientID *client_id)
1157 SilcBuffer idp1, idp2;
1159 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1160 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1161 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1162 2, idp1->data, idp1->len,
1163 idp2->data, idp2->len);
1164 silc_buffer_free(idp1);
1165 silc_buffer_free(idp2);
1168 /* Sends LEAVE notify type. This tells that `client_id' has left the
1169 `channel'. The Notify packet is always destined to the channel. */
1171 void silc_server_send_notify_leave(SilcServer server,
1172 SilcSocketConnection sock,
1174 SilcChannelEntry channel,
1175 SilcClientID *client_id)
1179 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1180 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1181 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1182 1, idp->data, idp->len);
1183 silc_buffer_free(idp);
1186 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1187 `channel' mode to `mode. The Notify packet is always destined to
1190 void silc_server_send_notify_cmode(SilcServer server,
1191 SilcSocketConnection sock,
1193 SilcChannelEntry channel,
1194 SilcUInt32 mode_mask,
1195 void *id, SilcIdType id_type,
1196 const char *cipher, const char *hmac,
1197 const char *passphrase,
1198 SilcPublicKey founder_key)
1201 unsigned char mode[4], *key = NULL;
1202 SilcUInt32 key_len = 0;
1204 idp = silc_id_payload_encode((void *)id, id_type);
1205 SILC_PUT32_MSB(mode_mask, mode);
1207 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1209 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1210 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1211 6, idp->data, idp->len,
1213 cipher, cipher ? strlen(cipher) : 0,
1214 hmac, hmac ? strlen(hmac) : 0,
1215 passphrase, passphrase ?
1216 strlen(passphrase) : 0,
1219 silc_buffer_free(idp);
1222 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1223 `target' client's mode on `channel'. The notify packet is always
1224 destined to the channel. */
1226 void silc_server_send_notify_cumode(SilcServer server,
1227 SilcSocketConnection sock,
1229 SilcChannelEntry channel,
1230 SilcUInt32 mode_mask,
1231 void *id, SilcIdType id_type,
1232 SilcClientID *target,
1233 SilcPublicKey founder_key)
1235 SilcBuffer idp1, idp2;
1236 unsigned char mode[4], *key = NULL;
1237 SilcUInt32 key_len = 0;
1239 idp1 = silc_id_payload_encode((void *)id, id_type);
1240 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1241 SILC_PUT32_MSB(mode_mask, mode);
1243 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1245 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1247 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
1248 idp1->data, idp1->len,
1250 idp2->data, idp2->len,
1253 silc_buffer_free(idp1);
1254 silc_buffer_free(idp2);
1257 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1258 left SILC network. This function is used only between server and router
1259 traffic. This is not used to send the notify to the channel for
1260 client. The `message may be NULL. */
1262 void silc_server_send_notify_signoff(SilcServer server,
1263 SilcSocketConnection sock,
1265 SilcClientID *client_id,
1266 const char *message)
1270 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1271 silc_server_send_notify(server, sock, broadcast,
1272 SILC_NOTIFY_TYPE_SIGNOFF,
1273 message ? 2 : 1, idp->data, idp->len,
1274 message, message ? strlen(message): 0);
1275 silc_buffer_free(idp);
1278 /* Sends TOPIC_SET notify type. This tells that `id' changed
1279 the `channel's topic to `topic'. The Notify packet is always destined
1280 to the channel. This function is used to send the topic set notifies
1283 void silc_server_send_notify_topic_set(SilcServer server,
1284 SilcSocketConnection sock,
1286 SilcChannelEntry channel,
1287 void *id, SilcIdType id_type,
1292 idp = silc_id_payload_encode(id, id_type);
1293 silc_server_send_notify_dest(server, sock, broadcast,
1294 (void *)channel->id, SILC_ID_CHANNEL,
1295 SILC_NOTIFY_TYPE_TOPIC_SET,
1297 idp->data, idp->len,
1298 topic, topic ? strlen(topic) : 0);
1299 silc_buffer_free(idp);
1302 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1303 was kicked off the channel. The `comment' may indicate the reason
1304 for the kicking. This function is used only between server and router
1307 void silc_server_send_notify_kicked(SilcServer server,
1308 SilcSocketConnection sock,
1310 SilcChannelEntry channel,
1311 SilcClientID *client_id,
1312 SilcClientID *kicker,
1318 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1319 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1320 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1321 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1322 idp1->data, idp1->len,
1323 comment, comment ? strlen(comment) : 0,
1324 idp2->data, idp2->len);
1325 silc_buffer_free(idp1);
1326 silc_buffer_free(idp2);
1329 /* Send KILLED notify type. This tells that the `client_id' client was
1330 killed from the network. The `comment' may indicate the reason
1333 void silc_server_send_notify_killed(SilcServer server,
1334 SilcSocketConnection sock,
1336 SilcClientID *client_id,
1337 const char *comment,
1338 void *killer, SilcIdType killer_type)
1343 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1344 idp2 = silc_id_payload_encode(killer, killer_type);
1345 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1346 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1347 3, idp1->data, idp1->len,
1348 comment, comment ? strlen(comment) : 0,
1349 idp2->data, idp2->len);
1350 silc_buffer_free(idp1);
1351 silc_buffer_free(idp2);
1354 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1355 user mode in the SILC Network was changed. This function is used to
1356 send the packet between routers as broadcast packet. */
1358 void silc_server_send_notify_umode(SilcServer server,
1359 SilcSocketConnection sock,
1361 SilcClientID *client_id,
1362 SilcUInt32 mode_mask)
1365 unsigned char mode[4];
1367 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1368 SILC_PUT32_MSB(mode_mask, mode);
1370 silc_server_send_notify(server, sock, broadcast,
1371 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1372 idp->data, idp->len,
1374 silc_buffer_free(idp);
1377 /* Sends BAN notify type. This tells that ban has been either `add'ed
1378 or `del'eted on the `channel. This function is used to send the packet
1379 between routers as broadcast packet. */
1381 void silc_server_send_notify_ban(SilcServer server,
1382 SilcSocketConnection sock,
1384 SilcChannelEntry channel,
1385 char *add, char *del)
1389 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1390 silc_server_send_notify(server, sock, broadcast,
1391 SILC_NOTIFY_TYPE_BAN, 3,
1392 idp->data, idp->len,
1393 add, add ? strlen(add) : 0,
1394 del, del ? strlen(del) : 0);
1395 silc_buffer_free(idp);
1398 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1399 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1400 This function is used to send the packet between routers as broadcast
1403 void silc_server_send_notify_invite(SilcServer server,
1404 SilcSocketConnection sock,
1406 SilcChannelEntry channel,
1407 SilcClientID *client_id,
1408 char *add, char *del)
1410 SilcBuffer idp, idp2;
1412 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1413 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1414 silc_server_send_notify(server, sock, broadcast,
1415 SILC_NOTIFY_TYPE_INVITE, 5,
1416 idp->data, idp->len,
1417 channel->channel_name, strlen(channel->channel_name),
1418 idp2->data, idp2->len,
1419 add, add ? strlen(add) : 0,
1420 del, del ? strlen(del) : 0);
1421 silc_buffer_free(idp);
1422 silc_buffer_free(idp2);
1425 /* Sends WATCH notify type. This tells that the `client' was watched and
1426 its status in the network has changed. */
1428 void silc_server_send_notify_watch(SilcServer server,
1429 SilcSocketConnection sock,
1430 SilcClientEntry watcher,
1431 SilcClientEntry client,
1432 const char *nickname,
1433 SilcNotifyType type)
1436 unsigned char mode[4], n[2];
1438 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1439 SILC_PUT16_MSB(type, n);
1440 SILC_PUT32_MSB(client->mode, mode);
1441 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1442 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1443 4, idp->data, idp->len,
1444 nickname, nickname ? strlen(nickname) : 0,
1446 type != SILC_NOTIFY_TYPE_NONE ?
1447 n : NULL, sizeof(n));
1448 silc_buffer_free(idp);
1451 /* Sends notify message destined to specific entity. */
1453 void silc_server_send_notify_dest(SilcServer server,
1454 SilcSocketConnection sock,
1457 SilcIdType dest_id_type,
1458 SilcNotifyType type,
1459 SilcUInt32 argc, ...)
1466 packet = silc_notify_payload_encode(type, argc, ap);
1467 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1468 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1469 dest_id, dest_id_type,
1470 packet->data, packet->len, FALSE);
1471 silc_buffer_free(packet);
1475 /* Sends notify message to a channel. The notify message sent is
1476 distributed to all clients on the channel. If `route_notify' is TRUE
1477 then the notify may be routed to primary route or to some other routers.
1478 If FALSE it is assured that the notify is sent only locally. If `sender'
1479 is provided then the packet is not sent to that connection since it
1480 originally came from it. */
1482 void silc_server_send_notify_to_channel(SilcServer server,
1483 SilcSocketConnection sender,
1484 SilcChannelEntry channel,
1486 SilcNotifyType type,
1487 SilcUInt32 argc, ...)
1494 packet = silc_notify_payload_encode(type, argc, ap);
1495 silc_server_packet_send_to_channel(server, sender, channel,
1496 SILC_PACKET_NOTIFY, route_notify,
1497 packet->data, packet->len, FALSE);
1498 silc_buffer_free(packet);
1502 /* Send notify message to all channels the client has joined. It is quaranteed
1503 that the message is sent only once to a client (ie. if a client is joined
1504 on two same channel it will receive only one notify message). Also, this
1505 sends only to local clients (locally connected if we are server, and to
1506 local servers if we are router). If `sender' is provided the packet is
1507 not sent to that client at all. */
1509 void silc_server_send_notify_on_channels(SilcServer server,
1510 SilcClientEntry sender,
1511 SilcClientEntry client,
1512 SilcNotifyType type,
1513 SilcUInt32 argc, ...)
1516 SilcSocketConnection sock = NULL;
1517 SilcPacketContext packetdata;
1519 SilcClientEntry *sent_clients = NULL;
1520 SilcUInt32 sent_clients_count = 0;
1521 SilcServerEntry *routed = NULL;
1522 SilcUInt32 routed_count = 0;
1523 SilcHashTableList htl, htl2;
1524 SilcChannelEntry channel;
1525 SilcChannelClientEntry chl, chl2;
1526 SilcIDListData idata;
1528 unsigned char *data;
1529 SilcUInt32 data_len;
1530 bool force_send = FALSE;
1533 if (!silc_hash_table_count(client->channels)) {
1534 SILC_LOG_DEBUG(("Client is not joined to any channels"));
1538 SILC_LOG_DEBUG(("Sending notify to joined channels"));
1541 packet = silc_notify_payload_encode(type, argc, ap);
1542 data = packet->data;
1543 data_len = packet->len;
1545 /* Set the packet context pointers. */
1546 packetdata.flags = 0;
1547 packetdata.type = SILC_PACKET_NOTIFY;
1548 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1549 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1550 packetdata.src_id_type = SILC_ID_SERVER;
1552 silc_hash_table_list(client->channels, &htl);
1553 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
1554 channel = chl->channel;
1556 /* Send the message to all clients on the channel's client list. */
1557 silc_hash_table_list(channel->user_list, &htl2);
1558 while (silc_hash_table_get(&htl2, NULL, (void **)&chl2)) {
1561 if (sender && c == sender)
1564 /* Check if we have sent the packet to this client already */
1565 for (k = 0; k < sent_clients_count; k++)
1566 if (sent_clients[k] == c)
1568 if (k < sent_clients_count)
1571 /* If we are router and if this client has router set it is not
1572 locally connected client and we will route the message to the
1573 router set in the client. */
1574 if (c && c->router && server->server_type == SILC_ROUTER) {
1575 /* Check if we have sent the packet to this route already */
1576 for (k = 0; k < routed_count; k++)
1577 if (routed[k] == c->router)
1579 if (k < routed_count)
1582 /* Get data used in packet header encryption, keys and stuff. */
1583 sock = (SilcSocketConnection)c->router->connection;
1584 idata = (SilcIDListData)c->router;
1587 SILC_LOG_DEBUG(("*****************"));
1588 SILC_LOG_DEBUG(("client->router->id %s",
1589 silc_id_render(c->router->id, SILC_ID_SERVER)));
1590 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1591 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1594 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1595 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1596 packetdata.dst_id_type = SILC_ID_SERVER;
1598 /* Send the packet */
1599 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1603 data, data_len, FALSE,
1606 silc_free(packetdata.dst_id);
1608 /* We want to make sure that the packet is routed to same router
1609 only once. Mark this route as sent route. */
1610 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1611 routed[routed_count++] = c->router;
1618 /* Send to locally connected client */
1621 /* Get data used in packet header encryption, keys and stuff. */
1622 sock = (SilcSocketConnection)c->connection;
1623 idata = (SilcIDListData)c;
1628 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1629 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1630 packetdata.dst_id_type = SILC_ID_CLIENT;
1632 /* Send the packet */
1633 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1637 data, data_len, FALSE,
1640 silc_free(packetdata.dst_id);
1642 /* Make sure that we send the notify only once per client. */
1643 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1644 (sent_clients_count + 1));
1645 sent_clients[sent_clients_count++] = c;
1648 silc_hash_table_list_reset(&htl2);
1651 silc_hash_table_list_reset(&htl);
1653 silc_free(sent_clients);
1654 silc_free(packetdata.src_id);
1655 silc_buffer_free(packet);
1659 /* Sends New ID Payload to remote end. The packet is used to distribute
1660 information about new registered clients, servers, channel etc. usually
1661 to routers so that they can keep these information up to date.
1662 If the argument `broadcast' is TRUE then the packet is sent as
1663 broadcast packet. */
1665 void silc_server_send_new_id(SilcServer server,
1666 SilcSocketConnection sock,
1668 void *id, SilcIdType id_type,
1673 SILC_LOG_DEBUG(("Sending new ID"));
1675 idp = silc_id_payload_encode(id, id_type);
1676 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1677 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1678 idp->data, idp->len, FALSE);
1679 silc_buffer_free(idp);
1682 /* Send New Channel Payload to notify about newly created channel in the
1683 SILC network. Router uses this to notify other routers in the network
1684 about new channel. This packet is broadcasted by router. */
1686 void silc_server_send_new_channel(SilcServer server,
1687 SilcSocketConnection sock,
1691 SilcUInt32 channel_id_len,
1696 SilcUInt32 name_len = strlen(channel_name);
1698 SILC_LOG_DEBUG(("Sending new channel"));
1700 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1704 /* Encode the channel payload */
1705 packet = silc_channel_payload_encode(channel_name, name_len,
1706 cid, channel_id_len, mode);
1708 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1709 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1710 packet->data, packet->len, FALSE);
1713 silc_buffer_free(packet);
1716 /* Send Channel Key payload to distribute the new channel key. Normal server
1717 sends this to router when new client joins to existing channel. Router
1718 sends this to the local server who sent the join command in case where
1719 the channel did not exist yet. Both normal and router servers uses this
1720 also to send this to locally connected clients on the channel. This
1721 must not be broadcasted packet. Routers do not send this to each other.
1722 If `sender is provided then the packet is not sent to that connection since
1723 it originally came from it. */
1725 void silc_server_send_channel_key(SilcServer server,
1726 SilcSocketConnection sender,
1727 SilcChannelEntry channel,
1728 unsigned char route)
1731 unsigned char *chid;
1734 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1736 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1740 /* Encode channel key packet */
1741 tmp_len = strlen(channel->channel_key->cipher->name);
1742 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1745 channel->channel_key->cipher->name,
1746 channel->key_len / 8, channel->key);
1747 silc_server_packet_send_to_channel(server, sender, channel,
1748 SILC_PACKET_CHANNEL_KEY,
1749 route, packet->data, packet->len,
1751 silc_buffer_free(packet);
1755 /* Generic function to send any command. The arguments must be sent already
1756 encoded into correct form in correct order. */
1758 void silc_server_send_command(SilcServer server,
1759 SilcSocketConnection sock,
1760 SilcCommand command,
1762 SilcUInt32 argc, ...)
1769 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1770 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1771 packet->data, packet->len, TRUE);
1772 silc_buffer_free(packet);
1776 /* Generic function to send any command reply. The arguments must be sent
1777 already encoded into correct form in correct order. */
1779 void silc_server_send_command_reply(SilcServer server,
1780 SilcSocketConnection sock,
1781 SilcCommand command,
1785 SilcUInt32 argc, ...)
1792 packet = silc_command_reply_payload_encode_vap(command, status, error,
1794 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1795 packet->data, packet->len, TRUE);
1796 silc_buffer_free(packet);
1800 /* Generic function to send any command reply. The arguments must be sent
1801 already encoded into correct form in correct order. */
1803 void silc_server_send_dest_command_reply(SilcServer server,
1804 SilcSocketConnection sock,
1806 SilcIdType dst_id_type,
1807 SilcCommand command,
1811 SilcUInt32 argc, ...)
1818 packet = silc_command_reply_payload_encode_vap(command, status, error,
1820 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1821 dst_id, dst_id_type, packet->data,
1823 silc_buffer_free(packet);
1827 /* Send the heartbeat packet. */
1829 void silc_server_send_heartbeat(SilcServer server,
1830 SilcSocketConnection sock)
1832 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1836 /* Generic function to relay packet we've received. This is used to relay
1837 packets to a client but generally can be used to other purposes as well. */
1839 void silc_server_relay_packet(SilcServer server,
1840 SilcSocketConnection dst_sock,
1843 SilcUInt32 sequence,
1844 SilcPacketContext *packet,
1847 const SilcBufferStruct p;
1849 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1850 + packet->dst_id_len + packet->padlen);
1851 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1852 (const SilcBuffer)&p)) {
1853 SILC_LOG_ERROR(("Cannot send packet"));
1856 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1858 /* Re-encrypt packet */
1859 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1861 /* Send the packet */
1862 silc_server_packet_send_real(server, dst_sock, force_send);
1864 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1865 + packet->dst_id_len + packet->padlen);
1868 /* Routine used to send the connection authentication packet. */
1870 void silc_server_send_connection_auth_request(SilcServer server,
1871 SilcSocketConnection sock,
1872 SilcUInt16 conn_type,
1873 SilcAuthMethod auth_meth)
1877 packet = silc_buffer_alloc(4);
1878 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1879 silc_buffer_format(packet,
1880 SILC_STR_UI_SHORT(conn_type),
1881 SILC_STR_UI_SHORT(auth_meth),
1884 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1885 0, packet->data, packet->len, FALSE);
1886 silc_buffer_free(packet);
1889 /* Purge the outgoing packet queue to the network if there is data. This
1890 function can be used to empty the packet queue. It is guaranteed that
1891 after this function returns the outgoing data queue is empty. */
1893 void silc_server_packet_queue_purge(SilcServer server,
1894 SilcSocketConnection sock)
1896 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1897 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1898 server->stat.packets_sent++;
1899 silc_packet_send(sock, TRUE);
1900 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1901 SILC_UNSET_OUTBUF_PENDING(sock);
1902 silc_buffer_clear(sock->outbuf);