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