SILC_FSM_* macro API changes.
[silc.git] / lib / silcclient / client_internal.h
1 /*
2
3   client_internal.h
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 #ifndef CLIENT_INTERNAL_H
21 #define CLIENT_INTERNAL_H
22
23 #include "command.h"
24 #include "command_reply.h"
25 #include "client_connect.h"
26 #include "client_register.h"
27 #include "client_entry.h"
28 #include "client_prvmsg.h"
29 #include "client_channel.h"
30 #include "client_notify.h"
31 #include "client_keyagr.h"
32
33 /****************************** Definitions *********************************/
34
35 /* Packet retry counter and timer defines, for exponential backoff algorithm.
36    Meaningful with UDP transport when packets may get lost. */
37 #define SILC_CLIENT_RETRY_COUNT   4      /* Max packet retry count */
38 #define SILC_CLIENT_RETRY_MUL     2      /* Retry timer interval growth */
39 #define SILC_CLIENT_RETRY_RAND    2      /* Randomizer, timeout += rnd % 2 */
40 #define SILC_CLIENT_RETRY_MIN     1      /* Min retry timeout, seconds */
41 #define SLIC_CLIENT_RETRY_MAX     16     /* Max retry timeout, seconds */
42
43 /********************************** Types ***********************************/
44
45 /* Public key verification context */
46 typedef struct {
47   SilcSKE ske;
48   SilcSKEVerifyCbCompletion completion;
49   void *completion_context;
50 } *VerifyKeyContext;
51
52 /* Structure to hold away messages set by user. This is mainly created
53    for future extensions where away messages could be set according filters
54    such as nickname and hostname. For now only one away message can
55    be set in one connection. */
56 struct SilcClientAwayStruct {
57   char *away;
58   struct SilcClientAwayStruct *next;
59 };
60
61 /* Command and command reply context used to hold registered commands
62    in the SILC client. */
63 typedef struct SilcClientCommandStruct {
64   struct SilcClientCommandStruct *next;
65   SilcCommand cmd;                    /* Command type */
66   SilcFSMStateCallback command;       /* Command function */
67   SilcFSMStateCallback reply;         /* Command reply callback */
68   char *name;                         /* Name of the command (optional) */
69   SilcUInt8 max_args;                 /* Maximum arguments (optional)  */
70 } *SilcClientCommand;
71
72 /* Command reply callback structure */
73 typedef struct SilcClientCommandReplyCallbackStruct  {
74   struct SilcClientCommandReplyCallbackStruct *next;
75   SilcClientCommandReply reply;       /* Command reply callback */
76   void *context;                      /* Command reply context */
77   unsigned int do_not_call     : 1;   /* Set to not call the callback */
78 } *SilcClientCommandReplyCallback;
79
80 /* Command context given as argument to command state functions.  This same
81    context is used when calling, sending and procesing command and command
82    reply. */
83 typedef struct SilcClientCommandContextStruct {
84   struct SilcClientCommandContextStruct *next;
85   SilcClientConnection conn;          /* Connection */
86   SilcFSMThreadStruct thread;         /* FSM thread for command call */
87
88   SilcCommand cmd;                    /* Command */
89   SilcUInt16 cmd_ident;               /* Command identifier */
90   SilcUInt32 argc;                    /* Number of arguments */
91   unsigned char **argv;               /* Arguments, may be NULL */
92   SilcUInt32 *argv_lens;              /* Argument lengths, may be NULL */
93   SilcUInt32 *argv_types;             /* Argument types, may be NULL */
94
95   SilcList reply_callbacks;           /* Command reply callbacks */
96   SilcStatus status;                  /* Current command reply status */
97   SilcStatus error;                   /* Current command reply error */
98
99   void *context;                      /* Context for free use */
100   unsigned int called        : 1;     /* Set when called by application */
101   unsigned int verbose       : 1;     /* Verbose with 'say' client operation */
102   unsigned int resolved      : 1;     /* Set when resolving something */
103 } *SilcClientCommandContext;
104
105 /* Internal context for the client->internal pointer in the SilcClient. */
106 struct SilcClientInternalStruct {
107   SilcFSMStruct fsm;                     /* Client's FSM */
108   SilcFSMEventStruct wait_event;                 /* Event signaller */
109   SilcClientOperations *ops;             /* Client operations */
110   SilcClientParams *params;              /* Client parameters */
111   SilcPacketEngine packet_engine;        /* Packet engine */
112   SilcMutex lock;                        /* Client lock */
113   SilcList commands;                     /* Registered commands */
114   SilcDList ftp_sessions;                /* FTP sessions */
115   char *silc_client_version;             /* Version set by application */
116   SilcClientRunning running;             /* Running/Stopped callback */
117   void *running_context;                 /* Context for runnign callback */
118   SilcAtomic16 conns;                    /* Number of connections in client */
119   SilcUInt16 next_session_id;            /* Next FTP session ID */
120
121   /* Events */
122   unsigned int stop              : 1;    /* Stop client */
123   unsigned int run_callback      : 1;    /* Call running/stopped callback */
124   unsigned int connection_closed : 1;    /* A connection closed */
125 };
126
127 /* Internal context for conn->internal in SilcClientConnection. */
128 struct SilcClientConnectionInternalStruct {
129   SilcClientConnectionParams params;     /* Connection parameters */
130   SilcFSMStruct fsm;                     /* Connection FSM */
131   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
132   SilcFSMEventStruct wait_event;         /* Event signaller */
133   SilcSchedule schedule;                 /* Connection's scheduler */
134   SilcMutex lock;                        /* Connection lock */
135   SilcSKE ske;                           /* Key exchange protocol */
136   SilcSKERekeyMaterial rekey;            /* Rekey material */
137   SilcList thread_pool;                  /* Packet thread pool */
138   SilcList pending_commands;             /* Pending commands list */
139   SilcHash hash;                         /* Negotiated hash function */
140   SilcHash sha1hash;                     /* SHA-1 default hash context */
141   SilcBuffer local_idp;                  /* Local ID Payload */
142   SilcBuffer remote_idp;                 /* Remote ID Payload */
143   SilcAsyncOperation op;                 /* Protocols async operation */
144   SilcAsyncOperation cop;                /* Async operation for application */
145   SilcHashTable attrs;                   /* Configured user attributes */
146
147   SilcIDCache client_cache;              /* Client entry cache */
148   SilcIDCache channel_cache;             /* Channel entry cache */
149   SilcIDCache server_cache;              /* Server entry cache */
150
151   SilcAtomic16 cmd_ident;                /* Current command identifier */
152   SilcUInt8 retry_count;                 /* Packet retry counter */
153   SilcUInt8 retry_timer;                 /* Packet retry timer */
154
155   /* Events */
156   unsigned int connect            : 1;   /* Connect remote host */
157   unsigned int disconnected       : 1;   /* Disconnect remote connection */
158   unsigned int key_exchange       : 1;   /* Start key exchange */
159   unsigned int rekeying           : 1;   /* Start rekey */
160
161   /* Flags */
162   unsigned int verbose            : 1;   /* Notify application */
163   unsigned int registering        : 1;   /* Set when registering to network */
164   unsigned int rekey_responder    : 1;   /* Set when rekeying as responder */
165   unsigned int callback_called    : 1;   /* Set when connect callback called */
166   unsigned int auth_request       : 1;   /* Set when requesting auth method */
167
168   SilcClientAway *away;
169   SilcClientFtpSession active_session;
170   SilcHashTable privmsg_wait;            /* Waited private messages */
171 };
172
173 SILC_FSM_STATE(silc_client_connection_st_run);
174 SILC_FSM_STATE(silc_client_connection_st_packet);
175 SILC_FSM_STATE(silc_client_connection_st_close);
176 SILC_FSM_STATE(silc_client_error);
177 SILC_FSM_STATE(silc_client_disconnect);
178 SILC_FSM_STATE(silc_client_st_stop);
179
180 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
181 SilcBool silc_client_del_client(SilcClient client, SilcClientConnection conn,
182                                 SilcClientEntry client_entry);
183 SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn,
184                                  SilcChannelEntry channel);
185 SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn,
186                                 SilcServerEntry server);
187 SilcUInt16 silc_client_command_send_argv(SilcClient client,
188                                          SilcClientConnection conn,
189                                          SilcCommand command,
190                                          SilcClientCommandReply reply,
191                                          void *reply_context,
192                                          SilcUInt32 argc,
193                                          unsigned char **argv,
194                                          SilcUInt32 *argv_lens,
195                                          SilcUInt32 *argv_types);
196 void silc_client_command_free(SilcClientCommandContext cmd);
197 void silc_client_fsm_destructor(SilcFSM fsm, void *fsm_context,
198                                 void *destructor_context);
199
200 void silc_client_ftp(SilcClient client, SilcClientConnection conn,
201                      SilcPacket packet);
202 void silc_client_connection_auth_request(SilcClient client,
203                                          SilcClientConnection conn,
204                                          SilcPacket packet);
205
206 #endif