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