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 SilcHashTableList htl;
422 SilcIDListData idata;
423 uint32 routed_count = 0;
425 /* This doesn't send channel message packets */
426 if (type == SILC_PACKET_CHANNEL_MESSAGE)
429 SILC_LOG_DEBUG(("Sending packet to channel"));
431 /* Set the packet context pointers. */
432 packetdata.flags = 0;
433 packetdata.type = type;
434 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
435 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
436 packetdata.src_id_type = SILC_ID_SERVER;
437 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
438 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
439 packetdata.dst_id_type = SILC_ID_CHANNEL;
440 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
441 packetdata.src_id_len + packetdata.dst_id_len;
442 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
444 /* If there are global users in the channel we will send the message
445 first to our router for further routing. */
446 if (route && server->server_type == SILC_SERVER && !server->standalone &&
447 channel->global_users) {
448 SilcServerEntry router;
450 /* Get data used in packet header encryption, keys and stuff. */
451 router = server->router;
452 sock = (SilcSocketConnection)router->connection;
453 idata = (SilcIDListData)router;
455 if (sock != sender) {
456 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
458 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
461 data, data_len, FALSE,
466 /* Send the message to clients on the channel's client list. */
467 silc_hash_table_list(channel->user_list, &htl);
468 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
469 client = chl->client;
471 /* If client has router set it is not locally connected client and
472 we will route the message to the router set in the client. Though,
473 send locally connected server in all cases. */
474 if (server->server_type == SILC_ROUTER && client && client->router &&
475 ((!route && client->router->router == server->id_entry) || route)) {
478 /* Check if we have sent the packet to this route already */
479 for (k = 0; k < routed_count; k++)
480 if (routed[k] == client->router)
482 if (k < routed_count)
485 /* Get data used in packet header encryption, keys and stuff. */
486 sock = (SilcSocketConnection)client->router->connection;
487 idata = (SilcIDListData)client->router;
489 if (sender && sock == sender)
492 /* Send the packet */
493 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
496 data, data_len, FALSE,
499 /* We want to make sure that the packet is routed to same router
500 only once. Mark this route as sent route. */
502 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
503 routed[k] = client->router;
509 if (client && client->router)
512 /* Send to locally connected client */
515 /* Get data used in packet header encryption, keys and stuff. */
516 sock = (SilcSocketConnection)client->connection;
517 idata = (SilcIDListData)client;
519 if (sender && sock == sender)
522 /* Send the packet */
523 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
526 data, data_len, FALSE,
533 silc_free(packetdata.src_id);
534 silc_free(packetdata.dst_id);
537 /* This checks whether the relayed packet came from router. If it did
538 then we'll need to encrypt it with the channel key. This is called
539 from the silc_server_packet_relay_to_channel. */
542 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
543 SilcSocketConnection sock,
544 SilcChannelEntry channel,
546 unsigned int data_len)
548 /* If we are router and the packet came from router and private key
549 has not been set for the channel then we must encrypt the packet
550 as it was decrypted with the session key shared between us and the
551 router which sent it. This is so, because cells does not share the
553 if (server->server_type == SILC_ROUTER &&
554 sock->type == SILC_SOCKET_TYPE_ROUTER &&
555 !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
556 channel->channel_key) {
559 uint16 data_len, flags;
561 iv_len = silc_cipher_get_block_len(channel->channel_key);
562 if (channel->iv[0] == '\0')
563 for (i = 0; i < iv_len; i++) channel->iv[i] =
564 silc_rng_get_byte(server->rng);
566 silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
568 /* Encode new payload. This encrypts it also. */
569 SILC_GET16_MSB(flags, data);
570 SILC_GET16_MSB(data_len, data + 2);
571 chp = silc_channel_message_payload_encode(flags, data_len,
574 channel->channel_key,
576 memcpy(data, chp->data, chp->len);
577 silc_buffer_free(chp);
581 /* This routine is explicitly used to relay messages to some channel.
582 Packets sent with this function we have received earlier and are
583 totally encrypted. This just sends the packet to all clients on
584 the channel. If the sender of the packet is someone on the channel
585 the message will not be sent to that client. The SILC Packet header
586 is encrypted with the session key shared between us and the client.
587 MAC is also computed before encrypting the header. Rest of the
588 packet will be untouched. */
590 void silc_server_packet_relay_to_channel(SilcServer server,
591 SilcSocketConnection sender_sock,
592 SilcChannelEntry channel,
594 SilcIdType sender_type,
601 SilcSocketConnection sock = NULL;
602 SilcPacketContext packetdata;
603 SilcClientEntry client = NULL;
604 SilcServerEntry *routed = NULL;
605 SilcChannelClientEntry chl;
606 uint32 routed_count = 0;
607 SilcIDListData idata;
608 SilcHashTableList htl;
610 SILC_LOG_DEBUG(("Relaying packet to channel"));
612 /* Set the packet context pointers. */
613 packetdata.flags = 0;
614 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
615 packetdata.src_id = silc_id_id2str(sender, sender_type);
616 packetdata.src_id_len = silc_id_get_len(sender, sender_type);
617 packetdata.src_id_type = sender_type;
618 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
619 packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
620 packetdata.dst_id_type = SILC_ID_CHANNEL;
621 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
622 packetdata.src_id_len +
623 packetdata.dst_id_len));
625 /* This encrypts the packet, if needed. It will be encrypted if
626 it came from the router thus it needs to be encrypted with the
627 channel key. If the channel key does not exist, then we know we
628 don't have a single local user on the channel. */
629 silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
633 /* If there are global users in the channel we will send the message
634 first to our router for further routing. */
635 if (server->server_type == SILC_SERVER && !server->standalone &&
636 channel->global_users) {
637 SilcServerEntry router;
639 router = server->router;
641 /* Check that the sender is not our router. */
642 if (sender_sock != (SilcSocketConnection)router->connection) {
644 /* Get data used in packet header encryption, keys and stuff. */
645 sock = (SilcSocketConnection)router->connection;
646 idata = (SilcIDListData)router;
648 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
650 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
653 data, data_len, TRUE,
658 /* Send the message to clients on the channel's client list. */
659 silc_hash_table_list(channel->user_list, &htl);
660 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
661 client = chl->client;
665 /* If sender is one on the channel do not send it the packet. */
666 if (!found && sender_type == SILC_ID_CLIENT &&
667 SILC_ID_CLIENT_COMPARE(client->id, sender)) {
672 /* If the client has set router it means that it is not locally
673 connected client and we will route the packet further. */
674 if (server->server_type == SILC_ROUTER && client->router) {
677 /* Sender maybe server as well so we want to make sure that
678 we won't send the message to the server it came from. */
679 if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
684 /* Check if we have sent the packet to this route already */
685 for (k = 0; k < routed_count; k++)
686 if (routed[k] == client->router)
688 if (k < routed_count)
691 /* Get data used in packet header encryption, keys and stuff. */
692 sock = (SilcSocketConnection)client->router->connection;
693 idata = (SilcIDListData)client->router;
695 /* Do not send to the sender. Check first whether the true
696 sender's router is same as this client's router. Also check
697 if the sender socket is the same as this client's router
700 ((SilcClientEntry)sender_entry)->router == client->router)
702 if (sender_sock && sock == sender_sock)
705 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
706 silc_id_render(client->id, SILC_ID_CLIENT),
707 sock->hostname, sock->ip));
709 /* We want to make sure that the packet is routed to same router
710 only once. Mark this route as sent route. */
712 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
713 routed[k] = client->router;
716 /* If the remote connection is router then we'll decrypt the
717 channel message and re-encrypt it with the session key shared
718 between us and the remote router. This is done because the
719 channel keys are cell specific and we have different channel
720 key than the remote router has. */
721 if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
723 /* If private key mode is not set then decrypt the packet
725 if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
726 unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
727 memcpy(tmp, data, data_len);
729 /* Decrypt the channel message (we don't check the MAC) */
730 /* XXX this could be optimized and removed all together by
731 taking a copy of the original data before encrypting it
732 and thus would not required decrypting. */
733 if (channel->channel_key &&
734 !silc_channel_message_payload_decrypt(tmp, data_len,
735 channel->channel_key,
737 memset(tmp, 0, data_len);
742 /* Now re-encrypt and send it to the router */
743 silc_server_packet_send_srcdest(server, sock,
744 SILC_PACKET_CHANNEL_MESSAGE, 0,
746 channel->id, SILC_ID_CHANNEL,
747 tmp, data_len, force_send);
749 /* Free the copy of the channel message */
750 memset(tmp, 0, data_len);
753 /* Private key mode is set, we don't have the channel key, so
754 just re-encrypt the entire packet and send it to the router. */
755 silc_server_packet_send_srcdest(server, sock,
756 SILC_PACKET_CHANNEL_MESSAGE, 0,
758 channel->id, SILC_ID_CHANNEL,
759 data, data_len, force_send);
764 /* Send the packet (to normal server) */
765 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
768 data, data_len, TRUE,
774 if (client && client->router)
777 /* Get data used in packet header encryption, keys and stuff. */
778 sock = (SilcSocketConnection)client->connection;
779 idata = (SilcIDListData)client;
781 if (sender_sock && sock == sender_sock)
784 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
785 silc_id_render(client->id, SILC_ID_CLIENT),
786 sock->hostname, sock->ip));
788 /* Send the packet */
789 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
792 data, data_len, TRUE,
797 silc_free(packetdata.src_id);
798 silc_free(packetdata.dst_id);
801 /* This function is used to send packets strictly to all local clients
802 on a particular channel. This is used for example to distribute new
803 channel key to all our locally connected clients on the channel.
804 The packets are always encrypted with the session key shared between
805 the client, this means these are not _to the channel_ but _to the client_
808 void silc_server_packet_send_local_channel(SilcServer server,
809 SilcChannelEntry channel,
811 SilcPacketFlags flags,
816 SilcChannelClientEntry chl;
817 SilcHashTableList htl;
818 SilcSocketConnection sock = NULL;
820 SILC_LOG_DEBUG(("Start"));
822 /* Send the message to clients on the channel's client list. */
823 silc_hash_table_list(channel->user_list, &htl);
824 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
825 if (chl->client && !chl->client->router) {
826 sock = (SilcSocketConnection)chl->client->connection;
828 /* Send the packet to the client */
829 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
830 SILC_ID_CLIENT, data, data_len,
836 /* Routine used to send (relay, route) private messages to some destination.
837 If the private message key does not exist then the message is re-encrypted,
838 otherwise we just pass it along. This really is not used to send new
839 private messages (as server does not send them) but to relay received
842 void silc_server_send_private_message(SilcServer server,
843 SilcSocketConnection dst_sock,
846 SilcPacketContext *packet)
848 SilcBuffer buffer = packet->buffer;
850 /* Re-encrypt and send if private messge key does not exist */
851 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
853 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
854 + packet->dst_id_len + packet->padlen);
855 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
856 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
858 /* Re-encrypt packet */
859 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
861 /* Send the packet */
862 silc_server_packet_send_real(server, dst_sock, FALSE);
865 /* Key exist so encrypt just header and send it */
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);
872 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf,
873 SILC_PACKET_HEADER_LEN + packet->src_id_len +
874 packet->dst_id_len + packet->padlen);
876 silc_server_packet_send_real(server, dst_sock, FALSE);
880 /* Sends current motd to client */
882 void silc_server_send_motd(SilcServer server,
883 SilcSocketConnection sock)
888 if (server->config && server->config->motd &&
889 server->config->motd->motd_file) {
891 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
895 silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
901 /* Sends error message. Error messages may or may not have any
904 void silc_server_send_error(SilcServer server,
905 SilcSocketConnection sock,
906 const char *fmt, ...)
909 unsigned char buf[4096];
911 memset(buf, 0, sizeof(buf));
913 vsprintf(buf, fmt, ap);
916 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
917 buf, strlen(buf), FALSE);
920 /* Sends notify message. If format is TRUE the variable arguments are
921 formatted and the formatted string is sent as argument payload. If it is
922 FALSE then each argument is sent as separate argument and their format
923 in the argument list must be { argument data, argument length }. */
925 void silc_server_send_notify(SilcServer server,
926 SilcSocketConnection sock,
936 packet = silc_notify_payload_encode(type, argc, ap);
937 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
938 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
939 packet->data, packet->len, FALSE);
940 silc_buffer_free(packet);
943 /* Sends notify message and gets the arguments from the `args' Argument
946 void silc_server_send_notify_args(SilcServer server,
947 SilcSocketConnection sock,
955 packet = silc_notify_payload_encode_args(type, argc, args);
956 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY,
957 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
958 packet->data, packet->len, FALSE);
959 silc_buffer_free(packet);
962 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
963 `old_id' with the `new_id'. */
965 void silc_server_send_notify_channel_change(SilcServer server,
966 SilcSocketConnection sock,
968 SilcChannelID *old_id,
969 SilcChannelID *new_id)
971 SilcBuffer idp1, idp2;
973 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
974 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
976 silc_server_send_notify(server, sock, broadcast,
977 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
978 2, idp1->data, idp1->len, idp2->data, idp2->len);
979 silc_buffer_free(idp1);
980 silc_buffer_free(idp2);
983 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
984 `old_id' with the `new_id'. */
986 void silc_server_send_notify_nick_change(SilcServer server,
987 SilcSocketConnection sock,
989 SilcClientID *old_id,
990 SilcClientID *new_id)
992 SilcBuffer idp1, idp2;
994 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
995 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
997 silc_server_send_notify(server, sock, broadcast,
998 SILC_NOTIFY_TYPE_NICK_CHANGE,
999 2, idp1->data, idp1->len, idp2->data, idp2->len);
1000 silc_buffer_free(idp1);
1001 silc_buffer_free(idp2);
1004 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1005 has joined to the `channel'. */
1007 void silc_server_send_notify_join(SilcServer server,
1008 SilcSocketConnection sock,
1010 SilcChannelEntry channel,
1011 SilcClientID *client_id)
1013 SilcBuffer idp1, idp2;
1015 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1016 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1017 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1018 2, idp1->data, idp1->len,
1019 idp2->data, idp2->len);
1020 silc_buffer_free(idp1);
1021 silc_buffer_free(idp2);
1024 /* Sends LEAVE notify type. This tells that `client_id' has left the
1025 `channel'. The Notify packet is always destined to the channel. */
1027 void silc_server_send_notify_leave(SilcServer server,
1028 SilcSocketConnection sock,
1030 SilcChannelEntry channel,
1031 SilcClientID *client_id)
1035 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1036 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1037 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1038 1, idp->data, idp->len);
1039 silc_buffer_free(idp);
1042 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1043 `channel' mode to `mode. The Notify packet is always destined to
1046 void silc_server_send_notify_cmode(SilcServer server,
1047 SilcSocketConnection sock,
1049 SilcChannelEntry channel,
1051 void *id, SilcIdType id_type,
1052 char *cipher, char *hmac)
1055 unsigned char mode[4];
1057 idp = silc_id_payload_encode((void *)id, id_type);
1058 SILC_PUT32_MSB(mode_mask, mode);
1060 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1061 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1062 4, idp->data, idp->len,
1064 cipher, cipher ? strlen(cipher) : 0,
1065 hmac, hmac ? strlen(hmac) : 0);
1066 silc_buffer_free(idp);
1069 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1070 `target' client's mode on `channel'. The Notify packet is always
1071 destined to the channel. */
1073 void silc_server_send_notify_cumode(SilcServer server,
1074 SilcSocketConnection sock,
1076 SilcChannelEntry channel,
1078 void *id, SilcIdType id_type,
1079 SilcClientID *target)
1081 SilcBuffer idp1, idp2;
1082 unsigned char mode[4];
1084 idp1 = silc_id_payload_encode((void *)id, id_type);
1085 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1086 SILC_PUT32_MSB(mode_mask, mode);
1088 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1090 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
1091 idp1->data, idp1->len,
1093 idp2->data, idp2->len);
1094 silc_buffer_free(idp1);
1095 silc_buffer_free(idp2);
1098 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1099 left SILC network. This function is used only between server and router
1100 traffic. This is not used to send the notify to the channel for
1101 client. The `message may be NULL. */
1103 void silc_server_send_notify_signoff(SilcServer server,
1104 SilcSocketConnection sock,
1106 SilcClientID *client_id,
1111 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1112 silc_server_send_notify(server, sock, broadcast,
1113 SILC_NOTIFY_TYPE_SIGNOFF,
1114 message ? 2 : 1, idp->data, idp->len,
1115 message, message ? strlen(message): 0);
1116 silc_buffer_free(idp);
1119 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1120 the `channel's topic to `topic'. The Notify packet is always destined
1121 to the channel. This function is used to send the topic set notifies
1124 void silc_server_send_notify_topic_set(SilcServer server,
1125 SilcSocketConnection sock,
1127 SilcChannelEntry channel,
1128 SilcClientID *client_id,
1133 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1134 silc_server_send_notify(server, sock, broadcast,
1135 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1137 idp->data, idp->len,
1138 topic, topic ? strlen(topic) : 0);
1139 silc_buffer_free(idp);
1142 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1143 was kicked off the channel. The `comment' may indicate the reason
1144 for the kicking. This function is used only between server and router
1147 void silc_server_send_notify_kicked(SilcServer server,
1148 SilcSocketConnection sock,
1150 SilcChannelEntry channel,
1151 SilcClientID *client_id,
1156 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1157 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1158 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1159 comment ? 2 : 1, idp->data, idp->len,
1160 comment, comment ? strlen(comment) : 0);
1161 silc_buffer_free(idp);
1164 /* Send KILLED notify type. This tells that the `client_id' client was
1165 killed from the network. The `comment' may indicate the reason
1168 void silc_server_send_notify_killed(SilcServer server,
1169 SilcSocketConnection sock,
1171 SilcClientID *client_id,
1176 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1177 silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1178 SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1179 comment ? 2 : 1, idp->data, idp->len,
1180 comment, comment ? strlen(comment) : 0);
1181 silc_buffer_free(idp);
1184 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1185 user mode in the SILC Network was changed. This function is used to
1186 send the packet between routers as broadcast packet. */
1188 void silc_server_send_notify_umode(SilcServer server,
1189 SilcSocketConnection sock,
1191 SilcClientID *client_id,
1195 unsigned char mode[4];
1197 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1198 SILC_PUT32_MSB(mode_mask, mode);
1200 silc_server_send_notify(server, sock, broadcast,
1201 SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1202 idp->data, idp->len,
1204 silc_buffer_free(idp);
1207 /* Sends BAN notify type. This tells that ban has been either `add'ed
1208 or `del'eted on the `channel. This function is used to send the packet
1209 between routers as broadcast packet. */
1211 void silc_server_send_notify_ban(SilcServer server,
1212 SilcSocketConnection sock,
1214 SilcChannelEntry channel,
1215 char *add, char *del)
1219 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1220 silc_server_send_notify(server, sock, broadcast,
1221 SILC_NOTIFY_TYPE_BAN, 3,
1222 idp->data, idp->len,
1223 add, add ? strlen(add) : 0,
1224 del, del ? strlen(del) : 0);
1225 silc_buffer_free(idp);
1228 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1229 or `del'eted on the `channel. The sender of the invite is the `client_id'.
1230 This function is used to send the packet between routers as broadcast
1233 void silc_server_send_notify_invite(SilcServer server,
1234 SilcSocketConnection sock,
1236 SilcChannelEntry channel,
1237 SilcClientID *client_id,
1238 char *add, char *del)
1240 SilcBuffer idp, idp2;
1242 idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1243 idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1244 silc_server_send_notify(server, sock, broadcast,
1245 SILC_NOTIFY_TYPE_INVITE, 5,
1246 idp->data, idp->len,
1247 channel->channel_name, strlen(channel->channel_name),
1248 idp2->data, idp2->len,
1249 add, add ? strlen(add) : 0,
1250 del, del ? strlen(del) : 0);
1251 silc_buffer_free(idp);
1252 silc_buffer_free(idp2);
1255 /* Sends notify message destined to specific entity. */
1257 void silc_server_send_notify_dest(SilcServer server,
1258 SilcSocketConnection sock,
1261 SilcIdType dest_id_type,
1262 SilcNotifyType type,
1270 packet = silc_notify_payload_encode(type, argc, ap);
1271 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
1272 dest_id, dest_id_type,
1273 packet->data, packet->len, FALSE);
1274 silc_buffer_free(packet);
1277 /* Sends notify message to a channel. The notify message sent is
1278 distributed to all clients on the channel. If `route_notify' is TRUE
1279 then the notify may be routed to primary route or to some other routers.
1280 If FALSE it is assured that the notify is sent only locally. If `sender'
1281 is provided then the packet is not sent to that connection since it
1282 originally came from it. */
1284 void silc_server_send_notify_to_channel(SilcServer server,
1285 SilcSocketConnection sender,
1286 SilcChannelEntry channel,
1287 unsigned char route_notify,
1288 SilcNotifyType type,
1296 packet = silc_notify_payload_encode(type, argc, ap);
1297 silc_server_packet_send_to_channel(server, sender, channel,
1298 SILC_PACKET_NOTIFY, route_notify,
1299 packet->data, packet->len, FALSE);
1300 silc_buffer_free(packet);
1303 /* Send notify message to all channels the client has joined. It is quaranteed
1304 that the message is sent only once to a client (ie. if a client is joined
1305 on two same channel it will receive only one notify message). Also, this
1306 sends only to local clients (locally connected if we are server, and to
1307 local servers if we are router). If `sender' is provided the packet is
1308 not sent to that client at all. */
1310 void silc_server_send_notify_on_channels(SilcServer server,
1311 SilcClientEntry sender,
1312 SilcClientEntry client,
1313 SilcNotifyType type,
1317 SilcSocketConnection sock = NULL;
1318 SilcPacketContext packetdata;
1320 SilcClientEntry *sent_clients = NULL;
1321 uint32 sent_clients_count = 0;
1322 SilcServerEntry *routed = NULL;
1323 uint32 routed_count = 0;
1324 SilcHashTableList htl, htl2;
1325 SilcChannelEntry channel;
1326 SilcChannelClientEntry chl, chl2;
1327 SilcIDListData idata;
1329 unsigned char *data;
1331 int force_send = FALSE;
1334 SILC_LOG_DEBUG(("Start"));
1336 if (!silc_hash_table_count(client->channels))
1340 packet = silc_notify_payload_encode(type, argc, ap);
1341 data = packet->data;
1342 data_len = packet->len;
1344 /* Set the packet context pointers. */
1345 packetdata.flags = 0;
1346 packetdata.type = SILC_PACKET_NOTIFY;
1347 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1348 packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1349 packetdata.src_id_type = SILC_ID_SERVER;
1351 silc_hash_table_list(client->channels, &htl);
1352 while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1353 channel = chl->channel;
1355 /* Send the message to all clients on the channel's client list. */
1356 silc_hash_table_list(channel->user_list, &htl2);
1357 while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1360 if (sender && c == sender)
1363 /* Check if we have sent the packet to this client already */
1364 for (k = 0; k < sent_clients_count; k++)
1365 if (sent_clients[k] == c)
1367 if (k < sent_clients_count)
1370 /* If we are router and if this client has router set it is not
1371 locally connected client and we will route the message to the
1372 router set in the client. */
1373 if (c && c->router && server->server_type == SILC_ROUTER) {
1374 /* Check if we have sent the packet to this route already */
1375 for (k = 0; k < routed_count; k++)
1376 if (routed[k] == c->router)
1378 if (k < routed_count)
1381 /* Get data used in packet header encryption, keys and stuff. */
1382 sock = (SilcSocketConnection)c->router->connection;
1383 idata = (SilcIDListData)c->router;
1385 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1386 packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1387 packetdata.dst_id_type = SILC_ID_SERVER;
1388 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1389 packetdata.src_id_len + packetdata.dst_id_len;
1390 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1392 /* Send the packet */
1393 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1396 data, data_len, FALSE,
1399 silc_free(packetdata.dst_id);
1401 /* We want to make sure that the packet is routed to same router
1402 only once. Mark this route as sent route. */
1404 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1405 routed[k] = c->router;
1414 /* Send to locally connected client */
1417 /* Get data used in packet header encryption, keys and stuff. */
1418 sock = (SilcSocketConnection)c->connection;
1419 idata = (SilcIDListData)c;
1421 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1422 packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1423 packetdata.dst_id_type = SILC_ID_CLIENT;
1424 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1425 packetdata.src_id_len + packetdata.dst_id_len;
1426 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1428 /* Send the packet */
1429 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1432 data, data_len, FALSE,
1435 silc_free(packetdata.dst_id);
1437 /* Make sure that we send the notify only once per client. */
1438 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1439 (sent_clients_count + 1));
1440 sent_clients[sent_clients_count] = c;
1441 sent_clients_count++;
1448 if (sent_clients_count)
1449 silc_free(sent_clients);
1450 silc_free(packetdata.src_id);
1453 /* Sends New ID Payload to remote end. The packet is used to distribute
1454 information about new registered clients, servers, channel etc. usually
1455 to routers so that they can keep these information up to date.
1456 If the argument `broadcast' is TRUE then the packet is sent as
1457 broadcast packet. */
1459 void silc_server_send_new_id(SilcServer server,
1460 SilcSocketConnection sock,
1462 void *id, SilcIdType id_type,
1467 SILC_LOG_DEBUG(("Start"));
1469 idp = silc_id_payload_encode(id, id_type);
1470 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1471 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1472 idp->data, idp->len, FALSE);
1473 silc_buffer_free(idp);
1476 /* Send New Channel Payload to notify about newly created channel in the
1477 SILC network. Normal server nevers sends this packet. Router uses this
1478 to notify other routers in the network about new channel. This packet
1481 void silc_server_send_new_channel(SilcServer server,
1482 SilcSocketConnection sock,
1486 uint32 channel_id_len,
1491 uint32 name_len = strlen(channel_name);
1493 SILC_LOG_DEBUG(("Start"));
1495 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1499 /* Encode the channel payload */
1500 packet = silc_channel_payload_encode(channel_name, name_len,
1501 cid, channel_id_len, mode);
1503 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1504 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1505 packet->data, packet->len, FALSE);
1508 silc_buffer_free(packet);
1511 /* Send Channel Key payload to distribute the new channel key. Normal server
1512 sends this to router when new client joins to existing channel. Router
1513 sends this to the local server who sent the join command in case where
1514 the channel did not exist yet. Both normal and router servers uses this
1515 also to send this to locally connected clients on the channel. This
1516 must not be broadcasted packet. Routers do not send this to each other.
1517 If `sender is provided then the packet is not sent to that connection since
1518 it originally came from it. */
1520 void silc_server_send_channel_key(SilcServer server,
1521 SilcSocketConnection sender,
1522 SilcChannelEntry channel,
1523 unsigned char route)
1526 unsigned char *chid;
1529 SILC_LOG_DEBUG(("Start"));
1531 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1535 /* Encode channel key packet */
1536 tmp_len = strlen(channel->channel_key->cipher->name);
1537 packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1540 channel->channel_key->cipher->name,
1541 channel->key_len / 8, channel->key);
1543 silc_server_packet_send_to_channel(server, sender, channel,
1544 SILC_PACKET_CHANNEL_KEY,
1545 route, packet->data, packet->len, FALSE);
1546 silc_buffer_free(packet);
1550 /* Generic function to send any command. The arguments must be sent already
1551 encoded into correct form in correct order. */
1553 void silc_server_send_command(SilcServer server,
1554 SilcSocketConnection sock,
1555 SilcCommand command,
1563 packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1564 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1565 packet->data, packet->len, TRUE);
1566 silc_buffer_free(packet);
1569 /* Send the heartbeat packet. */
1571 void silc_server_send_heartbeat(SilcServer server,
1572 SilcSocketConnection sock)
1574 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1578 /* Generic function to relay packet we've received. This is used to relay
1579 packets to a client but generally can be used to other purposes as well. */
1581 void silc_server_relay_packet(SilcServer server,
1582 SilcSocketConnection dst_sock,
1585 SilcPacketContext *packet,
1588 silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1589 + packet->dst_id_len + packet->padlen);
1591 silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1592 silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1594 /* Re-encrypt packet */
1595 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1597 /* Send the packet */
1598 silc_server_packet_send_real(server, dst_sock, force_send);
1600 silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
1601 + packet->dst_id_len + packet->padlen);
1604 /* Routine used to send the connection authentication packet. */
1606 void silc_server_send_connection_auth_request(SilcServer server,
1607 SilcSocketConnection sock,
1609 SilcAuthMethod auth_meth)
1613 packet = silc_buffer_alloc(4);
1614 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1615 silc_buffer_format(packet,
1616 SILC_STR_UI_SHORT(conn_type),
1617 SILC_STR_UI_SHORT(auth_meth),
1620 silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1621 0, packet->data, packet->len, FALSE);
1622 silc_buffer_free(packet);