1 /*
2 
3   client_internal.h
4 
5   Author: Pekka Riikonen <priikone@silcnet.org>
6 
7   Copyright (C) 1997 - 2014 Pekka Riikonen
8 
9   The contents of this file are subject to one of the Licenses specified
10   in the COPYING file;  You may not use this file except in compliance
11   with the License.
12 
13   The software distributed under the License is distributed on an "AS IS"
14   basis, in the hope that it will be useful, but WITHOUT WARRANTY OF ANY
15   KIND, either expressed or implied.  See the COPYING file for more
16   information.
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   SilcPublicKey public_key;
52   void *completion_context;
53   void *context;
54   SilcBool aborted;
55 } *SilcVerifyKeyContext;
56 
57 /* Command and command reply context used to hold registered commands
58    in the SILC client. */
59 typedef struct SilcClientCommandStruct {
60   struct SilcClientCommandStruct *next;
61   SilcCommand cmd;		      /* Command type */
62   SilcFSMStateCallback command;	      /* Command function */
63   SilcFSMStateCallback reply;	      /* Command reply callback */
64   char *name;			      /* Name of the command (optional) */
65   SilcUInt8 max_args;		      /* Maximum arguments (optional)  */
66 } *SilcClientCommand;
67 
68 /* Command reply callback structure */
69 typedef struct SilcClientCommandReplyCallbackStruct  {
70   struct SilcClientCommandReplyCallbackStruct *next;
71   SilcClientCommandReply reply;	      /* Command reply callback */
72   void *context;		      /* Command reply context */
73   unsigned int do_not_call     : 1;   /* Set to not call the callback */
74 } *SilcClientCommandReplyCallback;
75 
76 /* Command context given as argument to command state functions.  This same
77    context is used when calling, sending and procesing command and command
78    reply. */
79 typedef struct SilcClientCommandContextStruct {
80   struct SilcClientCommandContextStruct *next;
81   SilcClientConnection conn;          /* Connection */
82   SilcFSMThreadStruct thread;	      /* FSM thread for command call */
83 
84   SilcCommand cmd;		      /* Command */
85   SilcUInt16 cmd_ident;		      /* Command identifier */
86   SilcUInt32 argc;		      /* Number of arguments */
87   unsigned char **argv;		      /* Arguments, may be NULL */
88   SilcUInt32 *argv_lens;	      /* Argument lengths, may be NULL */
89   SilcUInt32 *argv_types;	      /* Argument types, may be NULL */
90 
91   SilcList reply_callbacks;	      /* Command reply callbacks */
92   SilcStatus status;		      /* Current command reply status */
93   SilcStatus error;		      /* Current command reply error */
94 
95   void *context;		      /* Context for free use */
96   unsigned int called        : 1;     /* Set when called by application */
97   unsigned int verbose       : 1;     /* Verbose with 'say' client operation */
98   unsigned int resolved      : 1;     /* Set when resolving something */
99 } *SilcClientCommandContext;
100 
101 /* Internal context for the client->internal pointer in the SilcClient. */
102 struct SilcClientInternalStruct {
103   SilcFSMStruct fsm;			 /* Client's FSM */
104   SilcFSMEventStruct wait_event;	 /* Event signaller */
105   SilcClientOperations *ops;		 /* Client operations */
106   SilcClientParams *params;		 /* Client parameters */
107   SilcPacketEngine packet_engine;        /* Packet engine */
108   SilcMutex lock;			 /* Client lock */
109   SilcList commands;			 /* Registered commands */
110   SilcDList ftp_sessions;	         /* FTP sessions */
111   char *silc_client_version;		 /* Version set by application */
112   SilcClientRunning running;	         /* Running/Stopped callback */
113   void *running_context;		 /* Context for runnign callback */
114   SilcAtomic32 conns;			 /* Number of connections in client */
115   SilcUInt16 next_session_id;		 /* Next FTP session ID */
116 
117   /* Events */
118   unsigned int stop              : 1;	 /* Stop client */
119   unsigned int run_callback      : 1;	 /* Call running/stopped callback */
120   unsigned int connection_closed : 1;	 /* A connection closed */
121 };
122 
123 /* Internal context for conn->internal in SilcClientConnection. */
124 struct SilcClientConnectionInternalStruct {
125   SilcClientConnectionParams params;	 /* Connection parameters */
126   SilcFSMStruct fsm;			 /* Connection FSM */
127   SilcFSMThreadStruct event_thread;      /* FSM thread for events */
128   SilcFSMEventStruct wait_event;	 /* Event signaller */
129   SilcSchedule schedule;		 /* Connection's scheduler */
130   SilcMutex lock;		         /* Connection lock */
131   SilcSKE ske;				 /* Key exchange protocol */
132   SilcSKERekeyMaterial rekey;		 /* Rekey material */
133   SilcList thread_pool;			 /* Packet thread pool */
134   SilcList pending_commands;		 /* Pending commands list */
135   SilcHash hash;			 /* Negotiated hash function */
136   SilcHash sha1hash;			 /* SHA-1 default hash context */
137   SilcBuffer local_idp;		         /* Local ID Payload */
138   SilcBuffer remote_idp;		 /* Remote ID Payload */
139   SilcAsyncOperation op;	         /* Protocols async operation */
140   SilcAsyncOperation cop;	         /* Async operation for application */
141   SilcHashTable attrs;		         /* Configured user attributes */
142   SilcStream user_stream;		 /* Low level stream in connecting */
143   char *disconnect_message;		 /* Disconnection message */
144   char *away_message;		         /* Away message */
145 
146   SilcIDCache client_cache;		 /* Client entry cache */
147   SilcIDCache channel_cache;		 /* Channel entry cache */
148   SilcIDCache server_cache;		 /* Server entry cache */
149 
150   SilcUInt32 remote_version;	         /* Remote SILC protocol version */
151   SilcAtomic16 cmd_ident;		 /* Current command identifier */
152   SilcUInt8 retry_count;		 /* Packet retry counter */
153   SilcUInt8 retry_timer;		 /* Packet retry timer */
154   SilcClientConnectionStatus status;	 /* Connection callback status */
155   SilcStatus error;			 /* Connection callback error */
156   SilcUInt32 ake_generation;		 /* next AKE rekey generation */
157 
158   /* Events */
159   unsigned int connect            : 1;	 /* Connect remote host */
160   unsigned int disconnected       : 1;	 /* Disconnect remote connection */
161   unsigned int key_exchange       : 1;   /* Start key exchange */
162   unsigned int rekeying           : 1;   /* Start rekey */
163 
164   /* Flags */
165   unsigned int verbose            : 1;   /* Notify application */
166   unsigned int registering        : 1;	 /* Set when registering to network */
167   unsigned int rekey_responder    : 1;   /* Set when rekeying as responder */
168   unsigned int auth_request       : 1;   /* Set when requesting auth method */
169 };
170 
171 SILC_FSM_STATE(silc_client_connection_st_run);
172 SILC_FSM_STATE(silc_client_connection_st_packet);
173 SILC_FSM_STATE(silc_client_connection_st_close);
174 SILC_FSM_STATE(silc_client_error);
175 SILC_FSM_STATE(silc_client_disconnect);
176 SILC_FSM_STATE(silc_client_st_stop);
177 
178 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
179 void silc_client_fsm_destructor(SilcFSM fsm, void *fsm_context,
180 				void *destructor_context);
181 void silc_client_command_free(SilcClientCommandContext cmd);
182 SilcClientConnection
183 silc_client_add_connection(SilcClient client,
184 			   SilcConnectionType conn_type,
185 			   SilcBool connect,
186 			   SilcClientConnectionParams *params,
187 			   SilcPublicKey public_key,
188 			   SilcPrivateKey private_key,
189 			   char *remote_host, int port,
190 			   SilcClientConnectCallback callback,
191 			   void *context);
192 SilcBuffer silc_client_attributes_process(SilcClient client,
193                                           SilcClientConnection conn,
194                                           SilcDList attrs);
195 
196 #endif /* CLIENT_INTERNAL_H */
197