clientlib: Close connection after failed rekey
[silc.git] / lib / silccore / silcpacket.h
1 /*
2
3   silcpacket.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2009 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 /****h* silccore/SILC Packet Engine Interface
21  *
22  * DESCRIPTION
23  *
24  * The SILC secure binary packet protocol interface, provides interface for
25  * sending and receiving SILC packets.  The interface provides a packet
26  * engine, that can be used to receive packets from packet streams, and
27  * routines for sending all kinds of SILC packets.
28  *
29  * The packet engine and packet stream are thread safe.  They can be safely
30  * used in multi threaded environment.
31  *
32  ***/
33
34 #ifndef SILCPACKET_H
35 #define SILCPACKET_H
36
37 /* Maximum packet length */
38 #define SILC_PACKET_MAX_LEN 0xffff
39
40 /* Maximum length of ID */
41 #define SILC_PACKET_MAX_ID_LEN 28
42
43 /****d* silccore/SilcPacketAPI/SilcPacketType
44  *
45  * NAME
46  *
47  *    typedef SilcUInt8 SilcPacketType;
48  *
49  * DESCRIPTION
50  *
51  *    SILC packet type definition and all the packet types.
52  *
53  * SOURCE
54  */
55 typedef SilcUInt8 SilcPacketType;
56
57 /* SILC Packet types. */
58 #define SILC_PACKET_DISCONNECT           1       /* Disconnection */
59 #define SILC_PACKET_SUCCESS              2       /* Success */
60 #define SILC_PACKET_FAILURE              3       /* Failure */
61 #define SILC_PACKET_REJECT               4       /* Rejected */
62 #define SILC_PACKET_NOTIFY               5       /* Notify message */
63 #define SILC_PACKET_ERROR                6       /* Error message */
64 #define SILC_PACKET_CHANNEL_MESSAGE      7       /* Message for channel */
65 #define SILC_PACKET_CHANNEL_KEY          8       /* Key of the channel */
66 #define SILC_PACKET_PRIVATE_MESSAGE      9       /* Private message */
67 #define SILC_PACKET_PRIVATE_MESSAGE_KEY  10      /* Private message key*/
68 #define SILC_PACKET_COMMAND              11      /* Command */
69 #define SILC_PACKET_COMMAND_REPLY        12      /* Reply to a command */
70 #define SILC_PACKET_KEY_EXCHANGE         13      /* Start of KE */
71 #define SILC_PACKET_KEY_EXCHANGE_1       14      /* KE1 */
72 #define SILC_PACKET_KEY_EXCHANGE_2       15      /* KE2 */
73 #define SILC_PACKET_CONNECTION_AUTH_REQUEST 16   /* Request of auth meth */
74 #define SILC_PACKET_CONNECTION_AUTH      17      /* Connectinon auth */
75 #define SILC_PACKET_NEW_ID               18      /* Sending new ID */
76 #define SILC_PACKET_NEW_CLIENT           19      /* Client registering */
77 #define SILC_PACKET_NEW_SERVER           20      /* Server registering */
78 #define SILC_PACKET_NEW_CHANNEL          21      /* Channel registering */
79 #define SILC_PACKET_REKEY                22      /* Re-key start */
80 #define SILC_PACKET_REKEY_DONE           23      /* Re-key done */
81 #define SILC_PACKET_HEARTBEAT            24      /* Heartbeat */
82 #define SILC_PACKET_KEY_AGREEMENT        25      /* Key Agreement request */
83 #define SILC_PACKET_RESUME_ROUTER        26      /* Backup router resume */
84 #define SILC_PACKET_FTP                  27      /* File Transfer */
85 #define SILC_PACKET_RESUME_CLIENT        28      /* Client resume */
86 #define SILC_PACKET_ACK                  29      /* Acknowledgement */
87
88 #define SILC_PACKET_PRIVATE              200     /* Private range start  */
89 #define SILC_PACKET_MAX                  255     /* RESERVED */
90
91 #define SILC_PACKET_NONE                 0       /* RESERVED */
92 #define SILC_PACKET_ANY                  0
93 /***/
94
95 /****d* silccore/SilcPacketAPI/SilcPacketFlags
96  *
97  * NAME
98  *
99  *    typedef SilcUInt8 SilcPacketFlags;
100  *
101  * DESCRIPTION
102  *
103  *    SILC packet flags type definition and all the packet flags.
104  *
105  * SOURCE
106  */
107 typedef SilcUInt8 SilcPacketFlags;
108
109 /* All defined packet flags */
110 #define SILC_PACKET_FLAG_NONE             0x00    /* No flags */
111 #define SILC_PACKET_FLAG_PRIVMSG_KEY      0x01    /* Private message key */
112 #define SILC_PACKET_FLAG_LIST             0x02    /* Packet is a list */
113 #define SILC_PACKET_FLAG_BROADCAST        0x04    /* Packet is a broadcast */
114 #define SILC_PACKET_FLAG_COMPRESSED       0x08    /* Payload is compressed */
115 #define SILC_PACKET_FLAG_ACK              0x10    /* Acknowledge packet */
116
117 /* Impelemntation specific flags */
118 #define SILC_PACKET_FLAG_LONG_PAD         0x20    /* Use maximum padding */
119 /***/
120
121 /****s* silccore/SilcPacketAPI/SilcPacketEngine
122  *
123  * NAME
124  *
125  *    typedef struct SilcPacketEngineStruct *SilcPacketEngine;
126  *
127  * DESCRIPTION
128  *
129  *    The packet engine context, allocated by silc_packet_engine_start.
130  *    The engine is destroyed with silc_packet_engine_stop.
131  *
132  ***/
133 typedef struct SilcPacketEngineStruct *SilcPacketEngine;
134
135 /****s* silccore/SilcPacketAPI/SilcPacketStream
136  *
137  * NAME
138  *
139  *    typedef struct SilcPacketStreamStruct *SilcPacketStream;
140  *
141  * DESCRIPTION
142  *
143  *    The packet stream context, allocated by silc_packet_stream_create.
144  *    The stream is destroyed with silc_packet_stream_destroy.
145  *
146  ***/
147 typedef struct SilcPacketStreamStruct *SilcPacketStream;
148
149 /****s* silccore/SilcPacketAPI/SilcPacket
150  *
151  * NAME
152  *
153  *    typedef struct SilcPacketStruct *SilcPacket;
154  *
155  * DESCRIPTION
156  *
157  *    The SilcPacket is returned by the packet engine in the SilcPacketReceive
158  *    callback.  The application can parse the data payload from the
159  *    SilcPacket.  Also packet type, flags, and sender and destination
160  *    IDs are available.  The application must free the packet with the
161  *    silc_packet_free function if it takes it in for processing.
162  *
163  *    The `buffer' field contains the parsed packet payload and the start
164  *    of the data area will point to the start of the packet payload.
165  *
166  *    The list pointer `next' can be used by the application to put the
167  *    packet context in a list during processing, if needed.
168  *
169  * SOURCE
170  */
171 typedef struct SilcPacketStruct {
172   struct SilcPacketStruct *next;     /* List pointer, application may set */
173   SilcPacketStream stream;           /* Packet stream this packet is from */
174   SilcBufferStruct buffer;           /* Packet data payload */
175   unsigned char *src_id;             /* Source ID */
176   unsigned char *dst_id;             /* Destination ID */
177   unsigned int src_id_len  : 6;      /* Source ID length */
178   unsigned int src_id_type : 2;      /* Source ID type */
179   unsigned int dst_id_len  : 6;      /* Destination ID length */
180   unsigned int dst_id_type : 2;      /* Destination ID type */
181   SilcPacketType type;               /* Packet type */
182   SilcPacketFlags flags;             /* Packet flags */
183 } *SilcPacket;
184 /***/
185
186 /****d* silcutil/SilcPacketAPI/SilcPacketError
187  *
188  * NAME
189  *
190  *    typedef enum { ... } SilcPacketError
191  *
192  * DESCRIPTION
193  *
194  *    Packet errors.  This is returned in the error callback.  If application
195  *    needs the actual lower level stream error, it needs to retrieve it
196  *    from the actual stream.  It can retrieve the underlaying stream from
197  *    the packet stream by calling silc_packet_stream_get_stream function.
198  *
199  *    You may retrieve string version of the SilcPacketError by calling
200  *    silc_packet_error_string.
201  *
202  * SOURCE
203  */
204 typedef enum {
205   SILC_PACKET_ERR_READ,                  /* Error while reading */
206   SILC_PACKET_ERR_WRITE,                 /* Error while writing */
207   SILC_PACKET_ERR_MAC_FAILED,            /* Packet MAC check failed */
208   SILC_PACKET_ERR_DECRYPTION_FAILED,     /* Packet decryption failed */
209   SILC_PACKET_ERR_UNKNOWN_SID,           /* Unknown SID (with IV included) */
210   SILC_PACKET_ERR_MALFORMED,             /* Packet is malformed */
211   SILC_PACKET_ERR_NO_MEMORY,             /* System out of memory */
212 } SilcPacketError;
213 /***/
214
215 /****f* silccore/SilcPacketAPI/SilcPacketReceiveCb
216  *
217  * SYNOPSIS
218  *
219  *    typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
220  *                                            SilcPacketStream stream,
221  *                                            SilcPacket packet,
222  *                                            void *callback_context,
223  *                                            void *stream_context);
224  *
225  * DESCRIPTION
226  *
227  *    The packet receive callback is called by the packet engine when a new
228  *    SILC Packet has arrived.  The application must free the returned
229  *    SilcPacket with silc_packet_free if it takes the packet in for
230  *    processing.  This callback is set in the SilcPacketCallbacks structure.
231  *    The `callback_context' is the context set as argument in the
232  *    silc_packet_engine_start function.  The `stream_context' is stream
233  *    specific context that was set by calling silc_packet_set_context.
234  *
235  *    If the application takes the received packet `packet' into processing
236  *    TRUE must be returned.  If FALSE is returned the packet engine will
237  *    pass the packet to other packet processor, if one has been linked
238  *    to the stream with silc_packet_stream_link function.  If no extra
239  *    processor is linked the packet is dropped.
240  *
241  * EXAMPLE
242  *
243  *    SilcBool
244  *    silc_foo_packet_receive_cb(SilcPacketEngine engine,
245  *                               SilcPacketStream stream, SilcPacket packet,
246  *                               void *callback_context, void *stream_context)
247  *    {
248  *      Application ctx = callback_context;
249  *
250  *      // If we're not up yet, let's not process the packet
251  *      if (ctx->initialized == FALSE)
252  *        return FALSE;
253  *
254  *      // Process the incoming packet...
255  *      ...
256  *
257  *      // It's our packet now, no one else will get it
258  *      return TRUE;
259  *    }
260  *
261  ***/
262 typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
263                                         SilcPacketStream stream,
264                                         SilcPacket packet,
265                                         void *callback_context,
266                                         void *stream_context);
267
268 /****f* silccore/SilcPacketAPI/SilcPacketEosCb
269  *
270  * SYNOPSIS
271  *
272  *    typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
273  *                                    SilcPacketStream stream,
274  *                                    void *callback_context,
275  *                                    void *stream_context);
276  *
277  * DESCRIPTION
278  *
279  *    The End Of Stream (EOS) callback, that is called by the packet engine
280  *    when the underlaying stream has ended.  No more data can be sent to
281  *    the stream or read from it.  The `stream' must be destroyed by
282  *    calling the silc_packet_stream_destroy.  This callback is set in the
283  *    SilcPacketCallbacks structure.
284  *
285  ***/
286 typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
287                                 SilcPacketStream stream,
288                                 void *callback_context,
289                                 void *stream_context);
290
291 /****f* silccore/SilcPacketAPI/SilcPacketErrorCb
292  *
293  * SYNOPSIS
294  *
295  *    typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
296  *                                      SilcPacketStream stream,
297  *                                      SilcPacketError error,
298  *                                      void *callback_context,
299  *                                      void *stream_context);
300  *
301  * DESCRIPTION
302  *
303  *    The error callback that is called by the packet engine if an error
304  *    occurs.  The `error' will indicate the error.  This callback is set
305  *    in the SilcPacketCallbacks structure.
306  *
307  ***/
308 typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
309                                   SilcPacketStream stream,
310                                   SilcPacketError error,
311                                   void *callback_context,
312                                   void *stream_context);
313
314 /****s* silccore/SilcPacketAPI/SilcPacketCallbacks
315  *
316  * NAME
317  *
318  *    typedef struct { ... } *SilcPacketCallbacks;
319  *
320  * DESCRIPTION
321  *
322  *    This structure is sent as argument to the silc_packet_engine_start
323  *    function to set the callback functions for the packet engine.  The
324  *    packet engine will call the callbacks when necessary.  Application
325  *    must always be provided for the packet engine.
326  *
327  * SOURCE
328  */
329 typedef struct {
330   SilcPacketReceiveCb packet_receive;    /* Called when packet is received */
331   SilcPacketEosCb eos;                   /* Called on end of stream */
332   SilcPacketErrorCb error;               /* Called on an error */
333 } SilcPacketCallbacks;
334 /***/
335
336 /* Prototypes */
337
338 /****f* silccore/SilcPacketAPI/silc_packet_engine_start
339  *
340  * SYNOPSIS
341  *
342  *    SilcPacketEngine
343  *    silc_packet_engine_start(SilcRng rng, SilcBool router,
344  *                             SilcPacketCallbacks *callbacks,
345  *                             void *callback_context);
346  *
347  * DESCRIPTION
348  *
349  *    Create new packet engine for processing incoming and outgoing packets.
350  *    If `router' is  TRUE then the application is considered to be router
351  *    server, and certain packets are handled differently.  Client and normal
352  *    server must set it to FALSE.  The `callbacks' is a SilcPacketCallbacks
353  *    structure provided by the caller which includes the callbacks that is
354  *    called when for example packet is received, or end of stream is called.
355  *
356  * NOTES
357  *
358  *    The packet engine is thread safe.  You can use one packet engine in
359  *    multi threaded application.
360  *
361  ***/
362 SilcPacketEngine
363 silc_packet_engine_start(SilcRng rng, SilcBool router,
364                          const SilcPacketCallbacks *callbacks,
365                          void *callback_context);
366
367 /****f* silccore/SilcPacketAPI/silc_packet_engine_stop
368  *
369  * SYNOPSIS
370  *
371  *    void silc_packet_engine_stop(SilcPacketEngine engine);
372  *
373  * DESCRIPTION
374  *
375  *    Stop the packet engine.  No new packets can be sent or received after
376  *    calling this, and the `engine' will become invalid.
377  *
378  ***/
379 void silc_packet_engine_stop(SilcPacketEngine engine);
380
381 /****f* silccore/SilcPacketAPI/silc_packet_error_string
382  *
383  * SYNOPSIS
384  *
385  *    const char *silc_packet_error_string(SilcPacketError error);
386  *
387  * DESCRIPTION
388  *
389  *    Return the packet error as string.
390  *
391  ***/
392 const char *silc_packet_error_string(SilcPacketError error);
393
394 /****f* silccore/SilcPacketAPI/silc_packet_engine_get_streams
395  *
396  * SYNOPSIS
397  *
398  *    SilcDList silc_packet_engine_get_streams(SilcPacketEngine engine);
399  *
400  * DESCRIPTION
401  *
402  *    Returns list of packet streams added to the packet engine.  The caller
403  *    must free the list with silc_packet_engine_free_streams_list.
404  *
405  * NOTES
406  *
407  *    This function may also return disconnected and destroyed streams.  The
408  *    caller should use silc_packet_stream_is_valid to check if the stream
409  *    is valid.
410  *
411  ***/
412 SilcDList silc_packet_engine_get_streams(SilcPacketEngine engine);
413
414 /****f* silccore/SilcPacketAPI/silc_packet_engine_free_streams_list
415  *
416  * SYNOPSIS
417  *
418  *    void silc_packet_engine_free_streams_list(SilcDList streams);
419  *
420  * DESCRIPTION
421  *
422  *    Free's the streams list returned by silc_packet_engine_get_streams.
423  *
424  ***/
425 void silc_packet_engine_free_streams_list(SilcDList streams);
426
427 /****f* silccore/SilcPacketAPI/silc_packet_stream_create
428  *
429  * SYNOPSIS
430  *
431  *    SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
432  *                                               SilcSchedule schedule,
433  *                                               SilcStream stream);
434  *
435  * DESCRIPTION
436  *
437  *    Create new packet stream and use the `stream' as underlaying stream.
438  *    Usually the `stream' would be a socket stream, but it can be any
439  *    stream.  After this function returns, packets can immediately be
440  *    sent to and received from the stream.
441  *
442  *    The `stream' will be owned by the packet stream and will be closed
443  *    and destroyed when the packet stream is destroyed.  If the packet
444  *    stream creation fails the `stream' is left intact.
445  *
446  * NOTES
447  *
448  *    SilcPacketStream cannot be used with silc_stream_* routines (such as
449  *    silc_stream_read and silc_stream_write) because of its special nature.
450  *    Use the silc_packet_send and the silc_packet_send_ext to send packets.
451  *    To read packets you will receive the packet receive callback from
452  *    packet engine.  Destroy the stream with silc_packet_stream_destroy.
453  *
454  *    The SilcPacketStream is thread safe.  Same context can be safely used
455  *    in multi threaded environment.
456  *
457  ***/
458 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
459                                            SilcSchedule schedule,
460                                            SilcStream stream);
461
462 /****f* silccore/SilcPacketAPI/silc_packet_stream_add_remote
463  *
464  * SYNOPSIS
465  *
466  *    SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
467  *                                                   const char *remote_ip,
468  *                                                   SilcUInt16 remote_port,
469  *                                                   SilcPacket packet);
470  *
471  * DESCRIPTION
472  *
473  *    This function is used to add remote receivers in packet stream `stream'
474  *    that has UDP/IP socket stream as the underlaying stream.  This function
475  *    cannot be used with other type of streams.  This returns new packet
476  *    stream context that can be used to send to and receive packets from
477  *    the specified remote IP and remote port, or NULL on error.  The `stream'
478  *    is the actual stream that is used to send and receive the data.
479  *
480  *    When the parent `stream' receives packets from remote IP address
481  *    and port that does not have its own remote packet stream, it returns
482  *    the packet to the packet callback set for `stream'.  The sender's
483  *    IP address and port can then be retrieved by using the
484  *    silc_packet_get_sender function and to create new packet stream by
485  *    calling this function.  After that, all packets from that IP address
486  *    and port will be received by the new packet stream.
487  *
488  *    If the `packet' is non-NULL it will be injected into the new packet
489  *    stream as soon as the scheduler associated with `stream' schedules
490  *    new tasks.  It can be used to inject an incoming packet to the stream.
491  *
492  *    This interface is for connectionless UDP streams.  If it is possible
493  *    to create connected stream it should be done for performance reasons.
494  *
495  * EXAMPLE
496  *
497  *    // Create parent packet stream, it can receive packets from anywhere
498  *    listener = silc_net_udp_connect("0.0.0.0", 500, NULL, 0, schedule);
499  *    parent = silc_packet_stream_create(engine, schedule, listener);
500  *
501  *    ...
502  *    // Received a packet to the parent stream, get the sender information.
503  *    silc_packet_get_sender(packet, &ip, &port);
504  *
505  *    // Create new packet stream for this remote location.
506  *    remote = silc_packet_stream_add_remote(parent, ip, port, packet);
507  *
508  ***/
509 SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
510                                                const char *remote_ip,
511                                                SilcUInt16 remote_port,
512                                                SilcPacket packet);
513
514 /****f* silccore/SilcPacketAPI/silc_packet_stream_destroy
515  *
516  * SYNOPSIS
517  *
518  *    void silc_packet_stream_destroy(SilcPacketStream stream);
519  *
520  * DESCRIPTION
521  *
522  *    Destroy packet stream and the underlaying stream.  This will also
523  *    close and destroy the underlaying stream.
524  *
525  ***/
526 void silc_packet_stream_destroy(SilcPacketStream stream);
527
528 /****f* silccore/SilcPacketAPI/silc_packet_stream_is_valid
529  *
530  * SYNOPSIS
531  *
532  *    SilcBool silc_packet_stream_is_valid(SilcPacketStream stream);
533  *
534  * DESCRIPTION
535  *
536  *    Returns TRUE if the packet stream indicated by `stream' is valid and
537  *    has not been disconnected or destroyed.
538  *
539  ***/
540 SilcBool silc_packet_stream_is_valid(SilcPacketStream stream);
541
542 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_router
543  *
544  * SYNOPSIS
545  *
546  *    void silc_packet_stream_set_router(SilcPacketStream stream);
547  *
548  * DESCRIPTION
549  *
550  *    When called sets the stream indicates by `stream' as SILC router
551  *    connection stream.  This causes that certain packets are handled
552  *    differently.  This must be called for router connection streams and
553  *    must not be called for any other stream.
554  *
555  ***/
556 void silc_packet_stream_set_router(SilcPacketStream stream);
557
558 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_iv_included
559  *
560  * SYNOPSIS
561  *
562  *    void silc_packet_stream_set_iv_included(SilcPacketStream stream);
563  *
564  * DESCRIPTION
565  *
566  *    Sets an IV Included property for the stream indicated by `stream'.
567  *    This means that the IV used in the encryption will be included in
568  *    the resulted ciphertext.  This makes it possible to send and receive
569  *    packets on unreliable network transport protocol, such as UDP/IP.
570  *    This must be called if the underlaying stream in the `stream' is UDP
571  *    stream.
572  *
573  *    When this is set to the stream the silc_packet_set_sid must be called
574  *    to set new Security ID.  The Security ID will be included with the IV
575  *    in the ciphertext.
576  *
577  ***/
578 void silc_packet_stream_set_iv_included(SilcPacketStream stream);
579
580 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_stream
581  *
582  * SYNOPSIS
583  *
584  *    void silc_packet_stream_set_stream(SilcPacketStream packet_stream,
585  *                                       SilcStream stream);
586  *
587  * DESCRIPTION
588  *
589  *    This function may be used to change the underlaying stream in the
590  *    packet stream indicated by `packet_stream'.  Note that the old
591  *    stream will not be used after calling this function.  The caller is
592  *    responsible destroying the old stream.  The `stream' will use
593  *    the same scheduler as the `packet_stream'.
594  *
595  ***/
596 void silc_packet_stream_set_stream(SilcPacketStream packet_stream,
597                                    SilcStream stream);
598
599 /****f* silccore/SilcPacketAPI/silc_packet_stream_get_stream
600  *
601  * SYNOPSIS
602  *
603  *    SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
604  *
605  * DESCRIPTION
606  *
607  *    Returns the actual stream that is associated with the packet stream
608  *    `stream'.  The caller must not free the returned stream.  The returned
609  *    stream is the same pointer that was set for silc_packet_stream_create.
610  *    This function could be used for example when an error callback is
611  *    called by the packet engine to retrieve the actual lower level error
612  *    from the stream.
613  *
614  ***/
615 SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
616
617 /****f* silccore/SilcPacketAPI/silc_packet_stream_link
618  *
619  * SYNOPSIS
620  *
621  *    SilcBool silc_packet_stream_link(SilcPacketStream stream,
622  *                                     SilcPacketCallbacks *callbacks,
623  *                                     void *callback_context,
624  *                                     int priority, ...);
625  *
626  * DESCRIPTION
627  *
628  *    Links the packet processing callbacks indicated by `callbacks' into
629  *    the packet stream indicated by `stream' with priority `priority' for
630  *    the packet types given in the variable argument list.  This function
631  *    can be used to link to the packet stream for specific packet types
632  *    and receive them in the specified callbacks.  This way, a third party,
633  *    for example some library may attach itself into the packet stream
634  *    and receive and process certain packets.  The variable argument
635  *    list is ended with -1.  To link to receive all packets use
636  *    SILC_PACKET_ANY.
637  *
638  *    The default packet processing callbacks given as argument to the
639  *    silc_packet_engine_start has the priority 0.  Any priority higher
640  *    than 0 will then take precedence over the default callbacks.  Any
641  *    priority lower than 0 (negative value) will be processed after the
642  *    default callbacks.
643  *
644  *    Note that setting only the 'packet_receive' callback in the `callbacks'
645  *    is required.
646  *
647  * EXAMPLE
648  *
649  *    // Link to this packet stream, with high priority, for
650  *    // SILC_PACKET_CONNECTION_AUTH and SILC_PACKET_CONNECTION_AUTH_REQUEST
651  *    // packets. We don't care about other packets.
652  *    silc_packet_stream_link(stream, our_callbacks, our_context,
653  *                            1000000, SILC_PACKET_CONNECTION_AUTH,
654  *                            SILC_PACKET_CONNECTION_AUTH_REQUEST, -1);
655  *
656  ***/
657 SilcBool silc_packet_stream_link(SilcPacketStream stream,
658                                  const SilcPacketCallbacks *callbacks,
659                                  void *callback_context,
660                                  int priority, ...);
661
662 /****f* silccore/SilcPacketAPI/silc_packet_stream_unlink
663  *
664  * SYNOPSIS
665  *
666  *    void silc_packet_stream_unlink(SilcPacketStream stream,
667  *                                   SilcPacketCallbacks *callbacks,
668  *                                   void *callback_context);
669  *
670  * DESCRIPTION
671  *
672  *    Unlinks the `callbacks' with `callback_context' from the packet stream
673  *    indicated by `stream'.  This function must be called for the callbacks
674  *    that was linked to `stream' when they are not needed anymore.
675  *
676  ***/
677 void silc_packet_stream_unlink(SilcPacketStream stream,
678                                const SilcPacketCallbacks *callbacks,
679                                void *callback_context);
680
681 /****f* silccore/SilcPacketAPI/SilcPacketWrapCoder
682  *
683  * SYNOPSIS
684  *
685  *    typedef SilcBool (*SilcPacketWrapCoder)(SilcStream stream,
686  *                                            SilcStreamStatus status,
687  *                                            SilcBuffer buffer,
688  *                                            void *context);
689  *
690  * DESCRIPTION
691  *
692  *    The encoder/decoder callback for silc_packet_stream_wrap.  If the
693  *    `status' is SILC_STREAM_CAN_WRITE then additional data can be added
694  *    to `buffer'.  It is added before the data that is written with
695  *    silc_stream_write.  The silc_buffer_enlarge should be called to verify
696  *    there is enough room in `buffer' before adding data to it.  The `buffer'
697  *    must not be freed.
698  *
699  *    If the `status' is SILC_STREAM_CAN_READ then data from the `buffer'
700  *    may be read before it is passed to readed when silc_stream_read is
701  *    called.  The `buffer' may be advanced also to hide data in it.
702  *
703  *    This function returns FALSE in case of error.
704  *
705  ***/
706 typedef SilcBool (*SilcPacketWrapCoder)(SilcStream stream,
707                                         SilcStreamStatus status,
708                                         SilcBuffer buffer,
709                                         void *context);
710
711 /****f* silccore/SilcPacketAPI/silc_packet_stream_wrap
712  *
713  * SYNOPSIS
714  *
715  *    SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
716  *                                       SilcPacketType type,
717  *                                       SilcPacketFlags flags,
718  *                                       SilcBool blocking_mode,
719  *                                       SilcPacketWrapCoder coder,
720  *                                       void *context);
721  *
722  * DESCRIPTION
723  *
724  *    Wraps the packet stream indicated by `stream' into a SilcStream for
725  *    the packet type indicated by `type' with packet flags indicated by
726  *    `flags'.  The returned SilcStream can be used to read and write the
727  *    specified SILC packets with the specified packet flags, by calling
728  *    silc_stream_read and silc_stream_write, respectively.  The returned
729  *    stream can be destroyed by calling silc_stream_destroy.  It does not
730  *    destroy the wrapped packet stream.
731  *
732  *    If the `blocking_mode' mode is TRUE then the silc_stream_read and
733  *    silc_stream_write may block the calling process or thread until SILC
734  *    packet is read or written.  If it is FALSE the stream is in non-blocking
735  *    mode and the calls never block.  The returned stream is thread-safe and
736  *    packets may be read and written in multi-threaded environment.
737  *
738  *    In non-blocking mode the silc_stream_set_notifier must be called before
739  *    the returned stream can be used to read packets.  The stream status
740  *    SILC_STREAM_CAN_READ will be returned to the notifier callback to
741  *    indicate that a packet is ready for reading.  Calling silc_stream_read
742  *    once returns one complete SILC packet data payload (which is of type of
743  *    `type').
744  *
745  *    The `coder' is optional encoder/decoder callback which the packet engine
746  *    will call if it is non-NULL.  It can be used to encode additional data
747  *    into each packet when silc_stream_write is called or decode data before
748  *    it is passed to reader when silc_stream_read is called.  The `context'
749  *    is passed to `coder'.
750  *
751  *    The returned SilcStream can be used as any normal stream and all
752  *    SilcStream API functions may be used with the stream.  This returns
753  *    NULL on error.
754  *
755  ***/
756 SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
757                                    SilcPacketType type,
758                                    SilcPacketFlags flags,
759                                    SilcBool blocking_mode,
760                                    SilcPacketWrapCoder coder,
761                                    void *context);
762
763 /****f* silccore/SilcPacketAPI/silc_packet_stream_is_udp
764  *
765  * SYNOPSIS
766  *
767  *    SilcBool silc_packet_stream_is_udp(SilcPacketStream stream);
768  *
769  * DESCRIPTION
770  *
771  *    Returns TRUE if the packet stream indicated by `stream' is using
772  *    UDP transport.
773  *
774  ***/
775 SilcBool silc_packet_stream_is_udp(SilcPacketStream stream);
776
777 /****f* silccore/SilcPacketAPI/silc_packet_get_sender
778  *
779  * SYNOPSIS
780  *
781  *    SilcBool silc_packet_get_sender(SilcPacket packet,
782  *                                    const char **sender_ip,
783  *                                    SilcUInt16 *sender_port);
784  *
785  * DESCRIPTION
786  *
787  *    Returns the packet sender's IP address and port from UDP packet
788  *    indicated by `packet'.  This can be called only from the packet
789  *    callback to retrieve the information of the packet's sender.  Returns
790  *    FALSE if the information is not available.
791  *
792  ***/
793 SilcBool silc_packet_get_sender(SilcPacket packet,
794                                 const char **sender_ip,
795                                 SilcUInt16 *sender_port);
796
797 /****f* silccore/SilcPacketAPI/silc_packet_stream_ref
798  *
799  * SYNOPSIS
800  *
801  *    void silc_packet_stream_ref(SilcPacketStream stream);
802  *
803  * DESCRIPTION
804  *
805  *    Increase reference counter for the stream indicated by `stream'.  This
806  *    can be used to take a reference for the stream.  To unreference the
807  *    stream call silc_packet_stream_unref function.
808  *
809  ***/
810 void silc_packet_stream_ref(SilcPacketStream stream);
811
812 /****f* silccore/SilcPacketAPI/silc_packet_stream_unref
813  *
814  * SYNOPSIS
815  *
816  *    void silc_packet_stream_unref(SilcPacketStream stream);
817  *
818  * DESCRIPTION
819  *
820  *    Decrease reference counter for the stream indicated by `stream'.  If
821  *    the counter hits zero the stream will be destroyed automatically.
822  *
823  ***/
824 void silc_packet_stream_unref(SilcPacketStream stream);
825
826 /****f* silccore/SilcPacketAPI/silc_packet_get_engine
827  *
828  * SYNOPSIS
829  *
830  *    SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
831  *
832  * DESCRIPTION
833  *
834  *    Returns the packet engine from the `stream'.
835  *
836  ***/
837 SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
838
839 /****f* silccore/SilcPacketAPI/silc_packet_set_context
840  *
841  * SYNOPSIS
842  *
843  *    void silc_packet_set_context(SilcPacketStream stream,
844  *                                 void *stream_context);
845  *
846  * DESCRIPTION
847  *
848  *    Sets a stream specific context to the stream.  The context will
849  *    be delivered to all callback functions, and it can be retrieved by
850  *    calling silc_packet_get_context function as well.  Note that this is
851  *    separate packet stream specific context, and not the same as
852  *    `callback_context' in silc_packet_engine_start.  Both will be delivered
853  *    to the callbacks, and this context as the `stream_context' argument.
854  *
855  ***/
856 void silc_packet_set_context(SilcPacketStream stream, void *stream_context);
857
858 /****f* silccore/SilcPacketAPI/silc_packet_get_context
859  *
860  * SYNOPSIS
861  *
862  *    void *silc_packet_get_context(SilcPacketStream stream);
863  *
864  * DESCRIPTION
865  *
866  *    Returns the current set application context, or NULL if none is set.
867  *
868  ***/
869 void *silc_packet_get_context(SilcPacketStream stream);
870
871 /****f* silccore/SilcPacketAPI/silc_packet_set_keys
872  *
873  * SYNOPSIS
874  *
875  *    void silc_packet_set_keys(SilcPacketStream stream, SilcCipher send_key,
876  *                              SilcCipher receive_key, SilcHmac send_hmac,
877  *                              SilcHmac receive_hmac, SilcBool rekey);
878  *
879  * DESCRIPTION
880  *
881  *    Set ciphers and HMACs to be used to encrypt sent packets, and decrypt
882  *    received packets.  This can be called multiple times to change the
883  *    ciphers and HMACs.
884  *
885  *    If the `rekey' is TRUE this function will send SILC_PACKET_REKEY_DONE
886  *    to the `stream' and will set the new keys.  If it is FALSE the keys
887  *    are changed but the packet is not changed.
888  *
889  *    When changing keys the old cipher and HMACs will be freed.  If the keys
890  *    are not set at all, packets will not be encrypted or decrypted.
891  *
892  ***/
893 SilcBool silc_packet_set_keys(SilcPacketStream stream, SilcCipher send_key,
894                               SilcCipher receive_key, SilcHmac send_hmac,
895                               SilcHmac receive_hmac, SilcBool rekey);
896
897 /****f* silccore/SilcPacketAPI/silc_packet_get_keys
898  *
899  * SYNOPSIS
900  *
901  *    SilcBool silc_packet_get_keys(SilcPacketStream stream,
902  *                                  SilcCipher *send_key,
903  *                                  SilcCipher *receive_key,
904  *                                  SilcHmac *send_hmac,
905  *                                  SilcHmac *receive_hmac);
906  *
907  * DESCRIPTION
908  *
909  *    Returns the pointers of current ciphers and HMACs from the `stream'.
910  *    Returns FALSE if keys are not set.
911  *
912  ***/
913 SilcBool silc_packet_get_keys(SilcPacketStream stream,
914                               SilcCipher *send_key, SilcCipher *receive_key,
915                               SilcHmac *send_hmac, SilcHmac *receive_hmac);
916
917 /****f* silccore/SilcPacketAPI/silc_packet_set_ids
918  *
919  * SYNOPSIS
920  *
921  *    SilcBool silc_packet_set_ids(SilcPacketStream stream,
922  *                                 SilcIdType src_id_type, const void *src_id
923  *                                 SilcIdType dst_id_type, const void *dst_id);
924  *
925  * DESCRIPTION
926  *
927  *    Set the source ID and destination ID to be used when sending packets to
928  *    this packet stream.  The IDs to be used for a packet stream can be
929  *    overridden when sending packets.  However, if the IDs do not ever change
930  *    for the packet stream it is recommended they are set using this function.
931  *    In this case they can be omitted when sending packets to the stream.
932  *    It is also possible to set only source or destination ID.
933  *
934  ***/
935 SilcBool silc_packet_set_ids(SilcPacketStream stream,
936                              SilcIdType src_id_type, const void *src_id,
937                              SilcIdType dst_id_type, const void *dst_id);
938
939 /****f* silccore/SilcPacketAPI/silc_packet_get_ids
940  *
941  * SYNOPSIS
942  *
943  *    SilcBool silc_packet_get_ids(SilcPacketStream stream,
944  *                                 SilcBool *src_id_set, SilcID *src_id,
945  *                                 SilcBool *dst_id_set, SilcID *dst_id);
946  *
947  * DESCRIPTION
948  *
949  *    Returns source and destination IDs from the packet stream.  The
950  *    `src_id_set' is set to TRUE if the source ID was returned.  The
951  *    `dst_id_set' is set to TRUE if the destination ID was returned.
952  *
953  ***/
954 SilcBool silc_packet_get_ids(SilcPacketStream stream,
955                              SilcBool *src_id_set, SilcID *src_id,
956                              SilcBool *dst_id_set, SilcID *dst_id);
957
958 /****f* silccore/SilcPacketAPI/silc_packet_set_sid
959  *
960  * SYNOPSIS
961  *
962  *    SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid);
963  *
964  * DESCRIPTION
965  *
966  *    Sets new Security ID to the packet stream indicated by `stream'.  This
967  *    is called only if the IV Included property was set to the stream
968  *    by calling silc_packet_stream_set_iv_included.  This function sets
969  *    new Security ID to the stream which is then included in the ciphertext
970  *    of a packet.  The `sid' must be 0 when it is set for the very first
971  *    time and must be increased by one after each rekey.  This function must
972  *    be called every time new keys are added to the stream after a rekey.
973  *
974  *    If this function is called when the IV Included property has not been
975  *    set to the stream the `sid' will be ignored.  Returns FALSE if the
976  *    IV Included has not been set, TRUE otherwise.
977  *
978  ***/
979 SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid);
980
981 /****f* silccore/SilcPacketAPI/silc_packet_send
982  *
983  * SYNOPSIS
984  *
985  *    SilcBool silc_packet_send(SilcPacketStream stream,
986  *                              SilcPacketType type, SilcPacketFlags flags,
987  *                              const unsigned char *data,
988  *                              SilcUInt32 data_len);
989  *
990  * DESCRIPTION
991  *
992  *    Send `data' of length of `data_len' to the packet stream indicated by
993  *    `stream'.  If ciphers and HMACs were set using silc_packet_set_keys
994  *    the packet will be encrypted and MAC will be computed for it.  If
995  *    silc_packet_set_ids was used to set source and destination ID for the
996  *    packet stream those IDs are used in the packet.  If IDs have not been
997  *    set and they need to be provided then silc_packet_send_ext function
998  *    should be used.  Otherwise, the packet will not have IDs set at all.
999  *    Returns FALSE if packet could not be sent.
1000  *
1001  ***/
1002 SilcBool silc_packet_send(SilcPacketStream stream,
1003                           SilcPacketType type, SilcPacketFlags flags,
1004                           const unsigned char *data, SilcUInt32 data_len);
1005
1006 /****f* silccore/SilcPacketAPI/silc_packet_send_ext
1007  *
1008  * SYNOPSIS
1009  *
1010  *    SilcBool
1011  *    silc_packet_send_ext(SilcPacketStream stream,
1012  *                         SilcPacketType type, SilcPacketFlags flags,
1013  *                         SilcIdType src_id_type, void *srd_id,
1014  *                         SilcIdType dst_id_type, void *dst_id,
1015  *                         const unsigned char *data, SilcUInt32 data_len,
1016  *                         SilcCipher cipher, SilcHmac hmac);
1017  *
1018  * DESCRIPTION
1019  *
1020  *    Same as silc_packet_send but with this function different sending
1021  *    parameters can be sent as argument.  This function can be used to
1022  *    set specific IDs, cipher and HMAC to be used in packet sending,
1023  *    instead of the ones saved in the `stream'.  If any of the extra
1024  *    pointers are NULL, default values set to the stream will apply.
1025  *
1026  ***/
1027 SilcBool silc_packet_send_ext(SilcPacketStream stream,
1028                               SilcPacketType type, SilcPacketFlags flags,
1029                               SilcIdType src_id_type, void *src_id,
1030                               SilcIdType dst_id_type, void *dst_id,
1031                               const unsigned char *data, SilcUInt32 data_len,
1032                               SilcCipher cipher, SilcHmac hmac);
1033
1034 /****f* silccore/SilcPacketAPI/silc_packet_send_va
1035  *
1036  * SYNOPSIS
1037  *
1038  *    SilcBool silc_packet_send_va(SilcPacketStream stream,
1039  *                                 SilcPacketType type,
1040  *                                 SilcPacketFlags flags, ...);
1041  *
1042  * DESCRIPTION
1043  *
1044  *    Same as silc_packet_send but takes the data in as variable argument
1045  *    formatted buffer (see silcbuffmt.h).  The arguments must be ended
1046  *    with SILC_STR_END.  Returns FALSE if packet could not be sent or
1047  *    the buffer could not be formatted.
1048  *
1049  * EXAMPLE
1050  *
1051  *    // Send NEW_CLIENT packet
1052  *    silc_packet_send_va(stream, SILC_PACKET_NEW_CLIENT, 0,
1053  *                        SILC_STR_UI_SHORT(username_len),
1054  *                        SILC_STR_DATA(username, username_len),
1055  *                        SILC_STR_UI_SHORT(realname_len),
1056  *                        SILC_STR_DATA(realname, realname_len),
1057  *                        SILC_STR_END);
1058  *
1059  ***/
1060 SilcBool silc_packet_send_va(SilcPacketStream stream,
1061                              SilcPacketType type, SilcPacketFlags flags, ...);
1062
1063 /****f* silccore/SilcPacketAPI/silc_packet_send_va_ext
1064  *
1065  * SYNOPSIS
1066  *
1067  *    SilcBool
1068  *    silc_packet_send_va_ext(SilcPacketStream stream,
1069  *                            SilcPacketType type, SilcPacketFlags flags,
1070  *                            SilcIdType src_id_type, void *srd_id,
1071  *                            SilcIdType dst_id_type, void *dst_id,
1072  *                            SilcCipher cipher, SilcHmac hmac, ...);
1073  *
1074  * DESCRIPTION
1075  *
1076  *    Same as silc_packet_send_va but with this function different sending
1077  *    parameters can be sent as argument.  This function can be used to
1078  *    set specific IDs, cipher and HMAC to be used in packet sending,
1079  *    instead of the ones saved in the `stream'.  If any of the extra
1080  *    pointers are NULL, default values set to the stream will apply.
1081  *
1082  ***/
1083 SilcBool silc_packet_send_va_ext(SilcPacketStream stream,
1084                                  SilcPacketType type, SilcPacketFlags flags,
1085                                  SilcIdType src_id_type, void *src_id,
1086                                  SilcIdType dst_id_type, void *dst_id,
1087                                  SilcCipher cipher, SilcHmac hmac, ...);
1088
1089 /****f* silccore/SilcPacketAPI/silc_packet_wait_init
1090  *
1091  * SYNOPSIS
1092  *
1093  *    void *silc_packet_wait_init(SilcPacketStream stream,
1094  *                                const SilcID *source_id, ...);
1095  *
1096  * DESCRIPTION
1097  *
1098  *    Initializes a packet waiter for the packet stream `stream' and
1099  *    for the variable argument list of packet types.  The function
1100  *    silc_packet_wait can be used to block the thread until a packet
1101  *    has been received.
1102  *
1103  *    This function is used to initialize the waiting and to give the list
1104  *    of packet types that caller wish to receive.  The variable argument
1105  *    list must end with -1.  To receive all packets use SILC_PACKET_ANY.
1106  *    If the `source_id' is non-NULL then only packets of the specified
1107  *    type from the specified `source_id' are received.  If it is NULL
1108  *    then the packet source is ignored.
1109  *
1110  *    Returns a context that must be given to the silc_packet_wait function
1111  *    as argument.  Returns NULL on error.  To uninitialize the waiting
1112  *    call silc_packet_wait_uninit.
1113  *
1114  * NOTES
1115  *
1116  *    Note that packets may be available immediately after calling this
1117  *    function and they will be buffered, until silc_packet_wait is called.
1118  *
1119  * EXAMPLE
1120  *
1121  *    void *waiter;
1122  *
1123  *    // Will wait for private message packets
1124  *    waiter = silc_packet_wait_init(stream, NULL,
1125  *                                   SILC_PACKET_PRIVATE_MESSAGE, -1);
1126  *
1127  ***/
1128 void *silc_packet_wait_init(SilcPacketStream stream,
1129                             const SilcID *source_id, ...);
1130
1131 /****f* silccore/SilcPacketAPI/silc_packet_wait_uninit
1132  *
1133  * SYNOPSIS
1134  *
1135  *    void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream);
1136  *
1137  * DESCRIPTION
1138  *
1139  *    Uninitializes the waiting context.  This may be called also from
1140  *    another thread while other thread is waiting for packets.  This will
1141  *    inform the waiting thread to stop waiting.
1142  *
1143  ***/
1144 void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream);
1145
1146 /****f* silccore/SilcPacketAPI/silc_packet_wait
1147  *
1148  * SYNOPSIS
1149  *
1150  *    int silc_packet_wait(void *waiter, int timeout,
1151  *                         SilcPacket *return_packet)
1152  *
1153  * DESCRIPTION
1154  *
1155  *    A special function that can be used to wait for a packet to arrive.
1156  *    This function will block the calling process or thread until either
1157  *    a packet is received into the `return_packet' pointer or the specified
1158  *    timeout value `timeout', which is in milliseconds, will expire.  If
1159  *    the timeout is 0, no timeout exist.  Before calling this function the
1160  *    silc_packet_wait_init must be called.  The caller is responsible for
1161  *    freeing the returned packet with silc_packet_free.
1162  *
1163  *    This function can be used for example from a thread that wants to
1164  *    block until SILC packet has been received.
1165  *
1166  *    Returns 1 when packet was received, 0 if timeout occurred and -1 if
1167  *    error occurred.
1168  *
1169  * EXAMPLE
1170  *
1171  *    static int foo_read_data(FooContext c)
1172  *    {
1173  *      SilcPacket packet;
1174  *      void *waiter;
1175  *      ...
1176  *
1177  *      // Will wait for private message packets
1178  *      if (c->initialized == FALSE) {
1179  *        waiter = silc_packet_wait_init(stream,
1180  *                                       SILC_PACKET_PRIVATE_MESSAGE, -1);
1181  *        c->initialized = TRUE;
1182  *      }
1183  *
1184  *      ...
1185  *      // Wait here until private message packet is received
1186  *      if ((silc_packet_wait(waiter, 0, &packet)) < 0)
1187  *        return -1;
1188  *
1189  *      ... process packet ...
1190  *
1191  *      return 1;
1192  *    }
1193  *
1194  ***/
1195 int silc_packet_wait(void *waiter, int timeout, SilcPacket *return_packet);
1196
1197 /****f* silccore/SilcPacketAPI/silc_packet_free
1198  *
1199  * SYNOPSIS
1200  *
1201  *    void silc_packet_free(SilcPacket packet);
1202  *
1203  * DESCRIPTION
1204  *
1205  *    This function is used to free the SilcPacket pointer that application
1206  *    receives in the SilcPacketReceive callback.  Application must free
1207  *    the packet if it takes it in to processing.
1208  *
1209  ***/
1210 void silc_packet_free(SilcPacket packet);
1211
1212 #endif /* SILCPACKET_H */