Send CMODE_CHANGE and CUMODE_CHANGE notifys to the original
[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 packet to channel %s", channel->channel_name));
600
601   routed = silc_calloc(silc_hash_table_count(channel->user_list), 
602                        sizeof(*routed));
603
604   /* Send the message to clients on the channel's client list. */
605   silc_hash_table_list(channel->user_list, &htl);
606   while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
607     client = chl->client;
608     if (!client)
609       continue;
610
611     /* If client has router set it is not locally connected client and
612        we will route the message to the router set in the client. Though,
613        send locally connected server in all cases. */
614     if (server->server_type == SILC_ROUTER && client->router && 
615         ((!route && client->router->router == server->id_entry) || route)) {
616
617       /* Check if we have sent the packet to this route already */
618       for (k = 0; k < routed_count; k++)
619         if (routed[k] == client->router)
620           break;
621       if (k < routed_count)
622         continue;
623
624       /* Get data used in packet header encryption, keys and stuff. */
625       sock = (SilcSocketConnection)client->router->connection;
626       idata = (SilcIDListData)client->router;
627
628       if (sender && sock == sender)
629         continue;
630
631       /* Route only once to router. Protocol prohibits sending channel
632          messages to more than one router. */
633       if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
634         if (gone)
635           continue;
636         gone = TRUE;
637       }
638
639       /* Send the packet */
640       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
641                                               idata->send_key, 
642                                               idata->hmac_send, 
643                                               idata->psn_send++,
644                                               data, data_len, FALSE, 
645                                               force_send);
646
647       /* Mark this route routed already */
648       routed[routed_count++] = client->router;
649       continue;
650     }
651
652     if (client->router)
653       continue;
654
655     /* Send to locally connected client */
656
657     /* Get data used in packet header encryption, keys and stuff. */
658     sock = (SilcSocketConnection)client->connection;
659     idata = (SilcIDListData)client;
660     
661     if (!sock || (sender && sock == sender))
662       continue;
663
664     /* Send the packet */
665     silc_server_packet_send_to_channel_real(server, sock, &packetdata,
666                                             idata->send_key, 
667                                             idata->hmac_send, 
668                                             idata->psn_send++, 
669                                             data, data_len, FALSE, 
670                                             force_send);
671   }
672   silc_hash_table_list_reset(&htl);
673
674  out:
675   silc_free(routed);
676   silc_free(packetdata.src_id);
677   silc_free(packetdata.dst_id);
678 }
679
680 /* This checks whether the relayed packet came from router. If it did
681    then we'll need to encrypt it with the channel key. This is called
682    from the silc_server_packet_relay_to_channel. */
683
684 static bool
685 silc_server_packet_relay_to_channel_encrypt(SilcServer server,
686                                             SilcSocketConnection sock,
687                                             SilcChannelEntry channel,
688                                             unsigned char *data,
689                                             unsigned int data_len)
690 {
691   /* If we are router and the packet came from router and private key
692      has not been set for the channel then we must encrypt the packet
693      as it was decrypted with the session key shared between us and the
694      router which sent it. This is so, because cells does not share the
695      same channel key. */
696   if (server->server_type == SILC_ROUTER &&
697       sock->type == SILC_SOCKET_TYPE_ROUTER &&
698       !(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) &&
699       channel->channel_key) {
700     SilcUInt32 mac_len = silc_hmac_len(channel->hmac);
701     SilcUInt32 iv_len = silc_cipher_get_block_len(channel->channel_key);
702     unsigned char iv[SILC_CIPHER_MAX_IV_SIZE];
703
704     if (data_len <= mac_len + iv_len) {
705       SILC_LOG_WARNING(("Corrupted channel message, cannot relay it"));
706       return FALSE;
707     }
708
709     memcpy(iv, data + (data_len - iv_len), iv_len);
710     silc_channel_message_payload_encrypt(data, data_len - iv_len - mac_len,
711                                          data_len, iv, iv_len,
712                                          channel->channel_key, channel->hmac);
713   }
714
715   return TRUE;
716 }
717
718 /* This routine is explicitly used to relay messages to some channel.
719    Packets sent with this function we have received earlier and are
720    totally encrypted. This just sends the packet to all clients on
721    the channel. If the sender of the packet is someone on the channel 
722    the message will not be sent to that client. The SILC Packet header
723    is encrypted with the session key shared between us and the client.
724    MAC is also computed before encrypting the header. Rest of the
725    packet will be untouched. */
726
727 void silc_server_packet_relay_to_channel(SilcServer server,
728                                          SilcSocketConnection sender_sock,
729                                          SilcChannelEntry channel,
730                                          void *sender_id,
731                                          SilcIdType sender_type,
732                                          SilcClientEntry sender_entry,
733                                          unsigned char *data,
734                                          SilcUInt32 data_len,
735                                          bool force_send)
736 {
737   SilcSocketConnection sock = NULL;
738   SilcPacketContext packetdata;
739   SilcClientEntry client = NULL;
740   SilcServerEntry *routed = NULL;
741   SilcChannelClientEntry chl, chl_sender;
742   SilcUInt32 routed_count = 0;
743   SilcIDListData idata;
744   SilcHashTableList htl;
745   bool gone = FALSE;
746   int k;
747
748   SILC_LOG_DEBUG(("Relaying packet to channel"));
749
750   if (!silc_server_client_on_channel(sender_entry, channel, &chl_sender))
751     return;
752
753   /* This encrypts the packet, if needed. It will be encrypted if
754      it came from the router thus it needs to be encrypted with the
755      channel key. If the channel key does not exist, then we know we
756      don't have a single local user on the channel. */
757   if (!silc_server_packet_relay_to_channel_encrypt(server, sender_sock,
758                                                    channel, data,
759                                                    data_len))
760     return;
761
762   /* Set the packet context pointers. */
763   packetdata.flags = 0;
764   packetdata.type = SILC_PACKET_CHANNEL_MESSAGE;
765   packetdata.src_id = silc_id_id2str(sender_id, sender_type);
766   packetdata.src_id_len = silc_id_get_len(sender_id, sender_type);
767   packetdata.src_id_type = sender_type;
768   packetdata.dst_id = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
769   packetdata.dst_id_len = silc_id_get_len(channel->id, SILC_ID_CHANNEL);
770   packetdata.dst_id_type = SILC_ID_CHANNEL;
771
772   /* If there are global users in the channel we will send the message
773      first to our router for further routing. */
774   if (server->server_type != SILC_ROUTER && !server->standalone &&
775       channel->global_users) {
776     SilcServerEntry router = server->router;
777
778     /* Check that the sender is not our router. */
779     if (sender_sock != (SilcSocketConnection)router->connection) {
780
781       /* Get data used in packet header encryption, keys and stuff. */
782       sock = (SilcSocketConnection)router->connection;
783       idata = (SilcIDListData)router;
784
785       SILC_LOG_DEBUG(("Sending channel message to router for routing"));
786
787       silc_server_packet_send_to_channel_real(server, sock, &packetdata,
788                                               idata->send_key, 
789                                               idata->hmac_send, 
790                                               idata->psn_send++, 
791                                               data, data_len, TRUE, 
792                                               force_send);
793     }
794   }
795
796   routed = silc_calloc(silc_hash_table_count(channel->user_list), 
797                        sizeof(*routed));
798
799   /* Assure we won't route the message back to the sender's way. */
800   if (sender_entry->router)
801     routed[routed_count++] = sender_entry->router;
802
803   /* Send the message to clients on the channel's client list. */
804   silc_hash_table_list(channel->user_list, &htl);
805   while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
806     client = chl->client;
807     if (!client || client == sender_entry)
808       continue;
809
810     /* Check whether message sending is blocked */
811     if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES)
812       continue;
813     if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_USERS &&
814         !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANOP) &&
815         !(chl_sender->mode & SILC_CHANNEL_UMODE_CHANFO))
816       continue;
817     if (chl->mode & SILC_CHANNEL_UMODE_BLOCK_MESSAGES_ROBOTS &&
818         sender_entry->mode & SILC_UMODE_ROBOT)
819       continue;
820
821     /* If the client has set router it means that it is not locally
822        connected client and we will route the packet further. */
823     if (server->server_type == SILC_ROUTER && client->router) {
824
825       /* Check if we have sent the packet to this route already */
826       for (k = 0; k < routed_count; k++)
827         if (routed[k] == client->router)
828           break;
829       if (k < routed_count)
830         continue;
831
832       /* Get data used in packet header encryption, keys and stuff. */
833       sock = (SilcSocketConnection)client->router->connection;
834       idata = (SilcIDListData)client->router;
835
836       /* Check if the sender socket is the same as this client's router
837          socket. */
838       if (sender_sock && sock == sender_sock)
839         continue;
840
841       SILC_LOG_DEBUG(("Relaying packet to client ID(%s) %s (%s)", 
842                       silc_id_render(client->id, SILC_ID_CLIENT),
843                       sock->hostname, sock->ip));
844
845       /* Mark this route routed already. */
846       routed[routed_count++] = client->router;
847         
848       if (sock->type == SILC_SOCKET_TYPE_ROUTER) {
849         /* The remote connection is router then we'll decrypt the
850            channel message and re-encrypt it with the session key shared
851            between us and the remote router. This is done because the
852            channel keys are cell specific and we have different channel
853            key than the remote router has. */
854
855         /* Route only once to router. Protocol prohibits sending channel
856            messages to more than one router. */
857         if (gone)
858           continue;
859         gone = TRUE;
860
861         SILC_LOG_DEBUG(("Remote is router, encrypt with session key"));
862
863         /* If private key mode is not set then decrypt the packet
864            and re-encrypt it */
865         if (!(channel->mode & SILC_CHANNEL_MODE_PRIVKEY) && 
866             channel->channel_key) {
867           unsigned char tmp[SILC_PACKET_MAX_LEN];
868
869           if (data_len > SILC_PACKET_MAX_LEN)
870             data_len = SILC_PACKET_MAX_LEN;
871           memcpy(tmp, data, data_len);
872
873           /* Decrypt the channel message (we don't check the MAC) */
874           silc_channel_message_payload_decrypt(tmp, data_len,
875                                                channel->channel_key, NULL);
876
877           /* Now re-encrypt and send it to the router */
878           silc_server_packet_send_srcdest(server, sock,
879                                           SILC_PACKET_CHANNEL_MESSAGE, 0,
880                                           sender_id, sender_type,
881                                           channel->id, SILC_ID_CHANNEL,
882                                           tmp, data_len, force_send);
883         } else {
884           /* Private key mode is set, we don't have the channel key, so
885              just re-encrypt the entire packet and send it to the router. */
886           silc_server_packet_send_srcdest(server, sock,
887                                           SILC_PACKET_CHANNEL_MESSAGE, 0,
888                                           sender_id, sender_type,
889                                           channel->id, SILC_ID_CHANNEL,
890                                           data, data_len, force_send);
891         }
892       } else {
893         /* Send the packet to normal server */
894         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
895                                                 idata->send_key,
896                                                 idata->hmac_send,
897                                                 idata->psn_send++,
898                                                 data, data_len, TRUE,
899                                                 force_send);
900       }
901
902       continue;
903     }
904
905     if (client->router)
906       continue;
907
908     /* Get data used in packet header encryption, keys and stuff. */
909     sock = (SilcSocketConnection)client->connection;
910     idata = (SilcIDListData)client;
911
912     if (!sock || (sender_sock && sock == sender_sock))
913       continue;
914
915     SILC_LOG_DEBUG(("Sending packet to client ID(%s) %s (%s)", 
916                     silc_id_render(client->id, SILC_ID_CLIENT),
917                     sock->hostname, sock->ip));
918
919     /* Send the packet */
920     silc_server_packet_send_to_channel_real(server, sock, &packetdata,
921                                             idata->send_key, 
922                                             idata->hmac_send, 
923                                             idata->psn_send++, 
924                                             data, data_len, TRUE, 
925                                             force_send);
926   }
927
928   silc_hash_table_list_reset(&htl);
929   silc_free(routed);
930   silc_free(packetdata.src_id);
931   silc_free(packetdata.dst_id);
932 }
933
934 /* This function is used to send packets strictly to all local clients
935    on a particular channel.  This is used for example to distribute new
936    channel key to all our locally connected clients on the channel. 
937    The packets are always encrypted with the session key shared between
938    the client, this means these are not _to the channel_ but _to the client_
939    on the channel. */
940
941 void silc_server_packet_send_local_channel(SilcServer server,
942                                            SilcChannelEntry channel,
943                                            SilcPacketType type,
944                                            SilcPacketFlags flags,
945                                            unsigned char *data,
946                                            SilcUInt32 data_len,
947                                            bool force_send)
948 {
949   SilcChannelClientEntry chl;
950   SilcHashTableList htl;
951   SilcSocketConnection sock = NULL;
952
953   SILC_LOG_DEBUG(("Start"));
954
955   /* Send the message to clients on the channel's client list. */
956   silc_hash_table_list(channel->user_list, &htl);
957   while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
958     if (chl->client && !chl->client->router) {
959       sock = (SilcSocketConnection)chl->client->connection;
960
961       /* Send the packet to the client */
962       silc_server_packet_send_dest(server, sock, type, flags, chl->client->id,
963                                    SILC_ID_CLIENT, data, data_len,
964                                    force_send);
965     }
966   }
967   silc_hash_table_list_reset(&htl);
968 }
969
970 /* Routine used to send (relay, route) private messages to some destination.
971    If the private message key does not exist then the message is re-encrypted,
972    otherwise we just pass it along. This really is not used to send new
973    private messages (as server does not send them) but to relay received
974    private messages. */
975
976 void silc_server_send_private_message(SilcServer server,
977                                       SilcSocketConnection dst_sock,
978                                       SilcCipher cipher,
979                                       SilcHmac hmac,
980                                       SilcUInt32 sequence,
981                                       SilcPacketContext *packet)
982 {
983   SilcBuffer buffer = packet->buffer;
984   const SilcBufferStruct p;
985
986   silc_buffer_push(buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
987                    + packet->dst_id_len + packet->padlen);
988   if (!silc_packet_send_prepare(dst_sock, 0, 0, buffer->len, hmac,
989                                 (const SilcBuffer)&p)) {
990     SILC_LOG_ERROR(("Cannot send packet"));
991     return;
992   }
993   silc_buffer_put((SilcBuffer)&p, buffer->data, buffer->len);
994
995   /* Re-encrypt and send if private messge key does not exist */
996   if (!(packet->flags & SILC_PACKET_FLAG_PRIVMSG_KEY)) {
997     /* Re-encrypt packet */
998     silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, buffer->len);
999   } else {
1000     /* Key exist so encrypt just header and send it */
1001     silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, 
1002                         SILC_PACKET_HEADER_LEN + packet->src_id_len + 
1003                         packet->dst_id_len + packet->padlen);
1004   }
1005
1006   /* Send the packet */
1007   silc_server_packet_send_real(server, dst_sock, FALSE);
1008 }
1009
1010 /* Sends current motd to client */
1011
1012 void silc_server_send_motd(SilcServer server,
1013                            SilcSocketConnection sock)
1014 {
1015   char *motd, *motd_file = NULL;
1016   SilcUInt32 motd_len;
1017
1018   if (server->config)
1019     motd_file = server->config->server_info->motd_file;
1020
1021   if (motd_file) {
1022     motd = silc_file_readfile(motd_file, &motd_len);
1023     if (!motd)
1024       return;
1025
1026     silc_server_send_notify(server, sock, FALSE, SILC_NOTIFY_TYPE_MOTD, 1,
1027                             motd, motd_len);
1028     silc_free(motd);
1029   }
1030 }
1031
1032 /* Sends error message. Error messages may or may not have any 
1033    implications. */
1034
1035 void silc_server_send_error(SilcServer server,
1036                             SilcSocketConnection sock,
1037                             const char *fmt, ...)
1038 {
1039   va_list ap;
1040   unsigned char buf[4096];
1041
1042   memset(buf, 0, sizeof(buf));
1043   va_start(ap, fmt);
1044   vsprintf(buf, fmt, ap);
1045   va_end(ap);
1046
1047   silc_server_packet_send(server, sock, SILC_PACKET_ERROR, 0, 
1048                           buf, strlen(buf), FALSE);
1049 }
1050
1051 /* Sends notify message. If format is TRUE the variable arguments are
1052    formatted and the formatted string is sent as argument payload. If it is
1053    FALSE then each argument is sent as separate argument and their format
1054    in the argument list must be { argument data, argument length }. */
1055
1056 void silc_server_send_notify(SilcServer server,
1057                              SilcSocketConnection sock,
1058                              bool broadcast,
1059                              SilcNotifyType type,
1060                              SilcUInt32 argc, ...)
1061 {
1062   va_list ap;
1063   SilcBuffer packet;
1064
1065   va_start(ap, argc);
1066
1067   packet = silc_notify_payload_encode(type, argc, ap);
1068   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 
1069                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1070                           packet->data, packet->len, FALSE);
1071
1072   /* Send to backup routers if this is being broadcasted to primary
1073      router.  The silc_server_backup_send checks further whether to
1074      actually send it or not. */
1075   if ((broadcast && sock && sock == SILC_PRIMARY_ROUTE(server)) ||
1076       (broadcast && !sock && !SILC_PRIMARY_ROUTE(server)))
1077     silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
1078                             packet->data, packet->len, FALSE, TRUE);
1079
1080   silc_buffer_free(packet);
1081   va_end(ap);
1082 }
1083
1084 /* Sends notify message and gets the arguments from the `args' Argument
1085    Payloads. */
1086
1087 void silc_server_send_notify_args(SilcServer server,
1088                                   SilcSocketConnection sock,
1089                                   bool broadcast,
1090                                   SilcNotifyType type,
1091                                   SilcUInt32 argc,
1092                                   SilcBuffer args)
1093 {
1094   SilcBuffer packet;
1095
1096   packet = silc_notify_payload_encode_args(type, argc, args);
1097   silc_server_packet_send(server, sock, SILC_PACKET_NOTIFY, 
1098                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1099                           packet->data, packet->len, FALSE);
1100   silc_buffer_free(packet);
1101 }
1102
1103 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
1104    `old_id' with the `new_id'. */
1105
1106 void silc_server_send_notify_channel_change(SilcServer server,
1107                                             SilcSocketConnection sock,
1108                                             bool broadcast,
1109                                             SilcChannelID *old_id,
1110                                             SilcChannelID *new_id)
1111 {
1112   SilcBuffer idp1, idp2;
1113
1114   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
1115   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
1116
1117   silc_server_send_notify(server, sock, broadcast,
1118                           SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
1119                           2, idp1->data, idp1->len, idp2->data, idp2->len);
1120   silc_buffer_free(idp1);
1121   silc_buffer_free(idp2);
1122 }
1123
1124 /* Send NICK_CHANGE notify type. This tells the receiver to replace the
1125    `old_id' with the `new_id'. */
1126
1127 void silc_server_send_notify_nick_change(SilcServer server,
1128                                          SilcSocketConnection sock,
1129                                          bool broadcast,
1130                                          SilcClientID *old_id,
1131                                          SilcClientID *new_id,
1132                                          const char *nickname)
1133 {
1134   SilcBuffer idp1, idp2;
1135
1136   idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CLIENT);
1137   idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CLIENT);
1138
1139   silc_server_send_notify(server, sock, broadcast, 
1140                           SILC_NOTIFY_TYPE_NICK_CHANGE,
1141                           3, idp1->data, idp1->len, idp2->data, idp2->len,
1142                           nickname, nickname ? strlen(nickname) : 0);
1143   silc_buffer_free(idp1);
1144   silc_buffer_free(idp2);
1145 }
1146
1147 /* Sends JOIN notify type. This tells that new client by `client_id' ID
1148    has joined to the `channel'. */
1149
1150 void silc_server_send_notify_join(SilcServer server,
1151                                   SilcSocketConnection sock,
1152                                   bool broadcast,
1153                                   SilcChannelEntry channel,
1154                                   SilcClientID *client_id)
1155 {
1156   SilcBuffer idp1, idp2;
1157
1158   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1159   idp2 = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1160   silc_server_send_notify(server, sock, broadcast, SILC_NOTIFY_TYPE_JOIN,
1161                           2, idp1->data, idp1->len,
1162                           idp2->data, idp2->len);
1163   silc_buffer_free(idp1);
1164   silc_buffer_free(idp2);
1165 }
1166
1167 /* Sends LEAVE notify type. This tells that `client_id' has left the
1168    `channel'. The Notify packet is always destined to the channel. */
1169
1170 void silc_server_send_notify_leave(SilcServer server,
1171                                    SilcSocketConnection sock,
1172                                    bool broadcast,
1173                                    SilcChannelEntry channel,
1174                                    SilcClientID *client_id)
1175 {
1176   SilcBuffer idp;
1177
1178   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1179   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1180                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_LEAVE,
1181                                1, idp->data, idp->len);
1182   silc_buffer_free(idp);
1183 }
1184
1185 /* Sends CMODE_CHANGE notify type. This tells that `client_id' changed the
1186    `channel' mode to `mode. The Notify packet is always destined to
1187    the channel. */
1188
1189 void silc_server_send_notify_cmode(SilcServer server,
1190                                    SilcSocketConnection sock,
1191                                    bool broadcast,
1192                                    SilcChannelEntry channel,
1193                                    SilcUInt32 mode_mask,
1194                                    void *id, SilcIdType id_type,
1195                                    const char *cipher, const char *hmac,
1196                                    const char *passphrase,
1197                                    SilcPublicKey founder_key)
1198 {
1199   SilcBuffer idp;
1200   unsigned char mode[4], *key = NULL;
1201   SilcUInt32 key_len = 0;
1202
1203   idp = silc_id_payload_encode((void *)id, id_type);
1204   SILC_PUT32_MSB(mode_mask, mode);
1205   if (founder_key)
1206     key = silc_pkcs_public_key_encode(founder_key, &key_len);
1207
1208   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1209                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_CMODE_CHANGE,
1210                                6, idp->data, idp->len,
1211                                mode, 4,
1212                                cipher, cipher ? strlen(cipher) : 0,
1213                                hmac, hmac ? strlen(hmac) : 0,
1214                                passphrase, passphrase ? 
1215                                strlen(passphrase) : 0,
1216                                key, key_len);
1217   silc_free(key);
1218   silc_buffer_free(idp);
1219 }
1220
1221 /* Sends CUMODE_CHANGE notify type. This tells that `id' changed the
1222    `target' client's mode on `channel'. The notify packet is always
1223    destined to the channel. */
1224
1225 void silc_server_send_notify_cumode(SilcServer server,
1226                                     SilcSocketConnection sock,
1227                                     bool broadcast,
1228                                     SilcChannelEntry channel,
1229                                     SilcUInt32 mode_mask,
1230                                     void *id, SilcIdType id_type,
1231                                     SilcClientID *target,
1232                                     SilcPublicKey founder_key)
1233 {
1234   SilcBuffer idp1, idp2;
1235   unsigned char mode[4], *key = NULL;
1236   SilcUInt32 key_len = 0;
1237
1238   idp1 = silc_id_payload_encode((void *)id, id_type);
1239   idp2 = silc_id_payload_encode((void *)target, SILC_ID_CLIENT);
1240   SILC_PUT32_MSB(mode_mask, mode);
1241   if (founder_key)
1242     key = silc_pkcs_public_key_encode(founder_key, &key_len);
1243
1244   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1245                                SILC_ID_CHANNEL, 
1246                                SILC_NOTIFY_TYPE_CUMODE_CHANGE, 4, 
1247                                idp1->data, idp1->len,
1248                                mode, 4,
1249                                idp2->data, idp2->len,
1250                                key, key_len);
1251   silc_free(key);
1252   silc_buffer_free(idp1);
1253   silc_buffer_free(idp2);
1254 }
1255
1256 /* Sends SIGNOFF notify type. This tells that `client_id' client has
1257    left SILC network. This function is used only between server and router
1258    traffic. This is not used to send the notify to the channel for
1259    client. The `message may be NULL. */
1260
1261 void silc_server_send_notify_signoff(SilcServer server,
1262                                      SilcSocketConnection sock,
1263                                      bool broadcast,
1264                                      SilcClientID *client_id,
1265                                      const char *message)
1266 {
1267   SilcBuffer idp;
1268
1269   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1270   silc_server_send_notify(server, sock, broadcast,
1271                           SILC_NOTIFY_TYPE_SIGNOFF,
1272                           message ? 2 : 1, idp->data, idp->len,
1273                           message, message ? strlen(message): 0);
1274   silc_buffer_free(idp);
1275 }
1276
1277 /* Sends TOPIC_SET notify type. This tells that `id' changed
1278    the `channel's topic to `topic'. The Notify packet is always destined
1279    to the channel. This function is used to send the topic set notifies
1280    between routers. */
1281
1282 void silc_server_send_notify_topic_set(SilcServer server,
1283                                        SilcSocketConnection sock,
1284                                        bool broadcast,
1285                                        SilcChannelEntry channel,
1286                                        void *id, SilcIdType id_type,
1287                                        char *topic)
1288 {
1289   SilcBuffer idp;
1290
1291   idp = silc_id_payload_encode(id, id_type);
1292   silc_server_send_notify_dest(server, sock, broadcast,
1293                                (void *)channel->id, SILC_ID_CHANNEL,
1294                                SILC_NOTIFY_TYPE_TOPIC_SET,
1295                                topic ? 2 : 1, 
1296                                idp->data, idp->len, 
1297                                topic, topic ? strlen(topic) : 0);
1298   silc_buffer_free(idp);
1299 }
1300
1301 /* Send KICKED notify type. This tells that the `client_id' on `channel'
1302    was kicked off the channel.  The `comment' may indicate the reason
1303    for the kicking. This function is used only between server and router
1304    traffic. */
1305
1306 void silc_server_send_notify_kicked(SilcServer server,
1307                                     SilcSocketConnection sock,
1308                                     bool broadcast,
1309                                     SilcChannelEntry channel,
1310                                     SilcClientID *client_id,
1311                                     SilcClientID *kicker,
1312                                     char *comment)
1313 {
1314   SilcBuffer idp1;
1315   SilcBuffer idp2;
1316
1317   idp1 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1318   idp2 = silc_id_payload_encode((void *)kicker, SILC_ID_CLIENT);
1319   silc_server_send_notify_dest(server, sock, broadcast, (void *)channel->id,
1320                                SILC_ID_CHANNEL, SILC_NOTIFY_TYPE_KICKED, 3,
1321                                idp1->data, idp1->len,
1322                                comment, comment ? strlen(comment) : 0,
1323                                idp2->data, idp2->len);
1324   silc_buffer_free(idp1);
1325   silc_buffer_free(idp2);
1326 }
1327
1328 /* Send KILLED notify type. This tells that the `client_id' client was
1329    killed from the network.  The `comment' may indicate the reason
1330    for the killing. */
1331
1332 void silc_server_send_notify_killed(SilcServer server,
1333                                     SilcSocketConnection sock,
1334                                     bool broadcast,
1335                                     SilcClientID *client_id,
1336                                     const char *comment,
1337                                     void *killer, SilcIdType killer_type)
1338 {
1339   SilcBuffer idp1;
1340   SilcBuffer idp2;
1341
1342   idp1 = silc_id_payload_encode(client_id, SILC_ID_CLIENT);
1343   idp2 = silc_id_payload_encode(killer, killer_type);
1344   silc_server_send_notify_dest(server, sock, broadcast, (void *)client_id,
1345                                SILC_ID_CLIENT, SILC_NOTIFY_TYPE_KILLED,
1346                                3, idp1->data, idp1->len,
1347                                comment, comment ? strlen(comment) : 0,
1348                                idp2->data, idp2->len);
1349   silc_buffer_free(idp1);
1350   silc_buffer_free(idp2);
1351 }
1352
1353 /* Sends UMODE_CHANGE notify type. This tells that `client_id' client's
1354    user mode in the SILC Network was changed. This function is used to
1355    send the packet between routers as broadcast packet. */
1356
1357 void silc_server_send_notify_umode(SilcServer server,
1358                                    SilcSocketConnection sock,
1359                                    bool broadcast,
1360                                    SilcClientID *client_id,
1361                                    SilcUInt32 mode_mask)
1362 {
1363   SilcBuffer idp;
1364   unsigned char mode[4];
1365
1366   idp = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1367   SILC_PUT32_MSB(mode_mask, mode);
1368
1369   silc_server_send_notify(server, sock, broadcast,
1370                           SILC_NOTIFY_TYPE_UMODE_CHANGE, 2,
1371                           idp->data, idp->len, 
1372                           mode, 4);
1373   silc_buffer_free(idp);
1374 }
1375
1376 /* Sends BAN notify type. This tells that ban has been either `add'ed
1377    or `del'eted on the `channel. This function is used to send the packet
1378    between routers as broadcast packet. */
1379
1380 void silc_server_send_notify_ban(SilcServer server,
1381                                  SilcSocketConnection sock,
1382                                  bool broadcast,
1383                                  SilcChannelEntry channel,
1384                                  char *add, char *del)
1385 {
1386   SilcBuffer idp;
1387
1388   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1389   silc_server_send_notify(server, sock, broadcast,
1390                           SILC_NOTIFY_TYPE_BAN, 3,
1391                           idp->data, idp->len,
1392                           add, add ? strlen(add) : 0,
1393                           del, del ? strlen(del) : 0);
1394   silc_buffer_free(idp);
1395 }
1396
1397 /* Sends INVITE notify type. This tells that invite has been either `add'ed
1398    or `del'eted on the `channel.  The sender of the invite is the `client_id'.
1399    This function is used to send the packet between routers as broadcast
1400    packet. */
1401
1402 void silc_server_send_notify_invite(SilcServer server,
1403                                     SilcSocketConnection sock,
1404                                     bool broadcast,
1405                                     SilcChannelEntry channel,
1406                                     SilcClientID *client_id,
1407                                     char *add, char *del)
1408 {
1409   SilcBuffer idp, idp2;
1410
1411   idp = silc_id_payload_encode((void *)channel->id, SILC_ID_CHANNEL);
1412   idp2 = silc_id_payload_encode((void *)client_id, SILC_ID_CLIENT);
1413   silc_server_send_notify(server, sock, broadcast,
1414                           SILC_NOTIFY_TYPE_INVITE, 5,
1415                           idp->data, idp->len,
1416                           channel->channel_name, strlen(channel->channel_name),
1417                           idp2->data, idp2->len,
1418                           add, add ? strlen(add) : 0,
1419                           del, del ? strlen(del) : 0);
1420   silc_buffer_free(idp);
1421   silc_buffer_free(idp2);
1422 }
1423
1424 /* Sends WATCH notify type. This tells that the `client' was watched and
1425    its status in the network has changed. */
1426
1427 void silc_server_send_notify_watch(SilcServer server,
1428                                    SilcSocketConnection sock,
1429                                    SilcClientEntry watcher,
1430                                    SilcClientEntry client,
1431                                    const char *nickname,
1432                                    SilcNotifyType type)
1433 {
1434   SilcBuffer idp;
1435   unsigned char mode[4], n[2];
1436
1437   idp = silc_id_payload_encode(client->id, SILC_ID_CLIENT);
1438   SILC_PUT16_MSB(type, n);
1439   SILC_PUT32_MSB(client->mode, mode);
1440   silc_server_send_notify_dest(server, sock, FALSE, watcher->id,
1441                                SILC_ID_CLIENT, SILC_NOTIFY_TYPE_WATCH,
1442                                4, idp->data, idp->len,
1443                                nickname, nickname ? strlen(nickname) : 0,
1444                                mode, sizeof(mode), 
1445                                type != SILC_NOTIFY_TYPE_NONE ?
1446                                n : NULL, sizeof(n));
1447   silc_buffer_free(idp);
1448 }
1449
1450 /* Sends notify message destined to specific entity. */
1451
1452 void silc_server_send_notify_dest(SilcServer server,
1453                                   SilcSocketConnection sock,
1454                                   bool broadcast,
1455                                   void *dest_id,
1456                                   SilcIdType dest_id_type,
1457                                   SilcNotifyType type,
1458                                   SilcUInt32 argc, ...)
1459 {
1460   va_list ap;
1461   SilcBuffer packet;
1462
1463   va_start(ap, argc);
1464
1465   packet = silc_notify_payload_encode(type, argc, ap);
1466   silc_server_packet_send_dest(server, sock, SILC_PACKET_NOTIFY, 
1467                                broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
1468                                dest_id, dest_id_type,
1469                                packet->data, packet->len, FALSE);
1470   silc_buffer_free(packet);
1471   va_end(ap);
1472 }
1473
1474 /* Sends notify message to a channel. The notify message sent is 
1475    distributed to all clients on the channel. If `route_notify' is TRUE
1476    then the notify may be routed to primary route or to some other routers.
1477    If FALSE it is assured that the notify is sent only locally. If `sender'
1478    is provided then the packet is not sent to that connection since it
1479    originally came from it. */
1480
1481 void silc_server_send_notify_to_channel(SilcServer server,
1482                                         SilcSocketConnection sender,
1483                                         SilcChannelEntry channel,
1484                                         bool route_notify,
1485                                         SilcNotifyType type,
1486                                         SilcUInt32 argc, ...)
1487 {
1488   va_list ap;
1489   SilcBuffer packet;
1490
1491   va_start(ap, argc);
1492
1493   packet = silc_notify_payload_encode(type, argc, ap);
1494   silc_server_packet_send_to_channel(server, sender, channel, 
1495                                      SILC_PACKET_NOTIFY, route_notify,
1496                                      packet->data, packet->len, FALSE);
1497   silc_buffer_free(packet);
1498   va_end(ap);
1499 }
1500
1501 /* Send notify message to all channels the client has joined. It is quaranteed
1502    that the message is sent only once to a client (ie. if a client is joined
1503    on two same channel it will receive only one notify message). Also, this
1504    sends only to local clients (locally connected if we are server, and to
1505    local servers if we are router). If `sender' is provided the packet is
1506    not sent to that client at all. */
1507
1508 void silc_server_send_notify_on_channels(SilcServer server,
1509                                          SilcClientEntry sender,
1510                                          SilcClientEntry client,
1511                                          SilcNotifyType type,
1512                                          SilcUInt32 argc, ...)
1513 {
1514   int k;
1515   SilcSocketConnection sock = NULL;
1516   SilcPacketContext packetdata;
1517   SilcClientEntry c;
1518   SilcClientEntry *sent_clients = NULL;
1519   SilcUInt32 sent_clients_count = 0;
1520   SilcServerEntry *routed = NULL;
1521   SilcUInt32 routed_count = 0;
1522   SilcHashTableList htl, htl2;
1523   SilcChannelEntry channel;
1524   SilcChannelClientEntry chl, chl2;
1525   SilcIDListData idata;
1526   SilcBuffer packet;
1527   unsigned char *data;
1528   SilcUInt32 data_len;
1529   bool force_send = FALSE;
1530   va_list ap;
1531
1532   if (!silc_hash_table_count(client->channels)) {
1533     SILC_LOG_DEBUG(("Client is not joined to any channels"));
1534     return;
1535   }
1536
1537   SILC_LOG_DEBUG(("Sending notify to joined channels"));
1538
1539   va_start(ap, argc);
1540   packet = silc_notify_payload_encode(type, argc, ap);
1541   data = packet->data;
1542   data_len = packet->len;
1543
1544   /* Set the packet context pointers. */
1545   packetdata.flags = 0;
1546   packetdata.type = SILC_PACKET_NOTIFY;
1547   packetdata.src_id = silc_id_id2str(server->id, SILC_ID_SERVER);
1548   packetdata.src_id_len = silc_id_get_len(server->id, SILC_ID_SERVER);
1549   packetdata.src_id_type = SILC_ID_SERVER;
1550
1551   silc_hash_table_list(client->channels, &htl);
1552   while (silc_hash_table_get(&htl, NULL, (void **)&chl)) {
1553     channel = chl->channel;
1554
1555     /* Send the message to all clients on the channel's client list. */
1556     silc_hash_table_list(channel->user_list, &htl2);
1557     while (silc_hash_table_get(&htl2, NULL, (void **)&chl2)) {
1558       c = chl2->client;
1559       
1560       if (sender && c == sender)
1561         continue;
1562
1563       /* Check if we have sent the packet to this client already */
1564       for (k = 0; k < sent_clients_count; k++)
1565         if (sent_clients[k] == c)
1566           break;
1567       if (k < sent_clients_count)
1568         continue;
1569
1570       /* If we are router and if this client has router set it is not
1571          locally connected client and we will route the message to the
1572          router set in the client. */
1573       if (c && c->router && server->server_type == SILC_ROUTER) {
1574         /* Check if we have sent the packet to this route already */
1575         for (k = 0; k < routed_count; k++)
1576           if (routed[k] == c->router)
1577             break;
1578         if (k < routed_count)
1579           continue;
1580         
1581         /* Get data used in packet header encryption, keys and stuff. */
1582         sock = (SilcSocketConnection)c->router->connection;
1583         idata = (SilcIDListData)c->router;
1584
1585         {
1586           SILC_LOG_DEBUG(("*****************"));
1587           SILC_LOG_DEBUG(("client->router->id %s",
1588                           silc_id_render(c->router->id, SILC_ID_SERVER)));
1589           SILC_LOG_DEBUG(("client->router->connection->user_data->id %s",
1590                           silc_id_render(((SilcServerEntry)sock->user_data)->id, SILC_ID_SERVER)));
1591         }
1592
1593         packetdata.dst_id = silc_id_id2str(c->router->id, SILC_ID_SERVER);
1594         packetdata.dst_id_len = silc_id_get_len(c->router->id, SILC_ID_SERVER);
1595         packetdata.dst_id_type = SILC_ID_SERVER;
1596
1597         /* Send the packet */
1598         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1599                                                 idata->send_key, 
1600                                                 idata->hmac_send, 
1601                                                 idata->psn_send++, 
1602                                                 data, data_len, FALSE, 
1603                                                 force_send);
1604         
1605         silc_free(packetdata.dst_id);
1606
1607         /* We want to make sure that the packet is routed to same router
1608            only once. Mark this route as sent route. */
1609         routed = silc_realloc(routed, sizeof(*routed) * (routed_count + 1));
1610         routed[routed_count++] = c->router;
1611         continue;
1612       }
1613
1614       if (c && c->router)
1615         continue;
1616
1617       /* Send to locally connected client */
1618       if (c) {
1619         
1620         /* Get data used in packet header encryption, keys and stuff. */
1621         sock = (SilcSocketConnection)c->connection;
1622         idata = (SilcIDListData)c;
1623         
1624         if (!sock)
1625           continue;
1626
1627         packetdata.dst_id = silc_id_id2str(c->id, SILC_ID_CLIENT);
1628         packetdata.dst_id_len = silc_id_get_len(c->id, SILC_ID_CLIENT);
1629         packetdata.dst_id_type = SILC_ID_CLIENT;
1630
1631         /* Send the packet */
1632         silc_server_packet_send_to_channel_real(server, sock, &packetdata,
1633                                                 idata->send_key, 
1634                                                 idata->hmac_send, 
1635                                                 idata->psn_send++, 
1636                                                 data, data_len, FALSE, 
1637                                                 force_send);
1638
1639         silc_free(packetdata.dst_id);
1640
1641         /* Make sure that we send the notify only once per client. */
1642         sent_clients = silc_realloc(sent_clients, sizeof(*sent_clients) * 
1643                                     (sent_clients_count + 1));
1644         sent_clients[sent_clients_count++] = c;
1645       }
1646     }
1647     silc_hash_table_list_reset(&htl2);
1648   }
1649
1650   silc_hash_table_list_reset(&htl);
1651   silc_free(routed);
1652   silc_free(sent_clients);
1653   silc_free(packetdata.src_id);
1654   silc_buffer_free(packet);
1655   va_end(ap);
1656 }
1657
1658 /* Sends New ID Payload to remote end. The packet is used to distribute
1659    information about new registered clients, servers, channel etc. usually
1660    to routers so that they can keep these information up to date. 
1661    If the argument `broadcast' is TRUE then the packet is sent as
1662    broadcast packet. */
1663
1664 void silc_server_send_new_id(SilcServer server,
1665                              SilcSocketConnection sock,
1666                              bool broadcast,
1667                              void *id, SilcIdType id_type, 
1668                              SilcUInt32 id_len)
1669 {
1670   SilcBuffer idp;
1671
1672   SILC_LOG_DEBUG(("Sending new ID"));
1673
1674   idp = silc_id_payload_encode(id, id_type);
1675   silc_server_packet_send(server, sock, SILC_PACKET_NEW_ID, 
1676                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1677                           idp->data, idp->len, FALSE);
1678   silc_buffer_free(idp);
1679 }
1680
1681 /* Send New Channel Payload to notify about newly created channel in the
1682    SILC network. Router uses this to notify other routers in the network 
1683    about new channel. This packet is broadcasted by router. */
1684
1685 void silc_server_send_new_channel(SilcServer server,
1686                                   SilcSocketConnection sock,
1687                                   bool broadcast,
1688                                   char *channel_name,
1689                                   void *channel_id, 
1690                                   SilcUInt32 channel_id_len,
1691                                   SilcUInt32 mode)
1692 {
1693   SilcBuffer packet;
1694   unsigned char *cid;
1695   SilcUInt32 name_len = strlen(channel_name);
1696
1697   SILC_LOG_DEBUG(("Sending new channel"));
1698
1699   cid = silc_id_id2str(channel_id, SILC_ID_CHANNEL);
1700   if (!cid)
1701     return;
1702
1703   /* Encode the channel payload */
1704   packet = silc_channel_payload_encode(channel_name, name_len,
1705                                        cid, channel_id_len, mode);
1706
1707   silc_server_packet_send(server, sock, SILC_PACKET_NEW_CHANNEL, 
1708                           broadcast ? SILC_PACKET_FLAG_BROADCAST : 0, 
1709                           packet->data, packet->len, FALSE);
1710
1711   silc_free(cid);
1712   silc_buffer_free(packet);
1713 }
1714
1715 /* Send Channel Key payload to distribute the new channel key. Normal server
1716    sends this to router when new client joins to existing channel. Router
1717    sends this to the local server who sent the join command in case where
1718    the channel did not exist yet. Both normal and router servers uses this
1719    also to send this to locally connected clients on the channel. This
1720    must not be broadcasted packet. Routers do not send this to each other. 
1721    If `sender is provided then the packet is not sent to that connection since
1722    it originally came from it. */
1723
1724 void silc_server_send_channel_key(SilcServer server,
1725                                   SilcSocketConnection sender,
1726                                   SilcChannelEntry channel,
1727                                   unsigned char route)
1728 {
1729   SilcBuffer packet;
1730   unsigned char *chid;
1731   SilcUInt32 tmp_len;
1732  
1733   SILC_LOG_DEBUG(("Sending key to channel %s", channel->channel_name));
1734  
1735   chid = silc_id_id2str(channel->id, SILC_ID_CHANNEL);
1736   if (!chid)
1737     return;
1738  
1739   /* Encode channel key packet */
1740   tmp_len = strlen(channel->channel_key->cipher->name);
1741   packet = silc_channel_key_payload_encode(silc_id_get_len(channel->id,
1742                                                            SILC_ID_CHANNEL),
1743                                            chid, tmp_len,
1744                                            channel->channel_key->cipher->name,
1745                                            channel->key_len / 8, channel->key);
1746   silc_server_packet_send_to_channel(server, sender, channel, 
1747                                      SILC_PACKET_CHANNEL_KEY,
1748                                      route, packet->data, packet->len, 
1749                                      FALSE);
1750   silc_buffer_free(packet);
1751   silc_free(chid);
1752 }
1753
1754 /* Generic function to send any command. The arguments must be sent already
1755    encoded into correct form in correct order. */
1756
1757 void silc_server_send_command(SilcServer server, 
1758                               SilcSocketConnection sock,
1759                               SilcCommand command, 
1760                               SilcUInt16 ident,
1761                               SilcUInt32 argc, ...)
1762 {
1763   SilcBuffer packet;
1764   va_list ap;
1765
1766   va_start(ap, argc);
1767
1768   packet = silc_command_payload_encode_vap(command, ident, argc, ap);
1769   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND, 0,
1770                           packet->data, packet->len, TRUE);
1771   silc_buffer_free(packet);
1772   va_end(ap);
1773 }
1774
1775 /* Generic function to send any command reply. The arguments must be sent
1776    already encoded into correct form in correct order. */
1777
1778 void silc_server_send_command_reply(SilcServer server, 
1779                                     SilcSocketConnection sock,
1780                                     SilcCommand command, 
1781                                     SilcStatus status,
1782                                     SilcStatus error,
1783                                     SilcUInt16 ident,
1784                                     SilcUInt32 argc, ...)
1785 {
1786   SilcBuffer packet;
1787   va_list ap;
1788
1789   va_start(ap, argc);
1790
1791   packet = silc_command_reply_payload_encode_vap(command, status, error,
1792                                                  ident, argc, ap);
1793   silc_server_packet_send(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1794                           packet->data, packet->len, TRUE);
1795   silc_buffer_free(packet);
1796   va_end(ap);
1797 }
1798
1799 /* Generic function to send any command reply. The arguments must be sent
1800    already encoded into correct form in correct order. */
1801
1802 void silc_server_send_dest_command_reply(SilcServer server, 
1803                                          SilcSocketConnection sock,
1804                                          void *dst_id,
1805                                          SilcIdType dst_id_type,
1806                                          SilcCommand command, 
1807                                          SilcStatus status,
1808                                          SilcStatus error,
1809                                          SilcUInt16 ident,
1810                                          SilcUInt32 argc, ...)
1811 {
1812   SilcBuffer packet;
1813   va_list ap;
1814
1815   va_start(ap, argc);
1816
1817   packet = silc_command_reply_payload_encode_vap(command, status, error,
1818                                                  ident, argc, ap);
1819   silc_server_packet_send_dest(server, sock, SILC_PACKET_COMMAND_REPLY, 0,
1820                                dst_id, dst_id_type, packet->data, 
1821                                packet->len, TRUE);
1822   silc_buffer_free(packet);
1823   va_end(ap);
1824 }
1825
1826 /* Send the heartbeat packet. */
1827
1828 void silc_server_send_heartbeat(SilcServer server,
1829                                 SilcSocketConnection sock)
1830 {
1831   silc_server_packet_send(server, sock, SILC_PACKET_HEARTBEAT, 0,
1832                           NULL, 0, FALSE);
1833 }
1834
1835 /* Generic function to relay packet we've received. This is used to relay
1836    packets to a client but generally can be used to other purposes as well. */
1837
1838 void silc_server_relay_packet(SilcServer server,
1839                               SilcSocketConnection dst_sock,
1840                               SilcCipher cipher,
1841                               SilcHmac hmac,
1842                               SilcUInt32 sequence,
1843                               SilcPacketContext *packet,
1844                               bool force_send)
1845 {
1846   const SilcBufferStruct p;
1847
1848   silc_buffer_push(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1849                    + packet->dst_id_len + packet->padlen);
1850   if (!silc_packet_send_prepare(dst_sock, 0, 0, packet->buffer->len, hmac,
1851                                 (const SilcBuffer)&p)) {
1852     SILC_LOG_ERROR(("Cannot send packet"));
1853     return;
1854   }
1855   silc_buffer_put((SilcBuffer)&p, packet->buffer->data, packet->buffer->len);
1856
1857   /* Re-encrypt packet */
1858   silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&p, p.len);
1859   
1860   /* Send the packet */
1861   silc_server_packet_send_real(server, dst_sock, force_send);
1862
1863   silc_buffer_pull(packet->buffer, SILC_PACKET_HEADER_LEN + packet->src_id_len 
1864                    + packet->dst_id_len + packet->padlen);
1865 }
1866
1867 /* Routine used to send the connection authentication packet. */
1868
1869 void silc_server_send_connection_auth_request(SilcServer server,
1870                                               SilcSocketConnection sock,
1871                                               SilcUInt16 conn_type,
1872                                               SilcAuthMethod auth_meth)
1873 {
1874   SilcBuffer packet;
1875
1876   packet = silc_buffer_alloc(4);
1877   silc_buffer_pull_tail(packet, SILC_BUFFER_END(packet));
1878   silc_buffer_format(packet,
1879                      SILC_STR_UI_SHORT(conn_type),
1880                      SILC_STR_UI_SHORT(auth_meth),
1881                      SILC_STR_END);
1882
1883   silc_server_packet_send(server, sock, SILC_PACKET_CONNECTION_AUTH_REQUEST,
1884                           0, packet->data, packet->len, FALSE);
1885   silc_buffer_free(packet);
1886 }
1887
1888 /* Purge the outgoing packet queue to the network if there is data. This
1889    function can be used to empty the packet queue. It is guaranteed that
1890    after this function returns the outgoing data queue is empty. */
1891
1892 void silc_server_packet_queue_purge(SilcServer server,
1893                                     SilcSocketConnection sock)
1894 {
1895   if (sock && SILC_IS_OUTBUF_PENDING(sock) && 
1896       (SILC_IS_DISCONNECTED(sock) == FALSE)) {
1897     server->stat.packets_sent++;
1898     silc_packet_send(sock, TRUE);
1899     SILC_SET_CONNECTION_FOR_INPUT(server->schedule, sock->sock);
1900     SILC_UNSET_OUTBUF_PENDING(sock);
1901     silc_buffer_clear(sock->outbuf);
1902   }
1903 }