updates.
[silc.git] / lib / silcclient / client_internal.h
1 /*
2
3   client_internal.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2001 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; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 #ifndef CLIENT_INTERNAL_H
21 #define CLIENT_INTERNAL_H
22
23 /* Internal context for connection process. This is needed as we
24    doing asynchronous connecting. */
25 typedef struct {
26   SilcClient client;
27   SilcClientConnection conn;
28   SilcTask task;
29   int sock;
30   char *host;
31   int port;
32   int tries;
33   void *context;
34 } SilcClientInternalConnectContext;
35
36 /* Structure to hold ping time information. Every PING command will 
37    add entry of this structure and is removed after reply to the ping
38    as been received. */
39 struct SilcClientPingStruct {
40   time_t start_time;
41   void *dest_id;
42   char *dest_name;
43 };
44
45 /* Structure to hold away messages set by user. This is mainly created
46    for future extensions where away messages could be set according filters
47    such as nickname and hostname. For now only one away message can 
48    be set in one connection. */
49 struct SilcClientAwayStruct {
50   char *away;
51   struct SilcClientAwayStruct *next;
52 };
53
54 /* Internal context for the client->internal pointer in the SilcClient. */
55 struct SilcClientInternalStruct {
56   /* All client operations that are implemented by the application. */
57   SilcClientOperations *ops;
58
59   /* Client Parameters */
60   SilcClientParams *params;
61
62   /* Table of connections in client. All the connection data is saved here. */
63   SilcClientConnection *conns;
64   SilcUInt32 conns_count;
65
66   /* Table of listenning sockets in client.  Client can have listeners
67      (like key agreement protocol server) and those sockets are saved here.
68      This table is checked always if the connection object cannot be found
69      from the `conns' table. */
70   SilcSocketConnection *sockets;
71   SilcUInt32 sockets_count;
72
73   /* Registered commands */
74   SilcList commands;
75
76   /* Generic cipher and hash objects. */
77   SilcCipher none_cipher;
78   SilcHash md5hash;
79   SilcHash sha1hash;
80   SilcHmac md5hmac;
81   SilcHmac sha1hmac;
82
83   /* Client version. Used to compare to remote host's version strings. */
84   char *silc_client_version;
85 };
86
87 /* Session resuming callback */
88 typedef void (*SilcClientResumeSessionCallback)(SilcClient client,
89                                                 SilcClientConnection conn,
90                                                 bool success,
91                                                 void *context);
92
93 /* Macros */
94
95 /* Registers generic task for file descriptor for reading from network and
96    writing to network. As being generic task the actual task is allocated 
97    only once and after that the same task applies to all registered fd's. */
98 #define SILC_CLIENT_REGISTER_CONNECTION_FOR_IO(fd)      \
99 do {                                                    \
100   silc_schedule_task_add(client->schedule, (fd),        \
101                          silc_client_packet_process,    \
102                          context, 0, 0,                 \
103                          SILC_TASK_GENERIC,             \
104                          SILC_TASK_PRI_NORMAL);         \
105 } while(0)
106
107 #define SILC_CLIENT_SET_CONNECTION_FOR_INPUT(s, fd)             \
108 do {                                                            \
109   silc_schedule_set_listen_fd((s), (fd), SILC_TASK_READ);       \
110 } while(0)
111      
112 #define SILC_CLIENT_SET_CONNECTION_FOR_OUTPUT(s, fd)            \
113 do {                                                            \
114   silc_schedule_set_listen_fd((s), (fd), (SILC_TASK_READ |      \
115                                           SILC_TASK_WRITE));    \
116 } while(0)
117
118 /* Finds socket connection object by file descriptor */
119 #define SILC_CLIENT_GET_SOCK(__x, __fd, __sock)                 \
120 do {                                                            \
121   int __i;                                                      \
122                                                                 \
123   for (__i = 0; __i < (__x)->internal->conns_count; __i++)      \
124     if ((__x)->internal->conns[__i] &&                          \
125         (__x)->internal->conns[__i]->sock->sock == (__fd))      \
126       break;                                                    \
127                                                                 \
128   if (__i >= (__x)->internal->conns_count) {                    \
129     (__sock) = NULL;                                            \
130     for (__i = 0; __i < (__x)->internal->sockets_count; __i++)  \
131       if ((__x)->internal->sockets[__i] &&                      \
132           (__x)->internal->sockets[__i]->sock == (__fd))        \
133         (__sock) = (__x)->internal->sockets[__i];               \
134   } else                                                        \
135     (__sock) = (__x)->internal->conns[__i]->sock;               \
136 } while(0)
137
138 /* Check whether rekey protocol is active */
139 #define SILC_CLIENT_IS_REKEY(sock)                                      \
140   (sock->protocol && sock->protocol->protocol &&                        \
141    sock->protocol->protocol->type == SILC_PROTOCOL_CLIENT_REKEY)
142
143 /* Prototypes */
144
145 SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process);
146 int silc_client_packet_send_real(SilcClient client,
147                                  SilcSocketConnection sock,
148                                  bool force_send);
149 void silc_client_ftp_free_sessions(SilcClient client,
150                                    SilcClientConnection conn);
151 void silc_client_ftp_session_free(SilcClientFtpSession session);
152 void silc_client_ftp_session_free_client(SilcClientConnection conn,
153                                          SilcClientEntry client_entry);
154 void silc_client_packet_send(SilcClient client, 
155                              SilcSocketConnection sock,
156                              SilcPacketType type, 
157                              void *dst_id,
158                              SilcIdType dst_id_type,
159                              SilcCipher cipher,
160                              SilcHmac hmac,
161                              unsigned char *data, 
162                              SilcUInt32 data_len, 
163                              int force_send);
164 void silc_client_close_connection_real(SilcClient client,
165                                        SilcSocketConnection sock,
166                                        SilcClientConnection conn);
167 void silc_client_disconnected_by_server(SilcClient client,
168                                         SilcSocketConnection sock,
169                                         SilcBuffer packet);
170 void silc_client_error_by_server(SilcClient client,
171                                  SilcSocketConnection sock,
172                                  SilcBuffer message);
173 void silc_client_receive_new_id(SilcClient client,
174                                 SilcSocketConnection sock,
175                                 SilcIDPayload idp);
176 void silc_client_save_channel_key(SilcClient client,
177                                   SilcClientConnection conn,
178                                   SilcBuffer key_payload, 
179                                   SilcChannelEntry channel);
180 void silc_client_receive_channel_key(SilcClient client,
181                                      SilcSocketConnection sock,
182                                      SilcBuffer packet);
183 void silc_client_channel_message(SilcClient client, 
184                                  SilcSocketConnection sock, 
185                                  SilcPacketContext *packet);
186 void silc_client_remove_from_channels(SilcClient client,
187                                       SilcClientConnection conn,
188                                       SilcClientEntry client_entry);
189 void silc_client_replace_from_channels(SilcClient client, 
190                                        SilcClientConnection conn,
191                                        SilcClientEntry old,
192                                        SilcClientEntry newclient);
193 void silc_client_process_failure(SilcClient client,
194                                  SilcSocketConnection sock,
195                                  SilcPacketContext *packet);
196 void silc_client_key_agreement(SilcClient client,
197                                SilcSocketConnection sock,
198                                SilcPacketContext *packet);
199 void silc_client_notify_by_server(SilcClient client,
200                                   SilcSocketConnection sock,
201                                   SilcPacketContext *packet);
202 void silc_client_private_message(SilcClient client, 
203                                  SilcSocketConnection sock, 
204                                  SilcPacketContext *packet);
205 void silc_client_connection_auth_request(SilcClient client,
206                                          SilcSocketConnection sock,
207                                          SilcPacketContext *packet);
208 void silc_client_ftp(SilcClient client,
209                      SilcSocketConnection sock,
210                      SilcPacketContext *packet);
211 SilcBuffer silc_client_get_detach_data(SilcClient client,
212                                        SilcClientConnection conn);
213 bool silc_client_process_detach_data(SilcClient client,
214                                      SilcClientConnection conn,
215                                      unsigned char **old_id,
216                                      SilcUInt16 *old_id_len);
217 void silc_client_resume_session(SilcClient client,
218                                 SilcClientConnection conn,
219                                 SilcClientResumeSessionCallback callback,
220                                 void *context);
221
222 #endif