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 /* 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   /* Client version. Used to compare to remote host's version strings. */
134   char *silc_client_version;
135
136   /* Events */
137   unsigned int run_callback    : 1;      /* Call running callback */
138 };
139
140 /* Internal context for conn->internal in SilcClientConnection. */
141 struct SilcClientConnectionInternalStruct {
142   SilcIDCacheEntry local_entry;          /* Local client cache entry */
143   SilcClientConnectionParams params;     /* Connection parameters */
144
145   SilcFSMStruct fsm;                     /* Connection FSM */
146   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
147   SilcFSMSemaStruct wait_event;          /* Event signaller */
148   SilcSchedule schedule;                 /* Connection's scheduler */
149   SilcMutex lock;                        /* Connection lock */
150   SilcSKE ske;                           /* Key exchange protocol */
151   SilcSKERekeyMaterial rekey;            /* Rekey material */
152   SilcList thread_pool;                  /* Packet thread pool */
153   SilcList pending_commands;             /* Pending commands list */
154   SilcHash hash;                         /* Negotiated hash function */
155   SilcHash sha1hash;                     /* SHA-1 default hash context */
156
157   SilcIDCache client_cache;              /* Client entry cache */
158   SilcIDCache channel_cache;             /* Channel entry cache */
159   SilcIDCache server_cache;              /* Server entry cache */
160
161   SilcBuffer local_idp;                  /* Local ID Payload */
162   SilcBuffer remote_idp;                 /* Remote ID Payload */
163
164   SilcAtomic16 cmd_ident;                /* Current command identifier */
165
166   /* Events */
167   unsigned int connect            : 1;   /* Connect remote host */
168   unsigned int disconnected       : 1;   /* Disconnected by remote host */
169   unsigned int key_exchange       : 1;   /* Start key exchange */
170
171   /* Flags */
172   unsigned int verbose            : 1;   /* Notify application */
173   unsigned int registering        : 1;   /* Set when registering to network */
174
175   SilcClientAway *away;
176   SilcClientConnAuthRequest connauth;
177   SilcDList ftp_sessions;
178   SilcUInt32 next_session_id;
179   SilcClientFtpSession active_session;
180   SilcHashTable attrs;
181   SilcHashTable privmsg_wait;            /* Waited private messages */
182 };
183
184 SILC_FSM_STATE(silc_client_connection_st_run);
185 SILC_FSM_STATE(silc_client_connection_st_packet);
186 SILC_FSM_STATE(silc_client_connection_st_close);
187 SILC_FSM_STATE(silc_client_error);
188 SILC_FSM_STATE(silc_client_disconnect);
189
190 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
191 SilcBool silc_client_del_client(SilcClient client, SilcClientConnection conn,
192                                 SilcClientEntry client_entry);
193 SilcBool silc_client_del_channel(SilcClient client, SilcClientConnection conn,
194                                  SilcChannelEntry channel);
195 SilcBool silc_client_del_server(SilcClient client, SilcClientConnection conn,
196                                 SilcServerEntry server);
197 SilcUInt16 silc_client_command_send_argv(SilcClient client,
198                                          SilcClientConnection conn,
199                                          SilcCommand command,
200                                          SilcClientCommandReply reply,
201                                          void *reply_context,
202                                          SilcUInt32 argc,
203                                          unsigned char **argv,
204                                          SilcUInt32 *argv_lens,
205                                          SilcUInt32 *argv_types);
206
207 void silc_client_ftp(SilcClient client, SilcClientConnection conn,
208                      SilcPacket packet);
209 void silc_client_key_agreement(SilcClient client,
210                                SilcClientConnection conn,
211                                SilcPacket packet);
212 void silc_client_connection_auth_request(SilcClient client,
213                                          SilcClientConnection conn,
214                                          SilcPacket packet);
215
216 #endif