More client library rewrites (added rekey)
[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 - 2006 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 /* Context to hold the connection authentication request callbacks that
53    will be called when the server has replied back to our request about
54    current authentication method in the session. */
55 typedef struct {
56   SilcConnectionAuthRequest callback;
57   void *context;
58   SilcTask timeout;
59 } *SilcClientConnAuthRequest;
60
61 /* Generic rekey context for connections */
62 typedef struct {
63   /* Current sending encryption key, provided for re-key. The `pfs'
64      is TRUE if the Perfect Forward Secrecy is performed in re-key. */
65   unsigned char *send_enc_key;
66   SilcUInt32 enc_key_len;
67   int ske_group;
68   SilcBool pfs;
69   SilcUInt32 timeout;
70   void *context;
71 } *SilcClientRekey;
72
73 /* Internal context for connection process. This is needed as we
74    doing asynchronous connecting. */
75 typedef struct {
76   SilcClient client;
77   SilcClientConnection conn;
78   SilcTask task;
79   int sock;
80   char *host;
81   int port;
82   void *context;
83 } SilcClientInternalConnectContext;
84
85 /* Structure to hold away messages set by user. This is mainly created
86    for future extensions where away messages could be set according filters
87    such as nickname and hostname. For now only one away message can
88    be set in one connection. */
89 struct SilcClientAwayStruct {
90   char *away;
91   struct SilcClientAwayStruct *next;
92 };
93
94 /* Command and command reply context used to hold registered commands
95    in the SILC client. */
96 typedef struct SilcClientCommandStruct {
97   struct SilcClientCommandStruct *next;
98   SilcCommand cmd;                    /* Command type */
99   SilcFSMStateCallback command;       /* Command function */
100   SilcFSMStateCallback reply;         /* Command reply callback */
101   char *name;                         /* Name of the command (optional) */
102   SilcUInt8 max_args;                 /* Maximum arguments (optional)  */
103 } *SilcClientCommand;
104
105 /* Command reply callback structure */
106 typedef struct SilcClientCommandReplyCallbackStruct  {
107   struct SilcClientCommandReplyCallbackStruct *next;
108   SilcClientCommandReply reply;       /* Command reply callback */
109   void *context;                      /* Command reply context */
110   unsigned int do_not_call     : 1;   /* Set to not call the callback */
111 } *SilcClientCommandReplyCallback;
112
113 /* Command context given as argument to command state functions.  This same
114    context is used when calling, sending and procesing command and command
115    reply. */
116 typedef struct SilcClientCommandContextStruct {
117   struct SilcClientCommandContextStruct *next;
118   SilcClientConnection conn;          /* Connection */
119   SilcFSMThreadStruct thread;         /* FSM thread for command call */
120
121   SilcCommand cmd;                    /* Command */
122   SilcUInt16 cmd_ident;               /* Command identifier */
123   SilcUInt32 argc;                    /* Number of arguments */
124   unsigned char **argv;               /* Arguments, may be NULL */
125   SilcUInt32 *argv_lens;              /* Argument lengths, may be NULL */
126   SilcUInt32 *argv_types;             /* Argument types, may be NULL */
127
128   SilcList reply_callbacks;           /* Command reply callbacks */
129   SilcStatus status;                  /* Current command reply status */
130   SilcStatus error;                   /* Current command reply error */
131
132   void *context;                      /* Context for free use */
133   unsigned int called        : 1;     /* Set when called by application */
134   unsigned int verbose       : 1;     /* Verbose with 'say' client operation */
135   unsigned int resolved      : 1;     /* Set when resolving something */
136 } *SilcClientCommandContext;
137
138 /* Internal context for the client->internal pointer in the SilcClient. */
139 struct SilcClientInternalStruct {
140   SilcFSMStruct fsm;                     /* Client's FSM */
141   SilcFSMSemaStruct wait_event;          /* Event signaller */
142   SilcClientOperations *ops;             /* Client operations */
143   SilcClientParams *params;              /* Client parameters */
144   SilcPacketEngine packet_engine;        /* Packet engine */
145   SilcMutex lock;                        /* Client lock */
146   SilcList commands;                     /* Registered commands */
147   char *silc_client_version;             /* Version set by application */
148
149   /* Events */
150   unsigned int run_callback    : 1;      /* Call running callback */
151 };
152
153 /* Internal context for conn->internal in SilcClientConnection. */
154 struct SilcClientConnectionInternalStruct {
155   SilcIDCacheEntry local_entry;          /* Local client cache entry */
156   SilcClientConnectionParams params;     /* Connection parameters */
157
158   SilcFSMStruct fsm;                     /* Connection FSM */
159   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
160   SilcFSMSemaStruct wait_event;          /* Event signaller */
161   SilcSchedule schedule;                 /* Connection's scheduler */
162   SilcMutex lock;                        /* Connection lock */
163   SilcSKE ske;                           /* Key exchange protocol */
164   SilcSKERekeyMaterial rekey;            /* Rekey material */
165   SilcList thread_pool;                  /* Packet thread pool */
166   SilcList pending_commands;             /* Pending commands list */
167   SilcHash hash;                         /* Negotiated hash function */
168   SilcHash sha1hash;                     /* SHA-1 default hash context */
169   SilcBuffer local_idp;                  /* Local ID Payload */
170   SilcBuffer remote_idp;                 /* Remote ID Payload */
171   SilcAsyncOperation op;                 /* Protocols async operation */
172
173   SilcIDCache client_cache;              /* Client entry cache */
174   SilcIDCache channel_cache;             /* Channel entry cache */
175   SilcIDCache server_cache;              /* Server entry cache */
176
177   SilcAtomic16 cmd_ident;                /* Current command identifier */
178   SilcUInt8 retry_count;                 /* Packet retry counter */
179   SilcUInt8 retry_timer;                 /* Packet retry timer */
180
181   /* Events */
182   unsigned int connect            : 1;   /* Connect remote host */
183   unsigned int disconnected       : 1;   /* Disconnected by remote host */
184   unsigned int key_exchange       : 1;   /* Start key exchange */
185   unsigned int rekeying           : 1;   /* Start rekey */
186
187   /* Flags */
188   unsigned int verbose            : 1;   /* Notify application */
189   unsigned int registering        : 1;   /* Set when registering to network */
190   unsigned int rekey_responder    : 1;   /* Set when rekeying as responder */
191
192   SilcClientAway *away;
193   SilcClientConnAuthRequest connauth;
194   SilcDList ftp_sessions;
195   SilcUInt32 next_session_id;
196   SilcClientFtpSession active_session;
197   SilcHashTable attrs;
198   SilcHashTable privmsg_wait;            /* Waited private messages */
199 };
200
201 SILC_FSM_STATE(silc_client_connection_st_run);
202 SILC_FSM_STATE(silc_client_connection_st_packet);
203 SILC_FSM_STATE(silc_client_connection_st_close);
204 SILC_FSM_STATE(silc_client_error);
205 SILC_FSM_STATE(silc_client_disconnect);
206
207 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
208 SilcBool silc_client_del_client(SilcClient client, SilcClientConnection conn,
209                                 SilcClientEntry client_entry);
210 SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn,
211                                  SilcChannelEntry channel);
212 SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn,
213                                 SilcServerEntry server);
214 SilcUInt16 silc_client_command_send_argv(SilcClient client,
215                                          SilcClientConnection conn,
216                                          SilcCommand command,
217                                          SilcClientCommandReply reply,
218                                          void *reply_context,
219                                          SilcUInt32 argc,
220                                          unsigned char **argv,
221                                          SilcUInt32 *argv_lens,
222                                          SilcUInt32 *argv_types);
223 void silc_client_command_free(SilcClientCommandContext cmd);
224 void silc_client_fsm_destructor(SilcFSM fsm, void *fsm_context,
225                                 void *destructor_context);
226
227 void silc_client_ftp(SilcClient client, SilcClientConnection conn,
228                      SilcPacket packet);
229 void silc_client_connection_auth_request(SilcClient client,
230                                          SilcClientConnection conn,
231                                          SilcPacket packet);
232
233 #endif