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