Splitted server.[ch] and created packet_send.[ch] and
[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 - 2000 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 /* 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). */
189
190 void silc_server_packet_forward(SilcServer server,
191                                 SilcSocketConnection sock,
192                                 unsigned char *data, unsigned int data_len,
193                                 int force_send)
194 {
195   SilcIDListData idata;
196   SilcCipher cipher = NULL;
197   SilcHmac hmac = NULL;
198
199   SILC_LOG_DEBUG(("Forwarding packet"));
200
201   /* Get data used in the packet sending, keys and stuff */
202   idata = (SilcIDListData)sock->user_data;
203
204   /* Prepare outgoing data buffer for packet sending */
205   silc_packet_send_prepare(sock, 0, 0, data_len);
206
207   /* Put the data to the buffer */
208   if (data && data_len)
209     silc_buffer_put(sock->outbuf, data, data_len);
210
211   /* Add the FORWARDED flag to packet flags */
212   sock->outbuf->data[2] |= (unsigned char)SILC_PACKET_FLAG_FORWARDED;
213
214   if (idata) {
215     cipher = idata->send_key;
216     hmac = idata->hmac;
217   }
218
219   /* Encrypt the packet */
220   silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
221
222   SILC_LOG_HEXDUMP(("Forwarded packet, len %d", sock->outbuf->len),
223                    sock->outbuf->data, sock->outbuf->len);
224
225   /* Now actually send the packet */
226   silc_server_packet_send_real(server, sock, force_send);
227 }
228
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. */
233
234 void silc_server_packet_broadcast(SilcServer server,
235                                   SilcSocketConnection sock,
236                                   SilcPacketContext *packet)
237 {
238   SilcBuffer buffer = packet->buffer;
239   SilcIDListData idata;
240   void *id;
241
242   SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
243
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;
249
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);
255
256     SILC_LOG_HEXDUMP(("Broadcasted packet, len %d", sock->outbuf->len),
257                      sock->outbuf->data, sock->outbuf->len);
258
259     /* Now actually send the packet */
260     silc_server_packet_send_real(server, sock, TRUE);
261     silc_free(id);
262     return;
263   }
264
265   SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
266                   "original sender of this packet"));
267   silc_free(id);
268 }
269
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. */
274
275 static void
276 silc_server_packet_send_to_channel_real(SilcServer server,
277                                         SilcSocketConnection sock,
278                                         SilcPacketContext *packet,
279                                         SilcCipher cipher,
280                                         SilcHmac hmac,
281                                         unsigned char *data,
282                                         unsigned int data_len,
283                                         int channel_message,
284                                         int force_send)
285 {
286   packet->truelen = data_len + SILC_PACKET_HEADER_LEN + 
287     packet->src_id_len + packet->dst_id_len;
288
289   /* Prepare outgoing data buffer for packet sending */
290   silc_packet_send_prepare(sock, 
291                            SILC_PACKET_HEADER_LEN +
292                            packet->src_id_len + 
293                            packet->dst_id_len,
294                            packet->padlen,
295                            data_len);
296
297   packet->buffer = sock->outbuf;
298
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);
303   if (channel_message)
304     silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN + 
305                         packet->src_id_len + packet->dst_id_len +
306                         packet->padlen);
307   else
308     silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
309     
310   SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
311                    sock->outbuf->data, sock->outbuf->len);
312
313   /* Now actually send the packet */
314   silc_server_packet_send_real(server, sock, force_send);
315 }
316
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. */
323
324 void silc_server_packet_send_to_channel(SilcServer server,
325                                         SilcChannelEntry channel,
326                                         SilcPacketType type,
327                                         unsigned char route,
328                                         unsigned char *data,
329                                         unsigned int data_len,
330                                         int force_send)
331 {
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;
339
340   /* This doesn't send channel message packets */
341   if (type == SILC_PACKET_CHANNEL_MESSAGE)
342     return;
343   
344   SILC_LOG_DEBUG(("Sending packet to channel"));
345
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);
359
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;
365
366     /* Get data used in packet header encryption, keys and stuff. */
367     router = server->router;
368     sock = (SilcSocketConnection)router->connection;
369     idata = (SilcIDListData)router;
370     
371     SILC_LOG_DEBUG(("Sending channel message to router for routing"));
372
373     silc_server_packet_send_to_channel_real(server, sock, &packetdata,
374                                             idata->send_key, idata->hmac, 
375                                             data, data_len, FALSE, force_send);
376   }
377
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;
382
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) {
387       int k;
388
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)
392           break;
393       if (k < routed_count)
394         continue;
395
396       /* Get data used in packet header encryption, keys and stuff. */
397       sock = (SilcSocketConnection)client->router->connection;
398       idata = (SilcIDListData)client->router;
399
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, 
404                                               force_send);
405
406       /* We want to make sure that the packet is routed to same router
407          only once. Mark this route as sent route. */
408       k = routed_count;
409       routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
410       routed[k] = client->router;
411       routed_count++;
412
413       continue;
414     }
415
416     /* Send to locally connected client */
417     if (client) {
418
419       /* Get data used in packet header encryption, keys and stuff. */
420       sock = (SilcSocketConnection)client->connection;
421       idata = (SilcIDListData)client;
422
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, 
427                                               force_send);
428     }
429   }
430
431   if (routed_count)
432     silc_free(routed);
433   silc_free(packetdata.src_id);
434   silc_free(packetdata.dst_id);
435 }
436
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. */
445
446 void silc_server_packet_relay_to_channel(SilcServer server,
447                                          SilcSocketConnection sender_sock,
448                                          SilcChannelEntry channel,
449                                          void *sender, 
450                                          SilcIdType sender_type,
451                                          unsigned char *data,
452                                          unsigned int data_len,
453                                          int force_send)
454 {
455   int found = FALSE;
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;
463
464   SILC_LOG_DEBUG(("Relaying packet to channel"));
465
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));
479
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;
485
486     router = server->router;
487
488     /* Check that the sender is not our router. */
489     if (sender_sock != (SilcSocketConnection)router->connection) {
490
491       /* Get data used in packet header encryption, keys and stuff. */
492       sock = (SilcSocketConnection)router->connection;
493       idata = (SilcIDListData)router;
494
495       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
496
497       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
498                                               idata->send_key, idata->hmac, 
499                                               data, data_len, TRUE, 
500                                               force_send);
501     }
502   }
503
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;
508
509     if (client) {
510
511       /* If sender is one on the channel do not send it the packet. */
512       if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
513         found = TRUE;
514         continue;
515       }
516
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) {
520         int k;
521
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)) {
525           found = TRUE;
526           continue;
527         }
528
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)
532             break;
533         if (k < routed_count)
534           continue;
535         
536         /* Get data used in packet header encryption, keys and stuff. */
537         sock = (SilcSocketConnection)client->router->connection;
538         idata = (SilcIDListData)client->router;
539
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, 
544                                                 force_send);
545         
546         /* We want to make sure that the packet is routed to same router
547            only once. Mark this route as sent route. */
548         k = routed_count;
549         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
550         routed[k] = client->router;
551         routed_count++;
552         
553         continue;
554       }
555
556       /* XXX Check client's mode on the channel. */
557
558       /* Get data used in packet header encryption, keys and stuff. */
559       sock = (SilcSocketConnection)client->connection;
560       idata = (SilcIDListData)client;
561
562       SILC_LOG_DEBUG(("Sending packet to client %s", 
563                       sock->hostname ? sock->hostname : sock->ip));
564
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, 
569                                               force_send);
570     }
571   }
572
573   silc_free(packetdata.src_id);
574   silc_free(packetdata.dst_id);
575 }
576
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_
582    on the channel. */
583
584 void silc_server_packet_send_local_channel(SilcServer server,
585                                            SilcChannelEntry channel,
586                                            SilcPacketType type,
587                                            SilcPacketFlags flags,
588                                            unsigned char *data,
589                                            unsigned int data_len,
590                                            int force_send)
591 {
592   SilcClientEntry client;
593   SilcChannelClientEntry chl;
594   SilcSocketConnection sock = NULL;
595
596   SILC_LOG_DEBUG(("Start"));
597
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;
602
603     if (client) {
604       sock = (SilcSocketConnection)client->connection;
605
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,
609                                    force_send);
610     }
611   }
612 }
613
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
618    private messages. */
619
620 void silc_server_send_private_message(SilcServer server,
621                                       SilcSocketConnection dst_sock,
622                                       SilcCipher cipher,
623                                       SilcHmac hmac,
624                                       SilcPacketContext *packet)
625 {
626   SilcBuffer buffer = packet->buffer;
627
628   /* Send and re-encrypt if private messge key does not exist */
629   if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
630
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);
635     
636     /* Re-encrypt packet */
637     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
638     
639     /* Send the packet */
640     silc_server_packet_send_real(server, dst_sock, FALSE);
641
642   } else {
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);
649   }
650 }
651
652 /* Sends current motd to client */
653
654 void silc_server_send_motd(SilcServer server,
655                            SilcSocketConnection sock)
656 {
657   char *motd;
658   int motd_len;
659
660   if (server->config && server->config->motd && 
661       server->config->motd->motd_file) {
662
663     motd = silc_file_read(server->config->motd->motd_file, &motd_len);
664     if (!motd)
665       return;
666
667     silc_server_send_notify(server, sock, SILC_NOTIFY_TYPE_MOTD, 1,
668                             motd, motd_len);
669     silc_free(motd);
670   }
671 }
672
673 /* Sends error message. Error messages may or may not have any 
674    implications. */
675
676 void silc_server_send_error(SilcServer server,
677                             SilcSocketConnection sock,
678                             const char *fmt, ...)
679 {
680   va_list ap;
681   unsigned char buf[4096];
682
683   memset(buf, 0, sizeof(buf));
684   va_start(ap, fmt);
685   vsprintf(buf, fmt, ap);
686   va_end(ap);
687
688   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
689                           buf, strlen(buf), FALSE);
690 }
691
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 }. */
696
697 void silc_server_send_notify(SilcServer server,
698                              SilcSocketConnection sock,
699                              SilcNotifyType type,
700                              unsigned int argc, ...)
701 {
702   va_list ap;
703   SilcBuffer packet;
704
705   va_start(ap, argc);
706
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);
711 }
712
713 /* Sends notify message destined to specific entity. */
714
715 void silc_server_send_notify_dest(SilcServer server,
716                                   SilcSocketConnection sock,
717                                   void *dest_id,
718                                   SilcIdType dest_id_type,
719                                   SilcNotifyType type,
720                                   unsigned int argc, ...)
721 {
722   va_list ap;
723   SilcBuffer packet;
724
725   va_start(ap, argc);
726
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);
732 }
733
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. */
738
739 void silc_server_send_notify_to_channel(SilcServer server,
740                                         SilcChannelEntry channel,
741                                         unsigned char route_notify,
742                                         SilcNotifyType type,
743                                         unsigned int argc, ...)
744 {
745   va_list ap;
746   SilcBuffer packet;
747
748   va_start(ap, argc);
749
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);
755 }
756
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). */
762
763 void silc_server_send_notify_on_channels(SilcServer server,
764                                          SilcClientEntry client,
765                                          SilcNotifyType type,
766                                          unsigned int argc, ...)
767 {
768   int k;
769   SilcSocketConnection sock = NULL;
770   SilcPacketContext packetdata;
771   SilcClientEntry c;
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;
779   SilcBuffer packet;
780   unsigned char *data;
781   unsigned int data_len;
782   int force_send = FALSE;
783   va_list ap;
784
785   SILC_LOG_DEBUG(("Start"));
786
787   if (!silc_list_count(client->channels))
788     return;
789
790   va_start(ap, argc);
791   packet = silc_notify_payload_encode(type, argc, ap);
792   data = packet->data;
793   data_len = packet->len;
794
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;
802
803   silc_list_start(client->channels);
804   while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
805     channel = chl->channel;
806
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) {
810       c = chl2->client;
811       
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)
815           break;
816       if (k < sent_clients_count)
817         continue;
818
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)
826             break;
827         if (k < routed_count)
828           continue;
829         
830         /* Get data used in packet header encryption, keys and stuff. */
831         sock = (SilcSocketConnection)c->router->connection;
832         idata = (SilcIDListData)c->router;
833         
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);
840
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, 
845                                                 force_send);
846         
847         silc_free(packetdata.dst_id);
848
849         /* We want to make sure that the packet is routed to same router
850            only once. Mark this route as sent route. */
851         k = routed_count;
852         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
853         routed[k] = c->router;
854         routed_count++;
855
856         continue;
857       }
858
859       /* Send to locally connected client */
860       if (c) {
861         
862         /* Get data used in packet header encryption, keys and stuff. */
863         sock = (SilcSocketConnection)c->connection;
864         idata = (SilcIDListData)c;
865         
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);
872
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, 
877                                                 force_send);
878
879         silc_free(packetdata.dst_id);
880
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++;
886       }
887     }
888   }
889
890   if (routed_count)
891     silc_free(routed);
892   if (sent_clients_count)
893     silc_free(sent_clients);
894   silc_free(packetdata.src_id);
895 }
896
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
901    broadcast packet. */
902
903 void silc_server_send_new_id(SilcServer server,
904                              SilcSocketConnection sock,
905                              int broadcast,
906                              void *id, SilcIdType id_type, 
907                              unsigned int id_len)
908 {
909   SilcBuffer idp;
910
911   SILC_LOG_DEBUG(("Start"));
912
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);
918 }
919
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
924    broadcast packet. */
925 /* XXX It would be expected that the new id is same type as the old
926    ID. :) */
927
928 void silc_server_send_replace_id(SilcServer server,
929                                  SilcSocketConnection sock,
930                                  int broadcast,
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)
935 {
936   SilcBuffer packet;
937   unsigned char *oid;
938   unsigned char *nid;
939
940   SILC_LOG_DEBUG(("Start"));
941
942   oid = silc_id_id2str(old_id, old_id_type);
943   if (!oid)
944     return;
945
946   nid = silc_id_id2str(new_id, new_id_type);
947   if (!nid)
948     return;
949
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),
959                      SILC_STR_END);
960
961   silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID, 
962                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
963                           packet->data, packet->len, FALSE);
964   silc_free(oid);
965   silc_free(nid);
966   silc_buffer_free(packet);
967 }
968
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. */
974
975 void silc_server_send_remove_channel_user(SilcServer server,
976                                           SilcSocketConnection sock,
977                                           int broadcast,
978                                           void *client_id, void *channel_id)
979 {
980   SilcBuffer packet;
981   unsigned char *clid, *chid;
982
983   SILC_LOG_DEBUG(("Start"));
984
985   clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
986   if (!clid)
987     return;
988
989   chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
990   if (!chid)
991     return;
992
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),
1000                      SILC_STR_END);
1001
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);
1005   silc_free(clid);
1006   silc_free(chid);
1007   silc_buffer_free(packet);
1008 }
1009
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
1013    is broadcasted. */
1014
1015 void silc_server_send_new_channel(SilcServer server,
1016                                   SilcSocketConnection sock,
1017                                   int broadcast,
1018                                   char *channel_name,
1019                                   void *channel_id, 
1020                                   unsigned int channel_id_len)
1021 {
1022   SilcBuffer packet;
1023   unsigned char *cid;
1024   unsigned int name_len = strlen(channel_name);
1025
1026   SILC_LOG_DEBUG(("Start"));
1027
1028   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1029   if (!cid)
1030     return;
1031
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),
1039                      SILC_STR_END);
1040
1041   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1042                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1043                           packet->data, packet->len, FALSE);
1044
1045   silc_free(cid);
1046   silc_buffer_free(packet);
1047 }
1048
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. */
1052
1053 void silc_server_send_new_channel_user(SilcServer server,
1054                                        SilcSocketConnection sock,
1055                                        int broadcast,
1056                                        void *channel_id, 
1057                                        unsigned int channel_id_len,
1058                                        void *client_id,
1059                                        unsigned int client_id_len)
1060 {
1061   SilcBuffer packet;
1062   unsigned char *clid, *chid;
1063
1064   SILC_LOG_DEBUG(("Start"));
1065
1066   clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1067   if (!clid)
1068     return;
1069
1070   chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1071   if (!chid)
1072     return;
1073
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),
1081                      SILC_STR_END);
1082
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);
1086   silc_free(clid);
1087   silc_free(chid);
1088   silc_buffer_free(packet);
1089 }