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