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