5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2006 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.
21 #include "silcserver.h"
22 #include "server_internal.h"
24 /************************** Types and definitions ***************************/
27 /************************ Static utility functions **************************/
30 /***************************** Packet received ******************************/
32 SILC_FSM_STATE(silc_server_st_packet_disconnect)
34 SilcPacket packet = fsm_context;
35 SilcEntryData data = silc_packet_get_context(packet->stream);
40 SILC_FSM_STATE(silc_server_st_packet_channel_message)
44 * Received channel message. Channel messages are special packets
45 * (although probably most common ones) thus they are handled
48 if (packet->flags & SILC_PACKET_FLAG_LIST)
50 idata->last_receive = time(NULL);
51 silc_server_channel_message(server, sock, packet);
57 SILC_FSM_STATE(silc_server_st_packet_channel_key)
61 * Received key for channel. As channels are created by the router
62 * the keys are as well. We will distribute the key to all of our
63 * locally connected clients on the particular channel. Router
64 * never receives this channel and thus is ignored.
66 if (packet->flags & SILC_PACKET_FLAG_LIST)
68 silc_server_channel_key(server, sock, packet);
74 SILC_FSM_STATE(silc_server_st_packet_private_message)
78 * Received private message packet. The packet is coming from either
81 if (packet->flags & SILC_PACKET_FLAG_LIST)
83 idata->last_receive = time(NULL);
84 silc_server_private_message(server, sock, packet);
90 SILC_FSM_STATE(silc_server_st_packet_private_message_key)
94 * Private message key packet.
96 if (packet->flags & SILC_PACKET_FLAG_LIST)
98 silc_server_private_message_key(server, sock, packet);
104 SILC_FSM_STATE(silc_server_st_packet_new_id)
108 * Received New ID packet. This includes some new ID that has been
109 * created. It may be for client, server or channel. This is the way
110 * to distribute information about new registered entities in the
113 if (packet->flags & SILC_PACKET_FLAG_LIST)
114 silc_server_new_id_list(server, sock, packet);
116 silc_server_new_id(server, sock, packet);
122 SILC_FSM_STATE(silc_server_st_packet_new_channel)
126 * Received new channel packet. Information about new channel in the
127 * network are distributed using this packet.
129 if (packet->flags & SILC_PACKET_FLAG_LIST)
130 silc_server_new_channel_list(server, sock, packet);
132 silc_server_new_channel(server, sock, packet);
138 SILC_FSM_STATE(silc_server_st_packet_key_agreement)
141 if (packet->flags & SILC_PACKET_FLAG_LIST)
143 silc_server_key_agreement(server, sock, packet);
149 SILC_FSM_STATE(silc_server_st_packet_ftp)
153 if (packet->flags & SILC_PACKET_FLAG_LIST)
155 silc_server_ftp(server, sock, packet);
161 SILC_FSM_STATE(silc_server_st_packet_resume_router)
164 /* Resume router packet received. This packet is received for backup
165 router resuming protocol. */
166 if (packet->flags & SILC_PACKET_FLAG_LIST)
168 silc_server_backup_resume_router(server, sock, packet);
174 SILC_FSM_STATE(silc_server_st_packet_received)
176 SilcServerThread thread = fsm_context;
177 SilcPacket packet = state_context;
179 SILC_LOG_DEBUG(("Received %s packet [flags %d]",
180 silc_get_packet_name(packet->type), packet->flags));
182 /* Parse the packet type */
183 switch (packet->type) {
184 case SILC_PACKET_CHANNEL_MESSAGE:
185 /** Packet CHANNEL_MESSAGE */
186 if (packet->flags & SILC_PACKET_FLAG_LIST)
188 silc_fsm_next(fsm, silc_server_st_packet_channel_message);
192 case SILC_PACKET_PRIVATE_MESSAGE:
193 /** Packet PRIVATE_MESSAGE */
194 if (packet->flags & SILC_PACKET_FLAG_LIST)
196 silc_fsm_next(fsm, silc_server_st_packet_private_message);
200 case SILC_PACKET_NOTIFY:
202 silc_fsm_next(fsm, silc_server_st_packet_notify);
206 case SILC_PACKET_COMMAND:
207 /** Packet COMMAND */
208 if (packet->flags & SILC_PACKET_FLAG_LIST)
210 silc_fsm_next(fsm, silc_server_st_packet_command);
214 case SILC_PACKET_COMMAND_REPLY:
215 /** Packet COMMAND_REPLY */
216 if (packet->flags & SILC_PACKET_FLAG_LIST)
218 silc_fsm_next(fsm, silc_server_st_packet_command_reply);
222 case SILC_PACKET_CHANNEL_KEY:
223 /** Packet CHANNEL_KEY */
224 if (packet->flags & SILC_PACKET_FLAG_LIST)
226 silc_fsm_next(fsm, silc_server_st_packet_channel_key);
230 case SILC_PACKET_NEW_ID:
232 silc_fsm_next(fsm, silc_server_st_packet_new_id);
236 case SILC_PACKET_NEW_CLIENT:
237 /** Packet NEW_CLIENT */
238 if (packet->flags & SILC_PACKET_FLAG_LIST)
240 silc_fsm_next(fsm, silc_server_st_packet_new_client);
244 case SILC_PACKET_NEW_SERVER:
245 /** Packet NEW_SERVER */
246 if (packet->flags & SILC_PACKET_FLAG_LIST)
248 silc_fsm_next(fsm, silc_server_st_packet_new_server);
252 case SILC_PACKET_NEW_CHANNEL:
253 /** Packet NEW_CHANNEL */
254 silc_fsm_next(fsm, silc_server_st_packet_new_channel);
258 case SILC_PACKET_KEY_AGREEMENT:
259 /** Packet KEY_AGREEMENT */
260 if (packet->flags & SILC_PACKET_FLAG_LIST)
262 silc_fsm_next(fsm, silc_server_st_packet_key_agreement);
266 case SILC_PACKET_FTP:
268 if (packet->flags & SILC_PACKET_FLAG_LIST)
270 silc_fsm_next(fsm, silc_server_st_packet_ftp);
274 case SILC_PACKET_RESUME_CLIENT:
275 /** Packet RESUME_CLIENT */
276 if (packet->flags & SILC_PACKET_FLAG_LIST)
278 silc_fsm_next(fsm, silc_server_st_packet_resume_client);
282 case SILC_PACKET_RESUME_ROUTER:
283 /** Packet RESUME_ROUTER */
284 if (packet->flags & SILC_PACKET_FLAG_LIST)
286 silc_fsm_next(fsm, silc_server_st_packet_resume_router);
290 case SILC_PACKET_DISCONNECT:
291 /** Packet DISCONNECT */
292 if (packet->flags & SILC_PACKET_FLAG_LIST)
294 silc_fsm_next(fsm, silc_server_st_packet_disconnect);
298 case SILC_PACKET_PRIVATE_MESSAGE_KEY:
299 /** Packet PRIVATE_MESSAGE */
300 if (packet->flags & SILC_PACKET_FLAG_LIST)
302 silc_fsm_next(fsm, silc_server_st_packet_private_message_key);
306 case SILC_PACKET_HEARTBEAT:
307 case SILC_PACKET_SUCCESS:
308 case SILC_PACKET_FAILURE:
309 case SILC_PACKET_REJECT:
310 case SILC_PACKET_KEY_EXCHANGE:
311 case SILC_PACKET_KEY_EXCHANGE_1:
312 case SILC_PACKET_KEY_EXCHANGE_2:
313 case SILC_PACKET_REKEY:
314 case SILC_PACKET_REKEY_DONE:
315 case SILC_PACKET_CONNECTION_AUTH:
316 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
321 SILC_LOG_ERROR(("Unsupported packet type %d", packet->type));
325 silc_packet_free(packet);
329 /* Received NEW_CLIENT packet, used to register client to SILC network. */
331 SILC_FSM_STATE(silc_server_st_packet_new_client)
333 SilcServerThread thread = fsm_context;
334 SilcPacket packet = state_context;
335 SilcServerAccept ac = silc_packet_get_context(packet->stream);
337 if (!ac || ac->register_packet) {
338 silc_packet_free(packet);
342 /* Signal that client registers to network */
343 ac->register_packet = packet;
344 SILC_FSM_EVENT_SIGNAL(&ac->wait_register);
349 /* Received NEW_SERVER packet, used to register server to SILC network. */
351 SILC_FSM_STATE(silc_server_st_packet_new_server)
353 SilcServerThread thread = fsm_context;
354 SilcPacket packet = state_context;
355 SilcServerAccept ac = silc_packet_get_context(packet->stream);
357 if (!ac || ac->register_packet) {
358 silc_packet_free(packet);
362 /* Signal that server registers to network */
363 ac->register_packet = packet;
364 SILC_FSM_EVENT_SIGNAL(&ac->wait_register);
369 /* Received RESUME_CLIENT packet, used to resume detached session. */
371 SILC_FSM_STATE(silc_server_st_packet_resume_client)
373 SilcServerThread thread = fsm_context;
374 SilcPacket packet = state_context;
375 SilcServerAccept ac = silc_packet_get_context(packet->stream);
377 if (!ac || ac->register_packet) {
378 silc_packet_free(packet);
382 /* Signal that client resumes session */
383 ac->register_packet = packet;
384 SILC_FSM_EVENT_SIGNAL(&ac->wait_register);