5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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,
39 ret = silc_packet_send(sock, force_send);
43 /* Mark that there is some outgoing data available for this connection.
44 This call sets the connection both for input and output (the input
45 is set always and this call keeps the input setting, actually).
46 Actual data sending is performed by silc_server_packet_process. */
47 SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
49 /* Mark to socket that data is pending in outgoing buffer. This flag
50 is needed if new data is added to the buffer before the earlier
51 put data is sent to the network. */
52 SILC_SET_OUTBUF_PENDING(sock);
57 /* Assembles a new packet to be sent out to network. This doesn't actually
58 send the packet but creates the packet and fills the outgoing data
59 buffer and marks the packet ready to be sent to network. However, If
60 argument force_send is TRUE the packet is sent immediately and not put
61 to queue. Normal case is that the packet is not sent immediately. */
63 void silc_server_packet_send(SilcServer server,
64 SilcSocketConnection sock,
66 SilcPacketFlags flags,
68 unsigned int data_len,
72 SilcIdType dst_id_type = SILC_ID_NONE;
77 /* Get data used in the packet sending, keys and stuff */
79 case SILC_SOCKET_TYPE_CLIENT:
80 dst_id = ((SilcClientEntry)sock->user_data)->id;
81 dst_id_type = SILC_ID_CLIENT;
83 case SILC_SOCKET_TYPE_SERVER:
84 case SILC_SOCKET_TYPE_ROUTER:
85 dst_id = ((SilcServerEntry)sock->user_data)->id;
86 dst_id_type = SILC_ID_SERVER;
92 silc_server_packet_send_dest(server, sock, type, flags, dst_id,
93 dst_id_type, data, data_len, force_send);
96 /* Assembles a new packet to be sent out to network. This doesn't actually
97 send the packet but creates the packet and fills the outgoing data
98 buffer and marks the packet ready to be sent to network. However, If
99 argument force_send is TRUE the packet is sent immediately and not put
100 to queue. Normal case is that the packet is not sent immediately.
101 Destination information is sent as argument for this function. */
103 void silc_server_packet_send_dest(SilcServer server,
104 SilcSocketConnection sock,
106 SilcPacketFlags flags,
108 SilcIdType dst_id_type,
110 unsigned int data_len,
113 SilcPacketContext packetdata;
114 SilcIDListData idata;
115 SilcCipher cipher = NULL;
116 SilcHmac hmac = NULL;
117 unsigned char *dst_id_data = NULL;
118 unsigned int dst_id_len = 0;
120 SILC_LOG_DEBUG(("Sending packet, type %d", type));
122 /* Get data used in the packet sending, keys and stuff */
123 idata = (SilcIDListData)sock->user_data;
126 dst_id_data = silc_id_id2str(dst_id, dst_id_type);
127 dst_id_len = silc_id_get_len(dst_id_type);
130 /* Set the packet context pointers */
131 packetdata.type = type;
132 packetdata.flags = flags;
133 packetdata.src_id = silc_id_id2str(server->id, server->id_type);
134 packetdata.src_id_len = SILC_ID_SERVER_LEN;
135 packetdata.src_id_type = server->id_type;
136 packetdata.dst_id = dst_id_data;
137 packetdata.dst_id_len = dst_id_len;
138 packetdata.dst_id_type = dst_id_type;
139 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
140 packetdata.src_id_len + dst_id_len;
141 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
142 packetdata.rng = server->rng;
144 /* Prepare outgoing data buffer for packet sending */
145 silc_packet_send_prepare(sock,
146 SILC_PACKET_HEADER_LEN +
147 packetdata.src_id_len +
148 packetdata.dst_id_len,
152 SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
154 packetdata.buffer = sock->outbuf;
156 /* Put the data to the buffer */
157 if (data && data_len)
158 silc_buffer_put(sock->outbuf, data, data_len);
160 /* Create the outgoing packet */
161 silc_packet_assemble(&packetdata);
164 cipher = idata->send_key;
168 /* Encrypt the packet */
169 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
171 SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
172 sock->outbuf->data, sock->outbuf->len);
174 /* Now actually send the packet */
175 silc_server_packet_send_real(server, sock, force_send);
177 if (packetdata.src_id)
178 silc_free(packetdata.src_id);
179 if (packetdata.dst_id)
180 silc_free(packetdata.dst_id);
183 /* Forwards packet. Packets sent with this function will be marked as
184 forwarded (in the SILC header flags) so that the receiver knows that
185 we have forwarded the packet to it. Forwarded packets are handled
186 specially by the receiver as they are not destined to the receiver
187 originally. However, the receiver knows this because the forwarded
188 flag has been set (and the flag is authenticated). */
190 void silc_server_packet_forward(SilcServer server,
191 SilcSocketConnection sock,
192 unsigned char *data, unsigned int data_len,
195 SilcIDListData idata;
196 SilcCipher cipher = NULL;
197 SilcHmac hmac = NULL;
199 SILC_LOG_DEBUG(("Forwarding packet"));
201 /* Get data used in the packet sending, keys and stuff */
202 idata = (SilcIDListData)sock->user_data;
204 /* Prepare outgoing data buffer for packet sending */
205 silc_packet_send_prepare(sock, 0, 0, data_len);
207 /* Put the data to the buffer */
208 if (data && data_len)
209 silc_buffer_put(sock->outbuf, data, data_len);
211 /* Add the FORWARDED flag to packet flags */
212 sock->outbuf->data[2] |= (unsigned char)SILC_PACKET_FLAG_FORWARDED;
215 cipher = idata->send_key;
219 /* Encrypt the packet */
220 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
222 SILC_LOG_HEXDUMP(("Forwarded packet, len %d", sock->outbuf->len),
223 sock->outbuf->data, sock->outbuf->len);
225 /* Now actually send the packet */
226 silc_server_packet_send_real(server, sock, force_send);
229 /* Broadcast received packet to our primary route. This function is used
230 by router to further route received broadcast packet. It is expected
231 that the broadcast flag from the packet is checked before calling this
232 function. This does not test or set the broadcast flag. */
234 void silc_server_packet_broadcast(SilcServer server,
235 SilcSocketConnection sock,
236 SilcPacketContext *packet)
238 SilcBuffer buffer = packet->buffer;
239 SilcIDListData idata;
242 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
244 /* If the packet is originated from our primary route we are
245 not allowed to send the packet. */
246 id = silc_id_str2id(packet->src_id, packet->src_id_type);
247 if (id && SILC_ID_SERVER_COMPARE(id, server->router->id)) {
248 idata = (SilcIDListData)sock->user_data;
250 silc_buffer_push(buffer, buffer->data - buffer->head);
251 silc_packet_send_prepare(sock, 0, 0, buffer->len);
252 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
253 silc_packet_encrypt(idata->send_key, idata->hmac,
254 sock->outbuf, sock->outbuf->len);
256 SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
257 sock->outbuf->data, sock->outbuf->len);
259 /* Now actually send the packet */
260 silc_server_packet_send_real(server, sock, TRUE);
265 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
266 "original sender of this packet"));
270 /* Routes received packet to `sock'. This is used to route the packets that
271 router receives but are not destined to it. */
273 void silc_server_packet_route(SilcServer server,
274 SilcSocketConnection sock,
275 SilcPacketContext *packet)
277 SilcBuffer buffer = packet->buffer;
278 SilcIDListData idata;
280 SILC_LOG_DEBUG(("Routing received packet"));
282 idata = (SilcIDListData)sock->user_data;
284 silc_buffer_push(buffer, buffer->data - buffer->head);
285 silc_packet_send_prepare(sock, 0, 0, buffer->len);
286 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
287 silc_packet_encrypt(idata->send_key, idata->hmac,
288 sock->outbuf, sock->outbuf->len);
290 SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
291 sock->outbuf->data, sock->outbuf->len);
293 /* Now actually send the packet */
294 silc_server_packet_send_real(server, sock, TRUE);
297 /* Internal routine to actually create the channel packet and send it
298 to network. This is common function in channel message sending. If
299 `channel_message' is TRUE this encrypts the message as it is strictly
300 a channel message. If FALSE normal encryption process is used. */
303 silc_server_packet_send_to_channel_real(SilcServer server,
304 SilcSocketConnection sock,
305 SilcPacketContext *packet,
309 unsigned int data_len,
313 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
314 packet->src_id_len + packet->dst_id_len;
316 /* Prepare outgoing data buffer for packet sending */
317 silc_packet_send_prepare(sock,
318 SILC_PACKET_HEADER_LEN +
324 packet->buffer = sock->outbuf;
326 /* Put the data to buffer, assemble and encrypt the packet. The packet
327 is encrypted with normal session key shared with the client. */
328 silc_buffer_put(sock->outbuf, data, data_len);
329 silc_packet_assemble(packet);
331 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
332 packet->src_id_len + packet->dst_id_len +
335 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
337 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
338 sock->outbuf->data, sock->outbuf->len);
340 /* Now actually send the packet */
341 silc_server_packet_send_real(server, sock, force_send);
344 /* This routine is used by the server to send packets to channel. The
345 packet sent with this function is distributed to all clients on
346 the channel. Usually this is used to send notify messages to the
347 channel, things like notify about new user joining to the channel.
348 If `route' is FALSE then the packet is sent only locally and will not
349 be routed anywhere (for router locally means cell wide). */
351 void silc_server_packet_send_to_channel(SilcServer server,
352 SilcChannelEntry channel,
356 unsigned int data_len,
359 SilcSocketConnection sock = NULL;
360 SilcPacketContext packetdata;
361 SilcClientEntry client = NULL;
362 SilcServerEntry *routed = NULL;
363 SilcChannelClientEntry chl;
364 SilcIDListData idata;
365 unsigned int routed_count = 0;
367 /* This doesn't send channel message packets */
368 if (type == SILC_PACKET_CHANNEL_MESSAGE)
371 SILC_LOG_DEBUG(("Sending packet to channel"));
373 /* Set the packet context pointers. */
374 packetdata.flags = 0;
375 packetdata.type = type;
376 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
377 packetdata.src_id_len = SILC_ID_SERVER_LEN;
378 packetdata.src_id_type = SILC_ID_SERVER;
379 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
380 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
381 packetdata.dst_id_type = SILC_ID_CHANNEL;
382 packetdata.rng = server->rng;
383 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
384 packetdata.src_id_len + packetdata.dst_id_len;
385 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
387 /* If there are global users in the channel we will send the message
388 first to our router for further routing. */
389 if (route && server->server_type == SILC_SERVER && !server->standalone &&
390 channel->global_users) {
391 SilcServerEntry router;
393 /* Get data used in packet header encryption, keys and stuff. */
394 router = server->router;
395 sock = (SilcSocketConnection)router->connection;
396 idata = (SilcIDListData)router;
398 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
400 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
401 idata->send_key, idata->hmac,
402 data, data_len, FALSE, force_send);
405 /* Send the message to clients on the channel's client list. */
406 silc_list_start(channel->user_list);
407 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
408 client = chl->client;
410 /* If client has router set it is not locally connected client and
411 we will route the message to the router set in the client. Though,
412 send locally connected server in all cases. */
413 if (server->server_type == SILC_ROUTER && client && client->router &&
414 ((!route && client->router->router == server->id_entry) || route)) {
417 /* Check if we have sent the packet to this route already */
418 for (k = 0; k < routed_count; k++)
419 if (routed[k] == client->router)
421 if (k < routed_count)
424 /* Get data used in packet header encryption, keys and stuff. */
425 sock = (SilcSocketConnection)client->router->connection;
426 idata = (SilcIDListData)client->router;
428 /* Send the packet */
429 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
430 idata->send_key, idata->hmac,
431 data, data_len, FALSE,
434 /* We want to make sure that the packet is routed to same router
435 only once. Mark this route as sent route. */
437 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
438 routed[k] = client->router;
444 /* Send to locally connected client */
447 /* Get data used in packet header encryption, keys and stuff. */
448 sock = (SilcSocketConnection)client->connection;
449 idata = (SilcIDListData)client;
451 /* Send the packet */
452 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
453 idata->send_key, idata->hmac,
454 data, data_len, FALSE,
461 silc_free(packetdata.src_id);
462 silc_free(packetdata.dst_id);
465 /* This routine is explicitly used to relay messages to some channel.
466 Packets sent with this function we have received earlier and are
467 totally encrypted. This just sends the packet to all clients on
468 the channel. If the sender of the packet is someone on the channel
469 the message will not be sent to that client. The SILC Packet header
470 is encrypted with the session key shared between us and the client.
471 MAC is also computed before encrypting the header. Rest of the
472 packet will be untouched. */
474 void silc_server_packet_relay_to_channel(SilcServer server,
475 SilcSocketConnection sender_sock,
476 SilcChannelEntry channel,
478 SilcIdType sender_type,
480 unsigned int data_len,
484 SilcSocketConnection sock = NULL;
485 SilcPacketContext packetdata;
486 SilcClientEntry client = NULL;
487 SilcServerEntry *routed = NULL;
488 SilcChannelClientEntry chl;
489 unsigned int routed_count = 0;
490 SilcIDListData idata;
492 SILC_LOG_DEBUG(("Relaying packet to channel"));
494 /* Set the packet context pointers. */
495 packetdata.flags = 0;
496 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
497 packetdata.src_id = silc_id_id2str(sender, sender_type);
498 packetdata.src_id_len = silc_id_get_len(sender_type);
499 packetdata.src_id_type = sender_type;
500 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
501 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
502 packetdata.dst_id_type = SILC_ID_CHANNEL;
503 packetdata.rng = server->rng;
504 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
505 packetdata.src_id_len +
506 packetdata.dst_id_len));
508 /* If there are global users in the channel we will send the message
509 first to our router for further routing. */
510 if (server->server_type == SILC_SERVER && !server->standalone &&
511 channel->global_users) {
512 SilcServerEntry router;
514 router = server->router;
516 /* Check that the sender is not our router. */
517 if (sender_sock != (SilcSocketConnection)router->connection) {
519 /* Get data used in packet header encryption, keys and stuff. */
520 sock = (SilcSocketConnection)router->connection;
521 idata = (SilcIDListData)router;
523 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
525 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
526 idata->send_key, idata->hmac,
527 data, data_len, TRUE,
532 /* Send the message to clients on the channel's client list. */
533 silc_list_start(channel->user_list);
534 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
535 client = chl->client;
539 /* If sender is one on the channel do not send it the packet. */
540 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
545 /* If the client has set router it means that it is not locally
546 connected client and we will route the packet further. */
547 if (server->server_type == SILC_ROUTER && client->router) {
550 /* Sender maybe server as well so we want to make sure that
551 we won't send the message to the server it came from. */
552 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
557 /* Check if we have sent the packet to this route already */
558 for (k = 0; k < routed_count; k++)
559 if (routed[k] == client->router)
561 if (k < routed_count)
564 /* Get data used in packet header encryption, keys and stuff. */
565 sock = (SilcSocketConnection)client->router->connection;
566 idata = (SilcIDListData)client->router;
568 /* Send the packet */
569 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
570 idata->send_key, idata->hmac,
571 data, data_len, TRUE,
574 /* We want to make sure that the packet is routed to same router
575 only once. Mark this route as sent route. */
577 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
578 routed[k] = client->router;
584 /* XXX Check client's mode on the channel. */
586 /* Get data used in packet header encryption, keys and stuff. */
587 sock = (SilcSocketConnection)client->connection;
588 idata = (SilcIDListData)client;
590 SILC_LOG_DEBUG(("Sending packet to client %s (%s)",
591 sock->hostname, sock->ip));
593 /* Send the packet */
594 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
595 idata->send_key, idata->hmac,
596 data, data_len, TRUE,
601 silc_free(packetdata.src_id);
602 silc_free(packetdata.dst_id);
605 /* This function is used to send packets strictly to all local clients
606 on a particular channel. This is used for example to distribute new
607 channel key to all our locally connected clients on the channel.
608 The packets are always encrypted with the session key shared between
609 the client, this means these are not _to the channel_ but _to the client_
612 void silc_server_packet_send_local_channel(SilcServer server,
613 SilcChannelEntry channel,
615 SilcPacketFlags flags,
617 unsigned int data_len,
620 SilcChannelClientEntry chl;
621 SilcSocketConnection sock = NULL;
623 SILC_LOG_DEBUG(("Start"));
625 /* Send the message to clients on the channel's client list. */
626 silc_list_start(channel->user_list);
627 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
629 sock = (SilcSocketConnection)chl->client->connection;
631 /* Send the packet to the client */
632 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
633 SILC_ID_CLIENT, data, data_len,
639 /* Routine used to send (relay, route) private messages to some destination.
640 If the private message key does not exist then the message is re-encrypted,
641 otherwise we just pass it along. This really is not used to send new
642 private messages (as server does not send them) but to relay received
645 void silc_server_send_private_message(SilcServer server,
646 SilcSocketConnection dst_sock,
649 SilcPacketContext *packet)
651 SilcBuffer buffer = packet->buffer;
653 /* Send and re-encrypt if private messge key does not exist */
654 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
656 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
657 + packet->dst_id_len + packet->padlen);
658 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
659 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
661 /* Re-encrypt packet */
662 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
664 /* Send the packet */
665 silc_server_packet_send_real(server, dst_sock, FALSE);
668 /* Key exist so just send it */
669 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
670 + packet->dst_id_len + packet->padlen);
671 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
672 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
673 silc_server_packet_send_real(server, dst_sock, FALSE);
677 /* Sends current motd to client */
679 void silc_server_send_motd(SilcServer server,
680 SilcSocketConnection sock)
685 if (server->config && server->config->motd &&
686 server->config->motd->motd_file) {
688 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
692 silc_server_send_notify(server, sock, SILC_NOTIFY_TYPE_MOTD, 1,
698 /* Sends error message. Error messages may or may not have any
701 void silc_server_send_error(SilcServer server,
702 SilcSocketConnection sock,
703 const char *fmt, ...)
706 unsigned char buf[4096];
708 memset(buf, 0, sizeof(buf));
710 vsprintf(buf, fmt, ap);
713 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
714 buf, strlen(buf), FALSE);
717 /* Sends notify message. If format is TRUE the variable arguments are
718 formatted and the formatted string is sent as argument payload. If it is
719 FALSE then each argument is sent as separate argument and their format
720 in the argument list must be { argument data, argument length }. */
722 void silc_server_send_notify(SilcServer server,
723 SilcSocketConnection sock,
725 unsigned int argc, ...)
732 packet = silc_notify_payload_encode(type, argc, ap);
733 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
734 packet->data, packet->len, FALSE);
735 silc_buffer_free(packet);
738 /* Sends notify message destined to specific entity. */
740 void silc_server_send_notify_dest(SilcServer server,
741 SilcSocketConnection sock,
743 SilcIdType dest_id_type,
745 unsigned int argc, ...)
752 packet = silc_notify_payload_encode(type, argc, ap);
753 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
754 dest_id, dest_id_type,
755 packet->data, packet->len, FALSE);
756 silc_buffer_free(packet);
759 /* Sends notify message to a channel. The notify message sent is
760 distributed to all clients on the channel. If `router_notify' is TRUE
761 then the notify may be routed to primary route or to some other routers.
762 If FALSE it is assured that the notify is sent only locally. */
764 void silc_server_send_notify_to_channel(SilcServer server,
765 SilcChannelEntry channel,
766 unsigned char route_notify,
768 unsigned int argc, ...)
775 packet = silc_notify_payload_encode(type, argc, ap);
776 silc_server_packet_send_to_channel(server, channel,
777 SILC_PACKET_NOTIFY, route_notify,
778 packet->data, packet->len, FALSE);
779 silc_buffer_free(packet);
782 /* Send notify message to all clients the client has joined. It is quaranteed
783 that the message is sent only once to a client (ie. if a client is joined
784 on two same channel it will receive only one notify message). Also, this
785 sends only to local clients (locally connected if we are server, and to
786 local servers if we are router). */
788 void silc_server_send_notify_on_channels(SilcServer server,
789 SilcClientEntry client,
791 unsigned int argc, ...)
794 SilcSocketConnection sock = NULL;
795 SilcPacketContext packetdata;
797 SilcClientEntry *sent_clients = NULL;
798 unsigned int sent_clients_count = 0;
799 SilcServerEntry *routed = NULL;
800 unsigned int routed_count = 0;
801 SilcChannelEntry channel;
802 SilcChannelClientEntry chl, chl2;
803 SilcIDListData idata;
806 unsigned int data_len;
807 int force_send = FALSE;
810 SILC_LOG_DEBUG(("Start"));
812 if (!silc_list_count(client->channels))
816 packet = silc_notify_payload_encode(type, argc, ap);
818 data_len = packet->len;
820 /* Set the packet context pointers. */
821 packetdata.flags = 0;
822 packetdata.type = SILC_PACKET_NOTIFY;
823 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
824 packetdata.src_id_len = SILC_ID_SERVER_LEN;
825 packetdata.src_id_type = SILC_ID_SERVER;
826 packetdata.rng = server->rng;
828 silc_list_start(client->channels);
829 while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
830 channel = chl->channel;
832 /* Send the message to all clients on the channel's client list. */
833 silc_list_start(channel->user_list);
834 while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
837 /* Check if we have sent the packet to this client already */
838 for (k = 0; k < sent_clients_count; k++)
839 if (sent_clients[k] == c)
841 if (k < sent_clients_count)
844 /* If we are router and if this client has router set it is not
845 locally connected client and we will route the message to the
846 router set in the client. */
847 if (c && c->router && server->server_type == SILC_ROUTER) {
848 /* Check if we have sent the packet to this route already */
849 for (k = 0; k < routed_count; k++)
850 if (routed[k] == c->router)
852 if (k < routed_count)
855 /* Get data used in packet header encryption, keys and stuff. */
856 sock = (SilcSocketConnection)c->router->connection;
857 idata = (SilcIDListData)c->router;
859 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
860 packetdata.dst_id_len = SILC_ID_SERVER_LEN;
861 packetdata.dst_id_type = SILC_ID_SERVER;
862 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
863 packetdata.src_id_len + packetdata.dst_id_len;
864 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
866 /* Send the packet */
867 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
868 idata->send_key, idata->hmac,
869 data, data_len, FALSE,
872 silc_free(packetdata.dst_id);
874 /* We want to make sure that the packet is routed to same router
875 only once. Mark this route as sent route. */
877 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
878 routed[k] = c->router;
884 /* Send to locally connected client */
887 /* Get data used in packet header encryption, keys and stuff. */
888 sock = (SilcSocketConnection)c->connection;
889 idata = (SilcIDListData)c;
891 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
892 packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
893 packetdata.dst_id_type = SILC_ID_CLIENT;
894 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
895 packetdata.src_id_len + packetdata.dst_id_len;
896 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
898 /* Send the packet */
899 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
900 idata->send_key, idata->hmac,
901 data, data_len, FALSE,
904 silc_free(packetdata.dst_id);
906 /* Make sure that we send the notify only once per client. */
907 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
908 (sent_clients_count + 1));
909 sent_clients[sent_clients_count] = c;
910 sent_clients_count++;
917 if (sent_clients_count)
918 silc_free(sent_clients);
919 silc_free(packetdata.src_id);
922 /* Sends New ID Payload to remote end. The packet is used to distribute
923 information about new registered clients, servers, channel etc. usually
924 to routers so that they can keep these information up to date.
925 If the argument `broadcast' is TRUE then the packet is sent as
928 void silc_server_send_new_id(SilcServer server,
929 SilcSocketConnection sock,
931 void *id, SilcIdType id_type,
936 SILC_LOG_DEBUG(("Start"));
938 idp = silc_id_payload_encode(id, id_type);
939 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
940 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
941 idp->data, idp->len, FALSE);
942 silc_buffer_free(idp);
945 /* Sends Replace ID payload to remote end. This is used to replace old
946 ID with new ID sent in the packet. This is called for example when
947 user changes nickname and we create new ID for the user. If the
948 argument `broadcast' is TRUE then the packet is sent as
950 /* XXX It would be expected that the new id is same type as the old
953 void silc_server_send_replace_id(SilcServer server,
954 SilcSocketConnection sock,
956 void *old_id, SilcIdType old_id_type,
957 unsigned int old_id_len,
958 void *new_id, SilcIdType new_id_type,
959 unsigned int new_id_len)
965 SILC_LOG_DEBUG(("Start"));
967 oid = silc_id_id2str(old_id, old_id_type);
971 nid = silc_id_id2str(new_id, new_id_type);
975 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
976 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
977 silc_buffer_format(packet,
978 SILC_STR_UI_SHORT(old_id_type),
979 SILC_STR_UI_SHORT(old_id_len),
980 SILC_STR_UI_XNSTRING(oid, old_id_len),
981 SILC_STR_UI_SHORT(new_id_type),
982 SILC_STR_UI_SHORT(new_id_len),
983 SILC_STR_UI_XNSTRING(nid, new_id_len),
986 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
987 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
988 packet->data, packet->len, FALSE);
991 silc_buffer_free(packet);
994 /* This function is used to send Remove Channel User payload. This may sent
995 by server but is usually used only by router to notify other routers that
996 user has left a channel. Normal server sends this packet to its router
997 to notify that the router should not hold a record about this client
998 on a channel anymore. Router distributes it further to other routers. */
1000 void silc_server_send_remove_channel_user(SilcServer server,
1001 SilcSocketConnection sock,
1003 void *client_id, void *channel_id)
1006 unsigned char *clid, *chid;
1008 SILC_LOG_DEBUG(("Start"));
1010 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1014 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1018 packet = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN + SILC_ID_CHANNEL_LEN);
1019 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1020 silc_buffer_format(packet,
1021 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
1022 SILC_STR_UI_XNSTRING(clid, SILC_ID_CLIENT_LEN),
1023 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
1024 SILC_STR_UI_XNSTRING(chid, SILC_ID_CHANNEL_LEN),
1027 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_CHANNEL_USER,
1028 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1029 packet->data, packet->len, FALSE);
1032 silc_buffer_free(packet);
1035 /* Send New Channel Payload to notify about newly created channel in the
1036 SILC network. Normal server nevers sends this packet. Router uses this
1037 to notify other routers in the network about new channel. This packet
1040 void silc_server_send_new_channel(SilcServer server,
1041 SilcSocketConnection sock,
1045 unsigned int channel_id_len)
1049 unsigned int name_len = strlen(channel_name);
1051 SILC_LOG_DEBUG(("Start"));
1053 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1057 packet = silc_buffer_alloc(2 + 2 + name_len + channel_id_len);
1058 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1059 silc_buffer_format(packet,
1060 SILC_STR_UI_SHORT(name_len),
1061 SILC_STR_UI_XNSTRING(channel_name, name_len),
1062 SILC_STR_UI_SHORT(channel_id_len),
1063 SILC_STR_UI_XNSTRING(cid, channel_id_len),
1066 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1067 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1068 packet->data, packet->len, FALSE);
1071 silc_buffer_free(packet);
1074 /* Send New Channel User payload to notify routers in the network about new
1075 user on the channel. The packet is may be broadcasted. Normal server
1076 can send this but must not receive. Router can send and receive it. */
1078 void silc_server_send_new_channel_user(SilcServer server,
1079 SilcSocketConnection sock,
1082 unsigned int channel_id_len,
1084 unsigned int client_id_len)
1087 unsigned char *clid, *chid;
1089 SILC_LOG_DEBUG(("Start"));
1091 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1095 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1099 packet = silc_buffer_alloc(2 + 2 + channel_id_len + client_id_len);
1100 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1101 silc_buffer_format(packet,
1102 SILC_STR_UI_SHORT(channel_id_len),
1103 SILC_STR_UI_XNSTRING(chid, channel_id_len),
1104 SILC_STR_UI_SHORT(client_id_len),
1105 SILC_STR_UI_XNSTRING(clid, client_id_len),
1108 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL_USER,
1109 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1110 packet->data, packet->len, FALSE);
1113 silc_buffer_free(packet);
1116 /* Send Channel Key payload to distribute the new channel key. Normal server
1117 sends this to router when new client joins to existing channel. Router
1118 sends this to the local server who forwarded join command in case where
1119 the channel did not exist yet. Both normal and router servers uses this
1120 also to send this to locally connected clients on the channel. This
1121 must not be broadcasted packet. */
1123 void silc_server_send_channel_key(SilcServer server,
1124 SilcChannelEntry channel,
1125 unsigned char route)
1128 unsigned char *chid;
1129 unsigned int tmp_len;
1131 SILC_LOG_DEBUG(("Start"));
1133 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1137 /* Encode channel key packet */
1138 tmp_len = strlen(channel->channel_key->cipher->name);
1139 packet = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, chid, tmp_len,
1140 channel->channel_key->cipher->name,
1141 channel->key_len / 8, channel->key);
1143 silc_server_packet_send_to_channel(server, channel, SILC_PACKET_CHANNEL_KEY,
1144 route, packet->data, packet->len, FALSE);
1145 silc_buffer_free(packet);
1149 /* Generic function to send any command. The arguments must be sent already
1150 encoded into correct form in correct order. */
1152 void silc_server_send_command(SilcServer server,
1153 SilcSocketConnection sock,
1154 SilcCommand command,
1155 unsigned int argc, ...)
1162 packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1163 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1164 packet->data, packet->len, TRUE);
1165 silc_buffer_free(packet);
1168 /* Function used to send REMOVE_ID packet. The packet is used to notify
1169 routers that certain ID should be removed. After that the ID will become
1170 invalid. If the argument `broadcast' is TRUE then the packet is sent as
1171 broadcast packet. */
1173 void silc_server_send_remove_id(SilcServer server,
1174 SilcSocketConnection sock,
1176 void *id, unsigned int id_len,
1181 SILC_LOG_DEBUG(("Start"));
1183 idp = silc_id_payload_encode(id, id_type);
1184 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_ID,
1185 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1186 idp->data, idp->len, FALSE);
1187 silc_buffer_free(idp);