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