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.
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);
37 return SILC_FSM_FINISH;
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);
54 return SILC_FSM_FINISH;
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);
71 return SILC_FSM_FINISH;
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);
87 return SILC_FSM_FINISH;
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);
101 return SILC_FSM_FINISH;
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);
119 return SILC_FSM_FINISH;
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);
135 return SILC_FSM_FINISH;
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);
146 return SILC_FSM_FINISH;
149 SILC_FSM_STATE(silc_server_st_packet_ftp)
153 if (packet->flags & SILC_PACKET_FLAG_LIST)
155 silc_server_ftp(server, sock, packet);
158 return SILC_FSM_FINISH;
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);
171 return SILC_FSM_FINISH;
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);
189 return SILC_FSM_CONTINUE;
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);
197 return SILC_FSM_CONTINUE;
200 case SILC_PACKET_NOTIFY:
202 silc_fsm_next(fsm, silc_server_st_packet_notify);
203 return SILC_FSM_CONTINUE;
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);
211 return SILC_FSM_CONTINUE;
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);
219 return SILC_FSM_CONTINUE;
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);
227 return SILC_FSM_CONTINUE;
230 case SILC_PACKET_NEW_ID:
232 silc_fsm_next(fsm, silc_server_st_packet_new_id);
233 return SILC_FSM_CONTINUE;
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);
241 return SILC_FSM_CONTINUE;
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);
249 return SILC_FSM_CONTINUE;
252 case SILC_PACKET_NEW_CHANNEL:
253 /** Packet NEW_CHANNEL */
254 silc_fsm_next(fsm, silc_server_st_packet_new_channel);
255 return SILC_FSM_CONTINUE;
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);
263 return SILC_FSM_CONTINUE;
266 case SILC_PACKET_FTP:
268 if (packet->flags & SILC_PACKET_FLAG_LIST)
270 silc_fsm_next(fsm, silc_server_st_packet_ftp);
271 return SILC_FSM_CONTINUE;
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);
279 return SILC_FSM_CONTINUE;
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);
287 return SILC_FSM_CONTINUE;
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);
295 return SILC_FSM_CONTINUE;
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);
303 return SILC_FSM_CONTINUE;
306 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
307 /** Packet CONNECTION_AUTH_REQUEST */
308 if (packet->flags & SILC_PACKET_FLAG_LIST)
310 silc_fsm_next(fsm, silc_server_st_packet_connection_auth_request);
311 return SILC_FSM_CONTINUE;
314 case SILC_PACKET_HEARTBEAT:
315 case SILC_PACKET_SUCCESS:
316 case SILC_PACKET_FAILURE:
317 case SILC_PACKET_REJECT:
318 case SILC_PACKET_KEY_EXCHANGE:
319 case SILC_PACKET_KEY_EXCHANGE_1:
320 case SILC_PACKET_KEY_EXCHANGE_2:
321 case SILC_PACKET_REKEY:
322 case SILC_PACKET_REKEY_DONE:
323 case SILC_PACKET_CONNECTION_AUTH:
324 case SILC_PACKET_CONNECTION_AUTH_REQUEST:
329 SILC_LOG_ERROR(("Unsupported packet type %d", packet->type));
333 silc_packet_free(packet);
334 return SILC_FSM_FINISH;
337 /* Received NEW_CLIENT packet, used to register client to SILC network. */
339 SILC_FSM_STATE(silc_server_st_packet_new_client)
341 SilcServerThread thread = fsm_context;
342 SilcPacket packet = state_context;
343 SilcServerAccept ac = silc_packet_get_context(packet->stream);
345 if (!ac || ac->register_packet) {
346 silc_packet_free(packet);
347 return SILC_FSM_FINISH;
350 /* Signal that client registers to network */
351 ac->register_packet = packet;
352 SILC_FSM_SEMA_POST(&ac->wait_register);
354 return SILC_FSM_FINISH;
357 /* Received NEW_SERVER packet, used to register server to SILC network. */
359 SILC_FSM_STATE(silc_server_st_packet_new_server)
361 SilcServerThread thread = fsm_context;
362 SilcPacket packet = state_context;
363 SilcServerAccept ac = silc_packet_get_context(packet->stream);
365 if (!ac || ac->register_packet) {
366 silc_packet_free(packet);
367 return SILC_FSM_FINISH;
370 /* Signal that server registers to network */
371 ac->register_packet = packet;
372 SILC_FSM_SEMA_POST(&ac->wait_register);
374 return SILC_FSM_FINISH;
377 /* Received RESUME_CLIENT packet, used to resume detached session. */
379 SILC_FSM_STATE(silc_server_st_packet_resume_client)
381 SilcServerThread thread = fsm_context;
382 SilcPacket packet = state_context;
383 SilcServerAccept ac = silc_packet_get_context(packet->stream);
385 if (!ac || ac->register_packet) {
386 silc_packet_free(packet);
387 return SILC_FSM_FINISH;
390 /* Signal that client resumes session */
391 ac->register_packet = packet;
392 SILC_FSM_SEMA_POST(&ac->wait_register);
394 return SILC_FSM_FINISH;