5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 * Server packet routines to send packets.
25 #include "serverincludes.h"
26 #include "server_internal.h"
28 /* Routine that sends packet or marks packet to be sent. This is used
29 directly only in special cases. Normal cases should use
30 silc_server_packet_send. Returns < 0 error. */
32 int silc_server_packet_send_real(SilcServer server,
33 SilcSocketConnection sock,
38 /* If disconnecting, ignore the data */
39 if (SILC_IS_DISCONNECTING(sock))
42 /* If rekey protocol is active we must assure that all packets are
43 sent through packet queue. */
44 if (SILC_SERVER_IS_REKEY(sock))
47 /* If outbound data is already pending do not force send */
48 if (SILC_IS_OUTBUF_PENDING(sock))
52 ret = silc_packet_send(sock, force_send);
56 /* Mark that there is some outgoing data available for this connection.
57 This call sets the connection both for input and output (the input
58 is set always and this call keeps the input setting, actually).
59 Actual data sending is performed by silc_server_packet_process. */
60 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
62 /* Mark to socket that data is pending in outgoing buffer. This flag
63 is needed if new data is added to the buffer before the earlier
64 put data is sent to the network. */
65 SILC_SET_OUTBUF_PENDING(sock);
70 /* Assembles a new packet to be sent out to network. This doesn't actually
71 send the packet but creates the packet and fills the outgoing data
72 buffer and marks the packet ready to be sent to network. However, If
73 argument force_send is TRUE the packet is sent immediately and not put
74 to queue. Normal case is that the packet is not sent immediately. */
76 void silc_server_packet_send(SilcServer server,
77 SilcSocketConnection sock,
79 SilcPacketFlags flags,
85 SilcIdType dst_id_type = SILC_ID_NONE;
86 SilcIDListData idata = (SilcIDListData)sock->user_data;
91 /* If disconnecting, ignore the data */
92 if (SILC_IS_DISCONNECTING(sock))
95 /* If entry is disabled do not sent anything. */
96 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
99 /* Get data used in the packet sending, keys and stuff */
101 case SILC_SOCKET_TYPE_CLIENT:
102 if (sock->user_data) {
103 dst_id = ((SilcClientEntry)sock->user_data)->id;
104 dst_id_type = SILC_ID_CLIENT;
107 case SILC_SOCKET_TYPE_SERVER:
108 case SILC_SOCKET_TYPE_ROUTER:
109 if (sock->user_data) {
110 dst_id = ((SilcServerEntry)sock->user_data)->id;
111 dst_id_type = SILC_ID_SERVER;
118 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
119 dst_id_type, data, data_len, force_send);
122 /* Assembles a new packet to be sent out to network. This doesn't actually
123 send the packet but creates the packet and fills the outgoing data
124 buffer and marks the packet ready to be sent to network. However, If
125 argument force_send is TRUE the packet is sent immediately and not put
126 to queue. Normal case is that the packet is not sent immediately.
127 Destination information is sent as argument for this function. */
129 void silc_server_packet_send_dest(SilcServer server,
130 SilcSocketConnection sock,
132 SilcPacketFlags flags,
134 SilcIdType dst_id_type,
139 SilcPacketContext packetdata;
140 SilcIDListData idata = (SilcIDListData)sock->user_data;
141 SilcCipher cipher = NULL;
142 SilcHmac hmac = NULL;
143 SilcUInt32 sequence = 0;
144 unsigned char *dst_id_data = NULL;
145 SilcUInt32 dst_id_len = 0;
148 /* If disconnecting, ignore the data */
149 if (SILC_IS_DISCONNECTING(sock))
152 /* If entry is disabled do not sent anything. */
153 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
156 SILC_LOG_DEBUG(("Sending packet, type %d", type));
159 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
160 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
164 cipher = idata->send_key;
165 hmac = idata->hmac_send;
166 sequence = idata->psn_send++;
167 block_len = silc_cipher_get_block_len(cipher);
170 /* Set the packet context pointers */
171 packetdata.type = type;
172 packetdata.flags = flags;
173 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
174 packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
175 packetdata.src_id_type = server->id_type;
176 packetdata.dst_id = dst_id_data;
177 packetdata.dst_id_len = dst_id_len;
178 packetdata.dst_id_type = dst_id_type;
179 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
180 packetdata.src_id_len +
181 packetdata.dst_id_len));
182 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
183 packetdata.src_id_len + dst_id_len;
184 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
186 /* Prepare outgoing data buffer for packet sending */
187 silc_packet_send_prepare(sock,
188 SILC_PACKET_HEADER_LEN +
189 packetdata.src_id_len +
190 packetdata.dst_id_len,
194 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
196 packetdata.buffer = sock->outbuf;
198 /* Put the data to the buffer */
199 if (data && data_len)
200 silc_buffer_put(sock->outbuf, data, data_len);
202 /* Create the outgoing packet */
203 silc_packet_assemble(&packetdata, cipher);
205 /* Encrypt the packet */
206 silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf, sock->outbuf->len);
208 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence,
210 sock->outbuf->data, sock->outbuf->len);
212 /* Now actually send the packet */
213 silc_server_packet_send_real(server, sock, force_send);
215 if (packetdata.src_id)
216 silc_free(packetdata.src_id);
217 if (packetdata.dst_id)
218 silc_free(packetdata.dst_id);
221 /* Assembles a new packet to be sent out to network. This doesn't actually
222 send the packet but creates the packet and fills the outgoing data
223 buffer and marks the packet ready to be sent to network. However, If
224 argument force_send is TRUE the packet is sent immediately and not put
225 to queue. Normal case is that the packet is not sent immediately.
226 The source and destination information is sent as argument for this
229 void silc_server_packet_send_srcdest(SilcServer server,
230 SilcSocketConnection sock,
232 SilcPacketFlags flags,
234 SilcIdType src_id_type,
236 SilcIdType dst_id_type,
241 SilcPacketContext packetdata;
242 SilcIDListData idata;
243 SilcCipher cipher = NULL;
244 SilcHmac hmac = NULL;
245 SilcUInt32 sequence = 0;
246 unsigned char *dst_id_data = NULL;
247 SilcUInt32 dst_id_len = 0;
248 unsigned char *src_id_data = NULL;
249 SilcUInt32 src_id_len = 0;
252 SILC_LOG_DEBUG(("Sending packet, type %d", type));
254 /* Get data used in the packet sending, keys and stuff */
255 idata = (SilcIDListData)sock->user_data;
258 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
259 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
263 src_id_data = silc_id_id2str(src_id, src_id_type);
264 src_id_len = silc_id_get_len(src_id, src_id_type);
268 cipher = idata->send_key;
269 hmac = idata->hmac_send;
270 sequence = idata->psn_send++;
271 block_len = silc_cipher_get_block_len(cipher);
274 /* Set the packet context pointers */
275 packetdata.type = type;
276 packetdata.flags = flags;
277 packetdata.src_id = src_id_data;
278 packetdata.src_id_len = src_id_len;
279 packetdata.src_id_type = src_id_type;
280 packetdata.dst_id = dst_id_data;
281 packetdata.dst_id_len = dst_id_len;
282 packetdata.dst_id_type = dst_id_type;
283 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
284 packetdata.src_id_len +
286 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
287 packetdata.src_id_len + dst_id_len;
288 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
290 /* Prepare outgoing data buffer for packet sending */
291 silc_packet_send_prepare(sock,
292 SILC_PACKET_HEADER_LEN +
293 packetdata.src_id_len +
294 packetdata.dst_id_len,
298 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
300 packetdata.buffer = sock->outbuf;
302 /* Put the data to the buffer */
303 if (data && data_len)
304 silc_buffer_put(sock->outbuf, data, data_len);
306 /* Create the outgoing packet */
307 silc_packet_assemble(&packetdata, cipher);
309 /* Encrypt the packet */
310 silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf, sock->outbuf->len);
312 SILC_LOG_HEXDUMP(("Outgoing packet (%d), len %d", sequence,
314 sock->outbuf->data, sock->outbuf->len);
316 /* Now actually send the packet */
317 silc_server_packet_send_real(server, sock, force_send);
319 if (packetdata.src_id)
320 silc_free(packetdata.src_id);
321 if (packetdata.dst_id)
322 silc_free(packetdata.dst_id);
325 /* Broadcast received packet to our primary route. This function is used
326 by router to further route received broadcast packet. It is expected
327 that the broadcast flag from the packet is checked before calling this
328 function. This does not test or set the broadcast flag. */
330 void silc_server_packet_broadcast(SilcServer server,
331 SilcSocketConnection sock,
332 SilcPacketContext *packet)
334 SilcBuffer buffer = packet->buffer;
335 SilcIDListData idata;
338 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
340 /* If the packet is originated from our primary route we are
341 not allowed to send the packet. */
342 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
343 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
344 idata = (SilcIDListData)sock->user_data;
346 silc_buffer_push(buffer, buffer->data - buffer->head);
347 silc_packet_send_prepare(sock, 0, 0, buffer->len);
348 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
349 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
350 sock->outbuf, sock->outbuf->len);
352 SILC_LOG_HEXDUMP(("Broadcasted packet (%d), len %d", idata->psn_send - 1,
354 sock->outbuf->data, sock->outbuf->len);
356 /* Now actually send the packet */
357 silc_server_packet_send_real(server, sock, TRUE);
362 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
363 "original sender of this packet"));
367 /* Routes received packet to `sock'. This is used to route the packets that
368 router receives but are not destined to it. */
370 void silc_server_packet_route(SilcServer server,
371 SilcSocketConnection sock,
372 SilcPacketContext *packet)
374 SilcBuffer buffer = packet->buffer;
375 SilcIDListData idata;
377 SILC_LOG_DEBUG(("Routing received packet"));
379 idata = (SilcIDListData)sock->user_data;
381 silc_buffer_push(buffer, buffer->data - buffer->head);
382 silc_packet_send_prepare(sock, 0, 0, buffer->len);
383 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
384 silc_packet_encrypt(idata->send_key, idata->hmac_send, idata->psn_send++,
385 sock->outbuf, sock->outbuf->len);
387 SILC_LOG_HEXDUMP(("Routed packet (%d), len %d", idata->psn_send - 1,
389 sock->outbuf->data, sock->outbuf->len);
391 /* Now actually send the packet */
392 silc_server_packet_send_real(server, sock, TRUE);
395 /* This routine can be used to send a packet to table of clients provided
396 in `clients'. If `route' is FALSE the packet is routed only to local
397 clients (for server locally connected, and for router local cell). */
399 void silc_server_packet_send_clients(SilcServer server,
400 SilcClientEntry *clients,
401 SilcUInt32 clients_count,
403 SilcPacketFlags flags,
409 SilcSocketConnection sock = NULL;
410 SilcClientEntry client = NULL;
411 SilcServerEntry *routed = NULL;
412 SilcUInt32 routed_count = 0;
416 SILC_LOG_DEBUG(("Sending packet to list of clients"));
418 /* Send to all clients in table */
419 for (i = 0; i < clients_count; i++) {
422 /* If client has router set it is not locally connected client and
423 we will route the message to the router set in the client. Though,
424 send locally connected server in all cases. */
425 if (server->server_type == SILC_ROUTER && client->router &&
426 ((!route && client->router->router == server->id_entry) || route)) {
428 /* Check if we have sent the packet to this route already */
429 for (k = 0; k < routed_count; k++)
430 if (routed[k] == client->router)
432 if (k < routed_count)
435 /* Route only once to router */
436 sock = (SilcSocketConnection)client->router->connection;
437 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
443 /* Send the packet */
444 silc_server_packet_send_dest(server, sock, type, flags,
445 client->router->id, SILC_ID_SERVER,
446 data, data_len, force_send);
448 /* Mark this route routed already */
449 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
450 routed[routed_count++] = client->router;
457 /* Send to locally connected client */
458 sock = (SilcSocketConnection)client->connection;
459 silc_server_packet_send_dest(server, sock, type, flags,
460 client->id, SILC_ID_CLIENT,
461 data, data_len, force_send);
467 /* Internal routine to actually create the channel packet and send it
468 to network. This is common function in channel message sending. If
469 `channel_message' is TRUE this encrypts the message as it is strictly
470 a channel message. If FALSE normal encryption process is used. */
473 silc_server_packet_send_to_channel_real(SilcServer server,
474 SilcSocketConnection sock,
475 SilcPacketContext *packet,
481 bool channel_message,
489 data_len = SILC_PACKET_DATALEN(data_len, (SILC_PACKET_HEADER_LEN +
491 packet->dst_id_len));
492 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
493 packet->src_id_len + packet->dst_id_len;
495 block_len = cipher ? silc_cipher_get_block_len(cipher) : 0;
497 packet->padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
499 packet->dst_id_len), block_len);
501 packet->padlen = SILC_PACKET_PADLEN(packet->truelen, block_len);
503 /* Prepare outgoing data buffer for packet sending */
504 silc_packet_send_prepare(sock,
505 SILC_PACKET_HEADER_LEN +
511 packet->buffer = sock->outbuf;
513 /* Put the data to buffer, assemble and encrypt the packet. The packet
514 is encrypted with normal session key shared with the client, unless
515 the `channel_message' is TRUE. */
516 silc_buffer_put(sock->outbuf, data, data_len);
517 silc_packet_assemble(packet, cipher);
519 silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf,
520 SILC_PACKET_HEADER_LEN + packet->src_id_len +
521 packet->dst_id_len + packet->padlen);
523 silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf,
526 SILC_LOG_HEXDUMP(("Channel packet (%d), len %d", sequence,
528 sock->outbuf->data, sock->outbuf->len);
530 /* Now actually send the packet */
531 silc_server_packet_send_real(server, sock, force_send);
534 /* This routine is used by the server to send packets to channel. The
535 packet sent with this function is distributed to all clients on
536 the channel. Usually this is used to send notify messages to the
537 channel, things like notify about new user joining to the channel.
538 If `route' is FALSE then the packet is sent only locally and will not
539 be routed anywhere (for router locally means cell wide). If `sender'
540 is provided then the packet is not sent to that connection since it
541 originally came from it. If `send_to_clients' is FALSE then the
542 packet is not sent clients, only servers. */
544 void silc_server_packet_send_to_channel(SilcServer server,
545 SilcSocketConnection sender,
546 SilcChannelEntry channel,
553 SilcSocketConnection sock = NULL;
554 SilcPacketContext packetdata;
555 SilcClientEntry client = NULL;
556 SilcServerEntry *routed = NULL;
557 SilcChannelClientEntry chl;
558 SilcHashTableList htl;
559 SilcIDListData idata;
560 SilcUInt32 routed_count = 0;
564 /* This doesn't send channel message packets */
565 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
567 SILC_LOG_DEBUG(("Sending packet to channel"));
569 /* Set the packet context pointers. */
570 packetdata.flags = 0;
571 packetdata.type = type;
572 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
573 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
574 packetdata.src_id_type = SILC_ID_SERVER;
575 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
576 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
577 packetdata.dst_id_type = SILC_ID_CHANNEL;
579 /* If there are global users in the channel we will send the message
580 first to our router for further routing. */
581 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
582 channel->global_users) {
583 SilcServerEntry router;
585 /* Get data used in packet header encryption, keys and stuff. */
586 router = server->router;
587 sock = (SilcSocketConnection)router->connection;
588 idata = (SilcIDListData)router;
590 if (sock != sender) {
591 SILC_LOG_DEBUG(("Sending packet to router for routing"));
593 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
597 data, data_len, FALSE,
602 routed = silc_calloc(silc_hash_table_count(channel->user_list),
605 /* Send the message to clients on the channel's client list. */
606 silc_hash_table_list(channel->user_list, &htl);
607 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
608 client = chl->client;
612 /* If client has router set it is not locally connected client and
613 we will route the message to the router set in the client. Though,
614 send locally connected server in all cases. */
615 if (server->server_type == SILC_ROUTER && client->router &&
616 ((!route && client->router->router == server->id_entry) || route)) {
618 /* Check if we have sent the packet to this route already */
619 for (k = 0; k < routed_count; k++)
620 if (routed[k] == client->router)
622 if (k < routed_count)
625 /* Get data used in packet header encryption, keys and stuff. */
626 sock = (SilcSocketConnection)client->router->connection;
627 idata = (SilcIDListData)client->router;
629 if (sender && sock == sender)
632 /* Route only once to router */
633 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
639 /* Send the packet */
640 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
644 data, data_len, FALSE,
647 /* Mark this route routed already */
648 routed[routed_count++] = client->router;
655 /* Send to locally connected client */
657 /* Get data used in packet header encryption, keys and stuff. */
658 sock = (SilcSocketConnection)client->connection;
659 idata = (SilcIDListData)client;
661 if (!sock || (sender && sock == sender))
664 /* Send the packet */
665 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
669 data, data_len, FALSE,
673 silc_hash_table_list_reset(&htl);
675 silc_free(packetdata.src_id);
676 silc_free(packetdata.dst_id);
679 /* This checks whether the relayed packet came from router. If it did
680 then we'll need to encrypt it with the channel key. This is called
681 from the silc_server_packet_relay_to_channel. */
684 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
685 SilcSocketConnection sock,
686 SilcChannelEntry channel,
688 unsigned int data_len)
690 /* If we are router and the packet came from router and private key
691 has not been set for the channel then we must encrypt the packet
692 as it was decrypted with the session key shared between us and the
693 router which sent it. This is so, because cells does not share the
695 if (server->server_type == SILC_ROUTER &&
696 sock->type == SILC_SOCKET_TYPE_ROUTER &&
697 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
698 channel->channel_key) {
700 SilcUInt32 iv_len, i;
701 SilcUInt16 dlen, flags;
703 iv_len = silc_cipher_get_block_len(channel->channel_key);
704 if (channel->iv[0] == '\0')
705 for (i = 0; i < iv_len; i++) channel->iv[i] =
706 silc_rng_get_byte(server->rng);
708 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
710 /* Encode new payload. This encrypts it also. */
711 SILC_GET16_MSB(flags, data);
712 SILC_GET16_MSB(dlen, data + 2);
714 if (dlen > data_len) {
715 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
719 chp = silc_channel_message_payload_encode(flags, dlen, data + 4,
721 channel->channel_key,
722 channel->hmac, NULL);
723 memcpy(data, chp->data, chp->len);
724 silc_buffer_free(chp);
730 /* This routine is explicitly used to relay messages to some channel.
731 Packets sent with this function we have received earlier and are
732 totally encrypted. This just sends the packet to all clients on
733 the channel. If the sender of the packet is someone on the channel
734 the message will not be sent to that client. The SILC Packet header
735 is encrypted with the session key shared between us and the client.
736 MAC is also computed before encrypting the header. Rest of the
737 packet will be untouched. */
739 void silc_server_packet_relay_to_channel(SilcServer server,
740 SilcSocketConnection sender_sock,
741 SilcChannelEntry channel,
743 SilcIdType sender_type,
750 SilcSocketConnection sock = NULL;
751 SilcPacketContext packetdata;
752 SilcClientEntry client = NULL;
753 SilcServerEntry *routed = NULL;
754 SilcChannelClientEntry chl;
755 SilcUInt32 routed_count = 0;
756 SilcIDListData idata;
757 SilcHashTableList htl;
761 SILC_LOG_DEBUG(("Relaying packet to channel"));
763 /* This encrypts the packet, if needed. It will be encrypted if
764 it came from the router thus it needs to be encrypted with the
765 channel key. If the channel key does not exist, then we know we
766 don't have a single local user on the channel. */
767 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
772 /* Set the packet context pointers. */
773 packetdata.flags = 0;
774 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
775 packetdata.src_id = silc_id_id2str(sender, sender_type);
776 packetdata.src_id_len = silc_id_get_len(sender, sender_type);
777 packetdata.src_id_type = sender_type;
778 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
779 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
780 packetdata.dst_id_type = SILC_ID_CHANNEL;
782 /* If there are global users in the channel we will send the message
783 first to our router for further routing. */
784 if (server->server_type != SILC_ROUTER && !server->standalone &&
785 channel->global_users) {
786 SilcServerEntry router = server->router;
788 /* Check that the sender is not our router. */
789 if (sender_sock != (SilcSocketConnection)router->connection) {
791 /* Get data used in packet header encryption, keys and stuff. */
792 sock = (SilcSocketConnection)router->connection;
793 idata = (SilcIDListData)router;
795 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
797 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
801 data, data_len, TRUE,
806 routed = silc_calloc(silc_hash_table_count(channel->user_list),
809 /* Mark that to the route the original sender if from is not routed */
810 if (sender_type == SILC_ID_CLIENT) {
811 client = (SilcClientEntry)sender_entry;
812 if (client->router) {
813 routed[routed_count++] = client->router;
814 SILC_LOG_DEBUG(("************* router %s",
815 silc_id_render(client->router->id, SILC_ID_SERVER)));
819 /* Send the message to clients on the channel's client list. */
820 silc_hash_table_list(channel->user_list, &htl);
821 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
822 client = chl->client;
826 /* Do not send to the sender */
827 if (!found && client == sender_entry) {
832 /* If the client has set router it means that it is not locally
833 connected client and we will route the packet further. */
834 if (server->server_type == SILC_ROUTER && client->router) {
836 /* Sender maybe server as well so we want to make sure that
837 we won't send the message to the server it came from. */
838 if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
840 routed[routed_count++] = client->router;
844 /* Check if we have sent the packet to this route already */
845 for (k = 0; k < routed_count; k++)
846 if (routed[k] == client->router)
848 if (k < routed_count)
851 /* Get data used in packet header encryption, keys and stuff. */
852 sock = (SilcSocketConnection)client->router->connection;
853 idata = (SilcIDListData)client->router;
855 /* Do not send to the sender. Check first whether the true
856 sender's router is same as this client's router. Also check
857 if the sender socket is the same as this client's router
860 ((SilcClientEntry)sender_entry)->router == client->router)
862 if (sender_sock && sock == sender_sock)
865 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
866 silc_id_render(client->id, SILC_ID_CLIENT),
867 sock->hostname, sock->ip));
869 /* Mark this route routed already. */
870 routed[routed_count++] = client->router;
872 /* If the remote connection is router then we'll decrypt the
873 channel message and re-encrypt it with the session key shared
874 between us and the remote router. This is done because the
875 channel keys are cell specific and we have different channel
876 key than the remote router has. */
877 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
881 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
884 /* If private key mode is not set then decrypt the packet
886 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
887 unsigned char *tmp = silc_memdup(data, data_len);
889 /* Decrypt the channel message (we don't check the MAC) */
890 if (channel->channel_key &&
891 !silc_channel_message_payload_decrypt(tmp, data_len,
892 channel->channel_key,
894 memset(tmp, 0, data_len);
899 /* Now re-encrypt and send it to the router */
900 silc_server_packet_send_srcdest(server, sock,
901 SILC_PACKET_CHANNEL_MESSAGE, 0,
903 channel->id, SILC_ID_CHANNEL,
904 tmp, data_len, force_send);
906 /* Free the copy of the channel message */
907 memset(tmp, 0, data_len);
910 /* Private key mode is set, we don't have the channel key, so
911 just re-encrypt the entire packet and send it to the router. */
912 silc_server_packet_send_srcdest(server, sock,
913 SILC_PACKET_CHANNEL_MESSAGE, 0,
915 channel->id, SILC_ID_CHANNEL,
916 data, data_len, force_send);
921 /* Send the packet (to normal server) */
922 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
926 data, data_len, TRUE,
935 /* Get data used in packet header encryption, keys and stuff. */
936 sock = (SilcSocketConnection)client->connection;
937 idata = (SilcIDListData)client;
939 if (!sock || (sender_sock && sock == sender_sock))
942 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
943 silc_id_render(client->id, SILC_ID_CLIENT),
944 sock->hostname, sock->ip));
946 /* Send the packet */
947 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
951 data, data_len, TRUE,
955 silc_hash_table_list_reset(&htl);
957 silc_free(packetdata.src_id);
958 silc_free(packetdata.dst_id);
961 /* This function is used to send packets strictly to all local clients
962 on a particular channel. This is used for example to distribute new
963 channel key to all our locally connected clients on the channel.
964 The packets are always encrypted with the session key shared between
965 the client, this means these are not _to the channel_ but _to the client_
968 void silc_server_packet_send_local_channel(SilcServer server,
969 SilcChannelEntry channel,
971 SilcPacketFlags flags,
976 SilcChannelClientEntry chl;
977 SilcHashTableList htl;
978 SilcSocketConnection sock = NULL;
980 SILC_LOG_DEBUG(("Start"));
982 /* Send the message to clients on the channel's client list. */
983 silc_hash_table_list(channel->user_list, &htl);
984 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
985 if (chl->client && !chl->client->router) {
986 sock = (SilcSocketConnection)chl->client->connection;
988 /* Send the packet to the client */
989 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
990 SILC_ID_CLIENT, data, data_len,
994 silc_hash_table_list_reset(&htl);
997 /* Routine used to send (relay, route) private messages to some destination.
998 If the private message key does not exist then the message is re-encrypted,
999 otherwise we just pass it along. This really is not used to send new
1000 private messages (as server does not send them) but to relay received
1001 private messages. */
1003 void silc_server_send_private_message(SilcServer server,
1004 SilcSocketConnection dst_sock,
1007 SilcUInt32 sequence,
1008 SilcPacketContext *packet)
1010 SilcBuffer buffer = packet->buffer;
1012 /* Re-encrypt and send if private messge key does not exist */
1013 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
1015 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1016 + packet->dst_id_len + packet->padlen);
1017 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1018 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1020 /* Re-encrypt packet */
1021 silc_packet_encrypt(cipher, hmac, sequence, dst_sock->outbuf, buffer->len);
1023 /* Send the packet */
1024 silc_server_packet_send_real(server, dst_sock, FALSE);
1027 /* Key exist so encrypt just header and send it */
1028 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1029 + packet->dst_id_len + packet->padlen);
1030 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
1031 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
1033 /* Encrypt header */
1034 silc_packet_encrypt(cipher, hmac, sequence, dst_sock->outbuf,
1035 SILC_PACKET_HEADER_LEN + packet->src_id_len +
1036 packet->dst_id_len + packet->padlen);
1038 silc_server_packet_send_real(server, dst_sock, FALSE);
1042 /* Sends current motd to client */
1044 void silc_server_send_motd(SilcServer server,
1045 SilcSocketConnection sock)
1047 char *motd, *motd_file = NULL;
1048 SilcUInt32 motd_len;
1051 motd_file = server->config->server_info->motd_file;
1054 motd = silc_file_readfile(motd_file, &motd_len);
1058 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1064 /* Sends error message. Error messages may or may not have any
1067 void silc_server_send_error(SilcServer server,
1068 SilcSocketConnection sock,
1069 const char *fmt, ...)
1072 unsigned char buf[4096];
1074 memset(buf, 0, sizeof(buf));
1076 vsprintf(buf, fmt, ap);
1079 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
1080 buf, strlen(buf), FALSE);
1083 /* Sends notify message. If format is TRUE the variable arguments are
1084 formatted and the formatted string is sent as argument payload. If it is
1085 FALSE then each argument is sent as separate argument and their format
1086 in the argument list must be { argument data, argument length }. */
1088 void silc_server_send_notify(SilcServer server,
1089 SilcSocketConnection sock,
1091 SilcNotifyType type,
1092 SilcUInt32 argc, ...)
1099 packet = silc_notify_payload_encode(type, argc, ap);
1100 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1101 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1102 packet->data, packet->len, FALSE);
1104 /* Send to backup routers if this is being broadcasted to primary
1106 if (server->router && server->router->connection &&
1107 sock == server->router->connection && broadcast)
1108 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1109 packet->data, packet->len, FALSE, TRUE);
1111 silc_buffer_free(packet);
1115 /* Sends notify message and gets the arguments from the `args' Argument
1118 void silc_server_send_notify_args(SilcServer server,
1119 SilcSocketConnection sock,
1121 SilcNotifyType type,
1127 packet = silc_notify_payload_encode_args(type, argc, args);
1128 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
1129 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1130 packet->data, packet->len, FALSE);
1131 silc_buffer_free(packet);
1134 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1135 `old_id' with the `new_id'. */
1137 void silc_server_send_notify_channel_change(SilcServer server,
1138 SilcSocketConnection sock,
1140 SilcChannelID *old_id,
1141 SilcChannelID *new_id)
1143 SilcBuffer idp1, idp2;
1145 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1146 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1148 silc_server_send_notify(server, sock, broadcast,
1149 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1150 2, idp1->data, idp1->len, idp2->data, idp2->len);
1151 silc_buffer_free(idp1);
1152 silc_buffer_free(idp2);
1155 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1156 `old_id' with the `new_id'. */
1158 void silc_server_send_notify_nick_change(SilcServer server,
1159 SilcSocketConnection sock,
1161 SilcClientID *old_id,
1162 SilcClientID *new_id)
1164 SilcBuffer idp1, idp2;
1166 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1167 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1169 silc_server_send_notify(server, sock, broadcast,
1170 SILC_NOTIFY_TYPE_NICK_CHANGE,
1171 2, idp1->data, idp1->len, idp2->data, idp2->len);
1172 silc_buffer_free(idp1);
1173 silc_buffer_free(idp2);
1176 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1177 has joined to the `channel'. */
1179 void silc_server_send_notify_join(SilcServer server,
1180 SilcSocketConnection sock,
1182 SilcChannelEntry channel,
1183 SilcClientID *client_id)
1185 SilcBuffer idp1, idp2;
1187 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1188 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1189 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1190 2, idp1->data, idp1->len,
1191 idp2->data, idp2->len);
1192 silc_buffer_free(idp1);
1193 silc_buffer_free(idp2);
1196 /* Sends LEAVE notify type. This tells that `client_id' has left the
1197 `channel'. The Notify packet is always destined to the channel. */
1199 void silc_server_send_notify_leave(SilcServer server,
1200 SilcSocketConnection sock,
1202 SilcChannelEntry channel,
1203 SilcClientID *client_id)
1207 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1208 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1209 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1210 1, idp->data, idp->len);
1211 silc_buffer_free(idp);
1214 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1215 `channel' mode to `mode. The Notify packet is always destined to
1218 void silc_server_send_notify_cmode(SilcServer server,
1219 SilcSocketConnection sock,
1221 SilcChannelEntry channel,
1222 SilcUInt32 mode_mask,
1223 void *id, SilcIdType id_type,
1224 char *cipher, char *hmac,
1228 unsigned char mode[4];
1230 idp = silc_id_payload_encode((void *)id, id_type);
1231 SILC_PUT32_MSB(mode_mask, mode);
1233 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1234 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1235 5, idp->data, idp->len,
1237 cipher, cipher ? strlen(cipher) : 0,
1238 hmac, hmac ? strlen(hmac) : 0,
1239 passphrase, passphrase ?
1240 strlen(passphrase) : 0);
1241 silc_buffer_free(idp);
1244 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1245 `target' client's mode on `channel'. The notify packet is always
1246 destined to the channel. */
1248 void silc_server_send_notify_cumode(SilcServer server,
1249 SilcSocketConnection sock,
1251 SilcChannelEntry channel,
1252 SilcUInt32 mode_mask,
1253 void *id, SilcIdType id_type,
1254 SilcClientID *target)
1256 SilcBuffer idp1, idp2;
1257 unsigned char mode[4];
1259 idp1 = silc_id_payload_encode((void *)id, id_type);
1260 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1261 SILC_PUT32_MSB(mode_mask, mode);
1263 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1265 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
1266 idp1->data, idp1->len,
1268 idp2->data, idp2->len);
1269 silc_buffer_free(idp1);
1270 silc_buffer_free(idp2);
1273 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1274 left SILC network. This function is used only between server and router
1275 traffic. This is not used to send the notify to the channel for
1276 client. The `message may be NULL. */
1278 void silc_server_send_notify_signoff(SilcServer server,
1279 SilcSocketConnection sock,
1281 SilcClientID *client_id,
1282 const char *message)
1286 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1287 silc_server_send_notify(server, sock, broadcast,
1288 SILC_NOTIFY_TYPE_SIGNOFF,
1289 message ? 2 : 1, idp->data, idp->len,
1290 message, message ? strlen(message): 0);
1291 silc_buffer_free(idp);
1294 /* Sends TOPIC_SET notify type. This tells that `id' changed
1295 the `channel's topic to `topic'. The Notify packet is always destined
1296 to the channel. This function is used to send the topic set notifies
1299 void silc_server_send_notify_topic_set(SilcServer server,
1300 SilcSocketConnection sock,
1302 SilcChannelEntry channel,
1303 void *id, SilcIdType id_type,
1308 idp = silc_id_payload_encode(id, id_type);
1309 silc_server_send_notify_dest(server, sock, broadcast,
1310 (void *)channel->id, SILC_ID_CHANNEL,
1311 SILC_NOTIFY_TYPE_TOPIC_SET,
1313 idp->data, idp->len,
1314 topic, topic ? strlen(topic) : 0);
1315 silc_buffer_free(idp);
1318 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1319 was kicked off the channel. The `comment' may indicate the reason
1320 for the kicking. This function is used only between server and router
1323 void silc_server_send_notify_kicked(SilcServer server,
1324 SilcSocketConnection sock,
1326 SilcChannelEntry channel,
1327 SilcClientID *client_id,
1328 SilcClientID *kicker,
1334 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1335 idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1336 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1337 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1338 idp1->data, idp1->len,
1339 comment, comment ? strlen(comment) : 0,
1340 idp2->data, idp2->len);
1341 silc_buffer_free(idp1);
1342 silc_buffer_free(idp2);
1345 /* Send KILLED notify type. This tells that the `client_id' client was
1346 killed from the network. The `comment' may indicate the reason
1349 void silc_server_send_notify_killed(SilcServer server,
1350 SilcSocketConnection sock,
1352 SilcClientID *client_id,
1357 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1358 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1359 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1360 comment ? 2 : 1, idp->data, idp->len,
1361 comment, comment ? strlen(comment) : 0);
1362 silc_buffer_free(idp);
1365 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1366 user mode in the SILC Network was changed. This function is used to
1367 send the packet between routers as broadcast packet. */
1369 void silc_server_send_notify_umode(SilcServer server,
1370 SilcSocketConnection sock,
1372 SilcClientID *client_id,
1373 SilcUInt32 mode_mask)
1376 unsigned char mode[4];
1378 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1379 SILC_PUT32_MSB(mode_mask, mode);
1381 silc_server_send_notify(server, sock, broadcast,
1382 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1383 idp->data, idp->len,
1385 silc_buffer_free(idp);
1388 /* Sends BAN notify type. This tells that ban has been either `add'ed
1389 or `del'eted on the `channel. This function is used to send the packet
1390 between routers as broadcast packet. */
1392 void silc_server_send_notify_ban(SilcServer server,
1393 SilcSocketConnection sock,
1395 SilcChannelEntry channel,
1396 char *add, char *del)
1400 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1401 silc_server_send_notify(server, sock, broadcast,
1402 SILC_NOTIFY_TYPE_BAN, 3,
1403 idp->data, idp->len,
1404 add, add ? strlen(add) : 0,
1405 del, del ? strlen(del) : 0);
1406 silc_buffer_free(idp);
1409 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1410 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1411 This function is used to send the packet between routers as broadcast
1414 void silc_server_send_notify_invite(SilcServer server,
1415 SilcSocketConnection sock,
1417 SilcChannelEntry channel,
1418 SilcClientID *client_id,
1419 char *add, char *del)
1421 SilcBuffer idp, idp2;
1423 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1424 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1425 silc_server_send_notify(server, sock, broadcast,
1426 SILC_NOTIFY_TYPE_INVITE, 5,
1427 idp->data, idp->len,
1428 channel->channel_name, strlen(channel->channel_name),
1429 idp2->data, idp2->len,
1430 add, add ? strlen(add) : 0,
1431 del, del ? strlen(del) : 0);
1432 silc_buffer_free(idp);
1433 silc_buffer_free(idp2);
1436 /* Sends notify message destined to specific entity. */
1438 void silc_server_send_notify_dest(SilcServer server,
1439 SilcSocketConnection sock,
1442 SilcIdType dest_id_type,
1443 SilcNotifyType type,
1444 SilcUInt32 argc, ...)
1451 packet = silc_notify_payload_encode(type, argc, ap);
1452 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY,
1453 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1454 dest_id, dest_id_type,
1455 packet->data, packet->len, FALSE);
1456 silc_buffer_free(packet);
1460 /* Sends notify message to a channel. The notify message sent is
1461 distributed to all clients on the channel. If `route_notify' is TRUE
1462 then the notify may be routed to primary route or to some other routers.
1463 If FALSE it is assured that the notify is sent only locally. If `sender'
1464 is provided then the packet is not sent to that connection since it
1465 originally came from it. */
1467 void silc_server_send_notify_to_channel(SilcServer server,
1468 SilcSocketConnection sender,
1469 SilcChannelEntry channel,
1471 SilcNotifyType type,
1472 SilcUInt32 argc, ...)
1479 packet = silc_notify_payload_encode(type, argc, ap);
1480 silc_server_packet_send_to_channel(server, sender, channel,
1481 SILC_PACKET_NOTIFY, route_notify,
1482 packet->data, packet->len, FALSE);
1483 silc_buffer_free(packet);
1487 /* Send notify message to all channels the client has joined. It is quaranteed
1488 that the message is sent only once to a client (ie. if a client is joined
1489 on two same channel it will receive only one notify message). Also, this
1490 sends only to local clients (locally connected if we are server, and to
1491 local servers if we are router). If `sender' is provided the packet is
1492 not sent to that client at all. */
1494 void silc_server_send_notify_on_channels(SilcServer server,
1495 SilcClientEntry sender,
1496 SilcClientEntry client,
1497 SilcNotifyType type,
1498 SilcUInt32 argc, ...)
1501 SilcSocketConnection sock = NULL;
1502 SilcPacketContext packetdata;
1504 SilcClientEntry *sent_clients = NULL;
1505 SilcUInt32 sent_clients_count = 0;
1506 SilcServerEntry *routed = NULL;
1507 SilcUInt32 routed_count = 0;
1508 SilcHashTableList htl, htl2;
1509 SilcChannelEntry channel;
1510 SilcChannelClientEntry chl, chl2;
1511 SilcIDListData idata;
1513 unsigned char *data;
1514 SilcUInt32 data_len;
1515 bool force_send = FALSE;
1518 SILC_LOG_DEBUG(("Start"));
1520 if (!silc_hash_table_count(client->channels))
1524 packet = silc_notify_payload_encode(type, argc, ap);
1525 data = packet->data;
1526 data_len = packet->len;
1528 /* Set the packet context pointers. */
1529 packetdata.flags = 0;
1530 packetdata.type = SILC_PACKET_NOTIFY;
1531 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1532 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1533 packetdata.src_id_type = SILC_ID_SERVER;
1535 silc_hash_table_list(client->channels, &htl);
1536 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1537 channel = chl->channel;
1539 /* Send the message to all clients on the channel's client list. */
1540 silc_hash_table_list(channel->user_list, &htl2);
1541 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1544 if (sender && c == sender)
1547 /* Check if we have sent the packet to this client already */
1548 for (k = 0; k < sent_clients_count; k++)
1549 if (sent_clients[k] == c)
1551 if (k < sent_clients_count)
1554 /* If we are router and if this client has router set it is not
1555 locally connected client and we will route the message to the
1556 router set in the client. */
1557 if (c && c->router && server->server_type == SILC_ROUTER) {
1558 /* Check if we have sent the packet to this route already */
1559 for (k = 0; k < routed_count; k++)
1560 if (routed[k] == c->router)
1562 if (k < routed_count)
1565 /* Get data used in packet header encryption, keys and stuff. */
1566 sock = (SilcSocketConnection)c->router->connection;
1567 idata = (SilcIDListData)c->router;
1570 SILC_LOG_DEBUG(("*****************"));
1571 SILC_LOG_DEBUG(("client->router->id %s",
1572 silc_id_render(c->router->id, SILC_ID_SERVER)));
1573 SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1574 silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1577 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1578 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1579 packetdata.dst_id_type = SILC_ID_SERVER;
1581 /* Send the packet */
1582 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1586 data, data_len, FALSE,
1589 silc_free(packetdata.dst_id);
1591 /* We want to make sure that the packet is routed to same router
1592 only once. Mark this route as sent route. */
1593 routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1594 routed[routed_count++] = c->router;
1601 /* Send to locally connected client */
1604 /* Get data used in packet header encryption, keys and stuff. */
1605 sock = (SilcSocketConnection)c->connection;
1606 idata = (SilcIDListData)c;
1611 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1612 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1613 packetdata.dst_id_type = SILC_ID_CLIENT;
1615 /* Send the packet */
1616 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1620 data, data_len, FALSE,
1623 silc_free(packetdata.dst_id);
1625 /* Make sure that we send the notify only once per client. */
1626 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1627 (sent_clients_count + 1));
1628 sent_clients[sent_clients_count++] = c;
1631 silc_hash_table_list_reset(&htl2);
1634 silc_hash_table_list_reset(&htl);
1636 silc_free(sent_clients);
1637 silc_free(packetdata.src_id);
1638 silc_buffer_free(packet);
1642 /* Sends New ID Payload to remote end. The packet is used to distribute
1643 information about new registered clients, servers, channel etc. usually
1644 to routers so that they can keep these information up to date.
1645 If the argument `broadcast' is TRUE then the packet is sent as
1646 broadcast packet. */
1648 void silc_server_send_new_id(SilcServer server,
1649 SilcSocketConnection sock,
1651 void *id, SilcIdType id_type,
1656 SILC_LOG_DEBUG(("Start"));
1658 idp = silc_id_payload_encode(id, id_type);
1659 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1660 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1661 idp->data, idp->len, FALSE);
1663 /* Send to backup routers if this is being broadcasted to primary
1665 if (server->router && server->router->connection &&
1666 sock == server->router->connection && broadcast)
1667 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
1668 idp->data, idp->len, FALSE, TRUE);
1670 silc_buffer_free(idp);
1673 /* Send New Channel Payload to notify about newly created channel in the
1674 SILC network. Router uses this to notify other routers in the network
1675 about new channel. This packet is broadcasted by router. */
1677 void silc_server_send_new_channel(SilcServer server,
1678 SilcSocketConnection sock,
1682 SilcUInt32 channel_id_len,
1687 SilcUInt32 name_len = strlen(channel_name);
1689 SILC_LOG_DEBUG(("Start"));
1691 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1695 /* Encode the channel payload */
1696 packet = silc_channel_payload_encode(channel_name, name_len,
1697 cid, channel_id_len, mode);
1699 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1700 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1701 packet->data, packet->len, FALSE);
1703 /* Send to backup routers if this is being broadcasted to primary
1705 if (server->server_type == SILC_ROUTER &&
1706 server->router && server->router->connection &&
1707 sock == server->router->connection && broadcast)
1708 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
1709 packet->data, packet->len, FALSE, TRUE);
1712 silc_buffer_free(packet);
1715 /* Send Channel Key payload to distribute the new channel key. Normal server
1716 sends this to router when new client joins to existing channel. Router
1717 sends this to the local server who sent the join command in case where
1718 the channel did not exist yet. Both normal and router servers uses this
1719 also to send this to locally connected clients on the channel. This
1720 must not be broadcasted packet. Routers do not send this to each other.
1721 If `sender is provided then the packet is not sent to that connection since
1722 it originally came from it. */
1724 void silc_server_send_channel_key(SilcServer server,
1725 SilcSocketConnection sender,
1726 SilcChannelEntry channel,
1727 unsigned char route)
1730 unsigned char *chid;
1733 SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1735 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1739 /* Encode channel key packet */
1740 tmp_len = strlen(channel->channel_key->cipher->name);
1741 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1744 channel->channel_key->cipher->name,
1745 channel->key_len / 8, channel->key);
1746 silc_server_packet_send_to_channel(server, sender, channel,
1747 SILC_PACKET_CHANNEL_KEY,
1748 route, packet->data, packet->len,
1750 silc_buffer_free(packet);
1754 /* Generic function to send any command. The arguments must be sent already
1755 encoded into correct form in correct order. */
1757 void silc_server_send_command(SilcServer server,
1758 SilcSocketConnection sock,
1759 SilcCommand command,
1761 SilcUInt32 argc, ...)
1768 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1769 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1770 packet->data, packet->len, TRUE);
1771 silc_buffer_free(packet);
1775 /* Generic function to send any command reply. The arguments must be sent
1776 already encoded into correct form in correct order. */
1778 void silc_server_send_command_reply(SilcServer server,
1779 SilcSocketConnection sock,
1780 SilcCommand command,
1781 SilcCommandStatus status,
1783 SilcUInt32 argc, ...)
1790 packet = silc_command_reply_payload_encode_vap(command, status, ident,
1792 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1793 packet->data, packet->len, TRUE);
1794 silc_buffer_free(packet);
1798 /* Generic function to send any command reply. The arguments must be sent
1799 already encoded into correct form in correct order. */
1801 void silc_server_send_dest_command_reply(SilcServer server,
1802 SilcSocketConnection sock,
1804 SilcIdType dst_id_type,
1805 SilcCommand command,
1806 SilcCommandStatus status,
1808 SilcUInt32 argc, ...)
1815 packet = silc_command_reply_payload_encode_vap(command, status, ident,
1817 silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1818 dst_id, dst_id_type, packet->data,
1820 silc_buffer_free(packet);
1824 /* Send the heartbeat packet. */
1826 void silc_server_send_heartbeat(SilcServer server,
1827 SilcSocketConnection sock)
1829 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1833 /* Generic function to relay packet we've received. This is used to relay
1834 packets to a client but generally can be used to other purposes as well. */
1836 void silc_server_relay_packet(SilcServer server,
1837 SilcSocketConnection dst_sock,
1840 SilcUInt32 sequence,
1841 SilcPacketContext *packet,
1844 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1845 + packet->dst_id_len + packet->padlen);
1847 silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1848 silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1850 /* Re-encrypt packet */
1851 silc_packet_encrypt(cipher, hmac, sequence, dst_sock->outbuf,
1852 packet->buffer->len);
1854 /* Send the packet */
1855 silc_server_packet_send_real(server, dst_sock, force_send);
1857 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1858 + packet->dst_id_len + packet->padlen);
1861 /* Routine used to send the connection authentication packet. */
1863 void silc_server_send_connection_auth_request(SilcServer server,
1864 SilcSocketConnection sock,
1865 SilcUInt16 conn_type,
1866 SilcAuthMethod auth_meth)
1870 packet = silc_buffer_alloc(4);
1871 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1872 silc_buffer_format(packet,
1873 SILC_STR_UI_SHORT(conn_type),
1874 SILC_STR_UI_SHORT(auth_meth),
1877 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1878 0, packet->data, packet->len, FALSE);
1879 silc_buffer_free(packet);
1882 /* Purge the outgoing packet queue to the network if there is data. This
1883 function can be used to empty the packet queue. It is guaranteed that
1884 after this function returns the outgoing data queue is empty. */
1886 void silc_server_packet_queue_purge(SilcServer server,
1887 SilcSocketConnection sock)
1889 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1890 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1891 server->stat.packets_sent++;
1893 if (sock->outbuf->data - sock->outbuf->head)
1894 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1896 silc_packet_send(sock, TRUE);
1898 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1899 SILC_UNSET_OUTBUF_PENDING(sock);
1900 silc_buffer_clear(sock->outbuf);