5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
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 rekey protocol is active we must assure that all packets are
39 sent through packet queue. */
40 if (SILC_SERVER_IS_REKEY(sock))
44 ret = silc_packet_send(sock, force_send);
48 /* Mark that there is some outgoing data available for this connection.
49 This call sets the connection both for input and output (the input
50 is set always and this call keeps the input setting, actually).
51 Actual data sending is performed by silc_server_packet_process. */
52 SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
54 /* Mark to socket that data is pending in outgoing buffer. This flag
55 is needed if new data is added to the buffer before the earlier
56 put data is sent to the network. */
57 SILC_SET_OUTBUF_PENDING(sock);
62 /* Assembles a new packet to be sent out to network. This doesn't actually
63 send the packet but creates the packet and fills the outgoing data
64 buffer and marks the packet ready to be sent to network. However, If
65 argument force_send is TRUE the packet is sent immediately and not put
66 to queue. Normal case is that the packet is not sent immediately. */
68 void silc_server_packet_send(SilcServer server,
69 SilcSocketConnection sock,
71 SilcPacketFlags flags,
77 SilcIdType dst_id_type = SILC_ID_NONE;
82 /* Get data used in the packet sending, keys and stuff */
84 case SILC_SOCKET_TYPE_CLIENT:
85 dst_id = ((SilcClientEntry)sock->user_data)->id;
86 dst_id_type = SILC_ID_CLIENT;
88 case SILC_SOCKET_TYPE_SERVER:
89 case SILC_SOCKET_TYPE_ROUTER:
90 dst_id = ((SilcServerEntry)sock->user_data)->id;
91 dst_id_type = SILC_ID_SERVER;
97 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
98 dst_id_type, data, data_len, force_send);
101 /* Assembles a new packet to be sent out to network. This doesn't actually
102 send the packet but creates the packet and fills the outgoing data
103 buffer and marks the packet ready to be sent to network. However, If
104 argument force_send is TRUE the packet is sent immediately and not put
105 to queue. Normal case is that the packet is not sent immediately.
106 Destination information is sent as argument for this function. */
108 void silc_server_packet_send_dest(SilcServer server,
109 SilcSocketConnection sock,
111 SilcPacketFlags flags,
113 SilcIdType dst_id_type,
118 SilcPacketContext packetdata;
119 SilcIDListData idata;
120 SilcCipher cipher = NULL;
121 SilcHmac hmac = NULL;
122 unsigned char *dst_id_data = NULL;
123 uint32 dst_id_len = 0;
125 SILC_LOG_DEBUG(("Sending packet, type %d", type));
127 /* Get data used in the packet sending, keys and stuff */
128 idata = (SilcIDListData)sock->user_data;
131 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
132 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
135 /* Set the packet context pointers */
136 packetdata.type = type;
137 packetdata.flags = flags;
138 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
139 packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
140 packetdata.src_id_type = server->id_type;
141 packetdata.dst_id = dst_id_data;
142 packetdata.dst_id_len = dst_id_len;
143 packetdata.dst_id_type = dst_id_type;
144 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
145 packetdata.src_id_len + dst_id_len;
146 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
148 /* Prepare outgoing data buffer for packet sending */
149 silc_packet_send_prepare(sock,
150 SILC_PACKET_HEADER_LEN +
151 packetdata.src_id_len +
152 packetdata.dst_id_len,
156 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
158 packetdata.buffer = sock->outbuf;
160 /* Put the data to the buffer */
161 if (data && data_len)
162 silc_buffer_put(sock->outbuf, data, data_len);
164 /* Create the outgoing packet */
165 silc_packet_assemble(&packetdata);
168 cipher = idata->send_key;
169 hmac = idata->hmac_send;
172 /* Encrypt the packet */
173 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
175 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
176 sock->outbuf->data, sock->outbuf->len);
178 /* Now actually send the packet */
179 silc_server_packet_send_real(server, sock, force_send);
181 if (packetdata.src_id)
182 silc_free(packetdata.src_id);
183 if (packetdata.dst_id)
184 silc_free(packetdata.dst_id);
187 /* Assembles a new packet to be sent out to network. This doesn't actually
188 send the packet but creates the packet and fills the outgoing data
189 buffer and marks the packet ready to be sent to network. However, If
190 argument force_send is TRUE the packet is sent immediately and not put
191 to queue. Normal case is that the packet is not sent immediately.
192 The source and destination information is sent as argument for this
195 void silc_server_packet_send_srcdest(SilcServer server,
196 SilcSocketConnection sock,
198 SilcPacketFlags flags,
200 SilcIdType src_id_type,
202 SilcIdType dst_id_type,
207 SilcPacketContext packetdata;
208 SilcIDListData idata;
209 SilcCipher cipher = NULL;
210 SilcHmac hmac = NULL;
211 unsigned char *dst_id_data = NULL;
212 uint32 dst_id_len = 0;
213 unsigned char *src_id_data = NULL;
214 uint32 src_id_len = 0;
216 SILC_LOG_DEBUG(("Sending packet, type %d", type));
218 /* Get data used in the packet sending, keys and stuff */
219 idata = (SilcIDListData)sock->user_data;
222 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
223 dst_id_len = silc_id_get_len(dst_id, dst_id_type);
227 src_id_data = silc_id_id2str(src_id, src_id_type);
228 src_id_len = silc_id_get_len(src_id, src_id_type);
231 /* Set the packet context pointers */
232 packetdata.type = type;
233 packetdata.flags = flags;
234 packetdata.src_id = src_id_data;
235 packetdata.src_id_len = src_id_len;
236 packetdata.src_id_type = src_id_type;
237 packetdata.dst_id = dst_id_data;
238 packetdata.dst_id_len = dst_id_len;
239 packetdata.dst_id_type = dst_id_type;
240 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
241 packetdata.src_id_len + dst_id_len;
242 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
244 /* Prepare outgoing data buffer for packet sending */
245 silc_packet_send_prepare(sock,
246 SILC_PACKET_HEADER_LEN +
247 packetdata.src_id_len +
248 packetdata.dst_id_len,
252 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
254 packetdata.buffer = sock->outbuf;
256 /* Put the data to the buffer */
257 if (data && data_len)
258 silc_buffer_put(sock->outbuf, data, data_len);
260 /* Create the outgoing packet */
261 silc_packet_assemble(&packetdata);
264 cipher = idata->send_key;
265 hmac = idata->hmac_send;
268 /* Encrypt the packet */
269 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
271 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
272 sock->outbuf->data, sock->outbuf->len);
274 /* Now actually send the packet */
275 silc_server_packet_send_real(server, sock, force_send);
277 if (packetdata.src_id)
278 silc_free(packetdata.src_id);
279 if (packetdata.dst_id)
280 silc_free(packetdata.dst_id);
283 /* Broadcast received packet to our primary route. This function is used
284 by router to further route received broadcast packet. It is expected
285 that the broadcast flag from the packet is checked before calling this
286 function. This does not test or set the broadcast flag. */
288 void silc_server_packet_broadcast(SilcServer server,
289 SilcSocketConnection sock,
290 SilcPacketContext *packet)
292 SilcBuffer buffer = packet->buffer;
293 SilcIDListData idata;
296 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
298 /* If the packet is originated from our primary route we are
299 not allowed to send the packet. */
300 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
301 if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
302 idata = (SilcIDListData)sock->user_data;
304 silc_buffer_push(buffer, buffer->data - buffer->head);
305 silc_packet_send_prepare(sock, 0, 0, buffer->len);
306 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
307 silc_packet_encrypt(idata->send_key, idata->hmac_send,
308 sock->outbuf, sock->outbuf->len);
310 SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
311 sock->outbuf->data, sock->outbuf->len);
313 /* Now actually send the packet */
314 silc_server_packet_send_real(server, sock, TRUE);
319 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
320 "original sender of this packet"));
324 /* Routes received packet to `sock'. This is used to route the packets that
325 router receives but are not destined to it. */
327 void silc_server_packet_route(SilcServer server,
328 SilcSocketConnection sock,
329 SilcPacketContext *packet)
331 SilcBuffer buffer = packet->buffer;
332 SilcIDListData idata;
334 SILC_LOG_DEBUG(("Routing received packet"));
336 idata = (SilcIDListData)sock->user_data;
338 silc_buffer_push(buffer, buffer->data - buffer->head);
339 silc_packet_send_prepare(sock, 0, 0, buffer->len);
340 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
341 silc_packet_encrypt(idata->send_key, idata->hmac_send,
342 sock->outbuf, sock->outbuf->len);
344 SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
345 sock->outbuf->data, sock->outbuf->len);
347 /* Now actually send the packet */
348 silc_server_packet_send_real(server, sock, TRUE);
351 /* Internal routine to actually create the channel packet and send it
352 to network. This is common function in channel message sending. If
353 `channel_message' is TRUE this encrypts the message as it is strictly
354 a channel message. If FALSE normal encryption process is used. */
357 silc_server_packet_send_to_channel_real(SilcServer server,
358 SilcSocketConnection sock,
359 SilcPacketContext *packet,
367 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
368 packet->src_id_len + packet->dst_id_len;
370 /* Prepare outgoing data buffer for packet sending */
371 silc_packet_send_prepare(sock,
372 SILC_PACKET_HEADER_LEN +
378 packet->buffer = sock->outbuf;
380 /* Put the data to buffer, assemble and encrypt the packet. The packet
381 is encrypted with normal session key shared with the client. */
382 silc_buffer_put(sock->outbuf, data, data_len);
383 silc_packet_assemble(packet);
385 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
386 packet->src_id_len + packet->dst_id_len +
389 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
391 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
392 sock->outbuf->data, sock->outbuf->len);
394 /* Now actually send the packet */
395 silc_server_packet_send_real(server, sock, force_send);
398 /* This routine is used by the server to send packets to channel. The
399 packet sent with this function is distributed to all clients on
400 the channel. Usually this is used to send notify messages to the
401 channel, things like notify about new user joining to the channel.
402 If `route' is FALSE then the packet is sent only locally and will not
403 be routed anywhere (for router locally means cell wide). If `sender'
404 is provided then the packet is not sent to that connection since it
405 originally came from it. */
407 void silc_server_packet_send_to_channel(SilcServer server,
408 SilcSocketConnection sender,
409 SilcChannelEntry channel,
416 SilcSocketConnection sock = NULL;
417 SilcPacketContext packetdata;
418 SilcClientEntry client = NULL;
419 SilcServerEntry *routed = NULL;
420 SilcChannelClientEntry chl;
421 SilcIDListData idata;
422 uint32 routed_count = 0;
424 /* This doesn't send channel message packets */
425 if (type == SILC_PACKET_CHANNEL_MESSAGE)
428 SILC_LOG_DEBUG(("Sending packet to channel"));
430 /* Set the packet context pointers. */
431 packetdata.flags = 0;
432 packetdata.type = type;
433 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
434 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
435 packetdata.src_id_type = SILC_ID_SERVER;
436 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
437 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
438 packetdata.dst_id_type = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
439 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
440 packetdata.src_id_len + packetdata.dst_id_len;
441 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
443 /* If there are global users in the channel we will send the message
444 first to our router for further routing. */
445 if (route && server->server_type == SILC_SERVER && !server->standalone &&
446 channel->global_users) {
447 SilcServerEntry router;
449 /* Get data used in packet header encryption, keys and stuff. */
450 router = server->router;
451 sock = (SilcSocketConnection)router->connection;
452 idata = (SilcIDListData)router;
454 if (sock != sender) {
455 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
457 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
460 data, data_len, FALSE,
465 /* Send the message to clients on the channel's client list. */
466 silc_list_start(channel->user_list);
467 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
468 client = chl->client;
470 /* If client has router set it is not locally connected client and
471 we will route the message to the router set in the client. Though,
472 send locally connected server in all cases. */
473 if (server->server_type == SILC_ROUTER && client && client->router &&
474 ((!route && client->router->router == server->id_entry) || route)) {
477 /* Check if we have sent the packet to this route already */
478 for (k = 0; k < routed_count; k++)
479 if (routed[k] == client->router)
481 if (k < routed_count)
484 /* Get data used in packet header encryption, keys and stuff. */
485 sock = (SilcSocketConnection)client->router->connection;
486 idata = (SilcIDListData)client->router;
488 if (sender && sock == sender)
491 /* Send the packet */
492 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
495 data, data_len, FALSE,
498 /* We want to make sure that the packet is routed to same router
499 only once. Mark this route as sent route. */
501 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
502 routed[k] = client->router;
508 if (client && client->router)
511 /* Send to locally connected client */
514 /* Get data used in packet header encryption, keys and stuff. */
515 sock = (SilcSocketConnection)client->connection;
516 idata = (SilcIDListData)client;
518 if (sender && sock == sender)
521 /* Send the packet */
522 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
525 data, data_len, FALSE,
532 silc_free(packetdata.src_id);
533 silc_free(packetdata.dst_id);
536 /* This checks whether the relayed packet came from router. If it did
537 then we'll need to encrypt it with the channel key. This is called
538 from the silc_server_packet_relay_to_channel. */
541 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
542 SilcSocketConnection sock,
543 SilcChannelEntry channel,
545 unsigned int data_len)
547 /* If we are router and the packet came from router and private key
548 has not been set for the channel then we must encrypt the packet
549 as it was decrypted with the session key shared between us and the
550 router which sent it. This is so, because cells does not share the
552 if (server->server_type == SILC_ROUTER &&
553 sock->type == SILC_SOCKET_TYPE_ROUTER &&
554 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
555 channel->channel_key) {
558 uint16 data_len, flags;
560 iv_len = silc_cipher_get_block_len(channel->channel_key);
561 if (channel->iv[0] == '\0')
562 for (i = 0; i < iv_len; i++) channel->iv[i] =
563 silc_rng_get_byte(server->rng);
565 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
567 /* Encode new payload. This encrypts it also. */
568 SILC_GET16_MSB(flags, data);
569 SILC_GET16_MSB(data_len, data + 2);
570 chp = silc_channel_message_payload_encode(flags, data_len,
573 channel->channel_key,
575 memcpy(data, chp->data, chp->len);
576 silc_buffer_free(chp);
580 /* This routine is explicitly used to relay messages to some channel.
581 Packets sent with this function we have received earlier and are
582 totally encrypted. This just sends the packet to all clients on
583 the channel. If the sender of the packet is someone on the channel
584 the message will not be sent to that client. The SILC Packet header
585 is encrypted with the session key shared between us and the client.
586 MAC is also computed before encrypting the header. Rest of the
587 packet will be untouched. */
589 void silc_server_packet_relay_to_channel(SilcServer server,
590 SilcSocketConnection sender_sock,
591 SilcChannelEntry channel,
593 SilcIdType sender_type,
600 SilcSocketConnection sock = NULL;
601 SilcPacketContext packetdata;
602 SilcClientEntry client = NULL;
603 SilcServerEntry *routed = NULL;
604 SilcChannelClientEntry chl;
605 uint32 routed_count = 0;
606 SilcIDListData idata;
608 SILC_LOG_DEBUG(("Relaying packet to channel"));
610 /* Set the packet context pointers. */
611 packetdata.flags = 0;
612 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
613 packetdata.src_id = silc_id_id2str(sender, sender_type);
614 packetdata.src_id_len = silc_id_get_len(sender, sender_type);
615 packetdata.src_id_type = sender_type;
616 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
617 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
618 packetdata.dst_id_type = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
619 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
620 packetdata.src_id_len +
621 packetdata.dst_id_len));
623 /* This encrypts the packet, if needed. It will be encrypted if
624 it came from the router thus it needs to be encrypted with the
625 channel key. If the channel key does not exist, then we know we
626 don't have a single local user on the channel. */
627 silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
631 /* If there are global users in the channel we will send the message
632 first to our router for further routing. */
633 if (server->server_type == SILC_SERVER && !server->standalone &&
634 channel->global_users) {
635 SilcServerEntry router;
637 router = server->router;
639 /* Check that the sender is not our router. */
640 if (sender_sock != (SilcSocketConnection)router->connection) {
642 /* Get data used in packet header encryption, keys and stuff. */
643 sock = (SilcSocketConnection)router->connection;
644 idata = (SilcIDListData)router;
646 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
648 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
651 data, data_len, TRUE,
656 /* Send the message to clients on the channel's client list. */
657 silc_list_start(channel->user_list);
658 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
659 client = chl->client;
663 /* If sender is one on the channel do not send it the packet. */
664 if (!found && sender_type == SILC_ID_CLIENT &&
665 SILC_ID_CLIENT_COMPARE(client->id, sender)) {
670 /* If the client has set router it means that it is not locally
671 connected client and we will route the packet further. */
672 if (server->server_type == SILC_ROUTER && client->router) {
675 /* Sender maybe server as well so we want to make sure that
676 we won't send the message to the server it came from. */
677 if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
682 /* Check if we have sent the packet to this route already */
683 for (k = 0; k < routed_count; k++)
684 if (routed[k] == client->router)
686 if (k < routed_count)
689 /* Get data used in packet header encryption, keys and stuff. */
690 sock = (SilcSocketConnection)client->router->connection;
691 idata = (SilcIDListData)client->router;
693 /* Do not send to the sender. Check first whether the true
694 sender's router is same as this client's router. Also check
695 if the sender socket is the same as this client's router
698 ((SilcClientEntry)sender_entry)->router == client->router)
700 if (sender_sock && sock == sender_sock)
703 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
704 silc_id_render(client->id, SILC_ID_CLIENT),
705 sock->hostname, sock->ip));
707 /* We want to make sure that the packet is routed to same router
708 only once. Mark this route as sent route. */
710 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
711 routed[k] = client->router;
714 /* If the remote connection is router then we'll decrypt the
715 channel message and re-encrypt it with the session key shared
716 between us and the remote router. This is done because the
717 channel keys are cell specific and we have different channel
718 key than the remote router has. */
719 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
721 /* If private key mode is not set then decrypt the packet
723 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
724 unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
725 memcpy(tmp, data, data_len);
727 /* Decrypt the channel message (we don't check the MAC) */
728 /* XXX this could be optimized and removed all together by
729 taking a copy of the original data before encrypting it
730 and thus would not required decrypting. */
731 if (channel->channel_key &&
732 !silc_channel_message_payload_decrypt(tmp, data_len,
733 channel->channel_key,
735 memset(tmp, 0, data_len);
740 /* Now re-encrypt and send it to the router */
741 silc_server_packet_send_srcdest(server, sock,
742 SILC_PACKET_CHANNEL_MESSAGE, 0,
744 channel->id, SILC_ID_CHANNEL,
745 tmp, data_len, force_send);
747 /* Free the copy of the channel message */
748 memset(tmp, 0, data_len);
751 /* Private key mode is set, we don't have the channel key, so
752 just re-encrypt the entire packet and send it to the router. */
753 silc_server_packet_send_srcdest(server, sock,
754 SILC_PACKET_CHANNEL_MESSAGE, 0,
756 channel->id, SILC_ID_CHANNEL,
757 data, data_len, force_send);
762 /* Send the packet (to normal server) */
763 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
766 data, data_len, TRUE,
772 if (client && client->router)
775 /* Get data used in packet header encryption, keys and stuff. */
776 sock = (SilcSocketConnection)client->connection;
777 idata = (SilcIDListData)client;
779 if (sender_sock && sock == sender_sock)
782 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
783 silc_id_render(client->id, SILC_ID_CLIENT),
784 sock->hostname, sock->ip));
786 /* Send the packet */
787 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
790 data, data_len, TRUE,
795 silc_free(packetdata.src_id);
796 silc_free(packetdata.dst_id);
799 /* This function is used to send packets strictly to all local clients
800 on a particular channel. This is used for example to distribute new
801 channel key to all our locally connected clients on the channel.
802 The packets are always encrypted with the session key shared between
803 the client, this means these are not _to the channel_ but _to the client_
806 void silc_server_packet_send_local_channel(SilcServer server,
807 SilcChannelEntry channel,
809 SilcPacketFlags flags,
814 SilcChannelClientEntry chl;
815 SilcSocketConnection sock = NULL;
817 SILC_LOG_DEBUG(("Start"));
819 /* Send the message to clients on the channel's client list. */
820 silc_list_start(channel->user_list);
821 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
822 if (chl->client && !chl->client->router) {
823 sock = (SilcSocketConnection)chl->client->connection;
825 /* Send the packet to the client */
826 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
827 SILC_ID_CLIENT, data, data_len,
833 /* Routine used to send (relay, route) private messages to some destination.
834 If the private message key does not exist then the message is re-encrypted,
835 otherwise we just pass it along. This really is not used to send new
836 private messages (as server does not send them) but to relay received
839 void silc_server_send_private_message(SilcServer server,
840 SilcSocketConnection dst_sock,
843 SilcPacketContext *packet)
845 SilcBuffer buffer = packet->buffer;
847 /* Re-encrypt and send if private messge key does not exist */
848 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
850 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
851 + packet->dst_id_len + packet->padlen);
852 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
853 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
855 /* Re-encrypt packet */
856 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
858 /* Send the packet */
859 silc_server_packet_send_real(server, dst_sock, FALSE);
862 /* Key exist so encrypt just header and send it */
863 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
864 + packet->dst_id_len + packet->padlen);
865 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
866 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
869 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf,
870 SILC_PACKET_HEADER_LEN + packet->src_id_len +
871 packet->dst_id_len + packet->padlen);
873 silc_server_packet_send_real(server, dst_sock, FALSE);
877 /* Sends current motd to client */
879 void silc_server_send_motd(SilcServer server,
880 SilcSocketConnection sock)
885 if (server->config && server->config->motd &&
886 server->config->motd->motd_file) {
888 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
892 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
898 /* Sends error message. Error messages may or may not have any
901 void silc_server_send_error(SilcServer server,
902 SilcSocketConnection sock,
903 const char *fmt, ...)
906 unsigned char buf[4096];
908 memset(buf, 0, sizeof(buf));
910 vsprintf(buf, fmt, ap);
913 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
914 buf, strlen(buf), FALSE);
917 /* Sends notify message. If format is TRUE the variable arguments are
918 formatted and the formatted string is sent as argument payload. If it is
919 FALSE then each argument is sent as separate argument and their format
920 in the argument list must be { argument data, argument length }. */
922 void silc_server_send_notify(SilcServer server,
923 SilcSocketConnection sock,
933 packet = silc_notify_payload_encode(type, argc, ap);
934 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
935 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
936 packet->data, packet->len, FALSE);
937 silc_buffer_free(packet);
940 /* Sends notify message and gets the arguments from the `args' Argument
943 void silc_server_send_notify_args(SilcServer server,
944 SilcSocketConnection sock,
952 packet = silc_notify_payload_encode_args(type, argc, args);
953 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
954 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
955 packet->data, packet->len, FALSE);
956 silc_buffer_free(packet);
959 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
960 `old_id' with the `new_id'. */
962 void silc_server_send_notify_channel_change(SilcServer server,
963 SilcSocketConnection sock,
965 SilcChannelID *old_id,
966 SilcChannelID *new_id)
968 SilcBuffer idp1, idp2;
970 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
971 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
973 silc_server_send_notify(server, sock, broadcast,
974 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
975 2, idp1->data, idp1->len, idp2->data, idp2->len);
976 silc_buffer_free(idp1);
977 silc_buffer_free(idp2);
980 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
981 `old_id' with the `new_id'. */
983 void silc_server_send_notify_nick_change(SilcServer server,
984 SilcSocketConnection sock,
986 SilcClientID *old_id,
987 SilcClientID *new_id)
989 SilcBuffer idp1, idp2;
991 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
992 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
994 silc_server_send_notify(server, sock, broadcast,
995 SILC_NOTIFY_TYPE_NICK_CHANGE,
996 2, idp1->data, idp1->len, idp2->data, idp2->len);
997 silc_buffer_free(idp1);
998 silc_buffer_free(idp2);
1001 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1002 has joined to the `channel'. */
1004 void silc_server_send_notify_join(SilcServer server,
1005 SilcSocketConnection sock,
1007 SilcChannelEntry channel,
1008 SilcClientID *client_id)
1010 SilcBuffer idp1, idp2;
1012 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1013 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1014 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1015 2, idp1->data, idp1->len,
1016 idp2->data, idp2->len);
1017 silc_buffer_free(idp1);
1018 silc_buffer_free(idp2);
1021 /* Sends LEAVE notify type. This tells that `client_id' has left the
1022 `channel'. The Notify packet is always destined to the channel. */
1024 void silc_server_send_notify_leave(SilcServer server,
1025 SilcSocketConnection sock,
1027 SilcChannelEntry channel,
1028 SilcClientID *client_id)
1032 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1033 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1034 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1035 1, idp->data, idp->len);
1036 silc_buffer_free(idp);
1039 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1040 `channel' mode to `mode. The Notify packet is always destined to
1043 void silc_server_send_notify_cmode(SilcServer server,
1044 SilcSocketConnection sock,
1046 SilcChannelEntry channel,
1048 void *id, SilcIdType id_type,
1049 char *cipher, char *hmac)
1052 unsigned char mode[4];
1054 idp = silc_id_payload_encode((void *)id, id_type);
1055 SILC_PUT32_MSB(mode_mask, mode);
1057 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1058 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1059 4, idp->data, idp->len,
1061 cipher, cipher ? strlen(cipher) : 0,
1062 hmac, hmac ? strlen(hmac) : 0);
1063 silc_buffer_free(idp);
1066 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1067 `target' client's mode on `channel'. The Notify packet is always
1068 destined to the channel. */
1070 void silc_server_send_notify_cumode(SilcServer server,
1071 SilcSocketConnection sock,
1073 SilcChannelEntry channel,
1075 void *id, SilcIdType id_type,
1076 SilcClientID *target)
1078 SilcBuffer idp1, idp2;
1079 unsigned char mode[4];
1081 idp1 = silc_id_payload_encode((void *)id, id_type);
1082 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1083 SILC_PUT32_MSB(mode_mask, mode);
1085 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1087 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
1088 idp1->data, idp1->len,
1090 idp2->data, idp2->len);
1091 silc_buffer_free(idp1);
1092 silc_buffer_free(idp2);
1095 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1096 left SILC network. This function is used only between server and router
1097 traffic. This is not used to send the notify to the channel for
1098 client. The `message may be NULL. */
1100 void silc_server_send_notify_signoff(SilcServer server,
1101 SilcSocketConnection sock,
1103 SilcClientID *client_id,
1108 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1109 silc_server_send_notify(server, sock, broadcast,
1110 SILC_NOTIFY_TYPE_SIGNOFF,
1111 message ? 2 : 1, idp->data, idp->len,
1112 message, message ? strlen(message): 0);
1113 silc_buffer_free(idp);
1116 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1117 the `channel's topic to `topic'. The Notify packet is always destined
1118 to the channel. This function is used to send the topic set notifies
1121 void silc_server_send_notify_topic_set(SilcServer server,
1122 SilcSocketConnection sock,
1124 SilcChannelEntry channel,
1125 SilcClientID *client_id,
1130 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1131 silc_server_send_notify(server, sock, broadcast,
1132 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1134 idp->data, idp->len,
1135 topic, topic ? strlen(topic) : 0);
1136 silc_buffer_free(idp);
1139 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1140 was kicked off the channel. The `comment' may indicate the reason
1141 for the kicking. This function is used only between server and router
1144 void silc_server_send_notify_kicked(SilcServer server,
1145 SilcSocketConnection sock,
1147 SilcChannelEntry channel,
1148 SilcClientID *client_id,
1153 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1154 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1155 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1156 comment ? 2 : 1, idp->data, idp->len,
1157 comment, comment ? strlen(comment) : 0);
1158 silc_buffer_free(idp);
1161 /* Send KILLED notify type. This tells that the `client_id' client was
1162 killed from the network. The `comment' may indicate the reason
1165 void silc_server_send_notify_killed(SilcServer server,
1166 SilcSocketConnection sock,
1168 SilcClientID *client_id,
1173 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1174 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1175 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1176 comment ? 2 : 1, idp->data, idp->len,
1177 comment, comment ? strlen(comment) : 0);
1178 silc_buffer_free(idp);
1181 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1182 user mode in the SILC Network was changed. This function is used to
1183 send the packet between routers as broadcast packet. */
1185 void silc_server_send_notify_umode(SilcServer server,
1186 SilcSocketConnection sock,
1188 SilcClientID *client_id,
1192 unsigned char mode[4];
1194 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1195 SILC_PUT32_MSB(mode_mask, mode);
1197 silc_server_send_notify(server, sock, broadcast,
1198 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1199 idp->data, idp->len,
1201 silc_buffer_free(idp);
1204 /* Sends BAN notify type. This tells that ban has been either `add'ed
1205 or `del'eted on the `channel. This function is used to send the packet
1206 between routers as broadcast packet. */
1208 void silc_server_send_notify_ban(SilcServer server,
1209 SilcSocketConnection sock,
1211 SilcChannelEntry channel,
1212 char *add, char *del)
1216 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1217 silc_server_send_notify(server, sock, broadcast,
1218 SILC_NOTIFY_TYPE_BAN, 3,
1219 idp->data, idp->len,
1220 add, add ? strlen(add) : 0,
1221 del, del ? strlen(del) : 0);
1222 silc_buffer_free(idp);
1225 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1226 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1227 This function is used to send the packet between routers as broadcast
1230 void silc_server_send_notify_invite(SilcServer server,
1231 SilcSocketConnection sock,
1233 SilcChannelEntry channel,
1234 SilcClientID *client_id,
1235 char *add, char *del)
1237 SilcBuffer idp, idp2;
1239 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1240 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1241 silc_server_send_notify(server, sock, broadcast,
1242 SILC_NOTIFY_TYPE_INVITE, 5,
1243 idp->data, idp->len,
1244 channel->channel_name, strlen(channel->channel_name),
1245 idp2->data, idp2->len,
1246 add, add ? strlen(add) : 0,
1247 del, del ? strlen(del) : 0);
1248 silc_buffer_free(idp);
1249 silc_buffer_free(idp2);
1252 /* Sends notify message destined to specific entity. */
1254 void silc_server_send_notify_dest(SilcServer server,
1255 SilcSocketConnection sock,
1258 SilcIdType dest_id_type,
1259 SilcNotifyType type,
1267 packet = silc_notify_payload_encode(type, argc, ap);
1268 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
1269 dest_id, dest_id_type,
1270 packet->data, packet->len, FALSE);
1271 silc_buffer_free(packet);
1274 /* Sends notify message to a channel. The notify message sent is
1275 distributed to all clients on the channel. If `route_notify' is TRUE
1276 then the notify may be routed to primary route or to some other routers.
1277 If FALSE it is assured that the notify is sent only locally. If `sender'
1278 is provided then the packet is not sent to that connection since it
1279 originally came from it. */
1281 void silc_server_send_notify_to_channel(SilcServer server,
1282 SilcSocketConnection sender,
1283 SilcChannelEntry channel,
1284 unsigned char route_notify,
1285 SilcNotifyType type,
1293 packet = silc_notify_payload_encode(type, argc, ap);
1294 silc_server_packet_send_to_channel(server, sender, channel,
1295 SILC_PACKET_NOTIFY, route_notify,
1296 packet->data, packet->len, FALSE);
1297 silc_buffer_free(packet);
1300 /* Send notify message to all channels the client has joined. It is quaranteed
1301 that the message is sent only once to a client (ie. if a client is joined
1302 on two same channel it will receive only one notify message). Also, this
1303 sends only to local clients (locally connected if we are server, and to
1304 local servers if we are router). If `sender' is provided the packet is
1305 not sent to that client at all. */
1307 void silc_server_send_notify_on_channels(SilcServer server,
1308 SilcClientEntry sender,
1309 SilcClientEntry client,
1310 SilcNotifyType type,
1314 SilcSocketConnection sock = NULL;
1315 SilcPacketContext packetdata;
1317 SilcClientEntry *sent_clients = NULL;
1318 uint32 sent_clients_count = 0;
1319 SilcServerEntry *routed = NULL;
1320 uint32 routed_count = 0;
1321 SilcChannelEntry channel;
1322 SilcChannelClientEntry chl, chl2;
1323 SilcIDListData idata;
1325 unsigned char *data;
1327 int force_send = FALSE;
1330 SILC_LOG_DEBUG(("Start"));
1332 if (!silc_list_count(client->channels))
1336 packet = silc_notify_payload_encode(type, argc, ap);
1337 data = packet->data;
1338 data_len = packet->len;
1340 /* Set the packet context pointers. */
1341 packetdata.flags = 0;
1342 packetdata.type = SILC_PACKET_NOTIFY;
1343 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1344 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1345 packetdata.src_id_type = SILC_ID_SERVER;
1347 silc_list_start(client->channels);
1348 while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
1349 channel = chl->channel;
1351 /* Send the message to all clients on the channel's client list. */
1352 silc_list_start(channel->user_list);
1353 while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
1356 if (sender && c == sender)
1359 /* Check if we have sent the packet to this client already */
1360 for (k = 0; k < sent_clients_count; k++)
1361 if (sent_clients[k] == c)
1363 if (k < sent_clients_count)
1366 /* If we are router and if this client has router set it is not
1367 locally connected client and we will route the message to the
1368 router set in the client. */
1369 if (c && c->router && server->server_type == SILC_ROUTER) {
1370 /* Check if we have sent the packet to this route already */
1371 for (k = 0; k < routed_count; k++)
1372 if (routed[k] == c->router)
1374 if (k < routed_count)
1377 /* Get data used in packet header encryption, keys and stuff. */
1378 sock = (SilcSocketConnection)c->router->connection;
1379 idata = (SilcIDListData)c->router;
1381 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1382 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1383 packetdata.dst_id_type = SILC_ID_SERVER;
1384 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1385 packetdata.src_id_len + packetdata.dst_id_len;
1386 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1388 /* Send the packet */
1389 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1392 data, data_len, FALSE,
1395 silc_free(packetdata.dst_id);
1397 /* We want to make sure that the packet is routed to same router
1398 only once. Mark this route as sent route. */
1400 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1401 routed[k] = c->router;
1410 /* Send to locally connected client */
1413 /* Get data used in packet header encryption, keys and stuff. */
1414 sock = (SilcSocketConnection)c->connection;
1415 idata = (SilcIDListData)c;
1417 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1418 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1419 packetdata.dst_id_type = SILC_ID_CLIENT;
1420 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1421 packetdata.src_id_len + packetdata.dst_id_len;
1422 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1424 /* Send the packet */
1425 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1428 data, data_len, FALSE,
1431 silc_free(packetdata.dst_id);
1433 /* Make sure that we send the notify only once per client. */
1434 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1435 (sent_clients_count + 1));
1436 sent_clients[sent_clients_count] = c;
1437 sent_clients_count++;
1444 if (sent_clients_count)
1445 silc_free(sent_clients);
1446 silc_free(packetdata.src_id);
1449 /* Sends New ID Payload to remote end. The packet is used to distribute
1450 information about new registered clients, servers, channel etc. usually
1451 to routers so that they can keep these information up to date.
1452 If the argument `broadcast' is TRUE then the packet is sent as
1453 broadcast packet. */
1455 void silc_server_send_new_id(SilcServer server,
1456 SilcSocketConnection sock,
1458 void *id, SilcIdType id_type,
1463 SILC_LOG_DEBUG(("Start"));
1465 idp = silc_id_payload_encode(id, id_type);
1466 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1467 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1468 idp->data, idp->len, FALSE);
1469 silc_buffer_free(idp);
1472 /* Send New Channel Payload to notify about newly created channel in the
1473 SILC network. Normal server nevers sends this packet. Router uses this
1474 to notify other routers in the network about new channel. This packet
1477 void silc_server_send_new_channel(SilcServer server,
1478 SilcSocketConnection sock,
1482 uint32 channel_id_len,
1487 uint32 name_len = strlen(channel_name);
1489 SILC_LOG_DEBUG(("Start"));
1491 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1495 /* Encode the channel payload */
1496 packet = silc_channel_payload_encode(channel_name, name_len,
1497 cid, channel_id_len, mode);
1499 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1500 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1501 packet->data, packet->len, FALSE);
1504 silc_buffer_free(packet);
1507 /* Send Channel Key payload to distribute the new channel key. Normal server
1508 sends this to router when new client joins to existing channel. Router
1509 sends this to the local server who sent the join command in case where
1510 the channel did not exist yet. Both normal and router servers uses this
1511 also to send this to locally connected clients on the channel. This
1512 must not be broadcasted packet. Routers do not send this to each other.
1513 If `sender is provided then the packet is not sent to that connection since
1514 it originally came from it. */
1516 void silc_server_send_channel_key(SilcServer server,
1517 SilcSocketConnection sender,
1518 SilcChannelEntry channel,
1519 unsigned char route)
1522 unsigned char *chid;
1525 SILC_LOG_DEBUG(("Start"));
1527 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1531 /* Encode channel key packet */
1532 tmp_len = strlen(channel->channel_key->cipher->name);
1533 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1536 channel->channel_key->cipher->name,
1537 channel->key_len / 8, channel->key);
1539 silc_server_packet_send_to_channel(server, sender, channel,
1540 SILC_PACKET_CHANNEL_KEY,
1541 route, packet->data, packet->len, FALSE);
1542 silc_buffer_free(packet);
1546 /* Generic function to send any command. The arguments must be sent already
1547 encoded into correct form in correct order. */
1549 void silc_server_send_command(SilcServer server,
1550 SilcSocketConnection sock,
1551 SilcCommand command,
1559 packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1560 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1561 packet->data, packet->len, TRUE);
1562 silc_buffer_free(packet);
1565 /* Send the heartbeat packet. */
1567 void silc_server_send_heartbeat(SilcServer server,
1568 SilcSocketConnection sock)
1570 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1574 /* Generic function to relay packet we've received. This is used to relay
1575 packets to a client but generally can be used to other purposes as well. */
1577 void silc_server_relay_packet(SilcServer server,
1578 SilcSocketConnection dst_sock,
1581 SilcPacketContext *packet,
1584 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1585 + packet->dst_id_len + packet->padlen);
1587 silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1588 silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1590 /* Re-encrypt packet */
1591 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1593 /* Send the packet */
1594 silc_server_packet_send_real(server, dst_sock, force_send);
1596 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1597 + packet->dst_id_len + packet->padlen);
1600 /* Routine used to send the connection authentication packet. */
1602 void silc_server_send_connection_auth_request(SilcServer server,
1603 SilcSocketConnection sock,
1605 SilcAuthMethod auth_meth)
1609 packet = silc_buffer_alloc(4);
1610 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1611 silc_buffer_format(packet,
1612 SILC_STR_UI_SHORT(conn_type),
1613 SILC_STR_UI_SHORT(auth_meth),
1616 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1617 0, packet->data, packet->len, FALSE);
1618 silc_buffer_free(packet);