updates.
[silc.git] / lib / silcclient / client.h
1 /*
2
3   client.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
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.
18
19 */
20
21 #ifndef CLIENT_H
22 #define CLIENT_H
23
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;
30
31 #include "idlist.h"
32 #include "command.h"
33 #include "silcapi.h"
34
35 /* Generic rekey context for connections */
36 typedef struct {
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;
40   uint32 enc_key_len;
41   int ske_group;
42   bool pfs;
43   uint32 timeout;
44   void *context;
45 } *SilcClientRekey;
46
47 /* Connection structure used in client to associate all the important
48    connection specific data to this structure. */
49 struct SilcClientConnectionStruct {
50   /*
51    * Local data 
52    */
53   char *nickname;
54
55   /* Local client ID for this connection */
56   SilcClientID *local_id;
57
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;
62
63   /* Own client entry. */
64   SilcClientEntry local_entry;
65
66   /*
67    * Remote data 
68    */
69   char *remote_host;
70   int remote_port;
71   int remote_type;
72   char *remote_info;
73
74   /* Remote server ID for this connection */
75   SilcServerID *remote_id;
76
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;
81
82   /*
83    * Common data 
84    */
85   /* Keys and stuff negotiated in the SKE protocol */
86   SilcCipher send_key;
87   SilcCipher receive_key;
88   SilcHmac hmac_send;
89   SilcHmac hmac_receive;
90   SilcHash hash;
91
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;
98
99   /* Current channel on window. All channels are saved (allocated) into
100      the cache entries. */
101   SilcChannelEntry current_channel;
102
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;
107
108   /* Pending command queue for this connection */
109   SilcDList pending_commands;
110
111   /* Current command identifier, 0 not used */
112   uint16 cmd_ident;
113
114   /* Requested pings. */
115   SilcClientPing *ping;
116   uint32 ping_count;
117
118   /* Set away message */
119   SilcClientAway *away;
120
121   /* Re-key context */
122   SilcClientRekey rekey;
123
124   /* Pointer back to the SilcClient. This object is passed to the application
125      and the actual client object is accesible through this pointer. */
126   SilcClient client;
127
128   /* User data context. Library does not touch this. */
129   void *context;
130 };
131
132 /* Main client structure. */
133 struct SilcClientStruct {
134   /*
135    * Public data. All the following pointers must be set by the allocator
136    * of this structure.
137    */
138
139   /* Users's username, hostname and realname. */
140   char *username;
141   char *hostname;
142   char *realname;
143
144   /* Private and public key of the user. */
145   SilcPKCS pkcs;
146   SilcPublicKey public_key;
147   SilcPrivateKey private_key;
148
149   /* Application specific user data pointer. Client library does not
150      touch this. */
151   void *application;
152
153   /*
154    * Private data. Following pointers are used internally by the client
155    * library and should be considered read-only fields.
156    */
157
158   /* All client operations that are implemented in the application. */
159   SilcClientOperations *ops;
160
161   /* Client Parameters */
162   SilcClientParams *params;
163
164   /* SILC client scheduler */
165   SilcSchedule schedule;
166
167   /* Table of connections in client. All the connection data is saved here. */
168   SilcClientConnection *conns;
169   uint32 conns_count;
170
171   /* Table of listenning sockets in client.  Client can have listeners
172      (like key agreement protocol server) and those sockets are saved here.
173      This table is checked always if the connection object cannot be found
174      from the `conns' table. */
175   SilcSocketConnection *sockets;
176   uint32 sockets_count;
177
178   /* Generic cipher and hash objects. These can be used and referenced
179      by the application as well. */
180   SilcCipher none_cipher;
181   SilcHash md5hash;
182   SilcHash sha1hash;
183   SilcHmac md5hmac;
184   SilcHmac sha1hmac;
185
186   /* Random Number Generator. Application should use this as its primary
187      random number generator. */
188   SilcRng rng;
189
190   /* Client version. Used to compare to remote host's version strings. */
191   char *silc_client_version;
192 };
193
194 /* Macros */
195
196 /* Registers generic task for file descriptor for reading from network and
197    writing to network. As being generic task the actual task is allocated 
198    only once and after that the same task applies to all registered fd's. */
199 #define SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd)      \
200 do {                                                    \
201   silc_schedule_task_add(client->schedule, (fd),        \
202                          silc_client_packet_process,    \
203                          context, 0, 0,                 \
204                          SILC_TASK_GENERIC,             \
205                          SILC_TASK_PRI_NORMAL);         \
206 } while(0)
207
208 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(s, fd)             \
209 do {                                                            \
210   silc_schedule_set_listen_fd((s), (fd), SILC_TASK_READ);       \
211 } while(0)
212      
213 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(s, fd)            \
214 do {                                                            \
215   silc_schedule_set_listen_fd((s), (fd), (SILC_TASK_READ |      \
216                                           SILC_TASK_WRITE));    \
217 } while(0)
218
219 /* Finds socket connection object by file descriptor */
220 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock)         \
221 do {                                                    \
222   int __i;                                              \
223                                                         \
224   for (__i = 0; __i < (__x)->conns_count; __i++)        \
225     if ((__x)->conns[__i] &&                            \
226         (__x)->conns[__i]->sock->sock == (__fd))        \
227       break;                                            \
228                                                         \
229   if (__i >= (__x)->conns_count) {                      \
230     (__sock) = NULL;                                    \
231     for (__i = 0; __i < (__x)->sockets_count; __i++)    \
232       if ((__x)->sockets[__i] &&                        \
233           (__x)->sockets[__i]->sock == (__fd))          \
234         (__sock) = (__x)->sockets[__i];                 \
235   } else                                                \
236     (__sock) = (__x)->conns[__i]->sock;                 \
237 } while(0)
238
239 /* Check whether rekey protocol is active */
240 #define SILC_CLIENT_IS_REKEY(sock)                                      \
241   (sock->protocol && sock->protocol->protocol &&                        \
242    sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)
243
244 /* Prototypes (some of the prototypes are defined in the silcapi.h) */
245
246 void silc_client_packet_send(SilcClient client, 
247                              SilcSocketConnection sock,
248                              SilcPacketType type, 
249                              void *dst_id,
250                              SilcIdType dst_id_type,
251                              SilcCipher cipher,
252                              SilcHmac hmac,
253                              unsigned char *data, 
254                              uint32 data_len, 
255                              int force_send);
256 void silc_client_disconnected_by_server(SilcClient client,
257                                         SilcSocketConnection sock,
258                                         SilcBuffer message);
259 void silc_client_error_by_server(SilcClient client,
260                                  SilcSocketConnection sock,
261                                  SilcBuffer message);
262 void silc_client_receive_new_id(SilcClient client,
263                                 SilcSocketConnection sock,
264                                 SilcIDPayload idp);
265 SilcChannelEntry silc_client_new_channel_id(SilcClient client,
266                                             SilcSocketConnection sock,
267                                             char *channel_name,
268                                             uint32 mode, 
269                                             SilcIDPayload idp);
270 void silc_client_save_channel_key(SilcClientConnection conn,
271                                   SilcBuffer key_payload, 
272                                   SilcChannelEntry channel);
273 void silc_client_receive_channel_key(SilcClient client,
274                                      SilcSocketConnection sock,
275                                      SilcBuffer packet);
276 void silc_client_channel_message(SilcClient client, 
277                                  SilcSocketConnection sock, 
278                                  SilcPacketContext *packet);
279 void silc_client_remove_from_channels(SilcClient client,
280                                       SilcClientConnection conn,
281                                       SilcClientEntry client_entry);
282 void silc_client_replace_from_channels(SilcClient client, 
283                                        SilcClientConnection conn,
284                                        SilcClientEntry old,
285                                        SilcClientEntry new);
286 void silc_client_process_failure(SilcClient client,
287                                  SilcSocketConnection sock,
288                                  SilcPacketContext *packet);
289 void silc_client_key_agreement(SilcClient client,
290                                SilcSocketConnection sock,
291                                SilcPacketContext *packet);
292 void silc_client_notify_by_server(SilcClient client,
293                                   SilcSocketConnection sock,
294                                   SilcPacketContext *packet);
295 void silc_client_private_message(SilcClient client, 
296                                  SilcSocketConnection sock, 
297                                  SilcPacketContext *packet);
298
299 #endif