5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 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 declaration for client */
25 typedef struct SilcClientObject *SilcClient;
27 /* Forward declaration for client connection */
28 typedef struct SilcClientConnectionObject *SilcClientConnection;
34 /* Structure to hold ping time information. Every PING command will
35 add entry of this structure and is removed after reply to the ping
37 typedef struct SilcClientPingStruct {
43 /* Structure to hold away messages set by user. This is mainly created
44 for future extensions where away messages could be set according filters
45 such as nickname and hostname. For now only one away message can
46 be set in one connection. */
47 typedef struct SilcClientAwayStruct {
49 struct SilcClientAwayStruct *next;
52 /* Connection structure used in client to associate all the important
53 connection specific data to this structure. */
54 struct SilcClientConnectionObject {
60 /* Local client ID for this connection */
61 SilcClientID *local_id;
63 /* Decoded local ID so that the above defined ID would not have
64 to be decoded for every packet. */
65 unsigned char *local_id_data;
66 unsigned int local_id_data_len;
68 /* Own client entry. */
69 SilcClientEntry local_entry;
79 /* Remote server ID for this connection */
80 SilcServerID *remote_id;
82 /* Decoded remote ID so that the above defined ID would not have
83 to be decoded for every packet. */
84 unsigned char *remote_id_data;
85 unsigned int remote_id_data_len;
92 SilcCipher receive_key;
94 unsigned char *hmac_key;
95 unsigned int hmac_key_len;
97 /* Client ID and Channel ID cache. Messages transmitted in SILC network
98 are done using different unique ID's. These are the cache for
99 thoses ID's used in the communication. */
100 SilcIDCache client_cache;
101 SilcIDCache channel_cache;
102 SilcIDCache server_cache;
104 /* Current channel on window. All channels are saved (allocated) into
105 the cache entries. */
106 SilcChannelEntry current_channel;
108 /* Socket connection object for this connection (window). This
109 object will have a back-pointer to this window object for fast
110 referencing (sock->user_data). */
111 SilcSocketConnection sock;
113 /* Requested pings. */
114 SilcClientPing *ping;
115 unsigned int ping_count;
117 /* Set away message */
118 SilcClientAway *away;
120 /* Pointer back to the SilcClient. This object is passed to the application
121 and the actual client object is accesible through this pointer. */
124 /* User data context. Library does not touch this. */
128 /* Main client structure. */
129 struct SilcClientObject {
131 * Public data. All the following pointers must be set by the allocator
135 /* Users's username, hostname and realname. */
140 /* Private and public key of the user. */
142 SilcPublicKey public_key;
143 SilcPrivateKey private_key;
145 /* Application specific user data pointer. Client library does not
150 * Private data. Following pointers are used internally by the client
151 * library and should be considered read-only fields.
154 /* All client operations that are implemented in the application. */
155 SilcClientOperations *ops;
157 /* SILC client task queues */
158 SilcTaskQueue io_queue;
159 SilcTaskQueue timeout_queue;
160 SilcTaskQueue generic_queue;
162 /* Table of connections in client. All the connection data is saved here. */
163 SilcClientConnection *conns;
164 unsigned int conns_count;
166 /* Generic cipher and hash objects. These can be used and referenced
167 by the application as well. */
168 SilcCipher none_cipher;
174 /* Random Number Generator. Application should use this as its primary
175 random number generator. */
181 /* Registers generic task for file descriptor for reading from network and
182 writing to network. As being generic task the actual task is allocated
183 only once and after that the same task applies to all registered fd's. */
184 #define SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd) \
186 SilcTask tmptask = silc_task_register(client->generic_queue, (fd), \
187 silc_client_packet_process, \
190 SILC_TASK_PRI_NORMAL); \
191 silc_task_set_iotype(tmptask, SILC_TASK_WRITE); \
194 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(fd) \
196 silc_schedule_set_listen_fd((fd), (1L << SILC_TASK_READ)); \
199 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(fd) \
201 silc_schedule_set_listen_fd((fd), ((1L << SILC_TASK_READ) | \
202 (1L << SILC_TASK_WRITE))); \
205 /* Finds socket connection object by file descriptor */
206 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock) \
210 for (__i = 0; __i < (__x)->conns_count; __i++) \
211 if ((__x)->conns[__i]->sock->sock == (__fd)) \
214 if (__i >= (__x)->conns_count) \
216 (__sock) = (__x)->conns[__i]->sock; \
221 SilcClient silc_client_alloc(SilcClientOperations *ops, void *application);
222 void silc_client_free(SilcClient client);
223 int silc_client_init(SilcClient client);
224 void silc_client_stop(SilcClient client);
225 void silc_client_run(SilcClient client);
226 SilcClientConnection silc_client_add_connection(SilcClient client,
230 int silc_client_connect_to_server(SilcClient client, int port,
231 char *host, void *context);
232 int silc_client_start_key_exchange(SilcClient client,
233 SilcClientConnection conn,
235 void silc_client_packet_send(SilcClient client,
236 SilcSocketConnection sock,
239 SilcIdType dst_id_type,
243 unsigned int data_len,
245 void silc_client_packet_send_to_channel(SilcClient client,
246 SilcSocketConnection sock,
247 SilcChannelEntry channel,
249 unsigned int data_len,
251 void silc_client_packet_send_private_message(SilcClient client,
252 SilcSocketConnection sock,
253 SilcClientEntry client_entry,
255 unsigned int data_len,
257 void silc_client_close_connection(SilcClient client,
258 SilcSocketConnection sock);
259 void silc_client_disconnected_by_server(SilcClient client,
260 SilcSocketConnection sock,
262 void silc_client_error_by_server(SilcClient client,
263 SilcSocketConnection sock,
265 void silc_client_notify_by_server(SilcClient client,
266 SilcSocketConnection sock,
267 SilcPacketContext *packet);
268 void silc_client_receive_new_id(SilcClient client,
269 SilcSocketConnection sock,
271 void silc_client_new_channel_id(SilcClient client,
272 SilcSocketConnection sock,
274 unsigned int mode, SilcIDPayload idp);
275 void silc_client_save_channel_key(SilcClientConnection conn,
276 SilcBuffer key_payload,
277 SilcChannelEntry channel);
278 void silc_client_receive_channel_key(SilcClient client,
279 SilcSocketConnection sock,
281 void silc_client_channel_message(SilcClient client,
282 SilcSocketConnection sock,
283 SilcPacketContext *packet);
284 void silc_client_private_message(SilcClient client,
285 SilcSocketConnection sock,
286 SilcPacketContext *packet);
287 void silc_client_remove_from_channels(SilcClient client,
288 SilcClientConnection conn,
289 SilcClientEntry client_entry);
290 void silc_client_replace_from_channels(SilcClient client,
291 SilcClientConnection conn,
293 SilcClientEntry new);
294 char *silc_client_chmode(unsigned int mode);
295 char *silc_client_chumode(unsigned int mode);
296 char *silc_client_chumode_char(unsigned int mode);