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