updates. New data types.
[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 /* Connection structure used in client to associate all the important
36    connection specific data to this structure. */
37 struct SilcClientConnectionStruct {
38   /*
39    * Local data 
40    */
41   char *nickname;
42
43   /* Local client ID for this connection */
44   SilcClientID *local_id;
45
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   uint32 local_id_data_len;
50
51   /* Own client entry. */
52   SilcClientEntry local_entry;
53
54   /*
55    * Remote data 
56    */
57   char *remote_host;
58   int remote_port;
59   int remote_type;
60   char *remote_info;
61
62   /* Remote server ID for this connection */
63   SilcServerID *remote_id;
64
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   uint32 remote_id_data_len;
69
70   /*
71    * Common data 
72    */
73   /* Keys and stuff negotiated in the SKE protocol */
74   SilcCipher send_key;
75   SilcCipher receive_key;
76   SilcHmac hmac;
77   unsigned char *hmac_key;
78   uint32 hmac_key_len;
79   SilcHash hash;
80
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;
87
88   /* Current channel on window. All channels are saved (allocated) into
89      the cache entries. */
90   SilcChannelEntry current_channel;
91
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;
96
97   /* Pending command queue for this connection */
98   SilcDList pending_commands;
99
100   /* Current command identifier, 0 not used */
101   uint16 cmd_ident;
102
103   /* Requested pings. */
104   SilcClientPing *ping;
105   uint32 ping_count;
106
107   /* Set away message */
108   SilcClientAway *away;
109
110   /* Pointer back to the SilcClient. This object is passed to the application
111      and the actual client object is accesible through this pointer. */
112   SilcClient client;
113
114   /* User data context. Library does not touch this. */
115   void *context;
116 };
117
118 /* Main client structure. */
119 struct SilcClientStruct {
120   /*
121    * Public data. All the following pointers must be set by the allocator
122    * of this structure.
123    */
124
125   /* Users's username, hostname and realname. */
126   char *username;
127   char *hostname;
128   char *realname;
129
130   /* Private and public key of the user. */
131   SilcPKCS pkcs;
132   SilcPublicKey public_key;
133   SilcPrivateKey private_key;
134
135   /* Application specific user data pointer. Client library does not
136      touch this. */
137   void *application;
138
139   /*
140    * Private data. Following pointers are used internally by the client
141    * library and should be considered read-only fields.
142    */
143
144   /* All client operations that are implemented in the application. */
145   SilcClientOperations *ops;
146
147   /* SILC client task queues */
148   SilcTaskQueue io_queue;
149   SilcTaskQueue timeout_queue;
150   SilcTaskQueue generic_queue;
151
152   /* Table of connections in client. All the connection data is saved here. */
153   SilcClientConnection *conns;
154   uint32 conns_count;
155
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   uint32 sockets_count;
162
163   /* Generic cipher and hash objects. These can be used and referenced
164      by the application as well. */
165   SilcCipher none_cipher;
166   SilcHash md5hash;
167   SilcHash sha1hash;
168   SilcHmac md5hmac;
169   SilcHmac sha1hmac;
170
171   /* Random Number Generator. Application should use this as its primary
172      random number generator. */
173   SilcRng rng;
174 };
175
176 /* Macros */
177
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)                      \
182 do {                                                                    \
183   SilcTask tmptask = silc_task_register(client->generic_queue, (fd),    \
184                                         silc_client_packet_process,     \
185                                         context, 0, 0,                  \
186                                         SILC_TASK_GENERIC,              \
187                                         SILC_TASK_PRI_NORMAL);          \
188   silc_task_set_iotype(tmptask, SILC_TASK_WRITE);                       \
189 } while(0)
190
191 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(fd)                \
192 do {                                                            \
193   silc_schedule_set_listen_fd((fd), (1L << SILC_TASK_READ));    \
194 } while(0)                                                      \
195      
196 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(fd)               \
197 do {                                                            \
198   silc_schedule_set_listen_fd((fd), ((1L << SILC_TASK_READ) |   \
199                                      (1L << SILC_TASK_WRITE))); \
200 } while(0)
201
202 /* Finds socket connection object by file descriptor */
203 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock)         \
204 do {                                                    \
205   int __i;                                              \
206                                                         \
207   for (__i = 0; __i < (__x)->conns_count; __i++)        \
208     if ((__x)->conns[__i] &&                            \
209         (__x)->conns[__i]->sock->sock == (__fd))        \
210       break;                                            \
211                                                         \
212   if (__i >= (__x)->conns_count) {                      \
213     (__sock) = NULL;                                    \
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];                 \
218   } else                                                \
219     (__sock) = (__x)->conns[__i]->sock;                 \
220 } while(0)
221
222 /* Prototypes (some of the prototypes are defined in the silcapi.h) */
223
224 void silc_client_packet_send(SilcClient client, 
225                              SilcSocketConnection sock,
226                              SilcPacketType type, 
227                              void *dst_id,
228                              SilcIdType dst_id_type,
229                              SilcCipher cipher,
230                              SilcHmac hmac,
231                              unsigned char *data, 
232                              uint32 data_len, 
233                              int force_send);
234 void silc_client_disconnected_by_server(SilcClient client,
235                                         SilcSocketConnection sock,
236                                         SilcBuffer message);
237 void silc_client_error_by_server(SilcClient client,
238                                  SilcSocketConnection sock,
239                                  SilcBuffer message);
240 void silc_client_receive_new_id(SilcClient client,
241                                 SilcSocketConnection sock,
242                                 SilcIDPayload idp);
243 SilcChannelEntry silc_client_new_channel_id(SilcClient client,
244                                             SilcSocketConnection sock,
245                                             char *channel_name,
246                                             uint32 mode, 
247                                             SilcIDPayload idp);
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,
253                                      SilcBuffer packet);
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,
262                                        SilcClientEntry old,
263                                        SilcClientEntry new);
264 char *silc_client_chmode(uint32 mode, SilcChannelEntry channel);
265 char *silc_client_chumode(uint32 mode);
266 char *silc_client_chumode_char(uint32 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);
279 #endif