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);
56 /* Mark that there is some outgoing data available for this connection.
57 This call sets the connection both for input and output (the input
58 is set always and this call keeps the input setting, actually).
59 Actual data sending is performed by silc_server_packet_process. */
60 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
62 /* Mark to socket that data is pending in outgoing buffer. This flag
63 is needed if new data is added to the buffer before the earlier
64 put data is sent to the network. */
65 SILC_SET_OUTBUF_PENDING(sock);
70 /* Assembles a new packet to be sent out to network. This doesn't actually
71 send the packet but creates the packet and fills the outgoing data
72 buffer and marks the packet ready to be sent to network. However, If
73 argument force_send is TRUE the packet is sent immediately and not put
74 to queue. Normal case is that the packet is not sent immediately. */
76 void silc_server_packet_send(SilcServer server,
77 SilcSocketConnection sock,
79 SilcPacketFlags flags,
85 SilcIdType dst_id_type = SILC_ID_NONE;
86 SilcIDListData idata = (SilcIDListData)sock->user_data;
91 /* If disconnecting, ignore the data */
92 if (SILC_IS_DISCONNECTING(sock))
95 /* If entry is disabled do not sent anything. */
96 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
99 /* Get data used in the packet sending, keys and stuff */
101 case SILC_SOCKET_TYPE_CLIENT:
102 if (sock->user_data) {
103 dst_id = ((SilcClientEntry)sock->user_data)->id;
104 dst_id_type = SILC_ID_CLIENT;
107 case SILC_SOCKET_TYPE_SERVER:
108 case SILC_SOCKET_TYPE_ROUTER:
109 if (sock->user_data) {
110 dst_id = ((SilcServerEntry)sock->user_data)->id;
111 dst_id_type = SILC_ID_SERVER;
118 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
119 dst_id_type, data, data_len, force_send);
122 /* Assembles a new packet to be sent out to network. This doesn't actually
123 send the packet but creates the packet and fills the outgoing data
124 buffer and marks the packet ready to be sent to network. However, If
125 argument force_send is TRUE the packet is sent immediately and not put
126 to queue. Normal case is that the packet is not sent immediately.
127 Destination information is sent as argument for this function. */
129 void silc_server_packet_send_dest(SilcServer server,
130 SilcSocketConnection sock,
132 SilcPacketFlags flags,
134 SilcIdType dst_id_type,
139 SilcPacketContext packetdata;
140 const SilcBufferStruct packet;
141 SilcIDListData idata = (SilcIDListData)sock->user_data;
142 SilcCipher cipher = NULL;
143 SilcHmac hmac = NULL;
144 SilcUInt32 sequence = 0;
145 unsigned char *dst_id_data = NULL;
146 SilcUInt32 dst_id_len = 0;
149 /* If disconnecting, ignore the data */
150 if (SILC_IS_DISCONNECTING(sock))
153 /* If entry is disabled do not sent anything. */
154 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
157 SILC_LOG_DEBUG(("Sending packet, type %d", type));
160 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
161 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
165 cipher = idata->send_key;
166 hmac = idata->hmac_send;
167 sequence = idata->psn_send++;
168 block_len = silc_cipher_get_block_len(cipher);
171 /* Set the packet context pointers */
172 packetdata.type = type;
173 packetdata.flags = flags;
174 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
175 packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
176 packetdata.src_id_type = server->id_type;
177 packetdata.dst_id = dst_id_data;
178 packetdata.dst_id_len = dst_id_len;
179 packetdata.dst_id_type = dst_id_type;
180 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
181 packetdata.src_id_len +
182 packetdata.dst_id_len));
183 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
184 packetdata.src_id_len + dst_id_len;
185 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
187 /* Create the outgoing packet */
188 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock,
189 data, data_len, (const SilcBuffer)&packet)) {
190 SILC_LOG_ERROR(("Cannot assemble packet"));
194 /* Encrypt the packet */
195 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
197 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
198 packet.data, packet.len);
200 /* Now actually send the packet */
201 silc_server_packet_send_real(server, sock, force_send);
204 if (packetdata.src_id)
205 silc_free(packetdata.src_id);
206 if (packetdata.dst_id)
207 silc_free(packetdata.dst_id);
210 /* Assembles a new packet to be sent out to network. This doesn't actually
211 send the packet but creates the packet and fills the outgoing data
212 buffer and marks the packet ready to be sent to network. However, If
213 argument force_send is TRUE the packet is sent immediately and not put
214 to queue. Normal case is that the packet is not sent immediately.
215 The source and destination information is sent as argument for this
218 void silc_server_packet_send_srcdest(SilcServer server,
219 SilcSocketConnection sock,
221 SilcPacketFlags flags,
223 SilcIdType src_id_type,
225 SilcIdType dst_id_type,
230 SilcPacketContext packetdata;
231 const SilcBufferStruct packet;
232 SilcIDListData idata;
233 SilcCipher cipher = NULL;
234 SilcHmac hmac = NULL;
235 SilcUInt32 sequence = 0;
236 unsigned char *dst_id_data = NULL;
237 SilcUInt32 dst_id_len = 0;
238 unsigned char *src_id_data = NULL;
239 SilcUInt32 src_id_len = 0;
242 SILC_LOG_DEBUG(("Sending packet, type %d", type));
244 /* Get data used in the packet sending, keys and stuff */
245 idata = (SilcIDListData)sock->user_data;
248 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
249 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
253 src_id_data = silc_id_id2str(src_id, src_id_type);
254 src_id_len = silc_id_get_len(src_id, src_id_type);
258 cipher = idata->send_key;
259 hmac = idata->hmac_send;
260 sequence = idata->psn_send++;
261 block_len = silc_cipher_get_block_len(cipher);
264 /* Set the packet context pointers */
265 packetdata.type = type;
266 packetdata.flags = flags;
267 packetdata.src_id = src_id_data;
268 packetdata.src_id_len = src_id_len;
269 packetdata.src_id_type = src_id_type;
270 packetdata.dst_id = dst_id_data;
271 packetdata.dst_id_len = dst_id_len;
272 packetdata.dst_id_type = dst_id_type;
273 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
274 packetdata.src_id_len +
276 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
277 packetdata.src_id_len + dst_id_len;
278 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
280 /* Create the outgoing packet */
281 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock, data,
282 data_len, (const SilcBuffer)&packet)) {
283 SILC_LOG_ERROR(("Cannot assemble packe"));
287 /* Encrypt the packet */
288 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
290 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
291 packet.data, packet.len);
293 /* Now actually send the packet */
294 silc_server_packet_send_real(server, sock, force_send);
297 if (packetdata.src_id)
298 silc_free(packetdata.src_id);
299 if (packetdata.dst_id)
300 silc_free(packetdata.dst_id);
303 /* Broadcast received packet to our primary route. This function is used
304 by router to further route received broadcast packet. It is expected
305 that the broadcast flag from the packet is checked before calling this
306 function. This does not test or set the broadcast flag. */
308 void silc_server_packet_broadcast(SilcServer server,
309 SilcSocketConnection sock,
310 SilcPacketContext *packet)
312 SilcBuffer buffer = packet->buffer;
313 SilcIDListData idata;
316 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
318 /* If the packet is originated from our primary route we are
319 not allowed to send the packet. */
320 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
321 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
322 const SilcBufferStruct p;
324 idata = (SilcIDListData)sock->user_data;
326 silc_buffer_push(buffer, buffer->data - buffer->head);
327 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
328 (const SilcBuffer)&p)) {
329 SILC_LOG_ERROR(("Cannot send packet"));
333 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
334 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
335 (SilcBuffer)&p, p.len);
337 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
338 p.len), p.data, p.len);
340 /* Now actually send the packet */
341 silc_server_packet_send_real(server, sock, TRUE);
346 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
347 "original sender of this packet"));
351 /* Routes received packet to `sock'. This is used to route the packets that
352 router receives but are not destined to it. */
354 void silc_server_packet_route(SilcServer server,
355 SilcSocketConnection sock,
356 SilcPacketContext *packet)
358 SilcBuffer buffer = packet->buffer;
359 const SilcBufferStruct p;
360 SilcIDListData idata;
362 SILC_LOG_DEBUG(("Routing received packet"));
364 idata = (SilcIDListData)sock->user_data;
366 silc_buffer_push(buffer, buffer->data - buffer->head);
367 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
368 (const SilcBuffer)&p)) {
369 SILC_LOG_ERROR(("Cannot send packet"));
372 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
373 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
374 (SilcBuffer)&p, p.len);
376 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
377 p.len), p.data, p.len);
379 /* Now actually send the packet */
380 silc_server_packet_send_real(server, sock, TRUE);
383 /* This routine can be used to send a packet to table of clients provided
384 in `clients'. If `route' is FALSE the packet is routed only to local
385 clients (for server locally connected, and for router local cell). */
387 void silc_server_packet_send_clients(SilcServer server,
388 SilcClientEntry *clients,
389 SilcUInt32 clients_count,
391 SilcPacketFlags flags,
397 SilcSocketConnection sock = NULL;
398 SilcClientEntry client = NULL;
399 SilcServerEntry *routed = NULL;
400 SilcUInt32 routed_count = 0;
404 SILC_LOG_DEBUG(("Sending packet to list of clients"));
406 /* Send to all clients in table */
407 for (i = 0; i < clients_count; i++) {
410 /* If client has router set it is not locally connected client and
411 we will route the message to the router set in the client. Though,
412 send locally connected server in all cases. */
413 if (server->server_type == SILC_ROUTER && client->router &&
414 ((!route && client->router->router == server->id_entry) || route)) {
416 /* Check if we have sent the packet to this route already */
417 for (k = 0; k < routed_count; k++)
418 if (routed[k] == client->router)
420 if (k < routed_count)
423 /* Route only once to router */
424 sock = (SilcSocketConnection)client->router->connection;
425 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
431 /* Send the packet */
432 silc_server_packet_send_dest(server, sock, type, flags,
433 client->router->id, SILC_ID_SERVER,
434 data, data_len, force_send);
436 /* Mark this route routed already */
437 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
438 routed[routed_count++] = client->router;
445 /* Send to locally connected client */
446 sock = (SilcSocketConnection)client->connection;
450 silc_server_packet_send_dest(server, sock, type, flags,
451 client->id, SILC_ID_CLIENT,
452 data, data_len, force_send);
458 /* Internal routine to actually create the channel packet and send it
459 to network. This is common function in channel message sending. If
460 `channel_message' is TRUE this encrypts the message as it is strictly
461 a channel message. If FALSE normal encryption process is used. */
464 silc_server_packet_send_to_channel_real(SilcServer server,
465 SilcSocketConnection sock,
466 SilcPacketContext *packet,
472 bool channel_message,
476 const SilcBufferStruct p;
481 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
483 packet->dst_id_len));
484 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
485 packet->src_id_len + packet->dst_id_len;
487 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
489 packet->padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
491 packet->dst_id_len), block_len);
493 packet->padlen = SILC_PACKET_PADLEN(packet->truelen, block_len);
495 /* Put the data to buffer, assemble and encrypt the packet. The packet
496 is encrypted with normal session key shared with the client, unless
497 the `channel_message' is TRUE. */
498 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
499 data_len, (const SilcBuffer)&p)) {
500 SILC_LOG_ERROR(("Cannot assemble packet"));
505 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
506 SILC_PACKET_HEADER_LEN + packet->src_id_len +
507 packet->dst_id_len + packet->padlen);
509 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
511 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
514 /* Now actually send the packet */
515 silc_server_packet_send_real(server, sock, force_send);
518 /* This routine is used by the server to send packets to channel. The
519 packet sent with this function is distributed to all clients on
520 the channel. Usually this is used to send notify messages to the
521 channel, things like notify about new user joining to the channel.
522 If `route' is FALSE then the packet is sent only locally and will not
523 be routed anywhere (for router locally means cell wide). If `sender'
524 is provided then the packet is not sent to that connection since it
525 originally came from it. If `send_to_clients' is FALSE then the
526 packet is not sent clients, only servers. */
528 void silc_server_packet_send_to_channel(SilcServer server,
529 SilcSocketConnection sender,
530 SilcChannelEntry channel,
537 SilcSocketConnection sock = NULL;
538 SilcPacketContext packetdata;
539 SilcClientEntry client = NULL;
540 SilcServerEntry *routed = NULL;
541 SilcChannelClientEntry chl;
542 SilcHashTableList htl;
543 SilcIDListData idata;
544 SilcUInt32 routed_count = 0;
548 /* This doesn't send channel message packets */
549 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
551 SILC_LOG_DEBUG(("Sending packet to channel"));
553 /* Set the packet context pointers. */
554 packetdata.flags = 0;
555 packetdata.type = type;
556 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
557 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
558 packetdata.src_id_type = SILC_ID_SERVER;
559 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
560 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
561 packetdata.dst_id_type = SILC_ID_CHANNEL;
563 /* If there are global users in the channel we will send the message
564 first to our router for further routing. */
565 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
566 channel->global_users) {
567 SilcServerEntry router;
569 /* Get data used in packet header encryption, keys and stuff. */
570 router = server->router;
571 sock = (SilcSocketConnection)router->connection;
572 idata = (SilcIDListData)router;
574 if (sock != sender) {
575 SILC_LOG_DEBUG(("Sending packet to router for routing"));
577 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
581 data, data_len, FALSE,
586 routed = silc_calloc(silc_hash_table_count(channel->user_list),
589 /* Send the message to clients on the channel's client list. */
590 silc_hash_table_list(channel->user_list, &htl);
591 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
592 client = chl->client;
596 /* If client has router set it is not locally connected client and
597 we will route the message to the router set in the client. Though,
598 send locally connected server in all cases. */
599 if (server->server_type == SILC_ROUTER && client->router &&
600 ((!route && client->router->router == server->id_entry) || route)) {
602 /* Check if we have sent the packet to this route already */
603 for (k = 0; k < routed_count; k++)
604 if (routed[k] == client->router)
606 if (k < routed_count)
609 /* Get data used in packet header encryption, keys and stuff. */
610 sock = (SilcSocketConnection)client->router->connection;
611 idata = (SilcIDListData)client->router;
613 if (sender && sock == sender)
616 /* Route only once to router. Protocol prohibits sending channel
617 messages to more than one router. */
618 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
624 /* Send the packet */
625 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
629 data, data_len, FALSE,
632 /* Mark this route routed already */
633 routed[routed_count++] = client->router;
640 /* Send to locally connected client */
642 /* Get data used in packet header encryption, keys and stuff. */
643 sock = (SilcSocketConnection)client->connection;
644 idata = (SilcIDListData)client;
646 if (!sock || (sender && sock == sender))
649 /* Send the packet */
650 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
654 data, data_len, FALSE,
658 silc_hash_table_list_reset(&htl);
660 silc_free(packetdata.src_id);
661 silc_free(packetdata.dst_id);
664 /* This checks whether the relayed packet came from router. If it did
665 then we'll need to encrypt it with the channel key. This is called
666 from the silc_server_packet_relay_to_channel. */
669 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
670 SilcSocketConnection sock,
671 SilcChannelEntry channel,
673 unsigned int data_len)
675 /* If we are router and the packet came from router and private key
676 has not been set for the channel then we must encrypt the packet
677 as it was decrypted with the session key shared between us and the
678 router which sent it. This is so, because cells does not share the
680 if (server->server_type == SILC_ROUTER &&
681 sock->type == SILC_SOCKET_TYPE_ROUTER &&
682 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
683 channel->channel_key) {
684 SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
685 SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
686 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
688 if (data_len <= mac_len + iv_len) {
689 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
693 memcpy(iv, data + (data_len - iv_len), iv_len);
694 silc_channel_message_payload_encrypt(data, data_len - iv_len - mac_len,
695 data_len, iv, iv_len,
696 channel->channel_key, channel->hmac);
702 /* This routine is explicitly used to relay messages to some channel.
703 Packets sent with this function we have received earlier and are
704 totally encrypted. This just sends the packet to all clients on
705 the channel. If the sender of the packet is someone on the channel
706 the message will not be sent to that client. The SILC Packet header
707 is encrypted with the session key shared between us and the client.
708 MAC is also computed before encrypting the header. Rest of the
709 packet will be untouched. */
711 void silc_server_packet_relay_to_channel(SilcServer server,
712 SilcSocketConnection sender_sock,
713 SilcChannelEntry channel,
715 SilcIdType sender_type,
716 SilcClientEntry sender_entry,
721 SilcSocketConnection sock = NULL;
722 SilcPacketContext packetdata;
723 SilcClientEntry client = NULL;
724 SilcServerEntry *routed = NULL;
725 SilcChannelClientEntry chl, chl_sender;
726 SilcUInt32 routed_count = 0;
727 SilcIDListData idata;
728 SilcHashTableList htl;
732 SILC_LOG_DEBUG(("Relaying packet to channel"));
734 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
737 /* This encrypts the packet, if needed. It will be encrypted if
738 it came from the router thus it needs to be encrypted with the
739 channel key. If the channel key does not exist, then we know we
740 don't have a single local user on the channel. */
741 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
746 /* Set the packet context pointers. */
747 packetdata.flags = 0;
748 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
749 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
750 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
751 packetdata.src_id_type = sender_type;
752 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
753 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
754 packetdata.dst_id_type = SILC_ID_CHANNEL;
756 /* If there are global users in the channel we will send the message
757 first to our router for further routing. */
758 if (server->server_type != SILC_ROUTER && !server->standalone &&
759 channel->global_users) {
760 SilcServerEntry router = server->router;
762 /* Check that the sender is not our router. */
763 if (sender_sock != (SilcSocketConnection)router->connection) {
765 /* Get data used in packet header encryption, keys and stuff. */
766 sock = (SilcSocketConnection)router->connection;
767 idata = (SilcIDListData)router;
769 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
771 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
775 data, data_len, TRUE,
780 routed = silc_calloc(silc_hash_table_count(channel->user_list),
783 /* Assure we won't route the message back to the sender's way. */
784 if (sender_entry->router)
785 routed[routed_count++] = sender_entry->router;
787 /* Send the message to clients on the channel's client list. */
788 silc_hash_table_list(channel->user_list, &htl);
789 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
790 client = chl->client;
791 if (!client || client == sender_entry)
794 /* Check whether message sending is blocked */
795 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
797 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
798 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
799 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
801 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
802 sender_entry->mode & SILC_UMODE_ROBOT)
805 /* If the client has set router it means that it is not locally
806 connected client and we will route the packet further. */
807 if (server->server_type == SILC_ROUTER && client->router) {
809 /* Check if we have sent the packet to this route already */
810 for (k = 0; k < routed_count; k++)
811 if (routed[k] == client->router)
813 if (k < routed_count)
816 /* Get data used in packet header encryption, keys and stuff. */
817 sock = (SilcSocketConnection)client->router->connection;
818 idata = (SilcIDListData)client->router;
820 /* Check if the sender socket is the same as this client's router
822 if (sender_sock && sock == sender_sock)
825 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
826 silc_id_render(client->id, SILC_ID_CLIENT),
827 sock->hostname, sock->ip));
829 /* Mark this route routed already. */
830 routed[routed_count++] = client->router;
832 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
833 /* The remote connection is router then we'll decrypt the
834 channel message and re-encrypt it with the session key shared
835 between us and the remote router. This is done because the
836 channel keys are cell specific and we have different channel
837 key than the remote router has. */
839 /* Route only once to router. Protocol prohibits sending channel
840 messages to more than one router. */
845 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
847 /* If private key mode is not set then decrypt the packet
849 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
850 channel->channel_key) {
851 unsigned char tmp[SILC_PACKET_MAX_LEN];
853 if (data_len > SILC_PACKET_MAX_LEN)
854 data_len = SILC_PACKET_MAX_LEN;
855 memcpy(tmp, data, data_len);
857 /* Decrypt the channel message (we don't check the MAC) */
858 silc_channel_message_payload_decrypt(tmp, data_len,
859 channel->channel_key, NULL);
861 /* Now re-encrypt and send it to the router */
862 silc_server_packet_send_srcdest(server, sock,
863 SILC_PACKET_CHANNEL_MESSAGE, 0,
864 sender_id, sender_type,
865 channel->id, SILC_ID_CHANNEL,
866 tmp, data_len, force_send);
868 /* Private key mode is set, we don't have the channel key, so
869 just re-encrypt the entire packet and send it to the router. */
870 silc_server_packet_send_srcdest(server, sock,
871 SILC_PACKET_CHANNEL_MESSAGE, 0,
872 sender_id, sender_type,
873 channel->id, SILC_ID_CHANNEL,
874 data, data_len, force_send);
877 /* Send the packet to normal server */
878 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
882 data, data_len, TRUE,
892 /* Get data used in packet header encryption, keys and stuff. */
893 sock = (SilcSocketConnection)client->connection;
894 idata = (SilcIDListData)client;
896 if (!sock || (sender_sock && sock == sender_sock))
899 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
900 silc_id_render(client->id, SILC_ID_CLIENT),
901 sock->hostname, sock->ip));
903 /* Send the packet */
904 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
908 data, data_len, TRUE,
912 silc_hash_table_list_reset(&htl);
914 silc_free(packetdata.src_id);
915 silc_free(packetdata.dst_id);
918 /* This function is used to send packets strictly to all local clients
919 on a particular channel. This is used for example to distribute new
920 channel key to all our locally connected clients on the channel.
921 The packets are always encrypted with the session key shared between
922 the client, this means these are not _to the channel_ but _to the client_
925 void silc_server_packet_send_local_channel(SilcServer server,
926 SilcChannelEntry channel,
928 SilcPacketFlags flags,
933 SilcChannelClientEntry chl;
934 SilcHashTableList htl;
935 SilcSocketConnection sock = NULL;
937 SILC_LOG_DEBUG(("Start"));
939 /* Send the message to clients on the channel's client list. */
940 silc_hash_table_list(channel->user_list, &htl);
941 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
942 if (chl->client && !chl->client->router) {
943 sock = (SilcSocketConnection)chl->client->connection;
945 /* Send the packet to the client */
946 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
947 SILC_ID_CLIENT, data, data_len,
951 silc_hash_table_list_reset(&htl);
954 /* Routine used to send (relay, route) private messages to some destination.
955 If the private message key does not exist then the message is re-encrypted,
956 otherwise we just pass it along. This really is not used to send new
957 private messages (as server does not send them) but to relay received
960 void silc_server_send_private_message(SilcServer server,
961 SilcSocketConnection dst_sock,
965 SilcPacketContext *packet)
967 SilcBuffer buffer = packet->buffer;
968 const SilcBufferStruct p;
970 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
971 + packet->dst_id_len + packet->padlen);
972 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
973 (const SilcBuffer)&p)) {
974 SILC_LOG_ERROR(("Cannot send packet"));
977 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
979 /* Re-encrypt and send if private messge key does not exist */
980 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
981 /* Re-encrypt packet */
982 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
984 /* Key exist so encrypt just header and send it */
985 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
986 SILC_PACKET_HEADER_LEN + packet->src_id_len +
987 packet->dst_id_len + packet->padlen);
990 /* Send the packet */
991 silc_server_packet_send_real(server, dst_sock, FALSE);
994 /* Sends current motd to client */
996 void silc_server_send_motd(SilcServer server,
997 SilcSocketConnection sock)
999 char *motd, *motd_file = NULL;
1000 SilcUInt32 motd_len;
1003 motd_file = server->config->server_info->motd_file;
1006 motd = silc_file_readfile(motd_file, &motd_len);
1010 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1016 /* Sends error message. Error messages may or may not have any
1019 void silc_server_send_error(SilcServer server,
1020 SilcSocketConnection sock,
1021 const char *fmt, ...)
1024 unsigned char buf[4096];
1026 memset(buf, 0, sizeof(buf));
1028 vsprintf(buf, fmt, ap);
1031 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1032 buf, strlen(buf), FALSE);
1035 /* Sends notify message. If format is TRUE the variable arguments are
1036 formatted and the formatted string is sent as argument payload. If it is
1037 FALSE then each argument is sent as separate argument and their format
1038 in the argument list must be { argument data, argument length }. */
1040 void silc_server_send_notify(SilcServer server,
1041 SilcSocketConnection sock,
1043 SilcNotifyType type,
1044 SilcUInt32 argc, ...)
1051 packet = silc_notify_payload_encode(type, argc, ap);
1052 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1053 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1054 packet->data, packet->len, FALSE);
1056 /* Send to backup routers if this is being broadcasted to primary
1058 if (server->router && server->router->connection &&
1059 sock == server->router->connection && broadcast)
1060 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1061 packet->data, packet->len, FALSE, TRUE);
1063 silc_buffer_free(packet);
1067 /* Sends notify message and gets the arguments from the `args' Argument
1070 void silc_server_send_notify_args(SilcServer server,
1071 SilcSocketConnection sock,
1073 SilcNotifyType type,
1079 packet = silc_notify_payload_encode_args(type, argc, args);
1080 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1081 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1082 packet->data, packet->len, FALSE);
1083 silc_buffer_free(packet);
1086 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1087 `old_id' with the `new_id'. */
1089 void silc_server_send_notify_channel_change(SilcServer server,
1090 SilcSocketConnection sock,
1092 SilcChannelID *old_id,
1093 SilcChannelID *new_id)
1095 SilcBuffer idp1, idp2;
1097 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1098 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1100 silc_server_send_notify(server, sock, broadcast,
1101 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1102 2, idp1->data, idp1->len, idp2->data, idp2->len);
1103 silc_buffer_free(idp1);
1104 silc_buffer_free(idp2);
1107 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1108 `old_id' with the `new_id'. */
1110 void silc_server_send_notify_nick_change(SilcServer server,
1111 SilcSocketConnection sock,
1113 SilcClientID *old_id,
1114 SilcClientID *new_id,
1115 const char *nickname)
1117 SilcBuffer idp1, idp2;
1119 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1120 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1122 silc_server_send_notify(server, sock, broadcast,
1123 SILC_NOTIFY_TYPE_NICK_CHANGE,
1124 3, idp1->data, idp1->len, idp2->data, idp2->len,
1125 nickname, nickname ? strlen(nickname) : 0);
1126 silc_buffer_free(idp1);
1127 silc_buffer_free(idp2);
1130 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1131 has joined to the `channel'. */
1133 void silc_server_send_notify_join(SilcServer server,
1134 SilcSocketConnection sock,
1136 SilcChannelEntry channel,
1137 SilcClientID *client_id)
1139 SilcBuffer idp1, idp2;
1141 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1142 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1143 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1144 2, idp1->data, idp1->len,
1145 idp2->data, idp2->len);
1146 silc_buffer_free(idp1);
1147 silc_buffer_free(idp2);
1150 /* Sends LEAVE notify type. This tells that `client_id' has left the
1151 `channel'. The Notify packet is always destined to the channel. */
1153 void silc_server_send_notify_leave(SilcServer server,
1154 SilcSocketConnection sock,
1156 SilcChannelEntry channel,
1157 SilcClientID *client_id)
1161 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1162 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1163 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1164 1, idp->data, idp->len);
1165 silc_buffer_free(idp);
1168 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1169 `channel' mode to `mode. The Notify packet is always destined to
1172 void silc_server_send_notify_cmode(SilcServer server,
1173 SilcSocketConnection sock,
1175 SilcChannelEntry channel,
1176 SilcUInt32 mode_mask,
1177 void *id, SilcIdType id_type,
1178 const char *cipher, const char *hmac,
1179 const char *passphrase,
1180 SilcPublicKey founder_key)
1183 unsigned char mode[4], *key = NULL;
1184 SilcUInt32 key_len = 0;
1186 idp = silc_id_payload_encode((void *)id, id_type);
1187 SILC_PUT32_MSB(mode_mask, mode);
1189 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1191 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1192 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1193 5, idp->data, idp->len,
1195 cipher, cipher ? strlen(cipher) : 0,
1196 hmac, hmac ? strlen(hmac) : 0,
1197 passphrase, passphrase ?
1198 strlen(passphrase) : 0,
1201 silc_buffer_free(idp);
1204 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1205 `target' client's mode on `channel'. The notify packet is always
1206 destined to the channel. */
1208 void silc_server_send_notify_cumode(SilcServer server,
1209 SilcSocketConnection sock,
1211 SilcChannelEntry channel,
1212 SilcUInt32 mode_mask,
1213 void *id, SilcIdType id_type,
1214 SilcClientID *target)
1216 SilcBuffer idp1, idp2;
1217 unsigned char mode[4];
1219 idp1 = silc_id_payload_encode((void *)id, id_type);
1220 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1221 SILC_PUT32_MSB(mode_mask, mode);
1223 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1225 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
1226 idp1->data, idp1->len,
1228 idp2->data, idp2->len);
1229 silc_buffer_free(idp1);
1230 silc_buffer_free(idp2);
1233 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1234 left SILC network. This function is used only between server and router
1235 traffic. This is not used to send the notify to the channel for
1236 client. The `message may be NULL. */
1238 void silc_server_send_notify_signoff(SilcServer server,
1239 SilcSocketConnection sock,
1241 SilcClientID *client_id,
1242 const char *message)
1246 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1247 silc_server_send_notify(server, sock, broadcast,
1248 SILC_NOTIFY_TYPE_SIGNOFF,
1249 message ? 2 : 1, idp->data, idp->len,
1250 message, message ? strlen(message): 0);
1251 silc_buffer_free(idp);
1254 /* Sends TOPIC_SET notify type. This tells that `id' changed
1255 the `channel's topic to `topic'. The Notify packet is always destined
1256 to the channel. This function is used to send the topic set notifies
1259 void silc_server_send_notify_topic_set(SilcServer server,
1260 SilcSocketConnection sock,
1262 SilcChannelEntry channel,
1263 void *id, SilcIdType id_type,
1268 idp = silc_id_payload_encode(id, id_type);
1269 silc_server_send_notify_dest(server, sock, broadcast,
1270 (void *)channel->id, SILC_ID_CHANNEL,
1271 SILC_NOTIFY_TYPE_TOPIC_SET,
1273 idp->data, idp->len,
1274 topic, topic ? strlen(topic) : 0);
1275 silc_buffer_free(idp);
1278 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1279 was kicked off the channel. The `comment' may indicate the reason
1280 for the kicking. This function is used only between server and router
1283 void silc_server_send_notify_kicked(SilcServer server,
1284 SilcSocketConnection sock,
1286 SilcChannelEntry channel,
1287 SilcClientID *client_id,
1288 SilcClientID *kicker,
1294 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1295 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1296 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1297 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1298 idp1->data, idp1->len,
1299 comment, comment ? strlen(comment) : 0,
1300 idp2->data, idp2->len);
1301 silc_buffer_free(idp1);
1302 silc_buffer_free(idp2);
1305 /* Send KILLED notify type. This tells that the `client_id' client was
1306 killed from the network. The `comment' may indicate the reason
1309 void silc_server_send_notify_killed(SilcServer server,
1310 SilcSocketConnection sock,
1312 SilcClientID *client_id,
1313 const char *comment,
1314 void *killer, SilcIdType killer_type)
1319 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1320 idp2 = silc_id_payload_encode(killer, killer_type);
1321 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1322 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1323 3, idp1->data, idp1->len,
1324 comment, comment ? strlen(comment) : 0,
1325 idp2->data, idp2->len);
1326 silc_buffer_free(idp1);
1327 silc_buffer_free(idp2);
1330 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1331 user mode in the SILC Network was changed. This function is used to
1332 send the packet between routers as broadcast packet. */
1334 void silc_server_send_notify_umode(SilcServer server,
1335 SilcSocketConnection sock,
1337 SilcClientID *client_id,
1338 SilcUInt32 mode_mask)
1341 unsigned char mode[4];
1343 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1344 SILC_PUT32_MSB(mode_mask, mode);
1346 silc_server_send_notify(server, sock, broadcast,
1347 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1348 idp->data, idp->len,
1350 silc_buffer_free(idp);
1353 /* Sends BAN notify type. This tells that ban has been either `add'ed
1354 or `del'eted on the `channel. This function is used to send the packet
1355 between routers as broadcast packet. */
1357 void silc_server_send_notify_ban(SilcServer server,
1358 SilcSocketConnection sock,
1360 SilcChannelEntry channel,
1361 char *add, char *del)
1365 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1366 silc_server_send_notify(server, sock, broadcast,
1367 SILC_NOTIFY_TYPE_BAN, 3,
1368 idp->data, idp->len,
1369 add, add ? strlen(add) : 0,
1370 del, del ? strlen(del) : 0);
1371 silc_buffer_free(idp);
1374 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1375 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1376 This function is used to send the packet between routers as broadcast
1379 void silc_server_send_notify_invite(SilcServer server,
1380 SilcSocketConnection sock,
1382 SilcChannelEntry channel,
1383 SilcClientID *client_id,
1384 char *add, char *del)
1386 SilcBuffer idp, idp2;
1388 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1389 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1390 silc_server_send_notify(server, sock, broadcast,
1391 SILC_NOTIFY_TYPE_INVITE, 5,
1392 idp->data, idp->len,
1393 channel->channel_name, strlen(channel->channel_name),
1394 idp2->data, idp2->len,
1395 add, add ? strlen(add) : 0,
1396 del, del ? strlen(del) : 0);
1397 silc_buffer_free(idp);
1398 silc_buffer_free(idp2);
1401 /* Sends WATCH notify type. This tells that the `client' was watched and
1402 its status in the network has changed. */
1404 void silc_server_send_notify_watch(SilcServer server,
1405 SilcSocketConnection sock,
1406 SilcClientEntry watcher,
1407 SilcClientEntry client,
1408 const char *nickname,
1409 SilcNotifyType type)
1412 unsigned char mode[4], n[2];
1414 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1415 SILC_PUT16_MSB(type, n);
1416 SILC_PUT32_MSB(client->mode, mode);
1417 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1418 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1419 4, idp->data, idp->len,
1420 nickname, strlen(nickname),
1422 type != SILC_NOTIFY_TYPE_NONE ?
1423 n : NULL, sizeof(n));
1424 silc_buffer_free(idp);
1427 /* Sends notify message destined to specific entity. */
1429 void silc_server_send_notify_dest(SilcServer server,
1430 SilcSocketConnection sock,
1433 SilcIdType dest_id_type,
1434 SilcNotifyType type,
1435 SilcUInt32 argc, ...)
1442 packet = silc_notify_payload_encode(type, argc, ap);
1443 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1444 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1445 dest_id, dest_id_type,
1446 packet->data, packet->len, FALSE);
1447 silc_buffer_free(packet);
1451 /* Sends notify message to a channel. The notify message sent is
1452 distributed to all clients on the channel. If `route_notify' is TRUE
1453 then the notify may be routed to primary route or to some other routers.
1454 If FALSE it is assured that the notify is sent only locally. If `sender'
1455 is provided then the packet is not sent to that connection since it
1456 originally came from it. */
1458 void silc_server_send_notify_to_channel(SilcServer server,
1459 SilcSocketConnection sender,
1460 SilcChannelEntry channel,
1462 SilcNotifyType type,
1463 SilcUInt32 argc, ...)
1470 packet = silc_notify_payload_encode(type, argc, ap);
1471 silc_server_packet_send_to_channel(server, sender, channel,
1472 SILC_PACKET_NOTIFY, route_notify,
1473 packet->data, packet->len, FALSE);
1474 silc_buffer_free(packet);
1478 /* Send notify message to all channels the client has joined. It is quaranteed
1479 that the message is sent only once to a client (ie. if a client is joined
1480 on two same channel it will receive only one notify message). Also, this
1481 sends only to local clients (locally connected if we are server, and to
1482 local servers if we are router). If `sender' is provided the packet is
1483 not sent to that client at all. */
1485 void silc_server_send_notify_on_channels(SilcServer server,
1486 SilcClientEntry sender,
1487 SilcClientEntry client,
1488 SilcNotifyType type,
1489 SilcUInt32 argc, ...)
1492 SilcSocketConnection sock = NULL;
1493 SilcPacketContext packetdata;
1495 SilcClientEntry *sent_clients = NULL;
1496 SilcUInt32 sent_clients_count = 0;
1497 SilcServerEntry *routed = NULL;
1498 SilcUInt32 routed_count = 0;
1499 SilcHashTableList htl, htl2;
1500 SilcChannelEntry channel;
1501 SilcChannelClientEntry chl, chl2;
1502 SilcIDListData idata;
1504 unsigned char *data;
1505 SilcUInt32 data_len;
1506 bool force_send = FALSE;
1509 SILC_LOG_DEBUG(("Start"));
1511 if (!silc_hash_table_count(client->channels))
1515 packet = silc_notify_payload_encode(type, argc, ap);
1516 data = packet->data;
1517 data_len = packet->len;
1519 /* Set the packet context pointers. */
1520 packetdata.flags = 0;
1521 packetdata.type = SILC_PACKET_NOTIFY;
1522 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1523 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1524 packetdata.src_id_type = SILC_ID_SERVER;
1526 silc_hash_table_list(client->channels, &htl);
1527 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1528 channel = chl->channel;
1530 /* Send the message to all clients on the channel's client list. */
1531 silc_hash_table_list(channel->user_list, &htl2);
1532 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1535 if (sender && c == sender)
1538 /* Check if we have sent the packet to this client already */
1539 for (k = 0; k < sent_clients_count; k++)
1540 if (sent_clients[k] == c)
1542 if (k < sent_clients_count)
1545 /* If we are router and if this client has router set it is not
1546 locally connected client and we will route the message to the
1547 router set in the client. */
1548 if (c && c->router && server->server_type == SILC_ROUTER) {
1549 /* Check if we have sent the packet to this route already */
1550 for (k = 0; k < routed_count; k++)
1551 if (routed[k] == c->router)
1553 if (k < routed_count)
1556 /* Get data used in packet header encryption, keys and stuff. */
1557 sock = (SilcSocketConnection)c->router->connection;
1558 idata = (SilcIDListData)c->router;
1561 SILC_LOG_DEBUG(("*****************"));
1562 SILC_LOG_DEBUG(("client->router->id %s",
1563 silc_id_render(c->router->id, SILC_ID_SERVER)));
1564 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1565 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1568 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1569 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1570 packetdata.dst_id_type = SILC_ID_SERVER;
1572 /* Send the packet */
1573 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1577 data, data_len, FALSE,
1580 silc_free(packetdata.dst_id);
1582 /* We want to make sure that the packet is routed to same router
1583 only once. Mark this route as sent route. */
1584 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1585 routed[routed_count++] = c->router;
1592 /* Send to locally connected client */
1595 /* Get data used in packet header encryption, keys and stuff. */
1596 sock = (SilcSocketConnection)c->connection;
1597 idata = (SilcIDListData)c;
1602 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1603 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1604 packetdata.dst_id_type = SILC_ID_CLIENT;
1606 /* Send the packet */
1607 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1611 data, data_len, FALSE,
1614 silc_free(packetdata.dst_id);
1616 /* Make sure that we send the notify only once per client. */
1617 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1618 (sent_clients_count + 1));
1619 sent_clients[sent_clients_count++] = c;
1622 silc_hash_table_list_reset(&htl2);
1625 silc_hash_table_list_reset(&htl);
1627 silc_free(sent_clients);
1628 silc_free(packetdata.src_id);
1629 silc_buffer_free(packet);
1633 /* Sends New ID Payload to remote end. The packet is used to distribute
1634 information about new registered clients, servers, channel etc. usually
1635 to routers so that they can keep these information up to date.
1636 If the argument `broadcast' is TRUE then the packet is sent as
1637 broadcast packet. */
1639 void silc_server_send_new_id(SilcServer server,
1640 SilcSocketConnection sock,
1642 void *id, SilcIdType id_type,
1647 SILC_LOG_DEBUG(("Start"));
1649 idp = silc_id_payload_encode(id, id_type);
1650 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1651 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1652 idp->data, idp->len, FALSE);
1654 /* Send to backup routers if this is being broadcasted to primary
1656 if (server->router && server->router->connection &&
1657 sock == server->router->connection && broadcast)
1658 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
1659 idp->data, idp->len, FALSE, TRUE);
1661 silc_buffer_free(idp);
1664 /* Send New Channel Payload to notify about newly created channel in the
1665 SILC network. Router uses this to notify other routers in the network
1666 about new channel. This packet is broadcasted by router. */
1668 void silc_server_send_new_channel(SilcServer server,
1669 SilcSocketConnection sock,
1673 SilcUInt32 channel_id_len,
1678 SilcUInt32 name_len = strlen(channel_name);
1680 SILC_LOG_DEBUG(("Start"));
1682 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1686 /* Encode the channel payload */
1687 packet = silc_channel_payload_encode(channel_name, name_len,
1688 cid, channel_id_len, mode);
1690 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1691 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1692 packet->data, packet->len, FALSE);
1694 /* Send to backup routers if this is being broadcasted to primary
1696 if (server->server_type == SILC_ROUTER &&
1697 server->router && server->router->connection &&
1698 sock == server->router->connection && broadcast)
1699 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
1700 packet->data, packet->len, FALSE, TRUE);
1703 silc_buffer_free(packet);
1706 /* Send Channel Key payload to distribute the new channel key. Normal server
1707 sends this to router when new client joins to existing channel. Router
1708 sends this to the local server who sent the join command in case where
1709 the channel did not exist yet. Both normal and router servers uses this
1710 also to send this to locally connected clients on the channel. This
1711 must not be broadcasted packet. Routers do not send this to each other.
1712 If `sender is provided then the packet is not sent to that connection since
1713 it originally came from it. */
1715 void silc_server_send_channel_key(SilcServer server,
1716 SilcSocketConnection sender,
1717 SilcChannelEntry channel,
1718 unsigned char route)
1721 unsigned char *chid;
1724 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1726 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1730 /* Encode channel key packet */
1731 tmp_len = strlen(channel->channel_key->cipher->name);
1732 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1735 channel->channel_key->cipher->name,
1736 channel->key_len / 8, channel->key);
1737 silc_server_packet_send_to_channel(server, sender, channel,
1738 SILC_PACKET_CHANNEL_KEY,
1739 route, packet->data, packet->len,
1741 silc_buffer_free(packet);
1745 /* Generic function to send any command. The arguments must be sent already
1746 encoded into correct form in correct order. */
1748 void silc_server_send_command(SilcServer server,
1749 SilcSocketConnection sock,
1750 SilcCommand command,
1752 SilcUInt32 argc, ...)
1759 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1760 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1761 packet->data, packet->len, TRUE);
1762 silc_buffer_free(packet);
1766 /* Generic function to send any command reply. The arguments must be sent
1767 already encoded into correct form in correct order. */
1769 void silc_server_send_command_reply(SilcServer server,
1770 SilcSocketConnection sock,
1771 SilcCommand command,
1775 SilcUInt32 argc, ...)
1782 packet = silc_command_reply_payload_encode_vap(command, status, error,
1784 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1785 packet->data, packet->len, TRUE);
1786 silc_buffer_free(packet);
1790 /* Generic function to send any command reply. The arguments must be sent
1791 already encoded into correct form in correct order. */
1793 void silc_server_send_dest_command_reply(SilcServer server,
1794 SilcSocketConnection sock,
1796 SilcIdType dst_id_type,
1797 SilcCommand command,
1801 SilcUInt32 argc, ...)
1808 packet = silc_command_reply_payload_encode_vap(command, status, error,
1810 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1811 dst_id, dst_id_type, packet->data,
1813 silc_buffer_free(packet);
1817 /* Send the heartbeat packet. */
1819 void silc_server_send_heartbeat(SilcServer server,
1820 SilcSocketConnection sock)
1822 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1826 /* Generic function to relay packet we've received. This is used to relay
1827 packets to a client but generally can be used to other purposes as well. */
1829 void silc_server_relay_packet(SilcServer server,
1830 SilcSocketConnection dst_sock,
1833 SilcUInt32 sequence,
1834 SilcPacketContext *packet,
1837 const SilcBufferStruct p;
1839 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1840 + packet->dst_id_len + packet->padlen);
1841 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1842 (const SilcBuffer)&p)) {
1843 SILC_LOG_ERROR(("Cannot send packet"));
1846 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1848 /* Re-encrypt packet */
1849 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1851 /* Send the packet */
1852 silc_server_packet_send_real(server, dst_sock, force_send);
1854 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1855 + packet->dst_id_len + packet->padlen);
1858 /* Routine used to send the connection authentication packet. */
1860 void silc_server_send_connection_auth_request(SilcServer server,
1861 SilcSocketConnection sock,
1862 SilcUInt16 conn_type,
1863 SilcAuthMethod auth_meth)
1867 packet = silc_buffer_alloc(4);
1868 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1869 silc_buffer_format(packet,
1870 SILC_STR_UI_SHORT(conn_type),
1871 SILC_STR_UI_SHORT(auth_meth),
1874 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1875 0, packet->data, packet->len, FALSE);
1876 silc_buffer_free(packet);
1879 /* Purge the outgoing packet queue to the network if there is data. This
1880 function can be used to empty the packet queue. It is guaranteed that
1881 after this function returns the outgoing data queue is empty. */
1883 void silc_server_packet_queue_purge(SilcServer server,
1884 SilcSocketConnection sock)
1886 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1887 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1888 server->stat.packets_sent++;
1889 silc_packet_send(sock, TRUE);
1890 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1891 SILC_UNSET_OUTBUF_PENDING(sock);
1892 silc_buffer_clear(sock->outbuf);