5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 Pekka Riikonen
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.
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.
22 #include "silcserver.h"
23 #include "server_internal.h"
25 /******************************* Heartbeat **********************************/
27 /* Send the heartbeat packet. */
29 SilcBool silc_server_send_heartbeat(SilcPacketStream stream)
31 return stream ? silc_packet_send(stream, SILC_PACKET_HEARTBEAT, 0,
36 /********************************* Error ************************************/
38 /* Sends error packet. */
40 SilcBool silc_server_send_error(SilcPacketStream stream, const char *fmt, ...)
42 unsigned char buf[2048];
48 memset(buf, 0, sizeof(buf));
50 vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
53 return silc_packet_send(stream, SILC_PACKET_ERROR, 0, buf, strlen(buf));
57 /********************************* New ID ***********************************/
59 /* Sends New ID packet. The packet is used to distribute information about
60 new registered clients, servers and channels. If the argument `broadcast'
61 is TRUE then the packet is sent as broadcast packet. */
63 SilcBool silc_server_send_new_id(SilcPacketStream stream,
65 void *id, SilcIdType id_type)
73 SILC_LOG_DEBUG(("Sending new ID (%s)", silc_id_render(id, id_type)));
75 idp = silc_id_payload_encode(id, id_type);
79 ret = silc_packet_send(stream, SILC_PACKET_NEW_ID,
80 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
81 idp->data, silc_buffer_len(idp));
83 silc_buffer_free(idp);
88 /****************************** Notify packets ******************************/
90 /* Sends notify packet. Each variable argument format in the argument list
91 must be { argument data, argument length }. */
93 SilcBool silc_server_send_notify(SilcServer server,
94 SilcPacketStream stream,
101 SilcBool ret = FALSE;
108 packet = silc_notify_payload_encode(type, argc, ap);
114 ret = silc_packet_send(stream, SILC_PACKET_NOTIFY,
115 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
116 packet->data, silc_buffer_len(packet));
119 /* Send to backup routers if this is being broadcasted to primary
120 router. The silc_server_backup_send checks further whether to
121 actually send it or not. */
122 if ((broadcast && stream == SILC_PRIMARY_ROUTE(server)) ||
123 (broadcast && !SILC_PRIMARY_ROUTE(server)))
124 silc_server_backup_send(server, NULL, SILC_PACKET_NOTIFY, 0,
125 packet->data, packet->len, FALSE, TRUE);
128 silc_buffer_free(packet);
134 /* Sends current motd to client in notify packet */
136 SilcBool silc_server_send_motd(SilcServer server, SilcPacketStream stream)
138 char *motd, *motd_file = NULL;
140 SilcBool ret = FALSE;
142 if (!stream || !server->params)
145 motd_file = server->params->server_info->motd_file;
149 motd = silc_file_readfile(motd_file, &motd_len);
154 ret = silc_server_send_notify(server, stream, FALSE,
155 SILC_NOTIFY_TYPE_MOTD, 1, motd, motd_len);
162 /* Sends notify packet and gets the arguments from the `args' Argument
165 SilcBool silc_server_send_notify_args(SilcPacketStream stream,
172 SilcBool ret = FALSE;
177 packet = silc_notify_payload_encode_args(type, argc, args);
181 ret = silc_packet_send(stream, SILC_PACKET_NOTIFY,
182 broadcast ? SILC_PACKET_FLAG_BROADCAST : 0,
183 packet->data, silc_buffer_len(packet));
185 silc_buffer_free(packet);
189 /* Send CHANNEL_CHANGE notify type. This tells the receiver to replace the
190 `old_id' with the `new_id'. */
192 SilcBool silc_server_send_notify_channel_change(SilcServer server,
193 SilcPacketStream stream,
195 SilcChannelID *old_id,
196 SilcChannelID *new_id)
198 SilcBuffer idp1, idp2;
199 SilcBool ret = FALSE;
201 if (!server || !stream)
204 idp1 = silc_id_payload_encode((void *)old_id, SILC_ID_CHANNEL);
205 idp2 = silc_id_payload_encode((void *)new_id, SILC_ID_CHANNEL);
209 ret = silc_server_send_notify(server, stream, broadcast,
210 SILC_NOTIFY_TYPE_CHANNEL_CHANGE,
211 2, idp1->data, silc_buffer_len(idp1),
212 idp2->data, silc_buffer_len(idp2));
213 silc_buffer_free(idp1);
214 silc_buffer_free(idp2);