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