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, FALSE, 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,
707 unsigned int argc, ...)
714 packet = silc_notify_payload_encode(type, argc, ap);
715 silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0,
716 packet->data, packet->len, FALSE);
717 silc_buffer_free(packet);
720 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
721 `old_id' with the `new_id'. */
723 void silc_server_send_notify_channel_change(SilcServer server,
724 SilcSocketConnection sock,
726 SilcChannelID *old_id,
727 SilcChannelID *new_id,
730 SilcBuffer idp1, idp2;
732 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
733 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
735 silc_server_send_notify(server, sock, broadcast,
736 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
737 2, idp1->data, idp1->len, idp2->data, idp2->len);
738 silc_buffer_free(idp1);
739 silc_buffer_free(idp2);
742 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
743 `old_id' with the `new_id'. */
745 void silc_server_send_notify_nick_change(SilcServer server,
746 SilcSocketConnection sock,
748 SilcClientID *old_id,
749 SilcClientID *new_id,
752 SilcBuffer idp1, idp2;
754 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
755 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
757 silc_server_send_notify(server, sock, broadcast,
758 SILC_NOTIFY_TYPE_NICK_CHANGE,
759 2, idp1->data, idp1->len, idp2->data, idp2->len);
760 silc_buffer_free(idp1);
761 silc_buffer_free(idp2);
764 /* Sends JOIN notify type. This tells that new client by `client_id' ID
765 has joined to the `channel'. */
767 void silc_server_send_notify_join(SilcServer server,
768 SilcSocketConnection sock,
770 SilcChannelEntry channel,
771 SilcClientID *client_id,
772 unsigned int client_id_len)
774 SilcBuffer idp1, idp2;
776 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
777 idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
778 silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
779 2, idp1->data, idp1->len,
780 idp2->data, idp2->len);
781 silc_buffer_free(idp1);
782 silc_buffer_free(idp2);
785 /* Sends LEAVE notify type. This tells that `client_id' has left the
786 `channel'. The Notify packet is always destined to the channel. */
788 void silc_server_send_notify_leave(SilcServer server,
789 SilcSocketConnection sock,
791 SilcChannelEntry channel,
792 SilcClientID *client_id,
793 unsigned int client_id_len)
797 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
798 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
799 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
800 1, idp->data, idp->len);
801 silc_buffer_free(idp);
804 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
805 `channel' mode to `mode. The Notify packet is always destined to
808 void silc_server_send_notify_cmode(SilcServer server,
809 SilcSocketConnection sock,
811 SilcChannelEntry channel,
812 unsigned int mode_mask,
813 SilcClientID *client_id,
814 unsigned int client_id_len)
817 unsigned char mode[4];
819 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
820 SILC_PUT32_MSB(mode_mask, mode);
822 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
823 SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
824 2, idp->data, idp->len,
826 silc_buffer_free(idp);
829 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
830 `target' client's mode on `channel'. The Notify packet is always
831 destined to the channel. */
833 void silc_server_send_notify_cumode(SilcServer server,
834 SilcSocketConnection sock,
836 SilcChannelEntry channel,
837 unsigned int mode_mask,
838 SilcClientID *client_id,
839 unsigned int client_id_len,
840 SilcClientID *target,
841 unsigned int target_len)
843 SilcBuffer idp1, idp2;
844 unsigned char mode[4];
846 idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
847 idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
848 SILC_PUT32_MSB(mode_mask, mode);
850 silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
852 SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3,
853 idp1->data, idp1->len,
855 idp2->data, idp2->len);
856 silc_buffer_free(idp1);
857 silc_buffer_free(idp2);
860 /* Sends SIGNOFF notify type. This tells that `client_id' client has
861 left SILC network. This function is used only between server and router
862 traffic. This is not used to send the notify to the channel for
863 client. The `message may be NULL. */
865 void silc_server_send_notify_signoff(SilcServer server,
866 SilcSocketConnection sock,
868 SilcClientID *client_id,
869 unsigned int client_id_len,
874 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
875 silc_server_send_notify(server, sock, broadcast,
876 SILC_NOTIFY_TYPE_SIGNOFF,
877 message ? 2 : 1, idp->data, idp->len,
878 message, message ? strlen(message): 0);
879 silc_buffer_free(idp);
882 /* Sends SERVER_SIGNOFF notify type. This tells that `server_id' server
883 has quit SILC network. */
885 void silc_server_send_notify_server_signoff(SilcServer server,
886 SilcSocketConnection sock,
888 SilcServerID *server_id,
889 unsigned int server_id_len)
893 idp = silc_id_payload_encode((void *)server_id, SILC_ID_SERVER);
894 silc_server_send_notify(server, sock, broadcast,
895 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
896 1, idp->data, idp->len);
897 silc_buffer_free(idp);
900 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
901 the `channel's topic to `topic'. The Notify packet is always destined
902 to the channel. This function is used to send the topic set notifies
905 void silc_server_send_notify_topic_set(SilcServer server,
906 SilcSocketConnection sock,
908 SilcChannelEntry channel,
909 SilcClientID *client_id,
910 unsigned int client_id_len,
915 idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
916 silc_server_send_notify(server, sock, broadcast,
917 SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
920 topic, topic ? strlen(topic) : 0);
921 silc_buffer_free(idp);
924 /* Sends notify message destined to specific entity. */
926 void silc_server_send_notify_dest(SilcServer server,
927 SilcSocketConnection sock,
930 SilcIdType dest_id_type,
932 unsigned int argc, ...)
939 packet = silc_notify_payload_encode(type, argc, ap);
940 silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0,
941 dest_id, dest_id_type,
942 packet->data, packet->len, FALSE);
943 silc_buffer_free(packet);
946 /* Sends notify message to a channel. The notify message sent is
947 distributed to all clients on the channel. If `router_notify' is TRUE
948 then the notify may be routed to primary route or to some other routers.
949 If FALSE it is assured that the notify is sent only locally. If `sender'
950 is provided then the packet is not sent to that connection since it
951 originally came from it. */
953 void silc_server_send_notify_to_channel(SilcServer server,
954 SilcSocketConnection sender,
955 SilcChannelEntry channel,
956 unsigned char route_notify,
958 unsigned int argc, ...)
965 packet = silc_notify_payload_encode(type, argc, ap);
966 silc_server_packet_send_to_channel(server, sender, channel,
967 SILC_PACKET_NOTIFY, route_notify,
968 packet->data, packet->len, FALSE);
969 silc_buffer_free(packet);
972 /* Send notify message to all clients the client has joined. It is quaranteed
973 that the message is sent only once to a client (ie. if a client is joined
974 on two same channel it will receive only one notify message). Also, this
975 sends only to local clients (locally connected if we are server, and to
976 local servers if we are router). */
978 void silc_server_send_notify_on_channels(SilcServer server,
979 SilcClientEntry client,
981 unsigned int argc, ...)
984 SilcSocketConnection sock = NULL;
985 SilcPacketContext packetdata;
987 SilcClientEntry *sent_clients = NULL;
988 unsigned int sent_clients_count = 0;
989 SilcServerEntry *routed = NULL;
990 unsigned int routed_count = 0;
991 SilcChannelEntry channel;
992 SilcChannelClientEntry chl, chl2;
993 SilcIDListData idata;
996 unsigned int data_len;
997 int force_send = FALSE;
1000 SILC_LOG_DEBUG(("Start"));
1002 if (!silc_list_count(client->channels))
1006 packet = silc_notify_payload_encode(type, argc, ap);
1007 data = packet->data;
1008 data_len = packet->len;
1010 /* Set the packet context pointers. */
1011 packetdata.flags = 0;
1012 packetdata.type = SILC_PACKET_NOTIFY;
1013 packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1014 packetdata.src_id_len = SILC_ID_SERVER_LEN;
1015 packetdata.src_id_type = SILC_ID_SERVER;
1016 packetdata.rng = server->rng;
1018 silc_list_start(client->channels);
1019 while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
1020 channel = chl->channel;
1022 /* Send the message to all clients on the channel's client list. */
1023 silc_list_start(channel->user_list);
1024 while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
1027 /* Check if we have sent the packet to this client already */
1028 for (k = 0; k < sent_clients_count; k++)
1029 if (sent_clients[k] == c)
1031 if (k < sent_clients_count)
1034 /* If we are router and if this client has router set it is not
1035 locally connected client and we will route the message to the
1036 router set in the client. */
1037 if (c && c->router && server->server_type == SILC_ROUTER) {
1038 /* Check if we have sent the packet to this route already */
1039 for (k = 0; k < routed_count; k++)
1040 if (routed[k] == c->router)
1042 if (k < routed_count)
1045 /* Get data used in packet header encryption, keys and stuff. */
1046 sock = (SilcSocketConnection)c->router->connection;
1047 idata = (SilcIDListData)c->router;
1049 packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1050 packetdata.dst_id_len = SILC_ID_SERVER_LEN;
1051 packetdata.dst_id_type = SILC_ID_SERVER;
1052 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1053 packetdata.src_id_len + packetdata.dst_id_len;
1054 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1056 /* Send the packet */
1057 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1058 idata->send_key, idata->hmac,
1059 data, data_len, FALSE,
1062 silc_free(packetdata.dst_id);
1064 /* We want to make sure that the packet is routed to same router
1065 only once. Mark this route as sent route. */
1067 routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1068 routed[k] = c->router;
1077 /* Send to locally connected client */
1080 /* Get data used in packet header encryption, keys and stuff. */
1081 sock = (SilcSocketConnection)c->connection;
1082 idata = (SilcIDListData)c;
1084 packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1085 packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
1086 packetdata.dst_id_type = SILC_ID_CLIENT;
1087 packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN +
1088 packetdata.src_id_len + packetdata.dst_id_len;
1089 packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1091 /* Send the packet */
1092 silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1093 idata->send_key, idata->hmac,
1094 data, data_len, FALSE,
1097 silc_free(packetdata.dst_id);
1099 /* Make sure that we send the notify only once per client. */
1100 sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) *
1101 (sent_clients_count + 1));
1102 sent_clients[sent_clients_count] = c;
1103 sent_clients_count++;
1110 if (sent_clients_count)
1111 silc_free(sent_clients);
1112 silc_free(packetdata.src_id);
1115 /* Sends New ID Payload to remote end. The packet is used to distribute
1116 information about new registered clients, servers, channel etc. usually
1117 to routers so that they can keep these information up to date.
1118 If the argument `broadcast' is TRUE then the packet is sent as
1119 broadcast packet. */
1121 void silc_server_send_new_id(SilcServer server,
1122 SilcSocketConnection sock,
1124 void *id, SilcIdType id_type,
1125 unsigned int id_len)
1129 SILC_LOG_DEBUG(("Start"));
1131 idp = silc_id_payload_encode(id, id_type);
1132 silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID,
1133 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1134 idp->data, idp->len, FALSE);
1135 silc_buffer_free(idp);
1138 /* Send New Channel Payload to notify about newly created channel in the
1139 SILC network. Normal server nevers sends this packet. Router uses this
1140 to notify other routers in the network about new channel. This packet
1143 void silc_server_send_new_channel(SilcServer server,
1144 SilcSocketConnection sock,
1148 unsigned int channel_id_len)
1152 unsigned int name_len = strlen(channel_name);
1154 SILC_LOG_DEBUG(("Start"));
1156 cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1160 packet = silc_buffer_alloc(2 + 2 + name_len + channel_id_len);
1161 silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1162 silc_buffer_format(packet,
1163 SILC_STR_UI_SHORT(name_len),
1164 SILC_STR_UI_XNSTRING(channel_name, name_len),
1165 SILC_STR_UI_SHORT(channel_id_len),
1166 SILC_STR_UI_XNSTRING(cid, channel_id_len),
1170 silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL,
1171 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1172 packet->data, packet->len, FALSE);
1175 silc_buffer_free(packet);
1178 /* Send Channel Key payload to distribute the new channel key. Normal server
1179 sends this to router when new client joins to existing channel. Router
1180 sends this to the local server who sent the join command in case where
1181 the channel did not exist yet. Both normal and router servers uses this
1182 also to send this to locally connected clients on the channel. This
1183 must not be broadcasted packet. Routers do not send this to each other.
1184 If `sender is provided then the packet is not sent to that connection since
1185 it originally came from it. */
1187 void silc_server_send_channel_key(SilcServer server,
1188 SilcSocketConnection sender,
1189 SilcChannelEntry channel,
1190 unsigned char route)
1193 unsigned char *chid;
1194 unsigned int tmp_len;
1196 SILC_LOG_DEBUG(("Start"));
1198 chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1202 /* Encode channel key packet */
1203 tmp_len = strlen(channel->channel_key->cipher->name);
1204 packet = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, chid, tmp_len,
1205 channel->channel_key->cipher->name,
1206 channel->key_len / 8, channel->key);
1208 silc_server_packet_send_to_channel(server, sender, channel,
1209 SILC_PACKET_CHANNEL_KEY,
1210 route, packet->data, packet->len, FALSE);
1211 silc_buffer_free(packet);
1215 /* Generic function to send any command. The arguments must be sent already
1216 encoded into correct form in correct order. */
1218 void silc_server_send_command(SilcServer server,
1219 SilcSocketConnection sock,
1220 SilcCommand command,
1221 unsigned int argc, ...)
1228 packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1229 silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1230 packet->data, packet->len, TRUE);
1231 silc_buffer_free(packet);
1234 /* Send the heartbeat packet. */
1236 void silc_server_send_heartbeat(SilcServer server,
1237 SilcSocketConnection sock)
1239 silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,