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;
88 SilcIDListData idata = (SilcIDListData)sock->user_data;
93 /* If disconnecting, ignore the data */
94 if (SILC_IS_DISCONNECTING(sock))
97 /* If entry is disabled do not sent anything. */
98 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
101 /* Get data used in the packet sending, keys and stuff */
103 case SILC_SOCKET_TYPE_CLIENT:
104 if (sock->user_data) {
105 dst_id = ((SilcClientEntry)sock->user_data)->id;
106 dst_id_type = SILC_ID_CLIENT;
109 case SILC_SOCKET_TYPE_SERVER:
110 case SILC_SOCKET_TYPE_ROUTER:
111 if (sock->user_data) {
112 dst_id = ((SilcServerEntry)sock->user_data)->id;
113 dst_id_type = SILC_ID_SERVER;
120 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
121 dst_id_type, data, data_len, force_send);
124 /* Assembles a new packet to be sent out to network. This doesn't actually
125 send the packet but creates the packet and fills the outgoing data
126 buffer and marks the packet ready to be sent to network. However, If
127 argument force_send is TRUE the packet is sent immediately and not put
128 to queue. Normal case is that the packet is not sent immediately.
129 Destination information is sent as argument for this function. */
131 void silc_server_packet_send_dest(SilcServer server,
132 SilcSocketConnection sock,
134 SilcPacketFlags flags,
136 SilcIdType dst_id_type,
141 SilcPacketContext packetdata;
142 const SilcBufferStruct packet;
143 SilcIDListData idata = (SilcIDListData)sock->user_data;
144 SilcCipher cipher = NULL;
145 SilcHmac hmac = NULL;
146 SilcUInt32 sequence = 0;
147 unsigned char *dst_id_data = NULL;
148 SilcUInt32 dst_id_len = 0;
151 /* If disconnecting, ignore the data */
152 if (SILC_IS_DISCONNECTING(sock))
155 /* If entry is disabled do not sent anything. */
156 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
159 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
162 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
163 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
167 cipher = idata->send_key;
168 hmac = idata->hmac_send;
169 sequence = idata->psn_send++;
170 block_len = silc_cipher_get_block_len(cipher);
173 /* Set the packet context pointers */
174 packetdata.type = type;
175 packetdata.flags = flags;
176 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
177 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
178 packetdata.src_id_type = SILC_ID_SERVER;
179 packetdata.dst_id = dst_id_data;
180 packetdata.dst_id_len = dst_id_len;
181 packetdata.dst_id_type = dst_id_type;
182 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
183 packetdata.src_id_len +
184 packetdata.dst_id_len));
185 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
186 packetdata.src_id_len + dst_id_len;
187 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
189 /* Create the outgoing packet */
190 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock,
191 data, data_len, (const SilcBuffer)&packet)) {
192 SILC_LOG_ERROR(("Cannot assemble packet"));
196 /* Encrypt the packet */
197 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
199 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
200 packet.data, packet.len);
202 /* Now actually send the packet */
203 silc_server_packet_send_real(server, sock, force_send);
206 if (packetdata.src_id)
207 silc_free(packetdata.src_id);
208 if (packetdata.dst_id)
209 silc_free(packetdata.dst_id);
212 /* Assembles a new packet to be sent out to network. This doesn't actually
213 send the packet but creates the packet and fills the outgoing data
214 buffer and marks the packet ready to be sent to network. However, If
215 argument force_send is TRUE the packet is sent immediately and not put
216 to queue. Normal case is that the packet is not sent immediately.
217 The source and destination information is sent as argument for this
220 void silc_server_packet_send_srcdest(SilcServer server,
221 SilcSocketConnection sock,
223 SilcPacketFlags flags,
225 SilcIdType src_id_type,
227 SilcIdType dst_id_type,
232 SilcPacketContext packetdata;
233 const SilcBufferStruct packet;
234 SilcIDListData idata;
235 SilcCipher cipher = NULL;
236 SilcHmac hmac = NULL;
237 SilcUInt32 sequence = 0;
238 unsigned char *dst_id_data = NULL;
239 SilcUInt32 dst_id_len = 0;
240 unsigned char *src_id_data = NULL;
241 SilcUInt32 src_id_len = 0;
244 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
246 /* Get data used in the packet sending, keys and stuff */
247 idata = (SilcIDListData)sock->user_data;
250 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
251 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
255 src_id_data = silc_id_id2str(src_id, src_id_type);
256 src_id_len = silc_id_get_len(src_id, src_id_type);
260 cipher = idata->send_key;
261 hmac = idata->hmac_send;
262 sequence = idata->psn_send++;
263 block_len = silc_cipher_get_block_len(cipher);
266 /* Set the packet context pointers */
267 packetdata.type = type;
268 packetdata.flags = flags;
269 packetdata.src_id = src_id_data;
270 packetdata.src_id_len = src_id_len;
271 packetdata.src_id_type = src_id_type;
272 packetdata.dst_id = dst_id_data;
273 packetdata.dst_id_len = dst_id_len;
274 packetdata.dst_id_type = dst_id_type;
275 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
276 packetdata.src_id_len +
278 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
279 packetdata.src_id_len + dst_id_len;
280 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
282 /* Create the outgoing packet */
283 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock, data,
284 data_len, (const SilcBuffer)&packet)) {
285 SILC_LOG_ERROR(("Cannot assemble packe"));
289 /* Encrypt the packet */
290 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
292 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
293 packet.data, packet.len);
295 /* Now actually send the packet */
296 silc_server_packet_send_real(server, sock, force_send);
299 if (packetdata.src_id)
300 silc_free(packetdata.src_id);
301 if (packetdata.dst_id)
302 silc_free(packetdata.dst_id);
305 /* Broadcast received packet to our primary route. This function is used
306 by router to further route received broadcast packet. It is expected
307 that the broadcast flag from the packet is checked before calling this
308 function. This does not test or set the broadcast flag. */
310 void silc_server_packet_broadcast(SilcServer server,
311 SilcSocketConnection sock,
312 SilcPacketContext *packet)
314 SilcBuffer buffer = packet->buffer;
315 SilcIDListData idata;
318 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
320 /* If the packet is originated from our primary route we are
321 not allowed to send the packet. */
322 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
323 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
324 const SilcBufferStruct p;
326 idata = (SilcIDListData)sock->user_data;
328 silc_buffer_push(buffer, buffer->data - buffer->head);
329 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
330 (const SilcBuffer)&p)) {
331 SILC_LOG_ERROR(("Cannot send packet"));
335 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
336 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
337 (SilcBuffer)&p, p.len);
339 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
340 p.len), p.data, p.len);
342 /* Now actually send the packet */
343 silc_server_packet_send_real(server, sock, TRUE);
348 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
349 "original sender of this packet"));
353 /* Routes received packet to `sock'. This is used to route the packets that
354 router receives but are not destined to it. */
356 void silc_server_packet_route(SilcServer server,
357 SilcSocketConnection sock,
358 SilcPacketContext *packet)
360 SilcBuffer buffer = packet->buffer;
361 const SilcBufferStruct p;
362 SilcIDListData idata;
364 SILC_LOG_DEBUG(("Routing received packet"));
366 idata = (SilcIDListData)sock->user_data;
368 silc_buffer_push(buffer, buffer->data - buffer->head);
369 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
370 (const SilcBuffer)&p)) {
371 SILC_LOG_ERROR(("Cannot send packet"));
374 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
375 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
376 (SilcBuffer)&p, p.len);
378 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
379 p.len), p.data, p.len);
381 /* Now actually send the packet */
382 silc_server_packet_send_real(server, sock, TRUE);
385 /* This routine can be used to send a packet to table of clients provided
386 in `clients'. If `route' is FALSE the packet is routed only to local
387 clients (for server locally connected, and for router local cell). */
389 void silc_server_packet_send_clients(SilcServer server,
390 SilcHashTable clients,
392 SilcPacketFlags flags,
398 SilcSocketConnection sock = NULL;
399 SilcHashTableList htl;
400 SilcClientEntry client = NULL;
401 SilcServerEntry *routed = NULL;
402 SilcUInt32 routed_count = 0;
406 SILC_LOG_DEBUG(("Sending packet to list of clients"));
408 /* Send to all clients in table */
409 silc_hash_table_list(clients, &htl);
410 while (silc_hash_table_get(&htl, NULL, (void **)&client)) {
411 /* If client has router set it is not locally connected client and
412 we will route the message to the router set in the client. Though,
413 send locally connected server in all cases. */
414 if (server->server_type == SILC_ROUTER && client->router &&
415 ((!route && client->router->router == server->id_entry) || route)) {
417 /* Check if we have sent the packet to this route already */
418 for (k = 0; k < routed_count; k++)
419 if (routed[k] == client->router)
421 if (k < routed_count)
424 /* Route only once to router */
425 sock = (SilcSocketConnection)client->router->connection;
426 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
432 /* Send the packet */
433 silc_server_packet_send_dest(server, sock, type, flags,
434 client->router->id, SILC_ID_SERVER,
435 data, data_len, force_send);
437 /* Mark this route routed already */
438 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
439 routed[routed_count++] = client->router;
446 /* Send to locally connected client */
447 sock = (SilcSocketConnection)client->connection;
451 silc_server_packet_send_dest(server, sock, type, flags,
452 client->id, SILC_ID_CLIENT,
453 data, data_len, force_send);
455 silc_hash_table_list_reset(&htl);
459 /* Internal routine to actually create the channel packet and send it
460 to network. This is common function in channel message sending. If
461 `channel_message' is TRUE this encrypts the message as it is strictly
462 a channel message. If FALSE normal encryption process is used. */
465 silc_server_packet_send_to_channel_real(SilcServer server,
466 SilcSocketConnection sock,
467 SilcPacketContext *packet,
473 bool channel_message,
477 const SilcBufferStruct p;
482 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
484 packet->dst_id_len));
485 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
486 packet->src_id_len + packet->dst_id_len;
488 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
490 packet->padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
492 packet->dst_id_len), block_len);
494 packet->padlen = SILC_PACKET_PADLEN(packet->truelen, block_len);
496 /* Put the data to buffer, assemble and encrypt the packet. The packet
497 is encrypted with normal session key shared with the client, unless
498 the `channel_message' is TRUE. */
499 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
500 data_len, (const SilcBuffer)&p)) {
501 SILC_LOG_ERROR(("Cannot assemble packet"));
506 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
507 SILC_PACKET_HEADER_LEN + packet->src_id_len +
508 packet->dst_id_len + packet->padlen);
510 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
512 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
515 /* Now actually send the packet */
516 silc_server_packet_send_real(server, sock, force_send);
519 /* This routine is used by the server to send packets to channel. The
520 packet sent with this function is distributed to all clients on
521 the channel. Usually this is used to send notify messages to the
522 channel, things like notify about new user joining to the channel.
523 If `route' is FALSE then the packet is sent only locally and will not
524 be routed anywhere (for router locally means cell wide). If `sender'
525 is provided then the packet is not sent to that connection since it
526 originally came from it. If `send_to_clients' is FALSE then the
527 packet is not sent clients, only servers. */
529 void silc_server_packet_send_to_channel(SilcServer server,
530 SilcSocketConnection sender,
531 SilcChannelEntry channel,
538 SilcSocketConnection sock = NULL;
539 SilcPacketContext packetdata;
540 SilcClientEntry client = NULL;
541 SilcServerEntry *routed = NULL;
542 SilcChannelClientEntry chl;
543 SilcHashTableList htl;
544 SilcIDListData idata;
545 SilcUInt32 routed_count = 0;
549 /* This doesn't send channel message packets */
550 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
552 SILC_LOG_DEBUG(("Sending packet to channel"));
554 /* Set the packet context pointers. */
555 packetdata.flags = 0;
556 packetdata.type = type;
557 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
558 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
559 packetdata.src_id_type = SILC_ID_SERVER;
560 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
561 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
562 packetdata.dst_id_type = SILC_ID_CHANNEL;
564 /* If there are global users in the channel we will send the message
565 first to our router for further routing. */
566 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
567 channel->global_users) {
568 SilcServerEntry router;
570 /* Get data used in packet header encryption, keys and stuff. */
571 router = server->router;
572 sock = (SilcSocketConnection)router->connection;
573 idata = (SilcIDListData)router;
575 if (sock != sender) {
576 SILC_LOG_DEBUG(("Sending packet to router for routing"));
578 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
582 data, data_len, FALSE,
587 routed = silc_calloc(silc_hash_table_count(channel->user_list),
590 /* Send the message to clients on the channel's client list. */
591 silc_hash_table_list(channel->user_list, &htl);
592 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
593 client = chl->client;
597 /* If client has router set it is not locally connected client and
598 we will route the message to the router set in the client. Though,
599 send locally connected server in all cases. */
600 if (server->server_type == SILC_ROUTER && client->router &&
601 ((!route && client->router->router == server->id_entry) || route)) {
603 /* Check if we have sent the packet to this route already */
604 for (k = 0; k < routed_count; k++)
605 if (routed[k] == client->router)
607 if (k < routed_count)
610 /* Get data used in packet header encryption, keys and stuff. */
611 sock = (SilcSocketConnection)client->router->connection;
612 idata = (SilcIDListData)client->router;
614 if (sender && sock == sender)
617 /* Route only once to router. Protocol prohibits sending channel
618 messages to more than one router. */
619 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
625 /* Send the packet */
626 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
630 data, data_len, FALSE,
633 /* Mark this route routed already */
634 routed[routed_count++] = client->router;
641 /* Send to locally connected client */
643 /* Get data used in packet header encryption, keys and stuff. */
644 sock = (SilcSocketConnection)client->connection;
645 idata = (SilcIDListData)client;
647 if (!sock || (sender && sock == sender))
650 /* Send the packet */
651 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
655 data, data_len, FALSE,
659 silc_hash_table_list_reset(&htl);
661 silc_free(packetdata.src_id);
662 silc_free(packetdata.dst_id);
665 /* This checks whether the relayed packet came from router. If it did
666 then we'll need to encrypt it with the channel key. This is called
667 from the silc_server_packet_relay_to_channel. */
670 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
671 SilcSocketConnection sock,
672 SilcChannelEntry channel,
674 unsigned int data_len)
676 /* If we are router and the packet came from router and private key
677 has not been set for the channel then we must encrypt the packet
678 as it was decrypted with the session key shared between us and the
679 router which sent it. This is so, because cells does not share the
681 if (server->server_type == SILC_ROUTER &&
682 sock->type == SILC_SOCKET_TYPE_ROUTER &&
683 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
684 channel->channel_key) {
685 SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
686 SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
687 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
689 if (data_len <= mac_len + iv_len) {
690 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
694 memcpy(iv, data + (data_len - iv_len), iv_len);
695 silc_channel_message_payload_encrypt(data, data_len - iv_len - mac_len,
696 data_len, iv, iv_len,
697 channel->channel_key, channel->hmac);
703 /* This routine is explicitly used to relay messages to some channel.
704 Packets sent with this function we have received earlier and are
705 totally encrypted. This just sends the packet to all clients on
706 the channel. If the sender of the packet is someone on the channel
707 the message will not be sent to that client. The SILC Packet header
708 is encrypted with the session key shared between us and the client.
709 MAC is also computed before encrypting the header. Rest of the
710 packet will be untouched. */
712 void silc_server_packet_relay_to_channel(SilcServer server,
713 SilcSocketConnection sender_sock,
714 SilcChannelEntry channel,
716 SilcIdType sender_type,
717 SilcClientEntry sender_entry,
722 SilcSocketConnection sock = NULL;
723 SilcPacketContext packetdata;
724 SilcClientEntry client = NULL;
725 SilcServerEntry *routed = NULL;
726 SilcChannelClientEntry chl, chl_sender;
727 SilcUInt32 routed_count = 0;
728 SilcIDListData idata;
729 SilcHashTableList htl;
733 SILC_LOG_DEBUG(("Relaying packet to channel"));
735 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
738 /* This encrypts the packet, if needed. It will be encrypted if
739 it came from the router thus it needs to be encrypted with the
740 channel key. If the channel key does not exist, then we know we
741 don't have a single local user on the channel. */
742 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
747 /* Set the packet context pointers. */
748 packetdata.flags = 0;
749 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
750 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
751 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
752 packetdata.src_id_type = sender_type;
753 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
754 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
755 packetdata.dst_id_type = SILC_ID_CHANNEL;
757 /* If there are global users in the channel we will send the message
758 first to our router for further routing. */
759 if (server->server_type != SILC_ROUTER && !server->standalone &&
760 channel->global_users) {
761 SilcServerEntry router = server->router;
763 /* Check that the sender is not our router. */
764 if (sender_sock != (SilcSocketConnection)router->connection) {
766 /* Get data used in packet header encryption, keys and stuff. */
767 sock = (SilcSocketConnection)router->connection;
768 idata = (SilcIDListData)router;
770 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
772 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
776 data, data_len, TRUE,
781 routed = silc_calloc(silc_hash_table_count(channel->user_list),
784 /* Assure we won't route the message back to the sender's way. */
785 if (sender_entry->router)
786 routed[routed_count++] = sender_entry->router;
788 /* Send the message to clients on the channel's client list. */
789 silc_hash_table_list(channel->user_list, &htl);
790 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
791 client = chl->client;
792 if (!client || client == sender_entry)
795 /* Check whether message sending is blocked */
796 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
798 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
799 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
800 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
802 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
803 sender_entry->mode & SILC_UMODE_ROBOT)
806 /* If the client has set router it means that it is not locally
807 connected client and we will route the packet further. */
808 if (server->server_type == SILC_ROUTER && client->router) {
810 /* Check if we have sent the packet to this route already */
811 for (k = 0; k < routed_count; k++)
812 if (routed[k] == client->router)
814 if (k < routed_count)
817 /* Get data used in packet header encryption, keys and stuff. */
818 sock = (SilcSocketConnection)client->router->connection;
819 idata = (SilcIDListData)client->router;
821 /* Check if the sender socket is the same as this client's router
823 if (sender_sock && sock == sender_sock)
826 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
827 silc_id_render(client->id, SILC_ID_CLIENT),
828 sock->hostname, sock->ip));
830 /* Mark this route routed already. */
831 routed[routed_count++] = client->router;
833 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
834 /* The remote connection is router then we'll decrypt the
835 channel message and re-encrypt it with the session key shared
836 between us and the remote router. This is done because the
837 channel keys are cell specific and we have different channel
838 key than the remote router has. */
840 /* Route only once to router. Protocol prohibits sending channel
841 messages to more than one router. */
846 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
848 /* If private key mode is not set then decrypt the packet
850 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
851 channel->channel_key) {
852 unsigned char tmp[SILC_PACKET_MAX_LEN];
854 if (data_len > SILC_PACKET_MAX_LEN)
855 data_len = SILC_PACKET_MAX_LEN;
856 memcpy(tmp, data, data_len);
858 /* Decrypt the channel message (we don't check the MAC) */
859 silc_channel_message_payload_decrypt(tmp, data_len,
860 channel->channel_key, NULL);
862 /* Now re-encrypt and send it to the router */
863 silc_server_packet_send_srcdest(server, sock,
864 SILC_PACKET_CHANNEL_MESSAGE, 0,
865 sender_id, sender_type,
866 channel->id, SILC_ID_CHANNEL,
867 tmp, data_len, force_send);
869 /* Private key mode is set, we don't have the channel key, so
870 just re-encrypt the entire packet and send it to the router. */
871 silc_server_packet_send_srcdest(server, sock,
872 SILC_PACKET_CHANNEL_MESSAGE, 0,
873 sender_id, sender_type,
874 channel->id, SILC_ID_CHANNEL,
875 data, data_len, force_send);
878 /* Send the packet to normal server */
879 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
883 data, data_len, TRUE,
893 /* Get data used in packet header encryption, keys and stuff. */
894 sock = (SilcSocketConnection)client->connection;
895 idata = (SilcIDListData)client;
897 if (!sock || (sender_sock && sock == sender_sock))
900 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
901 silc_id_render(client->id, SILC_ID_CLIENT),
902 sock->hostname, sock->ip));
904 /* Send the packet */
905 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
909 data, data_len, TRUE,
913 silc_hash_table_list_reset(&htl);
915 silc_free(packetdata.src_id);
916 silc_free(packetdata.dst_id);
919 /* This function is used to send packets strictly to all local clients
920 on a particular channel. This is used for example to distribute new
921 channel key to all our locally connected clients on the channel.
922 The packets are always encrypted with the session key shared between
923 the client, this means these are not _to the channel_ but _to the client_
926 void silc_server_packet_send_local_channel(SilcServer server,
927 SilcChannelEntry channel,
929 SilcPacketFlags flags,
934 SilcChannelClientEntry chl;
935 SilcHashTableList htl;
936 SilcSocketConnection sock = NULL;
938 SILC_LOG_DEBUG(("Start"));
940 /* Send the message to clients on the channel's client list. */
941 silc_hash_table_list(channel->user_list, &htl);
942 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
943 if (chl->client && !chl->client->router) {
944 sock = (SilcSocketConnection)chl->client->connection;
946 /* Send the packet to the client */
947 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
948 SILC_ID_CLIENT, data, data_len,
952 silc_hash_table_list_reset(&htl);
955 /* Routine used to send (relay, route) private messages to some destination.
956 If the private message key does not exist then the message is re-encrypted,
957 otherwise we just pass it along. This really is not used to send new
958 private messages (as server does not send them) but to relay received
961 void silc_server_send_private_message(SilcServer server,
962 SilcSocketConnection dst_sock,
966 SilcPacketContext *packet)
968 SilcBuffer buffer = packet->buffer;
969 const SilcBufferStruct p;
971 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
972 + packet->dst_id_len + packet->padlen);
973 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
974 (const SilcBuffer)&p)) {
975 SILC_LOG_ERROR(("Cannot send packet"));
978 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
980 /* Re-encrypt and send if private messge key does not exist */
981 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
982 /* Re-encrypt packet */
983 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
985 /* Key exist so encrypt just header and send it */
986 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
987 SILC_PACKET_HEADER_LEN + packet->src_id_len +
988 packet->dst_id_len + packet->padlen);
991 /* Send the packet */
992 silc_server_packet_send_real(server, dst_sock, FALSE);
995 /* Sends current motd to client */
997 void silc_server_send_motd(SilcServer server,
998 SilcSocketConnection sock)
1000 char *motd, *motd_file = NULL;
1001 SilcUInt32 motd_len;
1004 motd_file = server->config->server_info->motd_file;
1007 motd = silc_file_readfile(motd_file, &motd_len);
1011 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1017 /* Sends error message. Error messages may or may not have any
1020 void silc_server_send_error(SilcServer server,
1021 SilcSocketConnection sock,
1022 const char *fmt, ...)
1025 unsigned char buf[4096];
1027 memset(buf, 0, sizeof(buf));
1029 vsprintf(buf, fmt, ap);
1032 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1033 buf, strlen(buf), FALSE);
1036 /* Sends notify message. If format is TRUE the variable arguments are
1037 formatted and the formatted string is sent as argument payload. If it is
1038 FALSE then each argument is sent as separate argument and their format
1039 in the argument list must be { argument data, argument length }. */
1041 void silc_server_send_notify(SilcServer server,
1042 SilcSocketConnection sock,
1044 SilcNotifyType type,
1045 SilcUInt32 argc, ...)
1052 packet = silc_notify_payload_encode(type, argc, ap);
1053 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1054 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1055 packet->data, packet->len, FALSE);
1057 /* Send to backup routers if this is being broadcasted to primary
1059 if (server->router && server->router->connection &&
1060 sock == server->router->connection && broadcast)
1061 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1062 packet->data, packet->len, FALSE, TRUE);
1064 silc_buffer_free(packet);
1068 /* Sends notify message and gets the arguments from the `args' Argument
1071 void silc_server_send_notify_args(SilcServer server,
1072 SilcSocketConnection sock,
1074 SilcNotifyType type,
1080 packet = silc_notify_payload_encode_args(type, argc, args);
1081 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1082 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1083 packet->data, packet->len, FALSE);
1084 silc_buffer_free(packet);
1087 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1088 `old_id' with the `new_id'. */
1090 void silc_server_send_notify_channel_change(SilcServer server,
1091 SilcSocketConnection sock,
1093 SilcChannelID *old_id,
1094 SilcChannelID *new_id)
1096 SilcBuffer idp1, idp2;
1098 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1099 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1101 silc_server_send_notify(server, sock, broadcast,
1102 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1103 2, idp1->data, idp1->len, idp2->data, idp2->len);
1104 silc_buffer_free(idp1);
1105 silc_buffer_free(idp2);
1108 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1109 `old_id' with the `new_id'. */
1111 void silc_server_send_notify_nick_change(SilcServer server,
1112 SilcSocketConnection sock,
1114 SilcClientID *old_id,
1115 SilcClientID *new_id,
1116 const char *nickname)
1118 SilcBuffer idp1, idp2;
1120 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1121 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1123 silc_server_send_notify(server, sock, broadcast,
1124 SILC_NOTIFY_TYPE_NICK_CHANGE,
1125 3, idp1->data, idp1->len, idp2->data, idp2->len,
1126 nickname, nickname ? strlen(nickname) : 0);
1127 silc_buffer_free(idp1);
1128 silc_buffer_free(idp2);
1131 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1132 has joined to the `channel'. */
1134 void silc_server_send_notify_join(SilcServer server,
1135 SilcSocketConnection sock,
1137 SilcChannelEntry channel,
1138 SilcClientID *client_id)
1140 SilcBuffer idp1, idp2;
1142 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1143 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1144 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1145 2, idp1->data, idp1->len,
1146 idp2->data, idp2->len);
1147 silc_buffer_free(idp1);
1148 silc_buffer_free(idp2);
1151 /* Sends LEAVE notify type. This tells that `client_id' has left the
1152 `channel'. The Notify packet is always destined to the channel. */
1154 void silc_server_send_notify_leave(SilcServer server,
1155 SilcSocketConnection sock,
1157 SilcChannelEntry channel,
1158 SilcClientID *client_id)
1162 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1163 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1164 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1165 1, idp->data, idp->len);
1166 silc_buffer_free(idp);
1169 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1170 `channel' mode to `mode. The Notify packet is always destined to
1173 void silc_server_send_notify_cmode(SilcServer server,
1174 SilcSocketConnection sock,
1176 SilcChannelEntry channel,
1177 SilcUInt32 mode_mask,
1178 void *id, SilcIdType id_type,
1179 const char *cipher, const char *hmac,
1180 const char *passphrase,
1181 SilcPublicKey founder_key)
1184 unsigned char mode[4], *key = NULL;
1185 SilcUInt32 key_len = 0;
1187 idp = silc_id_payload_encode((void *)id, id_type);
1188 SILC_PUT32_MSB(mode_mask, mode);
1190 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1192 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1193 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1194 6, idp->data, idp->len,
1196 cipher, cipher ? strlen(cipher) : 0,
1197 hmac, hmac ? strlen(hmac) : 0,
1198 passphrase, passphrase ?
1199 strlen(passphrase) : 0,
1202 silc_buffer_free(idp);
1205 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1206 `target' client's mode on `channel'. The notify packet is always
1207 destined to the channel. */
1209 void silc_server_send_notify_cumode(SilcServer server,
1210 SilcSocketConnection sock,
1212 SilcChannelEntry channel,
1213 SilcUInt32 mode_mask,
1214 void *id, SilcIdType id_type,
1215 SilcClientID *target,
1216 SilcPublicKey founder_key)
1218 SilcBuffer idp1, idp2;
1219 unsigned char mode[4], *key = NULL;
1220 SilcUInt32 key_len = 0;
1222 idp1 = silc_id_payload_encode((void *)id, id_type);
1223 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1224 SILC_PUT32_MSB(mode_mask, mode);
1226 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1228 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1230 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
1231 idp1->data, idp1->len,
1233 idp2->data, idp2->len,
1236 silc_buffer_free(idp1);
1237 silc_buffer_free(idp2);
1240 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1241 left SILC network. This function is used only between server and router
1242 traffic. This is not used to send the notify to the channel for
1243 client. The `message may be NULL. */
1245 void silc_server_send_notify_signoff(SilcServer server,
1246 SilcSocketConnection sock,
1248 SilcClientID *client_id,
1249 const char *message)
1253 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1254 silc_server_send_notify(server, sock, broadcast,
1255 SILC_NOTIFY_TYPE_SIGNOFF,
1256 message ? 2 : 1, idp->data, idp->len,
1257 message, message ? strlen(message): 0);
1258 silc_buffer_free(idp);
1261 /* Sends TOPIC_SET notify type. This tells that `id' changed
1262 the `channel's topic to `topic'. The Notify packet is always destined
1263 to the channel. This function is used to send the topic set notifies
1266 void silc_server_send_notify_topic_set(SilcServer server,
1267 SilcSocketConnection sock,
1269 SilcChannelEntry channel,
1270 void *id, SilcIdType id_type,
1275 idp = silc_id_payload_encode(id, id_type);
1276 silc_server_send_notify_dest(server, sock, broadcast,
1277 (void *)channel->id, SILC_ID_CHANNEL,
1278 SILC_NOTIFY_TYPE_TOPIC_SET,
1280 idp->data, idp->len,
1281 topic, topic ? strlen(topic) : 0);
1282 silc_buffer_free(idp);
1285 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1286 was kicked off the channel. The `comment' may indicate the reason
1287 for the kicking. This function is used only between server and router
1290 void silc_server_send_notify_kicked(SilcServer server,
1291 SilcSocketConnection sock,
1293 SilcChannelEntry channel,
1294 SilcClientID *client_id,
1295 SilcClientID *kicker,
1301 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1302 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1303 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1304 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1305 idp1->data, idp1->len,
1306 comment, comment ? strlen(comment) : 0,
1307 idp2->data, idp2->len);
1308 silc_buffer_free(idp1);
1309 silc_buffer_free(idp2);
1312 /* Send KILLED notify type. This tells that the `client_id' client was
1313 killed from the network. The `comment' may indicate the reason
1316 void silc_server_send_notify_killed(SilcServer server,
1317 SilcSocketConnection sock,
1319 SilcClientID *client_id,
1320 const char *comment,
1321 void *killer, SilcIdType killer_type)
1326 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1327 idp2 = silc_id_payload_encode(killer, killer_type);
1328 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1329 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1330 3, idp1->data, idp1->len,
1331 comment, comment ? strlen(comment) : 0,
1332 idp2->data, idp2->len);
1333 silc_buffer_free(idp1);
1334 silc_buffer_free(idp2);
1337 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1338 user mode in the SILC Network was changed. This function is used to
1339 send the packet between routers as broadcast packet. */
1341 void silc_server_send_notify_umode(SilcServer server,
1342 SilcSocketConnection sock,
1344 SilcClientID *client_id,
1345 SilcUInt32 mode_mask)
1348 unsigned char mode[4];
1350 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1351 SILC_PUT32_MSB(mode_mask, mode);
1353 silc_server_send_notify(server, sock, broadcast,
1354 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1355 idp->data, idp->len,
1357 silc_buffer_free(idp);
1360 /* Sends BAN notify type. This tells that ban has been either `add'ed
1361 or `del'eted on the `channel. This function is used to send the packet
1362 between routers as broadcast packet. */
1364 void silc_server_send_notify_ban(SilcServer server,
1365 SilcSocketConnection sock,
1367 SilcChannelEntry channel,
1368 char *add, char *del)
1372 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1373 silc_server_send_notify(server, sock, broadcast,
1374 SILC_NOTIFY_TYPE_BAN, 3,
1375 idp->data, idp->len,
1376 add, add ? strlen(add) : 0,
1377 del, del ? strlen(del) : 0);
1378 silc_buffer_free(idp);
1381 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1382 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1383 This function is used to send the packet between routers as broadcast
1386 void silc_server_send_notify_invite(SilcServer server,
1387 SilcSocketConnection sock,
1389 SilcChannelEntry channel,
1390 SilcClientID *client_id,
1391 char *add, char *del)
1393 SilcBuffer idp, idp2;
1395 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1396 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1397 silc_server_send_notify(server, sock, broadcast,
1398 SILC_NOTIFY_TYPE_INVITE, 5,
1399 idp->data, idp->len,
1400 channel->channel_name, strlen(channel->channel_name),
1401 idp2->data, idp2->len,
1402 add, add ? strlen(add) : 0,
1403 del, del ? strlen(del) : 0);
1404 silc_buffer_free(idp);
1405 silc_buffer_free(idp2);
1408 /* Sends WATCH notify type. This tells that the `client' was watched and
1409 its status in the network has changed. */
1411 void silc_server_send_notify_watch(SilcServer server,
1412 SilcSocketConnection sock,
1413 SilcClientEntry watcher,
1414 SilcClientEntry client,
1415 const char *nickname,
1416 SilcNotifyType type)
1419 unsigned char mode[4], n[2];
1421 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1422 SILC_PUT16_MSB(type, n);
1423 SILC_PUT32_MSB(client->mode, mode);
1424 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1425 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1426 4, idp->data, idp->len,
1427 nickname, nickname ? strlen(nickname) : 0,
1429 type != SILC_NOTIFY_TYPE_NONE ?
1430 n : NULL, sizeof(n));
1431 silc_buffer_free(idp);
1434 /* Sends notify message destined to specific entity. */
1436 void silc_server_send_notify_dest(SilcServer server,
1437 SilcSocketConnection sock,
1440 SilcIdType dest_id_type,
1441 SilcNotifyType type,
1442 SilcUInt32 argc, ...)
1449 packet = silc_notify_payload_encode(type, argc, ap);
1450 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1451 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1452 dest_id, dest_id_type,
1453 packet->data, packet->len, FALSE);
1454 silc_buffer_free(packet);
1458 /* Sends notify message to a channel. The notify message sent is
1459 distributed to all clients on the channel. If `route_notify' is TRUE
1460 then the notify may be routed to primary route or to some other routers.
1461 If FALSE it is assured that the notify is sent only locally. If `sender'
1462 is provided then the packet is not sent to that connection since it
1463 originally came from it. */
1465 void silc_server_send_notify_to_channel(SilcServer server,
1466 SilcSocketConnection sender,
1467 SilcChannelEntry channel,
1469 SilcNotifyType type,
1470 SilcUInt32 argc, ...)
1477 packet = silc_notify_payload_encode(type, argc, ap);
1478 silc_server_packet_send_to_channel(server, sender, channel,
1479 SILC_PACKET_NOTIFY, route_notify,
1480 packet->data, packet->len, FALSE);
1481 silc_buffer_free(packet);
1485 /* Send notify message to all channels the client has joined. It is quaranteed
1486 that the message is sent only once to a client (ie. if a client is joined
1487 on two same channel it will receive only one notify message). Also, this
1488 sends only to local clients (locally connected if we are server, and to
1489 local servers if we are router). If `sender' is provided the packet is
1490 not sent to that client at all. */
1492 void silc_server_send_notify_on_channels(SilcServer server,
1493 SilcClientEntry sender,
1494 SilcClientEntry client,
1495 SilcNotifyType type,
1496 SilcUInt32 argc, ...)
1499 SilcSocketConnection sock = NULL;
1500 SilcPacketContext packetdata;
1502 SilcClientEntry *sent_clients = NULL;
1503 SilcUInt32 sent_clients_count = 0;
1504 SilcServerEntry *routed = NULL;
1505 SilcUInt32 routed_count = 0;
1506 SilcHashTableList htl, htl2;
1507 SilcChannelEntry channel;
1508 SilcChannelClientEntry chl, chl2;
1509 SilcIDListData idata;
1511 unsigned char *data;
1512 SilcUInt32 data_len;
1513 bool force_send = FALSE;
1516 SILC_LOG_DEBUG(("Start"));
1518 if (!silc_hash_table_count(client->channels))
1522 packet = silc_notify_payload_encode(type, argc, ap);
1523 data = packet->data;
1524 data_len = packet->len;
1526 /* Set the packet context pointers. */
1527 packetdata.flags = 0;
1528 packetdata.type = SILC_PACKET_NOTIFY;
1529 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1530 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1531 packetdata.src_id_type = SILC_ID_SERVER;
1533 silc_hash_table_list(client->channels, &htl);
1534 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
1535 channel = chl->channel;
1537 /* Send the message to all clients on the channel's client list. */
1538 silc_hash_table_list(channel->user_list, &htl2);
1539 while (silc_hash_table_get(&htl2, NULL, (void **)&chl2)) {
1542 if (sender && c == sender)
1545 /* Check if we have sent the packet to this client already */
1546 for (k = 0; k < sent_clients_count; k++)
1547 if (sent_clients[k] == c)
1549 if (k < sent_clients_count)
1552 /* If we are router and if this client has router set it is not
1553 locally connected client and we will route the message to the
1554 router set in the client. */
1555 if (c && c->router && server->server_type == SILC_ROUTER) {
1556 /* Check if we have sent the packet to this route already */
1557 for (k = 0; k < routed_count; k++)
1558 if (routed[k] == c->router)
1560 if (k < routed_count)
1563 /* Get data used in packet header encryption, keys and stuff. */
1564 sock = (SilcSocketConnection)c->router->connection;
1565 idata = (SilcIDListData)c->router;
1568 SILC_LOG_DEBUG(("*****************"));
1569 SILC_LOG_DEBUG(("client->router->id %s",
1570 silc_id_render(c->router->id, SILC_ID_SERVER)));
1571 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1572 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1575 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1576 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1577 packetdata.dst_id_type = SILC_ID_SERVER;
1579 /* Send the packet */
1580 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1584 data, data_len, FALSE,
1587 silc_free(packetdata.dst_id);
1589 /* We want to make sure that the packet is routed to same router
1590 only once. Mark this route as sent route. */
1591 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1592 routed[routed_count++] = c->router;
1599 /* Send to locally connected client */
1602 /* Get data used in packet header encryption, keys and stuff. */
1603 sock = (SilcSocketConnection)c->connection;
1604 idata = (SilcIDListData)c;
1609 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1610 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1611 packetdata.dst_id_type = SILC_ID_CLIENT;
1613 /* Send the packet */
1614 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1618 data, data_len, FALSE,
1621 silc_free(packetdata.dst_id);
1623 /* Make sure that we send the notify only once per client. */
1624 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1625 (sent_clients_count + 1));
1626 sent_clients[sent_clients_count++] = c;
1629 silc_hash_table_list_reset(&htl2);
1632 silc_hash_table_list_reset(&htl);
1634 silc_free(sent_clients);
1635 silc_free(packetdata.src_id);
1636 silc_buffer_free(packet);
1640 /* Sends New ID Payload to remote end. The packet is used to distribute
1641 information about new registered clients, servers, channel etc. usually
1642 to routers so that they can keep these information up to date.
1643 If the argument `broadcast' is TRUE then the packet is sent as
1644 broadcast packet. */
1646 void silc_server_send_new_id(SilcServer server,
1647 SilcSocketConnection sock,
1649 void *id, SilcIdType id_type,
1654 SILC_LOG_DEBUG(("Sending new ID"));
1656 idp = silc_id_payload_encode(id, id_type);
1657 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1658 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1659 idp->data, idp->len, FALSE);
1660 silc_buffer_free(idp);
1663 /* Send New Channel Payload to notify about newly created channel in the
1664 SILC network. Router uses this to notify other routers in the network
1665 about new channel. This packet is broadcasted by router. */
1667 void silc_server_send_new_channel(SilcServer server,
1668 SilcSocketConnection sock,
1672 SilcUInt32 channel_id_len,
1677 SilcUInt32 name_len = strlen(channel_name);
1679 SILC_LOG_DEBUG(("Sending new channel"));
1681 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1685 /* Encode the channel payload */
1686 packet = silc_channel_payload_encode(channel_name, name_len,
1687 cid, channel_id_len, mode);
1689 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1690 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1691 packet->data, packet->len, FALSE);
1694 silc_buffer_free(packet);
1697 /* Send Channel Key payload to distribute the new channel key. Normal server
1698 sends this to router when new client joins to existing channel. Router
1699 sends this to the local server who sent the join command in case where
1700 the channel did not exist yet. Both normal and router servers uses this
1701 also to send this to locally connected clients on the channel. This
1702 must not be broadcasted packet. Routers do not send this to each other.
1703 If `sender is provided then the packet is not sent to that connection since
1704 it originally came from it. */
1706 void silc_server_send_channel_key(SilcServer server,
1707 SilcSocketConnection sender,
1708 SilcChannelEntry channel,
1709 unsigned char route)
1712 unsigned char *chid;
1715 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1717 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1721 /* Encode channel key packet */
1722 tmp_len = strlen(channel->channel_key->cipher->name);
1723 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1726 channel->channel_key->cipher->name,
1727 channel->key_len / 8, channel->key);
1728 silc_server_packet_send_to_channel(server, sender, channel,
1729 SILC_PACKET_CHANNEL_KEY,
1730 route, packet->data, packet->len,
1732 silc_buffer_free(packet);
1736 /* Generic function to send any command. The arguments must be sent already
1737 encoded into correct form in correct order. */
1739 void silc_server_send_command(SilcServer server,
1740 SilcSocketConnection sock,
1741 SilcCommand command,
1743 SilcUInt32 argc, ...)
1750 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1751 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1752 packet->data, packet->len, TRUE);
1753 silc_buffer_free(packet);
1757 /* Generic function to send any command reply. The arguments must be sent
1758 already encoded into correct form in correct order. */
1760 void silc_server_send_command_reply(SilcServer server,
1761 SilcSocketConnection sock,
1762 SilcCommand command,
1766 SilcUInt32 argc, ...)
1773 packet = silc_command_reply_payload_encode_vap(command, status, error,
1775 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1776 packet->data, packet->len, TRUE);
1777 silc_buffer_free(packet);
1781 /* Generic function to send any command reply. The arguments must be sent
1782 already encoded into correct form in correct order. */
1784 void silc_server_send_dest_command_reply(SilcServer server,
1785 SilcSocketConnection sock,
1787 SilcIdType dst_id_type,
1788 SilcCommand command,
1792 SilcUInt32 argc, ...)
1799 packet = silc_command_reply_payload_encode_vap(command, status, error,
1801 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1802 dst_id, dst_id_type, packet->data,
1804 silc_buffer_free(packet);
1808 /* Send the heartbeat packet. */
1810 void silc_server_send_heartbeat(SilcServer server,
1811 SilcSocketConnection sock)
1813 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1817 /* Generic function to relay packet we've received. This is used to relay
1818 packets to a client but generally can be used to other purposes as well. */
1820 void silc_server_relay_packet(SilcServer server,
1821 SilcSocketConnection dst_sock,
1824 SilcUInt32 sequence,
1825 SilcPacketContext *packet,
1828 const SilcBufferStruct p;
1830 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1831 + packet->dst_id_len + packet->padlen);
1832 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1833 (const SilcBuffer)&p)) {
1834 SILC_LOG_ERROR(("Cannot send packet"));
1837 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1839 /* Re-encrypt packet */
1840 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1842 /* Send the packet */
1843 silc_server_packet_send_real(server, dst_sock, force_send);
1845 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1846 + packet->dst_id_len + packet->padlen);
1849 /* Routine used to send the connection authentication packet. */
1851 void silc_server_send_connection_auth_request(SilcServer server,
1852 SilcSocketConnection sock,
1853 SilcUInt16 conn_type,
1854 SilcAuthMethod auth_meth)
1858 packet = silc_buffer_alloc(4);
1859 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1860 silc_buffer_format(packet,
1861 SILC_STR_UI_SHORT(conn_type),
1862 SILC_STR_UI_SHORT(auth_meth),
1865 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1866 0, packet->data, packet->len, FALSE);
1867 silc_buffer_free(packet);
1870 /* Purge the outgoing packet queue to the network if there is data. This
1871 function can be used to empty the packet queue. It is guaranteed that
1872 after this function returns the outgoing data queue is empty. */
1874 void silc_server_packet_queue_purge(SilcServer server,
1875 SilcSocketConnection sock)
1877 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1878 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1879 server->stat.packets_sent++;
1880 silc_packet_send(sock, TRUE);
1881 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1882 SILC_UNSET_OUTBUF_PENDING(sock);
1883 silc_buffer_clear(sock->outbuf);