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,
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 /* Broadcast received packet to our primary route. This function is used
184 by router to further route received broadcast packet. It is expected
185 that the broadcast flag from the packet is checked before calling this
186 function. This does not test or set the broadcast flag. */
188 void silc_server_packet_broadcast(SilcServer server,
189 SilcSocketConnection sock,
190 SilcPacketContext *packet)
192 SilcBuffer buffer = packet->buffer;
193 SilcIDListData idata;
196 SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
198 /* If the packet is originated from our primary route we are
199 not allowed to send the packet. */
200 id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
201 if (id && SILC_ID_SERVER_COMPARE(id, server->router->id)) {
202 idata = (SilcIDListData)sock->user_data;
204 silc_buffer_push(buffer, buffer->data - buffer->head);
205 silc_packet_send_prepare(sock, 0, 0, buffer->len);
206 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
207 silc_packet_encrypt(idata->send_key, idata->hmac,
208 sock->outbuf, sock->outbuf->len);
210 SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
211 sock->outbuf->data, sock->outbuf->len);
213 /* Now actually send the packet */
214 silc_server_packet_send_real(server, sock, TRUE);
219 SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
220 "original sender of this packet"));
224 /* Routes received packet to `sock'. This is used to route the packets that
225 router receives but are not destined to it. */
227 void silc_server_packet_route(SilcServer server,
228 SilcSocketConnection sock,
229 SilcPacketContext *packet)
231 SilcBuffer buffer = packet->buffer;
232 SilcIDListData idata;
234 SILC_LOG_DEBUG(("Routing received packet"));
236 idata = (SilcIDListData)sock->user_data;
238 silc_buffer_push(buffer, buffer->data - buffer->head);
239 silc_packet_send_prepare(sock, 0, 0, buffer->len);
240 silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
241 silc_packet_encrypt(idata->send_key, idata->hmac,
242 sock->outbuf, sock->outbuf->len);
244 SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
245 sock->outbuf->data, sock->outbuf->len);
247 /* Now actually send the packet */
248 silc_server_packet_send_real(server, sock, TRUE);
251 /* Internal routine to actually create the channel packet and send it
252 to network. This is common function in channel message sending. If
253 `channel_message' is TRUE this encrypts the message as it is strictly
254 a channel message. If FALSE normal encryption process is used. */
257 silc_server_packet_send_to_channel_real(SilcServer server,
258 SilcSocketConnection sock,
259 SilcPacketContext *packet,
263 unsigned int data_len,
267 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
268 packet->src_id_len + packet->dst_id_len;
270 /* Prepare outgoing data buffer for packet sending */
271 silc_packet_send_prepare(sock,
272 SILC_PACKET_HEADER_LEN +
278 packet->buffer = sock->outbuf;
280 /* Put the data to buffer, assemble and encrypt the packet. The packet
281 is encrypted with normal session key shared with the client. */
282 silc_buffer_put(sock->outbuf, data, data_len);
283 silc_packet_assemble(packet);
285 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
286 packet->src_id_len + packet->dst_id_len +
289 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
291 SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
292 sock->outbuf->data, sock->outbuf->len);
294 /* Now actually send the packet */
295 silc_server_packet_send_real(server, sock, force_send);
298 /* This routine is used by the server to send packets to channel. The
299 packet sent with this function is distributed to all clients on
300 the channel. Usually this is used to send notify messages to the
301 channel, things like notify about new user joining to the channel.
302 If `route' is FALSE then the packet is sent only locally and will not
303 be routed anywhere (for router locally means cell wide). If `sender'
304 is provided then the packet is not sent to that connection since it
305 originally came from it. */
307 void silc_server_packet_send_to_channel(SilcServer server,
308 SilcSocketConnection sender,
309 SilcChannelEntry channel,
313 unsigned int data_len,
316 SilcSocketConnection sock = NULL;
317 SilcPacketContext packetdata;
318 SilcClientEntry client = NULL;
319 SilcServerEntry *routed = NULL;
320 SilcChannelClientEntry chl;
321 SilcIDListData idata;
322 unsigned int routed_count = 0;
324 /* This doesn't send channel message packets */
325 if (type == SILC_PACKET_CHANNEL_MESSAGE)
328 SILC_LOG_DEBUG(("Sending packet to channel"));
330 /* Set the packet context pointers. */
331 packetdata.flags = 0;
332 packetdata.type = type;
333 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
334 packetdata.src_id_len = SILC_ID_SERVER_LEN;
335 packetdata.src_id_type = SILC_ID_SERVER;
336 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
337 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
338 packetdata.dst_id_type = SILC_ID_CHANNEL;
339 packetdata.rng = server->rng;
340 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
341 packetdata.src_id_len + packetdata.dst_id_len;
342 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
344 /* If there are global users in the channel we will send the message
345 first to our router for further routing. */
346 if (route && server->server_type == SILC_SERVER && !server->standalone &&
347 channel->global_users) {
348 SilcServerEntry router;
350 /* Get data used in packet header encryption, keys and stuff. */
351 router = server->router;
352 sock = (SilcSocketConnection)router->connection;
353 idata = (SilcIDListData)router;
355 if (sock != sender) {
356 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
358 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
359 idata->send_key, idata->hmac,
360 data, data_len, FALSE,
365 /* Send the message to clients on the channel's client list. */
366 silc_list_start(channel->user_list);
367 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
368 client = chl->client;
370 /* If client has router set it is not locally connected client and
371 we will route the message to the router set in the client. Though,
372 send locally connected server in all cases. */
373 if (server->server_type == SILC_ROUTER && client && client->router &&
374 ((!route && client->router->router == server->id_entry) || route)) {
377 /* Check if we have sent the packet to this route already */
378 for (k = 0; k < routed_count; k++)
379 if (routed[k] == client->router)
381 if (k < routed_count)
384 /* Get data used in packet header encryption, keys and stuff. */
385 sock = (SilcSocketConnection)client->router->connection;
386 idata = (SilcIDListData)client->router;
388 if (sender && sock == sender)
391 /* Send the packet */
392 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
393 idata->send_key, idata->hmac,
394 data, data_len, FALSE,
397 /* We want to make sure that the packet is routed to same router
398 only once. Mark this route as sent route. */
400 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
401 routed[k] = client->router;
407 if (client && client->router)
410 /* Send to locally connected client */
413 /* Get data used in packet header encryption, keys and stuff. */
414 sock = (SilcSocketConnection)client->connection;
415 idata = (SilcIDListData)client;
417 if (sender && sock == sender)
420 /* Send the packet */
421 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
422 idata->send_key, idata->hmac,
423 data, data_len, FALSE,
430 silc_free(packetdata.src_id);
431 silc_free(packetdata.dst_id);
434 /* This routine is explicitly used to relay messages to some channel.
435 Packets sent with this function we have received earlier and are
436 totally encrypted. This just sends the packet to all clients on
437 the channel. If the sender of the packet is someone on the channel
438 the message will not be sent to that client. The SILC Packet header
439 is encrypted with the session key shared between us and the client.
440 MAC is also computed before encrypting the header. Rest of the
441 packet will be untouched. */
443 void silc_server_packet_relay_to_channel(SilcServer server,
444 SilcSocketConnection sender_sock,
445 SilcChannelEntry channel,
447 SilcIdType sender_type,
449 unsigned int data_len,
453 SilcSocketConnection sock = NULL;
454 SilcPacketContext packetdata;
455 SilcClientEntry client = NULL;
456 SilcServerEntry *routed = NULL;
457 SilcChannelClientEntry chl;
458 unsigned int routed_count = 0;
459 SilcIDListData idata;
461 SILC_LOG_DEBUG(("Relaying packet to channel"));
463 /* Set the packet context pointers. */
464 packetdata.flags = 0;
465 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
466 packetdata.src_id = silc_id_id2str(sender, sender_type);
467 packetdata.src_id_len = silc_id_get_len(sender_type);
468 packetdata.src_id_type = sender_type;
469 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
470 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
471 packetdata.dst_id_type = SILC_ID_CHANNEL;
472 packetdata.rng = server->rng;
473 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
474 packetdata.src_id_len +
475 packetdata.dst_id_len));
477 /* If there are global users in the channel we will send the message
478 first to our router for further routing. */
479 if (server->server_type == SILC_SERVER && !server->standalone &&
480 channel->global_users) {
481 SilcServerEntry router;
483 router = server->router;
485 /* Check that the sender is not our router. */
486 if (sender_sock != (SilcSocketConnection)router->connection) {
488 /* Get data used in packet header encryption, keys and stuff. */
489 sock = (SilcSocketConnection)router->connection;
490 idata = (SilcIDListData)router;
492 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
494 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
495 idata->send_key, idata->hmac,
496 data, data_len, TRUE,
501 /* Send the message to clients on the channel's client list. */
502 silc_list_start(channel->user_list);
503 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
504 client = chl->client;
508 /* If sender is one on the channel do not send it the packet. */
509 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
514 /* If the client has set router it means that it is not locally
515 connected client and we will route the packet further. */
516 if (server->server_type == SILC_ROUTER && client->router) {
519 /* Sender maybe server as well so we want to make sure that
520 we won't send the message to the server it came from. */
521 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
526 /* Check if we have sent the packet to this route already */
527 for (k = 0; k < routed_count; k++)
528 if (routed[k] == client->router)
530 if (k < routed_count)
533 /* Get data used in packet header encryption, keys and stuff. */
534 sock = (SilcSocketConnection)client->router->connection;
535 idata = (SilcIDListData)client->router;
537 if (sender_sock && sock == sender_sock)
540 SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)",
541 silc_id_render(client->id, SILC_ID_CLIENT),
542 sock->hostname, sock->ip));
544 /* Send the packet */
545 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
546 idata->send_key, idata->hmac,
547 data, data_len, TRUE,
550 /* We want to make sure that the packet is routed to same router
551 only once. Mark this route as sent route. */
553 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
554 routed[k] = client->router;
560 if (client && client->router)
563 /* Get data used in packet header encryption, keys and stuff. */
564 sock = (SilcSocketConnection)client->connection;
565 idata = (SilcIDListData)client;
567 if (sender_sock && sock == sender_sock)
570 SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)",
571 silc_id_render(client->id, SILC_ID_CLIENT),
572 sock->hostname, sock->ip));
574 /* Send the packet */
575 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
576 idata->send_key, idata->hmac,
577 data, data_len, TRUE,
582 silc_free(packetdata.src_id);
583 silc_free(packetdata.dst_id);
586 /* This function is used to send packets strictly to all local clients
587 on a particular channel. This is used for example to distribute new
588 channel key to all our locally connected clients on the channel.
589 The packets are always encrypted with the session key shared between
590 the client, this means these are not _to the channel_ but _to the client_
593 void silc_server_packet_send_local_channel(SilcServer server,
594 SilcChannelEntry channel,
596 SilcPacketFlags flags,
598 unsigned int data_len,
601 SilcChannelClientEntry chl;
602 SilcSocketConnection sock = NULL;
604 SILC_LOG_DEBUG(("Start"));
606 /* Send the message to clients on the channel's client list. */
607 silc_list_start(channel->user_list);
608 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
609 if (chl->client && !chl->client->router) {
610 sock = (SilcSocketConnection)chl->client->connection;
612 /* Send the packet to the client */
613 silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
614 SILC_ID_CLIENT, data, data_len,
620 /* Routine used to send (relay, route) private messages to some destination.
621 If the private message key does not exist then the message is re-encrypted,
622 otherwise we just pass it along. This really is not used to send new
623 private messages (as server does not send them) but to relay received
626 void silc_server_send_private_message(SilcServer server,
627 SilcSocketConnection dst_sock,
630 SilcPacketContext *packet)
632 SilcBuffer buffer = packet->buffer;
634 /* Send and re-encrypt if private messge key does not exist */
635 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
637 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
638 + packet->dst_id_len + packet->padlen);
639 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
640 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
642 /* Re-encrypt packet */
643 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
645 /* Send the packet */
646 silc_server_packet_send_real(server, dst_sock, FALSE);
649 /* Key exist so just send it */
650 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
651 + packet->dst_id_len + packet->padlen);
652 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
653 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
654 silc_server_packet_send_real(server, dst_sock, FALSE);
658 /* Sends current motd to client */
660 void silc_server_send_motd(SilcServer server,
661 SilcSocketConnection sock)
666 if (server->config && server->config->motd &&
667 server->config->motd->motd_file) {
669 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
673 silc_server_send_notify(server, sock, SILC_NOTIFY_TYPE_MOTD, 1,
679 /* Sends error message. Error messages may or may not have any
682 void silc_server_send_error(SilcServer server,
683 SilcSocketConnection sock,
684 const char *fmt, ...)
687 unsigned char buf[4096];
689 memset(buf, 0, sizeof(buf));
691 vsprintf(buf, fmt, ap);
694 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
695 buf, strlen(buf), FALSE);
698 /* Sends notify message. If format is TRUE the variable arguments are
699 formatted and the formatted string is sent as argument payload. If it is
700 FALSE then each argument is sent as separate argument and their format
701 in the argument list must be { argument data, argument length }. */
703 void silc_server_send_notify(SilcServer server,
704 SilcSocketConnection sock,
706 unsigned int argc, ...)
713 packet = silc_notify_payload_encode(type, argc, ap);
714 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
715 packet->data, packet->len, FALSE);
716 silc_buffer_free(packet);
719 /* Sends notify message destined to specific entity. */
721 void silc_server_send_notify_dest(SilcServer server,
722 SilcSocketConnection sock,
724 SilcIdType dest_id_type,
726 unsigned int argc, ...)
733 packet = silc_notify_payload_encode(type, argc, ap);
734 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
735 dest_id, dest_id_type,
736 packet->data, packet->len, FALSE);
737 silc_buffer_free(packet);
740 /* Sends notify message to a channel. The notify message sent is
741 distributed to all clients on the channel. If `router_notify' is TRUE
742 then the notify may be routed to primary route or to some other routers.
743 If FALSE it is assured that the notify is sent only locally. If `sender'
744 is provided then the packet is not sent to that connection since it
745 originally came from it. */
747 void silc_server_send_notify_to_channel(SilcServer server,
748 SilcSocketConnection sender,
749 SilcChannelEntry channel,
750 unsigned char route_notify,
752 unsigned int argc, ...)
759 packet = silc_notify_payload_encode(type, argc, ap);
760 silc_server_packet_send_to_channel(server, sender, channel,
761 SILC_PACKET_NOTIFY, route_notify,
762 packet->data, packet->len, FALSE);
763 silc_buffer_free(packet);
766 /* Send notify message to all clients the client has joined. It is quaranteed
767 that the message is sent only once to a client (ie. if a client is joined
768 on two same channel it will receive only one notify message). Also, this
769 sends only to local clients (locally connected if we are server, and to
770 local servers if we are router). */
772 void silc_server_send_notify_on_channels(SilcServer server,
773 SilcClientEntry client,
775 unsigned int argc, ...)
778 SilcSocketConnection sock = NULL;
779 SilcPacketContext packetdata;
781 SilcClientEntry *sent_clients = NULL;
782 unsigned int sent_clients_count = 0;
783 SilcServerEntry *routed = NULL;
784 unsigned int routed_count = 0;
785 SilcChannelEntry channel;
786 SilcChannelClientEntry chl, chl2;
787 SilcIDListData idata;
790 unsigned int data_len;
791 int force_send = FALSE;
794 SILC_LOG_DEBUG(("Start"));
796 if (!silc_list_count(client->channels))
800 packet = silc_notify_payload_encode(type, argc, ap);
802 data_len = packet->len;
804 /* Set the packet context pointers. */
805 packetdata.flags = 0;
806 packetdata.type = SILC_PACKET_NOTIFY;
807 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
808 packetdata.src_id_len = SILC_ID_SERVER_LEN;
809 packetdata.src_id_type = SILC_ID_SERVER;
810 packetdata.rng = server->rng;
812 silc_list_start(client->channels);
813 while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
814 channel = chl->channel;
816 /* Send the message to all clients on the channel's client list. */
817 silc_list_start(channel->user_list);
818 while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
821 /* Check if we have sent the packet to this client already */
822 for (k = 0; k < sent_clients_count; k++)
823 if (sent_clients[k] == c)
825 if (k < sent_clients_count)
828 /* If we are router and if this client has router set it is not
829 locally connected client and we will route the message to the
830 router set in the client. */
831 if (c && c->router && server->server_type == SILC_ROUTER) {
832 /* Check if we have sent the packet to this route already */
833 for (k = 0; k < routed_count; k++)
834 if (routed[k] == c->router)
836 if (k < routed_count)
839 /* Get data used in packet header encryption, keys and stuff. */
840 sock = (SilcSocketConnection)c->router->connection;
841 idata = (SilcIDListData)c->router;
843 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
844 packetdata.dst_id_len = SILC_ID_SERVER_LEN;
845 packetdata.dst_id_type = SILC_ID_SERVER;
846 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
847 packetdata.src_id_len + packetdata.dst_id_len;
848 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
850 /* Send the packet */
851 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
852 idata->send_key, idata->hmac,
853 data, data_len, FALSE,
856 silc_free(packetdata.dst_id);
858 /* We want to make sure that the packet is routed to same router
859 only once. Mark this route as sent route. */
861 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
862 routed[k] = c->router;
871 /* Send to locally connected client */
874 /* Get data used in packet header encryption, keys and stuff. */
875 sock = (SilcSocketConnection)c->connection;
876 idata = (SilcIDListData)c;
878 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
879 packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
880 packetdata.dst_id_type = SILC_ID_CLIENT;
881 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
882 packetdata.src_id_len + packetdata.dst_id_len;
883 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
885 /* Send the packet */
886 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
887 idata->send_key, idata->hmac,
888 data, data_len, FALSE,
891 silc_free(packetdata.dst_id);
893 /* Make sure that we send the notify only once per client. */
894 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
895 (sent_clients_count + 1));
896 sent_clients[sent_clients_count] = c;
897 sent_clients_count++;
904 if (sent_clients_count)
905 silc_free(sent_clients);
906 silc_free(packetdata.src_id);
909 /* Sends New ID Payload to remote end. The packet is used to distribute
910 information about new registered clients, servers, channel etc. usually
911 to routers so that they can keep these information up to date.
912 If the argument `broadcast' is TRUE then the packet is sent as
915 void silc_server_send_new_id(SilcServer server,
916 SilcSocketConnection sock,
918 void *id, SilcIdType id_type,
923 SILC_LOG_DEBUG(("Start"));
925 idp = silc_id_payload_encode(id, id_type);
926 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
927 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
928 idp->data, idp->len, FALSE);
929 silc_buffer_free(idp);
932 /* Sends Replace ID payload to remote end. This is used to replace old
933 ID with new ID sent in the packet. This is called for example when
934 user changes nickname and we create new ID for the user. If the
935 argument `broadcast' is TRUE then the packet is sent as
937 /* XXX It would be expected that the new id is same type as the old
940 void silc_server_send_replace_id(SilcServer server,
941 SilcSocketConnection sock,
943 void *old_id, SilcIdType old_id_type,
944 unsigned int old_id_len,
945 void *new_id, SilcIdType new_id_type,
946 unsigned int new_id_len)
952 SILC_LOG_DEBUG(("Start"));
954 oid = silc_id_id2str(old_id, old_id_type);
958 nid = silc_id_id2str(new_id, new_id_type);
962 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
963 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
964 silc_buffer_format(packet,
965 SILC_STR_UI_SHORT(old_id_type),
966 SILC_STR_UI_SHORT(old_id_len),
967 SILC_STR_UI_XNSTRING(oid, old_id_len),
968 SILC_STR_UI_SHORT(new_id_type),
969 SILC_STR_UI_SHORT(new_id_len),
970 SILC_STR_UI_XNSTRING(nid, new_id_len),
973 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
974 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
975 packet->data, packet->len, FALSE);
978 silc_buffer_free(packet);
981 /* This function is used to send Remove Channel User payload. This may sent
982 by server but is usually used only by router to notify other routers that
983 user has left a channel. Normal server sends this packet to its router
984 to notify that the router should not hold a record about this client
985 on a channel anymore. Router distributes it further to other routers. */
987 void silc_server_send_remove_channel_user(SilcServer server,
988 SilcSocketConnection sock,
990 void *client_id, void *channel_id)
993 unsigned char *clid, *chid;
995 SILC_LOG_DEBUG(("Start"));
997 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1001 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1005 packet = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN + SILC_ID_CHANNEL_LEN);
1006 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1007 silc_buffer_format(packet,
1008 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
1009 SILC_STR_UI_XNSTRING(clid, SILC_ID_CLIENT_LEN),
1010 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
1011 SILC_STR_UI_XNSTRING(chid, SILC_ID_CHANNEL_LEN),
1014 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_CHANNEL_USER,
1015 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1016 packet->data, packet->len, FALSE);
1019 silc_buffer_free(packet);
1022 /* Send New Channel Payload to notify about newly created channel in the
1023 SILC network. Normal server nevers sends this packet. Router uses this
1024 to notify other routers in the network about new channel. This packet
1027 void silc_server_send_new_channel(SilcServer server,
1028 SilcSocketConnection sock,
1032 unsigned int channel_id_len)
1036 unsigned int name_len = strlen(channel_name);
1038 SILC_LOG_DEBUG(("Start"));
1040 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1044 packet = silc_buffer_alloc(2 + 2 + name_len + channel_id_len);
1045 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1046 silc_buffer_format(packet,
1047 SILC_STR_UI_SHORT(name_len),
1048 SILC_STR_UI_XNSTRING(channel_name, name_len),
1049 SILC_STR_UI_SHORT(channel_id_len),
1050 SILC_STR_UI_XNSTRING(cid, channel_id_len),
1054 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1055 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1056 packet->data, packet->len, FALSE);
1059 silc_buffer_free(packet);
1062 /* Send New Channel User payload to notify routers in the network about new
1063 user on the channel. The packet is may be broadcasted. Normal server
1064 can send this but must not receive. Router can send and receive it. */
1066 void silc_server_send_new_channel_user(SilcServer server,
1067 SilcSocketConnection sock,
1070 unsigned int channel_id_len,
1072 unsigned int client_id_len)
1075 unsigned char *clid, *chid;
1077 SILC_LOG_DEBUG(("Start"));
1079 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1083 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1087 packet = silc_buffer_alloc(2 + 2 + channel_id_len + client_id_len);
1088 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1089 silc_buffer_format(packet,
1090 SILC_STR_UI_SHORT(channel_id_len),
1091 SILC_STR_UI_XNSTRING(chid, channel_id_len),
1092 SILC_STR_UI_SHORT(client_id_len),
1093 SILC_STR_UI_XNSTRING(clid, client_id_len),
1096 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL_USER,
1097 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1098 packet->data, packet->len, FALSE);
1101 silc_buffer_free(packet);
1104 /* Send Channel Key payload to distribute the new channel key. Normal server
1105 sends this to router when new client joins to existing channel. Router
1106 sends this to the local server who sent the join command in case where
1107 the channel did not exist yet. Both normal and router servers uses this
1108 also to send this to locally connected clients on the channel. This
1109 must not be broadcasted packet. Routers do not send this to each other.
1110 If `sender is provided then the packet is not sent to that connection since
1111 it originally came from it. */
1113 void silc_server_send_channel_key(SilcServer server,
1114 SilcSocketConnection sender,
1115 SilcChannelEntry channel,
1116 unsigned char route)
1119 unsigned char *chid;
1120 unsigned int tmp_len;
1122 SILC_LOG_DEBUG(("Start"));
1124 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1128 /* Encode channel key packet */
1129 tmp_len = strlen(channel->channel_key->cipher->name);
1130 packet = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, chid, tmp_len,
1131 channel->channel_key->cipher->name,
1132 channel->key_len / 8, channel->key);
1134 silc_server_packet_send_to_channel(server, sender, channel,
1135 SILC_PACKET_CHANNEL_KEY,
1136 route, packet->data, packet->len, FALSE);
1137 silc_buffer_free(packet);
1141 /* Generic function to send any command. The arguments must be sent already
1142 encoded into correct form in correct order. */
1144 void silc_server_send_command(SilcServer server,
1145 SilcSocketConnection sock,
1146 SilcCommand command,
1147 unsigned int argc, ...)
1154 packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1155 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1156 packet->data, packet->len, TRUE);
1157 silc_buffer_free(packet);
1160 /* Function used to send REMOVE_ID packet. The packet is used to notify
1161 routers that certain ID should be removed. After that the ID will become
1162 invalid. If the argument `broadcast' is TRUE then the packet is sent as
1163 broadcast packet. */
1165 void silc_server_send_remove_id(SilcServer server,
1166 SilcSocketConnection sock,
1168 void *id, unsigned int id_len,
1173 SILC_LOG_DEBUG(("Start"));
1175 idp = silc_id_payload_encode(id, id_type);
1176 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_ID,
1177 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1178 idp->data, idp->len, FALSE);
1179 silc_buffer_free(idp);
1182 /* Function used to send SET_MODE packet. The packet is used to notify routers
1183 that channel's or client's channel mode was changed. If the argument
1184 `broadcast' is TRUE then the packet is sent as broadcast packet. */
1186 void silc_server_send_set_mode(SilcServer server,
1187 SilcSocketConnection sock,
1189 int mode_type, unsigned int mode_mask,
1190 unsigned int argc, ...)
1195 SILC_LOG_DEBUG(("Start"));
1199 /* Encode Set Mode payload */
1200 packet = silc_set_mode_payload_encode(mode_type, mode_mask, argc, ap);
1204 /* Send the packet */
1205 silc_server_packet_send(server, sock, SILC_PACKET_SET_MODE,
1206 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1207 packet->data, packet->len, FALSE);
1209 silc_buffer_free(packet);
1212 /* Send the heartbeat packet. */
1214 void silc_server_send_heartbeat(SilcServer server,
1215 SilcSocketConnection sock)
1217 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,