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 /* Connection structure used in client to associate all the important
36 connection specific data to this structure. */
37 struct SilcClientConnectionStruct {
43 /* Local client ID for this connection */
44 SilcClientID *local_id;
46 /* Decoded local ID so that the above defined ID would not have
47 to be decoded for every packet. */
48 unsigned char *local_id_data;
49 unsigned int local_id_data_len;
51 /* Own client entry. */
52 SilcClientEntry local_entry;
62 /* Remote server ID for this connection */
63 SilcServerID *remote_id;
65 /* Decoded remote ID so that the above defined ID would not have
66 to be decoded for every packet. */
67 unsigned char *remote_id_data;
68 unsigned int remote_id_data_len;
73 /* Keys and stuff negotiated in the SKE protocol */
75 SilcCipher receive_key;
77 unsigned char *hmac_key;
78 unsigned int hmac_key_len;
81 /* Client ID and Channel ID cache. Messages transmitted in SILC network
82 are done using different unique ID's. These are the cache for
83 thoses ID's used in the communication. */
84 SilcIDCache client_cache;
85 SilcIDCache channel_cache;
86 SilcIDCache server_cache;
88 /* Current channel on window. All channels are saved (allocated) into
90 SilcChannelEntry current_channel;
92 /* Socket connection object for this connection (window). This
93 object will have a back-pointer to this window object for fast
94 referencing (sock->user_data). */
95 SilcSocketConnection sock;
97 /* Pending command queue for this connection */
98 SilcDList pending_commands;
100 /* Current command identifier, 0 not used */
101 unsigned short cmd_ident;
103 /* Requested pings. */
104 SilcClientPing *ping;
105 unsigned int ping_count;
107 /* Set away message */
108 SilcClientAway *away;
110 /* Pointer back to the SilcClient. This object is passed to the application
111 and the actual client object is accesible through this pointer. */
114 /* User data context. Library does not touch this. */
118 /* Main client structure. */
119 struct SilcClientStruct {
121 * Public data. All the following pointers must be set by the allocator
125 /* Users's username, hostname and realname. */
130 /* Private and public key of the user. */
132 SilcPublicKey public_key;
133 SilcPrivateKey private_key;
135 /* Application specific user data pointer. Client library does not
140 * Private data. Following pointers are used internally by the client
141 * library and should be considered read-only fields.
144 /* All client operations that are implemented in the application. */
145 SilcClientOperations *ops;
147 /* SILC client task queues */
148 SilcTaskQueue io_queue;
149 SilcTaskQueue timeout_queue;
150 SilcTaskQueue generic_queue;
152 /* Table of connections in client. All the connection data is saved here. */
153 SilcClientConnection *conns;
154 unsigned int conns_count;
156 /* Table of listenning sockets in client. Client can have listeners
157 (like key agreement protocol server) and those sockets are saved here.
158 This table is checked always if the connection object cannot be found
159 from the `conns' table. */
160 SilcSocketConnection *sockets;
161 unsigned int sockets_count;
163 /* Generic cipher and hash objects. These can be used and referenced
164 by the application as well. */
165 SilcCipher none_cipher;
171 /* Random Number Generator. Application should use this as its primary
172 random number generator. */
178 /* Registers generic task for file descriptor for reading from network and
179 writing to network. As being generic task the actual task is allocated
180 only once and after that the same task applies to all registered fd's. */
181 #define SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd) \
183 SilcTask tmptask = silc_task_register(client->generic_queue, (fd), \
184 silc_client_packet_process, \
187 SILC_TASK_PRI_NORMAL); \
188 silc_task_set_iotype(tmptask, SILC_TASK_WRITE); \
191 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(fd) \
193 silc_schedule_set_listen_fd((fd), (1L << SILC_TASK_READ)); \
196 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(fd) \
198 silc_schedule_set_listen_fd((fd), ((1L << SILC_TASK_READ) | \
199 (1L << SILC_TASK_WRITE))); \
202 /* Finds socket connection object by file descriptor */
203 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock) \
207 for (__i = 0; __i < (__x)->conns_count; __i++) \
208 if ((__x)->conns[__i] && \
209 (__x)->conns[__i]->sock->sock == (__fd)) \
212 if (__i >= (__x)->conns_count) { \
214 for (__i = 0; __i < (__x)->sockets_count; __i++) \
215 if ((__x)->sockets[__i] && \
216 (__x)->sockets[__i]->sock == (__fd)) \
217 (__sock) = (__x)->sockets[__i]; \
219 (__sock) = (__x)->conns[__i]->sock; \
222 /* Prototypes (some of the prototypes are defined in the silcapi.h) */
224 void silc_client_packet_send(SilcClient client,
225 SilcSocketConnection sock,
228 SilcIdType dst_id_type,
232 unsigned int data_len,
234 void silc_client_disconnected_by_server(SilcClient client,
235 SilcSocketConnection sock,
237 void silc_client_error_by_server(SilcClient client,
238 SilcSocketConnection sock,
240 void silc_client_receive_new_id(SilcClient client,
241 SilcSocketConnection sock,
243 SilcChannelEntry silc_client_new_channel_id(SilcClient client,
244 SilcSocketConnection sock,
248 void silc_client_save_channel_key(SilcClientConnection conn,
249 SilcBuffer key_payload,
250 SilcChannelEntry channel);
251 void silc_client_receive_channel_key(SilcClient client,
252 SilcSocketConnection sock,
254 void silc_client_channel_message(SilcClient client,
255 SilcSocketConnection sock,
256 SilcPacketContext *packet);
257 void silc_client_remove_from_channels(SilcClient client,
258 SilcClientConnection conn,
259 SilcClientEntry client_entry);
260 void silc_client_replace_from_channels(SilcClient client,
261 SilcClientConnection conn,
263 SilcClientEntry new);
264 char *silc_client_chmode(unsigned int mode, SilcChannelEntry channel);
265 char *silc_client_chumode(unsigned int mode);
266 char *silc_client_chumode_char(unsigned int mode);
267 void silc_client_process_failure(SilcClient client,
268 SilcSocketConnection sock,
269 SilcPacketContext *packet);
270 void silc_client_key_agreement(SilcClient client,
271 SilcSocketConnection sock,
272 SilcPacketContext *packet);
273 void silc_client_notify_by_server(SilcClient client,
274 SilcSocketConnection sock,
275 SilcPacketContext *packet);
276 void silc_client_private_message(SilcClient client,
277 SilcSocketConnection sock,
278 SilcPacketContext *packet);