5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2001 Pekka Riikonen
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; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
24 /* Forward declarations */
25 typedef struct SilcClientStruct *SilcClient;
26 typedef struct SilcClientConnectionStruct *SilcClientConnection;
27 typedef struct SilcClientPingStruct SilcClientPing;
28 typedef struct SilcClientAwayStruct SilcClientAway;
29 typedef struct SilcClientKeyAgreementStruct *SilcClientKeyAgreement;
35 /* Generic rekey context for connections */
37 /* Current sending encryption key, provided for re-key. The `pfs'
38 is TRUE if the Perfect Forward Secrecy is performed in re-key. */
39 unsigned char *send_enc_key;
47 /* Connection structure used in client to associate all the important
48 connection specific data to this structure. */
49 struct SilcClientConnectionStruct {
55 /* Local client ID for this connection */
56 SilcClientID *local_id;
58 /* Decoded local ID so that the above defined ID would not have
59 to be decoded for every packet. */
60 unsigned char *local_id_data;
61 uint32 local_id_data_len;
63 /* Own client entry. */
64 SilcClientEntry local_entry;
74 /* Remote server ID for this connection */
75 SilcServerID *remote_id;
77 /* Decoded remote ID so that the above defined ID would not have
78 to be decoded for every packet. */
79 unsigned char *remote_id_data;
80 uint32 remote_id_data_len;
85 /* Keys and stuff negotiated in the SKE protocol */
87 SilcCipher receive_key;
89 SilcHmac hmac_receive;
92 /* Client ID and Channel ID cache. Messages transmitted in SILC network
93 are done using different unique ID's. These are the cache for
94 thoses ID's used in the communication. */
95 SilcIDCache client_cache;
96 SilcIDCache channel_cache;
97 SilcIDCache server_cache;
99 /* Current channel on window. All channels are saved (allocated) into
100 the cache entries. */
101 SilcChannelEntry current_channel;
103 /* Socket connection object for this connection (window). This
104 object will have a back-pointer to this window object for fast
105 referencing (sock->user_data). */
106 SilcSocketConnection sock;
108 /* Pending command queue for this connection */
109 SilcDList pending_commands;
111 /* Current command identifier, 0 not used */
114 /* Requested pings. */
115 SilcClientPing *ping;
118 /* Set away message */
119 SilcClientAway *away;
122 SilcClientRekey rekey;
124 /* Pointer back to the SilcClient. This object is passed to the application
125 and the actual client object is accesible through this pointer. */
128 /* User data context. Library does not touch this. */
132 /* Main client structure. */
133 struct SilcClientStruct {
135 * Public data. All the following pointers must be set by the allocator
139 /* Users's username, hostname and realname. */
144 /* Private and public key of the user. */
146 SilcPublicKey public_key;
147 SilcPrivateKey private_key;
149 /* Application specific user data pointer. Client library does not
154 * Private data. Following pointers are used internally by the client
155 * library and should be considered read-only fields.
158 /* All client operations that are implemented in the application. */
159 SilcClientOperations *ops;
161 /* Client Parameters */
162 SilcClientParams *params;
164 /* SILC client scheduler and task queues */
165 SilcSchedule schedule;
166 SilcTaskQueue io_queue;
167 SilcTaskQueue timeout_queue;
168 SilcTaskQueue generic_queue;
170 /* Table of connections in client. All the connection data is saved here. */
171 SilcClientConnection *conns;
174 /* Table of listenning sockets in client. Client can have listeners
175 (like key agreement protocol server) and those sockets are saved here.
176 This table is checked always if the connection object cannot be found
177 from the `conns' table. */
178 SilcSocketConnection *sockets;
179 uint32 sockets_count;
181 /* Generic cipher and hash objects. These can be used and referenced
182 by the application as well. */
183 SilcCipher none_cipher;
189 /* Random Number Generator. Application should use this as its primary
190 random number generator. */
193 /* Client version. Used to compare to remote host's version strings. */
194 char *silc_client_version;
199 /* Registers generic task for file descriptor for reading from network and
200 writing to network. As being generic task the actual task is allocated
201 only once and after that the same task applies to all registered fd's. */
202 #define SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd) \
204 SilcTask tmptask = silc_task_register(client->generic_queue, (fd), \
205 silc_client_packet_process, \
208 SILC_TASK_PRI_NORMAL); \
209 silc_task_set_iotype(tmptask, SILC_TASK_WRITE); \
212 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(s, fd) \
214 silc_schedule_set_listen_fd((s), (fd), (1L << SILC_TASK_READ)); \
217 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(s, fd) \
219 silc_schedule_set_listen_fd((s), (fd), ((1L << SILC_TASK_READ) | \
220 (1L << SILC_TASK_WRITE))); \
223 /* Finds socket connection object by file descriptor */
224 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock) \
228 for (__i = 0; __i < (__x)->conns_count; __i++) \
229 if ((__x)->conns[__i] && \
230 (__x)->conns[__i]->sock->sock == (__fd)) \
233 if (__i >= (__x)->conns_count) { \
235 for (__i = 0; __i < (__x)->sockets_count; __i++) \
236 if ((__x)->sockets[__i] && \
237 (__x)->sockets[__i]->sock == (__fd)) \
238 (__sock) = (__x)->sockets[__i]; \
240 (__sock) = (__x)->conns[__i]->sock; \
243 /* Check whether rekey protocol is active */
244 #define SILC_CLIENT_IS_REKEY(sock) \
245 (sock->protocol && sock->protocol->protocol && \
246 sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)
248 /* Prototypes (some of the prototypes are defined in the silcapi.h) */
250 void silc_client_packet_send(SilcClient client,
251 SilcSocketConnection sock,
254 SilcIdType dst_id_type,
260 void silc_client_disconnected_by_server(SilcClient client,
261 SilcSocketConnection sock,
263 void silc_client_error_by_server(SilcClient client,
264 SilcSocketConnection sock,
266 void silc_client_receive_new_id(SilcClient client,
267 SilcSocketConnection sock,
269 SilcChannelEntry silc_client_new_channel_id(SilcClient client,
270 SilcSocketConnection sock,
274 void silc_client_save_channel_key(SilcClientConnection conn,
275 SilcBuffer key_payload,
276 SilcChannelEntry channel);
277 void silc_client_receive_channel_key(SilcClient client,
278 SilcSocketConnection sock,
280 void silc_client_channel_message(SilcClient client,
281 SilcSocketConnection sock,
282 SilcPacketContext *packet);
283 void silc_client_remove_from_channels(SilcClient client,
284 SilcClientConnection conn,
285 SilcClientEntry client_entry);
286 void silc_client_replace_from_channels(SilcClient client,
287 SilcClientConnection conn,
289 SilcClientEntry new);
290 void silc_client_process_failure(SilcClient client,
291 SilcSocketConnection sock,
292 SilcPacketContext *packet);
293 void silc_client_key_agreement(SilcClient client,
294 SilcSocketConnection sock,
295 SilcPacketContext *packet);
296 void silc_client_notify_by_server(SilcClient client,
297 SilcSocketConnection sock,
298 SilcPacketContext *packet);
299 void silc_client_private_message(SilcClient client,
300 SilcSocketConnection sock,
301 SilcPacketContext *packet);