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 /* Internal routine to actually create the channel packet and send it
271 to network. This is common function in channel message sending. If
272 `channel_message' is TRUE this encrypts the message as it is strictly
273 a channel message. If FALSE normal encryption process is used. */
276 silc_server_packet_send_to_channel_real(SilcServer server,
277 SilcSocketConnection sock,
278 SilcPacketContext *packet,
282 unsigned int data_len,
286 packet->truelen = data_len + SILC_PACKET_HEADER_LEN +
287 packet->src_id_len + packet->dst_id_len;
289 /* Prepare outgoing data buffer for packet sending */
290 silc_packet_send_prepare(sock,
291 SILC_PACKET_HEADER_LEN +
297 packet->buffer = sock->outbuf;
299 /* Put the data to buffer, assemble and encrypt the packet. The packet
300 is encrypted with normal session key shared with the client. */
301 silc_buffer_put(sock->outbuf, data, data_len);
302 silc_packet_assemble(packet);
304 silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN +
305 packet->src_id_len + packet->dst_id_len +
308 silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
310 SILC_LOG_HEXDUMP(("Channel 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, force_send);
317 /* This routine is used by the server to send packets to channel. The
318 packet sent with this function is distributed to all clients on
319 the channel. Usually this is used to send notify messages to the
320 channel, things like notify about new user joining to the channel.
321 If `route' is FALSE then the packet is sent only locally and will not
322 be routed anywhere. */
324 void silc_server_packet_send_to_channel(SilcServer server,
325 SilcChannelEntry channel,
329 unsigned int data_len,
332 SilcSocketConnection sock = NULL;
333 SilcPacketContext packetdata;
334 SilcClientEntry client = NULL;
335 SilcServerEntry *routed = NULL;
336 SilcChannelClientEntry chl;
337 SilcIDListData idata;
338 unsigned int routed_count = 0;
340 /* This doesn't send channel message packets */
341 if (type == SILC_PACKET_CHANNEL_MESSAGE)
344 SILC_LOG_DEBUG(("Sending packet to channel"));
346 /* Set the packet context pointers. */
347 packetdata.flags = 0;
348 packetdata.type = type;
349 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
350 packetdata.src_id_len = SILC_ID_SERVER_LEN;
351 packetdata.src_id_type = SILC_ID_SERVER;
352 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
353 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
354 packetdata.dst_id_type = SILC_ID_CHANNEL;
355 packetdata.rng = server->rng;
356 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
357 packetdata.src_id_len + packetdata.dst_id_len;
358 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
360 /* If there are global users in the channel we will send the message
361 first to our router for further routing. */
362 if (route && server->server_type == SILC_SERVER && !server->standalone &&
363 channel->global_users) {
364 SilcServerEntry router;
366 /* Get data used in packet header encryption, keys and stuff. */
367 router = server->router;
368 sock = (SilcSocketConnection)router->connection;
369 idata = (SilcIDListData)router;
371 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
373 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
374 idata->send_key, idata->hmac,
375 data, data_len, FALSE, force_send);
378 /* Send the message to clients on the channel's client list. */
379 silc_list_start(channel->user_list);
380 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
381 client = chl->client;
383 /* If client has router set it is not locally connected client and
384 we will route the message to the router set in the client. */
385 if (route && client && client->router &&
386 server->server_type == SILC_ROUTER) {
389 /* Check if we have sent the packet to this route already */
390 for (k = 0; k < routed_count; k++)
391 if (routed[k] == client->router)
393 if (k < routed_count)
396 /* Get data used in packet header encryption, keys and stuff. */
397 sock = (SilcSocketConnection)client->router->connection;
398 idata = (SilcIDListData)client->router;
400 /* Send the packet */
401 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
402 idata->send_key, idata->hmac,
403 data, data_len, FALSE,
406 /* We want to make sure that the packet is routed to same router
407 only once. Mark this route as sent route. */
409 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
410 routed[k] = client->router;
416 /* Send to locally connected client */
419 /* Get data used in packet header encryption, keys and stuff. */
420 sock = (SilcSocketConnection)client->connection;
421 idata = (SilcIDListData)client;
423 /* Send the packet */
424 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
425 idata->send_key, idata->hmac,
426 data, data_len, FALSE,
433 silc_free(packetdata.src_id);
434 silc_free(packetdata.dst_id);
437 /* This routine is explicitly used to relay messages to some channel.
438 Packets sent with this function we have received earlier and are
439 totally encrypted. This just sends the packet to all clients on
440 the channel. If the sender of the packet is someone on the channel
441 the message will not be sent to that client. The SILC Packet header
442 is encrypted with the session key shared between us and the client.
443 MAC is also computed before encrypting the header. Rest of the
444 packet will be untouched. */
446 void silc_server_packet_relay_to_channel(SilcServer server,
447 SilcSocketConnection sender_sock,
448 SilcChannelEntry channel,
450 SilcIdType sender_type,
452 unsigned int data_len,
456 SilcSocketConnection sock = NULL;
457 SilcPacketContext packetdata;
458 SilcClientEntry client = NULL;
459 SilcServerEntry *routed = NULL;
460 SilcChannelClientEntry chl;
461 unsigned int routed_count = 0;
462 SilcIDListData idata;
464 SILC_LOG_DEBUG(("Relaying packet to channel"));
466 /* Set the packet context pointers. */
467 packetdata.flags = 0;
468 packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
469 packetdata.src_id = silc_id_id2str(sender, sender_type);
470 packetdata.src_id_len = silc_id_get_len(sender_type);
471 packetdata.src_id_type = sender_type;
472 packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
473 packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
474 packetdata.dst_id_type = SILC_ID_CHANNEL;
475 packetdata.rng = server->rng;
476 packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
477 packetdata.src_id_len +
478 packetdata.dst_id_len));
480 /* If there are global users in the channel we will send the message
481 first to our router for further routing. */
482 if (server->server_type == SILC_SERVER && !server->standalone &&
483 channel->global_users) {
484 SilcServerEntry router;
486 router = server->router;
488 /* Check that the sender is not our router. */
489 if (sender_sock != (SilcSocketConnection)router->connection) {
491 /* Get data used in packet header encryption, keys and stuff. */
492 sock = (SilcSocketConnection)router->connection;
493 idata = (SilcIDListData)router;
495 SILC_LOG_DEBUG(("Sending channel message to router for routing"));
497 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
498 idata->send_key, idata->hmac,
499 data, data_len, TRUE,
504 /* Send the message to clients on the channel's client list. */
505 silc_list_start(channel->user_list);
506 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
507 client = chl->client;
511 /* If sender is one on the channel do not send it the packet. */
512 if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
517 /* If the client has set router it means that it is not locally
518 connected client and we will route the packet further. */
519 if (server->server_type == SILC_ROUTER && client->router) {
522 /* Sender maybe server as well so we want to make sure that
523 we won't send the message to the server it came from. */
524 if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
529 /* Check if we have sent the packet to this route already */
530 for (k = 0; k < routed_count; k++)
531 if (routed[k] == client->router)
533 if (k < routed_count)
536 /* Get data used in packet header encryption, keys and stuff. */
537 sock = (SilcSocketConnection)client->router->connection;
538 idata = (SilcIDListData)client->router;
540 /* Send the packet */
541 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
542 idata->send_key, idata->hmac,
543 data, data_len, TRUE,
546 /* We want to make sure that the packet is routed to same router
547 only once. Mark this route as sent route. */
549 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
550 routed[k] = client->router;
556 /* XXX Check client's mode on the channel. */
558 /* Get data used in packet header encryption, keys and stuff. */
559 sock = (SilcSocketConnection)client->connection;
560 idata = (SilcIDListData)client;
562 SILC_LOG_DEBUG(("Sending packet to client %s",
563 sock->hostname ? sock->hostname : sock->ip));
565 /* Send the packet */
566 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
567 idata->send_key, idata->hmac,
568 data, data_len, TRUE,
573 silc_free(packetdata.src_id);
574 silc_free(packetdata.dst_id);
577 /* This function is used to send packets strictly to all local clients
578 on a particular channel. This is used for example to distribute new
579 channel key to all our locally connected clients on the channel.
580 The packets are always encrypted with the session key shared between
581 the client, this means these are not _to the channel_ but _to the client_
584 void silc_server_packet_send_local_channel(SilcServer server,
585 SilcChannelEntry channel,
587 SilcPacketFlags flags,
589 unsigned int data_len,
592 SilcClientEntry client;
593 SilcChannelClientEntry chl;
594 SilcSocketConnection sock = NULL;
596 SILC_LOG_DEBUG(("Start"));
598 /* Send the message to clients on the channel's client list. */
599 silc_list_start(channel->user_list);
600 while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
601 client = chl->client;
604 sock = (SilcSocketConnection)client->connection;
606 /* Send the packet to the client */
607 silc_server_packet_send_dest(server, sock, type, flags, client->id,
608 SILC_ID_CLIENT, data, data_len,
614 /* Routine used to send (relay, route) private messages to some destination.
615 If the private message key does not exist then the message is re-encrypted,
616 otherwise we just pass it along. This really is not used to send new
617 private messages (as server does not send them) but to relay received
620 void silc_server_send_private_message(SilcServer server,
621 SilcSocketConnection dst_sock,
624 SilcPacketContext *packet)
626 SilcBuffer buffer = packet->buffer;
628 /* Send and re-encrypt if private messge key does not exist */
629 if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
631 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
632 + packet->dst_id_len + packet->padlen);
633 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
634 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
636 /* Re-encrypt packet */
637 silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
639 /* Send the packet */
640 silc_server_packet_send_real(server, dst_sock, FALSE);
643 /* Key exist so just send it */
644 silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len
645 + packet->dst_id_len + packet->padlen);
646 silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
647 silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
648 silc_server_packet_send_real(server, dst_sock, FALSE);
652 /* Sends current motd to client */
654 void silc_server_send_motd(SilcServer server,
655 SilcSocketConnection sock)
660 if (server->config && server->config->motd &&
661 server->config->motd->motd_file) {
663 motd = silc_file_read(server->config->motd->motd_file, &motd_len);
667 silc_server_send_notify(server, sock, SILC_NOTIFY_TYPE_MOTD, 1,
673 /* Sends error message. Error messages may or may not have any
676 void silc_server_send_error(SilcServer server,
677 SilcSocketConnection sock,
678 const char *fmt, ...)
681 unsigned char buf[4096];
683 memset(buf, 0, sizeof(buf));
685 vsprintf(buf, fmt, ap);
688 silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0,
689 buf, strlen(buf), FALSE);
692 /* Sends notify message. If format is TRUE the variable arguments are
693 formatted and the formatted string is sent as argument payload. If it is
694 FALSE then each argument is sent as separate argument and their format
695 in the argument list must be { argument data, argument length }. */
697 void silc_server_send_notify(SilcServer server,
698 SilcSocketConnection sock,
700 unsigned int argc, ...)
707 packet = silc_notify_payload_encode(type, argc, ap);
708 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
709 packet->data, packet->len, FALSE);
710 silc_buffer_free(packet);
713 /* Sends notify message destined to specific entity. */
715 void silc_server_send_notify_dest(SilcServer server,
716 SilcSocketConnection sock,
718 SilcIdType dest_id_type,
720 unsigned int argc, ...)
727 packet = silc_notify_payload_encode(type, argc, ap);
728 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
729 dest_id, dest_id_type,
730 packet->data, packet->len, FALSE);
731 silc_buffer_free(packet);
734 /* Sends notify message to a channel. The notify message sent is
735 distributed to all clients on the channel. If `router_notify' is TRUE
736 then the notify may be routed to primary route or to some other routers.
737 If FALSE it is assured that the notify is sent only locally. */
739 void silc_server_send_notify_to_channel(SilcServer server,
740 SilcChannelEntry channel,
741 unsigned char route_notify,
743 unsigned int argc, ...)
750 packet = silc_notify_payload_encode(type, argc, ap);
751 silc_server_packet_send_to_channel(server, channel,
752 SILC_PACKET_NOTIFY, route_notify,
753 packet->data, packet->len, FALSE);
754 silc_buffer_free(packet);
757 /* Send notify message to all clients the client has joined. It is quaranteed
758 that the message is sent only once to a client (ie. if a client is joined
759 on two same channel it will receive only one notify message). Also, this
760 sends only to local clients (locally connected if we are server, and to
761 local servers if we are router). */
763 void silc_server_send_notify_on_channels(SilcServer server,
764 SilcClientEntry client,
766 unsigned int argc, ...)
769 SilcSocketConnection sock = NULL;
770 SilcPacketContext packetdata;
772 SilcClientEntry *sent_clients = NULL;
773 unsigned int sent_clients_count = 0;
774 SilcServerEntry *routed = NULL;
775 unsigned int routed_count = 0;
776 SilcChannelEntry channel;
777 SilcChannelClientEntry chl, chl2;
778 SilcIDListData idata;
781 unsigned int data_len;
782 int force_send = FALSE;
785 SILC_LOG_DEBUG(("Start"));
787 if (!silc_list_count(client->channels))
791 packet = silc_notify_payload_encode(type, argc, ap);
793 data_len = packet->len;
795 /* Set the packet context pointers. */
796 packetdata.flags = 0;
797 packetdata.type = SILC_PACKET_NOTIFY;
798 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
799 packetdata.src_id_len = SILC_ID_SERVER_LEN;
800 packetdata.src_id_type = SILC_ID_SERVER;
801 packetdata.rng = server->rng;
803 silc_list_start(client->channels);
804 while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
805 channel = chl->channel;
807 /* Send the message to all clients on the channel's client list. */
808 silc_list_start(channel->user_list);
809 while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
812 /* Check if we have sent the packet to this client already */
813 for (k = 0; k < sent_clients_count; k++)
814 if (sent_clients[k] == c)
816 if (k < sent_clients_count)
819 /* If we are router and if this client has router set it is not
820 locally connected client and we will route the message to the
821 router set in the client. */
822 if (c && c->router && server->server_type == SILC_ROUTER) {
823 /* Check if we have sent the packet to this route already */
824 for (k = 0; k < routed_count; k++)
825 if (routed[k] == c->router)
827 if (k < routed_count)
830 /* Get data used in packet header encryption, keys and stuff. */
831 sock = (SilcSocketConnection)c->router->connection;
832 idata = (SilcIDListData)c->router;
834 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
835 packetdata.dst_id_len = SILC_ID_SERVER_LEN;
836 packetdata.dst_id_type = SILC_ID_SERVER;
837 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
838 packetdata.src_id_len + packetdata.dst_id_len;
839 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
841 /* Send the packet */
842 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
843 idata->send_key, idata->hmac,
844 data, data_len, FALSE,
847 silc_free(packetdata.dst_id);
849 /* We want to make sure that the packet is routed to same router
850 only once. Mark this route as sent route. */
852 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
853 routed[k] = c->router;
859 /* Send to locally connected client */
862 /* Get data used in packet header encryption, keys and stuff. */
863 sock = (SilcSocketConnection)c->connection;
864 idata = (SilcIDListData)c;
866 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
867 packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
868 packetdata.dst_id_type = SILC_ID_CLIENT;
869 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
870 packetdata.src_id_len + packetdata.dst_id_len;
871 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
873 /* Send the packet */
874 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
875 idata->send_key, idata->hmac,
876 data, data_len, FALSE,
879 silc_free(packetdata.dst_id);
881 /* Make sure that we send the notify only once per client. */
882 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
883 (sent_clients_count + 1));
884 sent_clients[sent_clients_count] = c;
885 sent_clients_count++;
892 if (sent_clients_count)
893 silc_free(sent_clients);
894 silc_free(packetdata.src_id);
897 /* Sends New ID Payload to remote end. The packet is used to distribute
898 information about new registered clients, servers, channel etc. usually
899 to routers so that they can keep these information up to date.
900 If the argument `broadcast' is TRUE then the packet is sent as
903 void silc_server_send_new_id(SilcServer server,
904 SilcSocketConnection sock,
906 void *id, SilcIdType id_type,
911 SILC_LOG_DEBUG(("Start"));
913 idp = silc_id_payload_encode(id, id_type);
914 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
915 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
916 idp->data, idp->len, FALSE);
917 silc_buffer_free(idp);
920 /* Sends Replace ID payload to remote end. This is used to replace old
921 ID with new ID sent in the packet. This is called for example when
922 user changes nickname and we create new ID for the user. If the
923 argument `broadcast' is TRUE then the packet is sent as
925 /* XXX It would be expected that the new id is same type as the old
928 void silc_server_send_replace_id(SilcServer server,
929 SilcSocketConnection sock,
931 void *old_id, SilcIdType old_id_type,
932 unsigned int old_id_len,
933 void *new_id, SilcIdType new_id_type,
934 unsigned int new_id_len)
940 SILC_LOG_DEBUG(("Start"));
942 oid = silc_id_id2str(old_id, old_id_type);
946 nid = silc_id_id2str(new_id, new_id_type);
950 packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
951 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
952 silc_buffer_format(packet,
953 SILC_STR_UI_SHORT(old_id_type),
954 SILC_STR_UI_SHORT(old_id_len),
955 SILC_STR_UI_XNSTRING(oid, old_id_len),
956 SILC_STR_UI_SHORT(new_id_type),
957 SILC_STR_UI_SHORT(new_id_len),
958 SILC_STR_UI_XNSTRING(nid, new_id_len),
961 silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID,
962 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
963 packet->data, packet->len, FALSE);
966 silc_buffer_free(packet);
969 /* This function is used to send Remove Channel User payload. This may sent
970 by server but is usually used only by router to notify other routers that
971 user has left a channel. Normal server sends this packet to its router
972 to notify that the router should not hold a record about this client
973 on a channel anymore. Router distributes it further to other routers. */
975 void silc_server_send_remove_channel_user(SilcServer server,
976 SilcSocketConnection sock,
978 void *client_id, void *channel_id)
981 unsigned char *clid, *chid;
983 SILC_LOG_DEBUG(("Start"));
985 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
989 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
993 packet = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN + SILC_ID_CHANNEL_LEN);
994 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
995 silc_buffer_format(packet,
996 SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
997 SILC_STR_UI_XNSTRING(clid, SILC_ID_CLIENT_LEN),
998 SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
999 SILC_STR_UI_XNSTRING(chid, SILC_ID_CHANNEL_LEN),
1002 silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_CHANNEL_USER,
1003 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1004 packet->data, packet->len, FALSE);
1007 silc_buffer_free(packet);
1010 /* Send New Channel Payload to notify about newly created channel in the
1011 SILC network. Normal server nevers sends this packet. Router uses this
1012 to notify other routers in the network about new channel. This packet
1015 void silc_server_send_new_channel(SilcServer server,
1016 SilcSocketConnection sock,
1020 unsigned int channel_id_len)
1024 unsigned int name_len = strlen(channel_name);
1026 SILC_LOG_DEBUG(("Start"));
1028 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1032 packet = silc_buffer_alloc(2 + 2 + name_len + channel_id_len);
1033 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1034 silc_buffer_format(packet,
1035 SILC_STR_UI_SHORT(name_len),
1036 SILC_STR_UI_XNSTRING(channel_name, name_len),
1037 SILC_STR_UI_SHORT(channel_id_len),
1038 SILC_STR_UI_XNSTRING(cid, channel_id_len),
1041 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1042 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1043 packet->data, packet->len, FALSE);
1046 silc_buffer_free(packet);
1049 /* Send New Channel User payload to notify routers in the network about new
1050 user on the channel. The packet is may be broadcasted. Normal server
1051 can send this but must not receive. Router can send and receive it. */
1053 void silc_server_send_new_channel_user(SilcServer server,
1054 SilcSocketConnection sock,
1057 unsigned int channel_id_len,
1059 unsigned int client_id_len)
1062 unsigned char *clid, *chid;
1064 SILC_LOG_DEBUG(("Start"));
1066 clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1070 chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1074 packet = silc_buffer_alloc(2 + 2 + channel_id_len + client_id_len);
1075 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1076 silc_buffer_format(packet,
1077 SILC_STR_UI_SHORT(channel_id_len),
1078 SILC_STR_UI_XNSTRING(chid, channel_id_len),
1079 SILC_STR_UI_SHORT(client_id_len),
1080 SILC_STR_UI_XNSTRING(clid, client_id_len),
1083 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL_USER,
1084 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1085 packet->data, packet->len, FALSE);
1088 silc_buffer_free(packet);