updates.
[silc.git] / apps / silcd / packet_send.c
1 /*
2
3   packet_send.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8
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.
13   
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.
18
19 */
20 /*
21  * Server packet routines to send packets. 
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
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. */
31
32 int silc_server_packet_send_real(SilcServer server,
33                                  SilcSocketConnection sock,
34                                  int force_send)
35 {
36   int ret;
37
38   /* Send the packet */
39   ret = silc_packet_send(sock, force_send);
40   if (ret != -2)
41     return ret;
42
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);
48
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);
53
54   return 0;
55 }
56
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. */
62
63 void silc_server_packet_send(SilcServer server,
64                              SilcSocketConnection sock, 
65                              SilcPacketType type, 
66                              SilcPacketFlags flags,
67                              unsigned char *data, 
68                              unsigned int data_len,
69                              int force_send)
70 {
71   void *dst_id = NULL;
72   SilcIdType dst_id_type = SILC_ID_NONE;
73
74   if (!sock)
75     return;
76
77   /* Get data used in the packet sending, keys and stuff */
78   switch(sock->type) {
79   case SILC_SOCKET_TYPE_CLIENT:
80     dst_id = ((SilcClientEntry)sock->user_data)->id;
81     dst_id_type = SILC_ID_CLIENT;
82     break;
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;
87     break;
88   default:
89     break;
90   }
91
92   silc_server_packet_send_dest(server, sock, type, flags, dst_id,
93                                dst_id_type, data, data_len, force_send);
94 }
95
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. */
102
103 void silc_server_packet_send_dest(SilcServer server,
104                                   SilcSocketConnection sock, 
105                                   SilcPacketType type, 
106                                   SilcPacketFlags flags,
107                                   void *dst_id,
108                                   SilcIdType dst_id_type,
109                                   unsigned char *data, 
110                                   unsigned int data_len,
111                                   int force_send)
112 {
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;
119
120   SILC_LOG_DEBUG(("Sending packet, type %d", type));
121
122   /* Get data used in the packet sending, keys and stuff */
123   idata = (SilcIDListData)sock->user_data;
124
125   if (dst_id) {
126     dst_id_data = silc_id_id2str(dst_id, dst_id_type);
127     dst_id_len = silc_id_get_len(dst_id_type);
128   }
129
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;
143
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,
149                            packetdata.padlen,
150                            data_len);
151
152   SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
153
154   packetdata.buffer = sock->outbuf;
155
156   /* Put the data to the buffer */
157   if (data && data_len)
158     silc_buffer_put(sock->outbuf, data, data_len);
159
160   /* Create the outgoing packet */
161   silc_packet_assemble(&packetdata);
162
163   if (idata) {
164     cipher = idata->send_key;
165     hmac = idata->hmac;
166   }
167
168   /* Encrypt the packet */
169   silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
170
171   SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
172                    sock->outbuf->data, sock->outbuf->len);
173
174   /* Now actually send the packet */
175   silc_server_packet_send_real(server, sock, force_send);
176
177   if (packetdata.src_id)
178     silc_free(packetdata.src_id);
179   if (packetdata.dst_id)
180     silc_free(packetdata.dst_id);
181 }
182
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. */
187
188 void silc_server_packet_broadcast(SilcServer server,
189                                   SilcSocketConnection sock,
190                                   SilcPacketContext *packet)
191 {
192   SilcBuffer buffer = packet->buffer;
193   SilcIDListData idata;
194   void *id;
195
196   SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
197
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;
203
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);
209
210     SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
211                      sock->outbuf->data, sock->outbuf->len);
212
213     /* Now actually send the packet */
214     silc_server_packet_send_real(server, sock, TRUE);
215     silc_free(id);
216     return;
217   }
218
219   SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
220                   "original sender of this packet"));
221   silc_free(id);
222 }
223
224 /* Routes received packet to `sock'. This is used to route the packets that
225    router receives but are not destined to it. */
226
227 void silc_server_packet_route(SilcServer server,
228                               SilcSocketConnection sock,
229                               SilcPacketContext *packet)
230 {
231   SilcBuffer buffer = packet->buffer;
232   SilcIDListData idata;
233
234   SILC_LOG_DEBUG(("Routing received packet"));
235
236   idata = (SilcIDListData)sock->user_data;
237
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);
243
244   SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
245                    sock->outbuf->data, sock->outbuf->len);
246
247   /* Now actually send the packet */
248   silc_server_packet_send_real(server, sock, TRUE);
249 }
250
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. */
255
256 static void
257 silc_server_packet_send_to_channel_real(SilcServer server,
258                                         SilcSocketConnection sock,
259                                         SilcPacketContext *packet,
260                                         SilcCipher cipher,
261                                         SilcHmac hmac,
262                                         unsigned char *data,
263                                         unsigned int data_len,
264                                         int channel_message,
265                                         int force_send)
266 {
267   packet->truelen = data_len + SILC_PACKET_HEADER_LEN + 
268     packet->src_id_len + packet->dst_id_len;
269
270   /* Prepare outgoing data buffer for packet sending */
271   silc_packet_send_prepare(sock, 
272                            SILC_PACKET_HEADER_LEN +
273                            packet->src_id_len + 
274                            packet->dst_id_len,
275                            packet->padlen,
276                            data_len);
277
278   packet->buffer = sock->outbuf;
279
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);
284   if (channel_message)
285     silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN + 
286                         packet->src_id_len + packet->dst_id_len +
287                         packet->padlen);
288   else
289     silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
290     
291   SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
292                    sock->outbuf->data, sock->outbuf->len);
293
294   /* Now actually send the packet */
295   silc_server_packet_send_real(server, sock, force_send);
296 }
297
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. */
306
307 void silc_server_packet_send_to_channel(SilcServer server,
308                                         SilcSocketConnection sender,
309                                         SilcChannelEntry channel,
310                                         SilcPacketType type,
311                                         unsigned char route,
312                                         unsigned char *data,
313                                         unsigned int data_len,
314                                         int force_send)
315 {
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;
323
324   /* This doesn't send channel message packets */
325   if (type == SILC_PACKET_CHANNEL_MESSAGE)
326     return;
327   
328   SILC_LOG_DEBUG(("Sending packet to channel"));
329
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);
343
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;
349
350     /* Get data used in packet header encryption, keys and stuff. */
351     router = server->router;
352     sock = (SilcSocketConnection)router->connection;
353     idata = (SilcIDListData)router;
354     
355     if (sock != sender) {
356       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
357       
358       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
359                                               idata->send_key, idata->hmac, 
360                                               data, data_len, FALSE, 
361                                               force_send);
362     }
363   }
364
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;
369
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)) {
375       int k;
376
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)
380           break;
381       if (k < routed_count)
382         continue;
383
384       /* Get data used in packet header encryption, keys and stuff. */
385       sock = (SilcSocketConnection)client->router->connection;
386       idata = (SilcIDListData)client->router;
387
388       if (sender && sock == sender)
389         continue;
390
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, 
395                                               force_send);
396
397       /* We want to make sure that the packet is routed to same router
398          only once. Mark this route as sent route. */
399       k = routed_count;
400       routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
401       routed[k] = client->router;
402       routed_count++;
403
404       continue;
405     }
406
407     if (client && client->router)
408       continue;
409
410     /* Send to locally connected client */
411     if (client) {
412
413       /* Get data used in packet header encryption, keys and stuff. */
414       sock = (SilcSocketConnection)client->connection;
415       idata = (SilcIDListData)client;
416
417       if (sender && sock == sender)
418         continue;
419
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, 
424                                               force_send);
425     }
426   }
427
428   if (routed_count)
429     silc_free(routed);
430   silc_free(packetdata.src_id);
431   silc_free(packetdata.dst_id);
432 }
433
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. */
442
443 void silc_server_packet_relay_to_channel(SilcServer server,
444                                          SilcSocketConnection sender_sock,
445                                          SilcChannelEntry channel,
446                                          void *sender, 
447                                          SilcIdType sender_type,
448                                          unsigned char *data,
449                                          unsigned int data_len,
450                                          int force_send)
451 {
452   int found = FALSE;
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;
460
461   SILC_LOG_DEBUG(("Relaying packet to channel"));
462
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));
476
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;
482
483     router = server->router;
484
485     /* Check that the sender is not our router. */
486     if (sender_sock != (SilcSocketConnection)router->connection) {
487
488       /* Get data used in packet header encryption, keys and stuff. */
489       sock = (SilcSocketConnection)router->connection;
490       idata = (SilcIDListData)router;
491
492       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
493
494       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
495                                               idata->send_key, idata->hmac, 
496                                               data, data_len, TRUE, 
497                                               force_send);
498     }
499   }
500
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;
505
506     if (client) {
507
508       /* If sender is one on the channel do not send it the packet. */
509       if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
510         found = TRUE;
511         continue;
512       }
513
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) {
517         int k;
518
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)) {
522           found = TRUE;
523           continue;
524         }
525
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)
529             break;
530         if (k < routed_count)
531           continue;
532         
533         /* Get data used in packet header encryption, keys and stuff. */
534         sock = (SilcSocketConnection)client->router->connection;
535         idata = (SilcIDListData)client->router;
536
537         if (sender_sock && sock == sender_sock)
538           continue;
539
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));
543
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, 
548                                                 force_send);
549         
550         /* We want to make sure that the packet is routed to same router
551            only once. Mark this route as sent route. */
552         k = routed_count;
553         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
554         routed[k] = client->router;
555         routed_count++;
556         
557         continue;
558       }
559
560       if (client && client->router)
561         continue;
562
563       /* Get data used in packet header encryption, keys and stuff. */
564       sock = (SilcSocketConnection)client->connection;
565       idata = (SilcIDListData)client;
566
567       if (sender_sock && sock == sender_sock)
568         continue;
569
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));
573
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, 
578                                               force_send);
579     }
580   }
581
582   silc_free(packetdata.src_id);
583   silc_free(packetdata.dst_id);
584 }
585
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_
591    on the channel. */
592
593 void silc_server_packet_send_local_channel(SilcServer server,
594                                            SilcChannelEntry channel,
595                                            SilcPacketType type,
596                                            SilcPacketFlags flags,
597                                            unsigned char *data,
598                                            unsigned int data_len,
599                                            int force_send)
600 {
601   SilcChannelClientEntry chl;
602   SilcSocketConnection sock = NULL;
603
604   SILC_LOG_DEBUG(("Start"));
605
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;
611
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,
615                                    force_send);
616     }
617   }
618 }
619
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
624    private messages. */
625
626 void silc_server_send_private_message(SilcServer server,
627                                       SilcSocketConnection dst_sock,
628                                       SilcCipher cipher,
629                                       SilcHmac hmac,
630                                       SilcPacketContext *packet)
631 {
632   SilcBuffer buffer = packet->buffer;
633
634   /* Send and re-encrypt if private messge key does not exist */
635   if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
636
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);
641     
642     /* Re-encrypt packet */
643     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
644     
645     /* Send the packet */
646     silc_server_packet_send_real(server, dst_sock, FALSE);
647
648   } else {
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);
655   }
656 }
657
658 /* Sends current motd to client */
659
660 void silc_server_send_motd(SilcServer server,
661                            SilcSocketConnection sock)
662 {
663   char *motd;
664   int motd_len;
665
666   if (server->config && server->config->motd && 
667       server->config->motd->motd_file) {
668
669     motd = silc_file_read(server->config->motd->motd_file, &motd_len);
670     if (!motd)
671       return;
672
673     silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
674                             motd, motd_len);
675     silc_free(motd);
676   }
677 }
678
679 /* Sends error message. Error messages may or may not have any 
680    implications. */
681
682 void silc_server_send_error(SilcServer server,
683                             SilcSocketConnection sock,
684                             const char *fmt, ...)
685 {
686   va_list ap;
687   unsigned char buf[4096];
688
689   memset(buf, 0, sizeof(buf));
690   va_start(ap, fmt);
691   vsprintf(buf, fmt, ap);
692   va_end(ap);
693
694   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
695                           buf, strlen(buf), FALSE);
696 }
697
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 }. */
702
703 void silc_server_send_notify(SilcServer server,
704                              SilcSocketConnection sock,
705                              int broadcast,
706                              SilcNotifyType type,
707                              unsigned int argc, ...)
708 {
709   va_list ap;
710   SilcBuffer packet;
711
712   va_start(ap, argc);
713
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);
718 }
719
720 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
721    `old_id' with the `new_id'. */
722
723 void silc_server_send_notify_channel_change(SilcServer server,
724                                             SilcSocketConnection sock,
725                                             int broadcast,
726                                             SilcChannelID *old_id,
727                                             SilcChannelID *new_id,
728                                             unsigned int id_len)
729 {
730   SilcBuffer idp1, idp2;
731
732   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
733   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
734
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);
740 }
741
742 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
743    `old_id' with the `new_id'. */
744
745 void silc_server_send_notify_nick_change(SilcServer server,
746                                          SilcSocketConnection sock,
747                                          int broadcast,
748                                          SilcClientID *old_id,
749                                          SilcClientID *new_id,
750                                          unsigned int id_len)
751 {
752   SilcBuffer idp1, idp2;
753
754   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
755   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
756
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);
762 }
763
764 /* Sends JOIN notify type. This tells that new client by `client_id' ID
765    has joined to the `channel'. */
766
767 void silc_server_send_notify_join(SilcServer server,
768                                   SilcSocketConnection sock,
769                                   int broadcast,
770                                   SilcChannelEntry channel,
771                                   SilcClientID *client_id,
772                                   unsigned int client_id_len)
773 {
774   SilcBuffer idp1, idp2;
775
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);
783 }
784
785 /* Sends LEAVE notify type. This tells that `client_id' has left the
786    `channel'. The Notify packet is always destined to the channel. */
787
788 void silc_server_send_notify_leave(SilcServer server,
789                                    SilcSocketConnection sock,
790                                    int broadcast,
791                                    SilcChannelEntry channel,
792                                    SilcClientID *client_id,
793                                    unsigned int client_id_len)
794 {
795   SilcBuffer idp;
796
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);
802 }
803
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
806    the channel. */
807
808 void silc_server_send_notify_cmode(SilcServer server,
809                                    SilcSocketConnection sock,
810                                    int broadcast,
811                                    SilcChannelEntry channel,
812                                    unsigned int mode_mask,
813                                    SilcClientID *client_id,
814                                    unsigned int client_id_len)
815 {
816   SilcBuffer idp;
817   unsigned char mode[4];
818
819   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
820   SILC_PUT32_MSB(mode_mask, mode);
821
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,
825                                mode, 4);
826   silc_buffer_free(idp);
827 }
828
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. */
832
833 void silc_server_send_notify_cumode(SilcServer server,
834                                     SilcSocketConnection sock,
835                                     int broadcast,
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)
842 {
843   SilcBuffer idp1, idp2;
844   unsigned char mode[4];
845
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);
849
850   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
851                                SILC_ID_CHANNEL, 
852                                SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3, 
853                                idp1->data, idp1->len,
854                                mode, 4,
855                                idp2->data, idp2->len);
856   silc_buffer_free(idp1);
857   silc_buffer_free(idp2);
858 }
859
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. */
864
865 void silc_server_send_notify_signoff(SilcServer server,
866                                      SilcSocketConnection sock,
867                                      int broadcast,
868                                      SilcClientID *client_id,
869                                      unsigned int client_id_len,
870                                      char *message)
871 {
872   SilcBuffer idp;
873
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);
880 }
881
882 /* Sends SERVER_SIGNOFF notify type. This tells that `server_id' server
883    has quit SILC network. */
884
885 void silc_server_send_notify_server_signoff(SilcServer server,
886                                             SilcSocketConnection sock,
887                                             int broadcast,
888                                             SilcServerID *server_id,
889                                             unsigned int server_id_len)
890 {
891   SilcBuffer idp;
892
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);
898 }
899
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
903    between routers. */
904
905 void silc_server_send_notify_topic_set(SilcServer server,
906                                        SilcSocketConnection sock,
907                                        int broadcast,
908                                        SilcChannelEntry channel,
909                                        SilcClientID *client_id,
910                                        unsigned int client_id_len,
911                                        char *topic)
912 {
913   SilcBuffer idp;
914
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,
918                           topic ? 2 : 1, 
919                           idp->data, idp->len, 
920                           topic, topic ? strlen(topic) : 0);
921   silc_buffer_free(idp);
922 }
923
924 /* Sends notify message destined to specific entity. */
925
926 void silc_server_send_notify_dest(SilcServer server,
927                                   SilcSocketConnection sock,
928                                   int broadcast,
929                                   void *dest_id,
930                                   SilcIdType dest_id_type,
931                                   SilcNotifyType type,
932                                   unsigned int argc, ...)
933 {
934   va_list ap;
935   SilcBuffer packet;
936
937   va_start(ap, argc);
938
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);
944 }
945
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. */
952
953 void silc_server_send_notify_to_channel(SilcServer server,
954                                         SilcSocketConnection sender,
955                                         SilcChannelEntry channel,
956                                         unsigned char route_notify,
957                                         SilcNotifyType type,
958                                         unsigned int argc, ...)
959 {
960   va_list ap;
961   SilcBuffer packet;
962
963   va_start(ap, argc);
964
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);
970 }
971
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). */
977
978 void silc_server_send_notify_on_channels(SilcServer server,
979                                          SilcClientEntry client,
980                                          SilcNotifyType type,
981                                          unsigned int argc, ...)
982 {
983   int k;
984   SilcSocketConnection sock = NULL;
985   SilcPacketContext packetdata;
986   SilcClientEntry c;
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;
994   SilcBuffer packet;
995   unsigned char *data;
996   unsigned int data_len;
997   int force_send = FALSE;
998   va_list ap;
999
1000   SILC_LOG_DEBUG(("Start"));
1001
1002   if (!silc_list_count(client->channels))
1003     return;
1004
1005   va_start(ap, argc);
1006   packet = silc_notify_payload_encode(type, argc, ap);
1007   data = packet->data;
1008   data_len = packet->len;
1009
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;
1017
1018   silc_list_start(client->channels);
1019   while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
1020     channel = chl->channel;
1021
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) {
1025       c = chl2->client;
1026       
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)
1030           break;
1031       if (k < sent_clients_count)
1032         continue;
1033
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)
1041             break;
1042         if (k < routed_count)
1043           continue;
1044         
1045         /* Get data used in packet header encryption, keys and stuff. */
1046         sock = (SilcSocketConnection)c->router->connection;
1047         idata = (SilcIDListData)c->router;
1048         
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);
1055
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, 
1060                                                 force_send);
1061         
1062         silc_free(packetdata.dst_id);
1063
1064         /* We want to make sure that the packet is routed to same router
1065            only once. Mark this route as sent route. */
1066         k = routed_count;
1067         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1068         routed[k] = c->router;
1069         routed_count++;
1070
1071         continue;
1072       }
1073
1074       if (c && c->router)
1075         continue;
1076
1077       /* Send to locally connected client */
1078       if (c) {
1079         
1080         /* Get data used in packet header encryption, keys and stuff. */
1081         sock = (SilcSocketConnection)c->connection;
1082         idata = (SilcIDListData)c;
1083         
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);
1090
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, 
1095                                                 force_send);
1096
1097         silc_free(packetdata.dst_id);
1098
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++;
1104       }
1105     }
1106   }
1107
1108   if (routed_count)
1109     silc_free(routed);
1110   if (sent_clients_count)
1111     silc_free(sent_clients);
1112   silc_free(packetdata.src_id);
1113 }
1114
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. */
1120
1121 void silc_server_send_new_id(SilcServer server,
1122                              SilcSocketConnection sock,
1123                              int broadcast,
1124                              void *id, SilcIdType id_type, 
1125                              unsigned int id_len)
1126 {
1127   SilcBuffer idp;
1128
1129   SILC_LOG_DEBUG(("Start"));
1130
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);
1136 }
1137
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
1141    is broadcasted. */
1142
1143 void silc_server_send_new_channel(SilcServer server,
1144                                   SilcSocketConnection sock,
1145                                   int broadcast,
1146                                   char *channel_name,
1147                                   void *channel_id, 
1148                                   unsigned int channel_id_len)
1149 {
1150   SilcBuffer packet;
1151   unsigned char *cid;
1152   unsigned int name_len = strlen(channel_name);
1153
1154   SILC_LOG_DEBUG(("Start"));
1155
1156   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1157   if (!cid)
1158     return;
1159
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),
1167                      SILC_STR_END);
1168
1169
1170   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1171                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1172                           packet->data, packet->len, FALSE);
1173
1174   silc_free(cid);
1175   silc_buffer_free(packet);
1176 }
1177
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. */
1186
1187 void silc_server_send_channel_key(SilcServer server,
1188                                   SilcSocketConnection sender,
1189                                   SilcChannelEntry channel,
1190                                   unsigned char route)
1191 {
1192   SilcBuffer packet;
1193   unsigned char *chid;
1194   unsigned int tmp_len;
1195  
1196   SILC_LOG_DEBUG(("Start"));
1197  
1198   chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1199   if (!chid)
1200     return;
1201  
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);
1207  
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);
1212   silc_free(chid);
1213 }
1214
1215 /* Generic function to send any command. The arguments must be sent already
1216    encoded into correct form in correct order. */
1217
1218 void silc_server_send_command(SilcServer server, 
1219                               SilcSocketConnection sock,
1220                               SilcCommand command, 
1221                               unsigned int argc, ...)
1222 {
1223   SilcBuffer packet;
1224   va_list ap;
1225
1226   va_start(ap, argc);
1227
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);
1232 }
1233
1234 /* Send the heartbeat packet. */
1235
1236 void silc_server_send_heartbeat(SilcServer server,
1237                                 SilcSocketConnection sock)
1238 {
1239   silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1240                           NULL, 0, FALSE);
1241 }