updates.
[silc.git] / apps / silcd / packet_send.c
1 /*
2
3   packet_send.c
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /*
21  * Server packet routines to send packets. 
22  */
23 /* $Id$ */
24
25 #include "serverincludes.h"
26 #include "server_internal.h"
27
28 /* Routine that sends packet or marks packet to be sent. This is used
29    directly only in special cases. Normal cases should use
30    silc_server_packet_send. Returns < 0 error. */
31
32 int silc_server_packet_send_real(SilcServer server,
33                                  SilcSocketConnection sock,
34                                  int force_send)
35 {
36   int ret;
37
38   /* If rekey protocol is active we must assure that all packets are
39      sent through packet queue. */
40   if (SILC_SERVER_IS_REKEY(sock))
41     force_send = FALSE;
42
43   /* Send the packet */
44   ret = silc_packet_send(sock, force_send);
45   if (ret != -2)
46     return ret;
47
48   /* Mark that there is some outgoing data available for this connection. 
49      This call sets the connection both for input and output (the input
50      is set always and this call keeps the input setting, actually). 
51      Actual data sending is performed by silc_server_packet_process. */
52   SILC_SET_CONNECTION_FOR_OUTPUT(sock->sock);
53
54   /* Mark to socket that data is pending in outgoing buffer. This flag
55      is needed if new data is added to the buffer before the earlier
56      put data is sent to the network. */
57   SILC_SET_OUTBUF_PENDING(sock);
58
59   return 0;
60 }
61
62 /* Assembles a new packet to be sent out to network. This doesn't actually
63    send the packet but creates the packet and fills the outgoing data
64    buffer and marks the packet ready to be sent to network. However, If 
65    argument force_send is TRUE the packet is sent immediately and not put 
66    to queue. Normal case is that the packet is not sent immediately. */
67
68 void silc_server_packet_send(SilcServer server,
69                              SilcSocketConnection sock, 
70                              SilcPacketType type, 
71                              SilcPacketFlags flags,
72                              unsigned char *data, 
73                              uint32 data_len,
74                              int force_send)
75 {
76   void *dst_id = NULL;
77   SilcIdType dst_id_type = SILC_ID_NONE;
78
79   if (!sock)
80     return;
81
82   /* Get data used in the packet sending, keys and stuff */
83   switch(sock->type) {
84   case SILC_SOCKET_TYPE_CLIENT:
85     dst_id = ((SilcClientEntry)sock->user_data)->id;
86     dst_id_type = SILC_ID_CLIENT;
87     break;
88   case SILC_SOCKET_TYPE_SERVER:
89   case SILC_SOCKET_TYPE_ROUTER:
90     dst_id = ((SilcServerEntry)sock->user_data)->id;
91     dst_id_type = SILC_ID_SERVER;
92     break;
93   default:
94     break;
95   }
96
97   silc_server_packet_send_dest(server, sock, type, flags, dst_id,
98                                dst_id_type, data, data_len, force_send);
99 }
100
101 /* Assembles a new packet to be sent out to network. This doesn't actually
102    send the packet but creates the packet and fills the outgoing data
103    buffer and marks the packet ready to be sent to network. However, If 
104    argument force_send is TRUE the packet is sent immediately and not put 
105    to queue. Normal case is that the packet is not sent immediately. 
106    Destination information is sent as argument for this function. */
107
108 void silc_server_packet_send_dest(SilcServer server,
109                                   SilcSocketConnection sock, 
110                                   SilcPacketType type, 
111                                   SilcPacketFlags flags,
112                                   void *dst_id,
113                                   SilcIdType dst_id_type,
114                                   unsigned char *data, 
115                                   uint32 data_len,
116                                   int force_send)
117 {
118   SilcPacketContext packetdata;
119   SilcIDListData idata;
120   SilcCipher cipher = NULL;
121   SilcHmac hmac = NULL;
122   unsigned char *dst_id_data = NULL;
123   uint32 dst_id_len = 0;
124
125   SILC_LOG_DEBUG(("Sending packet, type %d", type));
126
127   /* Get data used in the packet sending, keys and stuff */
128   idata = (SilcIDListData)sock->user_data;
129
130   if (dst_id) {
131     dst_id_data = silc_id_id2str(dst_id, dst_id_type);
132     dst_id_len = silc_id_get_len(dst_id, dst_id_type);
133   }
134
135   /* Set the packet context pointers */
136   packetdata.type = type;
137   packetdata.flags = flags;
138   packetdata.src_id = silc_id_id2str(server->id, server->id_type);
139   packetdata.src_id_len = silc_id_get_len(server->id, server->id_type);
140   packetdata.src_id_type = server->id_type;
141   packetdata.dst_id = dst_id_data;
142   packetdata.dst_id_len = dst_id_len;
143   packetdata.dst_id_type = dst_id_type;
144   packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
145     packetdata.src_id_len + dst_id_len;
146   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
147
148   /* Prepare outgoing data buffer for packet sending */
149   silc_packet_send_prepare(sock, 
150                            SILC_PACKET_HEADER_LEN +
151                            packetdata.src_id_len + 
152                            packetdata.dst_id_len,
153                            packetdata.padlen,
154                            data_len);
155
156   SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
157
158   packetdata.buffer = sock->outbuf;
159
160   /* Put the data to the buffer */
161   if (data && data_len)
162     silc_buffer_put(sock->outbuf, data, data_len);
163
164   /* Create the outgoing packet */
165   silc_packet_assemble(&packetdata);
166
167   if (idata) {
168     cipher = idata->send_key;
169     hmac = idata->hmac_send;
170   }
171
172   /* Encrypt the packet */
173   silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
174
175   SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
176                    sock->outbuf->data, sock->outbuf->len);
177
178   /* Now actually send the packet */
179   silc_server_packet_send_real(server, sock, force_send);
180
181   if (packetdata.src_id)
182     silc_free(packetdata.src_id);
183   if (packetdata.dst_id)
184     silc_free(packetdata.dst_id);
185 }
186
187 /* Assembles a new packet to be sent out to network. This doesn't actually
188    send the packet but creates the packet and fills the outgoing data
189    buffer and marks the packet ready to be sent to network. However, If 
190    argument force_send is TRUE the packet is sent immediately and not put 
191    to queue. Normal case is that the packet is not sent immediately. 
192    The source and destination information is sent as argument for this
193    function. */
194
195 void silc_server_packet_send_srcdest(SilcServer server,
196                                      SilcSocketConnection sock, 
197                                      SilcPacketType type, 
198                                      SilcPacketFlags flags,
199                                      void *src_id,
200                                      SilcIdType src_id_type,
201                                      void *dst_id,
202                                      SilcIdType dst_id_type,
203                                      unsigned char *data, 
204                                      uint32 data_len,
205                                      int force_send)
206 {
207   SilcPacketContext packetdata;
208   SilcIDListData idata;
209   SilcCipher cipher = NULL;
210   SilcHmac hmac = NULL;
211   unsigned char *dst_id_data = NULL;
212   uint32 dst_id_len = 0;
213   unsigned char *src_id_data = NULL;
214   uint32 src_id_len = 0;
215
216   SILC_LOG_DEBUG(("Sending packet, type %d", type));
217
218   /* Get data used in the packet sending, keys and stuff */
219   idata = (SilcIDListData)sock->user_data;
220
221   if (dst_id) {
222     dst_id_data = silc_id_id2str(dst_id, dst_id_type);
223     dst_id_len = silc_id_get_len(dst_id, dst_id_type);
224   }
225
226   if (src_id) {
227     src_id_data = silc_id_id2str(src_id, src_id_type);
228     src_id_len = silc_id_get_len(src_id, src_id_type);
229   }
230
231   /* Set the packet context pointers */
232   packetdata.type = type;
233   packetdata.flags = flags;
234   packetdata.src_id = src_id_data;
235   packetdata.src_id_len = src_id_len;
236   packetdata.src_id_type = src_id_type;
237   packetdata.dst_id = dst_id_data;
238   packetdata.dst_id_len = dst_id_len;
239   packetdata.dst_id_type = dst_id_type;
240   packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
241     packetdata.src_id_len + dst_id_len;
242   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
243
244   /* Prepare outgoing data buffer for packet sending */
245   silc_packet_send_prepare(sock, 
246                            SILC_PACKET_HEADER_LEN +
247                            packetdata.src_id_len + 
248                            packetdata.dst_id_len,
249                            packetdata.padlen,
250                            data_len);
251
252   SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
253
254   packetdata.buffer = sock->outbuf;
255
256   /* Put the data to the buffer */
257   if (data && data_len)
258     silc_buffer_put(sock->outbuf, data, data_len);
259
260   /* Create the outgoing packet */
261   silc_packet_assemble(&packetdata);
262
263   if (idata) {
264     cipher = idata->send_key;
265     hmac = idata->hmac_send;
266   }
267
268   /* Encrypt the packet */
269   silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
270
271   SILC_LOG_HEXDUMP(("Outgoing packet, len %d", sock->outbuf->len),
272                    sock->outbuf->data, sock->outbuf->len);
273
274   /* Now actually send the packet */
275   silc_server_packet_send_real(server, sock, force_send);
276
277   if (packetdata.src_id)
278     silc_free(packetdata.src_id);
279   if (packetdata.dst_id)
280     silc_free(packetdata.dst_id);
281 }
282
283 /* Broadcast received packet to our primary route. This function is used
284    by router to further route received broadcast packet. It is expected
285    that the broadcast flag from the packet is checked before calling this
286    function. This does not test or set the broadcast flag. */
287
288 void silc_server_packet_broadcast(SilcServer server,
289                                   SilcSocketConnection sock,
290                                   SilcPacketContext *packet)
291 {
292   SilcBuffer buffer = packet->buffer;
293   SilcIDListData idata;
294   void *id;
295
296   SILC_LOG_DEBUG(("Broadcasting received broadcast packet"));
297
298   /* If the packet is originated from our primary route we are
299      not allowed to send the packet. */
300   id = silc_id_str2id(packet->src_id, packet->src_id_len, packet->src_id_type);
301   if (id && !SILC_ID_SERVER_COMPARE(id, server->router->id)) {
302     idata = (SilcIDListData)sock->user_data;
303
304     silc_buffer_push(buffer, buffer->data - buffer->head);
305     silc_packet_send_prepare(sock, 0, 0, buffer->len); 
306     silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
307     silc_packet_encrypt(idata->send_key, idata->hmac_send, 
308                         sock->outbuf, sock->outbuf->len);
309
310     SILC_LOG_HEXDUMP(("Broadcasted 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, TRUE);
315     silc_free(id);
316     return;
317   }
318
319   SILC_LOG_DEBUG(("Will not broadcast to primary route since it is the "
320                   "original sender of this packet"));
321   silc_free(id);
322 }
323
324 /* Routes received packet to `sock'. This is used to route the packets that
325    router receives but are not destined to it. */
326
327 void silc_server_packet_route(SilcServer server,
328                               SilcSocketConnection sock,
329                               SilcPacketContext *packet)
330 {
331   SilcBuffer buffer = packet->buffer;
332   SilcIDListData idata;
333
334   SILC_LOG_DEBUG(("Routing received packet"));
335
336   idata = (SilcIDListData)sock->user_data;
337
338   silc_buffer_push(buffer, buffer->data - buffer->head);
339   silc_packet_send_prepare(sock, 0, 0, buffer->len); 
340   silc_buffer_put(sock->outbuf, buffer->data, buffer->len);
341   silc_packet_encrypt(idata->send_key, idata->hmac_send, 
342                       sock->outbuf, sock->outbuf->len);
343
344   SILC_LOG_HEXDUMP(("Routed packet, len %d", sock->outbuf->len),
345                    sock->outbuf->data, sock->outbuf->len);
346
347   /* Now actually send the packet */
348   silc_server_packet_send_real(server, sock, TRUE);
349 }
350
351 /* Internal routine to actually create the channel packet and send it
352    to network. This is common function in channel message sending. If
353    `channel_message' is TRUE this encrypts the message as it is strictly
354    a channel message. If FALSE normal encryption process is used. */
355
356 static void
357 silc_server_packet_send_to_channel_real(SilcServer server,
358                                         SilcSocketConnection sock,
359                                         SilcPacketContext *packet,
360                                         SilcCipher cipher,
361                                         SilcHmac hmac,
362                                         unsigned char *data,
363                                         uint32 data_len,
364                                         int channel_message,
365                                         int force_send)
366 {
367   packet->truelen = data_len + SILC_PACKET_HEADER_LEN + 
368     packet->src_id_len + packet->dst_id_len;
369
370   /* Prepare outgoing data buffer for packet sending */
371   silc_packet_send_prepare(sock, 
372                            SILC_PACKET_HEADER_LEN +
373                            packet->src_id_len + 
374                            packet->dst_id_len,
375                            packet->padlen,
376                            data_len);
377
378   packet->buffer = sock->outbuf;
379
380   /* Put the data to buffer, assemble and encrypt the packet. The packet
381      is encrypted with normal session key shared with the client. */
382   silc_buffer_put(sock->outbuf, data, data_len);
383   silc_packet_assemble(packet);
384   if (channel_message)
385     silc_packet_encrypt(cipher, hmac, sock->outbuf, SILC_PACKET_HEADER_LEN + 
386                         packet->src_id_len + packet->dst_id_len +
387                         packet->padlen);
388   else
389     silc_packet_encrypt(cipher, hmac, sock->outbuf, sock->outbuf->len);
390     
391   SILC_LOG_HEXDUMP(("Channel packet, len %d", sock->outbuf->len),
392                    sock->outbuf->data, sock->outbuf->len);
393
394   /* Now actually send the packet */
395   silc_server_packet_send_real(server, sock, force_send);
396 }
397
398 /* This routine is used by the server to send packets to channel. The 
399    packet sent with this function is distributed to all clients on
400    the channel. Usually this is used to send notify messages to the
401    channel, things like notify about new user joining to the channel. 
402    If `route' is FALSE then the packet is sent only locally and will not
403    be routed anywhere (for router locally means cell wide). If `sender'
404    is provided then the packet is not sent to that connection since it
405    originally came from it. */
406
407 void silc_server_packet_send_to_channel(SilcServer server,
408                                         SilcSocketConnection sender,
409                                         SilcChannelEntry channel,
410                                         SilcPacketType type,
411                                         unsigned char route,
412                                         unsigned char *data,
413                                         uint32 data_len,
414                                         int force_send)
415 {
416   SilcSocketConnection sock = NULL;
417   SilcPacketContext packetdata;
418   SilcClientEntry client = NULL;
419   SilcServerEntry *routed = NULL;
420   SilcChannelClientEntry chl;
421   SilcHashTableList htl;
422   SilcIDListData idata;
423   uint32 routed_count = 0;
424
425   /* This doesn't send channel message packets */
426   if (type == SILC_PACKET_CHANNEL_MESSAGE)
427     return;
428   
429   SILC_LOG_DEBUG(("Sending packet to channel"));
430
431   /* Set the packet context pointers. */
432   packetdata.flags = 0;
433   packetdata.type = type;
434   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
435   packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
436   packetdata.src_id_type = SILC_ID_SERVER;
437   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
438   packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
439   packetdata.dst_id_type = SILC_ID_CHANNEL;
440   packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
441     packetdata.src_id_len + packetdata.dst_id_len;
442   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
443
444   /* If there are global users in the channel we will send the message
445      first to our router for further routing. */
446   if (route && server->server_type == SILC_SERVER && !server->standalone &&
447       channel->global_users) {
448     SilcServerEntry router;
449
450     /* Get data used in packet header encryption, keys and stuff. */
451     router = server->router;
452     sock = (SilcSocketConnection)router->connection;
453     idata = (SilcIDListData)router;
454     
455     if (sock != sender) {
456       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
457       
458       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
459                                               idata->send_key, 
460                                               idata->hmac_send, 
461                                               data, data_len, FALSE, 
462                                               force_send);
463     }
464   }
465
466   /* Send the message to clients on the channel's client list. */
467   silc_hash_table_list(channel->user_list, &htl);
468   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
469     client = chl->client;
470
471     /* If client has router set it is not locally connected client and
472        we will route the message to the router set in the client. Though,
473        send locally connected server in all cases. */
474     if (server->server_type == SILC_ROUTER && client && client->router && 
475         ((!route && client->router->router == server->id_entry) || route)) {
476       int k;
477
478       /* Check if we have sent the packet to this route already */
479       for (k = 0; k < routed_count; k++)
480         if (routed[k] == client->router)
481           break;
482       if (k < routed_count)
483         continue;
484
485       /* Get data used in packet header encryption, keys and stuff. */
486       sock = (SilcSocketConnection)client->router->connection;
487       idata = (SilcIDListData)client->router;
488
489       if (sender && sock == sender)
490         continue;
491
492       /* Send the packet */
493       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
494                                               idata->send_key, 
495                                               idata->hmac_send, 
496                                               data, data_len, FALSE, 
497                                               force_send);
498
499       /* We want to make sure that the packet is routed to same router
500          only once. Mark this route as sent route. */
501       k = routed_count;
502       routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
503       routed[k] = client->router;
504       routed_count++;
505
506       continue;
507     }
508
509     if (client && client->router)
510       continue;
511
512     /* Send to locally connected client */
513     if (client) {
514
515       /* Get data used in packet header encryption, keys and stuff. */
516       sock = (SilcSocketConnection)client->connection;
517       idata = (SilcIDListData)client;
518
519       if (sender && sock == sender)
520         continue;
521
522       /* Send the packet */
523       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
524                                               idata->send_key, 
525                                               idata->hmac_send, 
526                                               data, data_len, FALSE, 
527                                               force_send);
528     }
529   }
530
531   if (routed_count)
532     silc_free(routed);
533   silc_free(packetdata.src_id);
534   silc_free(packetdata.dst_id);
535 }
536
537 /* This checks whether the relayed packet came from router. If it did
538    then we'll need to encrypt it with the channel key. This is called
539    from the silc_server_packet_relay_to_channel. */
540
541 static void
542 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
543                                             SilcSocketConnection sock,
544                                             SilcChannelEntry channel,
545                                             unsigned char *data,
546                                             unsigned int data_len)
547 {
548   /* If we are router and the packet came from router and private key
549      has not been set for the channel then we must encrypt the packet
550      as it was decrypted with the session key shared between us and the
551      router which sent it. This is so, because cells does not share the
552      same channel key. */
553   if (server->server_type == SILC_ROUTER &&
554       sock->type == SILC_SOCKET_TYPE_ROUTER &&
555       !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
556       channel->channel_key) {
557     SilcBuffer chp;
558     uint32 iv_len, i;
559     uint16 data_len, flags;
560
561     iv_len = silc_cipher_get_block_len(channel->channel_key);
562     if (channel->iv[0] == '\0')
563       for (i = 0; i < iv_len; i++) channel->iv[i] = 
564                                      silc_rng_get_byte(server->rng);
565     else
566       silc_hash_make(server->md5hash, channel->iv, iv_len, channel->iv);
567     
568     /* Encode new payload. This encrypts it also. */
569     SILC_GET16_MSB(flags, data);
570     SILC_GET16_MSB(data_len, data + 2);
571     chp = silc_channel_message_payload_encode(flags, data_len, 
572                                               data + 4,
573                                               iv_len, channel->iv,
574                                               channel->channel_key,
575                                               channel->hmac);
576     memcpy(data, chp->data, chp->len);
577     silc_buffer_free(chp);
578   }
579 }
580
581 /* This routine is explicitly used to relay messages to some channel.
582    Packets sent with this function we have received earlier and are
583    totally encrypted. This just sends the packet to all clients on
584    the channel. If the sender of the packet is someone on the channel 
585    the message will not be sent to that client. The SILC Packet header
586    is encrypted with the session key shared between us and the client.
587    MAC is also computed before encrypting the header. Rest of the
588    packet will be untouched. */
589
590 void silc_server_packet_relay_to_channel(SilcServer server,
591                                          SilcSocketConnection sender_sock,
592                                          SilcChannelEntry channel,
593                                          void *sender, 
594                                          SilcIdType sender_type,
595                                          void *sender_entry,
596                                          unsigned char *data,
597                                          uint32 data_len,
598                                          int force_send)
599 {
600   bool found = FALSE;
601   SilcSocketConnection sock = NULL;
602   SilcPacketContext packetdata;
603   SilcClientEntry client = NULL;
604   SilcServerEntry *routed = NULL;
605   SilcChannelClientEntry chl;
606   uint32 routed_count = 0;
607   SilcIDListData idata;
608   SilcHashTableList htl;
609
610   SILC_LOG_DEBUG(("Relaying packet to channel"));
611
612   /* Set the packet context pointers. */
613   packetdata.flags = 0;
614   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
615   packetdata.src_id = silc_id_id2str(sender, sender_type);
616   packetdata.src_id_len = silc_id_get_len(sender, sender_type);
617   packetdata.src_id_type = sender_type;
618   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
619   packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
620   packetdata.dst_id_type = SILC_ID_CHANNEL;
621   packetdata.padlen = SILC_PACKET_PADLEN((SILC_PACKET_HEADER_LEN +
622                                           packetdata.src_id_len +
623                                           packetdata.dst_id_len));
624
625   /* This encrypts the packet, if needed. It will be encrypted if
626      it came from the router thus it needs to be encrypted with the
627      channel key. If the channel key does not exist, then we know we
628      don't have a single local user on the channel. */
629   silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
630                                               channel, data,
631                                               data_len);
632
633   /* If there are global users in the channel we will send the message
634      first to our router for further routing. */
635   if (server->server_type == SILC_SERVER && !server->standalone &&
636       channel->global_users) {
637     SilcServerEntry router;
638
639     router = server->router;
640
641     /* Check that the sender is not our router. */
642     if (sender_sock != (SilcSocketConnection)router->connection) {
643
644       /* Get data used in packet header encryption, keys and stuff. */
645       sock = (SilcSocketConnection)router->connection;
646       idata = (SilcIDListData)router;
647
648       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
649
650       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
651                                               idata->send_key, 
652                                               idata->hmac_send, 
653                                               data, data_len, TRUE, 
654                                               force_send);
655     }
656   }
657
658   /* Send the message to clients on the channel's client list. */
659   silc_hash_table_list(channel->user_list, &htl);
660   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
661     client = chl->client;
662
663     if (client) {
664
665       /* If sender is one on the channel do not send it the packet. */
666       if (!found && sender_type == SILC_ID_CLIENT &&
667           SILC_ID_CLIENT_COMPARE(client->id, sender)) {
668         found = TRUE;
669         continue;
670       }
671
672       /* If the client has set router it means that it is not locally
673          connected client and we will route the packet further. */
674       if (server->server_type == SILC_ROUTER && client->router) {
675         int k;
676
677         /* Sender maybe server as well so we want to make sure that
678            we won't send the message to the server it came from. */
679         if (!found && SILC_ID_SERVER_COMPARE(client->router->id, sender)) {
680           found = TRUE;
681           continue;
682         }
683
684         /* Check if we have sent the packet to this route already */
685         for (k = 0; k < routed_count; k++)
686           if (routed[k] == client->router)
687             break;
688         if (k < routed_count)
689           continue;
690         
691         /* Get data used in packet header encryption, keys and stuff. */
692         sock = (SilcSocketConnection)client->router->connection;
693         idata = (SilcIDListData)client->router;
694
695         /* Do not send to the sender. Check first whether the true
696            sender's router is same as this client's router. Also check
697            if the sender socket is the same as this client's router
698            socket. */
699         if (sender_entry && 
700             ((SilcClientEntry)sender_entry)->router == client->router)
701           continue;
702         if (sender_sock && sock == sender_sock)
703           continue;
704
705         SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)", 
706                         silc_id_render(client->id, SILC_ID_CLIENT),
707                         sock->hostname, sock->ip));
708
709         /* We want to make sure that the packet is routed to same router
710            only once. Mark this route as sent route. */
711         k = routed_count;
712         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
713         routed[k] = client->router;
714         routed_count++;
715         
716         /* If the remote connection is router then we'll decrypt the
717            channel message and re-encrypt it with the session key shared
718            between us and the remote router. This is done because the
719            channel keys are cell specific and we have different channel
720            key than the remote router has. */
721         if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
722
723           /* If private key mode is not set then decrypt the packet
724              and re-encrypt it */
725           if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY)) {
726             unsigned char *tmp = silc_calloc(data_len, sizeof(*data));
727             memcpy(tmp, data, data_len);
728
729             /* Decrypt the channel message (we don't check the MAC) */
730             /* XXX this could be optimized and removed all together by
731                taking a copy of the original data before encrypting it
732                and thus would not required decrypting. */
733             if (channel->channel_key &&
734                 !silc_channel_message_payload_decrypt(tmp, data_len, 
735                                                       channel->channel_key,
736                                                       NULL)) {
737               memset(tmp, 0, data_len);
738               silc_free(tmp);
739               continue;
740             }
741
742             /* Now re-encrypt and send it to the router */
743             silc_server_packet_send_srcdest(server, sock, 
744                                             SILC_PACKET_CHANNEL_MESSAGE, 0,
745                                             sender, sender_type,
746                                             channel->id, SILC_ID_CHANNEL,
747                                             tmp, data_len, force_send);
748             
749             /* Free the copy of the channel message */
750             memset(tmp, 0, data_len);
751             silc_free(tmp);
752           } else {
753             /* Private key mode is set, we don't have the channel key, so
754                just re-encrypt the entire packet and send it to the router. */
755             silc_server_packet_send_srcdest(server, sock, 
756                                             SILC_PACKET_CHANNEL_MESSAGE, 0,
757                                             sender, sender_type,
758                                             channel->id, SILC_ID_CHANNEL,
759                                             data, data_len, force_send);
760           }
761           continue;
762         }
763
764         /* Send the packet (to normal server) */
765         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
766                                                 idata->send_key, 
767                                                 idata->hmac_send, 
768                                                 data, data_len, TRUE, 
769                                                 force_send);
770
771         continue;
772       }
773
774       if (client && client->router)
775         continue;
776
777       /* Get data used in packet header encryption, keys and stuff. */
778       sock = (SilcSocketConnection)client->connection;
779       idata = (SilcIDListData)client;
780
781       if (sender_sock && sock == sender_sock)
782         continue;
783
784       SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)", 
785                       silc_id_render(client->id, SILC_ID_CLIENT),
786                       sock->hostname, sock->ip));
787
788       /* Send the packet */
789       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
790                                               idata->send_key, 
791                                               idata->hmac_send, 
792                                               data, data_len, TRUE, 
793                                               force_send);
794     }
795   }
796
797   silc_free(packetdata.src_id);
798   silc_free(packetdata.dst_id);
799 }
800
801 /* This function is used to send packets strictly to all local clients
802    on a particular channel.  This is used for example to distribute new
803    channel key to all our locally connected clients on the channel. 
804    The packets are always encrypted with the session key shared between
805    the client, this means these are not _to the channel_ but _to the client_
806    on the channel. */
807
808 void silc_server_packet_send_local_channel(SilcServer server,
809                                            SilcChannelEntry channel,
810                                            SilcPacketType type,
811                                            SilcPacketFlags flags,
812                                            unsigned char *data,
813                                            uint32 data_len,
814                                            int force_send)
815 {
816   SilcChannelClientEntry chl;
817   SilcHashTableList htl;
818   SilcSocketConnection sock = NULL;
819
820   SILC_LOG_DEBUG(("Start"));
821
822   /* Send the message to clients on the channel's client list. */
823   silc_hash_table_list(channel->user_list, &htl);
824   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
825     if (chl->client && !chl->client->router) {
826       sock = (SilcSocketConnection)chl->client->connection;
827
828       /* Send the packet to the client */
829       silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
830                                    SILC_ID_CLIENT, data, data_len,
831                                    force_send);
832     }
833   }
834 }
835
836 /* Routine used to send (relay, route) private messages to some destination.
837    If the private message key does not exist then the message is re-encrypted,
838    otherwise we just pass it along. This really is not used to send new
839    private messages (as server does not send them) but to relay received
840    private messages. */
841
842 void silc_server_send_private_message(SilcServer server,
843                                       SilcSocketConnection dst_sock,
844                                       SilcCipher cipher,
845                                       SilcHmac hmac,
846                                       SilcPacketContext *packet)
847 {
848   SilcBuffer buffer = packet->buffer;
849
850   /* Re-encrypt and send if private messge key does not exist */
851   if ((packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY) == FALSE) {
852
853     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
854                      + packet->dst_id_len + packet->padlen);
855     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
856     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
857     
858     /* Re-encrypt packet */
859     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, buffer->len);
860     
861     /* Send the packet */
862     silc_server_packet_send_real(server, dst_sock, FALSE);
863
864   } else {
865     /* Key exist so encrypt just header and send it */
866     silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
867                      + packet->dst_id_len + packet->padlen);
868     silc_packet_send_prepare(dst_sock, 0, 0, buffer->len);
869     silc_buffer_put(dst_sock->outbuf, buffer->data, buffer->len);
870
871     /* Encrypt header */
872     silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, 
873                         SILC_PACKET_HEADER_LEN + packet->src_id_len + 
874                         packet->dst_id_len + packet->padlen);
875
876     silc_server_packet_send_real(server, dst_sock, FALSE);
877   }
878 }
879
880 /* Sends current motd to client */
881
882 void silc_server_send_motd(SilcServer server,
883                            SilcSocketConnection sock)
884 {
885   char *motd;
886   uint32 motd_len;
887
888   if (server->config && server->config->motd && 
889       server->config->motd->motd_file) {
890
891     motd = silc_file_read(server->config->motd->motd_file, &motd_len);
892     if (!motd)
893       return;
894
895     silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
896                             motd, motd_len);
897     silc_free(motd);
898   }
899 }
900
901 /* Sends error message. Error messages may or may not have any 
902    implications. */
903
904 void silc_server_send_error(SilcServer server,
905                             SilcSocketConnection sock,
906                             const char *fmt, ...)
907 {
908   va_list ap;
909   unsigned char buf[4096];
910
911   memset(buf, 0, sizeof(buf));
912   va_start(ap, fmt);
913   vsprintf(buf, fmt, ap);
914   va_end(ap);
915
916   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
917                           buf, strlen(buf), FALSE);
918 }
919
920 /* Sends notify message. If format is TRUE the variable arguments are
921    formatted and the formatted string is sent as argument payload. If it is
922    FALSE then each argument is sent as separate argument and their format
923    in the argument list must be { argument data, argument length }. */
924
925 void silc_server_send_notify(SilcServer server,
926                              SilcSocketConnection sock,
927                              int broadcast,
928                              SilcNotifyType type,
929                              uint32 argc, ...)
930 {
931   va_list ap;
932   SilcBuffer packet;
933
934   va_start(ap, argc);
935
936   packet = silc_notify_payload_encode(type, argc, ap);
937   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 
938                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
939                           packet->data, packet->len, FALSE);
940   silc_buffer_free(packet);
941 }
942
943 /* Sends notify message and gets the arguments from the `args' Argument
944    Payloads. */
945
946 void silc_server_send_notify_args(SilcServer server,
947                                   SilcSocketConnection sock,
948                                   int broadcast,
949                                   SilcNotifyType type,
950                                   uint32 argc,
951                                   SilcBuffer args)
952 {
953   SilcBuffer packet;
954
955   packet = silc_notify_payload_encode_args(type, argc, args);
956   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 
957                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
958                           packet->data, packet->len, FALSE);
959   silc_buffer_free(packet);
960 }
961
962 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
963    `old_id' with the `new_id'. */
964
965 void silc_server_send_notify_channel_change(SilcServer server,
966                                             SilcSocketConnection sock,
967                                             int broadcast,
968                                             SilcChannelID *old_id,
969                                             SilcChannelID *new_id)
970 {
971   SilcBuffer idp1, idp2;
972
973   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
974   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
975
976   silc_server_send_notify(server, sock, broadcast,
977                           SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
978                           2, idp1->data, idp1->len, idp2->data, idp2->len);
979   silc_buffer_free(idp1);
980   silc_buffer_free(idp2);
981 }
982
983 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
984    `old_id' with the `new_id'. */
985
986 void silc_server_send_notify_nick_change(SilcServer server,
987                                          SilcSocketConnection sock,
988                                          int broadcast,
989                                          SilcClientID *old_id,
990                                          SilcClientID *new_id)
991 {
992   SilcBuffer idp1, idp2;
993
994   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
995   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
996
997   silc_server_send_notify(server, sock, broadcast, 
998                           SILC_NOTIFY_TYPE_NICK_CHANGE,
999                           2, idp1->data, idp1->len, idp2->data, idp2->len);
1000   silc_buffer_free(idp1);
1001   silc_buffer_free(idp2);
1002 }
1003
1004 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1005    has joined to the `channel'. */
1006
1007 void silc_server_send_notify_join(SilcServer server,
1008                                   SilcSocketConnection sock,
1009                                   int broadcast,
1010                                   SilcChannelEntry channel,
1011                                   SilcClientID *client_id)
1012 {
1013   SilcBuffer idp1, idp2;
1014
1015   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1016   idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1017   silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1018                           2, idp1->data, idp1->len,
1019                           idp2->data, idp2->len);
1020   silc_buffer_free(idp1);
1021   silc_buffer_free(idp2);
1022 }
1023
1024 /* Sends LEAVE notify type. This tells that `client_id' has left the
1025    `channel'. The Notify packet is always destined to the channel. */
1026
1027 void silc_server_send_notify_leave(SilcServer server,
1028                                    SilcSocketConnection sock,
1029                                    int broadcast,
1030                                    SilcChannelEntry channel,
1031                                    SilcClientID *client_id)
1032 {
1033   SilcBuffer idp;
1034
1035   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1036   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1037                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1038                                1, idp->data, idp->len);
1039   silc_buffer_free(idp);
1040 }
1041
1042 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1043    `channel' mode to `mode. The Notify packet is always destined to
1044    the channel. */
1045
1046 void silc_server_send_notify_cmode(SilcServer server,
1047                                    SilcSocketConnection sock,
1048                                    int broadcast,
1049                                    SilcChannelEntry channel,
1050                                    uint32 mode_mask,
1051                                    void *id, SilcIdType id_type,
1052                                    char *cipher, char *hmac)
1053 {
1054   SilcBuffer idp;
1055   unsigned char mode[4];
1056
1057   idp = silc_id_payload_encode((void *)id, id_type);
1058   SILC_PUT32_MSB(mode_mask, mode);
1059
1060   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1061                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1062                                4, idp->data, idp->len,
1063                                mode, 4,
1064                                cipher, cipher ? strlen(cipher) : 0,
1065                                hmac, hmac ? strlen(hmac) : 0);
1066   silc_buffer_free(idp);
1067 }
1068
1069 /* Sends CUMODE_CHANGE notify type. This tells that `client_id' changed the
1070    `target' client's mode on `channel'. The Notify packet is always
1071    destined to the channel. */
1072
1073 void silc_server_send_notify_cumode(SilcServer server,
1074                                     SilcSocketConnection sock,
1075                                     int broadcast,
1076                                     SilcChannelEntry channel,
1077                                     uint32 mode_mask,
1078                                     void *id, SilcIdType id_type,
1079                                     SilcClientID *target)
1080 {
1081   SilcBuffer idp1, idp2;
1082   unsigned char mode[4];
1083
1084   idp1 = silc_id_payload_encode((void *)id, id_type);
1085   idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1086   SILC_PUT32_MSB(mode_mask, mode);
1087
1088   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1089                                SILC_ID_CHANNEL, 
1090                                SILC_NOTIFY_TYPE_CUMODE_CHANGE, 3, 
1091                                idp1->data, idp1->len,
1092                                mode, 4,
1093                                idp2->data, idp2->len);
1094   silc_buffer_free(idp1);
1095   silc_buffer_free(idp2);
1096 }
1097
1098 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1099    left SILC network. This function is used only between server and router
1100    traffic. This is not used to send the notify to the channel for
1101    client. The `message may be NULL. */
1102
1103 void silc_server_send_notify_signoff(SilcServer server,
1104                                      SilcSocketConnection sock,
1105                                      int broadcast,
1106                                      SilcClientID *client_id,
1107                                      char *message)
1108 {
1109   SilcBuffer idp;
1110
1111   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1112   silc_server_send_notify(server, sock, broadcast,
1113                           SILC_NOTIFY_TYPE_SIGNOFF,
1114                           message ? 2 : 1, idp->data, idp->len,
1115                           message, message ? strlen(message): 0);
1116   silc_buffer_free(idp);
1117 }
1118
1119 /* Sends TOPIC_SET notify type. This tells that `client_id' changed
1120    the `channel's topic to `topic'. The Notify packet is always destined
1121    to the channel. This function is used to send the topic set notifies
1122    between routers. */
1123
1124 void silc_server_send_notify_topic_set(SilcServer server,
1125                                        SilcSocketConnection sock,
1126                                        int broadcast,
1127                                        SilcChannelEntry channel,
1128                                        SilcClientID *client_id,
1129                                        char *topic)
1130 {
1131   SilcBuffer idp;
1132
1133   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1134   silc_server_send_notify(server, sock, broadcast,
1135                           SILC_NOTIFY_TYPE_SERVER_SIGNOFF,
1136                           topic ? 2 : 1, 
1137                           idp->data, idp->len, 
1138                           topic, topic ? strlen(topic) : 0);
1139   silc_buffer_free(idp);
1140 }
1141
1142 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1143    was kicked off the channel.  The `comment' may indicate the reason
1144    for the kicking. This function is used only between server and router
1145    traffic. */
1146
1147 void silc_server_send_notify_kicked(SilcServer server,
1148                                     SilcSocketConnection sock,
1149                                     int broadcast,
1150                                     SilcChannelEntry channel,
1151                                     SilcClientID *client_id,
1152                                     char *comment)
1153 {
1154   SilcBuffer idp;
1155
1156   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1157   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1158                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED,
1159                                comment ? 2 : 1, idp->data, idp->len,
1160                                comment, comment ? strlen(comment) : 0);
1161   silc_buffer_free(idp);
1162 }
1163
1164 /* Send KILLED notify type. This tells that the `client_id' client was
1165    killed from the network.  The `comment' may indicate the reason
1166    for the killing. */
1167
1168 void silc_server_send_notify_killed(SilcServer server,
1169                                     SilcSocketConnection sock,
1170                                     int broadcast,
1171                                     SilcClientID *client_id,
1172                                     char *comment)
1173 {
1174   SilcBuffer idp;
1175
1176   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1177   silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1178                                SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1179                                comment ? 2 : 1, idp->data, idp->len,
1180                                comment, comment ? strlen(comment) : 0);
1181   silc_buffer_free(idp);
1182 }
1183
1184 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1185    user mode in the SILC Network was changed. This function is used to
1186    send the packet between routers as broadcast packet. */
1187
1188 void silc_server_send_notify_umode(SilcServer server,
1189                                    SilcSocketConnection sock,
1190                                    int broadcast,
1191                                    SilcClientID *client_id,
1192                                    uint32 mode_mask)
1193 {
1194   SilcBuffer idp;
1195   unsigned char mode[4];
1196
1197   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1198   SILC_PUT32_MSB(mode_mask, mode);
1199
1200   silc_server_send_notify(server, sock, broadcast,
1201                           SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1202                           idp->data, idp->len, 
1203                           mode, 4);
1204   silc_buffer_free(idp);
1205 }
1206
1207 /* Sends BAN notify type. This tells that ban has been either `add'ed
1208    or `del'eted on the `channel. This function is used to send the packet
1209    between routers as broadcast packet. */
1210
1211 void silc_server_send_notify_ban(SilcServer server,
1212                                  SilcSocketConnection sock,
1213                                  int broadcast,
1214                                  SilcChannelEntry channel,
1215                                  char *add, char *del)
1216 {
1217   SilcBuffer idp;
1218
1219   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1220   silc_server_send_notify(server, sock, broadcast,
1221                           SILC_NOTIFY_TYPE_BAN, 3,
1222                           idp->data, idp->len,
1223                           add, add ? strlen(add) : 0,
1224                           del, del ? strlen(del) : 0);
1225   silc_buffer_free(idp);
1226 }
1227
1228 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1229    or `del'eted on the `channel.  The sender of the invite is the `client_id'.
1230    This function is used to send the packet between routers as broadcast
1231    packet. */
1232
1233 void silc_server_send_notify_invite(SilcServer server,
1234                                     SilcSocketConnection sock,
1235                                     int broadcast,
1236                                     SilcChannelEntry channel,
1237                                     SilcClientID *client_id,
1238                                     char *add, char *del)
1239 {
1240   SilcBuffer idp, idp2;
1241
1242   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1243   idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1244   silc_server_send_notify(server, sock, broadcast,
1245                           SILC_NOTIFY_TYPE_INVITE, 5,
1246                           idp->data, idp->len,
1247                           channel->channel_name, strlen(channel->channel_name),
1248                           idp2->data, idp2->len,
1249                           add, add ? strlen(add) : 0,
1250                           del, del ? strlen(del) : 0);
1251   silc_buffer_free(idp);
1252   silc_buffer_free(idp2);
1253 }
1254
1255 /* Sends notify message destined to specific entity. */
1256
1257 void silc_server_send_notify_dest(SilcServer server,
1258                                   SilcSocketConnection sock,
1259                                   int broadcast,
1260                                   void *dest_id,
1261                                   SilcIdType dest_id_type,
1262                                   SilcNotifyType type,
1263                                   uint32 argc, ...)
1264 {
1265   va_list ap;
1266   SilcBuffer packet;
1267
1268   va_start(ap, argc);
1269
1270   packet = silc_notify_payload_encode(type, argc, ap);
1271   silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 0, 
1272                                dest_id, dest_id_type,
1273                                packet->data, packet->len, FALSE);
1274   silc_buffer_free(packet);
1275 }
1276
1277 /* Sends notify message to a channel. The notify message sent is 
1278    distributed to all clients on the channel. If `route_notify' is TRUE
1279    then the notify may be routed to primary route or to some other routers.
1280    If FALSE it is assured that the notify is sent only locally. If `sender'
1281    is provided then the packet is not sent to that connection since it
1282    originally came from it. */
1283
1284 void silc_server_send_notify_to_channel(SilcServer server,
1285                                         SilcSocketConnection sender,
1286                                         SilcChannelEntry channel,
1287                                         unsigned char route_notify,
1288                                         SilcNotifyType type,
1289                                         uint32 argc, ...)
1290 {
1291   va_list ap;
1292   SilcBuffer packet;
1293
1294   va_start(ap, argc);
1295
1296   packet = silc_notify_payload_encode(type, argc, ap);
1297   silc_server_packet_send_to_channel(server, sender, channel, 
1298                                      SILC_PACKET_NOTIFY, route_notify,
1299                                      packet->data, packet->len, FALSE);
1300   silc_buffer_free(packet);
1301 }
1302
1303 /* Send notify message to all channels the client has joined. It is quaranteed
1304    that the message is sent only once to a client (ie. if a client is joined
1305    on two same channel it will receive only one notify message). Also, this
1306    sends only to local clients (locally connected if we are server, and to
1307    local servers if we are router). If `sender' is provided the packet is
1308    not sent to that client at all. */
1309
1310 void silc_server_send_notify_on_channels(SilcServer server,
1311                                          SilcClientEntry sender,
1312                                          SilcClientEntry client,
1313                                          SilcNotifyType type,
1314                                          uint32 argc, ...)
1315 {
1316   int k;
1317   SilcSocketConnection sock = NULL;
1318   SilcPacketContext packetdata;
1319   SilcClientEntry c;
1320   SilcClientEntry *sent_clients = NULL;
1321   uint32 sent_clients_count = 0;
1322   SilcServerEntry *routed = NULL;
1323   uint32 routed_count = 0;
1324   SilcHashTableList htl, htl2;
1325   SilcChannelEntry channel;
1326   SilcChannelClientEntry chl, chl2;
1327   SilcIDListData idata;
1328   SilcBuffer packet;
1329   unsigned char *data;
1330   uint32 data_len;
1331   int force_send = FALSE;
1332   va_list ap;
1333
1334   SILC_LOG_DEBUG(("Start"));
1335
1336   if (!silc_hash_table_count(client->channels))
1337     return;
1338
1339   va_start(ap, argc);
1340   packet = silc_notify_payload_encode(type, argc, ap);
1341   data = packet->data;
1342   data_len = packet->len;
1343
1344   /* Set the packet context pointers. */
1345   packetdata.flags = 0;
1346   packetdata.type = SILC_PACKET_NOTIFY;
1347   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1348   packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1349   packetdata.src_id_type = SILC_ID_SERVER;
1350
1351   silc_hash_table_list(client->channels, &htl);
1352   while (silc_hash_table_get(&htl, NULL, (void *)&chl)) {
1353     channel = chl->channel;
1354
1355     /* Send the message to all clients on the channel's client list. */
1356     silc_hash_table_list(channel->user_list, &htl2);
1357     while (silc_hash_table_get(&htl2, NULL, (void *)&chl2)) {
1358       c = chl2->client;
1359       
1360       if (sender && c == sender)
1361         continue;
1362
1363       /* Check if we have sent the packet to this client already */
1364       for (k = 0; k < sent_clients_count; k++)
1365         if (sent_clients[k] == c)
1366           break;
1367       if (k < sent_clients_count)
1368         continue;
1369
1370       /* If we are router and if this client has router set it is not
1371          locally connected client and we will route the message to the
1372          router set in the client. */
1373       if (c && c->router && server->server_type == SILC_ROUTER) {
1374         /* Check if we have sent the packet to this route already */
1375         for (k = 0; k < routed_count; k++)
1376           if (routed[k] == c->router)
1377             break;
1378         if (k < routed_count)
1379           continue;
1380         
1381         /* Get data used in packet header encryption, keys and stuff. */
1382         sock = (SilcSocketConnection)c->router->connection;
1383         idata = (SilcIDListData)c->router;
1384         
1385         packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1386         packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1387         packetdata.dst_id_type = SILC_ID_SERVER;
1388         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
1389           packetdata.src_id_len + packetdata.dst_id_len;
1390         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1391
1392         /* Send the packet */
1393         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1394                                                 idata->send_key, 
1395                                                 idata->hmac_send, 
1396                                                 data, data_len, FALSE, 
1397                                                 force_send);
1398         
1399         silc_free(packetdata.dst_id);
1400
1401         /* We want to make sure that the packet is routed to same router
1402            only once. Mark this route as sent route. */
1403         k = routed_count;
1404         routed = silc_realloc(routed, sizeof(*routed) * (k + 1));
1405         routed[k] = c->router;
1406         routed_count++;
1407
1408         continue;
1409       }
1410
1411       if (c && c->router)
1412         continue;
1413
1414       /* Send to locally connected client */
1415       if (c) {
1416         
1417         /* Get data used in packet header encryption, keys and stuff. */
1418         sock = (SilcSocketConnection)c->connection;
1419         idata = (SilcIDListData)c;
1420         
1421         packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1422         packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1423         packetdata.dst_id_type = SILC_ID_CLIENT;
1424         packetdata.truelen = data_len + SILC_PACKET_HEADER_LEN + 
1425           packetdata.src_id_len + packetdata.dst_id_len;
1426         packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen);
1427
1428         /* Send the packet */
1429         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1430                                                 idata->send_key, 
1431                                                 idata->hmac_send, 
1432                                                 data, data_len, FALSE, 
1433                                                 force_send);
1434
1435         silc_free(packetdata.dst_id);
1436
1437         /* Make sure that we send the notify only once per client. */
1438         sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) * 
1439                                     (sent_clients_count + 1));
1440         sent_clients[sent_clients_count] = c;
1441         sent_clients_count++;
1442       }
1443     }
1444   }
1445
1446   if (routed_count)
1447     silc_free(routed);
1448   if (sent_clients_count)
1449     silc_free(sent_clients);
1450   silc_free(packetdata.src_id);
1451 }
1452
1453 /* Sends New ID Payload to remote end. The packet is used to distribute
1454    information about new registered clients, servers, channel etc. usually
1455    to routers so that they can keep these information up to date. 
1456    If the argument `broadcast' is TRUE then the packet is sent as
1457    broadcast packet. */
1458
1459 void silc_server_send_new_id(SilcServer server,
1460                              SilcSocketConnection sock,
1461                              int broadcast,
1462                              void *id, SilcIdType id_type, 
1463                              uint32 id_len)
1464 {
1465   SilcBuffer idp;
1466
1467   SILC_LOG_DEBUG(("Start"));
1468
1469   idp = silc_id_payload_encode(id, id_type);
1470   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 
1471                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1472                           idp->data, idp->len, FALSE);
1473   silc_buffer_free(idp);
1474 }
1475
1476 /* Send New Channel Payload to notify about newly created channel in the
1477    SILC network. Normal server nevers sends this packet. Router uses this
1478    to notify other routers in the network about new channel. This packet
1479    is broadcasted. */
1480
1481 void silc_server_send_new_channel(SilcServer server,
1482                                   SilcSocketConnection sock,
1483                                   int broadcast,
1484                                   char *channel_name,
1485                                   void *channel_id, 
1486                                   uint32 channel_id_len,
1487                                   uint32 mode)
1488 {
1489   SilcBuffer packet;
1490   unsigned char *cid;
1491   uint32 name_len = strlen(channel_name);
1492
1493   SILC_LOG_DEBUG(("Start"));
1494
1495   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1496   if (!cid)
1497     return;
1498
1499   /* Encode the channel payload */
1500   packet = silc_channel_payload_encode(channel_name, name_len,
1501                                        cid, channel_id_len, mode);
1502
1503   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1504                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1505                           packet->data, packet->len, FALSE);
1506
1507   silc_free(cid);
1508   silc_buffer_free(packet);
1509 }
1510
1511 /* Send Channel Key payload to distribute the new channel key. Normal server
1512    sends this to router when new client joins to existing channel. Router
1513    sends this to the local server who sent the join command in case where
1514    the channel did not exist yet. Both normal and router servers uses this
1515    also to send this to locally connected clients on the channel. This
1516    must not be broadcasted packet. Routers do not send this to each other. 
1517    If `sender is provided then the packet is not sent to that connection since
1518    it originally came from it. */
1519
1520 void silc_server_send_channel_key(SilcServer server,
1521                                   SilcSocketConnection sender,
1522                                   SilcChannelEntry channel,
1523                                   unsigned char route)
1524 {
1525   SilcBuffer packet;
1526   unsigned char *chid;
1527   uint32 tmp_len;
1528  
1529   SILC_LOG_DEBUG(("Start"));
1530  
1531   chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1532   if (!chid)
1533     return;
1534  
1535   /* Encode channel key packet */
1536   tmp_len = strlen(channel->channel_key->cipher->name);
1537   packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1538                                                            SILC_ID_CHANNEL),
1539                                            chid, tmp_len,
1540                                            channel->channel_key->cipher->name,
1541                                            channel->key_len / 8, channel->key);
1542  
1543   silc_server_packet_send_to_channel(server, sender, channel, 
1544                                      SILC_PACKET_CHANNEL_KEY,
1545                                      route, packet->data, packet->len, FALSE);
1546   silc_buffer_free(packet);
1547   silc_free(chid);
1548 }
1549
1550 /* Generic function to send any command. The arguments must be sent already
1551    encoded into correct form in correct order. */
1552
1553 void silc_server_send_command(SilcServer server, 
1554                               SilcSocketConnection sock,
1555                               SilcCommand command, 
1556                               uint32 argc, ...)
1557 {
1558   SilcBuffer packet;
1559   va_list ap;
1560
1561   va_start(ap, argc);
1562
1563   packet = silc_command_payload_encode_vap(command, 0, argc, ap);
1564   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1565                           packet->data, packet->len, TRUE);
1566   silc_buffer_free(packet);
1567 }
1568
1569 /* Send the heartbeat packet. */
1570
1571 void silc_server_send_heartbeat(SilcServer server,
1572                                 SilcSocketConnection sock)
1573 {
1574   silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1575                           NULL, 0, FALSE);
1576 }
1577
1578 /* Generic function to relay packet we've received. This is used to relay
1579    packets to a client but generally can be used to other purposes as well. */
1580
1581 void silc_server_relay_packet(SilcServer server,
1582                               SilcSocketConnection dst_sock,
1583                               SilcCipher cipher,
1584                               SilcHmac hmac,
1585                               SilcPacketContext *packet,
1586                               int force_send)
1587 {
1588   silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1589                    + packet->dst_id_len + packet->padlen);
1590
1591   silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len);
1592   silc_buffer_put(dst_sock->outbuf, packet->buffer->data, packet->buffer->len);
1593   
1594   /* Re-encrypt packet */
1595   silc_packet_encrypt(cipher, hmac, dst_sock->outbuf, packet->buffer->len);
1596   
1597   /* Send the packet */
1598   silc_server_packet_send_real(server, dst_sock, force_send);
1599
1600   silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1601                    + packet->dst_id_len + packet->padlen);
1602 }
1603
1604 /* Routine used to send the connection authentication packet. */
1605
1606 void silc_server_send_connection_auth_request(SilcServer server,
1607                                               SilcSocketConnection sock,
1608                                               uint16 conn_type,
1609                                               SilcAuthMethod auth_meth)
1610 {
1611   SilcBuffer packet;
1612
1613   packet = silc_buffer_alloc(4);
1614   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1615   silc_buffer_format(packet,
1616                      SILC_STR_UI_SHORT(conn_type),
1617                      SILC_STR_UI_SHORT(auth_meth),
1618                      SILC_STR_END);
1619
1620   silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1621                           0, packet->data, packet->len, FALSE);
1622   silc_buffer_free(packet);
1623 }