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