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