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))
48 ret = silc_packet_send(sock, force_send);
52 /* Mark that there is some outgoing data available for this connection.
53 This call sets the connection both for input and output (the input
54 is set always and this call keeps the input setting, actually).
55 Actual data sending is performed by silc_server_packet_process. */
56 SILC_SET_CONNECTION_FOR_OUTPUT(server->schedule, sock->sock);
58 /* Mark to socket that data is pending in outgoing buffer. This flag
59 is needed if new data is added to the buffer before the earlier
60 put data is sent to the network. */
61 SILC_SET_OUTBUF_PENDING(sock);
66 /* Assembles a new packet to be sent out to network. This doesn't actually
67 send the packet but creates the packet and fills the outgoing data
68 buffer and marks the packet ready to be sent to network. However, If
69 argument force_send is TRUE the packet is sent immediately and not put
70 to queue. Normal case is that the packet is not sent immediately. */
72 void silc_server_packet_send(SilcServer server,
73 SilcSocketConnection sock,
75 SilcPacketFlags flags,
81 SilcIdType dst_id_type = SILC_ID_NONE;
86 /* If disconnecting, ignore the data */
87 if (SILC_IS_DISCONNECTING(sock))
90 /* Get data used in the packet sending, keys and stuff */
92 case SILC_SOCKET_TYPE_CLIENT:
93 if (sock->user_data) {
94 dst_id = ((SilcClientEntry)sock->user_data)->id;
95 dst_id_type = SILC_ID_CLIENT;
98 case SILC_SOCKET_TYPE_SERVER:
99 case SILC_SOCKET_TYPE_ROUTER:
100 if (sock->user_data) {
101 dst_id = ((SilcServerEntry)sock->user_data)->id;
102 dst_id_type = SILC_ID_SERVER;
109 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
110 dst_id_type, data, data_len, force_send);
113 /* Assembles a new packet to be sent out to network. This doesn't actually
114 send the packet but creates the packet and fills the outgoing data
115 buffer and marks the packet ready to be sent to network. However, If
116 argument force_send is TRUE the packet is sent immediately and not put
117 to queue. Normal case is that the packet is not sent immediately.
118 Destination information is sent as argument for this function. */
120 void silc_server_packet_send_dest(SilcServer server,
121 SilcSocketConnection sock,
123 SilcPacketFlags flags,
125 SilcIdType dst_id_type,
130 SilcPacketContext packetdata;
131 SilcIDListData idata;
132 SilcCipher cipher = NULL;
133 SilcHmac hmac = NULL;
134 unsigned char *dst_id_data = NULL;
135 uint32 dst_id_len = 0;
137 /* If disconnecting, ignore the data */
138 if (SILC_IS_DISCONNECTING(sock))
141 SILC_LOG_DEBUG(("Sending packet, type %d", type));
143 /* Get data used in the packet sending, keys and stuff */
144 idata = (SilcIDListData)sock->user_data;
147 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
148 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
151 /* Set the packet context pointers */
152 packetdata.type = type;
153 packetdata.flags = flags;
154 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
155 packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
156 packetdata.src_id_type = server->id_type;
157 packetdata.dst_id = dst_id_data;
158 packetdata.dst_id_len = dst_id_len;
159 packetdata.dst_id_type = dst_id_type;
160 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
161 packetdata.src_id_len + dst_id_len;
162 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
164 /* Prepare outgoing data buffer for packet sending */
165 silc_packet_send_prepare(sock,
166 SILC_PACKET_HEADER_LEN +
167 packetdata.src_id_len +
168 packetdata.dst_id_len,
172 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
174 packetdata.buffer = sock->outbuf;
176 /* Put the data to the buffer */
177 if (data && data_len)
178 silc_buffer_put(sock->outbuf, data, data_len);
180 /* Create the outgoing packet */
181 silc_packet_assemble(&packetdata);
184 cipher = idata->send_key;
185 hmac = idata->hmac_send;
188 /* Encrypt the packet */
189 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
191 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
192 sock->outbuf->data, sock->outbuf->len);
194 /* Now actually send the packet */
195 silc_server_packet_send_real(server, sock, force_send);
197 if (packetdata.src_id)
198 silc_free(packetdata.src_id);
199 if (packetdata.dst_id)
200 silc_free(packetdata.dst_id);
203 /* Assembles a new packet to be sent out to network. This doesn't actually
204 send the packet but creates the packet and fills the outgoing data
205 buffer and marks the packet ready to be sent to network. However, If
206 argument force_send is TRUE the packet is sent immediately and not put
207 to queue. Normal case is that the packet is not sent immediately.
208 The source and destination information is sent as argument for this
211 void silc_server_packet_send_srcdest(SilcServer server,
212 SilcSocketConnection sock,
214 SilcPacketFlags flags,
216 SilcIdType src_id_type,
218 SilcIdType dst_id_type,
223 SilcPacketContext packetdata;
224 SilcIDListData idata;
225 SilcCipher cipher = NULL;
226 SilcHmac hmac = NULL;
227 unsigned char *dst_id_data = NULL;
228 uint32 dst_id_len = 0;
229 unsigned char *src_id_data = NULL;
230 uint32 src_id_len = 0;
232 SILC_LOG_DEBUG(("Sending packet, type %d", type));
234 /* Get data used in the packet sending, keys and stuff */
235 idata = (SilcIDListData)sock->user_data;
238 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
239 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
243 src_id_data = silc_id_id2str(src_id, src_id_type);
244 src_id_len = silc_id_get_len(src_id, src_id_type);
247 /* Set the packet context pointers */
248 packetdata.type = type;
249 packetdata.flags = flags;
250 packetdata.src_id = src_id_data;
251 packetdata.src_id_len = src_id_len;
252 packetdata.src_id_type = src_id_type;
253 packetdata.dst_id = dst_id_data;
254 packetdata.dst_id_len = dst_id_len;
255 packetdata.dst_id_type = dst_id_type;
256 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
257 packetdata.src_id_len + dst_id_len;
258 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
260 /* Prepare outgoing data buffer for packet sending */
261 silc_packet_send_prepare(sock,
262 SILC_PACKET_HEADER_LEN +
263 packetdata.src_id_len +
264 packetdata.dst_id_len,
268 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
270 packetdata.buffer = sock->outbuf;
272 /* Put the data to the buffer */
273 if (data && data_len)
274 silc_buffer_put(sock->outbuf, data, data_len);
276 /* Create the outgoing packet */
277 silc_packet_assemble(&packetdata);
280 cipher = idata->send_key;
281 hmac = idata->hmac_send;
284 /* Encrypt the packet */
285 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
287 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
288 sock->outbuf->data, sock->outbuf->len);
290 /* Now actually send the packet */
291 silc_server_packet_send_real(server, sock, force_send);
293 if (packetdata.src_id)
294 silc_free(packetdata.src_id);
295 if (packetdata.dst_id)
296 silc_free(packetdata.dst_id);
299 /* Broadcast received packet to our primary route. This function is used
300 by router to further route received broadcast packet. It is expected
301 that the broadcast flag from the packet is checked before calling this
302 function. This does not test or set the broadcast flag. */
304 void silc_server_packet_broadcast(SilcServer server,
305 SilcSocketConnection sock,
306 SilcPacketContext *packet)
308 SilcBuffer buffer = packet->buffer;
309 SilcIDListData idata;
312 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
314 /* If the packet is originated from our primary route we are
315 not allowed to send the packet. */
316 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
317 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
318 idata = (SilcIDListData)sock->user_data;
320 silc_buffer_push(buffer, buffer->data - buffer->head);
321 silc_packet_send_prepare(sock, 0, 0, buffer->len);
322 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
323 silc_packet_encrypt(idata->send_key, idata->hmac_send,
324 sock->outbuf, sock->outbuf->len);
326 SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
327 sock->outbuf->data, sock->outbuf->len);
329 /* Now actually send the packet */
330 silc_server_packet_send_real(server, sock, TRUE);
335 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
336 "original sender of this packet"));
340 /* Routes received packet to `sock'. This is used to route the packets that
341 router receives but are not destined to it. */
343 void silc_server_packet_route(SilcServer server,
344 SilcSocketConnection sock,
345 SilcPacketContext *packet)
347 SilcBuffer buffer = packet->buffer;
348 SilcIDListData idata;
350 SILC_LOG_DEBUG(("Routing received packet"));
352 idata = (SilcIDListData)sock->user_data;
354 silc_buffer_push(buffer, buffer->data - buffer->head);
355 silc_packet_send_prepare(sock, 0, 0, buffer->len);
356 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
357 silc_packet_encrypt(idata->send_key, idata->hmac_send,
358 sock->outbuf, sock->outbuf->len);
360 SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
361 sock->outbuf->data, sock->outbuf->len);
363 /* Now actually send the packet */
364 silc_server_packet_send_real(server, sock, TRUE);
367 /* Internal routine to actually create the channel packet and send it
368 to network. This is common function in channel message sending. If
369 `channel_message' is TRUE this encrypts the message as it is strictly
370 a channel message. If FALSE normal encryption process is used. */
373 silc_server_packet_send_to_channel_real(SilcServer server,
374 SilcSocketConnection sock,
375 SilcPacketContext *packet,
383 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
384 packet->src_id_len + packet->dst_id_len;
386 /* Prepare outgoing data buffer for packet sending */
387 silc_packet_send_prepare(sock,
388 SILC_PACKET_HEADER_LEN +
394 packet->buffer = sock->outbuf;
396 /* Put the data to buffer, assemble and encrypt the packet. The packet
397 is encrypted with normal session key shared with the client. */
398 silc_buffer_put(sock->outbuf, data, data_len);
399 silc_packet_assemble(packet);
401 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
402 packet->src_id_len + packet->dst_id_len +
405 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
407 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
408 sock->outbuf->data, sock->outbuf->len);
410 /* Now actually send the packet */
411 silc_server_packet_send_real(server, sock, force_send);
414 /* This routine is used by the server to send packets to channel. The
415 packet sent with this function is distributed to all clients on
416 the channel. Usually this is used to send notify messages to the
417 channel, things like notify about new user joining to the channel.
418 If `route' is FALSE then the packet is sent only locally and will not
419 be routed anywhere (for router locally means cell wide). If `sender'
420 is provided then the packet is not sent to that connection since it
421 originally came from it. */
423 void silc_server_packet_send_to_channel(SilcServer server,
424 SilcSocketConnection sender,
425 SilcChannelEntry channel,
432 SilcSocketConnection sock = NULL;
433 SilcPacketContext packetdata;
434 SilcClientEntry client = NULL;
435 SilcServerEntry *routed = NULL;
436 SilcChannelClientEntry chl;
437 SilcHashTableList htl;
438 SilcIDListData idata;
439 uint32 routed_count = 0;
441 /* This doesn't send channel message packets */
442 if (type == SILC_PACKET_CHANNEL_MESSAGE)
445 SILC_LOG_DEBUG(("Sending packet to channel"));
447 /* Set the packet context pointers. */
448 packetdata.flags = 0;
449 packetdata.type = type;
450 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
451 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
452 packetdata.src_id_type = SILC_ID_SERVER;
453 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
454 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
455 packetdata.dst_id_type = SILC_ID_CHANNEL;
456 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
457 packetdata.src_id_len + packetdata.dst_id_len;
458 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
460 /* If there are global users in the channel we will send the message
461 first to our router for further routing. */
462 if (route && server->server_type == SILC_SERVER && !server->standalone &&
463 channel->global_users) {
464 SilcServerEntry router;
466 /* Get data used in packet header encryption, keys and stuff. */
467 router = server->router;
468 sock = (SilcSocketConnection)router->connection;
469 idata = (SilcIDListData)router;
471 if (sock != sender) {
472 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
474 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
477 data, data_len, FALSE,
482 /* Send the message to clients on the channel's client list. */
483 silc_hash_table_list(channel->user_list, &htl);
484 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
485 client = chl->client;
487 /* If client has router set it is not locally connected client and
488 we will route the message to the router set in the client. Though,
489 send locally connected server in all cases. */
490 if (server->server_type == SILC_ROUTER && client && client->router &&
491 ((!route && client->router->router == server->id_entry) || route)) {
494 /* Check if we have sent the packet to this route already */
495 for (k = 0; k < routed_count; k++)
496 if (routed[k] == client->router)
498 if (k < routed_count)
501 /* Get data used in packet header encryption, keys and stuff. */
502 sock = (SilcSocketConnection)client->router->connection;
503 idata = (SilcIDListData)client->router;
505 if (sender && sock == sender)
508 /* Send the packet */
509 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
512 data, data_len, FALSE,
515 /* We want to make sure that the packet is routed to same router
516 only once. Mark this route as sent route. */
518 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
519 routed[k] = client->router;
525 if (client && client->router)
528 /* Send to locally connected client */
531 /* Get data used in packet header encryption, keys and stuff. */
532 sock = (SilcSocketConnection)client->connection;
533 idata = (SilcIDListData)client;
535 if (sender && sock == sender)
538 /* Send the packet */
539 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
542 data, data_len, FALSE,
549 silc_free(packetdata.src_id);
550 silc_free(packetdata.dst_id);
553 /* This checks whether the relayed packet came from router. If it did
554 then we'll need to encrypt it with the channel key. This is called
555 from the silc_server_packet_relay_to_channel. */
558 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
559 SilcSocketConnection sock,
560 SilcChannelEntry channel,
562 unsigned int data_len)
564 /* If we are router and the packet came from router and private key
565 has not been set for the channel then we must encrypt the packet
566 as it was decrypted with the session key shared between us and the
567 router which sent it. This is so, because cells does not share the
569 if (server->server_type == SILC_ROUTER &&
570 sock->type == SILC_SOCKET_TYPE_ROUTER &&
571 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
572 channel->channel_key) {
575 uint16 data_len, flags;
577 iv_len = silc_cipher_get_block_len(channel->channel_key);
578 if (channel->iv[0] == '\0')
579 for (i = 0; i < iv_len; i++) channel->iv[i] =
580 silc_rng_get_byte(server->rng);
582 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
584 /* Encode new payload. This encrypts it also. */
585 SILC_GET16_MSB(flags, data);
586 SILC_GET16_MSB(data_len, data + 2);
587 chp = silc_channel_message_payload_encode(flags, data_len,
590 channel->channel_key,
592 memcpy(data, chp->data, chp->len);
593 silc_buffer_free(chp);
597 /* This routine is explicitly used to relay messages to some channel.
598 Packets sent with this function we have received earlier and are
599 totally encrypted. This just sends the packet to all clients on
600 the channel. If the sender of the packet is someone on the channel
601 the message will not be sent to that client. The SILC Packet header
602 is encrypted with the session key shared between us and the client.
603 MAC is also computed before encrypting the header. Rest of the
604 packet will be untouched. */
606 void silc_server_packet_relay_to_channel(SilcServer server,
607 SilcSocketConnection sender_sock,
608 SilcChannelEntry channel,
610 SilcIdType sender_type,
617 SilcSocketConnection sock = NULL;
618 SilcPacketContext packetdata;
619 SilcClientEntry client = NULL;
620 SilcServerEntry *routed = NULL;
621 SilcChannelClientEntry chl;
622 uint32 routed_count = 0;
623 SilcIDListData idata;
624 SilcHashTableList htl;
626 SILC_LOG_DEBUG(("Relaying packet to channel"));
628 /* Set the packet context pointers. */
629 packetdata.flags = 0;
630 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
631 packetdata.src_id = silc_id_id2str(sender, sender_type);
632 packetdata.src_id_len = silc_id_get_len(sender, sender_type);
633 packetdata.src_id_type = sender_type;
634 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
635 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
636 packetdata.dst_id_type = SILC_ID_CHANNEL;
637 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
638 packetdata.src_id_len +
639 packetdata.dst_id_len));
641 /* This encrypts the packet, if needed. It will be encrypted if
642 it came from the router thus it needs to be encrypted with the
643 channel key. If the channel key does not exist, then we know we
644 don't have a single local user on the channel. */
645 silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
649 /* If there are global users in the channel we will send the message
650 first to our router for further routing. */
651 if (server->server_type == SILC_SERVER && !server->standalone &&
652 channel->global_users) {
653 SilcServerEntry router;
655 router = server->router;
657 /* Check that the sender is not our router. */
658 if (sender_sock != (SilcSocketConnection)router->connection) {
660 /* Get data used in packet header encryption, keys and stuff. */
661 sock = (SilcSocketConnection)router->connection;
662 idata = (SilcIDListData)router;
664 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
666 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
669 data, data_len, TRUE,
674 /* Send the message to clients on the channel's client list. */
675 silc_hash_table_list(channel->user_list, &htl);
676 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
677 client = chl->client;
681 /* If sender is one on the channel do not send it the packet. */
682 if (!found && sender_type == SILC_ID_CLIENT &&
683 SILC_ID_CLIENT_COMPARE(client->id, sender)) {
688 /* If the client has set router it means that it is not locally
689 connected client and we will route the packet further. */
690 if (server->server_type == SILC_ROUTER && client->router) {
693 /* Sender maybe server as well so we want to make sure that
694 we won't send the message to the server it came from. */
695 if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
700 /* Check if we have sent the packet to this route already */
701 for (k = 0; k < routed_count; k++)
702 if (routed[k] == client->router)
704 if (k < routed_count)
707 /* Get data used in packet header encryption, keys and stuff. */
708 sock = (SilcSocketConnection)client->router->connection;
709 idata = (SilcIDListData)client->router;
711 /* Do not send to the sender. Check first whether the true
712 sender's router is same as this client's router. Also check
713 if the sender socket is the same as this client's router
716 ((SilcClientEntry)sender_entry)->router == client->router)
718 if (sender_sock && sock == sender_sock)
721 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
722 silc_id_render(client->id, SILC_ID_CLIENT),
723 sock->hostname, sock->ip));
725 /* We want to make sure that the packet is routed to same router
726 only once. Mark this route as sent route. */
728 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
729 routed[k] = client->router;
732 /* If the remote connection is router then we'll decrypt the
733 channel message and re-encrypt it with the session key shared
734 between us and the remote router. This is done because the
735 channel keys are cell specific and we have different channel
736 key than the remote router has. */
737 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
739 /* If private key mode is not set then decrypt the packet
741 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
742 unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
743 memcpy(tmp, data, data_len);
745 /* Decrypt the channel message (we don't check the MAC) */
746 if (channel->channel_key &&
747 !silc_channel_message_payload_decrypt(tmp, data_len,
748 channel->channel_key,
750 memset(tmp, 0, data_len);
755 /* Now re-encrypt and send it to the router */
756 silc_server_packet_send_srcdest(server, sock,
757 SILC_PACKET_CHANNEL_MESSAGE, 0,
759 channel->id, SILC_ID_CHANNEL,
760 tmp, data_len, force_send);
762 /* Free the copy of the channel message */
763 memset(tmp, 0, data_len);
766 /* Private key mode is set, we don't have the channel key, so
767 just re-encrypt the entire packet and send it to the router. */
768 silc_server_packet_send_srcdest(server, sock,
769 SILC_PACKET_CHANNEL_MESSAGE, 0,
771 channel->id, SILC_ID_CHANNEL,
772 data, data_len, force_send);
777 /* Send the packet (to normal server) */
778 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
781 data, data_len, TRUE,
787 if (client && client->router)
790 /* Get data used in packet header encryption, keys and stuff. */
791 sock = (SilcSocketConnection)client->connection;
792 idata = (SilcIDListData)client;
794 if (sender_sock && sock == sender_sock)
797 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
798 silc_id_render(client->id, SILC_ID_CLIENT),
799 sock->hostname, sock->ip));
801 /* Send the packet */
802 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
805 data, data_len, TRUE,
810 silc_free(packetdata.src_id);
811 silc_free(packetdata.dst_id);
814 /* This function is used to send packets strictly to all local clients
815 on a particular channel. This is used for example to distribute new
816 channel key to all our locally connected clients on the channel.
817 The packets are always encrypted with the session key shared between
818 the client, this means these are not _to the channel_ but _to the client_
821 void silc_server_packet_send_local_channel(SilcServer server,
822 SilcChannelEntry channel,
824 SilcPacketFlags flags,
829 SilcChannelClientEntry chl;
830 SilcHashTableList htl;
831 SilcSocketConnection sock = NULL;
833 SILC_LOG_DEBUG(("Start"));
835 /* Send the message to clients on the channel's client list. */
836 silc_hash_table_list(channel->user_list, &htl);
837 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
838 if (chl->client && !chl->client->router) {
839 sock = (SilcSocketConnection)chl->client->connection;
841 /* Send the packet to the client */
842 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
843 SILC_ID_CLIENT, data, data_len,
849 /* Routine used to send (relay, route) private messages to some destination.
850 If the private message key does not exist then the message is re-encrypted,
851 otherwise we just pass it along. This really is not used to send new
852 private messages (as server does not send them) but to relay received
855 void silc_server_send_private_message(SilcServer server,
856 SilcSocketConnection dst_sock,
859 SilcPacketContext *packet)
861 SilcBuffer buffer = packet->buffer;
863 /* Re-encrypt and send if private messge key does not exist */
864 if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
866 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
867 + packet->dst_id_len + packet->padlen);
868 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
869 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
871 /* Re-encrypt packet */
872 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
874 /* Send the packet */
875 silc_server_packet_send_real(server, dst_sock, FALSE);
878 /* Key exist so encrypt just header and send it */
879 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
880 + packet->dst_id_len + packet->padlen);
881 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
882 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
885 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf,
886 SILC_PACKET_HEADER_LEN + packet->src_id_len +
887 packet->dst_id_len + packet->padlen);
889 silc_server_packet_send_real(server, dst_sock, FALSE);
893 /* Sends current motd to client */
895 void silc_server_send_motd(SilcServer server,
896 SilcSocketConnection sock)
901 if (server->config && server->config->motd &&
902 server->config->motd->motd_file) {
904 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
908 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
914 /* Sends error message. Error messages may or may not have any
917 void silc_server_send_error(SilcServer server,
918 SilcSocketConnection sock,
919 const char *fmt, ...)
922 unsigned char buf[4096];
924 memset(buf, 0, sizeof(buf));
926 vsprintf(buf, fmt, ap);
929 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
930 buf, strlen(buf), FALSE);
933 /* Sends notify message. If format is TRUE the variable arguments are
934 formatted and the formatted string is sent as argument payload. If it is
935 FALSE then each argument is sent as separate argument and their format
936 in the argument list must be { argument data, argument length }. */
938 void silc_server_send_notify(SilcServer server,
939 SilcSocketConnection sock,
949 packet = silc_notify_payload_encode(type, argc, ap);
950 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
951 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
952 packet->data, packet->len, FALSE);
953 silc_buffer_free(packet);
957 /* Sends notify message and gets the arguments from the `args' Argument
960 void silc_server_send_notify_args(SilcServer server,
961 SilcSocketConnection sock,
969 packet = silc_notify_payload_encode_args(type, argc, args);
970 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
971 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
972 packet->data, packet->len, FALSE);
973 silc_buffer_free(packet);
976 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
977 `old_id' with the `new_id'. */
979 void silc_server_send_notify_channel_change(SilcServer server,
980 SilcSocketConnection sock,
982 SilcChannelID *old_id,
983 SilcChannelID *new_id)
985 SilcBuffer idp1, idp2;
987 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
988 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
990 silc_server_send_notify(server, sock, broadcast,
991 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
992 2, idp1->data, idp1->len, idp2->data, idp2->len);
993 silc_buffer_free(idp1);
994 silc_buffer_free(idp2);
997 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
998 `old_id' with the `new_id'. */
1000 void silc_server_send_notify_nick_change(SilcServer server,
1001 SilcSocketConnection sock,
1003 SilcClientID *old_id,
1004 SilcClientID *new_id)
1006 SilcBuffer idp1, idp2;
1008 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1009 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1011 silc_server_send_notify(server, sock, broadcast,
1012 SILC_NOTIFY_TYPE_NICK_CHANGE,
1013 2, idp1->data, idp1->len, idp2->data, idp2->len);
1014 silc_buffer_free(idp1);
1015 silc_buffer_free(idp2);
1018 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1019 has joined to the `channel'. */
1021 void silc_server_send_notify_join(SilcServer server,
1022 SilcSocketConnection sock,
1024 SilcChannelEntry channel,
1025 SilcClientID *client_id)
1027 SilcBuffer idp1, idp2;
1029 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1030 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1031 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1032 2, idp1->data, idp1->len,
1033 idp2->data, idp2->len);
1034 silc_buffer_free(idp1);
1035 silc_buffer_free(idp2);
1038 /* Sends LEAVE notify type. This tells that `client_id' has left the
1039 `channel'. The Notify packet is always destined to the channel. */
1041 void silc_server_send_notify_leave(SilcServer server,
1042 SilcSocketConnection sock,
1044 SilcChannelEntry channel,
1045 SilcClientID *client_id)
1049 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1050 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1051 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1052 1, idp->data, idp->len);
1053 silc_buffer_free(idp);
1056 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1057 `channel' mode to `mode. The Notify packet is always destined to
1060 void silc_server_send_notify_cmode(SilcServer server,
1061 SilcSocketConnection sock,
1063 SilcChannelEntry channel,
1065 void *id, SilcIdType id_type,
1066 char *cipher, char *hmac)
1069 unsigned char mode[4];
1071 idp = silc_id_payload_encode((void *)id, id_type);
1072 SILC_PUT32_MSB(mode_mask, mode);
1074 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1075 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1076 4, idp->data, idp->len,
1078 cipher, cipher ? strlen(cipher) : 0,
1079 hmac, hmac ? strlen(hmac) : 0);
1080 silc_buffer_free(idp);
1083 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1084 `target' client's mode on `channel'. The Notify packet is always
1085 destined to the channel. */
1087 void silc_server_send_notify_cumode(SilcServer server,
1088 SilcSocketConnection sock,
1090 SilcChannelEntry channel,
1092 void *id, SilcIdType id_type,
1093 SilcClientID *target)
1095 SilcBuffer idp1, idp2;
1096 unsigned char mode[4];
1098 idp1 = silc_id_payload_encode((void *)id, id_type);
1099 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1100 SILC_PUT32_MSB(mode_mask, mode);
1102 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1104 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
1105 idp1->data, idp1->len,
1107 idp2->data, idp2->len);
1108 silc_buffer_free(idp1);
1109 silc_buffer_free(idp2);
1112 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1113 left SILC network. This function is used only between server and router
1114 traffic. This is not used to send the notify to the channel for
1115 client. The `message may be NULL. */
1117 void silc_server_send_notify_signoff(SilcServer server,
1118 SilcSocketConnection sock,
1120 SilcClientID *client_id,
1125 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1126 silc_server_send_notify(server, sock, broadcast,
1127 SILC_NOTIFY_TYPE_SIGNOFF,
1128 message ? 2 : 1, idp->data, idp->len,
1129 message, message ? strlen(message): 0);
1130 silc_buffer_free(idp);
1133 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1134 the `channel's topic to `topic'. The Notify packet is always destined
1135 to the channel. This function is used to send the topic set notifies
1138 void silc_server_send_notify_topic_set(SilcServer server,
1139 SilcSocketConnection sock,
1141 SilcChannelEntry channel,
1142 SilcClientID *client_id,
1147 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1148 silc_server_send_notify(server, sock, broadcast,
1149 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1151 idp->data, idp->len,
1152 topic, topic ? strlen(topic) : 0);
1153 silc_buffer_free(idp);
1156 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1157 was kicked off the channel. The `comment' may indicate the reason
1158 for the kicking. This function is used only between server and router
1161 void silc_server_send_notify_kicked(SilcServer server,
1162 SilcSocketConnection sock,
1164 SilcChannelEntry channel,
1165 SilcClientID *client_id,
1170 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1171 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1172 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1173 comment ? 2 : 1, idp->data, idp->len,
1174 comment, comment ? strlen(comment) : 0);
1175 silc_buffer_free(idp);
1178 /* Send KILLED notify type. This tells that the `client_id' client was
1179 killed from the network. The `comment' may indicate the reason
1182 void silc_server_send_notify_killed(SilcServer server,
1183 SilcSocketConnection sock,
1185 SilcClientID *client_id,
1190 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1191 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1192 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1193 comment ? 2 : 1, idp->data, idp->len,
1194 comment, comment ? strlen(comment) : 0);
1195 silc_buffer_free(idp);
1198 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1199 user mode in the SILC Network was changed. This function is used to
1200 send the packet between routers as broadcast packet. */
1202 void silc_server_send_notify_umode(SilcServer server,
1203 SilcSocketConnection sock,
1205 SilcClientID *client_id,
1209 unsigned char mode[4];
1211 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1212 SILC_PUT32_MSB(mode_mask, mode);
1214 silc_server_send_notify(server, sock, broadcast,
1215 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1216 idp->data, idp->len,
1218 silc_buffer_free(idp);
1221 /* Sends BAN notify type. This tells that ban has been either `add'ed
1222 or `del'eted on the `channel. This function is used to send the packet
1223 between routers as broadcast packet. */
1225 void silc_server_send_notify_ban(SilcServer server,
1226 SilcSocketConnection sock,
1228 SilcChannelEntry channel,
1229 char *add, char *del)
1233 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1234 silc_server_send_notify(server, sock, broadcast,
1235 SILC_NOTIFY_TYPE_BAN, 3,
1236 idp->data, idp->len,
1237 add, add ? strlen(add) : 0,
1238 del, del ? strlen(del) : 0);
1239 silc_buffer_free(idp);
1242 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1243 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1244 This function is used to send the packet between routers as broadcast
1247 void silc_server_send_notify_invite(SilcServer server,
1248 SilcSocketConnection sock,
1250 SilcChannelEntry channel,
1251 SilcClientID *client_id,
1252 char *add, char *del)
1254 SilcBuffer idp, idp2;
1256 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1257 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1258 silc_server_send_notify(server, sock, broadcast,
1259 SILC_NOTIFY_TYPE_INVITE, 5,
1260 idp->data, idp->len,
1261 channel->channel_name, strlen(channel->channel_name),
1262 idp2->data, idp2->len,
1263 add, add ? strlen(add) : 0,
1264 del, del ? strlen(del) : 0);
1265 silc_buffer_free(idp);
1266 silc_buffer_free(idp2);
1269 /* Sends notify message destined to specific entity. */
1271 void silc_server_send_notify_dest(SilcServer server,
1272 SilcSocketConnection sock,
1275 SilcIdType dest_id_type,
1276 SilcNotifyType type,
1284 packet = silc_notify_payload_encode(type, argc, ap);
1285 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
1286 dest_id, dest_id_type,
1287 packet->data, packet->len, FALSE);
1288 silc_buffer_free(packet);
1292 /* Sends notify message to a channel. The notify message sent is
1293 distributed to all clients on the channel. If `route_notify' is TRUE
1294 then the notify may be routed to primary route or to some other routers.
1295 If FALSE it is assured that the notify is sent only locally. If `sender'
1296 is provided then the packet is not sent to that connection since it
1297 originally came from it. */
1299 void silc_server_send_notify_to_channel(SilcServer server,
1300 SilcSocketConnection sender,
1301 SilcChannelEntry channel,
1302 unsigned char route_notify,
1303 SilcNotifyType type,
1311 packet = silc_notify_payload_encode(type, argc, ap);
1312 silc_server_packet_send_to_channel(server, sender, channel,
1313 SILC_PACKET_NOTIFY, route_notify,
1314 packet->data, packet->len, FALSE);
1315 silc_buffer_free(packet);
1319 /* Send notify message to all channels the client has joined. It is quaranteed
1320 that the message is sent only once to a client (ie. if a client is joined
1321 on two same channel it will receive only one notify message). Also, this
1322 sends only to local clients (locally connected if we are server, and to
1323 local servers if we are router). If `sender' is provided the packet is
1324 not sent to that client at all. */
1326 void silc_server_send_notify_on_channels(SilcServer server,
1327 SilcClientEntry sender,
1328 SilcClientEntry client,
1329 SilcNotifyType type,
1333 SilcSocketConnection sock = NULL;
1334 SilcPacketContext packetdata;
1336 SilcClientEntry *sent_clients = NULL;
1337 uint32 sent_clients_count = 0;
1338 SilcServerEntry *routed = NULL;
1339 uint32 routed_count = 0;
1340 SilcHashTableList htl, htl2;
1341 SilcChannelEntry channel;
1342 SilcChannelClientEntry chl, chl2;
1343 SilcIDListData idata;
1345 unsigned char *data;
1347 int force_send = FALSE;
1350 SILC_LOG_DEBUG(("Start"));
1352 if (!silc_hash_table_count(client->channels))
1356 packet = silc_notify_payload_encode(type, argc, ap);
1357 data = packet->data;
1358 data_len = packet->len;
1360 /* Set the packet context pointers. */
1361 packetdata.flags = 0;
1362 packetdata.type = SILC_PACKET_NOTIFY;
1363 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1364 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1365 packetdata.src_id_type = SILC_ID_SERVER;
1367 silc_hash_table_list(client->channels, &htl);
1368 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1369 channel = chl->channel;
1371 /* Send the message to all clients on the channel's client list. */
1372 silc_hash_table_list(channel->user_list, &htl2);
1373 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1376 if (sender && c == sender)
1379 /* Check if we have sent the packet to this client already */
1380 for (k = 0; k < sent_clients_count; k++)
1381 if (sent_clients[k] == c)
1383 if (k < sent_clients_count)
1386 /* If we are router and if this client has router set it is not
1387 locally connected client and we will route the message to the
1388 router set in the client. */
1389 if (c && c->router && server->server_type == SILC_ROUTER) {
1390 /* Check if we have sent the packet to this route already */
1391 for (k = 0; k < routed_count; k++)
1392 if (routed[k] == c->router)
1394 if (k < routed_count)
1397 /* Get data used in packet header encryption, keys and stuff. */
1398 sock = (SilcSocketConnection)c->router->connection;
1399 idata = (SilcIDListData)c->router;
1401 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1402 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1403 packetdata.dst_id_type = SILC_ID_SERVER;
1404 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1405 packetdata.src_id_len + packetdata.dst_id_len;
1406 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1408 /* Send the packet */
1409 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1412 data, data_len, FALSE,
1415 silc_free(packetdata.dst_id);
1417 /* We want to make sure that the packet is routed to same router
1418 only once. Mark this route as sent route. */
1420 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1421 routed[k] = c->router;
1430 /* Send to locally connected client */
1433 /* Get data used in packet header encryption, keys and stuff. */
1434 sock = (SilcSocketConnection)c->connection;
1435 idata = (SilcIDListData)c;
1437 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1438 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1439 packetdata.dst_id_type = SILC_ID_CLIENT;
1440 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1441 packetdata.src_id_len + packetdata.dst_id_len;
1442 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1444 /* Send the packet */
1445 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1448 data, data_len, FALSE,
1451 silc_free(packetdata.dst_id);
1453 /* Make sure that we send the notify only once per client. */
1454 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1455 (sent_clients_count + 1));
1456 sent_clients[sent_clients_count] = c;
1457 sent_clients_count++;
1464 if (sent_clients_count)
1465 silc_free(sent_clients);
1466 silc_free(packetdata.src_id);
1470 /* Sends New ID Payload to remote end. The packet is used to distribute
1471 information about new registered clients, servers, channel etc. usually
1472 to routers so that they can keep these information up to date.
1473 If the argument `broadcast' is TRUE then the packet is sent as
1474 broadcast packet. */
1476 void silc_server_send_new_id(SilcServer server,
1477 SilcSocketConnection sock,
1479 void *id, SilcIdType id_type,
1484 SILC_LOG_DEBUG(("Start"));
1486 idp = silc_id_payload_encode(id, id_type);
1487 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1488 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1489 idp->data, idp->len, FALSE);
1490 silc_buffer_free(idp);
1493 /* Send New Channel Payload to notify about newly created channel in the
1494 SILC network. Normal server nevers sends this packet. Router uses this
1495 to notify other routers in the network about new channel. This packet
1498 void silc_server_send_new_channel(SilcServer server,
1499 SilcSocketConnection sock,
1503 uint32 channel_id_len,
1508 uint32 name_len = strlen(channel_name);
1510 SILC_LOG_DEBUG(("Start"));
1512 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1516 /* Encode the channel payload */
1517 packet = silc_channel_payload_encode(channel_name, name_len,
1518 cid, channel_id_len, mode);
1520 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1521 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1522 packet->data, packet->len, FALSE);
1525 silc_buffer_free(packet);
1528 /* Send Channel Key payload to distribute the new channel key. Normal server
1529 sends this to router when new client joins to existing channel. Router
1530 sends this to the local server who sent the join command in case where
1531 the channel did not exist yet. Both normal and router servers uses this
1532 also to send this to locally connected clients on the channel. This
1533 must not be broadcasted packet. Routers do not send this to each other.
1534 If `sender is provided then the packet is not sent to that connection since
1535 it originally came from it. */
1537 void silc_server_send_channel_key(SilcServer server,
1538 SilcSocketConnection sender,
1539 SilcChannelEntry channel,
1540 unsigned char route)
1543 unsigned char *chid;
1546 SILC_LOG_DEBUG(("Start"));
1548 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1552 /* Encode channel key packet */
1553 tmp_len = strlen(channel->channel_key->cipher->name);
1554 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1557 channel->channel_key->cipher->name,
1558 channel->key_len / 8, channel->key);
1560 silc_server_packet_send_to_channel(server, sender, channel,
1561 SILC_PACKET_CHANNEL_KEY,
1562 route, packet->data, packet->len, FALSE);
1563 silc_buffer_free(packet);
1567 /* Generic function to send any command. The arguments must be sent already
1568 encoded into correct form in correct order. */
1570 void silc_server_send_command(SilcServer server,
1571 SilcSocketConnection sock,
1572 SilcCommand command,
1581 packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1582 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1583 packet->data, packet->len, TRUE);
1584 silc_buffer_free(packet);
1588 /* Send the heartbeat packet. */
1590 void silc_server_send_heartbeat(SilcServer server,
1591 SilcSocketConnection sock)
1593 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1597 /* Generic function to relay packet we've received. This is used to relay
1598 packets to a client but generally can be used to other purposes as well. */
1600 void silc_server_relay_packet(SilcServer server,
1601 SilcSocketConnection dst_sock,
1604 SilcPacketContext *packet,
1607 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1608 + packet->dst_id_len + packet->padlen);
1610 silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1611 silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1613 /* Re-encrypt packet */
1614 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1616 /* Send the packet */
1617 silc_server_packet_send_real(server, dst_sock, force_send);
1619 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1620 + packet->dst_id_len + packet->padlen);
1623 /* Routine used to send the connection authentication packet. */
1625 void silc_server_send_connection_auth_request(SilcServer server,
1626 SilcSocketConnection sock,
1628 SilcAuthMethod auth_meth)
1632 packet = silc_buffer_alloc(4);
1633 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1634 silc_buffer_format(packet,
1635 SILC_STR_UI_SHORT(conn_type),
1636 SILC_STR_UI_SHORT(auth_meth),
1639 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1640 0, packet->data, packet->len, FALSE);
1641 silc_buffer_free(packet);