updates.
[silc.git] / lib / silcclient / silcapi.h
1 /*
2
3   silcapi.h
4
5   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
6
7   Copyright (C) 2000 - 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; 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 SILCAPI_H
22 #define SILCAPI_H
23
24 #include "clientlibincludes.h"
25
26 /*
27    This file defines the SILC Client Library API for the application.  The
28    client operations are defined first.  These are callback functions that
29    the application MUST implement since the library may call the functions
30    at any time.  At the end of file is the API for the application that
31    it can use from the library.  This is the only file that the application
32    may include from the SIlC Client Library.
33
34    Please, refer to the README file in this directory for the directions
35    of how to use the SILC Client Library.
36 */
37
38 /******************************************************************************
39
40                            SILC Client Operations
41
42   These functions must be implemented by the application calling the SILC
43   client library. The client library can call these functions at any time.
44
45   To use this structure: define a static SilcClientOperations variable,
46   fill it and pass its pointer to silc_client_alloc function.
47
48 ******************************************************************************/
49
50 /* SILC Client Operations. These must be implemented by the application. */
51 typedef struct {
52   /* Message sent to the application by library. `conn' associates the
53      message to a specific connection.  `conn', however, may be NULL. */
54   void (*say)(SilcClient client, SilcClientConnection conn, char *msg, ...);
55
56   /* Message for a channel. The `sender' is the sender of the message 
57      The `channel' is the channel. */
58   void (*channel_message)(SilcClient client, SilcClientConnection conn, 
59                           SilcClientEntry sender, SilcChannelEntry channel, 
60                           char *msg);
61
62   /* Private message to the client. The `sender' is the sender of the
63      message. */
64   void (*private_message)(SilcClient client, SilcClientConnection conn,
65                           SilcClientEntry sender, char *msg);
66
67
68   /* Notify message to the client. The notify arguments are sent in the
69      same order as servers sends them. The arguments are same as received
70      from the server except for ID's.  If ID is received application receives
71      the corresponding entry to the ID. For example, if Client ID is received
72      application receives SilcClientEntry.  Also, if the notify type is
73      for channel the channel entry is sent to application (even if server
74      does not send it because client library gets the channel entry from
75      the Channel ID in the packet's header). */
76   void (*notify)(SilcClient client, SilcClientConnection conn, 
77                  SilcNotifyType type, ...);
78
79
80   /* Command handler. This function is called always in the command function.
81      If error occurs it will be called as well. `conn' is the associated
82      client connection. `cmd_context' is the command context that was
83      originally sent to the command. `success' is FALSE if error occured
84      during command. `command' is the command being processed. It must be
85      noted that this is not reply from server. This is merely called just
86      after application has called the command. Just to tell application
87      that the command really was processed. */
88   void (*command)(SilcClient client, SilcClientConnection conn, 
89                   SilcClientCommandContext cmd_context, int success,
90                   SilcCommand command);
91
92
93   /* Command reply handler. This function is called always in the command reply
94      function. If error occurs it will be called as well. Normal scenario
95      is that it will be called after the received command data has been parsed
96      and processed. The function is used to pass the received command data to
97      the application. 
98      
99      `conn' is the associated client connection. `cmd_payload' is the command
100      payload data received from server and it can be ignored. It is provided
101      if the application would like to re-parse the received command data,
102      however, it must be noted that the data is parsed already by the library
103      thus the payload can be ignored. `success' is FALSE if error occured.
104      In this case arguments are not sent to the application. The `status' is
105      the command reply status server returned. The `command' is the command
106      reply being processed. The function has variable argument list and each
107      command defines the number and type of arguments it passes to the
108      application (on error they are not sent). */
109   void (*command_reply)(SilcClient client, SilcClientConnection conn,
110                         SilcCommandPayload cmd_payload, int success,
111                         SilcCommand command, SilcCommandStatus status, ...);
112
113   /* Called to indicate that connection was either successfully established
114      or connecting failed.  This is also the first time application receives
115      the SilcClientConnection objecet which it should save somewhere. */
116   void (*connect)(SilcClient client, SilcClientConnection conn, int success);
117
118   /* Called to indicate that connection was disconnected to the server. */
119   void (*disconnect)(SilcClient client, SilcClientConnection conn);
120
121   /* Find authentication method and authentication data by hostname and
122      port. The hostname may be IP address as well. The found authentication
123      method and authentication data is returned to `auth_meth', `auth_data'
124      and `auth_data_len'. The function returns TRUE if authentication method
125      is found and FALSE if not. `conn' may be NULL. */
126   int (*get_auth_method)(SilcClient client, SilcClientConnection conn,
127                          char *hostname, unsigned short port,
128                          SilcProtocolAuthMeth *auth_meth,
129                          unsigned char **auth_data,
130                          unsigned int *auth_data_len);
131
132   /* Verifies received public key. The public key has been received from
133      a server. If user decides to trust the key may be saved as trusted
134      server key for later use. If user does not trust the key this returns
135      FALSE. If everything is Ok this returns TRUE. */ 
136   int (*verify_server_key)(SilcClient client, SilcClientConnection conn,
137                            unsigned char *pk, unsigned int pk_len,
138                            SilcSKEPKType pk_type);
139
140   /* Ask (interact, that is) a passphrase from user. Returns the passphrase
141      or NULL on error. */
142   unsigned char *(*ask_passphrase)(SilcClient client, 
143                                    SilcClientConnection conn);
144
145   /* Notifies application that failure packet was received.  This is called
146      if there is some protocol active in the client.  The `protocol' is the
147      protocol context.  The `failure' is opaque pointer to the failure
148      indication.  Note, that the `failure' is protocol dependant and
149      application must explicitly cast it to correct type.  Usually `failure'
150      is 32 bit failure type (see protocol specs for all protocol failure
151      types). */
152   void (*failure)(SilcClient client, SilcClientConnection conn, 
153                   SilcProtocol protocol, void *failure);
154
155   /* Asks whether the user would like to perform the key agreement protocol.
156      This is called after we have received an key agreement packet or an
157      reply to our key agreement packet. This returns TRUE if the user wants
158      the library to perform the key agreement protocol and FALSE if it is not
159      desired. */
160   int (*key_agreement)(SilcClient client, SilcClientConnection conn,
161                        SilcClientEntry client_entry);
162 } SilcClientOperations;
163
164
165
166 /******************************************************************************
167
168                            SILC Client Library API
169
170   This is the API that is published by the SILC Client Library for the
171   applications.  These functions are implemented in the SILC Client Library.
172   Application may freely call these functions from the library.
173
174 ******************************************************************************/
175
176 /* Initialization functions */
177
178 /* Allocates new client object. This has to be done before client may
179    work. After calling this one must call silc_client_init to initialize
180    the client. The `application' is application specific user data pointer
181    and caller must free it. */
182 SilcClient silc_client_alloc(SilcClientOperations *ops, void *application);
183
184 /* Frees client object and its internals. */
185 void silc_client_free(SilcClient client);
186
187 /* Initializes the client. This makes all the necessary steps to make
188    the client ready to be run. One must call silc_client_run to run the
189    client. Returns FALSE if error occured, TRUE otherwise. */
190 int silc_client_init(SilcClient client);
191
192 /* Runs the client. This starts the scheduler from the utility library.
193    When this functions returns the execution of the appliation is over. */
194 void silc_client_run(SilcClient client);
195
196 /* Stops the client. This is called to stop the client and thus to stop
197    the program. */
198 void silc_client_stop(SilcClient client);
199
200
201 /* Connecting functions */
202
203 /* Connects to remote server. This is the main routine used to connect
204    to SILC server. Returns -1 on error and the created socket otherwise. 
205    The `context' is user context that is saved into the SilcClientConnection
206    that is created after the connection is created. Note that application
207    may handle the connecting process outside the library. If this is the
208    case then this function is not used at all. When the connecting is
209    done the `connect' client operation is called. */
210 int silc_client_connect_to_server(SilcClient client, int port,
211                                   char *host, void *context);
212
213 /* Allocates and adds new connection to the client. This adds the allocated
214    connection to the connection table and returns a pointer to it. A client
215    can have multiple connections to multiple servers. Every connection must
216    be added to the client using this function. User data `context' may
217    be sent as argument. This function is normally used only if the 
218    application performed the connecting outside the library. The library
219    however may use this internally. */
220 SilcClientConnection silc_client_add_connection(SilcClient client,
221                                                 char *hostname,
222                                                 int port,
223                                                 void *context);
224
225 /* Removes connection from client. Frees all memory. */
226 void silc_client_del_connection(SilcClient client, SilcClientConnection conn);
227
228 /* Start SILC Key Exchange (SKE) protocol to negotiate shared secret
229    key material between client and server.  This function can be called
230    directly if application is performing its own connecting and does not
231    use the connecting provided by this library. This function is normally
232    used only if the application performed the connecting outside the library.
233    The library however may use this internally. */
234 int silc_client_start_key_exchange(SilcClient client,
235                                    SilcClientConnection conn,
236                                    int fd);
237
238 /* Closes connection to remote end. Free's all allocated data except
239    for some information such as nickname etc. that are valid at all time. */
240 void silc_client_close_connection(SilcClient client,
241                                   SilcClientConnection conn);
242
243
244 /* Message sending functions */
245
246 /* Sends packet to the `channel'. Packet to channel is always encrypted
247    differently from "normal" packets. SILC header of the packet is 
248    encrypted with the next receiver's key and the rest of the packet is
249    encrypted with the channel specific key. Padding and HMAC is computed
250    with the next receiver's key. The `data' is the channel message. If
251    the `force_send' is TRUE then the packet is sent immediately. */
252 void silc_client_send_channel_message(SilcClient client, 
253                                       SilcClientConnection conn,
254                                       SilcChannelEntry channel,
255                                       unsigned char *data, 
256                                       unsigned int data_len, 
257                                       int force_send);
258
259 /* Sends private message to remote client. If private message key has
260    not been set with this client then the message will be encrypted using
261    normal session keys. Private messages are special packets in SILC
262    network hence we need this own function for them. This is similiar
263    to silc_client_packet_send_to_channel except that we send private
264    message. The `data' is the private message. If the `force_send' is
265    TRUE the packet is sent immediately. */
266 void silc_client_send_private_message(SilcClient client,
267                                       SilcClientConnection conn,
268                                       SilcClientEntry client_entry,
269                                       unsigned char *data, 
270                                       unsigned int data_len, 
271                                       int force_send);
272
273
274 /* Client and Channel entry retrieval */
275
276 /* Callback function given to the silc_client_get_client function. The
277    found entries are allocated into the `clients' array. The array must
278    not be freed by the caller, the library will free it later. If the
279    `clients' is NULL, no such clients exist in the SILC Network. */
280 typedef void (*SilcGetClientCallback)(SilcClient client,
281                                       SilcClientConnection conn,
282                                       SilcClientEntry *clients,
283                                       unsigned int clients_count,
284                                       void *context);
285
286 /* Finds client entry or entries by the `nickname' and `server'. The 
287    completion callback will be called when the client entries has been found.
288
289    Note: this function is always asynchronous and resolves the client
290    information from the server. Thus, if you already know the client
291    information then use the silc_client_get_client_by_id function to
292    get the client entry since this function may be very slow and should
293    be used only to initially get the client entries. */
294 void silc_client_get_clients(SilcClient client,
295                              SilcClientConnection conn,
296                              char *nickname,
297                              char *server,
298                              SilcGetClientCallback completion,
299                              void *context);
300
301 /* Same as above function but does not resolve anything from the server.
302    This checks local cache and returns all clients from the cache. */
303 SilcClientEntry *silc_client_get_clients_local(SilcClient client,
304                                                SilcClientConnection conn,
305                                                char *nickname,
306                                                char *server,
307                                                unsigned int *clients_count);
308
309 /* Find entry for client by the client's ID. Returns the entry or NULL
310    if the entry was not found. */
311 SilcClientEntry silc_client_get_client_by_id(SilcClient client,
312                                              SilcClientConnection conn,
313                                              SilcClientID *client_id);
314
315 /* Finds entry for channel by the channel name. Returns the entry or NULL
316    if the entry was not found. It is found only if the client is joined
317    to the channel. */
318 SilcChannelEntry silc_client_get_channel(SilcClient client,
319                                          SilcClientConnection conn,
320                                          char *channel);
321
322
323 /* Command management */
324
325 /* Allocate Command Context. The context is defined in `command.h' file.
326    The context is used by the library commands and applications should use
327    it as well. However, application may choose to use some own context
328    for its local commands. All library commands, however, must use this
329    context. */
330 SilcClientCommandContext silc_client_command_alloc();
331
332 /* Free command context and its internals */
333 void silc_client_command_free(SilcClientCommandContext ctx);
334
335 /* Duplicate Command Context by adding reference counter. The context won't
336    be free'd untill it hits zero. */
337 SilcClientCommandContext silc_client_command_dup(SilcClientCommandContext ctx);
338
339 /* Finds and returns a pointer to the command list. Return NULL if the
340    command is not found. See the `command.[ch]' for the command list. */
341 SilcClientCommand *silc_client_command_find(const char *name);
342
343 /* Generic function to send any command. The arguments must be sent already
344    encoded into correct form and in correct order. */
345 void silc_client_send_command(SilcClient client, SilcClientConnection conn,
346                               SilcCommand command, unsigned short ident,
347                               unsigned int argc, ...);
348
349 /* Pending Command callback destructor. This is called after calling the
350    pending callback or if error occurs while processing the pending command.
351    If error occurs then the callback won't be called at all, and only this
352    destructor is called. The `context' is the context given for the function
353    silc_client_command_pending. */
354 typedef void (*SilcClientPendingDestructor)(void *context);
355
356 /* Add new pending command to be executed when reply to a command has been
357    received.  The `reply_cmd' is the command that will call the `callback'
358    with `context' when reply has been received.  If `ident is non-zero
359    the `callback' will be executed when received reply with command 
360    identifier `ident'. */
361 void silc_client_command_pending(SilcClientConnection conn,
362                                  SilcCommand reply_cmd,
363                                  unsigned short ident,
364                                  SilcClientPendingDestructor destructor,
365                                  SilcCommandCb callback,
366                                  void *context);
367
368
369 /* Private Message key management */
370
371 /* Adds private message key to the client library. The key will be used to
372    encrypt all private message between the client and the remote client
373    indicated by the `client_entry'.  If `key' is NULL and the boolean value
374    `generate_key' is TRUE then the library will generate random key.  
375    Otherwise the `key' provided by the application will be used.  It maybe
376    random key or pre-shared-key.
377
378    It is not necessary to set key for normal private message usage. If the
379    key is not set then the private messages are encrypted using normal
380    session keys. Setting the private key, however, increases the security. 
381
382    Note that the key set using this function is sent to the remote client
383    through the SILC network. The packet is protected using normal session
384    keys. 
385
386    Returns FALSE if the key is already set for the `client_entry', TRUE
387    otherwise. */
388 int silc_client_add_private_message_key(SilcClient client,
389                                         SilcClientConnection conn,
390                                         SilcClientConnection client_entry,
391                                         unsigned char *key,
392                                         unsigned int key_len,
393                                         int generate_key);
394
395 /* Removes the private message from the library. The key won't be used
396    after this to protect the private messages with the remote `client_entry'
397    client. Returns FALSE on error, TRUE otherwise. */
398 int silc_client_del_private_message_key(SilcClient client,
399                                         SilcClientConnection conn,
400                                         SilcClientEntry client_entry);
401
402 /* Structure to hold the list of private message keys. The array of this
403    structure is returned by the silc_client_list_private_message_keys
404    function.  */
405 typedef struct {
406   SilcClientEntry client_entry;       /* The remote client entry */
407   unsigned char *key;                 /* The raw key data */
408   unsigned int key_len;               /* The key length */
409   int generated;                      /* TRUE if library generated the key */
410 } *SilcPrivateMessageKeys;
411
412 /* Returns array of set private message keys associated to the connection
413    `conn'. Returns allocated SilcPrivateMessageKeys array and the array
414    count to the `key_count' argument. The array must be freed by the caller
415    by calling the silc_client_free_private_message_keys function. Note: 
416    the keys returned in the array is in raw format. It might not be desired
417    to show the keys as is. The application might choose not to show the keys
418    at all or to show the fingerprints of the keys. */
419 SilcPrivateMessageKeys
420 silc_client_list_private_message_keys(SilcClient client,
421                                       SilcClientConnection conn,
422                                       unsigned int *key_count);
423
424 /* Frees the SilcPrivateMessageKeys array returned by the function
425    silc_client_list_private_message_keys. */
426 void silc_client_free_private_message_keys(SilcPrivateMessageKeys keys,
427                                            unsigned int key_count);
428
429
430 /* Channel private key management */
431
432 /* Adds private key for channel. This may be set only if the channel's mode
433    mask includes the SILC_CHANNEL_MODE_PRIVKEY. This returns FALSE if the
434    mode is not set. When channel has private key then the messages are
435    encrypted using that key. All clients on the channel must also know the
436    key in order to decrypt the messages. However, it is possible to have
437    several private keys per one channel. In this case only some of the
438    clients on the channel may now the one key and only some the other key.
439
440    The private key for channel is optional. If it is not set then the
441    channel messages are encrypted using the channel key generated by the
442    server. However, setting the private key (or keys) for the channel 
443    significantly adds security. If more than one key is set the library
444    will automatically try all keys at the message decryption phase. Note:
445    setting many keys slows down the decryption phase as all keys has to
446    be tried in order to find the correct decryption key. However, setting
447    a few keys does not have big impact to the decryption performace. 
448
449    Note that this is entirely local setting. The key set using this function
450    is not sent to the network at any phase. */
451 int silc_client_add_channel_private_key(SilcClient client,
452                                         SilcClientConnection conn,
453                                         SilcChannelEntry channel,
454                                         unsigned char *key,
455                                         unsigned int key_len);
456
457 /* Removes all private keys from the `channel'. The old channel key is used
458    after calling this to protect the channel messages. Returns FALSE on
459    on error, TRUE otherwise. */
460 int silc_client_del_channel_private_keys(SilcClient client,
461                                          SilcClientConnection conn,
462                                          SilcChannelEntry channel);
463
464 /* Structure to hold one channel private key. */
465 typedef struct {
466   unsigned char *key;                 /* The key */
467   unsigned int key_len;               /* The key length */
468 } *SilcChannelPrivateKey;
469
470 /* Removes and frees private key `key' from the channel `channel'. The `key'
471    is retrieved by calling the function silc_client_list_channel_private_keys.
472    The key is not used after this. If the key was last private key then the
473    old channel key is used hereafter to protect the channel messages. This
474    returns FALSE on error, TRUE otherwise. */
475 int silc_client_del_channel_private_key(SilcClient client,
476                                         SilcClientConnection conn,
477                                         SilcChannelEntry channel,
478                                         SilcChannelPrivateKey key);
479
480 /* Returns array (pointers) of private keys associated to the `channel'.
481    The caller must free the array by calling the function
482    silc_client_free_channel_private_keys. The pointers in the array may be
483    used to delete the specific key by giving the pointer as argument to the
484    function silc_client_del_channel_private_key. */
485 SilcChannelPrivateKey *
486 silc_client_list_channel_private_keys(SilcClient client,
487                                       SilcClientConnection conn,
488                                       SilcChannelEntry channel,
489                                       unsigned int key_count);
490
491 /* Frees the SilcChannelPrivateKey array. */
492 void silc_client_free_channel_private_keys(SilcChannelPrivateKey *keys,
493                                            unsigned int key_count);
494
495
496 /* Key Agreement routines */
497
498 /* Key agreement callback that is called after the key agreement protocol
499    has been performed. This is called also if error occured during the
500    key agreement protocol. The `key' is the allocated key material and
501    the caller is responsible of freeing it. The `key' is NULL if error
502    has occured. The application can freely use the `key' to whatever
503    purpose it needs. */
504 typedef void (*SilcKeyAgreementCallback)(SilcClient client,
505                                          SilcClientConnection conn,
506                                          SilcClientEntry client_entry,
507                                          unsigned char *key,
508                                          unsigned int key_len,
509                                          void *context);
510
511 /* Sends key agreement request to the remote client indicated by the
512    `client_entry'. If the caller provides the `hostname' and the `port'
513    arguments then the library will bind the client to that hostname and
514    that port for the key agreement protocol. It also sends the `hostname'
515    and the `port' in the key agreement packet to the remote client. This
516    would indicate that the remote client may initiate the key agreement
517    protocol to the `hostname' on the `port'.
518
519    If the `hostname' and `port' is not provided then empty key agreement
520    packet is sent to the remote client. The remote client may reply with
521    the same packet including its hostname and port. If the library receives
522    the reply from the remote client the `key_agreement' client operation
523    callback will be called to verify whether the user wants to perform the
524    key agreement or not.
525
526    Note, that if the remote side decides not to initiate the key agreement
527    or decides not to reply with the key agreement packet then we cannot
528    perform the key agreement at all. If the key agreement protocol is
529    performed the `completion' callback with `context' will be called.
530    If remote side decides to ignore the request the `completion' will never
531    be called and the caller is responsible of freeing the `context' memory. 
532    Application can do this by setting for example timeout. */
533 void silc_client_send_key_agreement(SilcClient client,
534                                     SilcClientConnection conn,
535                                     SilcClientEntry client_entry,
536                                     char *hostname,
537                                     int port,
538                                     SilcKeyAgreementCallback completion,
539                                     void *context);
540
541 /* This function can be called to unbind the hostname and the port for
542    the key agreement protocol. However, this function has effect only 
543    before the key agreement protocol has been performed. After it has
544    been performed the library will automatically unbind the port. The 
545    `client_entry' is the client to which we sent the key agreement 
546    request. */
547 void silc_client_abort_key_agreement(SilcClient client,
548                                      SilcClientConnection conn,
549                                      SilcClientEntry client_entry);
550
551 #endif