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