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