Fix reference count bug leading to memory corruption on duplicate deletions.
[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 - 2007 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 #include "client_ftp.h"
33 #include "client_listener.h"
34
35 /****************************** Definitions *********************************/
36
37 /* Packet retry counter and timer defines, for exponential backoff algorithm.
38    Meaningful with UDP transport when packets may get lost. */
39 #define SILC_CLIENT_RETRY_COUNT   4      /* Max packet retry count */
40 #define SILC_CLIENT_RETRY_MUL     2      /* Retry timer interval growth */
41 #define SILC_CLIENT_RETRY_RAND    2      /* Randomizer, timeout += rnd % 2 */
42 #define SILC_CLIENT_RETRY_MIN     1      /* Min retry timeout, seconds */
43 #define SLIC_CLIENT_RETRY_MAX     16     /* Max retry timeout, seconds */
44
45 /********************************** Types ***********************************/
46
47 /* Public key verification context */
48 typedef struct {
49   SilcSKE ske;
50   SilcSKEVerifyCbCompletion completion;
51   void *completion_context;
52 } *SilcVerifyKeyContext;
53
54 /* Command and command reply context used to hold registered commands
55    in the SILC client. */
56 typedef struct SilcClientCommandStruct {
57   struct SilcClientCommandStruct *next;
58   SilcCommand cmd;                    /* Command type */
59   SilcFSMStateCallback command;       /* Command function */
60   SilcFSMStateCallback reply;         /* Command reply callback */
61   char *name;                         /* Name of the command (optional) */
62   SilcUInt8 max_args;                 /* Maximum arguments (optional)  */
63 } *SilcClientCommand;
64
65 /* Command reply callback structure */
66 typedef struct SilcClientCommandReplyCallbackStruct  {
67   struct SilcClientCommandReplyCallbackStruct *next;
68   SilcClientCommandReply reply;       /* Command reply callback */
69   void *context;                      /* Command reply context */
70   unsigned int do_not_call     : 1;   /* Set to not call the callback */
71 } *SilcClientCommandReplyCallback;
72
73 /* Command context given as argument to command state functions.  This same
74    context is used when calling, sending and procesing command and command
75    reply. */
76 typedef struct SilcClientCommandContextStruct {
77   struct SilcClientCommandContextStruct *next;
78   SilcClientConnection conn;          /* Connection */
79   SilcFSMThreadStruct thread;         /* FSM thread for command call */
80
81   SilcCommand cmd;                    /* Command */
82   SilcUInt16 cmd_ident;               /* Command identifier */
83   SilcUInt32 argc;                    /* Number of arguments */
84   unsigned char **argv;               /* Arguments, may be NULL */
85   SilcUInt32 *argv_lens;              /* Argument lengths, may be NULL */
86   SilcUInt32 *argv_types;             /* Argument types, may be NULL */
87
88   SilcList reply_callbacks;           /* Command reply callbacks */
89   SilcStatus status;                  /* Current command reply status */
90   SilcStatus error;                   /* Current command reply error */
91
92   void *context;                      /* Context for free use */
93   unsigned int called        : 1;     /* Set when called by application */
94   unsigned int verbose       : 1;     /* Verbose with 'say' client operation */
95   unsigned int resolved      : 1;     /* Set when resolving something */
96 } *SilcClientCommandContext;
97
98 /* Internal context for the client->internal pointer in the SilcClient. */
99 struct SilcClientInternalStruct {
100   SilcFSMStruct fsm;                     /* Client's FSM */
101   SilcFSMEventStruct wait_event;         /* Event signaller */
102   SilcClientOperations *ops;             /* Client operations */
103   SilcClientParams *params;              /* Client parameters */
104   SilcPacketEngine packet_engine;        /* Packet engine */
105   SilcMutex lock;                        /* Client lock */
106   SilcList commands;                     /* Registered commands */
107   SilcDList ftp_sessions;                /* FTP sessions */
108   char *silc_client_version;             /* Version set by application */
109   SilcClientRunning running;             /* Running/Stopped callback */
110   void *running_context;                 /* Context for runnign callback */
111   SilcAtomic32 conns;                    /* Number of connections in client */
112   SilcUInt16 next_session_id;            /* Next FTP session ID */
113
114   /* Events */
115   unsigned int stop              : 1;    /* Stop client */
116   unsigned int run_callback      : 1;    /* Call running/stopped callback */
117   unsigned int connection_closed : 1;    /* A connection closed */
118 };
119
120 /* Internal context for conn->internal in SilcClientConnection. */
121 struct SilcClientConnectionInternalStruct {
122   SilcClientConnectionParams params;     /* Connection parameters */
123   SilcFSMStruct fsm;                     /* Connection FSM */
124   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
125   SilcFSMEventStruct wait_event;         /* Event signaller */
126   SilcSchedule schedule;                 /* Connection's scheduler */
127   SilcMutex lock;                        /* Connection lock */
128   SilcSKE ske;                           /* Key exchange protocol */
129   SilcSKERekeyMaterial rekey;            /* Rekey material */
130   SilcList thread_pool;                  /* Packet thread pool */
131   SilcList pending_commands;             /* Pending commands list */
132   SilcHash hash;                         /* Negotiated hash function */
133   SilcHash sha1hash;                     /* SHA-1 default hash context */
134   SilcBuffer local_idp;                  /* Local ID Payload */
135   SilcBuffer remote_idp;                 /* Remote ID Payload */
136   SilcAsyncOperation op;                 /* Protocols async operation */
137   SilcAsyncOperation cop;                /* Async operation for application */
138   SilcHashTable attrs;                   /* Configured user attributes */
139   SilcStream user_stream;                /* Low level stream in connecting */
140   char *disconnect_message;              /* Disconnection message */
141   char *away_message;                    /* Away message */
142
143   SilcIDCache client_cache;              /* Client entry cache */
144   SilcIDCache channel_cache;             /* Channel entry cache */
145   SilcIDCache server_cache;              /* Server entry cache */
146
147   SilcUInt32 remote_version;             /* Remote SILC protocol version */
148   SilcAtomic16 cmd_ident;                /* Current command identifier */
149   SilcUInt8 retry_count;                 /* Packet retry counter */
150   SilcUInt8 retry_timer;                 /* Packet retry timer */
151   SilcClientConnectionStatus status;     /* Connection callback status */
152   SilcStatus error;                      /* Connection callback error */
153
154   /* Events */
155   unsigned int connect            : 1;   /* Connect remote host */
156   unsigned int disconnected       : 1;   /* Disconnect remote connection */
157   unsigned int key_exchange       : 1;   /* Start key exchange */
158   unsigned int rekeying           : 1;   /* Start rekey */
159
160   /* Flags */
161   unsigned int verbose            : 1;   /* Notify application */
162   unsigned int registering        : 1;   /* Set when registering to network */
163   unsigned int rekey_responder    : 1;   /* Set when rekeying as responder */
164   unsigned int auth_request       : 1;   /* Set when requesting auth method */
165 };
166
167 SILC_FSM_STATE(silc_client_connection_st_run);
168 SILC_FSM_STATE(silc_client_connection_st_packet);
169 SILC_FSM_STATE(silc_client_connection_st_close);
170 SILC_FSM_STATE(silc_client_error);
171 SILC_FSM_STATE(silc_client_disconnect);
172 SILC_FSM_STATE(silc_client_st_stop);
173
174 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
175 void silc_client_fsm_destructor(SilcFSM fsm, void *fsm_context,
176                                 void *destructor_context);
177 void silc_client_command_free(SilcClientCommandContext cmd);
178 SilcClientConnection
179 silc_client_add_connection(SilcClient client,
180                            SilcConnectionType conn_type,
181                            SilcBool connect,
182                            SilcClientConnectionParams *params,
183                            SilcPublicKey public_key,
184                            SilcPrivateKey private_key,
185                            char *remote_host, int port,
186                            SilcClientConnectCallback callback,
187                            void *context);
188 SilcBuffer silc_client_attributes_process(SilcClient client,
189                                           SilcClientConnection conn,
190                                           SilcDList attrs);
191
192 #endif /* CLIENT_INTERNAL_H */