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