Nickname formatting/parsing rewrite.
[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   SilcFSMEventStruct 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   SilcClientRunning running;             /* Running/Stopped callback */
149   void *running_context;                 /* Context for runnign callback */
150   SilcAtomic16 conns;                    /* Number of connections in client */
151
152   /* Events */
153   unsigned int stop              : 1;    /* Stop client */
154   unsigned int run_callback      : 1;    /* Call running/stopped callback */
155   unsigned int connection_closed : 1;    /* A connection closed */
156 };
157
158 /* Internal context for conn->internal in SilcClientConnection. */
159 struct SilcClientConnectionInternalStruct {
160   SilcClientConnectionParams params;     /* Connection parameters */
161   SilcFSMStruct fsm;                     /* Connection FSM */
162   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
163   SilcFSMEventStruct wait_event;                 /* Event signaller */
164   SilcSchedule schedule;                 /* Connection's scheduler */
165   SilcMutex lock;                        /* Connection lock */
166   SilcSKE ske;                           /* Key exchange protocol */
167   SilcSKERekeyMaterial rekey;            /* Rekey material */
168   SilcList thread_pool;                  /* Packet thread pool */
169   SilcList pending_commands;             /* Pending commands list */
170   SilcHash hash;                         /* Negotiated hash function */
171   SilcHash sha1hash;                     /* SHA-1 default hash context */
172   SilcBuffer local_idp;                  /* Local ID Payload */
173   SilcBuffer remote_idp;                 /* Remote ID Payload */
174   SilcAsyncOperation op;                 /* Protocols async operation */
175   SilcAsyncOperation cop;                /* Async operation for application */
176
177   SilcIDCache client_cache;              /* Client entry cache */
178   SilcIDCache channel_cache;             /* Channel entry cache */
179   SilcIDCache server_cache;              /* Server entry cache */
180
181   SilcAtomic16 cmd_ident;                /* Current command identifier */
182   SilcUInt8 retry_count;                 /* Packet retry counter */
183   SilcUInt8 retry_timer;                 /* Packet retry timer */
184
185   /* Events */
186   unsigned int connect            : 1;   /* Connect remote host */
187   unsigned int disconnected       : 1;   /* Disconnect remote connection */
188   unsigned int key_exchange       : 1;   /* Start key exchange */
189   unsigned int rekeying           : 1;   /* Start rekey */
190
191   /* Flags */
192   unsigned int verbose            : 1;   /* Notify application */
193   unsigned int registering        : 1;   /* Set when registering to network */
194   unsigned int rekey_responder    : 1;   /* Set when rekeying as responder */
195   unsigned int callback_called    : 1;   /* Set when connect callback called */
196
197   SilcClientAway *away;
198   SilcClientConnAuthRequest connauth;
199   SilcDList ftp_sessions;
200   SilcUInt32 next_session_id;
201   SilcClientFtpSession active_session;
202   SilcHashTable attrs;
203   SilcHashTable privmsg_wait;            /* Waited private messages */
204 };
205
206 SILC_FSM_STATE(silc_client_connection_st_run);
207 SILC_FSM_STATE(silc_client_connection_st_packet);
208 SILC_FSM_STATE(silc_client_connection_st_close);
209 SILC_FSM_STATE(silc_client_error);
210 SILC_FSM_STATE(silc_client_disconnect);
211 SILC_FSM_STATE(silc_client_st_stop);
212
213 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
214 SilcBool silc_client_del_client(SilcClient client, SilcClientConnection conn,
215                                 SilcClientEntry client_entry);
216 SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn,
217                                  SilcChannelEntry channel);
218 SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn,
219                                 SilcServerEntry server);
220 SilcUInt16 silc_client_command_send_argv(SilcClient client,
221                                          SilcClientConnection conn,
222                                          SilcCommand command,
223                                          SilcClientCommandReply reply,
224                                          void *reply_context,
225                                          SilcUInt32 argc,
226                                          unsigned char **argv,
227                                          SilcUInt32 *argv_lens,
228                                          SilcUInt32 *argv_types);
229 void silc_client_command_free(SilcClientCommandContext cmd);
230 void silc_client_fsm_destructor(SilcFSM fsm, void *fsm_context,
231                                 void *destructor_context);
232
233 void silc_client_ftp(SilcClient client, SilcClientConnection conn,
234                      SilcPacket packet);
235 void silc_client_connection_auth_request(SilcClient client,
236                                          SilcClientConnection conn,
237                                          SilcPacket packet);
238
239 #endif