5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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) || SILC_IS_DISCONNECTED(sock))
42 ret = silc_packet_send(sock, FALSE);
45 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
46 SILC_UNSET_OUTBUF_PENDING(sock);
47 silc_buffer_clear(sock->outbuf);
49 SILC_LOG_ERROR(("Error sending packet to connection "
50 "%s:%d [%s]", sock->hostname, sock->port,
51 (sock->type == SILC_SOCKET_TYPE_UNKNOWN ? "Unknown" :
52 sock->type == SILC_SOCKET_TYPE_CLIENT ? "Client" :
53 sock->type == SILC_SOCKET_TYPE_SERVER ? "Server" :
56 if (sock->user_data) {
57 /* If backup then mark that resuming will not be allowed */
58 if (server->server_type == SILC_ROUTER && !server->backup_router &&
59 sock->type == SILC_SOCKET_TYPE_SERVER) {
60 SilcServerEntry server_entry = sock->user_data;
61 if (server_entry->server_type == SILC_BACKUP_ROUTER)
62 server->backup_closed = TRUE;
65 silc_server_free_sock_user_data(server, sock, NULL);
67 SILC_SET_DISCONNECTING(sock);
68 silc_server_close_connection(server, sock);
72 server->stat.packets_sent++;
76 /* Mark that there is some outgoing data available for this connection.
77 This call sets the connection both for input and output (the input
78 is set always and this call keeps the input setting, actually).
79 Actual data sending is performed by silc_server_packet_process. */
80 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
82 /* Mark to socket that data is pending in outgoing buffer. This flag
83 is needed if new data is added to the buffer before the earlier
84 put data is sent to the network. */
85 SILC_SET_OUTBUF_PENDING(sock);
90 /* Assembles a new packet to be sent out to network. This doesn't actually
91 send the packet but creates the packet and fills the outgoing data
92 buffer and marks the packet ready to be sent to network. However, If
93 argument force_send is TRUE the packet is sent immediately and not put
94 to queue. Normal case is that the packet is not sent immediately. */
96 void silc_server_packet_send(SilcServer server,
97 SilcSocketConnection sock,
99 SilcPacketFlags flags,
105 SilcIdType dst_id_type = SILC_ID_NONE;
106 SilcIDListData idata;
111 idata = (SilcIDListData)sock->user_data;
113 /* If disconnecting, ignore the data */
114 if (SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock))
117 /* If entry is disabled do not sent anything. Allow hearbeat and
119 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED &&
120 type != SILC_PACKET_HEARTBEAT && type != SILC_PACKET_REKEY &&
121 type != SILC_PACKET_REKEY_DONE && type != SILC_PACKET_KEY_EXCHANGE_1
122 && type != SILC_PACKET_KEY_EXCHANGE_2) ||
123 (sock->user_data == server->id_entry)) {
124 SILC_LOG_DEBUG(("Connection is disabled"));
128 /* Get data used in the packet sending, keys and stuff */
130 case SILC_SOCKET_TYPE_CLIENT:
131 if (sock->user_data) {
132 dst_id = ((SilcClientEntry)sock->user_data)->id;
133 dst_id_type = SILC_ID_CLIENT;
136 case SILC_SOCKET_TYPE_SERVER:
137 case SILC_SOCKET_TYPE_ROUTER:
138 if (sock->user_data) {
139 dst_id = ((SilcServerEntry)sock->user_data)->id;
140 dst_id_type = SILC_ID_SERVER;
147 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
148 dst_id_type, data, data_len, force_send);
151 /* Assembles a new packet to be sent out to network. This doesn't actually
152 send the packet but creates the packet and fills the outgoing data
153 buffer and marks the packet ready to be sent to network. However, If
154 argument force_send is TRUE the packet is sent immediately and not put
155 to queue. Normal case is that the packet is not sent immediately.
156 Destination information is sent as argument for this function. */
158 void silc_server_packet_send_dest(SilcServer server,
159 SilcSocketConnection sock,
161 SilcPacketFlags flags,
163 SilcIdType dst_id_type,
168 SilcPacketContext packetdata;
169 const SilcBufferStruct packet;
170 SilcIDListData idata;
171 SilcCipher cipher = NULL;
172 SilcHmac hmac = NULL;
173 SilcUInt32 sequence = 0;
174 unsigned char *dst_id_data = NULL;
175 SilcUInt32 dst_id_len = 0;
178 /* If disconnecting, ignore the data */
179 if (!sock || SILC_IS_DISCONNECTING(sock) || SILC_IS_DISCONNECTED(sock))
182 idata = (SilcIDListData)sock->user_data;
184 /* If entry is disabled do not sent anything. Allow hearbeat and
186 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED &&
187 type != SILC_PACKET_HEARTBEAT && type != SILC_PACKET_REKEY &&
188 type != SILC_PACKET_REKEY_DONE && type != SILC_PACKET_KEY_EXCHANGE_1
189 && type != SILC_PACKET_KEY_EXCHANGE_2) ||
190 (sock->user_data == server->id_entry)) {
191 SILC_LOG_DEBUG(("Connection is disabled"));
195 SILC_LOG_DEBUG(("Sending %s packet (forced=%s)",
196 silc_get_packet_name(type), force_send ? "yes" : "no"));
199 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
200 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
204 cipher = idata->send_key;
205 hmac = idata->hmac_send;
206 sequence = idata->psn_send++;
208 block_len = silc_cipher_get_block_len(cipher);
210 /* Check for mandatory rekey */
211 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
212 silc_schedule_task_add(server->schedule, sock->sock,
213 silc_server_rekey_callback, sock, 0, 1,
214 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
217 /* Set the packet context pointers */
218 packetdata.type = type;
219 packetdata.flags = flags;
220 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
221 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
222 packetdata.src_id_type = SILC_ID_SERVER;
223 packetdata.dst_id = dst_id_data;
224 packetdata.dst_id_len = dst_id_len;
225 packetdata.dst_id_type = dst_id_type;
226 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
227 packetdata.src_id_len +
228 packetdata.dst_id_len));
229 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
230 packetdata.src_id_len + dst_id_len;
231 if (type == SILC_PACKET_CONNECTION_AUTH)
232 SILC_PACKET_PADLEN_MAX(packetdata.truelen, block_len, packetdata.padlen);
234 SILC_PACKET_PADLEN(packetdata.truelen, block_len, packetdata.padlen);
236 /* Create the outgoing packet */
237 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock,
238 data, data_len, (const SilcBuffer)&packet)) {
239 SILC_LOG_ERROR(("Cannot assemble packet"));
243 /* Encrypt the packet */
244 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
246 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
247 packet.data, packet.len);
249 /* Now actually send the packet */
250 silc_server_packet_send_real(server, sock, force_send);
253 silc_free(packetdata.src_id);
254 silc_free(packetdata.dst_id);
257 /* Assembles a new packet to be sent out to network. This doesn't actually
258 send the packet but creates the packet and fills the outgoing data
259 buffer and marks the packet ready to be sent to network. However, If
260 argument force_send is TRUE the packet is sent immediately and not put
261 to queue. Normal case is that the packet is not sent immediately.
262 The source and destination information is sent as argument for this
265 void silc_server_packet_send_srcdest(SilcServer server,
266 SilcSocketConnection sock,
268 SilcPacketFlags flags,
270 SilcIdType src_id_type,
272 SilcIdType dst_id_type,
277 SilcPacketContext packetdata;
278 const SilcBufferStruct packet;
279 SilcIDListData idata;
280 SilcCipher cipher = NULL;
281 SilcHmac hmac = NULL;
282 SilcUInt32 sequence = 0;
283 unsigned char *dst_id_data = NULL;
284 SilcUInt32 dst_id_len = 0;
285 unsigned char *src_id_data = NULL;
286 SilcUInt32 src_id_len = 0;
289 SILC_LOG_DEBUG(("Sending %s packet", silc_get_packet_name(type)));
294 /* Get data used in the packet sending, keys and stuff */
295 idata = (SilcIDListData)sock->user_data;
297 /* If entry is disabled do not sent anything. Allow hearbeat and
299 if ((idata && idata->status & SILC_IDLIST_STATUS_DISABLED &&
300 type != SILC_PACKET_HEARTBEAT && type != SILC_PACKET_REKEY &&
301 type != SILC_PACKET_REKEY_DONE && type != SILC_PACKET_KEY_EXCHANGE_1
302 && type != SILC_PACKET_KEY_EXCHANGE_2) ||
303 (sock->user_data == server->id_entry)) {
304 SILC_LOG_DEBUG(("Connection is disabled"));
309 cipher = idata->send_key;
310 hmac = idata->hmac_send;
311 sequence = idata->psn_send++;
312 block_len = silc_cipher_get_block_len(cipher);
314 /* Check for mandatory rekey */
315 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
316 silc_schedule_task_add(server->schedule, sock->sock,
317 silc_server_rekey_callback, sock, 0, 1,
318 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
322 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
323 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
327 src_id_data = silc_id_id2str(src_id, src_id_type);
328 src_id_len = silc_id_get_len(src_id, src_id_type);
331 /* Set the packet context pointers */
332 packetdata.type = type;
333 packetdata.flags = flags;
334 packetdata.src_id = src_id_data;
335 packetdata.src_id_len = src_id_len;
336 packetdata.src_id_type = src_id_type;
337 packetdata.dst_id = dst_id_data;
338 packetdata.dst_id_len = dst_id_len;
339 packetdata.dst_id_type = dst_id_type;
340 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
341 packetdata.src_id_len +
343 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
344 packetdata.src_id_len + dst_id_len;
345 SILC_PACKET_PADLEN(packetdata.truelen, block_len, packetdata.padlen);
347 /* Create the outgoing packet */
348 if (!silc_packet_assemble(&packetdata, NULL, cipher, hmac, sock, data,
349 data_len, (const SilcBuffer)&packet)) {
350 SILC_LOG_ERROR(("Cannot assemble packe"));
354 /* Encrypt the packet */
355 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet, packet.len);
357 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence, packet.len),
358 packet.data, packet.len);
360 /* Now actually send the packet */
361 silc_server_packet_send_real(server, sock, force_send);
364 silc_free(packetdata.src_id);
365 silc_free(packetdata.dst_id);
368 /* Broadcast received packet to our primary route. This function is used
369 by router to further route received broadcast packet. It is expected
370 that the broadcast flag from the packet is checked before calling this
371 function. This does not test or set the broadcast flag. */
373 void silc_server_packet_broadcast(SilcServer server,
374 SilcSocketConnection sock,
375 SilcPacketContext *packet)
377 SilcBuffer buffer = packet->buffer;
378 SilcIDListData idata;
384 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
386 /* If the packet is originated from our primary route we are
387 not allowed to send the packet. */
388 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
389 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
390 const SilcBufferStruct p;
392 idata = (SilcIDListData)sock->user_data;
394 silc_buffer_push(buffer, buffer->data - buffer->head);
395 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
396 (const SilcBuffer)&p)) {
397 SILC_LOG_ERROR(("Cannot send packet"));
401 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
402 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
403 (SilcBuffer)&p, p.len);
405 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
406 p.len), p.data, p.len);
408 /* Now actually send the packet */
409 silc_server_packet_send_real(server, sock, TRUE);
412 /* Check for mandatory rekey */
413 if (idata->psn_send == SILC_SERVER_REKEY_THRESHOLD)
414 silc_schedule_task_add(server->schedule, sock->sock,
415 silc_server_rekey_callback, sock, 0, 1,
416 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
420 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
421 "original sender of this packet"));
425 /* Routes received packet to `sock'. This is used to route the packets that
426 router receives but are not destined to it. */
428 void silc_server_packet_route(SilcServer server,
429 SilcSocketConnection sock,
430 SilcPacketContext *packet)
432 SilcBuffer buffer = packet->buffer;
433 const SilcBufferStruct p;
434 SilcIDListData idata;
436 SILC_LOG_DEBUG(("Routing received packet"));
438 idata = (SilcIDListData)sock->user_data;
440 silc_buffer_push(buffer, buffer->data - buffer->head);
441 if (!silc_packet_send_prepare(sock, 0, 0, buffer->len, idata->hmac_send,
442 (const SilcBuffer)&p)) {
443 SILC_LOG_ERROR(("Cannot send packet"));
446 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
447 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
448 (SilcBuffer)&p, p.len);
450 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
451 p.len), p.data, p.len);
453 /* Now actually send the packet */
454 silc_server_packet_send_real(server, sock, TRUE);
456 /* Check for mandatory rekey */
457 if (idata->psn_send == SILC_SERVER_REKEY_THRESHOLD)
458 silc_schedule_task_add(server->schedule, sock->sock,
459 silc_server_rekey_callback, sock, 0, 1,
460 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
463 /* This routine can be used to send a packet to table of clients provided
464 in `clients'. If `route' is FALSE the packet is routed only to local
465 clients (for server locally connected, and for router local cell). */
467 void silc_server_packet_send_clients(SilcServer server,
468 SilcHashTable clients,
470 SilcPacketFlags flags,
476 SilcSocketConnection sock = NULL;
477 SilcHashTableList htl;
478 SilcClientEntry client = NULL;
479 SilcServerEntry *routed = NULL;
480 SilcUInt32 routed_count = 0;
484 if (!silc_hash_table_count(clients))
487 SILC_LOG_DEBUG(("Sending packet to %d clients",
488 silc_hash_table_count(clients)));
490 /* Send to all clients in table */
491 silc_hash_table_list(clients, &htl);
492 while (silc_hash_table_get(&htl, NULL, (void *)&client)) {
493 /* If client has router set it is not locally connected client and
494 we will route the message to the router set in the client. Though,
495 send locally connected server in all cases. */
496 if (server->server_type == SILC_ROUTER && client->router &&
497 ((!route && client->router->router == server->id_entry) || route)) {
499 /* Check if we have sent the packet to this route already */
500 for (k = 0; k < routed_count; k++)
501 if (routed[k] == client->router)
503 if (k < routed_count)
506 /* Route only once to router */
507 sock = (SilcSocketConnection)client->router->connection;
508 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
514 /* Send the packet */
515 silc_server_packet_send_dest(server, sock, type, flags,
516 client->router->id, SILC_ID_SERVER,
517 data, data_len, force_send);
519 /* Mark this route routed already */
520 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
521 routed[routed_count++] = client->router;
528 /* Send to locally connected client */
529 sock = (SilcSocketConnection)client->connection;
533 silc_server_packet_send_dest(server, sock, type, flags,
534 client->id, SILC_ID_CLIENT,
535 data, data_len, force_send);
537 silc_hash_table_list_reset(&htl);
541 /* Internal routine to actually create the channel packet and send it
542 to network. This is common function in channel message sending. If
543 `channel_message' is TRUE this encrypts the message as it is strictly
544 a channel message. If FALSE normal encryption process is used. */
547 silc_server_packet_send_to_channel_real(SilcServer server,
548 SilcSocketConnection sock,
549 SilcPacketContext *packet,
555 bool channel_message,
559 const SilcBufferStruct p;
564 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
566 packet->dst_id_len));
567 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
568 packet->src_id_len + packet->dst_id_len;
570 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
572 SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
574 packet->dst_id_len), block_len, packet->padlen);
576 SILC_PACKET_PADLEN(packet->truelen, block_len, packet->padlen);
578 /* Put the data to buffer, assemble and encrypt the packet. The packet
579 is encrypted with normal session key shared with the client, unless
580 the `channel_message' is TRUE. */
581 if (!silc_packet_assemble(packet, NULL, cipher, hmac, sock, data,
582 data_len, (const SilcBuffer)&p)) {
583 SILC_LOG_ERROR(("Cannot assemble packet"));
588 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
589 SILC_PACKET_HEADER_LEN + packet->src_id_len +
590 packet->dst_id_len + packet->padlen);
592 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
594 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence, p.len),
597 /* Now actually send the packet */
598 silc_server_packet_send_real(server, sock, force_send);
601 /* This routine is used by the server to send packets to channel. The
602 packet sent with this function is distributed to all clients on
603 the channel. Usually this is used to send notify messages to the
604 channel, things like notify about new user joining to the channel.
605 If `route' is FALSE then the packet is sent only locally and will not
606 be routed anywhere (for router locally means cell wide). If `sender'
607 is provided then the packet is not sent to that connection since it
608 originally came from it. If `send_to_clients' is FALSE then the
609 packet is not sent clients, only servers. */
611 void silc_server_packet_send_to_channel(SilcServer server,
612 SilcSocketConnection sender,
613 SilcChannelEntry channel,
616 bool send_to_clients,
621 SilcSocketConnection sock = NULL;
622 SilcPacketContext packetdata;
623 SilcClientEntry client = NULL;
624 SilcServerEntry *routed = NULL;
625 SilcChannelClientEntry chl;
626 SilcHashTableList htl;
627 SilcIDListData idata;
628 SilcUInt32 routed_count = 0;
632 /* This doesn't send channel message packets */
633 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
635 /* Set the packet context pointers. */
636 packetdata.flags = 0;
637 packetdata.type = type;
638 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
639 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
640 packetdata.src_id_type = SILC_ID_SERVER;
641 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
642 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
643 packetdata.dst_id_type = SILC_ID_CHANNEL;
645 /* If there are global users in the channel we will send the message
646 first to our router for further routing. */
647 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
648 channel->global_users) {
649 SilcServerEntry router;
651 /* Get data used in packet header encryption, keys and stuff. */
652 router = server->router;
653 sock = (SilcSocketConnection)router->connection;
654 idata = (SilcIDListData)router;
656 if (sock != sender) {
657 SILC_LOG_DEBUG(("Sending packet to router for routing"));
658 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
662 data, data_len, FALSE,
667 if (!silc_hash_table_count(channel->user_list)) {
668 SILC_LOG_DEBUG(("Channel %s is empty", channel->channel_name));
672 SILC_LOG_DEBUG(("Sending %s to channel %s",
673 silc_get_packet_name(type), channel->channel_name));
675 routed = silc_calloc(silc_hash_table_count(channel->user_list),
678 /* Send the message to clients on the channel's client list. */
679 silc_hash_table_list(channel->user_list, &htl);
680 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
681 client = chl->client;
685 /* If client has router set it is not locally connected client and
686 we will route the message to the router set in the client. Though,
687 send locally connected server in all cases. */
688 if (server->server_type == SILC_ROUTER && client->router &&
689 ((!route && client->router->router == server->id_entry) || route)) {
691 /* Check if we have sent the packet to this route already */
692 for (k = 0; k < routed_count; k++)
693 if (routed[k] == client->router)
695 if (k < routed_count)
698 /* Get data used in packet header encryption, keys and stuff. */
699 sock = (SilcSocketConnection)client->router->connection;
700 idata = (SilcIDListData)client->router;
702 if (sender && sock == sender)
705 /* Route only once to router. Protocol prohibits sending channel
706 messages to more than one router. */
707 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
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 /* Mark this route routed already */
726 routed[routed_count++] = client->router;
730 if (client->router || !send_to_clients)
733 /* Send to locally connected client */
735 /* Get data used in packet header encryption, keys and stuff. */
736 sock = (SilcSocketConnection)client->connection;
737 idata = (SilcIDListData)client;
739 if (!sock || (sender && sock == sender))
742 SILC_LOG_DEBUG(("Sending packet to client %s",
743 client->nickname ? client->nickname :
744 (unsigned char *)""));
746 /* Send the packet */
747 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
751 data, data_len, FALSE,
754 silc_hash_table_list_reset(&htl);
758 silc_free(packetdata.src_id);
759 silc_free(packetdata.dst_id);
762 /* This checks whether the relayed packet came from router. If it did
763 then we'll need to encrypt it with the channel key. This is called
764 from the silc_server_packet_relay_to_channel. */
767 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
768 SilcSocketConnection sock,
769 SilcChannelEntry channel,
771 unsigned int data_len)
773 SilcUInt32 mac_len, iv_len;
774 unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
775 SilcUInt16 totlen, len;
777 /* If we are router and the packet came from router and private key
778 has not been set for the channel then we must encrypt the packet
779 as it was decrypted with the session key shared between us and the
780 router which sent it. This is so, because cells does not share the
782 if (server->server_type == SILC_ROUTER &&
783 sock->type == SILC_SOCKET_TYPE_ROUTER &&
784 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) && channel->key) {
786 /* If we are backup router and remote is our primary router and
787 we are currently doing backup resuming protocol we must not
788 re-encrypt message with session key. */
789 if (server->backup_router && SILC_SERVER_IS_BACKUP(sock) &&
790 SILC_PRIMARY_ROUTE(server) == sock)
793 mac_len = silc_hmac_len(channel->hmac);
794 iv_len = silc_cipher_get_block_len(channel->channel_key);
796 if (data_len <= mac_len + iv_len) {
797 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
802 SILC_GET16_MSB(len, data + totlen);
804 if (totlen + iv_len + mac_len + 2 > data_len) {
805 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
808 SILC_GET16_MSB(len, data + totlen);
810 if (totlen + iv_len + mac_len > data_len) {
811 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
815 memcpy(iv, data + (data_len - iv_len - mac_len), iv_len);
816 silc_message_payload_encrypt(data, totlen, data_len - mac_len,
817 iv, iv_len, channel->channel_key,
824 /* This routine is explicitly used to relay messages to some channel.
825 Packets sent with this function we have received earlier and are
826 totally encrypted. This just sends the packet to all clients on
827 the channel. If the sender of the packet is someone on the channel
828 the message will not be sent to that client. The SILC Packet header
829 is encrypted with the session key shared between us and the client.
830 MAC is also computed before encrypting the header. Rest of the
831 packet will be untouched. */
833 void silc_server_packet_relay_to_channel(SilcServer server,
834 SilcSocketConnection sender_sock,
835 SilcChannelEntry channel,
837 SilcIdType sender_type,
838 SilcClientEntry sender_entry,
843 SilcSocketConnection sock = NULL;
844 SilcPacketContext packetdata;
845 SilcClientEntry client = NULL;
846 SilcServerEntry *routed = NULL;
847 SilcChannelClientEntry chl, chl_sender;
848 SilcUInt32 routed_count = 0;
849 SilcIDListData idata;
850 SilcHashTableList htl;
854 if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
857 SILC_LOG_DEBUG(("Relaying packet to channel %s", channel->channel_name));
859 /* This encrypts the packet, if needed. It will be encrypted if
860 it came from the router thus it needs to be encrypted with the
861 channel key. If the channel key does not exist, then we know we
862 don't have a single local user on the channel. */
863 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
868 /* Set the packet context pointers. */
869 packetdata.flags = 0;
870 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
871 packetdata.src_id = silc_id_id2str(sender_id, sender_type);
872 packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
873 packetdata.src_id_type = sender_type;
874 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
875 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
876 packetdata.dst_id_type = SILC_ID_CHANNEL;
878 /* If there are global users in the channel we will send the message
879 first to our router for further routing. */
880 if (server->server_type != SILC_ROUTER && !server->standalone &&
881 channel->global_users) {
882 SilcServerEntry router = server->router;
884 /* Check that the sender is not our router. */
885 if (sender_sock != (SilcSocketConnection)router->connection) {
887 /* Get data used in packet header encryption, keys and stuff. */
888 sock = (SilcSocketConnection)router->connection;
889 idata = (SilcIDListData)router;
891 SILC_LOG_DEBUG(("Sending message to router for routing"));
893 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
897 data, data_len, TRUE,
902 routed = silc_calloc(silc_hash_table_count(channel->user_list),
905 /* Assure we won't route the message back to the sender's way. */
906 if (sender_entry->router)
907 routed[routed_count++] = sender_entry->router;
909 /* Send the message to clients on the channel's client list. */
910 silc_hash_table_list(channel->user_list, &htl);
911 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
912 client = chl->client;
913 if (!client || client == sender_entry)
916 /* Check whether message sending is blocked */
917 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
919 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
920 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
921 !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
923 if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
924 sender_entry->mode & SILC_UMODE_ROBOT)
927 /* If the client has set router it means that it is not locally
928 connected client and we will route the packet further. */
929 if (server->server_type == SILC_ROUTER && client->router) {
931 /* Check if we have sent the packet to this route already */
932 for (k = 0; k < routed_count; k++)
933 if (routed[k] == client->router)
935 if (k < routed_count)
938 /* Get data used in packet header encryption, keys and stuff. */
939 sock = (SilcSocketConnection)client->router->connection;
940 idata = (SilcIDListData)client->router;
942 /* Check if the sender socket is the same as this client's router
944 if (sender_sock && sock == sender_sock)
947 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
948 silc_id_render(client->id, SILC_ID_CLIENT),
949 sock->hostname, sock->ip));
951 /* Mark this route routed already. */
952 routed[routed_count++] = client->router;
954 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
955 /* The remote connection is router then we'll decrypt the
956 channel message and re-encrypt it with the session key shared
957 between us and the remote router. This is done because the
958 channel keys are cell specific and we have different channel
959 key than the remote router has. */
961 /* Route only once to router. Protocol prohibits sending channel
962 messages to more than one router. */
967 /* If we are backup router and remote is our primary router and
968 we are currently doing backup resuming protocol we must not
969 re-encrypt message with session key. */
970 if (server->backup_router && SILC_SERVER_IS_BACKUP(sock) &&
971 SILC_PRIMARY_ROUTE(server) == sock) {
972 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
976 data, data_len, TRUE,
981 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
983 /* If private key mode is not set then decrypt the packet
985 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
986 channel->channel_key) {
987 unsigned char tmp[SILC_PACKET_MAX_LEN];
989 if (data_len > SILC_PACKET_MAX_LEN)
990 data_len = SILC_PACKET_MAX_LEN;
991 memcpy(tmp, data, data_len);
993 /* Decrypt the channel message (we don't check the MAC) */
994 silc_message_payload_decrypt(tmp, data_len, FALSE, FALSE,
995 channel->channel_key,
996 channel->hmac, FALSE);
998 /* Now re-encrypt and send it to the router */
999 silc_server_packet_send_srcdest(server, sock,
1000 SILC_PACKET_CHANNEL_MESSAGE, 0,
1001 sender_id, sender_type,
1002 channel->id, SILC_ID_CHANNEL,
1003 tmp, data_len, force_send);
1005 /* Private key mode is set, we don't have the channel key, so
1006 just re-encrypt the entire packet and send it to the router. */
1007 silc_server_packet_send_srcdest(server, sock,
1008 SILC_PACKET_CHANNEL_MESSAGE, 0,
1009 sender_id, sender_type,
1010 channel->id, SILC_ID_CHANNEL,
1011 data, data_len, force_send);
1014 /* Send the packet to normal server */
1015 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1019 data, data_len, TRUE,
1029 /* Get data used in packet header encryption, keys and stuff. */
1030 sock = (SilcSocketConnection)client->connection;
1031 idata = (SilcIDListData)client;
1033 if (!sock || (sender_sock && sock == sender_sock))
1036 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
1037 silc_id_render(client->id, SILC_ID_CLIENT),
1038 sock->hostname, sock->ip));
1040 /* Send the packet */
1041 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1045 data, data_len, TRUE,
1049 silc_hash_table_list_reset(&htl);
1051 silc_free(packetdata.src_id);
1052 silc_free(packetdata.dst_id);
1055 /* This function is used to send packets strictly to all local clients
1056 on a particular channel. This is used for example to distribute new
1057 channel key to all our locally connected clients on the channel.
1058 The packets are always encrypted with the session key shared between
1059 the client, this means these are not _to the channel_ but _to the client_
1062 void silc_server_packet_send_local_channel(SilcServer server,
1063 SilcChannelEntry channel,
1064 SilcPacketType type,
1065 SilcPacketFlags flags,
1066 unsigned char *data,
1067 SilcUInt32 data_len,
1070 SilcChannelClientEntry chl;
1071 SilcHashTableList htl;
1072 SilcSocketConnection sock = NULL;
1074 SILC_LOG_DEBUG(("Send packet to local clients on channel %s",
1075 channel->channel_name));
1077 /* Send the message to clients on the channel's client list. */
1078 silc_hash_table_list(channel->user_list, &htl);
1079 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1080 if (chl->client && SILC_IS_LOCAL(chl->client)) {
1081 sock = chl->client->connection;
1083 /* Send the packet to the client */
1084 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
1085 SILC_ID_CLIENT, data, data_len,
1089 silc_hash_table_list_reset(&htl);
1092 /* Routine used to send (relay, route) private messages to some destination.
1093 If the private message key does not exist then the message is re-encrypted,
1094 otherwise we just pass it along. This really is not used to send new
1095 private messages (as server does not send them) but to relay received
1096 private messages. */
1098 void silc_server_send_private_message(SilcServer server,
1099 SilcSocketConnection dst_sock,
1102 SilcUInt32 sequence,
1103 SilcPacketContext *packet)
1105 SilcBuffer buffer = packet->buffer;
1106 const SilcBufferStruct p;
1108 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1109 + packet->dst_id_len + packet->padlen);
1110 if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
1111 (const SilcBuffer)&p)) {
1112 SILC_LOG_ERROR(("Cannot send packet"));
1115 silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
1117 /* Re-encrypt and send if private messge key does not exist */
1118 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
1119 /* Re-encrypt packet */
1120 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
1122 /* Key exist so encrypt just header and send it */
1123 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p,
1124 SILC_PACKET_HEADER_LEN + packet->src_id_len +
1125 packet->dst_id_len + packet->padlen);
1128 /* Send the packet */
1129 silc_server_packet_send_real(server, dst_sock, FALSE);
1131 /* Check for mandatory rekey */
1132 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
1133 silc_schedule_task_add(server->schedule, dst_sock->sock,
1134 silc_server_rekey_callback, dst_sock, 0, 1,
1135 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
1138 /* Sends current motd to client */
1140 void silc_server_send_motd(SilcServer server,
1141 SilcSocketConnection sock)
1143 char *motd, *motd_file = NULL;
1144 SilcUInt32 motd_len;
1147 motd_file = server->config->server_info->motd_file;
1150 motd = silc_file_readfile(motd_file, &motd_len);
1155 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1161 /* Sends error message. Error messages may or may not have any
1164 void silc_server_send_error(SilcServer server,
1165 SilcSocketConnection sock,
1166 const char *fmt, ...)
1169 unsigned char buf[4096];
1171 memset(buf, 0, sizeof(buf));
1173 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1176 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1177 buf, strlen(buf), FALSE);
1180 /* Sends notify message. If format is TRUE the variable arguments are
1181 formatted and the formatted string is sent as argument payload. If it is
1182 FALSE then each argument is sent as separate argument and their format
1183 in the argument list must be { argument data, argument length }. */
1185 void silc_server_send_notify(SilcServer server,
1186 SilcSocketConnection sock,
1188 SilcNotifyType type,
1189 SilcUInt32 argc, ...)
1196 packet = silc_notify_payload_encode(type, argc, ap);
1197 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1198 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1199 packet->data, packet->len, FALSE);
1201 /* Send to backup routers if this is being broadcasted to primary
1202 router. The silc_server_backup_send checks further whether to
1203 actually send it or not. */
1204 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1205 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1206 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1207 packet->data, packet->len, FALSE, TRUE);
1209 silc_buffer_free(packet);
1213 /* Sends notify message and gets the arguments from the `args' Argument
1216 void silc_server_send_notify_args(SilcServer server,
1217 SilcSocketConnection sock,
1219 SilcNotifyType type,
1225 packet = silc_notify_payload_encode_args(type, argc, args);
1226 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1227 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1228 packet->data, packet->len, FALSE);
1229 silc_buffer_free(packet);
1232 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1233 `old_id' with the `new_id'. */
1235 void silc_server_send_notify_channel_change(SilcServer server,
1236 SilcSocketConnection sock,
1238 SilcChannelID *old_id,
1239 SilcChannelID *new_id)
1241 SilcBuffer idp1, idp2;
1243 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1244 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1246 silc_server_send_notify(server, sock, broadcast,
1247 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1248 2, idp1->data, idp1->len, idp2->data, idp2->len);
1249 silc_buffer_free(idp1);
1250 silc_buffer_free(idp2);
1253 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1254 `old_id' with the `new_id'. */
1256 void silc_server_send_notify_nick_change(SilcServer server,
1257 SilcSocketConnection sock,
1259 SilcClientID *old_id,
1260 SilcClientID *new_id,
1261 const char *nickname)
1263 SilcBuffer idp1, idp2;
1265 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1266 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1268 silc_server_send_notify(server, sock, broadcast,
1269 SILC_NOTIFY_TYPE_NICK_CHANGE,
1270 3, idp1->data, idp1->len, idp2->data, idp2->len,
1271 nickname, nickname ? strlen(nickname) : 0);
1272 silc_buffer_free(idp1);
1273 silc_buffer_free(idp2);
1276 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1277 has joined to the `channel'. */
1279 void silc_server_send_notify_join(SilcServer server,
1280 SilcSocketConnection sock,
1282 SilcChannelEntry channel,
1283 SilcClientID *client_id)
1285 SilcBuffer idp1, idp2;
1287 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1288 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1289 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1290 2, idp1->data, idp1->len,
1291 idp2->data, idp2->len);
1292 silc_buffer_free(idp1);
1293 silc_buffer_free(idp2);
1296 /* Sends LEAVE notify type. This tells that `client_id' has left the
1297 `channel'. The Notify packet is always destined to the channel. */
1299 void silc_server_send_notify_leave(SilcServer server,
1300 SilcSocketConnection sock,
1302 SilcChannelEntry channel,
1303 SilcClientID *client_id)
1307 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1308 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1309 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1310 1, idp->data, idp->len);
1311 silc_buffer_free(idp);
1314 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1315 `channel' mode to `mode. The Notify packet is always destined to
1318 void silc_server_send_notify_cmode(SilcServer server,
1319 SilcSocketConnection sock,
1321 SilcChannelEntry channel,
1322 SilcUInt32 mode_mask,
1323 void *id, SilcIdType id_type,
1324 const char *cipher, const char *hmac,
1325 const char *passphrase,
1326 SilcPublicKey founder_key,
1327 SilcBuffer channel_pubkeys)
1329 SilcBuffer idp, fkey = NULL;
1330 unsigned char mode[4], ulimit[4];
1332 idp = silc_id_payload_encode((void *)id, id_type);
1333 SILC_PUT32_MSB(mode_mask, mode);
1335 fkey = silc_pkcs_public_key_payload_encode(founder_key);
1336 if (channel->mode & SILC_CHANNEL_MODE_ULIMIT)
1337 SILC_PUT32_MSB(channel->user_limit, ulimit);
1339 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1340 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1341 8, idp->data, idp->len,
1343 cipher, cipher ? strlen(cipher) : 0,
1344 hmac, hmac ? strlen(hmac) : 0,
1345 passphrase, passphrase ?
1346 strlen(passphrase) : 0,
1347 fkey ? fkey->data : NULL, fkey ? fkey->len : 0,
1348 channel_pubkeys ? channel_pubkeys->data : NULL,
1349 channel_pubkeys ? channel_pubkeys->len : 0,
1350 mode_mask & SILC_CHANNEL_MODE_ULIMIT ?
1352 mode_mask & SILC_CHANNEL_MODE_ULIMIT ?
1353 sizeof(ulimit) : 0);
1354 silc_buffer_free(fkey);
1355 silc_buffer_free(idp);
1358 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1359 `target' client's mode on `channel'. The notify packet is always
1360 destined to the channel. */
1362 void silc_server_send_notify_cumode(SilcServer server,
1363 SilcSocketConnection sock,
1365 SilcChannelEntry channel,
1366 SilcUInt32 mode_mask,
1367 void *id, SilcIdType id_type,
1368 SilcClientID *target,
1369 SilcPublicKey founder_key)
1371 SilcBuffer idp1, idp2, fkey = NULL;
1372 unsigned char mode[4];
1374 idp1 = silc_id_payload_encode((void *)id, id_type);
1375 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1376 SILC_PUT32_MSB(mode_mask, mode);
1378 fkey = silc_pkcs_public_key_payload_encode(founder_key);
1380 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1382 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4,
1383 idp1->data, idp1->len,
1385 idp2->data, idp2->len,
1386 fkey ? fkey->data : NULL, fkey ? fkey->len : 0);
1387 silc_buffer_free(fkey);
1388 silc_buffer_free(idp1);
1389 silc_buffer_free(idp2);
1392 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1393 left SILC network. This function is used only between server and router
1394 traffic. This is not used to send the notify to the channel for
1395 client. The `message may be NULL. */
1397 void silc_server_send_notify_signoff(SilcServer server,
1398 SilcSocketConnection sock,
1400 SilcClientID *client_id,
1401 const char *message)
1405 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1406 silc_server_send_notify(server, sock, broadcast,
1407 SILC_NOTIFY_TYPE_SIGNOFF,
1408 message ? 2 : 1, idp->data, idp->len,
1409 message, message ? strlen(message): 0);
1410 silc_buffer_free(idp);
1413 /* Sends TOPIC_SET notify type. This tells that `id' changed
1414 the `channel's topic to `topic'. The Notify packet is always destined
1415 to the channel. This function is used to send the topic set notifies
1418 void silc_server_send_notify_topic_set(SilcServer server,
1419 SilcSocketConnection sock,
1421 SilcChannelEntry channel,
1422 void *id, SilcIdType id_type,
1427 idp = silc_id_payload_encode(id, id_type);
1428 silc_server_send_notify_dest(server, sock, broadcast,
1429 (void *)channel->id, SILC_ID_CHANNEL,
1430 SILC_NOTIFY_TYPE_TOPIC_SET,
1432 idp->data, idp->len,
1433 topic, topic ? strlen(topic) : 0);
1434 silc_buffer_free(idp);
1437 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1438 was kicked off the channel. The `comment' may indicate the reason
1439 for the kicking. This function is used only between server and router
1442 void silc_server_send_notify_kicked(SilcServer server,
1443 SilcSocketConnection sock,
1445 SilcChannelEntry channel,
1446 SilcClientID *client_id,
1447 SilcClientID *kicker,
1453 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1454 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1455 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1456 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1457 idp1->data, idp1->len,
1458 comment, comment ? strlen(comment) : 0,
1459 idp2->data, idp2->len);
1460 silc_buffer_free(idp1);
1461 silc_buffer_free(idp2);
1464 /* Send KILLED notify type. This tells that the `client_id' client was
1465 killed from the network. The `comment' may indicate the reason
1468 void silc_server_send_notify_killed(SilcServer server,
1469 SilcSocketConnection sock,
1471 SilcClientID *client_id,
1472 const char *comment,
1473 void *killer, SilcIdType killer_type)
1478 idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1479 idp2 = silc_id_payload_encode(killer, killer_type);
1480 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1481 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1482 3, idp1->data, idp1->len,
1483 comment, comment ? strlen(comment) : 0,
1484 idp2->data, idp2->len);
1485 silc_buffer_free(idp1);
1486 silc_buffer_free(idp2);
1489 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1490 user mode in the SILC Network was changed. This function is used to
1491 send the packet between routers as broadcast packet. */
1493 void silc_server_send_notify_umode(SilcServer server,
1494 SilcSocketConnection sock,
1496 SilcClientID *client_id,
1497 SilcUInt32 mode_mask)
1500 unsigned char mode[4];
1502 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1503 SILC_PUT32_MSB(mode_mask, mode);
1505 silc_server_send_notify(server, sock, broadcast,
1506 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1507 idp->data, idp->len,
1509 silc_buffer_free(idp);
1512 /* Sends BAN notify type. This tells that ban has been either `add'ed
1513 or `del'eted on the `channel. This function is used to send the packet
1514 between routers as broadcast packet. */
1516 void silc_server_send_notify_ban(SilcServer server,
1517 SilcSocketConnection sock,
1519 SilcChannelEntry channel,
1520 unsigned char *action,
1525 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1526 silc_server_send_notify(server, sock, broadcast,
1527 SILC_NOTIFY_TYPE_BAN, 3,
1528 idp->data, idp->len,
1529 action ? action : NULL, action ? 1 : 0,
1530 list ? list->data : NULL, list ? list->len : 0);
1531 silc_buffer_free(idp);
1534 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1535 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1536 This function is used to send the packet between routers as broadcast
1539 void silc_server_send_notify_invite(SilcServer server,
1540 SilcSocketConnection sock,
1542 SilcChannelEntry channel,
1543 SilcClientID *client_id,
1544 unsigned char *action,
1547 SilcBuffer idp, idp2;
1549 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1550 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1551 silc_server_send_notify(server, sock, broadcast,
1552 SILC_NOTIFY_TYPE_INVITE, 5,
1553 idp->data, idp->len,
1554 channel->channel_name, strlen(channel->channel_name),
1555 idp2->data, idp2->len,
1556 action ? action : NULL, action ? 1 : 0,
1557 list ? list->data : NULL, list ? list->len : 0);
1558 silc_buffer_free(idp);
1559 silc_buffer_free(idp2);
1562 /* Sends WATCH notify type. This tells that the `client' was watched and
1563 its status in the network has changed. */
1565 void silc_server_send_notify_watch(SilcServer server,
1566 SilcSocketConnection sock,
1567 SilcClientEntry watcher,
1568 SilcClientEntry client,
1569 const char *nickname,
1570 SilcNotifyType type,
1571 SilcPublicKey public_key)
1573 SilcBuffer idp, pkp = NULL;
1574 unsigned char mode[4], n[2];
1576 idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1577 SILC_PUT16_MSB(type, n);
1578 SILC_PUT32_MSB(client->mode, mode);
1580 pkp = silc_pkcs_public_key_payload_encode(public_key);
1581 silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1582 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1583 5, idp->data, idp->len,
1584 nickname, nickname ? strlen(nickname) : 0,
1586 type != SILC_NOTIFY_TYPE_NONE ?
1587 n : NULL, sizeof(n),
1588 pkp ? pkp->data : NULL,
1589 pkp ? pkp->len : 0);
1590 silc_buffer_free(idp);
1591 silc_buffer_free(pkp);
1594 /* Sends notify message destined to specific entity. */
1596 void silc_server_send_notify_dest(SilcServer server,
1597 SilcSocketConnection sock,
1600 SilcIdType dest_id_type,
1601 SilcNotifyType type,
1602 SilcUInt32 argc, ...)
1609 packet = silc_notify_payload_encode(type, argc, ap);
1610 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1611 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1612 dest_id, dest_id_type,
1613 packet->data, packet->len, FALSE);
1615 /* Send to backup routers if this is being broadcasted to primary
1616 router. The silc_server_backup_send checks further whether to
1617 actually send it or not. */
1618 if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1619 (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1620 silc_server_backup_send_dest(server, NULL, SILC_PACKET_NOTIFY, 0,
1621 dest_id, dest_id_type,
1622 packet->data, packet->len, FALSE, TRUE);
1624 silc_buffer_free(packet);
1628 /* Sends notify message to a channel. The notify message sent is
1629 distributed to all clients on the channel. If `route_notify' is TRUE
1630 then the notify may be routed to primary route or to some other routers.
1631 If FALSE it is assured that the notify is sent only locally. If `sender'
1632 is provided then the packet is not sent to that connection since it
1633 originally came from it. */
1635 void silc_server_send_notify_to_channel(SilcServer server,
1636 SilcSocketConnection sender,
1637 SilcChannelEntry channel,
1639 bool send_to_clients,
1640 SilcNotifyType type,
1641 SilcUInt32 argc, ...)
1648 packet = silc_notify_payload_encode(type, argc, ap);
1649 silc_server_packet_send_to_channel(server, sender, channel,
1650 SILC_PACKET_NOTIFY, route_notify,
1652 packet->data, packet->len, FALSE);
1653 silc_buffer_free(packet);
1657 /* Send notify message to all channels the client has joined. It is quaranteed
1658 that the message is sent only once to a client (ie. if a client is joined
1659 on two same channel it will receive only one notify message). Also, this
1660 sends only to local clients (locally connected if we are server, and to
1661 local servers if we are router). If `sender' is provided the packet is
1662 not sent to that client at all. */
1664 void silc_server_send_notify_on_channels(SilcServer server,
1665 SilcClientEntry sender,
1666 SilcClientEntry client,
1667 SilcNotifyType type,
1668 SilcUInt32 argc, ...)
1671 SilcSocketConnection sock = NULL;
1672 SilcPacketContext packetdata;
1674 SilcClientEntry *sent_clients = NULL;
1675 SilcUInt32 sent_clients_count = 0;
1676 SilcServerEntry *routed = NULL;
1677 SilcUInt32 routed_count = 0;
1678 SilcHashTableList htl, htl2;
1679 SilcChannelEntry channel;
1680 SilcChannelClientEntry chl, chl2;
1681 SilcIDListData idata;
1683 unsigned char *data;
1684 SilcUInt32 data_len;
1685 bool force_send = FALSE;
1688 if (!silc_hash_table_count(client->channels)) {
1689 SILC_LOG_DEBUG(("Client is not joined to any channels"));
1693 SILC_LOG_DEBUG(("Sending notify to joined channels"));
1696 packet = silc_notify_payload_encode(type, argc, ap);
1697 data = packet->data;
1698 data_len = packet->len;
1700 /* Set the packet context pointers. */
1701 packetdata.flags = 0;
1702 packetdata.type = SILC_PACKET_NOTIFY;
1703 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1704 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1705 packetdata.src_id_type = SILC_ID_SERVER;
1707 silc_hash_table_list(client->channels, &htl);
1708 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1709 channel = chl->channel;
1711 /* Send the message to all clients on the channel's client list. */
1712 silc_hash_table_list(channel->user_list, &htl2);
1713 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1716 if (sender && c == sender)
1719 /* Check if we have sent the packet to this client already */
1720 for (k = 0; k < sent_clients_count; k++)
1721 if (sent_clients[k] == c)
1723 if (k < sent_clients_count)
1726 /* If we are router and if this client has router set it is not
1727 locally connected client and we will route the message to the
1728 router set in the client. */
1729 if (c && c->router && server->server_type == SILC_ROUTER) {
1730 /* Check if we have sent the packet to this route already */
1731 for (k = 0; k < routed_count; k++)
1732 if (routed[k] == c->router)
1734 if (k < routed_count)
1737 /* Get data used in packet header encryption, keys and stuff. */
1738 sock = (SilcSocketConnection)c->router->connection;
1739 idata = (SilcIDListData)c->router;
1742 SILC_LOG_DEBUG(("*****************"));
1743 SILC_LOG_DEBUG(("client->router->id %s",
1744 silc_id_render(c->router->id, SILC_ID_SERVER)));
1745 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1746 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1749 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1750 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1751 packetdata.dst_id_type = SILC_ID_SERVER;
1753 /* Send the packet */
1754 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1758 data, data_len, FALSE,
1761 silc_free(packetdata.dst_id);
1763 /* We want to make sure that the packet is routed to same router
1764 only once. Mark this route as sent route. */
1765 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1766 routed[routed_count++] = c->router;
1773 /* Send to locally connected client */
1776 /* Get data used in packet header encryption, keys and stuff. */
1777 sock = (SilcSocketConnection)c->connection;
1778 idata = (SilcIDListData)c;
1783 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1784 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1785 packetdata.dst_id_type = SILC_ID_CLIENT;
1787 /* Send the packet */
1788 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1792 data, data_len, FALSE,
1795 silc_free(packetdata.dst_id);
1797 /* Make sure that we send the notify only once per client. */
1798 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1799 (sent_clients_count + 1));
1800 sent_clients[sent_clients_count++] = c;
1803 silc_hash_table_list_reset(&htl2);
1806 silc_hash_table_list_reset(&htl);
1808 silc_free(sent_clients);
1809 silc_free(packetdata.src_id);
1810 silc_buffer_free(packet);
1814 /* Sends New ID Payload to remote end. The packet is used to distribute
1815 information about new registered clients, servers, channel etc. usually
1816 to routers so that they can keep these information up to date.
1817 If the argument `broadcast' is TRUE then the packet is sent as
1818 broadcast packet. */
1820 void silc_server_send_new_id(SilcServer server,
1821 SilcSocketConnection sock,
1823 void *id, SilcIdType id_type,
1828 SILC_LOG_DEBUG(("Sending new ID"));
1830 idp = silc_id_payload_encode(id, id_type);
1831 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1832 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1833 idp->data, idp->len, FALSE);
1834 silc_buffer_free(idp);
1837 /* Send New Channel Payload to notify about newly created channel in the
1838 SILC network. Router uses this to notify other routers in the network
1839 about new channel. This packet is broadcasted by router. */
1841 void silc_server_send_new_channel(SilcServer server,
1842 SilcSocketConnection sock,
1846 SilcUInt32 channel_id_len,
1851 SilcUInt32 name_len = strlen(channel_name);
1853 SILC_LOG_DEBUG(("Sending new channel"));
1855 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1859 /* Encode the channel payload */
1860 packet = silc_channel_payload_encode(channel_name, name_len,
1861 cid, channel_id_len, mode);
1863 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1864 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1865 packet->data, packet->len, FALSE);
1868 silc_buffer_free(packet);
1871 /* Send Channel Key payload to distribute the new channel key. Normal server
1872 sends this to router when new client joins to existing channel. Router
1873 sends this to the local server who sent the join command in case where
1874 the channel did not exist yet. Both normal and router servers uses this
1875 also to send this to locally connected clients on the channel. This
1876 must not be broadcasted packet. Routers do not send this to each other.
1877 If `sender is provided then the packet is not sent to that connection since
1878 it originally came from it. */
1880 void silc_server_send_channel_key(SilcServer server,
1881 SilcSocketConnection sender,
1882 SilcChannelEntry channel,
1883 unsigned char route)
1886 unsigned char *chid;
1890 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1892 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1896 if (!channel->channel_key)
1899 /* Encode channel key packet */
1900 cipher = silc_cipher_get_name(channel->channel_key);
1901 tmp_len = strlen(cipher);
1902 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1904 chid, tmp_len, cipher,
1905 channel->key_len / 8, channel->key);
1906 silc_server_packet_send_to_channel(server, sender, channel,
1907 SILC_PACKET_CHANNEL_KEY,
1908 route, TRUE, packet->data, packet->len,
1910 silc_buffer_free(packet);
1914 /* Generic function to send any command. The arguments must be sent already
1915 encoded into correct form in correct order. */
1917 void silc_server_send_command(SilcServer server,
1918 SilcSocketConnection sock,
1919 SilcCommand command,
1921 SilcUInt32 argc, ...)
1927 server->stat.commands_sent++;
1931 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1932 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1933 packet->data, packet->len, FALSE);
1934 silc_buffer_free(packet);
1938 /* Generic function to send any command reply. The arguments must be sent
1939 already encoded into correct form in correct order. */
1941 void silc_server_send_command_reply(SilcServer server,
1942 SilcSocketConnection sock,
1943 SilcCommand command,
1947 SilcUInt32 argc, ...)
1953 server->stat.commands_sent++;
1957 packet = silc_command_reply_payload_encode_vap(command, status, error,
1959 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1960 packet->data, packet->len, FALSE);
1961 silc_buffer_free(packet);
1965 /* Generic function to send any command reply. The arguments must be sent
1966 already encoded into correct form in correct order. */
1968 void silc_server_send_dest_command_reply(SilcServer server,
1969 SilcSocketConnection sock,
1971 SilcIdType dst_id_type,
1972 SilcCommand command,
1976 SilcUInt32 argc, ...)
1982 server->stat.commands_sent++;
1986 packet = silc_command_reply_payload_encode_vap(command, status, error,
1988 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1989 dst_id, dst_id_type, packet->data,
1990 packet->len, FALSE);
1991 silc_buffer_free(packet);
1995 /* Send the heartbeat packet. */
1997 void silc_server_send_heartbeat(SilcServer server,
1998 SilcSocketConnection sock)
2000 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
2004 /* Generic function to relay packet we've received. This is used to relay
2005 packets to a client but generally can be used to other purposes as well. */
2007 void silc_server_relay_packet(SilcServer server,
2008 SilcSocketConnection dst_sock,
2011 SilcUInt32 sequence,
2012 SilcPacketContext *packet,
2015 const SilcBufferStruct p;
2017 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2018 + packet->dst_id_len + packet->padlen);
2019 if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
2020 (const SilcBuffer)&p)) {
2021 SILC_LOG_ERROR(("Cannot send packet"));
2024 silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
2026 /* Re-encrypt packet */
2027 silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
2029 /* Send the packet */
2030 silc_server_packet_send_real(server, dst_sock, force_send);
2032 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
2033 + packet->dst_id_len + packet->padlen);
2035 /* Check for mandatory rekey */
2036 if (sequence == SILC_SERVER_REKEY_THRESHOLD)
2037 silc_schedule_task_add(server->schedule, dst_sock->sock,
2038 silc_server_rekey_callback, dst_sock, 0, 1,
2039 SILC_TASK_TIMEOUT, SILC_TASK_PRI_NORMAL);
2042 /* Routine used to send the connection authentication packet. */
2044 void silc_server_send_connection_auth_request(SilcServer server,
2045 SilcSocketConnection sock,
2046 SilcUInt16 conn_type,
2047 SilcAuthMethod auth_meth)
2051 packet = silc_buffer_alloc(4);
2052 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
2053 silc_buffer_format(packet,
2054 SILC_STR_UI_SHORT(conn_type),
2055 SILC_STR_UI_SHORT(auth_meth),
2058 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
2059 0, packet->data, packet->len, FALSE);
2060 silc_buffer_free(packet);
2063 /* Purge the outgoing packet queue to the network if there is data. This
2064 function can be used to empty the packet queue. */
2066 void silc_server_packet_queue_purge(SilcServer server,
2067 SilcSocketConnection sock)
2069 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
2070 !(SILC_IS_DISCONNECTING(sock)) && !(SILC_IS_DISCONNECTED(sock))) {
2073 SILC_LOG_DEBUG(("Purging outgoing queue"));
2075 server->stat.packets_sent++;
2076 ret = silc_packet_send(sock, TRUE);
2078 if (sock->outbuf && sock->outbuf->len > 0) {
2079 /* Couldn't send all data, put the queue back up, we'll send
2081 SILC_LOG_DEBUG(("Could not purge immediately, sending rest later"));
2082 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
2083 SILC_SET_OUTBUF_PENDING(sock);
2086 } else if (ret == -1) {
2087 SILC_LOG_ERROR(("Error purging packet queue, packets dropped"));
2090 /* Purged all data */
2091 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
2092 SILC_UNSET_OUTBUF_PENDING(sock);
2093 silc_buffer_clear(sock->outbuf);
2097 /* Send packet to clients that are known to be operators. If server
2098 is router and `route' is TRUE then the packet would go to all operators
2099 in the SILC network. If `route' is FALSE then only local operators
2100 (local for server and cell wide for router). If `local' is TRUE then
2101 only locally connected operators receive the packet. If `local' is
2102 TRUE then `route' is ignored. If server is normal server and `route'
2103 is FALSE it is equivalent to `local' being TRUE. */
2105 void silc_server_send_opers(SilcServer server,
2106 SilcPacketType type,
2107 SilcPacketFlags flags,
2108 bool route, bool local,
2109 unsigned char *data,
2110 SilcUInt32 data_len,
2113 SilcIDCacheList list = NULL;
2114 SilcIDCacheEntry id_cache = NULL;
2115 SilcClientEntry client = NULL;
2116 SilcSocketConnection sock;
2117 SilcServerEntry *routed = NULL;
2118 SilcUInt32 routed_count = 0;
2122 SILC_LOG_DEBUG(("Sending %s packet to operators",
2123 silc_get_packet_name(type)));
2125 /* If local was requested send only locally connected operators. */
2126 if (local || (server->server_type == SILC_SERVER && !route)) {
2127 if (!silc_idcache_get_all(server->local_list->clients, &list) ||
2128 !silc_idcache_list_first(list, &id_cache))
2131 client = (SilcClientEntry)id_cache->context;
2132 if (!client->router && SILC_IS_LOCAL(client) &&
2133 (client->mode & SILC_UMODE_SERVER_OPERATOR ||
2134 client->mode & SILC_UMODE_ROUTER_OPERATOR)) {
2136 /* Send the packet to locally connected operator */
2137 silc_server_packet_send_dest(server, client->connection, type, flags,
2138 client->id, SILC_ID_CLIENT,
2139 data, data_len, force_send);
2142 if (!silc_idcache_list_next(list, &id_cache))
2145 silc_idcache_list_free(list);
2149 if (!silc_idcache_get_all(server->local_list->clients, &list) ||
2150 !silc_idcache_list_first(list, &id_cache))
2153 client = (SilcClientEntry)id_cache->context;
2154 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
2155 !(client->mode & SILC_UMODE_ROUTER_OPERATOR))
2158 if (server->server_type != SILC_SERVER && client->router &&
2159 ((!route && client->router->router == server->id_entry) || route)) {
2161 /* Check if we have sent the packet to this route already */
2162 for (k = 0; k < routed_count; k++)
2163 if (routed[k] == client->router)
2165 if (k < routed_count)
2168 /* Route only once to router */
2169 sock = (SilcSocketConnection)client->router->connection;
2170 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2176 /* Send the packet */
2177 silc_server_packet_send_dest(server, sock, type, flags,
2178 client->id, SILC_ID_CLIENT,
2179 data, data_len, force_send);
2181 /* Mark this route routed already */
2182 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
2183 routed[routed_count++] = client->router;
2187 if (client->router || !client->connection)
2190 /* Send to locally connected client */
2191 sock = (SilcSocketConnection)client->connection;
2192 silc_server_packet_send_dest(server, sock, type, flags,
2193 client->id, SILC_ID_CLIENT,
2194 data, data_len, force_send);
2197 if (!silc_idcache_list_next(list, &id_cache))
2200 silc_idcache_list_free(list);
2202 if (!silc_idcache_get_all(server->global_list->clients, &list) ||
2203 !silc_idcache_list_first(list, &id_cache))
2206 client = (SilcClientEntry)id_cache->context;
2207 if (!(client->mode & SILC_UMODE_SERVER_OPERATOR) &&
2208 !(client->mode & SILC_UMODE_ROUTER_OPERATOR))
2211 if (server->server_type != SILC_SERVER && client->router &&
2212 ((!route && client->router->router == server->id_entry) || route)) {
2214 /* Check if we have sent the packet to this route already */
2215 for (k = 0; k < routed_count; k++)
2216 if (routed[k] == client->router)
2218 if (k < routed_count)
2221 /* Route only once to router */
2222 sock = (SilcSocketConnection)client->router->connection;
2223 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
2229 /* Send the packet */
2230 silc_server_packet_send_dest(server, sock, type, flags,
2231 client->id, SILC_ID_CLIENT,
2232 data, data_len, force_send);
2234 /* Mark this route routed already */
2235 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
2236 routed[routed_count++] = client->router;
2240 if (client->router || !client->connection)
2243 /* Send to locally connected client */
2244 sock = (SilcSocketConnection)client->connection;
2245 silc_server_packet_send_dest(server, sock, type, flags,
2246 client->id, SILC_ID_CLIENT,
2247 data, data_len, force_send);
2250 if (!silc_idcache_list_next(list, &id_cache))
2253 silc_idcache_list_free(list);
2257 /* Send a notify packet to operators */
2259 void silc_server_send_opers_notify(SilcServer server,
2262 SilcNotifyType type,
2263 SilcUInt32 argc, ...)
2269 packet = silc_notify_payload_encode(type, argc, ap);
2270 silc_server_send_opers(server, SILC_PACKET_NOTIFY, 0,
2271 route, local, packet->data, packet->len,
2273 silc_buffer_free(packet);