Added SILC Server library.
[silc.git] / lib / silcserver / server_st_packet.c
1 /*
2
3   server_st_packet.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005 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 "silc.h"
21 #include "silcserver.h"
22 #include "server_internal.h"
23
24 /************************** Types and definitions ***************************/
25
26
27 /************************ Static utility functions **************************/
28
29
30 /***************************** Packet received ******************************/
31
32 SILC_FSM_STATE(silc_server_st_packet_disconnect)
33 {
34   SilcPacket packet = fsm_context;
35   SilcEntryData data = silc_packet_get_context(packet->stream);
36
37   return SILC_FSM_FINISH;
38 }
39
40 SILC_FSM_STATE(silc_server_st_packet_channel_message)
41 {
42 #if 0
43     /*
44      * Received channel message. Channel messages are special packets
45      * (although probably most common ones) thus they are handled
46      * specially.
47      */
48     if (packet->flags & SILC_PACKET_FLAG_LIST)
49       break;
50     idata->last_receive = time(NULL);
51     silc_server_channel_message(server, sock, packet);
52 #endif
53
54   return SILC_FSM_FINISH;
55 }
56
57 SILC_FSM_STATE(silc_server_st_packet_channel_key)
58 {
59 #if 0
60     /*
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.
65      */
66     if (packet->flags & SILC_PACKET_FLAG_LIST)
67       break;
68     silc_server_channel_key(server, sock, packet);
69 #endif
70
71   return SILC_FSM_FINISH;
72 }
73
74 SILC_FSM_STATE(silc_server_st_packet_private_message)
75 {
76 #if 0
77     /*
78      * Received private message packet. The packet is coming from either
79      * client or server.
80      */
81     if (packet->flags & SILC_PACKET_FLAG_LIST)
82       break;
83     idata->last_receive = time(NULL);
84     silc_server_private_message(server, sock, packet);
85 #endif
86
87   return SILC_FSM_FINISH;
88 }
89
90 SILC_FSM_STATE(silc_server_st_packet_private_message_key)
91 {
92 #if 0
93     /*
94      * Private message key packet.
95      */
96     if (packet->flags & SILC_PACKET_FLAG_LIST)
97       break;
98     silc_server_private_message_key(server, sock, packet);
99 #endif
100
101   return SILC_FSM_FINISH;
102 }
103
104 SILC_FSM_STATE(silc_server_st_packet_new_id)
105 {
106 #if 0
107     /*
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
111      * SILC network.
112      */
113     if (packet->flags & SILC_PACKET_FLAG_LIST)
114       silc_server_new_id_list(server, sock, packet);
115     else
116       silc_server_new_id(server, sock, packet);
117 #endif
118
119   return SILC_FSM_FINISH;
120 }
121
122 SILC_FSM_STATE(silc_server_st_packet_new_channel)
123 {
124 #if 0
125     /*
126      * Received new channel packet. Information about new channel in the
127      * network are distributed using this packet.
128      */
129     if (packet->flags & SILC_PACKET_FLAG_LIST)
130       silc_server_new_channel_list(server, sock, packet);
131     else
132       silc_server_new_channel(server, sock, packet);
133 #endif
134
135   return SILC_FSM_FINISH;
136 }
137
138 SILC_FSM_STATE(silc_server_st_packet_key_agreement)
139 {
140 #if 0
141     if (packet->flags & SILC_PACKET_FLAG_LIST)
142       break;
143     silc_server_key_agreement(server, sock, packet);
144 #endif
145
146   return SILC_FSM_FINISH;
147 }
148
149 SILC_FSM_STATE(silc_server_st_packet_ftp)
150 {
151 #if 0
152     /* FTP packet */
153     if (packet->flags & SILC_PACKET_FLAG_LIST)
154       break;
155     silc_server_ftp(server, sock, packet);
156 #endif
157
158   return SILC_FSM_FINISH;
159 }
160
161 SILC_FSM_STATE(silc_server_st_packet_resume_router)
162 {
163 #if 0
164     /* Resume router packet received. This packet is received for backup
165        router resuming protocol. */
166     if (packet->flags & SILC_PACKET_FLAG_LIST)
167       break;
168     silc_server_backup_resume_router(server, sock, packet);
169 #endif
170
171   return SILC_FSM_FINISH;
172 }
173
174 SILC_FSM_STATE(silc_server_st_packet_received)
175 {
176   SilcServerThread thread = fsm_context;
177   SilcPacket packet = state_context;
178
179   SILC_LOG_DEBUG(("Received %s packet [flags %d]",
180                   silc_get_packet_name(packet->type), packet->flags));
181
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)
187       break;
188     silc_fsm_next(fsm, silc_server_st_packet_channel_message);
189     return SILC_FSM_CONTINUE;
190     break;
191
192   case SILC_PACKET_PRIVATE_MESSAGE:
193     /** Packet PRIVATE_MESSAGE */
194     if (packet->flags & SILC_PACKET_FLAG_LIST)
195       break;
196     silc_fsm_next(fsm, silc_server_st_packet_private_message);
197     return SILC_FSM_CONTINUE;
198     break;
199
200   case SILC_PACKET_NOTIFY:
201     /** Packet NOTIFY */
202     silc_fsm_next(fsm, silc_server_st_packet_notify);
203     return SILC_FSM_CONTINUE;
204     break;
205
206   case SILC_PACKET_COMMAND:
207     /** Packet COMMAND */
208     if (packet->flags & SILC_PACKET_FLAG_LIST)
209       break;
210     silc_fsm_next(fsm, silc_server_st_packet_command);
211     return SILC_FSM_CONTINUE;
212     break;
213
214   case SILC_PACKET_COMMAND_REPLY:
215     /** Packet COMMAND_REPLY */
216     if (packet->flags & SILC_PACKET_FLAG_LIST)
217       break;
218     silc_fsm_next(fsm, silc_server_st_packet_command_reply);
219     return SILC_FSM_CONTINUE;
220     break;
221
222   case SILC_PACKET_CHANNEL_KEY:
223     /** Packet CHANNEL_KEY */
224     if (packet->flags & SILC_PACKET_FLAG_LIST)
225       break;
226     silc_fsm_next(fsm, silc_server_st_packet_channel_key);
227     return SILC_FSM_CONTINUE;
228     break;
229
230   case SILC_PACKET_NEW_ID:
231     /** Packet NEW_ID */
232     silc_fsm_next(fsm, silc_server_st_packet_new_id);
233     return SILC_FSM_CONTINUE;
234     break;
235
236   case SILC_PACKET_NEW_CLIENT:
237     /** Packet NEW_CLIENT */
238     if (packet->flags & SILC_PACKET_FLAG_LIST)
239       break;
240     silc_fsm_next(fsm, silc_server_st_packet_new_client);
241     return SILC_FSM_CONTINUE;
242     break;
243
244   case SILC_PACKET_NEW_SERVER:
245     /** Packet NEW_SERVER */
246     if (packet->flags & SILC_PACKET_FLAG_LIST)
247       break;
248     silc_fsm_next(fsm, silc_server_st_packet_new_server);
249     return SILC_FSM_CONTINUE;
250     break;
251
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;
256     break;
257
258   case SILC_PACKET_KEY_AGREEMENT:
259     /** Packet KEY_AGREEMENT */
260     if (packet->flags & SILC_PACKET_FLAG_LIST)
261       break;
262     silc_fsm_next(fsm, silc_server_st_packet_key_agreement);
263     return SILC_FSM_CONTINUE;
264     break;
265
266   case SILC_PACKET_FTP:
267     /** Packet FTP */
268     if (packet->flags & SILC_PACKET_FLAG_LIST)
269       break;
270     silc_fsm_next(fsm, silc_server_st_packet_ftp);
271     return SILC_FSM_CONTINUE;
272     break;
273
274   case SILC_PACKET_RESUME_CLIENT:
275     /** Packet RESUME_CLIENT */
276     if (packet->flags & SILC_PACKET_FLAG_LIST)
277       break;
278     silc_fsm_next(fsm, silc_server_st_packet_resume_client);
279     return SILC_FSM_CONTINUE;
280     break;
281
282   case SILC_PACKET_RESUME_ROUTER:
283     /** Packet RESUME_ROUTER */
284     if (packet->flags & SILC_PACKET_FLAG_LIST)
285       break;
286     silc_fsm_next(fsm, silc_server_st_packet_resume_router);
287     return SILC_FSM_CONTINUE;
288     break;
289
290   case SILC_PACKET_DISCONNECT:
291     /** Packet DISCONNECT */
292     if (packet->flags & SILC_PACKET_FLAG_LIST)
293       break;
294     silc_fsm_next(fsm, silc_server_st_packet_disconnect);
295     return SILC_FSM_CONTINUE;
296     break;
297
298   case SILC_PACKET_PRIVATE_MESSAGE_KEY:
299     /** Packet PRIVATE_MESSAGE */
300     if (packet->flags & SILC_PACKET_FLAG_LIST)
301       break;
302     silc_fsm_next(fsm, silc_server_st_packet_private_message_key);
303     return SILC_FSM_CONTINUE;
304     break;
305
306   case SILC_PACKET_CONNECTION_AUTH_REQUEST:
307     /** Packet CONNECTION_AUTH_REQUEST */
308     if (packet->flags & SILC_PACKET_FLAG_LIST)
309       break;
310     silc_fsm_next(fsm, silc_server_st_packet_connection_auth_request);
311     return SILC_FSM_CONTINUE;
312     break;
313
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:
325     /* Not handled */
326     break;
327
328   default:
329     SILC_LOG_ERROR(("Unsupported packet type %d", packet->type));
330     break;
331   }
332
333   silc_packet_free(packet);
334   return SILC_FSM_FINISH;
335 }
336
337 /* Received NEW_CLIENT packet, used to register client to SILC network. */
338
339 SILC_FSM_STATE(silc_server_st_packet_new_client)
340 {
341   SilcServerThread thread = fsm_context;
342   SilcPacket packet = state_context;
343   SilcServerAccept ac = silc_packet_get_context(packet->stream);
344
345   if (!ac || ac->register_packet) {
346     silc_packet_free(packet);
347     return SILC_FSM_FINISH;
348   }
349
350   /* Signal that client registers to network */
351   ac->register_packet = packet;
352   SILC_FSM_SEMA_POST(&ac->wait_register);
353
354   return SILC_FSM_FINISH;
355 }
356
357 /* Received NEW_SERVER packet, used to register server to SILC network. */
358
359 SILC_FSM_STATE(silc_server_st_packet_new_server)
360 {
361   SilcServerThread thread = fsm_context;
362   SilcPacket packet = state_context;
363   SilcServerAccept ac = silc_packet_get_context(packet->stream);
364
365   if (!ac || ac->register_packet) {
366     silc_packet_free(packet);
367     return SILC_FSM_FINISH;
368   }
369
370   /* Signal that server registers to network */
371   ac->register_packet = packet;
372   SILC_FSM_SEMA_POST(&ac->wait_register);
373
374   return SILC_FSM_FINISH;
375 }
376
377 /* Received RESUME_CLIENT packet, used to resume detached session. */
378
379 SILC_FSM_STATE(silc_server_st_packet_resume_client)
380 {
381   SilcServerThread thread = fsm_context;
382   SilcPacket packet = state_context;
383   SilcServerAccept ac = silc_packet_get_context(packet->stream);
384
385   if (!ac || ac->register_packet) {
386     silc_packet_free(packet);
387     return SILC_FSM_FINISH;
388   }
389
390   /* Signal that client resumes session */
391   ac->register_packet = packet;
392   SILC_FSM_SEMA_POST(&ac->wait_register);
393
394   return SILC_FSM_FINISH;
395 }