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