Major rewrite of ID Cache system. Support added for the new
[silc.git] / apps / silc / client.h
1 /*
2
3   client.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 1997 - 2000 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 declaration for client */
25 typedef struct SilcClientObject *SilcClient;
26
27 /* Forward declaration for client window */
28 typedef struct SilcClientWindowObject *SilcClientWindow;
29
30 #include "idlist.h"
31
32 /* Structure to hold ping time information. Every PING command will 
33    add entry of this structure and is removed after reply to the ping
34    as been received. */
35 typedef struct SilcClientPingStruct {
36   time_t start_time;
37   void *dest_id;
38   char *dest_name;
39 } SilcClientPing;
40
41 /* Window structure used in client to associate all the important
42    connection (window) specific data to this structure. How the window
43    actually appears on the screen in handeled by the silc_screen*
44    routines in screen.c. */
45 struct SilcClientWindowObject {
46   /*
47    * Local data 
48    */
49   char *nickname;
50
51   /* Local client ID for this connection */
52   SilcClientID *local_id;
53
54   /* Decoded local ID so that the above defined ID would not have
55      to be decoded for every packet. */
56   unsigned char *local_id_data;
57   unsigned int local_id_data_len;
58
59   /* Own client entry. */
60   SilcClientEntry local_entry;
61
62   /*
63    * Remote data 
64    */
65   char *remote_host;
66   int remote_port;
67   int remote_type;
68   char *remote_info;
69
70   /* Remote client ID for this connection */
71   SilcClientID *remote_id;
72
73   /* Remote local ID so that the above defined ID would not have
74      to be decoded for every packet. */
75   unsigned char *remote_id_data;
76   unsigned int remote_id_data_len;
77
78   /*
79    * Common data 
80    */
81   /* Keys */
82   SilcCipher send_key;
83   SilcCipher receive_key;
84   SilcHmac hmac;
85   unsigned char *hmac_key;
86   unsigned int hmac_key_len;
87
88   /* Client ID and Channel ID cache. Messages transmitted in SILC network
89      are done using different unique ID's. These are the cache for
90      thoses ID's used in the communication. */
91   SilcIDCache client_cache;
92   SilcIDCache channel_cache;
93   SilcIDCache server_cache;
94
95   /* Current channel on window. All channel's are saved (allocated) into
96      the cache entries. */
97   SilcChannelEntry current_channel;
98
99   /* Socket connection object for this connection (window). This
100      object will have a back-pointer to this window object for fast
101      referencing (sock->user_data). */
102   SilcSocketConnection sock;
103
104   /* Requested pings. */
105   SilcClientPing *ping;
106   unsigned int ping_count;
107
108   /* The actual physical screen. This data is handled by the
109      screen handling routines. */
110   void *screen;
111 };
112
113 struct SilcClientObject {
114   char *username;
115   char *realname;
116
117   /* Private and public key */
118   SilcPKCS pkcs;
119   SilcPublicKey public_key;
120   SilcPrivateKey private_key;
121
122   /* SILC client task queues */
123   SilcTaskQueue io_queue;
124   SilcTaskQueue timeout_queue;
125   SilcTaskQueue generic_queue;
126
127   /* Input buffer that holds the characters user types. This is
128      used only to store the typed chars for a while. */
129   SilcBuffer input_buffer;
130
131   /* Table of windows in client. All the data, including connection
132      specific data, is saved in here. */
133   SilcClientWindow *windows;
134   unsigned int windows_count;
135
136   /* Currently active window. This is pointer to the window table 
137      defined above. This must never be free'd directly. */
138   SilcClientWindow current_win;
139
140   /* The SILC client screen object */
141   SilcScreen screen;
142
143   /* Generic cipher and hash objects */
144   SilcCipher none_cipher;
145   SilcHash md5hash;
146   SilcHash sha1hash;
147   SilcHmac md5hmac;
148   SilcHmac sha1hmac;
149
150   /* Configuration object */
151   SilcClientConfig config;
152
153   /* Random Number Generator */
154   SilcRng rng;
155
156 #ifdef SILC_SIM
157   /* SIM (SILC Module) table */
158   SilcSimContext **sim;
159   unsigned int sim_count;
160 #endif
161 };
162
163 /* Macros */
164
165 #ifndef CTRL
166 #define CTRL(x) ((x) & 0x1f)    /* Ctrl+x */
167 #endif
168
169 /* Registers generic task for file descriptor for reading from network and
170    writing to network. As being generic task the actual task is allocated 
171    only once and after that the same task applies to all registered fd's. */
172 #define SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd)                      \
173 do {                                                                    \
174   SilcTask tmptask = silc_task_register(client->generic_queue, (fd),    \
175                                         silc_client_packet_process,     \
176                                         context, 0, 0,                  \
177                                         SILC_TASK_GENERIC,              \
178                                         SILC_TASK_PRI_NORMAL);          \
179   silc_task_set_iotype(tmptask, SILC_TASK_WRITE);                       \
180 } while(0)
181
182 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(fd)                \
183 do {                                                            \
184   silc_schedule_set_listen_fd((fd), (1L << SILC_TASK_READ));    \
185 } while(0)                                                      \
186      
187 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(fd)               \
188 do {                                                            \
189   silc_schedule_set_listen_fd((fd), ((1L << SILC_TASK_READ) |   \
190                                      (1L << SILC_TASK_WRITE))); \
191 } while(0)
192
193 /* Finds socket connection object by file descriptor */
194 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock)         \
195 do {                                                    \
196   int __i;                                              \
197                                                         \
198   for (__i = 0; __i < (__x)->windows_count; __i++)      \
199     if ((__x)->windows[__i]->sock->sock == (__fd))      \
200       break;                                            \
201                                                         \
202   if (__i >= (__x)->windows_count)                      \
203     (__sock) = NULL;                                    \
204  (__sock) = (__x)->windows[__i]->sock;                  \
205 } while(0)
206
207 /* Returns TRUE if windows is currently active window */
208 #define SILC_CLIENT_IS_CURRENT_WIN(__x, __win) ((__x)->current_win == (__win))
209
210 /* Prototypes */
211 int silc_client_alloc(SilcClient *new_client);
212 void silc_client_free(SilcClient client);
213 int silc_client_init(SilcClient client);
214 void silc_client_stop(SilcClient client);
215 void silc_client_run(SilcClient client);
216 void silc_client_parse_command_line(unsigned char *buffer, 
217                                     unsigned char ***parsed,
218                                     unsigned int **parsed_lens,
219                                     unsigned int **parsed_types,
220                                     unsigned int *parsed_num,
221                                     unsigned int max_args);
222 int silc_client_connect_to_server(SilcClient client, int port,
223                                   char *host);
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                              unsigned int data_len, 
233                              int force_send);
234 void silc_client_packet_send_to_channel(SilcClient client, 
235                                         SilcSocketConnection sock,
236                                         SilcChannelEntry channel,
237                                         unsigned char *data, 
238                                         unsigned int data_len, 
239                                         int force_send);
240 void silc_client_packet_send_private_message(SilcClient client,
241                                              SilcSocketConnection sock,
242                                              SilcClientEntry client_entry,
243                                              unsigned char *data, 
244                                              unsigned int data_len, 
245                                              int force_send);
246 void silc_client_close_connection(SilcClient client,
247                                   SilcSocketConnection sock);
248 void silc_client_disconnected_by_server(SilcClient client,
249                                         SilcSocketConnection sock,
250                                         SilcBuffer message);
251 void silc_client_error_by_server(SilcClient client,
252                                  SilcSocketConnection sock,
253                                  SilcBuffer message);
254 void silc_client_notify_by_server(SilcClient client,
255                                   SilcSocketConnection sock,
256                                   SilcBuffer message);
257 void silc_client_receive_new_id(SilcClient client,
258                                 SilcSocketConnection sock,
259                                 unsigned char *id_string);
260 void silc_client_new_channel_id(SilcClient client,
261                                 SilcSocketConnection sock,
262                                 char *channel_name,
263                                 unsigned int mode,
264                                 unsigned char *id_string);
265 void silc_client_receive_channel_key(SilcClient client,
266                                      SilcSocketConnection sock,
267                                      SilcBuffer packet);
268 void silc_client_channel_message(SilcClient client, 
269                                  SilcSocketConnection sock, 
270                                  SilcPacketContext *packet);
271 #endif