Added entry locking using read/write locks.
[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
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 } *SilcVerifyKeyContext;
51
52 /* Command and command reply context used to hold registered commands
53    in the SILC client. */
54 typedef struct SilcClientCommandStruct {
55   struct SilcClientCommandStruct *next;
56   SilcCommand cmd;                    /* Command type */
57   SilcFSMStateCallback command;       /* Command function */
58   SilcFSMStateCallback reply;         /* Command reply callback */
59   char *name;                         /* Name of the command (optional) */
60   SilcUInt8 max_args;                 /* Maximum arguments (optional)  */
61 } *SilcClientCommand;
62
63 /* Command reply callback structure */
64 typedef struct SilcClientCommandReplyCallbackStruct  {
65   struct SilcClientCommandReplyCallbackStruct *next;
66   SilcClientCommandReply reply;       /* Command reply callback */
67   void *context;                      /* Command reply context */
68   unsigned int do_not_call     : 1;   /* Set to not call the callback */
69 } *SilcClientCommandReplyCallback;
70
71 /* Command context given as argument to command state functions.  This same
72    context is used when calling, sending and procesing command and command
73    reply. */
74 typedef struct SilcClientCommandContextStruct {
75   struct SilcClientCommandContextStruct *next;
76   SilcClientConnection conn;          /* Connection */
77   SilcFSMThreadStruct thread;         /* FSM thread for command call */
78
79   SilcCommand cmd;                    /* Command */
80   SilcUInt16 cmd_ident;               /* Command identifier */
81   SilcUInt32 argc;                    /* Number of arguments */
82   unsigned char **argv;               /* Arguments, may be NULL */
83   SilcUInt32 *argv_lens;              /* Argument lengths, may be NULL */
84   SilcUInt32 *argv_types;             /* Argument types, may be NULL */
85
86   SilcList reply_callbacks;           /* Command reply callbacks */
87   SilcStatus status;                  /* Current command reply status */
88   SilcStatus error;                   /* Current command reply error */
89
90   void *context;                      /* Context for free use */
91   unsigned int called        : 1;     /* Set when called by application */
92   unsigned int verbose       : 1;     /* Verbose with 'say' client operation */
93   unsigned int resolved      : 1;     /* Set when resolving something */
94 } *SilcClientCommandContext;
95
96 /* Internal context for the client->internal pointer in the SilcClient. */
97 struct SilcClientInternalStruct {
98   SilcFSMStruct fsm;                     /* Client's FSM */
99   SilcFSMEventStruct wait_event;         /* Event signaller */
100   SilcClientOperations *ops;             /* Client operations */
101   SilcClientParams *params;              /* Client parameters */
102   SilcPacketEngine packet_engine;        /* Packet engine */
103   SilcMutex lock;                        /* Client lock */
104   SilcList commands;                     /* Registered commands */
105   SilcDList ftp_sessions;                /* FTP sessions */
106   char *silc_client_version;             /* Version set by application */
107   SilcClientRunning running;             /* Running/Stopped callback */
108   void *running_context;                 /* Context for runnign callback */
109   SilcAtomic16 conns;                    /* Number of connections in client */
110   SilcUInt16 next_session_id;            /* Next FTP session ID */
111
112   /* Events */
113   unsigned int stop              : 1;    /* Stop client */
114   unsigned int run_callback      : 1;    /* Call running/stopped callback */
115   unsigned int connection_closed : 1;    /* A connection closed */
116 };
117
118 /* Internal context for conn->internal in SilcClientConnection. */
119 struct SilcClientConnectionInternalStruct {
120   SilcClientConnectionParams params;     /* Connection parameters */
121   SilcFSMStruct fsm;                     /* Connection FSM */
122   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
123   SilcFSMEventStruct wait_event;         /* Event signaller */
124   SilcSchedule schedule;                 /* Connection's scheduler */
125   SilcMutex lock;                        /* Connection lock */
126   SilcSKE ske;                           /* Key exchange protocol */
127   SilcSKERekeyMaterial rekey;            /* Rekey material */
128   SilcList thread_pool;                  /* Packet thread pool */
129   SilcList pending_commands;             /* Pending commands list */
130   SilcHash hash;                         /* Negotiated hash function */
131   SilcHash sha1hash;                     /* SHA-1 default hash context */
132   SilcBuffer local_idp;                  /* Local ID Payload */
133   SilcBuffer remote_idp;                 /* Remote ID Payload */
134   SilcAsyncOperation op;                 /* Protocols async operation */
135   SilcAsyncOperation cop;                /* Async operation for application */
136   SilcHashTable attrs;                   /* Configured user attributes */
137   char *disconnect_message;              /* Disconnection message */
138   char *away_message;                    /* Away message */
139   void *prv_waiter;                      /* Private message packet waiter */
140
141   SilcIDCache client_cache;              /* Client entry cache */
142   SilcIDCache channel_cache;             /* Channel entry cache */
143   SilcIDCache server_cache;              /* Server entry cache */
144
145   SilcAtomic16 cmd_ident;                /* Current command identifier */
146   SilcUInt8 retry_count;                 /* Packet retry counter */
147   SilcUInt8 retry_timer;                 /* Packet retry timer */
148   SilcClientConnectionStatus status;     /* Connection callback status */
149   SilcStatus error;                      /* Connection callback error */
150
151   /* Events */
152   unsigned int connect            : 1;   /* Connect remote host */
153   unsigned int disconnected       : 1;   /* Disconnect remote connection */
154   unsigned int key_exchange       : 1;   /* Start key exchange */
155   unsigned int rekeying           : 1;   /* Start rekey */
156
157   /* Flags */
158   unsigned int verbose            : 1;   /* Notify application */
159   unsigned int registering        : 1;   /* Set when registering to network */
160   unsigned int rekey_responder    : 1;   /* Set when rekeying as responder */
161   unsigned int auth_request       : 1;   /* Set when requesting auth method */
162 };
163
164 SILC_FSM_STATE(silc_client_connection_st_run);
165 SILC_FSM_STATE(silc_client_connection_st_packet);
166 SILC_FSM_STATE(silc_client_connection_st_close);
167 SILC_FSM_STATE(silc_client_error);
168 SILC_FSM_STATE(silc_client_disconnect);
169 SILC_FSM_STATE(silc_client_st_stop);
170
171 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
172 void silc_client_fsm_destructor(SilcFSM fsm, void *fsm_context,
173                                 void *destructor_context);
174 void silc_client_command_free(SilcClientCommandContext cmd);
175
176 #endif /* CLIENT_INTERNAL_H */