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 packet, type %d", 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, server->id_type);
177 packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
178 packetdata.src_id_type = server->id_type;
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 packet, type %d", 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 SilcClientEntry *clients,
391 SilcUInt32 clients_count,
393 SilcPacketFlags flags,
399 SilcSocketConnection sock = NULL;
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 for (i = 0; i < clients_count; i++) {
412 /* If client has router set it is not locally connected client and
413 we will route the message to the router set in the client. Though,
414 send locally connected server in all cases. */
415 if (server->server_type == SILC_ROUTER && client->router &&
416 ((!route && client->router->router == server->id_entry) || route)) {
418 /* Check if we have sent the packet to this route already */
419 for (k = 0; k < routed_count; k++)
420 if (routed[k] == client->router)
422 if (k < routed_count)
425 /* Route only once to router */
426 sock = (SilcSocketConnection)client->router->connection;
427 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
433 /* Send the packet */
434 silc_server_packet_send_dest(server, sock, type, flags,
435 client->router->id, SILC_ID_SERVER,
436 data, data_len, force_send);
438 /* Mark this route routed already */
439 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
440 routed[routed_count++] = client->router;
447 /* Send to locally connected client */
448 sock = (SilcSocketConnection)client->connection;
452 silc_server_packet_send_dest(server, sock, type, flags,
453 client->id, SILC_ID_CLIENT,
454 data, data_len, force_send);
460 /* Internal routine to actually create the channel packet and send it
461 to network. This is common function in channel message sending. If
462 `channel_message' is TRUE this encrypts the message as it is strictly
463 a channel message. If FALSE normal encryption process is used. */
466 silc_server_packet_send_to_channel_real(SilcServer server,
467 SilcSocketConnection sock,
468 SilcPacketContext *packet,
474 bool channel_message,
478 const SilcBufferStruct p;
483 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
485 packet->dst_id_len));
486 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
487 packet->src_id_len + packet->dst_id_len;
489 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
491 packet->padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
493 packet->dst_id_len), block_len);
495 packet->padlen = SILC_PACKET_PADLEN(packet->truelen, block_len);
497 /* Put the data to buffer, assemble and encrypt the packet. The packet
498 is encrypted with normal session key shared with the client, unless
499 the `channel_message' is TRUE. */
500 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
501 data_len, (const SilcBuffer)&p)) {
502 SILC_LOG_ERROR(("Cannot assemble packet"));
507 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
508 SILC_PACKET_HEADER_LEN + packet->src_id_len +
509 packet->dst_id_len + packet->padlen);
511 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
513 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
516 /* Now actually send the packet */
517 silc_server_packet_send_real(server, sock, force_send);
520 /* This routine is used by the server to send packets to channel. The
521 packet sent with this function is distributed to all clients on
522 the channel. Usually this is used to send notify messages to the
523 channel, things like notify about new user joining to the channel.
524 If `route' is FALSE then the packet is sent only locally and will not
525 be routed anywhere (for router locally means cell wide). If `sender'
526 is provided then the packet is not sent to that connection since it
527 originally came from it. If `send_to_clients' is FALSE then the
528 packet is not sent clients, only servers. */
530 void silc_server_packet_send_to_channel(SilcServer server,
531 SilcSocketConnection sender,
532 SilcChannelEntry channel,
539 SilcSocketConnection sock = NULL;
540 SilcPacketContext packetdata;
541 SilcClientEntry client = NULL;
542 SilcServerEntry *routed = NULL;
543 SilcChannelClientEntry chl;
544 SilcHashTableList htl;
545 SilcIDListData idata;
546 SilcUInt32 routed_count = 0;
550 /* This doesn't send channel message packets */
551 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
553 SILC_LOG_DEBUG(("Sending packet to channel"));
555 /* Set the packet context pointers. */
556 packetdata.flags = 0;
557 packetdata.type = type;
558 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
559 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
560 packetdata.src_id_type = SILC_ID_SERVER;
561 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
562 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
563 packetdata.dst_id_type = SILC_ID_CHANNEL;
565 /* If there are global users in the channel we will send the message
566 first to our router for further routing. */
567 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
568 channel->global_users) {
569 SilcServerEntry router;
571 /* Get data used in packet header encryption, keys and stuff. */
572 router = server->router;
573 sock = (SilcSocketConnection)router->connection;
574 idata = (SilcIDListData)router;
576 if (sock != sender) {
577 SILC_LOG_DEBUG(("Sending packet to router for routing"));
579 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
583 data, data_len, FALSE,
588 routed = silc_calloc(silc_hash_table_count(channel->user_list),
591 /* Send the message to clients on the channel's client list. */
592 silc_hash_table_list(channel->user_list, &htl);
593 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
594 client = chl->client;
598 /* If client has router set it is not locally connected client and
599 we will route the message to the router set in the client. Though,
600 send locally connected server in all cases. */
601 if (server->server_type == SILC_ROUTER && client->router &&
602 ((!route && client->router->router == server->id_entry) || route)) {
604 /* Check if we have sent the packet to this route already */
605 for (k = 0; k < routed_count; k++)
606 if (routed[k] == client->router)
608 if (k < routed_count)
611 /* Get data used in packet header encryption, keys and stuff. */
612 sock = (SilcSocketConnection)client->router->connection;
613 idata = (SilcIDListData)client->router;
615 if (sender && sock == sender)
618 /* Route only once to router. Protocol prohibits sending channel
619 messages to more than one router. */
620 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
626 /* Send the packet */
627 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
631 data, data_len, FALSE,
634 /* Mark this route routed already */
635 routed[routed_count++] = client->router;
642 /* Send to locally connected client */
644 /* Get data used in packet header encryption, keys and stuff. */
645 sock = (SilcSocketConnection)client->connection;
646 idata = (SilcIDListData)client;
648 if (!sock || (sender && sock == sender))
651 /* Send the packet */
652 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
656 data, data_len, FALSE,
660 silc_hash_table_list_reset(&htl);
662 silc_free(packetdata.src_id);
663 silc_free(packetdata.dst_id);
666 /* This checks whether the relayed packet came from router. If it did
667 then we'll need to encrypt it with the channel key. This is called
668 from the silc_server_packet_relay_to_channel. */
671 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
672 SilcSocketConnection sock,
673 SilcChannelEntry channel,
675 unsigned int data_len)
677 /* If we are router and the packet came from router and private key
678 has not been set for the channel then we must encrypt the packet
679 as it was decrypted with the session key shared between us and the
680 router which sent it. This is so, because cells does not share the
682 if (server->server_type == SILC_ROUTER &&
683 sock->type == SILC_SOCKET_TYPE_ROUTER &&
684 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
685 channel->channel_key) {
686 SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
687 SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
688 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
690 if (data_len <= mac_len + iv_len) {
691 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
695 memcpy(iv, data + (data_len - iv_len), iv_len);
696 silc_channel_message_payload_encrypt(data, data_len - iv_len - mac_len,
697 data_len, iv, iv_len,
698 channel->channel_key, channel->hmac);
704 /* This routine is explicitly used to relay messages to some channel.
705 Packets sent with this function we have received earlier and are
706 totally encrypted. This just sends the packet to all clients on
707 the channel. If the sender of the packet is someone on the channel
708 the message will not be sent to that client. The SILC Packet header
709 is encrypted with the session key shared between us and the client.
710 MAC is also computed before encrypting the header. Rest of the
711 packet will be untouched. */
713 void silc_server_packet_relay_to_channel(SilcServer server,
714 SilcSocketConnection sender_sock,
715 SilcChannelEntry channel,
717 SilcIdType sender_type,
718 SilcClientEntry sender_entry,
723 SilcSocketConnection sock = NULL;
724 SilcPacketContext packetdata;
725 SilcClientEntry client = NULL;
726 SilcServerEntry *routed = NULL;
727 SilcChannelClientEntry chl, chl_sender;
728 SilcUInt32 routed_count = 0;
729 SilcIDListData idata;
730 SilcHashTableList htl;
734 SILC_LOG_DEBUG(("Relaying packet to channel"));
736 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
739 /* This encrypts the packet, if needed. It will be encrypted if
740 it came from the router thus it needs to be encrypted with the
741 channel key. If the channel key does not exist, then we know we
742 don't have a single local user on the channel. */
743 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
748 /* Set the packet context pointers. */
749 packetdata.flags = 0;
750 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
751 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
752 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
753 packetdata.src_id_type = sender_type;
754 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
755 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
756 packetdata.dst_id_type = SILC_ID_CHANNEL;
758 /* If there are global users in the channel we will send the message
759 first to our router for further routing. */
760 if (server->server_type != SILC_ROUTER && !server->standalone &&
761 channel->global_users) {
762 SilcServerEntry router = server->router;
764 /* Check that the sender is not our router. */
765 if (sender_sock != (SilcSocketConnection)router->connection) {
767 /* Get data used in packet header encryption, keys and stuff. */
768 sock = (SilcSocketConnection)router->connection;
769 idata = (SilcIDListData)router;
771 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
773 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
777 data, data_len, TRUE,
782 routed = silc_calloc(silc_hash_table_count(channel->user_list),
785 /* Assure we won't route the message back to the sender's way. */
786 if (sender_entry->router)
787 routed[routed_count++] = sender_entry->router;
789 /* Send the message to clients on the channel's client list. */
790 silc_hash_table_list(channel->user_list, &htl);
791 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
792 client = chl->client;
793 if (!client || client == sender_entry)
796 /* Check whether message sending is blocked */
797 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
799 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
800 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
801 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
803 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
804 sender_entry->mode & SILC_UMODE_ROBOT)
807 /* If the client has set router it means that it is not locally
808 connected client and we will route the packet further. */
809 if (server->server_type == SILC_ROUTER && client->router) {
811 /* Check if we have sent the packet to this route already */
812 for (k = 0; k < routed_count; k++)
813 if (routed[k] == client->router)
815 if (k < routed_count)
818 /* Get data used in packet header encryption, keys and stuff. */
819 sock = (SilcSocketConnection)client->router->connection;
820 idata = (SilcIDListData)client->router;
822 /* Check if the sender socket is the same as this client's router
824 if (sender_sock && sock == sender_sock)
827 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
828 silc_id_render(client->id, SILC_ID_CLIENT),
829 sock->hostname, sock->ip));
831 /* Mark this route routed already. */
832 routed[routed_count++] = client->router;
834 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
835 /* The remote connection is router then we'll decrypt the
836 channel message and re-encrypt it with the session key shared
837 between us and the remote router. This is done because the
838 channel keys are cell specific and we have different channel
839 key than the remote router has. */
841 /* Route only once to router. Protocol prohibits sending channel
842 messages to more than one router. */
847 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
849 /* If private key mode is not set then decrypt the packet
851 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
852 channel->channel_key) {
853 unsigned char tmp[SILC_PACKET_MAX_LEN];
855 if (data_len > SILC_PACKET_MAX_LEN)
856 data_len = SILC_PACKET_MAX_LEN;
857 memcpy(tmp, data, data_len);
859 /* Decrypt the channel message (we don't check the MAC) */
860 silc_channel_message_payload_decrypt(tmp, data_len,
861 channel->channel_key, NULL);
863 /* Now re-encrypt and send it to the router */
864 silc_server_packet_send_srcdest(server, sock,
865 SILC_PACKET_CHANNEL_MESSAGE, 0,
866 sender_id, sender_type,
867 channel->id, SILC_ID_CHANNEL,
868 tmp, data_len, force_send);
870 /* Private key mode is set, we don't have the channel key, so
871 just re-encrypt the entire packet and send it to the router. */
872 silc_server_packet_send_srcdest(server, sock,
873 SILC_PACKET_CHANNEL_MESSAGE, 0,
874 sender_id, sender_type,
875 channel->id, SILC_ID_CHANNEL,
876 data, data_len, force_send);
879 /* Send the packet to normal server */
880 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
884 data, data_len, TRUE,
894 /* Get data used in packet header encryption, keys and stuff. */
895 sock = (SilcSocketConnection)client->connection;
896 idata = (SilcIDListData)client;
898 if (!sock || (sender_sock && sock == sender_sock))
901 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
902 silc_id_render(client->id, SILC_ID_CLIENT),
903 sock->hostname, sock->ip));
905 /* Send the packet */
906 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
910 data, data_len, TRUE,
914 silc_hash_table_list_reset(&htl);
916 silc_free(packetdata.src_id);
917 silc_free(packetdata.dst_id);
920 /* This function is used to send packets strictly to all local clients
921 on a particular channel. This is used for example to distribute new
922 channel key to all our locally connected clients on the channel.
923 The packets are always encrypted with the session key shared between
924 the client, this means these are not _to the channel_ but _to the client_
927 void silc_server_packet_send_local_channel(SilcServer server,
928 SilcChannelEntry channel,
930 SilcPacketFlags flags,
935 SilcChannelClientEntry chl;
936 SilcHashTableList htl;
937 SilcSocketConnection sock = NULL;
939 SILC_LOG_DEBUG(("Start"));
941 /* Send the message to clients on the channel's client list. */
942 silc_hash_table_list(channel->user_list, &htl);
943 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
944 if (chl->client && !chl->client->router) {
945 sock = (SilcSocketConnection)chl->client->connection;
947 /* Send the packet to the client */
948 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
949 SILC_ID_CLIENT, data, data_len,
953 silc_hash_table_list_reset(&htl);
956 /* Routine used to send (relay, route) private messages to some destination.
957 If the private message key does not exist then the message is re-encrypted,
958 otherwise we just pass it along. This really is not used to send new
959 private messages (as server does not send them) but to relay received
962 void silc_server_send_private_message(SilcServer server,
963 SilcSocketConnection dst_sock,
967 SilcPacketContext *packet)
969 SilcBuffer buffer = packet->buffer;
970 const SilcBufferStruct p;
972 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
973 + packet->dst_id_len + packet->padlen);
974 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
975 (const SilcBuffer)&p)) {
976 SILC_LOG_ERROR(("Cannot send packet"));
979 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
981 /* Re-encrypt and send if private messge key does not exist */
982 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
983 /* Re-encrypt packet */
984 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
986 /* Key exist so encrypt just header and send it */
987 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
988 SILC_PACKET_HEADER_LEN + packet->src_id_len +
989 packet->dst_id_len + packet->padlen);
992 /* Send the packet */
993 silc_server_packet_send_real(server, dst_sock, FALSE);
996 /* Sends current motd to client */
998 void silc_server_send_motd(SilcServer server,
999 SilcSocketConnection sock)
1001 char *motd, *motd_file = NULL;
1002 SilcUInt32 motd_len;
1005 motd_file = server->config->server_info->motd_file;
1008 motd = silc_file_readfile(motd_file, &motd_len);
1012 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1018 /* Sends error message. Error messages may or may not have any
1021 void silc_server_send_error(SilcServer server,
1022 SilcSocketConnection sock,
1023 const char *fmt, ...)
1026 unsigned char buf[4096];
1028 memset(buf, 0, sizeof(buf));
1030 vsprintf(buf, fmt, ap);
1033 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1034 buf, strlen(buf), FALSE);
1037 /* Sends notify message. If format is TRUE the variable arguments are
1038 formatted and the formatted string is sent as argument payload. If it is
1039 FALSE then each argument is sent as separate argument and their format
1040 in the argument list must be { argument data, argument length }. */
1042 void silc_server_send_notify(SilcServer server,
1043 SilcSocketConnection sock,
1045 SilcNotifyType type,
1046 SilcUInt32 argc, ...)
1053 packet = silc_notify_payload_encode(type, argc, ap);
1054 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1055 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1056 packet->data, packet->len, FALSE);
1058 /* Send to backup routers if this is being broadcasted to primary
1060 if (server->router && server->router->connection &&
1061 sock == server->router->connection && broadcast)
1062 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1063 packet->data, packet->len, FALSE, TRUE);
1065 silc_buffer_free(packet);
1069 /* Sends notify message and gets the arguments from the `args' Argument
1072 void silc_server_send_notify_args(SilcServer server,
1073 SilcSocketConnection sock,
1075 SilcNotifyType type,
1081 packet = silc_notify_payload_encode_args(type, argc, args);
1082 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1083 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1084 packet->data, packet->len, FALSE);
1085 silc_buffer_free(packet);
1088 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1089 `old_id' with the `new_id'. */
1091 void silc_server_send_notify_channel_change(SilcServer server,
1092 SilcSocketConnection sock,
1094 SilcChannelID *old_id,
1095 SilcChannelID *new_id)
1097 SilcBuffer idp1, idp2;
1099 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1100 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1102 silc_server_send_notify(server, sock, broadcast,
1103 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1104 2, idp1->data, idp1->len, idp2->data, idp2->len);
1105 silc_buffer_free(idp1);
1106 silc_buffer_free(idp2);
1109 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1110 `old_id' with the `new_id'. */
1112 void silc_server_send_notify_nick_change(SilcServer server,
1113 SilcSocketConnection sock,
1115 SilcClientID *old_id,
1116 SilcClientID *new_id,
1117 const char *nickname)
1119 SilcBuffer idp1, idp2;
1121 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1122 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1124 silc_server_send_notify(server, sock, broadcast,
1125 SILC_NOTIFY_TYPE_NICK_CHANGE,
1126 3, idp1->data, idp1->len, idp2->data, idp2->len,
1127 nickname, nickname ? strlen(nickname) : 0);
1128 silc_buffer_free(idp1);
1129 silc_buffer_free(idp2);
1132 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1133 has joined to the `channel'. */
1135 void silc_server_send_notify_join(SilcServer server,
1136 SilcSocketConnection sock,
1138 SilcChannelEntry channel,
1139 SilcClientID *client_id)
1141 SilcBuffer idp1, idp2;
1143 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1144 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1145 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1146 2, idp1->data, idp1->len,
1147 idp2->data, idp2->len);
1148 silc_buffer_free(idp1);
1149 silc_buffer_free(idp2);
1152 /* Sends LEAVE notify type. This tells that `client_id' has left the
1153 `channel'. The Notify packet is always destined to the channel. */
1155 void silc_server_send_notify_leave(SilcServer server,
1156 SilcSocketConnection sock,
1158 SilcChannelEntry channel,
1159 SilcClientID *client_id)
1163 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1164 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1165 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1166 1, idp->data, idp->len);
1167 silc_buffer_free(idp);
1170 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1171 `channel' mode to `mode. The Notify packet is always destined to
1174 void silc_server_send_notify_cmode(SilcServer server,
1175 SilcSocketConnection sock,
1177 SilcChannelEntry channel,
1178 SilcUInt32 mode_mask,
1179 void *id, SilcIdType id_type,
1180 const char *cipher, const char *hmac,
1181 const char *passphrase,
1182 SilcPublicKey founder_key)
1185 unsigned char mode[4], *key = NULL;
1186 SilcUInt32 key_len = 0;
1188 idp = silc_id_payload_encode((void *)id, id_type);
1189 SILC_PUT32_MSB(mode_mask, mode);
1191 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1193 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1194 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1195 6, idp->data, idp->len,
1197 cipher, cipher ? strlen(cipher) : 0,
1198 hmac, hmac ? strlen(hmac) : 0,
1199 passphrase, passphrase ?
1200 strlen(passphrase) : 0,
1203 silc_buffer_free(idp);
1206 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1207 `target' client's mode on `channel'. The notify packet is always
1208 destined to the channel. */
1210 void silc_server_send_notify_cumode(SilcServer server,
1211 SilcSocketConnection sock,
1213 SilcChannelEntry channel,
1214 SilcUInt32 mode_mask,
1215 void *id, SilcIdType id_type,
1216 SilcClientID *target,
1217 SilcPublicKey founder_key)
1219 SilcBuffer idp1, idp2;
1220 unsigned char mode[4], *key = NULL;
1221 SilcUInt32 key_len = 0;
1223 idp1 = silc_id_payload_encode((void *)id, id_type);
1224 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1225 SILC_PUT32_MSB(mode_mask, mode);
1227 key = silc_pkcs_public_key_encode(founder_key, &key_len);
1229 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1231 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
1232 idp1->data, idp1->len,
1234 idp2->data, idp2->len,
1237 silc_buffer_free(idp1);
1238 silc_buffer_free(idp2);
1241 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1242 left SILC network. This function is used only between server and router
1243 traffic. This is not used to send the notify to the channel for
1244 client. The `message may be NULL. */
1246 void silc_server_send_notify_signoff(SilcServer server,
1247 SilcSocketConnection sock,
1249 SilcClientID *client_id,
1250 const char *message)
1254 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1255 silc_server_send_notify(server, sock, broadcast,
1256 SILC_NOTIFY_TYPE_SIGNOFF,
1257 message ? 2 : 1, idp->data, idp->len,
1258 message, message ? strlen(message): 0);
1259 silc_buffer_free(idp);
1262 /* Sends TOPIC_SET notify type. This tells that `id' changed
1263 the `channel's topic to `topic'. The Notify packet is always destined
1264 to the channel. This function is used to send the topic set notifies
1267 void silc_server_send_notify_topic_set(SilcServer server,
1268 SilcSocketConnection sock,
1270 SilcChannelEntry channel,
1271 void *id, SilcIdType id_type,
1276 idp = silc_id_payload_encode(id, id_type);
1277 silc_server_send_notify_dest(server, sock, broadcast,
1278 (void *)channel->id, SILC_ID_CHANNEL,
1279 SILC_NOTIFY_TYPE_TOPIC_SET,
1281 idp->data, idp->len,
1282 topic, topic ? strlen(topic) : 0);
1283 silc_buffer_free(idp);
1286 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1287 was kicked off the channel. The `comment' may indicate the reason
1288 for the kicking. This function is used only between server and router
1291 void silc_server_send_notify_kicked(SilcServer server,
1292 SilcSocketConnection sock,
1294 SilcChannelEntry channel,
1295 SilcClientID *client_id,
1296 SilcClientID *kicker,
1302 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1303 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1304 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1305 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1306 idp1->data, idp1->len,
1307 comment, comment ? strlen(comment) : 0,
1308 idp2->data, idp2->len);
1309 silc_buffer_free(idp1);
1310 silc_buffer_free(idp2);
1313 /* Send KILLED notify type. This tells that the `client_id' client was
1314 killed from the network. The `comment' may indicate the reason
1317 void silc_server_send_notify_killed(SilcServer server,
1318 SilcSocketConnection sock,
1320 SilcClientID *client_id,
1321 const char *comment,
1322 void *killer, SilcIdType killer_type)
1327 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1328 idp2 = silc_id_payload_encode(killer, killer_type);
1329 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1330 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1331 3, idp1->data, idp1->len,
1332 comment, comment ? strlen(comment) : 0,
1333 idp2->data, idp2->len);
1334 silc_buffer_free(idp1);
1335 silc_buffer_free(idp2);
1338 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1339 user mode in the SILC Network was changed. This function is used to
1340 send the packet between routers as broadcast packet. */
1342 void silc_server_send_notify_umode(SilcServer server,
1343 SilcSocketConnection sock,
1345 SilcClientID *client_id,
1346 SilcUInt32 mode_mask)
1349 unsigned char mode[4];
1351 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1352 SILC_PUT32_MSB(mode_mask, mode);
1354 silc_server_send_notify(server, sock, broadcast,
1355 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1356 idp->data, idp->len,
1358 silc_buffer_free(idp);
1361 /* Sends BAN notify type. This tells that ban has been either `add'ed
1362 or `del'eted on the `channel. This function is used to send the packet
1363 between routers as broadcast packet. */
1365 void silc_server_send_notify_ban(SilcServer server,
1366 SilcSocketConnection sock,
1368 SilcChannelEntry channel,
1369 char *add, char *del)
1373 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1374 silc_server_send_notify(server, sock, broadcast,
1375 SILC_NOTIFY_TYPE_BAN, 3,
1376 idp->data, idp->len,
1377 add, add ? strlen(add) : 0,
1378 del, del ? strlen(del) : 0);
1379 silc_buffer_free(idp);
1382 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1383 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1384 This function is used to send the packet between routers as broadcast
1387 void silc_server_send_notify_invite(SilcServer server,
1388 SilcSocketConnection sock,
1390 SilcChannelEntry channel,
1391 SilcClientID *client_id,
1392 char *add, char *del)
1394 SilcBuffer idp, idp2;
1396 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1397 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1398 silc_server_send_notify(server, sock, broadcast,
1399 SILC_NOTIFY_TYPE_INVITE, 5,
1400 idp->data, idp->len,
1401 channel->channel_name, strlen(channel->channel_name),
1402 idp2->data, idp2->len,
1403 add, add ? strlen(add) : 0,
1404 del, del ? strlen(del) : 0);
1405 silc_buffer_free(idp);
1406 silc_buffer_free(idp2);
1409 /* Sends WATCH notify type. This tells that the `client' was watched and
1410 its status in the network has changed. */
1412 void silc_server_send_notify_watch(SilcServer server,
1413 SilcSocketConnection sock,
1414 SilcClientEntry watcher,
1415 SilcClientEntry client,
1416 const char *nickname,
1417 SilcNotifyType type)
1420 unsigned char mode[4], n[2];
1422 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1423 SILC_PUT16_MSB(type, n);
1424 SILC_PUT32_MSB(client->mode, mode);
1425 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1426 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1427 4, idp->data, idp->len,
1428 nickname, nickname ? strlen(nickname) : 0,
1430 type != SILC_NOTIFY_TYPE_NONE ?
1431 n : NULL, sizeof(n));
1432 silc_buffer_free(idp);
1435 /* Sends notify message destined to specific entity. */
1437 void silc_server_send_notify_dest(SilcServer server,
1438 SilcSocketConnection sock,
1441 SilcIdType dest_id_type,
1442 SilcNotifyType type,
1443 SilcUInt32 argc, ...)
1450 packet = silc_notify_payload_encode(type, argc, ap);
1451 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1452 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1453 dest_id, dest_id_type,
1454 packet->data, packet->len, FALSE);
1455 silc_buffer_free(packet);
1459 /* Sends notify message to a channel. The notify message sent is
1460 distributed to all clients on the channel. If `route_notify' is TRUE
1461 then the notify may be routed to primary route or to some other routers.
1462 If FALSE it is assured that the notify is sent only locally. If `sender'
1463 is provided then the packet is not sent to that connection since it
1464 originally came from it. */
1466 void silc_server_send_notify_to_channel(SilcServer server,
1467 SilcSocketConnection sender,
1468 SilcChannelEntry channel,
1470 SilcNotifyType type,
1471 SilcUInt32 argc, ...)
1478 packet = silc_notify_payload_encode(type, argc, ap);
1479 silc_server_packet_send_to_channel(server, sender, channel,
1480 SILC_PACKET_NOTIFY, route_notify,
1481 packet->data, packet->len, FALSE);
1482 silc_buffer_free(packet);
1486 /* Send notify message to all channels the client has joined. It is quaranteed
1487 that the message is sent only once to a client (ie. if a client is joined
1488 on two same channel it will receive only one notify message). Also, this
1489 sends only to local clients (locally connected if we are server, and to
1490 local servers if we are router). If `sender' is provided the packet is
1491 not sent to that client at all. */
1493 void silc_server_send_notify_on_channels(SilcServer server,
1494 SilcClientEntry sender,
1495 SilcClientEntry client,
1496 SilcNotifyType type,
1497 SilcUInt32 argc, ...)
1500 SilcSocketConnection sock = NULL;
1501 SilcPacketContext packetdata;
1503 SilcClientEntry *sent_clients = NULL;
1504 SilcUInt32 sent_clients_count = 0;
1505 SilcServerEntry *routed = NULL;
1506 SilcUInt32 routed_count = 0;
1507 SilcHashTableList htl, htl2;
1508 SilcChannelEntry channel;
1509 SilcChannelClientEntry chl, chl2;
1510 SilcIDListData idata;
1512 unsigned char *data;
1513 SilcUInt32 data_len;
1514 bool force_send = FALSE;
1517 SILC_LOG_DEBUG(("Start"));
1519 if (!silc_hash_table_count(client->channels))
1523 packet = silc_notify_payload_encode(type, argc, ap);
1524 data = packet->data;
1525 data_len = packet->len;
1527 /* Set the packet context pointers. */
1528 packetdata.flags = 0;
1529 packetdata.type = SILC_PACKET_NOTIFY;
1530 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1531 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1532 packetdata.src_id_type = SILC_ID_SERVER;
1534 silc_hash_table_list(client->channels, &htl);
1535 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1536 channel = chl->channel;
1538 /* Send the message to all clients on the channel's client list. */
1539 silc_hash_table_list(channel->user_list, &htl2);
1540 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1543 if (sender && c == sender)
1546 /* Check if we have sent the packet to this client already */
1547 for (k = 0; k < sent_clients_count; k++)
1548 if (sent_clients[k] == c)
1550 if (k < sent_clients_count)
1553 /* If we are router and if this client has router set it is not
1554 locally connected client and we will route the message to the
1555 router set in the client. */
1556 if (c && c->router && server->server_type == SILC_ROUTER) {
1557 /* Check if we have sent the packet to this route already */
1558 for (k = 0; k < routed_count; k++)
1559 if (routed[k] == c->router)
1561 if (k < routed_count)
1564 /* Get data used in packet header encryption, keys and stuff. */
1565 sock = (SilcSocketConnection)c->router->connection;
1566 idata = (SilcIDListData)c->router;
1569 SILC_LOG_DEBUG(("*****************"));
1570 SILC_LOG_DEBUG(("client->router->id %s",
1571 silc_id_render(c->router->id, SILC_ID_SERVER)));
1572 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1573 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1576 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1577 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1578 packetdata.dst_id_type = SILC_ID_SERVER;
1580 /* Send the packet */
1581 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1585 data, data_len, FALSE,
1588 silc_free(packetdata.dst_id);
1590 /* We want to make sure that the packet is routed to same router
1591 only once. Mark this route as sent route. */
1592 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1593 routed[routed_count++] = c->router;
1600 /* Send to locally connected client */
1603 /* Get data used in packet header encryption, keys and stuff. */
1604 sock = (SilcSocketConnection)c->connection;
1605 idata = (SilcIDListData)c;
1610 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1611 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1612 packetdata.dst_id_type = SILC_ID_CLIENT;
1614 /* Send the packet */
1615 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1619 data, data_len, FALSE,
1622 silc_free(packetdata.dst_id);
1624 /* Make sure that we send the notify only once per client. */
1625 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1626 (sent_clients_count + 1));
1627 sent_clients[sent_clients_count++] = c;
1630 silc_hash_table_list_reset(&htl2);
1633 silc_hash_table_list_reset(&htl);
1635 silc_free(sent_clients);
1636 silc_free(packetdata.src_id);
1637 silc_buffer_free(packet);
1641 /* Sends New ID Payload to remote end. The packet is used to distribute
1642 information about new registered clients, servers, channel etc. usually
1643 to routers so that they can keep these information up to date.
1644 If the argument `broadcast' is TRUE then the packet is sent as
1645 broadcast packet. */
1647 void silc_server_send_new_id(SilcServer server,
1648 SilcSocketConnection sock,
1650 void *id, SilcIdType id_type,
1655 SILC_LOG_DEBUG(("Start"));
1657 idp = silc_id_payload_encode(id, id_type);
1658 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1659 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1660 idp->data, idp->len, FALSE);
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);
1695 silc_buffer_free(packet);
1698 /* Send Channel Key payload to distribute the new channel key. Normal server
1699 sends this to router when new client joins to existing channel. Router
1700 sends this to the local server who sent the join command in case where
1701 the channel did not exist yet. Both normal and router servers uses this
1702 also to send this to locally connected clients on the channel. This
1703 must not be broadcasted packet. Routers do not send this to each other.
1704 If `sender is provided then the packet is not sent to that connection since
1705 it originally came from it. */
1707 void silc_server_send_channel_key(SilcServer server,
1708 SilcSocketConnection sender,
1709 SilcChannelEntry channel,
1710 unsigned char route)
1713 unsigned char *chid;
1716 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1718 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1722 /* Encode channel key packet */
1723 tmp_len = strlen(channel->channel_key->cipher->name);
1724 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1727 channel->channel_key->cipher->name,
1728 channel->key_len / 8, channel->key);
1729 silc_server_packet_send_to_channel(server, sender, channel,
1730 SILC_PACKET_CHANNEL_KEY,
1731 route, packet->data, packet->len,
1733 silc_buffer_free(packet);
1737 /* Generic function to send any command. The arguments must be sent already
1738 encoded into correct form in correct order. */
1740 void silc_server_send_command(SilcServer server,
1741 SilcSocketConnection sock,
1742 SilcCommand command,
1744 SilcUInt32 argc, ...)
1751 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1752 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1753 packet->data, packet->len, TRUE);
1754 silc_buffer_free(packet);
1758 /* Generic function to send any command reply. The arguments must be sent
1759 already encoded into correct form in correct order. */
1761 void silc_server_send_command_reply(SilcServer server,
1762 SilcSocketConnection sock,
1763 SilcCommand command,
1767 SilcUInt32 argc, ...)
1774 packet = silc_command_reply_payload_encode_vap(command, status, error,
1776 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1777 packet->data, packet->len, TRUE);
1778 silc_buffer_free(packet);
1782 /* Generic function to send any command reply. The arguments must be sent
1783 already encoded into correct form in correct order. */
1785 void silc_server_send_dest_command_reply(SilcServer server,
1786 SilcSocketConnection sock,
1788 SilcIdType dst_id_type,
1789 SilcCommand command,
1793 SilcUInt32 argc, ...)
1800 packet = silc_command_reply_payload_encode_vap(command, status, error,
1802 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1803 dst_id, dst_id_type, packet->data,
1805 silc_buffer_free(packet);
1809 /* Send the heartbeat packet. */
1811 void silc_server_send_heartbeat(SilcServer server,
1812 SilcSocketConnection sock)
1814 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1818 /* Generic function to relay packet we've received. This is used to relay
1819 packets to a client but generally can be used to other purposes as well. */
1821 void silc_server_relay_packet(SilcServer server,
1822 SilcSocketConnection dst_sock,
1825 SilcUInt32 sequence,
1826 SilcPacketContext *packet,
1829 const SilcBufferStruct p;
1831 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1832 + packet->dst_id_len + packet->padlen);
1833 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1834 (const SilcBuffer)&p)) {
1835 SILC_LOG_ERROR(("Cannot send packet"));
1838 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1840 /* Re-encrypt packet */
1841 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1843 /* Send the packet */
1844 silc_server_packet_send_real(server, dst_sock, force_send);
1846 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1847 + packet->dst_id_len + packet->padlen);
1850 /* Routine used to send the connection authentication packet. */
1852 void silc_server_send_connection_auth_request(SilcServer server,
1853 SilcSocketConnection sock,
1854 SilcUInt16 conn_type,
1855 SilcAuthMethod auth_meth)
1859 packet = silc_buffer_alloc(4);
1860 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1861 silc_buffer_format(packet,
1862 SILC_STR_UI_SHORT(conn_type),
1863 SILC_STR_UI_SHORT(auth_meth),
1866 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1867 0, packet->data, packet->len, FALSE);
1868 silc_buffer_free(packet);
1871 /* Purge the outgoing packet queue to the network if there is data. This
1872 function can be used to empty the packet queue. It is guaranteed that
1873 after this function returns the outgoing data queue is empty. */
1875 void silc_server_packet_queue_purge(SilcServer server,
1876 SilcSocketConnection sock)
1878 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1879 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1880 server->stat.packets_sent++;
1881 silc_packet_send(sock, TRUE);
1882 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1883 SILC_UNSET_OUTBUF_PENDING(sock);
1884 silc_buffer_clear(sock->outbuf);