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; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 * Server packet routines to send packets.
24 #include "serverincludes.h"
25 #include "server_internal.h"
27 /* Routine that sends packet or marks packet to be sent. This is used
28 directly only in special cases. Normal cases should use
29 silc_server_packet_send. Returns < 0 error. */
31 int silc_server_packet_send_real(SilcServer server,
32 SilcSocketConnection sock,
37 /* If disconnecting, ignore the data */
38 if (SILC_IS_DISCONNECTING(sock))
41 /* If rekey protocol is active we must assure that all packets are
42 sent through packet queue. */
43 if (SILC_SERVER_IS_REKEY(sock))
46 /* If outbound data is already pending do not force send */
47 if (SILC_IS_OUTBUF_PENDING(sock))
51 ret = silc_packet_send(sock, force_send);
53 server->stat.packets_sent++;
57 /* Mark that there is some outgoing data available for this connection.
58 This call sets the connection both for input and output (the input
59 is set always and this call keeps the input setting, actually).
60 Actual data sending is performed by silc_server_packet_process. */
61 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
63 /* Mark to socket that data is pending in outgoing buffer. This flag
64 is needed if new data is added to the buffer before the earlier
65 put data is sent to the network. */
66 SILC_SET_OUTBUF_PENDING(sock);
71 /* Assembles a new packet to be sent out to network. This doesn't actually
72 send the packet but creates the packet and fills the outgoing data
73 buffer and marks the packet ready to be sent to network. However, If
74 argument force_send is TRUE the packet is sent immediately and not put
75 to queue. Normal case is that the packet is not sent immediately. */
77 void silc_server_packet_send(SilcServer server,
78 SilcSocketConnection sock,
80 SilcPacketFlags flags,
86 SilcIdType dst_id_type = SILC_ID_NONE;
92 idata = (SilcIDListData)sock->user_data;
94 /* If disconnecting, ignore the data */
95 if (SILC_IS_DISCONNECTING(sock))
98 /* If entry is disabled do not sent anything. Allow hearbeat and
100 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED &&
101 type != SILC_PACKET_HEARTBEAT && type != SILC_PACKET_REKEY &&
102 type != SILC_PACKET_REKEY_DONE) ||
103 sock->user_data == server->id_entry) {
104 SILC_LOG_DEBUG(("Connection is disabled"));
108 /* Get data used in the packet sending, keys and stuff */
110 case SILC_SOCKET_TYPE_CLIENT:
111 if (sock->user_data) {
112 dst_id = ((SilcClientEntry)sock->user_data)->id;
113 dst_id_type = SILC_ID_CLIENT;
116 case SILC_SOCKET_TYPE_SERVER:
117 case SILC_SOCKET_TYPE_ROUTER:
118 if (sock->user_data) {
119 dst_id = ((SilcServerEntry)sock->user_data)->id;
120 dst_id_type = SILC_ID_SERVER;
127 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
128 dst_id_type, data, data_len, force_send);
131 /* Assembles a new packet to be sent out to network. This doesn't actually
132 send the packet but creates the packet and fills the outgoing data
133 buffer and marks the packet ready to be sent to network. However, If
134 argument force_send is TRUE the packet is sent immediately and not put
135 to queue. Normal case is that the packet is not sent immediately.
136 Destination information is sent as argument for this function. */
138 void silc_server_packet_send_dest(SilcServer server,
139 SilcSocketConnection sock,
141 SilcPacketFlags flags,
143 SilcIdType dst_id_type,
148 SilcPacketContext packetdata;
149 const SilcBufferStruct packet;
150 SilcIDListData idata;
151 SilcCipher cipher = NULL;
152 SilcHmac hmac = NULL;
153 SilcUInt32 sequence = 0;
154 unsigned char *dst_id_data = NULL;
155 SilcUInt32 dst_id_len = 0;
158 /* If disconnecting, ignore the data */
159 if (!sock || SILC_IS_DISCONNECTING(sock))
162 idata = (SilcIDListData)sock->user_data;
164 /* If entry is disabled do not sent anything. */
165 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED) ||
166 sock->user_data == server->id_entry) {
167 SILC_LOG_DEBUG(("Connection is disabled"));
171 SILC_LOG_DEBUG(("Sending %s packet (forced=%s)",
172 silc_get_packet_name(type), force_send ? "yes" : "no"));
175 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
176 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
180 cipher = idata->send_key;
181 hmac = idata->hmac_send;
182 sequence = idata->psn_send++;
184 block_len = silc_cipher_get_block_len(cipher);
186 /* Check for mandatory rekey */
187 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
188 silc_schedule_task_add(server->schedule, sock->sock,
189 silc_server_rekey_callback, sock, 0, 1,
190 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
193 /* Set the packet context pointers */
194 packetdata.type = type;
195 packetdata.flags = flags;
196 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
197 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
198 packetdata.src_id_type = SILC_ID_SERVER;
199 packetdata.dst_id = dst_id_data;
200 packetdata.dst_id_len = dst_id_len;
201 packetdata.dst_id_type = dst_id_type;
202 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
203 packetdata.src_id_len +
204 packetdata.dst_id_len));
205 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
206 packetdata.src_id_len + dst_id_len;
207 if (type == SILC_PACKET_CONNECTION_AUTH)
208 SILC_PACKET_PADLEN_MAX(packetdata.truelen, block_len, packetdata.padlen);
210 SILC_PACKET_PADLEN(packetdata.truelen, block_len, packetdata.padlen);
212 /* Create the outgoing packet */
213 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock,
214 data, data_len, (const SilcBuffer)&packet)) {
215 SILC_LOG_ERROR(("Cannot assemble packet"));
219 /* Encrypt the packet */
220 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
222 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
223 packet.data, packet.len);
225 /* Now actually send the packet */
226 silc_server_packet_send_real(server, sock, force_send);
229 silc_free(packetdata.src_id);
230 silc_free(packetdata.dst_id);
233 /* Assembles a new packet to be sent out to network. This doesn't actually
234 send the packet but creates the packet and fills the outgoing data
235 buffer and marks the packet ready to be sent to network. However, If
236 argument force_send is TRUE the packet is sent immediately and not put
237 to queue. Normal case is that the packet is not sent immediately.
238 The source and destination information is sent as argument for this
241 void silc_server_packet_send_srcdest(SilcServer server,
242 SilcSocketConnection sock,
244 SilcPacketFlags flags,
246 SilcIdType src_id_type,
248 SilcIdType dst_id_type,
253 SilcPacketContext packetdata;
254 const SilcBufferStruct packet;
255 SilcIDListData idata;
256 SilcCipher cipher = NULL;
257 SilcHmac hmac = NULL;
258 SilcUInt32 sequence = 0;
259 unsigned char *dst_id_data = NULL;
260 SilcUInt32 dst_id_len = 0;
261 unsigned char *src_id_data = NULL;
262 SilcUInt32 src_id_len = 0;
265 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
270 /* Get data used in the packet sending, keys and stuff */
271 idata = (SilcIDListData)sock->user_data;
273 /* If entry is disabled do not sent anything. */
274 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED) ||
275 sock->user_data == server->id_entry) {
276 SILC_LOG_DEBUG(("Connection is disabled"));
281 cipher = idata->send_key;
282 hmac = idata->hmac_send;
283 sequence = idata->psn_send++;
284 block_len = silc_cipher_get_block_len(cipher);
286 /* Check for mandatory rekey */
287 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
288 silc_schedule_task_add(server->schedule, sock->sock,
289 silc_server_rekey_callback, sock, 0, 1,
290 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
294 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
295 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
299 src_id_data = silc_id_id2str(src_id, src_id_type);
300 src_id_len = silc_id_get_len(src_id, src_id_type);
303 /* Set the packet context pointers */
304 packetdata.type = type;
305 packetdata.flags = flags;
306 packetdata.src_id = src_id_data;
307 packetdata.src_id_len = src_id_len;
308 packetdata.src_id_type = src_id_type;
309 packetdata.dst_id = dst_id_data;
310 packetdata.dst_id_len = dst_id_len;
311 packetdata.dst_id_type = dst_id_type;
312 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
313 packetdata.src_id_len +
315 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
316 packetdata.src_id_len + dst_id_len;
317 SILC_PACKET_PADLEN(packetdata.truelen, block_len, packetdata.padlen);
319 /* Create the outgoing packet */
320 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock, data,
321 data_len, (const SilcBuffer)&packet)) {
322 SILC_LOG_ERROR(("Cannot assemble packe"));
326 /* Encrypt the packet */
327 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
329 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
330 packet.data, packet.len);
332 /* Now actually send the packet */
333 silc_server_packet_send_real(server, sock, force_send);
336 silc_free(packetdata.src_id);
337 silc_free(packetdata.dst_id);
340 /* Broadcast received packet to our primary route. This function is used
341 by router to further route received broadcast packet. It is expected
342 that the broadcast flag from the packet is checked before calling this
343 function. This does not test or set the broadcast flag. */
345 void silc_server_packet_broadcast(SilcServer server,
346 SilcSocketConnection sock,
347 SilcPacketContext *packet)
349 SilcBuffer buffer = packet->buffer;
350 SilcIDListData idata;
356 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
358 /* If the packet is originated from our primary route we are
359 not allowed to send the packet. */
360 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
361 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
362 const SilcBufferStruct p;
364 idata = (SilcIDListData)sock->user_data;
366 silc_buffer_push(buffer, buffer->data - buffer->head);
367 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
368 (const SilcBuffer)&p)) {
369 SILC_LOG_ERROR(("Cannot send packet"));
373 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
374 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
375 (SilcBuffer)&p, p.len);
377 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
378 p.len), p.data, p.len);
380 /* Now actually send the packet */
381 silc_server_packet_send_real(server, sock, TRUE);
384 /* Check for mandatory rekey */
385 if (idata->psn_send == SILC_SERVER_REKEY_THRESHOLD)
386 silc_schedule_task_add(server->schedule, sock->sock,
387 silc_server_rekey_callback, sock, 0, 1,
388 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
392 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
393 "original sender of this packet"));
397 /* Routes received packet to `sock'. This is used to route the packets that
398 router receives but are not destined to it. */
400 void silc_server_packet_route(SilcServer server,
401 SilcSocketConnection sock,
402 SilcPacketContext *packet)
404 SilcBuffer buffer = packet->buffer;
405 const SilcBufferStruct p;
406 SilcIDListData idata;
408 SILC_LOG_DEBUG(("Routing received packet"));
410 idata = (SilcIDListData)sock->user_data;
412 silc_buffer_push(buffer, buffer->data - buffer->head);
413 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
414 (const SilcBuffer)&p)) {
415 SILC_LOG_ERROR(("Cannot send packet"));
418 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
419 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
420 (SilcBuffer)&p, p.len);
422 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
423 p.len), p.data, p.len);
425 /* Now actually send the packet */
426 silc_server_packet_send_real(server, sock, TRUE);
428 /* Check for mandatory rekey */
429 if (idata->psn_send == SILC_SERVER_REKEY_THRESHOLD)
430 silc_schedule_task_add(server->schedule, sock->sock,
431 silc_server_rekey_callback, sock, 0, 1,
432 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
435 /* This routine can be used to send a packet to table of clients provided
436 in `clients'. If `route' is FALSE the packet is routed only to local
437 clients (for server locally connected, and for router local cell). */
439 void silc_server_packet_send_clients(SilcServer server,
440 SilcHashTable clients,
442 SilcPacketFlags flags,
448 SilcSocketConnection sock = NULL;
449 SilcHashTableList htl;
450 SilcClientEntry client = NULL;
451 SilcServerEntry *routed = NULL;
452 SilcUInt32 routed_count = 0;
456 if (!silc_hash_table_count(clients))
459 SILC_LOG_DEBUG(("Sending packet to %d clients",
460 silc_hash_table_count(clients)));
462 /* Send to all clients in table */
463 silc_hash_table_list(clients, &htl);
464 while (silc_hash_table_get(&htl, NULL, (void **)&client)) {
465 /* If client has router set it is not locally connected client and
466 we will route the message to the router set in the client. Though,
467 send locally connected server in all cases. */
468 if (server->server_type == SILC_ROUTER && client->router &&
469 ((!route && client->router->router == server->id_entry) || route)) {
471 /* Check if we have sent the packet to this route already */
472 for (k = 0; k < routed_count; k++)
473 if (routed[k] == client->router)
475 if (k < routed_count)
478 /* Route only once to router */
479 sock = (SilcSocketConnection)client->router->connection;
480 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
486 /* Send the packet */
487 silc_server_packet_send_dest(server, sock, type, flags,
488 client->router->id, SILC_ID_SERVER,
489 data, data_len, force_send);
491 /* Mark this route routed already */
492 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
493 routed[routed_count++] = client->router;
500 /* Send to locally connected client */
501 sock = (SilcSocketConnection)client->connection;
505 silc_server_packet_send_dest(server, sock, type, flags,
506 client->id, SILC_ID_CLIENT,
507 data, data_len, force_send);
509 silc_hash_table_list_reset(&htl);
513 /* Internal routine to actually create the channel packet and send it
514 to network. This is common function in channel message sending. If
515 `channel_message' is TRUE this encrypts the message as it is strictly
516 a channel message. If FALSE normal encryption process is used. */
519 silc_server_packet_send_to_channel_real(SilcServer server,
520 SilcSocketConnection sock,
521 SilcPacketContext *packet,
527 bool channel_message,
531 const SilcBufferStruct p;
536 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
538 packet->dst_id_len));
539 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
540 packet->src_id_len + packet->dst_id_len;
542 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
544 SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
546 packet->dst_id_len), block_len, packet->padlen);
548 SILC_PACKET_PADLEN(packet->truelen, block_len, packet->padlen);
550 /* Put the data to buffer, assemble and encrypt the packet. The packet
551 is encrypted with normal session key shared with the client, unless
552 the `channel_message' is TRUE. */
553 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
554 data_len, (const SilcBuffer)&p)) {
555 SILC_LOG_ERROR(("Cannot assemble packet"));
560 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
561 SILC_PACKET_HEADER_LEN + packet->src_id_len +
562 packet->dst_id_len + packet->padlen);
564 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
566 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
569 /* Now actually send the packet */
570 silc_server_packet_send_real(server, sock, force_send);
573 /* This routine is used by the server to send packets to channel. The
574 packet sent with this function is distributed to all clients on
575 the channel. Usually this is used to send notify messages to the
576 channel, things like notify about new user joining to the channel.
577 If `route' is FALSE then the packet is sent only locally and will not
578 be routed anywhere (for router locally means cell wide). If `sender'
579 is provided then the packet is not sent to that connection since it
580 originally came from it. If `send_to_clients' is FALSE then the
581 packet is not sent clients, only servers. */
583 void silc_server_packet_send_to_channel(SilcServer server,
584 SilcSocketConnection sender,
585 SilcChannelEntry channel,
592 SilcSocketConnection sock = NULL;
593 SilcPacketContext packetdata;
594 SilcClientEntry client = NULL;
595 SilcServerEntry *routed = NULL;
596 SilcChannelClientEntry chl;
597 SilcHashTableList htl;
598 SilcIDListData idata;
599 SilcUInt32 routed_count = 0;
603 /* This doesn't send channel message packets */
604 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
606 /* Set the packet context pointers. */
607 packetdata.flags = 0;
608 packetdata.type = type;
609 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
610 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
611 packetdata.src_id_type = SILC_ID_SERVER;
612 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
613 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
614 packetdata.dst_id_type = SILC_ID_CHANNEL;
616 /* If there are global users in the channel we will send the message
617 first to our router for further routing. */
618 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
619 channel->global_users) {
620 SilcServerEntry router;
622 /* Get data used in packet header encryption, keys and stuff. */
623 router = server->router;
624 sock = (SilcSocketConnection)router->connection;
625 idata = (SilcIDListData)router;
627 if (sock != sender) {
628 SILC_LOG_DEBUG(("Sending packet to router for routing"));
629 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
633 data, data_len, FALSE,
638 if (!silc_hash_table_count(channel->user_list)) {
639 SILC_LOG_DEBUG(("Channel %s is empty", channel->channel_name));
643 SILC_LOG_DEBUG(("Sending %s to channel %s",
644 silc_get_packet_name(type), channel->channel_name));
646 routed = silc_calloc(silc_hash_table_count(channel->user_list),
649 /* Send the message to clients on the channel's client list. */
650 silc_hash_table_list(channel->user_list, &htl);
651 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
652 client = chl->client;
656 /* If client has router set it is not locally connected client and
657 we will route the message to the router set in the client. Though,
658 send locally connected server in all cases. */
659 if (server->server_type == SILC_ROUTER && client->router &&
660 ((!route && client->router->router == server->id_entry) || route)) {
662 /* Check if we have sent the packet to this route already */
663 for (k = 0; k < routed_count; k++)
664 if (routed[k] == client->router)
666 if (k < routed_count)
669 /* Get data used in packet header encryption, keys and stuff. */
670 sock = (SilcSocketConnection)client->router->connection;
671 idata = (SilcIDListData)client->router;
673 if (sender && sock == sender)
676 /* Route only once to router. Protocol prohibits sending channel
677 messages to more than one router. */
678 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
684 SILC_LOG_DEBUG(("Sending packet to client %s",
685 client->nickname ? client->nickname :
686 (unsigned char *)""));
688 /* Send the packet */
689 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
693 data, data_len, FALSE,
696 /* Mark this route routed already */
697 routed[routed_count++] = client->router;
704 /* Send to locally connected client */
706 /* Get data used in packet header encryption, keys and stuff. */
707 sock = (SilcSocketConnection)client->connection;
708 idata = (SilcIDListData)client;
710 if (!sock || (sender && sock == sender))
713 SILC_LOG_DEBUG(("Sending packet to client %s",
714 client->nickname ? client->nickname :
715 (unsigned char *)""));
717 /* Send the packet */
718 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
722 data, data_len, FALSE,
725 silc_hash_table_list_reset(&htl);
729 silc_free(packetdata.src_id);
730 silc_free(packetdata.dst_id);
733 /* This checks whether the relayed packet came from router. If it did
734 then we'll need to encrypt it with the channel key. This is called
735 from the silc_server_packet_relay_to_channel. */
738 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
739 SilcSocketConnection sock,
740 SilcChannelEntry channel,
742 unsigned int data_len)
744 /* If we are router and the packet came from router and private key
745 has not been set for the channel then we must encrypt the packet
746 as it was decrypted with the session key shared between us and the
747 router which sent it. This is so, because cells does not share the
749 if (server->server_type == SILC_ROUTER &&
750 sock->type == SILC_SOCKET_TYPE_ROUTER &&
751 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
752 channel->channel_key) {
753 SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
754 SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
755 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
757 if (data_len <= mac_len + iv_len) {
758 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
762 memcpy(iv, data + (data_len - iv_len - mac_len), iv_len);
763 silc_message_payload_encrypt(data, data_len - iv_len, data_len,
764 iv, iv_len, channel->channel_key,
771 /* This routine is explicitly used to relay messages to some channel.
772 Packets sent with this function we have received earlier and are
773 totally encrypted. This just sends the packet to all clients on
774 the channel. If the sender of the packet is someone on the channel
775 the message will not be sent to that client. The SILC Packet header
776 is encrypted with the session key shared between us and the client.
777 MAC is also computed before encrypting the header. Rest of the
778 packet will be untouched. */
780 void silc_server_packet_relay_to_channel(SilcServer server,
781 SilcSocketConnection sender_sock,
782 SilcChannelEntry channel,
784 SilcIdType sender_type,
785 SilcClientEntry sender_entry,
790 SilcSocketConnection sock = NULL;
791 SilcPacketContext packetdata;
792 SilcClientEntry client = NULL;
793 SilcServerEntry *routed = NULL;
794 SilcChannelClientEntry chl, chl_sender;
795 SilcUInt32 routed_count = 0;
796 SilcIDListData idata;
797 SilcHashTableList htl;
801 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
804 SILC_LOG_DEBUG(("Relaying packet to channel %s", channel->channel_name));
806 /* This encrypts the packet, if needed. It will be encrypted if
807 it came from the router thus it needs to be encrypted with the
808 channel key. If the channel key does not exist, then we know we
809 don't have a single local user on the channel. */
810 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
815 /* Set the packet context pointers. */
816 packetdata.flags = 0;
817 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
818 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
819 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
820 packetdata.src_id_type = sender_type;
821 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
822 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
823 packetdata.dst_id_type = SILC_ID_CHANNEL;
825 /* If there are global users in the channel we will send the message
826 first to our router for further routing. */
827 if (server->server_type != SILC_ROUTER && !server->standalone &&
828 channel->global_users) {
829 SilcServerEntry router = server->router;
831 /* Check that the sender is not our router. */
832 if (sender_sock != (SilcSocketConnection)router->connection) {
834 /* Get data used in packet header encryption, keys and stuff. */
835 sock = (SilcSocketConnection)router->connection;
836 idata = (SilcIDListData)router;
838 SILC_LOG_DEBUG(("Sending message to router for routing"));
840 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
844 data, data_len, TRUE,
849 routed = silc_calloc(silc_hash_table_count(channel->user_list),
852 /* Assure we won't route the message back to the sender's way. */
853 if (sender_entry->router)
854 routed[routed_count++] = sender_entry->router;
856 /* Send the message to clients on the channel's client list. */
857 silc_hash_table_list(channel->user_list, &htl);
858 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
859 client = chl->client;
860 if (!client || client == sender_entry)
863 /* Check whether message sending is blocked */
864 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
866 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
867 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
868 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
870 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
871 sender_entry->mode & SILC_UMODE_ROBOT)
874 /* If the client has set router it means that it is not locally
875 connected client and we will route the packet further. */
876 if (server->server_type == SILC_ROUTER && client->router) {
878 /* Check if we have sent the packet to this route already */
879 for (k = 0; k < routed_count; k++)
880 if (routed[k] == client->router)
882 if (k < routed_count)
885 /* Get data used in packet header encryption, keys and stuff. */
886 sock = (SilcSocketConnection)client->router->connection;
887 idata = (SilcIDListData)client->router;
889 /* Check if the sender socket is the same as this client's router
891 if (sender_sock && sock == sender_sock)
894 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
895 silc_id_render(client->id, SILC_ID_CLIENT),
896 sock->hostname, sock->ip));
898 /* Mark this route routed already. */
899 routed[routed_count++] = client->router;
901 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
902 /* The remote connection is router then we'll decrypt the
903 channel message and re-encrypt it with the session key shared
904 between us and the remote router. This is done because the
905 channel keys are cell specific and we have different channel
906 key than the remote router has. */
908 /* Route only once to router. Protocol prohibits sending channel
909 messages to more than one router. */
914 /* If we are backup router and remote is our primary router and
915 we are currently doing backup resuming protocol we must not
916 re-encrypt message with session key. */
917 if (server->backup_router && SILC_SERVER_IS_BACKUP(sock) &&
918 SILC_PRIMARY_ROUTE(server) == sock) {
919 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
923 data, data_len, TRUE,
928 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
930 /* If private key mode is not set then decrypt the packet
932 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
933 channel->channel_key) {
934 unsigned char tmp[SILC_PACKET_MAX_LEN];
936 if (data_len > SILC_PACKET_MAX_LEN)
937 data_len = SILC_PACKET_MAX_LEN;
938 memcpy(tmp, data, data_len);
940 /* Decrypt the channel message (we don't check the MAC) */
941 silc_message_payload_decrypt(tmp, data_len, FALSE, FALSE,
942 channel->channel_key,
943 channel->hmac, FALSE);
945 /* Now re-encrypt and send it to the router */
946 silc_server_packet_send_srcdest(server, sock,
947 SILC_PACKET_CHANNEL_MESSAGE, 0,
948 sender_id, sender_type,
949 channel->id, SILC_ID_CHANNEL,
950 tmp, data_len, force_send);
952 /* Private key mode is set, we don't have the channel key, so
953 just re-encrypt the entire packet and send it to the router. */
954 silc_server_packet_send_srcdest(server, sock,
955 SILC_PACKET_CHANNEL_MESSAGE, 0,
956 sender_id, sender_type,
957 channel->id, SILC_ID_CHANNEL,
958 data, data_len, force_send);
961 /* Send the packet to normal server */
962 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
966 data, data_len, TRUE,
976 /* Get data used in packet header encryption, keys and stuff. */
977 sock = (SilcSocketConnection)client->connection;
978 idata = (SilcIDListData)client;
980 if (!sock || (sender_sock && sock == sender_sock))
983 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
984 silc_id_render(client->id, SILC_ID_CLIENT),
985 sock->hostname, sock->ip));
987 /* Send the packet */
988 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
992 data, data_len, TRUE,
996 silc_hash_table_list_reset(&htl);
998 silc_free(packetdata.src_id);
999 silc_free(packetdata.dst_id);
1002 /* This function is used to send packets strictly to all local clients
1003 on a particular channel. This is used for example to distribute new
1004 channel key to all our locally connected clients on the channel.
1005 The packets are always encrypted with the session key shared between
1006 the client, this means these are not _to the channel_ but _to the client_
1009 void silc_server_packet_send_local_channel(SilcServer server,
1010 SilcChannelEntry channel,
1011 SilcPacketType type,
1012 SilcPacketFlags flags,
1013 unsigned char *data,
1014 SilcUInt32 data_len,
1017 SilcChannelClientEntry chl;
1018 SilcHashTableList htl;
1019 SilcSocketConnection sock = NULL;
1021 SILC_LOG_DEBUG(("Send packet to local clients on channel %s",
1022 channel->channel_name));
1024 /* Send the message to clients on the channel's client list. */
1025 silc_hash_table_list(channel->user_list, &htl);
1026 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
1027 if (chl->client && SILC_IS_LOCAL(chl->client)) {
1028 sock = chl->client->connection;
1030 /* Send the packet to the client */
1031 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
1032 SILC_ID_CLIENT, data, data_len,
1036 silc_hash_table_list_reset(&htl);
1039 /* Routine used to send (relay, route) private messages to some destination.
1040 If the private message key does not exist then the message is re-encrypted,
1041 otherwise we just pass it along. This really is not used to send new
1042 private messages (as server does not send them) but to relay received
1043 private messages. */
1045 void silc_server_send_private_message(SilcServer server,
1046 SilcSocketConnection dst_sock,
1049 SilcUInt32 sequence,
1050 SilcPacketContext *packet)
1052 SilcBuffer buffer = packet->buffer;
1053 const SilcBufferStruct p;
1055 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1056 + packet->dst_id_len + packet->padlen);
1057 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
1058 (const SilcBuffer)&p)) {
1059 SILC_LOG_ERROR(("Cannot send packet"));
1062 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1064 /* Re-encrypt and send if private messge key does not exist */
1065 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
1066 /* Re-encrypt packet */
1067 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
1069 /* Key exist so encrypt just header and send it */
1070 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
1071 SILC_PACKET_HEADER_LEN + packet->src_id_len +
1072 packet->dst_id_len + packet->padlen);
1075 /* Send the packet */
1076 silc_server_packet_send_real(server, dst_sock, FALSE);
1078 /* Check for mandatory rekey */
1079 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
1080 silc_schedule_task_add(server->schedule, dst_sock->sock,
1081 silc_server_rekey_callback, dst_sock, 0, 1,
1082 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1085 /* Sends current motd to client */
1087 void silc_server_send_motd(SilcServer server,
1088 SilcSocketConnection sock)
1090 char *motd, *motd_file = NULL;
1091 SilcUInt32 motd_len;
1094 motd_file = server->config->server_info->motd_file;
1097 motd = silc_file_readfile(motd_file, &motd_len);
1102 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1108 /* Sends error message. Error messages may or may not have any
1111 void silc_server_send_error(SilcServer server,
1112 SilcSocketConnection sock,
1113 const char *fmt, ...)
1116 unsigned char buf[4096];
1118 memset(buf, 0, sizeof(buf));
1120 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1123 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1124 buf, strlen(buf), FALSE);
1127 /* Sends notify message. If format is TRUE the variable arguments are
1128 formatted and the formatted string is sent as argument payload. If it is
1129 FALSE then each argument is sent as separate argument and their format
1130 in the argument list must be { argument data, argument length }. */
1132 void silc_server_send_notify(SilcServer server,
1133 SilcSocketConnection sock,
1135 SilcNotifyType type,
1136 SilcUInt32 argc, ...)
1143 packet = silc_notify_payload_encode(type, argc, ap);
1144 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1145 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1146 packet->data, packet->len, FALSE);
1148 /* Send to backup routers if this is being broadcasted to primary
1149 router. The silc_server_backup_send checks further whether to
1150 actually send it or not. */
1151 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1152 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1153 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1154 packet->data, packet->len, FALSE, TRUE);
1156 silc_buffer_free(packet);
1160 /* Sends notify message and gets the arguments from the `args' Argument
1163 void silc_server_send_notify_args(SilcServer server,
1164 SilcSocketConnection sock,
1166 SilcNotifyType type,
1172 packet = silc_notify_payload_encode_args(type, argc, args);
1173 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1174 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1175 packet->data, packet->len, FALSE);
1176 silc_buffer_free(packet);
1179 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1180 `old_id' with the `new_id'. */
1182 void silc_server_send_notify_channel_change(SilcServer server,
1183 SilcSocketConnection sock,
1185 SilcChannelID *old_id,
1186 SilcChannelID *new_id)
1188 SilcBuffer idp1, idp2;
1190 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1191 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1193 silc_server_send_notify(server, sock, broadcast,
1194 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1195 2, idp1->data, idp1->len, idp2->data, idp2->len);
1196 silc_buffer_free(idp1);
1197 silc_buffer_free(idp2);
1200 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1201 `old_id' with the `new_id'. */
1203 void silc_server_send_notify_nick_change(SilcServer server,
1204 SilcSocketConnection sock,
1206 SilcClientID *old_id,
1207 SilcClientID *new_id,
1208 const char *nickname)
1210 SilcBuffer idp1, idp2;
1212 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1213 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1215 silc_server_send_notify(server, sock, broadcast,
1216 SILC_NOTIFY_TYPE_NICK_CHANGE,
1217 3, idp1->data, idp1->len, idp2->data, idp2->len,
1218 nickname, nickname ? strlen(nickname) : 0);
1219 silc_buffer_free(idp1);
1220 silc_buffer_free(idp2);
1223 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1224 has joined to the `channel'. */
1226 void silc_server_send_notify_join(SilcServer server,
1227 SilcSocketConnection sock,
1229 SilcChannelEntry channel,
1230 SilcClientID *client_id)
1232 SilcBuffer idp1, idp2;
1234 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1235 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1236 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1237 2, idp1->data, idp1->len,
1238 idp2->data, idp2->len);
1239 silc_buffer_free(idp1);
1240 silc_buffer_free(idp2);
1243 /* Sends LEAVE notify type. This tells that `client_id' has left the
1244 `channel'. The Notify packet is always destined to the channel. */
1246 void silc_server_send_notify_leave(SilcServer server,
1247 SilcSocketConnection sock,
1249 SilcChannelEntry channel,
1250 SilcClientID *client_id)
1254 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1255 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1256 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1257 1, idp->data, idp->len);
1258 silc_buffer_free(idp);
1261 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1262 `channel' mode to `mode. The Notify packet is always destined to
1265 void silc_server_send_notify_cmode(SilcServer server,
1266 SilcSocketConnection sock,
1268 SilcChannelEntry channel,
1269 SilcUInt32 mode_mask,
1270 void *id, SilcIdType id_type,
1271 const char *cipher, const char *hmac,
1272 const char *passphrase,
1273 SilcPublicKey founder_key)
1275 SilcBuffer idp, fkey = NULL;
1276 unsigned char mode[4];
1278 idp = silc_id_payload_encode((void *)id, id_type);
1279 SILC_PUT32_MSB(mode_mask, mode);
1281 fkey = silc_pkcs_public_key_payload_encode(founder_key);
1283 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1284 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1285 6, idp->data, idp->len,
1287 cipher, cipher ? strlen(cipher) : 0,
1288 hmac, hmac ? strlen(hmac) : 0,
1289 passphrase, passphrase ?
1290 strlen(passphrase) : 0,
1291 fkey ? fkey->data : NULL, fkey ? fkey->len : 0);
1292 silc_buffer_free(fkey),
1293 silc_buffer_free(idp);
1296 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1297 `target' client's mode on `channel'. The notify packet is always
1298 destined to the channel. */
1300 void silc_server_send_notify_cumode(SilcServer server,
1301 SilcSocketConnection sock,
1303 SilcChannelEntry channel,
1304 SilcUInt32 mode_mask,
1305 void *id, SilcIdType id_type,
1306 SilcClientID *target,
1307 SilcPublicKey founder_key)
1309 SilcBuffer idp1, idp2, fkey = NULL;
1310 unsigned char mode[4];
1312 idp1 = silc_id_payload_encode((void *)id, id_type);
1313 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1314 SILC_PUT32_MSB(mode_mask, mode);
1316 fkey = silc_pkcs_public_key_payload_encode(founder_key);
1318 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1320 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
1321 idp1->data, idp1->len,
1323 idp2->data, idp2->len,
1324 fkey ? fkey->data : NULL, fkey ? fkey->len : 0);
1325 silc_buffer_free(fkey);
1326 silc_buffer_free(idp1);
1327 silc_buffer_free(idp2);
1330 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1331 left SILC network. This function is used only between server and router
1332 traffic. This is not used to send the notify to the channel for
1333 client. The `message may be NULL. */
1335 void silc_server_send_notify_signoff(SilcServer server,
1336 SilcSocketConnection sock,
1338 SilcClientID *client_id,
1339 const char *message)
1343 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1344 silc_server_send_notify(server, sock, broadcast,
1345 SILC_NOTIFY_TYPE_SIGNOFF,
1346 message ? 2 : 1, idp->data, idp->len,
1347 message, message ? strlen(message): 0);
1348 silc_buffer_free(idp);
1351 /* Sends TOPIC_SET notify type. This tells that `id' changed
1352 the `channel's topic to `topic'. The Notify packet is always destined
1353 to the channel. This function is used to send the topic set notifies
1356 void silc_server_send_notify_topic_set(SilcServer server,
1357 SilcSocketConnection sock,
1359 SilcChannelEntry channel,
1360 void *id, SilcIdType id_type,
1365 idp = silc_id_payload_encode(id, id_type);
1366 silc_server_send_notify_dest(server, sock, broadcast,
1367 (void *)channel->id, SILC_ID_CHANNEL,
1368 SILC_NOTIFY_TYPE_TOPIC_SET,
1370 idp->data, idp->len,
1371 topic, topic ? strlen(topic) : 0);
1372 silc_buffer_free(idp);
1375 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1376 was kicked off the channel. The `comment' may indicate the reason
1377 for the kicking. This function is used only between server and router
1380 void silc_server_send_notify_kicked(SilcServer server,
1381 SilcSocketConnection sock,
1383 SilcChannelEntry channel,
1384 SilcClientID *client_id,
1385 SilcClientID *kicker,
1391 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1392 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1393 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1394 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1395 idp1->data, idp1->len,
1396 comment, comment ? strlen(comment) : 0,
1397 idp2->data, idp2->len);
1398 silc_buffer_free(idp1);
1399 silc_buffer_free(idp2);
1402 /* Send KILLED notify type. This tells that the `client_id' client was
1403 killed from the network. The `comment' may indicate the reason
1406 void silc_server_send_notify_killed(SilcServer server,
1407 SilcSocketConnection sock,
1409 SilcClientID *client_id,
1410 const char *comment,
1411 void *killer, SilcIdType killer_type)
1416 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1417 idp2 = silc_id_payload_encode(killer, killer_type);
1418 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1419 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1420 3, idp1->data, idp1->len,
1421 comment, comment ? strlen(comment) : 0,
1422 idp2->data, idp2->len);
1423 silc_buffer_free(idp1);
1424 silc_buffer_free(idp2);
1427 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1428 user mode in the SILC Network was changed. This function is used to
1429 send the packet between routers as broadcast packet. */
1431 void silc_server_send_notify_umode(SilcServer server,
1432 SilcSocketConnection sock,
1434 SilcClientID *client_id,
1435 SilcUInt32 mode_mask)
1438 unsigned char mode[4];
1440 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1441 SILC_PUT32_MSB(mode_mask, mode);
1443 silc_server_send_notify(server, sock, broadcast,
1444 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1445 idp->data, idp->len,
1447 silc_buffer_free(idp);
1450 /* Sends BAN notify type. This tells that ban has been either `add'ed
1451 or `del'eted on the `channel. This function is used to send the packet
1452 between routers as broadcast packet. */
1454 void silc_server_send_notify_ban(SilcServer server,
1455 SilcSocketConnection sock,
1457 SilcChannelEntry channel,
1458 unsigned char *action,
1463 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1464 silc_server_send_notify(server, sock, broadcast,
1465 SILC_NOTIFY_TYPE_BAN, 3,
1466 idp->data, idp->len,
1467 action ? action : NULL, action ? 1 : 0,
1468 list ? list->data : NULL, list ? list->len : 0);
1469 silc_buffer_free(idp);
1472 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1473 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1474 This function is used to send the packet between routers as broadcast
1477 void silc_server_send_notify_invite(SilcServer server,
1478 SilcSocketConnection sock,
1480 SilcChannelEntry channel,
1481 SilcClientID *client_id,
1482 unsigned char *action,
1485 SilcBuffer idp, idp2;
1487 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1488 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1489 silc_server_send_notify(server, sock, broadcast,
1490 SILC_NOTIFY_TYPE_INVITE, 5,
1491 idp->data, idp->len,
1492 channel->channel_name, strlen(channel->channel_name),
1493 idp2->data, idp2->len,
1494 action ? action : NULL, action ? 1 : 0,
1495 list ? list->data : NULL, list ? list->len : 0);
1496 silc_buffer_free(idp);
1497 silc_buffer_free(idp2);
1500 /* Sends WATCH notify type. This tells that the `client' was watched and
1501 its status in the network has changed. */
1503 void silc_server_send_notify_watch(SilcServer server,
1504 SilcSocketConnection sock,
1505 SilcClientEntry watcher,
1506 SilcClientEntry client,
1507 const char *nickname,
1508 SilcNotifyType type)
1511 unsigned char mode[4], n[2];
1513 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1514 SILC_PUT16_MSB(type, n);
1515 SILC_PUT32_MSB(client->mode, mode);
1516 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1517 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1518 4, idp->data, idp->len,
1519 nickname, nickname ? strlen(nickname) : 0,
1521 type != SILC_NOTIFY_TYPE_NONE ?
1522 n : NULL, sizeof(n));
1523 silc_buffer_free(idp);
1526 /* Sends notify message destined to specific entity. */
1528 void silc_server_send_notify_dest(SilcServer server,
1529 SilcSocketConnection sock,
1532 SilcIdType dest_id_type,
1533 SilcNotifyType type,
1534 SilcUInt32 argc, ...)
1541 packet = silc_notify_payload_encode(type, argc, ap);
1542 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1543 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1544 dest_id, dest_id_type,
1545 packet->data, packet->len, FALSE);
1547 /* Send to backup routers if this is being broadcasted to primary
1548 router. The silc_server_backup_send checks further whether to
1549 actually send it or not. */
1550 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1551 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1552 silc_server_backup_send_dest(server, NULL, SILC_PACKET_NOTIFY, 0,
1553 dest_id, dest_id_type,
1554 packet->data, packet->len, FALSE, TRUE);
1556 silc_buffer_free(packet);
1560 /* Sends notify message to a channel. The notify message sent is
1561 distributed to all clients on the channel. If `route_notify' is TRUE
1562 then the notify may be routed to primary route or to some other routers.
1563 If FALSE it is assured that the notify is sent only locally. If `sender'
1564 is provided then the packet is not sent to that connection since it
1565 originally came from it. */
1567 void silc_server_send_notify_to_channel(SilcServer server,
1568 SilcSocketConnection sender,
1569 SilcChannelEntry channel,
1571 SilcNotifyType type,
1572 SilcUInt32 argc, ...)
1579 packet = silc_notify_payload_encode(type, argc, ap);
1580 silc_server_packet_send_to_channel(server, sender, channel,
1581 SILC_PACKET_NOTIFY, route_notify,
1582 packet->data, packet->len, FALSE);
1583 silc_buffer_free(packet);
1587 /* Send notify message to all channels the client has joined. It is quaranteed
1588 that the message is sent only once to a client (ie. if a client is joined
1589 on two same channel it will receive only one notify message). Also, this
1590 sends only to local clients (locally connected if we are server, and to
1591 local servers if we are router). If `sender' is provided the packet is
1592 not sent to that client at all. */
1594 void silc_server_send_notify_on_channels(SilcServer server,
1595 SilcClientEntry sender,
1596 SilcClientEntry client,
1597 SilcNotifyType type,
1598 SilcUInt32 argc, ...)
1601 SilcSocketConnection sock = NULL;
1602 SilcPacketContext packetdata;
1604 SilcClientEntry *sent_clients = NULL;
1605 SilcUInt32 sent_clients_count = 0;
1606 SilcServerEntry *routed = NULL;
1607 SilcUInt32 routed_count = 0;
1608 SilcHashTableList htl, htl2;
1609 SilcChannelEntry channel;
1610 SilcChannelClientEntry chl, chl2;
1611 SilcIDListData idata;
1613 unsigned char *data;
1614 SilcUInt32 data_len;
1615 bool force_send = FALSE;
1618 if (!silc_hash_table_count(client->channels)) {
1619 SILC_LOG_DEBUG(("Client is not joined to any channels"));
1623 SILC_LOG_DEBUG(("Sending notify to joined channels"));
1626 packet = silc_notify_payload_encode(type, argc, ap);
1627 data = packet->data;
1628 data_len = packet->len;
1630 /* Set the packet context pointers. */
1631 packetdata.flags = 0;
1632 packetdata.type = SILC_PACKET_NOTIFY;
1633 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1634 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1635 packetdata.src_id_type = SILC_ID_SERVER;
1637 silc_hash_table_list(client->channels, &htl);
1638 while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
1639 channel = chl->channel;
1641 /* Send the message to all clients on the channel's client list. */
1642 silc_hash_table_list(channel->user_list, &htl2);
1643 while (silc_hash_table_get(&htl2, NULL, (void **)&chl2)) {
1646 if (sender && c == sender)
1649 /* Check if we have sent the packet to this client already */
1650 for (k = 0; k < sent_clients_count; k++)
1651 if (sent_clients[k] == c)
1653 if (k < sent_clients_count)
1656 /* If we are router and if this client has router set it is not
1657 locally connected client and we will route the message to the
1658 router set in the client. */
1659 if (c && c->router && server->server_type == SILC_ROUTER) {
1660 /* Check if we have sent the packet to this route already */
1661 for (k = 0; k < routed_count; k++)
1662 if (routed[k] == c->router)
1664 if (k < routed_count)
1667 /* Get data used in packet header encryption, keys and stuff. */
1668 sock = (SilcSocketConnection)c->router->connection;
1669 idata = (SilcIDListData)c->router;
1672 SILC_LOG_DEBUG(("*****************"));
1673 SILC_LOG_DEBUG(("client->router->id %s",
1674 silc_id_render(c->router->id, SILC_ID_SERVER)));
1675 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1676 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1679 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1680 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1681 packetdata.dst_id_type = SILC_ID_SERVER;
1683 /* Send the packet */
1684 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1688 data, data_len, FALSE,
1691 silc_free(packetdata.dst_id);
1693 /* We want to make sure that the packet is routed to same router
1694 only once. Mark this route as sent route. */
1695 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1696 routed[routed_count++] = c->router;
1703 /* Send to locally connected client */
1706 /* Get data used in packet header encryption, keys and stuff. */
1707 sock = (SilcSocketConnection)c->connection;
1708 idata = (SilcIDListData)c;
1713 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1714 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1715 packetdata.dst_id_type = SILC_ID_CLIENT;
1717 /* Send the packet */
1718 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1722 data, data_len, FALSE,
1725 silc_free(packetdata.dst_id);
1727 /* Make sure that we send the notify only once per client. */
1728 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1729 (sent_clients_count + 1));
1730 sent_clients[sent_clients_count++] = c;
1733 silc_hash_table_list_reset(&htl2);
1736 silc_hash_table_list_reset(&htl);
1738 silc_free(sent_clients);
1739 silc_free(packetdata.src_id);
1740 silc_buffer_free(packet);
1744 /* Sends New ID Payload to remote end. The packet is used to distribute
1745 information about new registered clients, servers, channel etc. usually
1746 to routers so that they can keep these information up to date.
1747 If the argument `broadcast' is TRUE then the packet is sent as
1748 broadcast packet. */
1750 void silc_server_send_new_id(SilcServer server,
1751 SilcSocketConnection sock,
1753 void *id, SilcIdType id_type,
1758 SILC_LOG_DEBUG(("Sending new ID"));
1760 idp = silc_id_payload_encode(id, id_type);
1761 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1762 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1763 idp->data, idp->len, FALSE);
1764 silc_buffer_free(idp);
1767 /* Send New Channel Payload to notify about newly created channel in the
1768 SILC network. Router uses this to notify other routers in the network
1769 about new channel. This packet is broadcasted by router. */
1771 void silc_server_send_new_channel(SilcServer server,
1772 SilcSocketConnection sock,
1776 SilcUInt32 channel_id_len,
1781 SilcUInt32 name_len = strlen(channel_name);
1783 SILC_LOG_DEBUG(("Sending new channel"));
1785 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1789 /* Encode the channel payload */
1790 packet = silc_channel_payload_encode(channel_name, name_len,
1791 cid, channel_id_len, mode);
1793 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1794 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1795 packet->data, packet->len, FALSE);
1798 silc_buffer_free(packet);
1801 /* Send Channel Key payload to distribute the new channel key. Normal server
1802 sends this to router when new client joins to existing channel. Router
1803 sends this to the local server who sent the join command in case where
1804 the channel did not exist yet. Both normal and router servers uses this
1805 also to send this to locally connected clients on the channel. This
1806 must not be broadcasted packet. Routers do not send this to each other.
1807 If `sender is provided then the packet is not sent to that connection since
1808 it originally came from it. */
1810 void silc_server_send_channel_key(SilcServer server,
1811 SilcSocketConnection sender,
1812 SilcChannelEntry channel,
1813 unsigned char route)
1816 unsigned char *chid;
1820 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1822 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1826 if (!channel->channel_key)
1829 /* Encode channel key packet */
1830 cipher = silc_cipher_get_name(channel->channel_key);
1831 tmp_len = strlen(cipher);
1832 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1834 chid, tmp_len, cipher,
1835 channel->key_len / 8, channel->key);
1836 silc_server_packet_send_to_channel(server, sender, channel,
1837 SILC_PACKET_CHANNEL_KEY,
1838 route, packet->data, packet->len,
1840 silc_buffer_free(packet);
1844 /* Generic function to send any command. The arguments must be sent already
1845 encoded into correct form in correct order. */
1847 void silc_server_send_command(SilcServer server,
1848 SilcSocketConnection sock,
1849 SilcCommand command,
1851 SilcUInt32 argc, ...)
1858 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1859 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1860 packet->data, packet->len, FALSE);
1861 silc_buffer_free(packet);
1865 /* Generic function to send any command reply. The arguments must be sent
1866 already encoded into correct form in correct order. */
1868 void silc_server_send_command_reply(SilcServer server,
1869 SilcSocketConnection sock,
1870 SilcCommand command,
1874 SilcUInt32 argc, ...)
1881 packet = silc_command_reply_payload_encode_vap(command, status, error,
1883 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1884 packet->data, packet->len, TRUE);
1885 silc_buffer_free(packet);
1889 /* Generic function to send any command reply. The arguments must be sent
1890 already encoded into correct form in correct order. */
1892 void silc_server_send_dest_command_reply(SilcServer server,
1893 SilcSocketConnection sock,
1895 SilcIdType dst_id_type,
1896 SilcCommand command,
1900 SilcUInt32 argc, ...)
1907 packet = silc_command_reply_payload_encode_vap(command, status, error,
1909 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1910 dst_id, dst_id_type, packet->data,
1911 packet->len, FALSE);
1912 silc_buffer_free(packet);
1916 /* Send the heartbeat packet. */
1918 void silc_server_send_heartbeat(SilcServer server,
1919 SilcSocketConnection sock)
1921 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1925 /* Generic function to relay packet we've received. This is used to relay
1926 packets to a client but generally can be used to other purposes as well. */
1928 void silc_server_relay_packet(SilcServer server,
1929 SilcSocketConnection dst_sock,
1932 SilcUInt32 sequence,
1933 SilcPacketContext *packet,
1936 const SilcBufferStruct p;
1938 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1939 + packet->dst_id_len + packet->padlen);
1940 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1941 (const SilcBuffer)&p)) {
1942 SILC_LOG_ERROR(("Cannot send packet"));
1945 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1947 /* Re-encrypt packet */
1948 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1950 /* Send the packet */
1951 silc_server_packet_send_real(server, dst_sock, force_send);
1953 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1954 + packet->dst_id_len + packet->padlen);
1956 /* Check for mandatory rekey */
1957 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
1958 silc_schedule_task_add(server->schedule, dst_sock->sock,
1959 silc_server_rekey_callback, dst_sock, 0, 1,
1960 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1963 /* Routine used to send the connection authentication packet. */
1965 void silc_server_send_connection_auth_request(SilcServer server,
1966 SilcSocketConnection sock,
1967 SilcUInt16 conn_type,
1968 SilcAuthMethod auth_meth)
1972 packet = silc_buffer_alloc(4);
1973 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1974 silc_buffer_format(packet,
1975 SILC_STR_UI_SHORT(conn_type),
1976 SILC_STR_UI_SHORT(auth_meth),
1979 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1980 0, packet->data, packet->len, FALSE);
1981 silc_buffer_free(packet);
1984 /* Purge the outgoing packet queue to the network if there is data. This
1985 function can be used to empty the packet queue. It is guaranteed that
1986 after this function returns the outgoing data queue is empty. */
1988 void silc_server_packet_queue_purge(SilcServer server,
1989 SilcSocketConnection sock)
1991 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1992 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1993 server->stat.packets_sent++;
1994 silc_packet_send(sock, TRUE);
1995 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1996 SILC_UNSET_OUTBUF_PENDING(sock);
1997 silc_buffer_clear(sock->outbuf);
2001 /* Send packet to clients that are known to be operators. If server
2002 is router and `route' is TRUE then the packet would go to all operators
2003 in the SILC network. If `route' is FALSE then only local operators
2004 (local for server and cell wide for router). If `local' is TRUE then
2005 only locally connected operators receive the packet. If `local' is
2006 TRUE then `route' is ignored. If server is normal server and `route'
2007 is FALSE it is equivalent to `local' being TRUE. */
2009 void silc_server_send_opers(SilcServer server,
2010 SilcPacketType type,
2011 SilcPacketFlags flags,
2012 bool route, bool local,
2013 unsigned char *data,
2014 SilcUInt32 data_len,
2017 SilcIDCacheList list = NULL;
2018 SilcIDCacheEntry id_cache = NULL;
2019 SilcClientEntry client = NULL;
2020 SilcSocketConnection sock;
2021 SilcServerEntry *routed = NULL;
2022 SilcUInt32 routed_count = 0;
2026 SILC_LOG_DEBUG(("Sending %s packet to operators",
2027 silc_get_packet_name(type)));
2029 /* If local was requested send only locally connected operators. */
2030 if (local || (server->server_type == SILC_SERVER && !route)) {
2031 if (!silc_idcache_get_all(server->local_list->clients, &list) ||
2032 !silc_idcache_list_first(list, &id_cache))
2035 client = (SilcClientEntry)id_cache->context;
2036 if (!client->router && SILC_IS_LOCAL(client) &&
2037 (client->mode & SILC_UMODE_SERVER_OPERATOR ||
2038 client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2040 /* Send the packet to locally connected operator */
2041 silc_server_packet_send_dest(server, client->connection, type, flags,
2042 client->id, SILC_ID_CLIENT,
2043 data, data_len, force_send);
2046 if (!silc_idcache_list_next(list, &id_cache))
2049 silc_idcache_list_free(list);
2053 if (!silc_idcache_get_all(server->local_list->clients, &list) ||
2054 !silc_idcache_list_first(list, &id_cache))
2057 client = (SilcClientEntry)id_cache->context;
2058 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
2059 !(client->mode & SILC_UMODE_ROUTER_OPERATOR))
2062 if (server->server_type != SILC_SERVER && client->router &&
2063 ((!route && client->router->router == server->id_entry) || route)) {
2065 /* Check if we have sent the packet to this route already */
2066 for (k = 0; k < routed_count; k++)
2067 if (routed[k] == client->router)
2069 if (k < routed_count)
2072 /* Route only once to router */
2073 sock = (SilcSocketConnection)client->router->connection;
2074 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2080 /* Send the packet */
2081 silc_server_packet_send_dest(server, sock, type, flags,
2082 client->id, SILC_ID_CLIENT,
2083 data, data_len, force_send);
2085 /* Mark this route routed already */
2086 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
2087 routed[routed_count++] = client->router;
2091 if (client->router || !client->connection)
2094 /* Send to locally connected client */
2095 sock = (SilcSocketConnection)client->connection;
2096 silc_server_packet_send_dest(server, sock, type, flags,
2097 client->id, SILC_ID_CLIENT,
2098 data, data_len, force_send);
2101 if (!silc_idcache_list_next(list, &id_cache))
2104 silc_idcache_list_free(list);
2106 if (!silc_idcache_get_all(server->global_list->clients, &list) ||
2107 !silc_idcache_list_first(list, &id_cache))
2110 client = (SilcClientEntry)id_cache->context;
2111 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
2112 !(client->mode & SILC_UMODE_ROUTER_OPERATOR))
2115 if (server->server_type != SILC_SERVER && client->router &&
2116 ((!route && client->router->router == server->id_entry) || route)) {
2118 /* Check if we have sent the packet to this route already */
2119 for (k = 0; k < routed_count; k++)
2120 if (routed[k] == client->router)
2122 if (k < routed_count)
2125 /* Route only once to router */
2126 sock = (SilcSocketConnection)client->router->connection;
2127 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2133 /* Send the packet */
2134 silc_server_packet_send_dest(server, sock, type, flags,
2135 client->id, SILC_ID_CLIENT,
2136 data, data_len, force_send);
2138 /* Mark this route routed already */
2139 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
2140 routed[routed_count++] = client->router;
2144 if (client->router || !client->connection)
2147 /* Send to locally connected client */
2148 sock = (SilcSocketConnection)client->connection;
2149 silc_server_packet_send_dest(server, sock, type, flags,
2150 client->id, SILC_ID_CLIENT,
2151 data, data_len, force_send);
2154 if (!silc_idcache_list_next(list, &id_cache))
2157 silc_idcache_list_free(list);
2161 /* Send a notify packet to operators */
2163 void silc_server_send_opers_notify(SilcServer server,
2166 SilcNotifyType type,
2167 SilcUInt32 argc, ...)
2173 packet = silc_notify_payload_encode(type, argc, ap);
2174 silc_server_send_opers(server, SILC_PACKET_NOTIFY, 0,
2175 route, local, packet->data, packet->len,
2177 silc_buffer_free(packet);