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 - 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 (for router locally means cell wide). */
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. Though,
385        send locally connected server in all cases. */
386     if (server->server_type == SILC_ROUTER && client && client->router && 
387         ((!route && client->router->router == server->id_entry) || route)) {
388       int k;
389
390       /* Check if we have sent the packet to this route already */
391       for (k = 0; k < routed_count; k++)
392         if (routed[k] == client->router)
393           break;
394       if (k < routed_count)
395         continue;
396
397       /* Get data used in packet header encryption, keys and stuff. */
398       sock = (SilcSocketConnection)client->router->connection;
399       idata = (SilcIDListData)client->router;
400
401       /* Send the packet */
402       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
403                                               idata->send_key, idata->hmac, 
404                                               data, data_len, FALSE, 
405                                               force_send);
406
407       /* We want to make sure that the packet is routed to same router
408          only once. Mark this route as sent route. */
409       k = routed_count;
410       routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
411       routed[k] = client->router;
412       routed_count++;
413
414       continue;
415     }
416
417     /* Send to locally connected client */
418     if (client) {
419
420       /* Get data used in packet header encryption, keys and stuff. */
421       sock = (SilcSocketConnection)client->connection;
422       idata = (SilcIDListData)client;
423
424       /* Send the packet */
425       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
426                                               idata->send_key, idata->hmac, 
427                                               data, data_len, FALSE, 
428                                               force_send);
429     }
430   }
431
432   if (routed_count)
433     silc_free(routed);
434   silc_free(packetdata.src_id);
435   silc_free(packetdata.dst_id);
436 }
437
438 /* This routine is explicitly used to relay messages to some channel.
439    Packets sent with this function we have received earlier and are
440    totally encrypted. This just sends the packet to all clients on
441    the channel. If the sender of the packet is someone on the channel 
442    the message will not be sent to that client. The SILC Packet header
443    is encrypted with the session key shared between us and the client.
444    MAC is also computed before encrypting the header. Rest of the
445    packet will be untouched. */
446
447 void silc_server_packet_relay_to_channel(SilcServer server,
448                                          SilcSocketConnection sender_sock,
449                                          SilcChannelEntry channel,
450                                          void *sender, 
451                                          SilcIdType sender_type,
452                                          unsigned char *data,
453                                          unsigned int data_len,
454                                          int force_send)
455 {
456   int found = FALSE;
457   SilcSocketConnection sock = NULL;
458   SilcPacketContext packetdata;
459   SilcClientEntry client = NULL;
460   SilcServerEntry *routed = NULL;
461   SilcChannelClientEntry chl;
462   unsigned int routed_count = 0;
463   SilcIDListData idata;
464
465   SILC_LOG_DEBUG(("Relaying packet to channel"));
466
467   /* Set the packet context pointers. */
468   packetdata.flags = 0;
469   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
470   packetdata.src_id = silc_id_id2str(sender, sender_type);
471   packetdata.src_id_len = silc_id_get_len(sender_type);
472   packetdata.src_id_type = sender_type;
473   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
474   packetdata.dst_id_len = SILC_ID_CHANNEL_LEN;
475   packetdata.dst_id_type = SILC_ID_CHANNEL;
476   packetdata.rng = server->rng;
477   packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
478                                           packetdata.src_id_len +
479                                           packetdata.dst_id_len));
480
481   /* If there are global users in the channel we will send the message
482      first to our router for further routing. */
483   if (server->server_type == SILC_SERVER && !server->standalone &&
484       channel->global_users) {
485     SilcServerEntry router;
486
487     router = server->router;
488
489     /* Check that the sender is not our router. */
490     if (sender_sock != (SilcSocketConnection)router->connection) {
491
492       /* Get data used in packet header encryption, keys and stuff. */
493       sock = (SilcSocketConnection)router->connection;
494       idata = (SilcIDListData)router;
495
496       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
497
498       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
499                                               idata->send_key, idata->hmac, 
500                                               data, data_len, TRUE, 
501                                               force_send);
502     }
503   }
504
505   /* Send the message to clients on the channel's client list. */
506   silc_list_start(channel->user_list);
507   while ((chl = silc_list_get(channel->user_list)) != SILC_LIST_END) {
508     client = chl->client;
509
510     if (client) {
511
512       /* If sender is one on the channel do not send it the packet. */
513       if (!found && !SILC_ID_CLIENT_COMPARE(client->id, sender)) {
514         found = TRUE;
515         continue;
516       }
517
518       /* If the client has set router it means that it is not locally
519          connected client and we will route the packet further. */
520       if (server->server_type == SILC_ROUTER && client->router) {
521         int k;
522
523         /* Sender maybe server as well so we want to make sure that
524            we won't send the message to the server it came from. */
525         if (!found && !SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
526           found = TRUE;
527           continue;
528         }
529
530         /* Check if we have sent the packet to this route already */
531         for (k = 0; k < routed_count; k++)
532           if (routed[k] == client->router)
533             break;
534         if (k < routed_count)
535           continue;
536         
537         /* Get data used in packet header encryption, keys and stuff. */
538         sock = (SilcSocketConnection)client->router->connection;
539         idata = (SilcIDListData)client->router;
540
541         /* Send the packet */
542         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
543                                                 idata->send_key, idata->hmac, 
544                                                 data, data_len, TRUE, 
545                                                 force_send);
546         
547         /* We want to make sure that the packet is routed to same router
548            only once. Mark this route as sent route. */
549         k = routed_count;
550         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
551         routed[k] = client->router;
552         routed_count++;
553         
554         continue;
555       }
556
557       /* XXX Check client's mode on the channel. */
558
559       /* Get data used in packet header encryption, keys and stuff. */
560       sock = (SilcSocketConnection)client->connection;
561       idata = (SilcIDListData)client;
562
563       SILC_LOG_DEBUG(("Sending packet to client %s", 
564                       sock->hostname ? sock->hostname : sock->ip));
565
566       /* Send the packet */
567       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
568                                               idata->send_key, idata->hmac, 
569                                               data, data_len, TRUE, 
570                                               force_send);
571     }
572   }
573
574   silc_free(packetdata.src_id);
575   silc_free(packetdata.dst_id);
576 }
577
578 /* This function is used to send packets strictly to all local clients
579    on a particular channel.  This is used for example to distribute new
580    channel key to all our locally connected clients on the channel. 
581    The packets are always encrypted with the session key shared between
582    the client, this means these are not _to the channel_ but _to the client_
583    on the channel. */
584
585 void silc_server_packet_send_local_channel(SilcServer server,
586                                            SilcChannelEntry channel,
587                                            SilcPacketType type,
588                                            SilcPacketFlags flags,
589                                            unsigned char *data,
590                                            unsigned int data_len,
591                                            int force_send)
592 {
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     if (chl->client) {
602       sock = (SilcSocketConnection)chl->client->connection;
603
604       /* Send the packet to the client */
605       silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
606                                    SILC_ID_CLIENT, data, data_len,
607                                    force_send);
608     }
609   }
610 }
611
612 /* Routine used to send (relay, route) private messages to some destination.
613    If the private message key does not exist then the message is re-encrypted,
614    otherwise we just pass it along. This really is not used to send new
615    private messages (as server does not send them) but to relay received
616    private messages. */
617
618 void silc_server_send_private_message(SilcServer server,
619                                       SilcSocketConnection dst_sock,
620                                       SilcCipher cipher,
621                                       SilcHmac hmac,
622                                       SilcPacketContext *packet)
623 {
624   SilcBuffer buffer = packet->buffer;
625
626   /* Send and re-encrypt if private messge key does not exist */
627   if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
628
629     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
630                      + packet->dst_id_len + packet->padlen);
631     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
632     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
633     
634     /* Re-encrypt packet */
635     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
636     
637     /* Send the packet */
638     silc_server_packet_send_real(server, dst_sock, FALSE);
639
640   } else {
641     /* Key exist so just send it */
642     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
643                      + packet->dst_id_len + packet->padlen);
644     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
645     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
646     silc_server_packet_send_real(server, dst_sock, FALSE);
647   }
648 }
649
650 /* Sends current motd to client */
651
652 void silc_server_send_motd(SilcServer server,
653                            SilcSocketConnection sock)
654 {
655   char *motd;
656   int motd_len;
657
658   if (server->config && server->config->motd && 
659       server->config->motd->motd_file) {
660
661     motd = silc_file_read(server->config->motd->motd_file, &motd_len);
662     if (!motd)
663       return;
664
665     silc_server_send_notify(server, sock, SILC_NOTIFY_TYPE_MOTD, 1,
666                             motd, motd_len);
667     silc_free(motd);
668   }
669 }
670
671 /* Sends error message. Error messages may or may not have any 
672    implications. */
673
674 void silc_server_send_error(SilcServer server,
675                             SilcSocketConnection sock,
676                             const char *fmt, ...)
677 {
678   va_list ap;
679   unsigned char buf[4096];
680
681   memset(buf, 0, sizeof(buf));
682   va_start(ap, fmt);
683   vsprintf(buf, fmt, ap);
684   va_end(ap);
685
686   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
687                           buf, strlen(buf), FALSE);
688 }
689
690 /* Sends notify message. If format is TRUE the variable arguments are
691    formatted and the formatted string is sent as argument payload. If it is
692    FALSE then each argument is sent as separate argument and their format
693    in the argument list must be { argument data, argument length }. */
694
695 void silc_server_send_notify(SilcServer server,
696                              SilcSocketConnection sock,
697                              SilcNotifyType type,
698                              unsigned int argc, ...)
699 {
700   va_list ap;
701   SilcBuffer packet;
702
703   va_start(ap, argc);
704
705   packet = silc_notify_payload_encode(type, argc, ap);
706   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 0, 
707                           packet->data, packet->len, FALSE);
708   silc_buffer_free(packet);
709 }
710
711 /* Sends notify message destined to specific entity. */
712
713 void silc_server_send_notify_dest(SilcServer server,
714                                   SilcSocketConnection sock,
715                                   void *dest_id,
716                                   SilcIdType dest_id_type,
717                                   SilcNotifyType type,
718                                   unsigned int argc, ...)
719 {
720   va_list ap;
721   SilcBuffer packet;
722
723   va_start(ap, argc);
724
725   packet = silc_notify_payload_encode(type, argc, ap);
726   silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0, 
727                                dest_id, dest_id_type,
728                                packet->data, packet->len, FALSE);
729   silc_buffer_free(packet);
730 }
731
732 /* Sends notify message to a channel. The notify message sent is 
733    distributed to all clients on the channel. If `router_notify' is TRUE
734    then the notify may be routed to primary route or to some other routers.
735    If FALSE it is assured that the notify is sent only locally. */
736
737 void silc_server_send_notify_to_channel(SilcServer server,
738                                         SilcChannelEntry channel,
739                                         unsigned char route_notify,
740                                         SilcNotifyType type,
741                                         unsigned int argc, ...)
742 {
743   va_list ap;
744   SilcBuffer packet;
745
746   va_start(ap, argc);
747
748   packet = silc_notify_payload_encode(type, argc, ap);
749   silc_server_packet_send_to_channel(server, channel, 
750                                      SILC_PACKET_NOTIFY, route_notify,
751                                      packet->data, packet->len, FALSE);
752   silc_buffer_free(packet);
753 }
754
755 /* Send notify message to all clients the client has joined. It is quaranteed
756    that the message is sent only once to a client (ie. if a client is joined
757    on two same channel it will receive only one notify message). Also, this
758    sends only to local clients (locally connected if we are server, and to
759    local servers if we are router). */
760
761 void silc_server_send_notify_on_channels(SilcServer server,
762                                          SilcClientEntry client,
763                                          SilcNotifyType type,
764                                          unsigned int argc, ...)
765 {
766   int k;
767   SilcSocketConnection sock = NULL;
768   SilcPacketContext packetdata;
769   SilcClientEntry c;
770   SilcClientEntry *sent_clients = NULL;
771   unsigned int sent_clients_count = 0;
772   SilcServerEntry *routed = NULL;
773   unsigned int routed_count = 0;
774   SilcChannelEntry channel;
775   SilcChannelClientEntry chl, chl2;
776   SilcIDListData idata;
777   SilcBuffer packet;
778   unsigned char *data;
779   unsigned int data_len;
780   int force_send = FALSE;
781   va_list ap;
782
783   SILC_LOG_DEBUG(("Start"));
784
785   if (!silc_list_count(client->channels))
786     return;
787
788   va_start(ap, argc);
789   packet = silc_notify_payload_encode(type, argc, ap);
790   data = packet->data;
791   data_len = packet->len;
792
793   /* Set the packet context pointers. */
794   packetdata.flags = 0;
795   packetdata.type = SILC_PACKET_NOTIFY;
796   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
797   packetdata.src_id_len = SILC_ID_SERVER_LEN;
798   packetdata.src_id_type = SILC_ID_SERVER;
799   packetdata.rng = server->rng;
800
801   silc_list_start(client->channels);
802   while ((chl = silc_list_get(client->channels)) != SILC_LIST_END) {
803     channel = chl->channel;
804
805     /* Send the message to all clients on the channel's client list. */
806     silc_list_start(channel->user_list);
807     while ((chl2 = silc_list_get(channel->user_list)) != SILC_LIST_END) {
808       c = chl2->client;
809       
810       /* Check if we have sent the packet to this client already */
811       for (k = 0; k < sent_clients_count; k++)
812         if (sent_clients[k] == c)
813           break;
814       if (k < sent_clients_count)
815         continue;
816
817       /* If we are router and if this client has router set it is not
818          locally connected client and we will route the message to the
819          router set in the client. */
820       if (c && c->router && server->server_type == SILC_ROUTER) {
821         /* Check if we have sent the packet to this route already */
822         for (k = 0; k < routed_count; k++)
823           if (routed[k] == c->router)
824             break;
825         if (k < routed_count)
826           continue;
827         
828         /* Get data used in packet header encryption, keys and stuff. */
829         sock = (SilcSocketConnection)c->router->connection;
830         idata = (SilcIDListData)c->router;
831         
832         packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
833         packetdata.dst_id_len = SILC_ID_SERVER_LEN;
834         packetdata.dst_id_type = SILC_ID_SERVER;
835         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
836           packetdata.src_id_len + packetdata.dst_id_len;
837         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
838
839         /* Send the packet */
840         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
841                                                 idata->send_key, idata->hmac, 
842                                                 data, data_len, FALSE, 
843                                                 force_send);
844         
845         silc_free(packetdata.dst_id);
846
847         /* We want to make sure that the packet is routed to same router
848            only once. Mark this route as sent route. */
849         k = routed_count;
850         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
851         routed[k] = c->router;
852         routed_count++;
853
854         continue;
855       }
856
857       /* Send to locally connected client */
858       if (c) {
859         
860         /* Get data used in packet header encryption, keys and stuff. */
861         sock = (SilcSocketConnection)c->connection;
862         idata = (SilcIDListData)c;
863         
864         packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
865         packetdata.dst_id_len = SILC_ID_CLIENT_LEN;
866         packetdata.dst_id_type = SILC_ID_CLIENT;
867         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
868           packetdata.src_id_len + packetdata.dst_id_len;
869         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
870
871         /* Send the packet */
872         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
873                                                 idata->send_key, idata->hmac, 
874                                                 data, data_len, FALSE, 
875                                                 force_send);
876
877         silc_free(packetdata.dst_id);
878
879         /* Make sure that we send the notify only once per client. */
880         sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) * 
881                                     (sent_clients_count + 1));
882         sent_clients[sent_clients_count] = c;
883         sent_clients_count++;
884       }
885     }
886   }
887
888   if (routed_count)
889     silc_free(routed);
890   if (sent_clients_count)
891     silc_free(sent_clients);
892   silc_free(packetdata.src_id);
893 }
894
895 /* Sends New ID Payload to remote end. The packet is used to distribute
896    information about new registered clients, servers, channel etc. usually
897    to routers so that they can keep these information up to date. 
898    If the argument `broadcast' is TRUE then the packet is sent as
899    broadcast packet. */
900
901 void silc_server_send_new_id(SilcServer server,
902                              SilcSocketConnection sock,
903                              int broadcast,
904                              void *id, SilcIdType id_type, 
905                              unsigned int id_len)
906 {
907   SilcBuffer idp;
908
909   SILC_LOG_DEBUG(("Start"));
910
911   idp = silc_id_payload_encode(id, id_type);
912   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 
913                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
914                           idp->data, idp->len, FALSE);
915   silc_buffer_free(idp);
916 }
917
918 /* Sends Replace ID payload to remote end. This is used to replace old
919    ID with new ID sent in the packet.  This is called for example when
920    user changes nickname and we create new ID for the user.  If the 
921    argument `broadcast' is TRUE then the packet is sent as
922    broadcast packet. */
923 /* XXX It would be expected that the new id is same type as the old
924    ID. :) */
925
926 void silc_server_send_replace_id(SilcServer server,
927                                  SilcSocketConnection sock,
928                                  int broadcast,
929                                  void *old_id, SilcIdType old_id_type,
930                                  unsigned int old_id_len,
931                                  void *new_id, SilcIdType new_id_type,
932                                  unsigned int new_id_len)
933 {
934   SilcBuffer packet;
935   unsigned char *oid;
936   unsigned char *nid;
937
938   SILC_LOG_DEBUG(("Start"));
939
940   oid = silc_id_id2str(old_id, old_id_type);
941   if (!oid)
942     return;
943
944   nid = silc_id_id2str(new_id, new_id_type);
945   if (!nid)
946     return;
947
948   packet = silc_buffer_alloc(2 + 2 + 2 + 2 + old_id_len + new_id_len);
949   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
950   silc_buffer_format(packet,
951                      SILC_STR_UI_SHORT(old_id_type),
952                      SILC_STR_UI_SHORT(old_id_len),
953                      SILC_STR_UI_XNSTRING(oid, old_id_len),
954                      SILC_STR_UI_SHORT(new_id_type),
955                      SILC_STR_UI_SHORT(new_id_len),
956                      SILC_STR_UI_XNSTRING(nid, new_id_len),
957                      SILC_STR_END);
958
959   silc_server_packet_send(server, sock, SILC_PACKET_REPLACE_ID, 
960                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
961                           packet->data, packet->len, FALSE);
962   silc_free(oid);
963   silc_free(nid);
964   silc_buffer_free(packet);
965 }
966
967 /* This function is used to send Remove Channel User payload. This may sent
968    by server but is usually used only by router to notify other routers that
969    user has left a channel. Normal server sends this packet to its router
970    to notify that the router should not hold a record about this client
971    on a channel anymore. Router distributes it further to other routers. */
972
973 void silc_server_send_remove_channel_user(SilcServer server,
974                                           SilcSocketConnection sock,
975                                           int broadcast,
976                                           void *client_id, void *channel_id)
977 {
978   SilcBuffer packet;
979   unsigned char *clid, *chid;
980
981   SILC_LOG_DEBUG(("Start"));
982
983   clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
984   if (!clid)
985     return;
986
987   chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
988   if (!chid)
989     return;
990
991   packet = silc_buffer_alloc(2 + 2 + SILC_ID_CLIENT_LEN + SILC_ID_CHANNEL_LEN);
992   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
993   silc_buffer_format(packet,
994                      SILC_STR_UI_SHORT(SILC_ID_CLIENT_LEN),
995                      SILC_STR_UI_XNSTRING(clid, SILC_ID_CLIENT_LEN),
996                      SILC_STR_UI_SHORT(SILC_ID_CHANNEL_LEN),
997                      SILC_STR_UI_XNSTRING(chid, SILC_ID_CHANNEL_LEN),
998                      SILC_STR_END);
999
1000   silc_server_packet_send(server, sock, SILC_PACKET_REMOVE_CHANNEL_USER, 
1001                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1002                           packet->data, packet->len, FALSE);
1003   silc_free(clid);
1004   silc_free(chid);
1005   silc_buffer_free(packet);
1006 }
1007
1008 /* Send New Channel Payload to notify about newly created channel in the
1009    SILC network. Normal server nevers sends this packet. Router uses this
1010    to notify other routers in the network about new channel. This packet
1011    is broadcasted. */
1012
1013 void silc_server_send_new_channel(SilcServer server,
1014                                   SilcSocketConnection sock,
1015                                   int broadcast,
1016                                   char *channel_name,
1017                                   void *channel_id, 
1018                                   unsigned int channel_id_len)
1019 {
1020   SilcBuffer packet;
1021   unsigned char *cid;
1022   unsigned int name_len = strlen(channel_name);
1023
1024   SILC_LOG_DEBUG(("Start"));
1025
1026   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1027   if (!cid)
1028     return;
1029
1030   packet = silc_buffer_alloc(2 + 2 + name_len + channel_id_len);
1031   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1032   silc_buffer_format(packet,
1033                      SILC_STR_UI_SHORT(name_len),
1034                      SILC_STR_UI_XNSTRING(channel_name, name_len),
1035                      SILC_STR_UI_SHORT(channel_id_len),
1036                      SILC_STR_UI_XNSTRING(cid, channel_id_len),
1037                      SILC_STR_END);
1038
1039   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1040                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1041                           packet->data, packet->len, FALSE);
1042
1043   silc_free(cid);
1044   silc_buffer_free(packet);
1045 }
1046
1047 /* Send New Channel User payload to notify routers in the network about new
1048    user on the channel. The packet is may be broadcasted. Normal server
1049    can send this but must not receive. Router can send and receive it. */
1050
1051 void silc_server_send_new_channel_user(SilcServer server,
1052                                        SilcSocketConnection sock,
1053                                        int broadcast,
1054                                        void *channel_id, 
1055                                        unsigned int channel_id_len,
1056                                        void *client_id,
1057                                        unsigned int client_id_len)
1058 {
1059   SilcBuffer packet;
1060   unsigned char *clid, *chid;
1061
1062   SILC_LOG_DEBUG(("Start"));
1063
1064   clid = silc_id_id2str(client_id, SILC_ID_CLIENT);
1065   if (!clid)
1066     return;
1067
1068   chid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1069   if (!chid)
1070     return;
1071
1072   packet = silc_buffer_alloc(2 + 2 + channel_id_len + client_id_len);
1073   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1074   silc_buffer_format(packet,
1075                      SILC_STR_UI_SHORT(channel_id_len),
1076                      SILC_STR_UI_XNSTRING(chid, channel_id_len),
1077                      SILC_STR_UI_SHORT(client_id_len),
1078                      SILC_STR_UI_XNSTRING(clid, client_id_len),
1079                      SILC_STR_END);
1080
1081   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL_USER, 
1082                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1083                           packet->data, packet->len, FALSE);
1084   silc_free(clid);
1085   silc_free(chid);
1086   silc_buffer_free(packet);
1087 }
1088
1089 /* Send Channel Key payload to distribute the new channel key. Normal server
1090    sends this to router when new client joins to existing channel. Router
1091    sends this to the local server who forwarded join command in case where
1092    the channel did not exist yet.  Both normal and router servers uses this
1093    also to send this to locally connected clients on the channel. This
1094    must not be broadcasted packet. */
1095
1096 void silc_server_send_channel_key(SilcServer server,
1097                                   SilcChannelEntry channel,
1098                                   unsigned char route)
1099 {
1100   SilcBuffer packet;
1101   unsigned char *chid;
1102   unsigned int tmp_len;
1103
1104   SILC_LOG_DEBUG(("Start"));
1105
1106   chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1107   if (!chid)
1108     return;
1109
1110   /* Encode channel key packet */
1111   tmp_len = strlen(channel->channel_key->cipher->name);
1112   packet = silc_channel_key_payload_encode(SILC_ID_CHANNEL_LEN, chid, tmp_len,
1113                                            channel->channel_key->cipher->name,
1114                                            channel->key_len / 8, channel->key);
1115
1116   silc_server_packet_send_to_channel(server, channel, SILC_PACKET_CHANNEL_KEY,
1117                                      route, packet->data, packet->len, FALSE);
1118   silc_buffer_free(packet);
1119   silc_free(chid);
1120 }
1121
1122 /* Generic function to send any command. The arguments must be sent already
1123    encoded into correct form in correct order. */
1124
1125 void silc_server_send_command(SilcServer server, 
1126                               SilcSocketConnection sock,
1127                               SilcCommand command, 
1128                               unsigned int argc, ...)
1129 {
1130   SilcBuffer packet;
1131   va_list ap;
1132
1133   va_start(ap, argc);
1134
1135   packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1136   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1137                           packet->data, packet->len, TRUE);
1138   silc_buffer_free(packet);
1139 }