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 unsigned char *dst_id_data = NULL;
144 uint32 dst_id_len = 0;
146 /* If disconnecting, ignore the data */
147 if (SILC_IS_DISCONNECTING(sock))
150 /* If entry is disabled do not sent anything. */
151 if (idata && idata->status & SILC_IDLIST_STATUS_DISABLED)
154 SILC_LOG_DEBUG(("Sending packet, type %d", type));
157 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
158 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
161 /* Set the packet context pointers */
162 packetdata.type = type;
163 packetdata.flags = flags;
164 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
165 packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
166 packetdata.src_id_type = server->id_type;
167 packetdata.dst_id = dst_id_data;
168 packetdata.dst_id_len = dst_id_len;
169 packetdata.dst_id_type = dst_id_type;
170 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
171 packetdata.src_id_len + dst_id_len;
172 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
174 /* Prepare outgoing data buffer for packet sending */
175 silc_packet_send_prepare(sock,
176 SILC_PACKET_HEADER_LEN +
177 packetdata.src_id_len +
178 packetdata.dst_id_len,
182 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
184 packetdata.buffer = sock->outbuf;
186 /* Put the data to the buffer */
187 if (data && data_len)
188 silc_buffer_put(sock->outbuf, data, data_len);
190 /* Create the outgoing packet */
191 silc_packet_assemble(&packetdata);
194 cipher = idata->send_key;
195 hmac = idata->hmac_send;
198 /* Encrypt the packet */
199 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
201 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
202 sock->outbuf->data, sock->outbuf->len);
204 /* Now actually send the packet */
205 silc_server_packet_send_real(server, sock, force_send);
207 if (packetdata.src_id)
208 silc_free(packetdata.src_id);
209 if (packetdata.dst_id)
210 silc_free(packetdata.dst_id);
213 /* Assembles a new packet to be sent out to network. This doesn't actually
214 send the packet but creates the packet and fills the outgoing data
215 buffer and marks the packet ready to be sent to network. However, If
216 argument force_send is TRUE the packet is sent immediately and not put
217 to queue. Normal case is that the packet is not sent immediately.
218 The source and destination information is sent as argument for this
221 void silc_server_packet_send_srcdest(SilcServer server,
222 SilcSocketConnection sock,
224 SilcPacketFlags flags,
226 SilcIdType src_id_type,
228 SilcIdType dst_id_type,
233 SilcPacketContext packetdata;
234 SilcIDListData idata;
235 SilcCipher cipher = NULL;
236 SilcHmac hmac = NULL;
237 unsigned char *dst_id_data = NULL;
238 uint32 dst_id_len = 0;
239 unsigned char *src_id_data = NULL;
240 uint32 src_id_len = 0;
242 SILC_LOG_DEBUG(("Sending packet, type %d", type));
244 /* Get data used in the packet sending, keys and stuff */
245 idata = (SilcIDListData)sock->user_data;
248 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
249 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
253 src_id_data = silc_id_id2str(src_id, src_id_type);
254 src_id_len = silc_id_get_len(src_id, src_id_type);
257 /* Set the packet context pointers */
258 packetdata.type = type;
259 packetdata.flags = flags;
260 packetdata.src_id = src_id_data;
261 packetdata.src_id_len = src_id_len;
262 packetdata.src_id_type = src_id_type;
263 packetdata.dst_id = dst_id_data;
264 packetdata.dst_id_len = dst_id_len;
265 packetdata.dst_id_type = dst_id_type;
266 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
267 packetdata.src_id_len + dst_id_len;
268 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
270 /* Prepare outgoing data buffer for packet sending */
271 silc_packet_send_prepare(sock,
272 SILC_PACKET_HEADER_LEN +
273 packetdata.src_id_len +
274 packetdata.dst_id_len,
278 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
280 packetdata.buffer = sock->outbuf;
282 /* Put the data to the buffer */
283 if (data && data_len)
284 silc_buffer_put(sock->outbuf, data, data_len);
286 /* Create the outgoing packet */
287 silc_packet_assemble(&packetdata);
290 cipher = idata->send_key;
291 hmac = idata->hmac_send;
294 /* Encrypt the packet */
295 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
297 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
298 sock->outbuf->data, sock->outbuf->len);
300 /* Now actually send the packet */
301 silc_server_packet_send_real(server, sock, force_send);
303 if (packetdata.src_id)
304 silc_free(packetdata.src_id);
305 if (packetdata.dst_id)
306 silc_free(packetdata.dst_id);
309 /* Broadcast received packet to our primary route. This function is used
310 by router to further route received broadcast packet. It is expected
311 that the broadcast flag from the packet is checked before calling this
312 function. This does not test or set the broadcast flag. */
314 void silc_server_packet_broadcast(SilcServer server,
315 SilcSocketConnection sock,
316 SilcPacketContext *packet)
318 SilcBuffer buffer = packet->buffer;
319 SilcIDListData idata;
322 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
324 /* If the packet is originated from our primary route we are
325 not allowed to send the packet. */
326 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
327 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
328 idata = (SilcIDListData)sock->user_data;
330 silc_buffer_push(buffer, buffer->data - buffer->head);
331 silc_packet_send_prepare(sock, 0, 0, buffer->len);
332 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
333 silc_packet_encrypt(idata->send_key, idata->hmac_send,
334 sock->outbuf, sock->outbuf->len);
336 SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
337 sock->outbuf->data, sock->outbuf->len);
339 /* Now actually send the packet */
340 silc_server_packet_send_real(server, sock, TRUE);
345 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
346 "original sender of this packet"));
350 /* Routes received packet to `sock'. This is used to route the packets that
351 router receives but are not destined to it. */
353 void silc_server_packet_route(SilcServer server,
354 SilcSocketConnection sock,
355 SilcPacketContext *packet)
357 SilcBuffer buffer = packet->buffer;
358 SilcIDListData idata;
360 SILC_LOG_DEBUG(("Routing received packet"));
362 idata = (SilcIDListData)sock->user_data;
364 silc_buffer_push(buffer, buffer->data - buffer->head);
365 silc_packet_send_prepare(sock, 0, 0, buffer->len);
366 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
367 silc_packet_encrypt(idata->send_key, idata->hmac_send,
368 sock->outbuf, sock->outbuf->len);
370 SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
371 sock->outbuf->data, sock->outbuf->len);
373 /* Now actually send the packet */
374 silc_server_packet_send_real(server, sock, TRUE);
377 /* Internal routine to actually create the channel packet and send it
378 to network. This is common function in channel message sending. If
379 `channel_message' is TRUE this encrypts the message as it is strictly
380 a channel message. If FALSE normal encryption process is used. */
383 silc_server_packet_send_to_channel_real(SilcServer server,
384 SilcSocketConnection sock,
385 SilcPacketContext *packet,
393 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
394 packet->src_id_len + packet->dst_id_len;
396 /* Prepare outgoing data buffer for packet sending */
397 silc_packet_send_prepare(sock,
398 SILC_PACKET_HEADER_LEN +
404 packet->buffer = sock->outbuf;
406 /* Put the data to buffer, assemble and encrypt the packet. The packet
407 is encrypted with normal session key shared with the client, unless
408 the `channel_message' is TRUE. */
409 silc_buffer_put(sock->outbuf, data, data_len);
410 silc_packet_assemble(packet);
412 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
413 packet->src_id_len + packet->dst_id_len +
416 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
418 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
419 sock->outbuf->data, sock->outbuf->len);
421 /* Now actually send the packet */
422 silc_server_packet_send_real(server, sock, force_send);
425 /* This routine is used by the server to send packets to channel. The
426 packet sent with this function is distributed to all clients on
427 the channel. Usually this is used to send notify messages to the
428 channel, things like notify about new user joining to the channel.
429 If `route' is FALSE then the packet is sent only locally and will not
430 be routed anywhere (for router locally means cell wide). If `sender'
431 is provided then the packet is not sent to that connection since it
432 originally came from it. */
434 void silc_server_packet_send_to_channel(SilcServer server,
435 SilcSocketConnection sender,
436 SilcChannelEntry channel,
443 SilcSocketConnection sock = NULL;
444 SilcPacketContext packetdata;
445 SilcClientEntry client = NULL;
446 SilcServerEntry *routed = NULL;
447 SilcChannelClientEntry chl;
448 SilcHashTableList htl;
449 SilcIDListData idata;
450 uint32 routed_count = 0;
452 /* This doesn't send channel message packets */
453 assert(type != SILC_PACKET_CHANNEL_MESSAGE);
455 SILC_LOG_DEBUG(("Sending packet to channel"));
457 /* Set the packet context pointers. */
458 packetdata.flags = 0;
459 packetdata.type = type;
460 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
461 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
462 packetdata.src_id_type = SILC_ID_SERVER;
463 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
464 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
465 packetdata.dst_id_type = SILC_ID_CHANNEL;
466 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
467 packetdata.src_id_len + packetdata.dst_id_len;
468 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
470 /* If there are global users in the channel we will send the message
471 first to our router for further routing. */
472 if (route && server->server_type != SILC_ROUTER && !server->standalone &&
473 channel->global_users) {
474 SilcServerEntry router;
476 /* Get data used in packet header encryption, keys and stuff. */
477 router = server->router;
478 sock = (SilcSocketConnection)router->connection;
479 idata = (SilcIDListData)router;
481 if (sock != sender) {
482 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
484 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
487 data, data_len, FALSE,
492 /* Send the message to clients on the channel's client list. */
493 silc_hash_table_list(channel->user_list, &htl);
494 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
495 client = chl->client;
497 /* If client has router set it is not locally connected client and
498 we will route the message to the router set in the client. Though,
499 send locally connected server in all cases. */
500 if (server->server_type == SILC_ROUTER && client && client->router &&
501 ((!route && client->router->router == server->id_entry) || route)) {
504 /* Check if we have sent the packet to this route already */
505 for (k = 0; k < routed_count; k++)
506 if (routed[k] == client->router)
508 if (k < routed_count)
511 /* Get data used in packet header encryption, keys and stuff. */
512 sock = (SilcSocketConnection)client->router->connection;
513 idata = (SilcIDListData)client->router;
515 if (sender && sock == sender)
518 /* Send the packet */
519 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
522 data, data_len, FALSE,
525 /* We want to make sure that the packet is routed to same router
526 only once. Mark this route as sent route. */
528 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
529 routed[k] = client->router;
535 if (client && client->router)
538 /* Send to locally connected client */
541 /* Get data used in packet header encryption, keys and stuff. */
542 sock = (SilcSocketConnection)client->connection;
543 idata = (SilcIDListData)client;
545 if (sender && sock == sender)
548 /* Send the packet */
549 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
552 data, data_len, FALSE,
559 silc_free(packetdata.src_id);
560 silc_free(packetdata.dst_id);
563 /* This checks whether the relayed packet came from router. If it did
564 then we'll need to encrypt it with the channel key. This is called
565 from the silc_server_packet_relay_to_channel. */
568 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
569 SilcSocketConnection sock,
570 SilcChannelEntry channel,
572 unsigned int data_len)
574 /* If we are router and the packet came from router and private key
575 has not been set for the channel then we must encrypt the packet
576 as it was decrypted with the session key shared between us and the
577 router which sent it. This is so, because cells does not share the
579 if (server->server_type == SILC_ROUTER &&
580 sock->type == SILC_SOCKET_TYPE_ROUTER &&
581 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
582 channel->channel_key) {
587 iv_len = silc_cipher_get_block_len(channel->channel_key);
588 if (channel->iv[0] == '\0')
589 for (i = 0; i < iv_len; i++) channel->iv[i] =
590 silc_rng_get_byte(server->rng);
592 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
594 /* Encode new payload. This encrypts it also. */
595 SILC_GET16_MSB(flags, data);
596 SILC_GET16_MSB(dlen, data + 2);
598 if (dlen > data_len) {
599 SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
603 chp = silc_channel_message_payload_encode(flags, dlen, data + 4,
605 channel->channel_key,
607 memcpy(data, chp->data, chp->len);
608 silc_buffer_free(chp);
614 /* This routine is explicitly used to relay messages to some channel.
615 Packets sent with this function we have received earlier and are
616 totally encrypted. This just sends the packet to all clients on
617 the channel. If the sender of the packet is someone on the channel
618 the message will not be sent to that client. The SILC Packet header
619 is encrypted with the session key shared between us and the client.
620 MAC is also computed before encrypting the header. Rest of the
621 packet will be untouched. */
623 void silc_server_packet_relay_to_channel(SilcServer server,
624 SilcSocketConnection sender_sock,
625 SilcChannelEntry channel,
627 SilcIdType sender_type,
634 SilcSocketConnection sock = NULL;
635 SilcPacketContext packetdata;
636 SilcClientEntry client = NULL;
637 SilcServerEntry *routed = NULL;
638 SilcChannelClientEntry chl;
639 uint32 routed_count = 0;
640 SilcIDListData idata;
641 SilcHashTableList htl;
643 SILC_LOG_DEBUG(("Relaying packet to channel"));
645 /* This encrypts the packet, if needed. It will be encrypted if
646 it came from the router thus it needs to be encrypted with the
647 channel key. If the channel key does not exist, then we know we
648 don't have a single local user on the channel. */
649 if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
654 /* Set the packet context pointers. */
655 packetdata.flags = 0;
656 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
657 packetdata.src_id = silc_id_id2str(sender, sender_type);
658 packetdata.src_id_len = silc_id_get_len(sender, sender_type);
659 packetdata.src_id_type = sender_type;
660 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
661 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
662 packetdata.dst_id_type = SILC_ID_CHANNEL;
663 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
664 packetdata.src_id_len +
665 packetdata.dst_id_len));
667 /* If there are global users in the channel we will send the message
668 first to our router for further routing. */
669 if (server->server_type != SILC_ROUTER && !server->standalone &&
670 channel->global_users) {
671 SilcServerEntry router;
673 router = server->router;
675 /* Check that the sender is not our router. */
676 if (sender_sock != (SilcSocketConnection)router->connection) {
678 /* Get data used in packet header encryption, keys and stuff. */
679 sock = (SilcSocketConnection)router->connection;
680 idata = (SilcIDListData)router;
682 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
684 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
687 data, data_len, TRUE,
692 /* Send the message to clients on the channel's client list. */
693 silc_hash_table_list(channel->user_list, &htl);
694 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
695 client = chl->client;
699 /* If sender is one on the channel do not send it the packet. */
700 if (!found && sender_type == SILC_ID_CLIENT &&
701 SILC_ID_CLIENT_COMPARE(client->id, sender)) {
706 /* If the client has set router it means that it is not locally
707 connected client and we will route the packet further. */
708 if (server->server_type == SILC_ROUTER && client->router) {
711 /* Sender maybe server as well so we want to make sure that
712 we won't send the message to the server it came from. */
713 if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
718 /* Check if we have sent the packet to this route already */
719 for (k = 0; k < routed_count; k++)
720 if (routed[k] == client->router)
722 if (k < routed_count)
725 /* Get data used in packet header encryption, keys and stuff. */
726 sock = (SilcSocketConnection)client->router->connection;
727 idata = (SilcIDListData)client->router;
729 /* Do not send to the sender. Check first whether the true
730 sender's router is same as this client's router. Also check
731 if the sender socket is the same as this client's router
734 ((SilcClientEntry)sender_entry)->router == client->router)
736 if (sender_sock && sock == sender_sock)
739 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
740 silc_id_render(client->id, SILC_ID_CLIENT),
741 sock->hostname, sock->ip));
743 /* We want to make sure that the packet is routed to same router
744 only once. Mark this route as sent route. */
746 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
747 routed[k] = client->router;
750 /* If the remote connection is router then we'll decrypt the
751 channel message and re-encrypt it with the session key shared
752 between us and the remote router. This is done because the
753 channel keys are cell specific and we have different channel
754 key than the remote router has. */
755 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
756 SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
758 /* If private key mode is not set then decrypt the packet
760 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
761 unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
762 memcpy(tmp, data, data_len);
764 /* Decrypt the channel message (we don't check the MAC) */
765 if (channel->channel_key &&
766 !silc_channel_message_payload_decrypt(tmp, data_len,
767 channel->channel_key,
769 memset(tmp, 0, data_len);
774 /* Now re-encrypt and send it to the router */
775 silc_server_packet_send_srcdest(server, sock,
776 SILC_PACKET_CHANNEL_MESSAGE, 0,
778 channel->id, SILC_ID_CHANNEL,
779 tmp, data_len, force_send);
781 /* Free the copy of the channel message */
782 memset(tmp, 0, data_len);
785 /* Private key mode is set, we don't have the channel key, so
786 just re-encrypt the entire packet and send it to the router. */
787 silc_server_packet_send_srcdest(server, sock,
788 SILC_PACKET_CHANNEL_MESSAGE, 0,
790 channel->id, SILC_ID_CHANNEL,
791 data, data_len, force_send);
796 /* Send the packet (to normal server) */
797 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
800 data, data_len, TRUE,
806 if (client && client->router)
809 /* Get data used in packet header encryption, keys and stuff. */
810 sock = (SilcSocketConnection)client->connection;
811 idata = (SilcIDListData)client;
813 if (sender_sock && sock == sender_sock)
816 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
817 silc_id_render(client->id, SILC_ID_CLIENT),
818 sock->hostname, sock->ip));
820 /* Send the packet */
821 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
824 data, data_len, TRUE,
829 silc_free(packetdata.src_id);
830 silc_free(packetdata.dst_id);
833 /* This function is used to send packets strictly to all local clients
834 on a particular channel. This is used for example to distribute new
835 channel key to all our locally connected clients on the channel.
836 The packets are always encrypted with the session key shared between
837 the client, this means these are not _to the channel_ but _to the client_
840 void silc_server_packet_send_local_channel(SilcServer server,
841 SilcChannelEntry channel,
843 SilcPacketFlags flags,
848 SilcChannelClientEntry chl;
849 SilcHashTableList htl;
850 SilcSocketConnection sock = NULL;
852 SILC_LOG_DEBUG(("Start"));
854 /* Send the message to clients on the channel's client list. */
855 silc_hash_table_list(channel->user_list, &htl);
856 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
857 if (chl->client && !chl->client->router) {
858 sock = (SilcSocketConnection)chl->client->connection;
860 /* Send the packet to the client */
861 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
862 SILC_ID_CLIENT, data, data_len,
868 /* Routine used to send (relay, route) private messages to some destination.
869 If the private message key does not exist then the message is re-encrypted,
870 otherwise we just pass it along. This really is not used to send new
871 private messages (as server does not send them) but to relay received
874 void silc_server_send_private_message(SilcServer server,
875 SilcSocketConnection dst_sock,
878 SilcPacketContext *packet)
880 SilcBuffer buffer = packet->buffer;
882 /* Re-encrypt and send if private messge key does not exist */
883 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
885 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
886 + packet->dst_id_len + packet->padlen);
887 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
888 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
890 /* Re-encrypt packet */
891 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
893 /* Send the packet */
894 silc_server_packet_send_real(server, dst_sock, FALSE);
897 /* Key exist so encrypt just header and send it */
898 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
899 + packet->dst_id_len + packet->padlen);
900 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
901 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
904 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf,
905 SILC_PACKET_HEADER_LEN + packet->src_id_len +
906 packet->dst_id_len + packet->padlen);
908 silc_server_packet_send_real(server, dst_sock, FALSE);
912 /* Sends current motd to client */
914 void silc_server_send_motd(SilcServer server,
915 SilcSocketConnection sock)
920 if (server->config && server->config->motd &&
921 server->config->motd->motd_file) {
923 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
927 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
933 /* Sends error message. Error messages may or may not have any
936 void silc_server_send_error(SilcServer server,
937 SilcSocketConnection sock,
938 const char *fmt, ...)
941 unsigned char buf[4096];
943 memset(buf, 0, sizeof(buf));
945 vsprintf(buf, fmt, ap);
948 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
949 buf, strlen(buf), FALSE);
952 /* Sends notify message. If format is TRUE the variable arguments are
953 formatted and the formatted string is sent as argument payload. If it is
954 FALSE then each argument is sent as separate argument and their format
955 in the argument list must be { argument data, argument length }. */
957 void silc_server_send_notify(SilcServer server,
958 SilcSocketConnection sock,
968 packet = silc_notify_payload_encode(type, argc, ap);
969 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
970 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
971 packet->data, packet->len, FALSE);
973 /* Send to backup routers if this is being broadcasted to primary
975 if (server->router && server->router->connection &&
976 sock == server->router->connection && broadcast)
977 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
978 packet->data, packet->len, FALSE, TRUE);
980 silc_buffer_free(packet);
984 /* Sends notify message and gets the arguments from the `args' Argument
987 void silc_server_send_notify_args(SilcServer server,
988 SilcSocketConnection sock,
996 packet = silc_notify_payload_encode_args(type, argc, args);
997 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
998 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
999 packet->data, packet->len, FALSE);
1000 silc_buffer_free(packet);
1003 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1004 `old_id' with the `new_id'. */
1006 void silc_server_send_notify_channel_change(SilcServer server,
1007 SilcSocketConnection sock,
1009 SilcChannelID *old_id,
1010 SilcChannelID *new_id)
1012 SilcBuffer idp1, idp2;
1014 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1015 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1017 silc_server_send_notify(server, sock, broadcast,
1018 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1019 2, idp1->data, idp1->len, idp2->data, idp2->len);
1020 silc_buffer_free(idp1);
1021 silc_buffer_free(idp2);
1024 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1025 `old_id' with the `new_id'. */
1027 void silc_server_send_notify_nick_change(SilcServer server,
1028 SilcSocketConnection sock,
1030 SilcClientID *old_id,
1031 SilcClientID *new_id)
1033 SilcBuffer idp1, idp2;
1035 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1036 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1038 silc_server_send_notify(server, sock, broadcast,
1039 SILC_NOTIFY_TYPE_NICK_CHANGE,
1040 2, idp1->data, idp1->len, idp2->data, idp2->len);
1041 silc_buffer_free(idp1);
1042 silc_buffer_free(idp2);
1045 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1046 has joined to the `channel'. */
1048 void silc_server_send_notify_join(SilcServer server,
1049 SilcSocketConnection sock,
1051 SilcChannelEntry channel,
1052 SilcClientID *client_id)
1054 SilcBuffer idp1, idp2;
1056 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1057 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1058 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1059 2, idp1->data, idp1->len,
1060 idp2->data, idp2->len);
1061 silc_buffer_free(idp1);
1062 silc_buffer_free(idp2);
1065 /* Sends LEAVE notify type. This tells that `client_id' has left the
1066 `channel'. The Notify packet is always destined to the channel. */
1068 void silc_server_send_notify_leave(SilcServer server,
1069 SilcSocketConnection sock,
1071 SilcChannelEntry channel,
1072 SilcClientID *client_id)
1076 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1077 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1078 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1079 1, idp->data, idp->len);
1080 silc_buffer_free(idp);
1083 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1084 `channel' mode to `mode. The Notify packet is always destined to
1087 void silc_server_send_notify_cmode(SilcServer server,
1088 SilcSocketConnection sock,
1090 SilcChannelEntry channel,
1092 void *id, SilcIdType id_type,
1093 char *cipher, char *hmac)
1096 unsigned char mode[4];
1098 idp = silc_id_payload_encode((void *)id, id_type);
1099 SILC_PUT32_MSB(mode_mask, mode);
1101 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1102 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1103 4, idp->data, idp->len,
1105 cipher, cipher ? strlen(cipher) : 0,
1106 hmac, hmac ? strlen(hmac) : 0);
1107 silc_buffer_free(idp);
1110 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1111 `target' client's mode on `channel'. The Notify packet is always
1112 destined to the channel. */
1114 void silc_server_send_notify_cumode(SilcServer server,
1115 SilcSocketConnection sock,
1117 SilcChannelEntry channel,
1119 void *id, SilcIdType id_type,
1120 SilcClientID *target)
1122 SilcBuffer idp1, idp2;
1123 unsigned char mode[4];
1125 idp1 = silc_id_payload_encode((void *)id, id_type);
1126 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1127 SILC_PUT32_MSB(mode_mask, mode);
1129 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1131 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
1132 idp1->data, idp1->len,
1134 idp2->data, idp2->len);
1135 silc_buffer_free(idp1);
1136 silc_buffer_free(idp2);
1139 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1140 left SILC network. This function is used only between server and router
1141 traffic. This is not used to send the notify to the channel for
1142 client. The `message may be NULL. */
1144 void silc_server_send_notify_signoff(SilcServer server,
1145 SilcSocketConnection sock,
1147 SilcClientID *client_id,
1152 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1153 silc_server_send_notify(server, sock, broadcast,
1154 SILC_NOTIFY_TYPE_SIGNOFF,
1155 message ? 2 : 1, idp->data, idp->len,
1156 message, message ? strlen(message): 0);
1157 silc_buffer_free(idp);
1160 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1161 the `channel's topic to `topic'. The Notify packet is always destined
1162 to the channel. This function is used to send the topic set notifies
1165 void silc_server_send_notify_topic_set(SilcServer server,
1166 SilcSocketConnection sock,
1168 SilcChannelEntry channel,
1169 SilcClientID *client_id,
1174 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1175 silc_server_send_notify(server, sock, broadcast,
1176 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1178 idp->data, idp->len,
1179 topic, topic ? strlen(topic) : 0);
1180 silc_buffer_free(idp);
1183 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1184 was kicked off the channel. The `comment' may indicate the reason
1185 for the kicking. This function is used only between server and router
1188 void silc_server_send_notify_kicked(SilcServer server,
1189 SilcSocketConnection sock,
1191 SilcChannelEntry channel,
1192 SilcClientID *client_id,
1197 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1198 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1199 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1200 comment ? 2 : 1, idp->data, idp->len,
1201 comment, comment ? strlen(comment) : 0);
1202 silc_buffer_free(idp);
1205 /* Send KILLED notify type. This tells that the `client_id' client was
1206 killed from the network. The `comment' may indicate the reason
1209 void silc_server_send_notify_killed(SilcServer server,
1210 SilcSocketConnection sock,
1212 SilcClientID *client_id,
1217 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1218 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1219 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1220 comment ? 2 : 1, idp->data, idp->len,
1221 comment, comment ? strlen(comment) : 0);
1222 silc_buffer_free(idp);
1225 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1226 user mode in the SILC Network was changed. This function is used to
1227 send the packet between routers as broadcast packet. */
1229 void silc_server_send_notify_umode(SilcServer server,
1230 SilcSocketConnection sock,
1232 SilcClientID *client_id,
1236 unsigned char mode[4];
1238 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1239 SILC_PUT32_MSB(mode_mask, mode);
1241 silc_server_send_notify(server, sock, broadcast,
1242 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1243 idp->data, idp->len,
1245 silc_buffer_free(idp);
1248 /* Sends BAN notify type. This tells that ban has been either `add'ed
1249 or `del'eted on the `channel. This function is used to send the packet
1250 between routers as broadcast packet. */
1252 void silc_server_send_notify_ban(SilcServer server,
1253 SilcSocketConnection sock,
1255 SilcChannelEntry channel,
1256 char *add, char *del)
1260 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1261 silc_server_send_notify(server, sock, broadcast,
1262 SILC_NOTIFY_TYPE_BAN, 3,
1263 idp->data, idp->len,
1264 add, add ? strlen(add) : 0,
1265 del, del ? strlen(del) : 0);
1266 silc_buffer_free(idp);
1269 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1270 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1271 This function is used to send the packet between routers as broadcast
1274 void silc_server_send_notify_invite(SilcServer server,
1275 SilcSocketConnection sock,
1277 SilcChannelEntry channel,
1278 SilcClientID *client_id,
1279 char *add, char *del)
1281 SilcBuffer idp, idp2;
1283 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1284 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1285 silc_server_send_notify(server, sock, broadcast,
1286 SILC_NOTIFY_TYPE_INVITE, 5,
1287 idp->data, idp->len,
1288 channel->channel_name, strlen(channel->channel_name),
1289 idp2->data, idp2->len,
1290 add, add ? strlen(add) : 0,
1291 del, del ? strlen(del) : 0);
1292 silc_buffer_free(idp);
1293 silc_buffer_free(idp2);
1296 /* Sends notify message destined to specific entity. */
1298 void silc_server_send_notify_dest(SilcServer server,
1299 SilcSocketConnection sock,
1302 SilcIdType dest_id_type,
1303 SilcNotifyType type,
1311 packet = silc_notify_payload_encode(type, argc, ap);
1312 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
1313 dest_id, dest_id_type,
1314 packet->data, packet->len, FALSE);
1315 silc_buffer_free(packet);
1319 /* Sends notify message to a channel. The notify message sent is
1320 distributed to all clients on the channel. If `route_notify' is TRUE
1321 then the notify may be routed to primary route or to some other routers.
1322 If FALSE it is assured that the notify is sent only locally. If `sender'
1323 is provided then the packet is not sent to that connection since it
1324 originally came from it. */
1326 void silc_server_send_notify_to_channel(SilcServer server,
1327 SilcSocketConnection sender,
1328 SilcChannelEntry channel,
1329 unsigned char route_notify,
1330 SilcNotifyType type,
1338 packet = silc_notify_payload_encode(type, argc, ap);
1339 silc_server_packet_send_to_channel(server, sender, channel,
1340 SILC_PACKET_NOTIFY, route_notify,
1341 packet->data, packet->len, FALSE);
1342 silc_buffer_free(packet);
1346 /* Send notify message to all channels the client has joined. It is quaranteed
1347 that the message is sent only once to a client (ie. if a client is joined
1348 on two same channel it will receive only one notify message). Also, this
1349 sends only to local clients (locally connected if we are server, and to
1350 local servers if we are router). If `sender' is provided the packet is
1351 not sent to that client at all. */
1353 void silc_server_send_notify_on_channels(SilcServer server,
1354 SilcClientEntry sender,
1355 SilcClientEntry client,
1356 SilcNotifyType type,
1360 SilcSocketConnection sock = NULL;
1361 SilcPacketContext packetdata;
1363 SilcClientEntry *sent_clients = NULL;
1364 uint32 sent_clients_count = 0;
1365 SilcServerEntry *routed = NULL;
1366 uint32 routed_count = 0;
1367 SilcHashTableList htl, htl2;
1368 SilcChannelEntry channel;
1369 SilcChannelClientEntry chl, chl2;
1370 SilcIDListData idata;
1372 unsigned char *data;
1374 bool force_send = FALSE;
1377 SILC_LOG_DEBUG(("Start"));
1379 if (!silc_hash_table_count(client->channels))
1383 packet = silc_notify_payload_encode(type, argc, ap);
1384 data = packet->data;
1385 data_len = packet->len;
1387 /* Set the packet context pointers. */
1388 packetdata.flags = 0;
1389 packetdata.type = SILC_PACKET_NOTIFY;
1390 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1391 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1392 packetdata.src_id_type = SILC_ID_SERVER;
1394 silc_hash_table_list(client->channels, &htl);
1395 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1396 channel = chl->channel;
1398 /* Send the message to all clients on the channel's client list. */
1399 silc_hash_table_list(channel->user_list, &htl2);
1400 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1403 if (sender && c == sender)
1406 /* Check if we have sent the packet to this client already */
1407 for (k = 0; k < sent_clients_count; k++)
1408 if (sent_clients[k] == c)
1410 if (k < sent_clients_count)
1413 /* If we are router and if this client has router set it is not
1414 locally connected client and we will route the message to the
1415 router set in the client. */
1416 if (c && c->router && server->server_type == SILC_ROUTER) {
1417 /* Check if we have sent the packet to this route already */
1418 for (k = 0; k < routed_count; k++)
1419 if (routed[k] == c->router)
1421 if (k < routed_count)
1424 /* Get data used in packet header encryption, keys and stuff. */
1425 sock = (SilcSocketConnection)c->router->connection;
1426 idata = (SilcIDListData)c->router;
1428 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1429 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1430 packetdata.dst_id_type = SILC_ID_SERVER;
1431 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1432 packetdata.src_id_len + packetdata.dst_id_len;
1433 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1435 /* Send the packet */
1436 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1439 data, data_len, FALSE,
1442 silc_free(packetdata.dst_id);
1444 /* We want to make sure that the packet is routed to same router
1445 only once. Mark this route as sent route. */
1447 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1448 routed[k] = c->router;
1457 /* Send to locally connected client */
1460 /* Get data used in packet header encryption, keys and stuff. */
1461 sock = (SilcSocketConnection)c->connection;
1462 idata = (SilcIDListData)c;
1464 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1465 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1466 packetdata.dst_id_type = SILC_ID_CLIENT;
1467 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1468 packetdata.src_id_len + packetdata.dst_id_len;
1469 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1471 /* Send the packet */
1472 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1475 data, data_len, FALSE,
1478 silc_free(packetdata.dst_id);
1480 /* Make sure that we send the notify only once per client. */
1481 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1482 (sent_clients_count + 1));
1483 sent_clients[sent_clients_count] = c;
1484 sent_clients_count++;
1491 if (sent_clients_count)
1492 silc_free(sent_clients);
1493 silc_free(packetdata.src_id);
1497 /* Sends New ID Payload to remote end. The packet is used to distribute
1498 information about new registered clients, servers, channel etc. usually
1499 to routers so that they can keep these information up to date.
1500 If the argument `broadcast' is TRUE then the packet is sent as
1501 broadcast packet. */
1503 void silc_server_send_new_id(SilcServer server,
1504 SilcSocketConnection sock,
1506 void *id, SilcIdType id_type,
1511 SILC_LOG_DEBUG(("Start"));
1513 idp = silc_id_payload_encode(id, id_type);
1514 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1515 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1516 idp->data, idp->len, FALSE);
1518 /* Send to backup routers if this is being broadcasted to primary
1520 if (server->router && server->router->connection &&
1521 sock == server->router->connection && broadcast)
1522 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_ID, 0,
1523 idp->data, idp->len, FALSE, TRUE);
1525 silc_buffer_free(idp);
1528 /* Send New Channel Payload to notify about newly created channel in the
1529 SILC network. Normal server nevers sends this packet. Router uses this
1530 to notify other routers in the network about new channel. This packet
1533 void silc_server_send_new_channel(SilcServer server,
1534 SilcSocketConnection sock,
1538 uint32 channel_id_len,
1543 uint32 name_len = strlen(channel_name);
1545 SILC_LOG_DEBUG(("Start"));
1547 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1551 /* Encode the channel payload */
1552 packet = silc_channel_payload_encode(channel_name, name_len,
1553 cid, channel_id_len, mode);
1555 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1556 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1557 packet->data, packet->len, FALSE);
1559 /* Send to backup routers if this is being broadcasted to primary
1561 if (server->router && server->router->connection &&
1562 sock == server->router->connection && broadcast)
1563 silc_server_backup_send(server, NULL, SILC_PACKET_NEW_CHANNEL, 0,
1564 packet->data, packet->len, FALSE, TRUE);
1567 silc_buffer_free(packet);
1570 /* Send Channel Key payload to distribute the new channel key. Normal server
1571 sends this to router when new client joins to existing channel. Router
1572 sends this to the local server who sent the join command in case where
1573 the channel did not exist yet. Both normal and router servers uses this
1574 also to send this to locally connected clients on the channel. This
1575 must not be broadcasted packet. Routers do not send this to each other.
1576 If `sender is provided then the packet is not sent to that connection since
1577 it originally came from it. */
1579 void silc_server_send_channel_key(SilcServer server,
1580 SilcSocketConnection sender,
1581 SilcChannelEntry channel,
1582 unsigned char route)
1585 unsigned char *chid;
1588 SILC_LOG_DEBUG(("Start"));
1590 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1594 /* Encode channel key packet */
1595 tmp_len = strlen(channel->channel_key->cipher->name);
1596 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1599 channel->channel_key->cipher->name,
1600 channel->key_len / 8, channel->key);
1601 silc_server_packet_send_to_channel(server, sender, channel,
1602 SILC_PACKET_CHANNEL_KEY,
1603 route, packet->data, packet->len, FALSE);
1604 silc_buffer_free(packet);
1608 /* Generic function to send any command. The arguments must be sent already
1609 encoded into correct form in correct order. */
1611 void silc_server_send_command(SilcServer server,
1612 SilcSocketConnection sock,
1613 SilcCommand command,
1622 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1623 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1624 packet->data, packet->len, TRUE);
1625 silc_buffer_free(packet);
1629 /* Send the heartbeat packet. */
1631 void silc_server_send_heartbeat(SilcServer server,
1632 SilcSocketConnection sock)
1634 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1638 /* Generic function to relay packet we've received. This is used to relay
1639 packets to a client but generally can be used to other purposes as well. */
1641 void silc_server_relay_packet(SilcServer server,
1642 SilcSocketConnection dst_sock,
1645 SilcPacketContext *packet,
1648 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1649 + packet->dst_id_len + packet->padlen);
1651 silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1652 silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1654 /* Re-encrypt packet */
1655 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1657 /* Send the packet */
1658 silc_server_packet_send_real(server, dst_sock, force_send);
1660 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1661 + packet->dst_id_len + packet->padlen);
1664 /* Routine used to send the connection authentication packet. */
1666 void silc_server_send_connection_auth_request(SilcServer server,
1667 SilcSocketConnection sock,
1669 SilcAuthMethod auth_meth)
1673 packet = silc_buffer_alloc(4);
1674 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1675 silc_buffer_format(packet,
1676 SILC_STR_UI_SHORT(conn_type),
1677 SILC_STR_UI_SHORT(auth_meth),
1680 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1681 0, packet->data, packet->len, FALSE);
1682 silc_buffer_free(packet);
1685 /* Purge the outgoing packet queue to the network if there is data. This
1686 function can be used to empty the packet queue. It is guaranteed that
1687 after this function returns the outgoing data queue is empty. */
1689 void silc_server_packet_queue_purge(SilcServer server,
1690 SilcSocketConnection sock)
1692 if (sock && SILC_IS_OUTBUF_PENDING(sock) &&
1693 (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1694 server->stat.packets_sent++;
1696 if (sock->outbuf->data - sock->outbuf->head)
1697 silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
1699 silc_packet_send(sock, TRUE);
1701 SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1702 SILC_UNSET_OUTBUF_PENDING(sock);
1703 silc_buffer_clear(sock->outbuf);