Added silc_packet_stream_set_stream.
[silc.git] / lib / silccore / silcpacket.h
1 /*
2
3   silcpacket.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2006 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/Packet Protocol 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
89 #define SILC_PACKET_PRIVATE              200     /* Private range start  */
90 #define SILC_PACKET_MAX                  255     /* RESERVED */
91
92 #define SILC_PACKET_NONE                 0       /* RESERVED */
93 #define SILC_PACKET_ANY                  0
94 /***/
95
96 /****d* silccore/SilcPacketAPI/SilcPacketFlags
97  *
98  * NAME
99  *
100  *    typedef SilcUInt8 SilcPacketFlags;
101  *
102  * DESCRIPTION
103  *
104  *    SILC packet flags type definition and all the packet flags.
105  *
106  * SOURCE
107  */
108 typedef SilcUInt8 SilcPacketFlags;
109
110 /* All defined packet flags */
111 #define SILC_PACKET_FLAG_NONE             0x00    /* No flags */
112 #define SILC_PACKET_FLAG_PRIVMSG_KEY      0x01    /* Private message key */
113 #define SILC_PACKET_FLAG_LIST             0x02    /* Packet is a list */
114 #define SILC_PACKET_FLAG_BROADCAST        0x04    /* Packet is a broadcast */
115 #define SILC_PACKET_FLAG_COMPRESSED       0x08    /* Payload is compressed */
116
117 /* Impelemntation specific flags */
118 #define SILC_PACKET_FLAG_LONG_PAD         0x10    /* 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 list pointer `next' can be used by the application to put the
164  *    packet context in a list during processing, if needed.
165  *
166  * SOURCE
167  */
168 typedef struct SilcPacketStruct {
169   struct SilcPacketStruct *next;     /* List pointer, application may set */
170   SilcPacketStream stream;           /* Packet stream this packet is from */
171   SilcBufferStruct buffer;           /* Packet data payload */
172   unsigned char *src_id;             /* Source ID */
173   unsigned char *dst_id;             /* Destination ID */
174   unsigned int src_id_len  : 6;      /* Source ID length */
175   unsigned int src_id_type : 2;      /* Source ID type */
176   unsigned int dst_id_len  : 6;      /* Destination ID length */
177   unsigned int dst_id_type : 2;      /* Destination ID type */
178   SilcPacketType type;               /* Packet type */
179   SilcPacketFlags flags;             /* Packet flags */
180 } *SilcPacket;
181 /***/
182
183 /****d* silcutil/SilcPacketAPI/SilcPacketError
184  *
185  * NAME
186  *
187  *    typedef enum { ... } SilcPacketError
188  *
189  * DESCRIPTION
190  *
191  *    Packet errors.  This is returned in the error callback.  If application
192  *    needs the actual lower level stream error, it needs to retrieve it
193  *    from the actual stream.  It can retrieve the underlaying stream from
194  *    the packet stream by calling silc_packet_stream_get_stream function.
195  *
196  * SOURCE
197  */
198 typedef enum {
199   SILC_PACKET_ERR_READ,                  /* Error while reading */
200   SILC_PACKET_ERR_WRITE,                 /* Error while writing */
201   SILC_PACKET_ERR_MAC_FAILED,            /* Packet MAC check failed */
202   SILC_PACKET_ERR_DECRYPTION_FAILED,     /* Packet decryption failed */
203   SILC_PACKET_ERR_UNKNOWN_SID,           /* Unknown SID (with IV included) */
204   SILC_PACKET_ERR_MALFORMED,             /* Packet is malformed */
205   SILC_PACKET_ERR_NO_MEMORY,             /* System out of memory */
206 } SilcPacketError;
207 /***/
208
209 /****f* silccore/SilcPacketAPI/SilcPacketReceiveCb
210  *
211  * SYNOPSIS
212  *
213  *    typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
214  *                                            SilcPacketStream stream,
215  *                                            SilcPacket packet,
216  *                                            void *callback_context,
217  *                                            void *stream_context);
218  *
219  * DESCRIPTION
220  *
221  *    The packet receive callback is called by the packet engine when a new
222  *    SILC Packet has arrived.  The application must free the returned
223  *    SilcPacket with silc_packet_free if it takes the packet in for
224  *    processing.  This callback is set in the SilcPacketCallbacks structure.
225  *    The `callback_context' is the context set as argument in the
226  *    silc_packet_engine_start function.  The `stream_context' is stream
227  *    specific context that was set by calling silc_packet_set_context.
228  *
229  *    If the application takes the received packet `packet' into processing
230  *    TRUE must be returned.  If FALSE is returned the packet engine will
231  *    pass the packet to other packet processor, if one has been linked
232  *    to the stream with silc_packet_stream_link function.  If no extra
233  *    processor is linked the packet is dropped.
234  *
235  * EXAMPLE
236  *
237  *    SilcBool
238  *    silc_foo_packet_receive_cb(SilcPacketEngine engine,
239  *                               SilcPacketStream stream, SilcPacket packet,
240  *                               void *callback_context, void *stream_context)
241  *    {
242  *      Application ctx = callback_context;
243  *
244  *      // If we're not up yet, let's not process the packet
245  *      if (ctx->initialized == FALSE)
246  *        return FALSE;
247  *
248  *      // Process the incoming packet...
249  *      ...
250  *
251  *      // It's our packet now, no one else will get it
252  *      return TRUE;
253  *    }
254  *
255  ***/
256 typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
257                                         SilcPacketStream stream,
258                                         SilcPacket packet,
259                                         void *callback_context,
260                                         void *stream_context);
261
262 /****f* silccore/SilcPacketAPI/SilcPacketEosCb
263  *
264  * SYNOPSIS
265  *
266  *    typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
267  *                                    SilcPacketStream stream,
268  *                                    void *callback_context,
269  *                                    void *stream_context);
270  *
271  * DESCRIPTION
272  *
273  *    The End Of Stream (EOS) callback, that is called by the packet engine
274  *    when the underlaying stream has ended.  No more data can be sent to
275  *    the stream or read from it.  The `stream' must be destroyed by
276  *    calling the silc_packet_stream_destroy.  This callback is set in the
277  *    SilcPacketCallbacks structure.
278  *
279  ***/
280 typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
281                                 SilcPacketStream stream,
282                                 void *callback_context,
283                                 void *stream_context);
284
285 /****f* silccore/SilcPacketAPI/SilcPacketErrorCb
286  *
287  * SYNOPSIS
288  *
289  *    typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
290  *                                      SilcPacketStream stream,
291  *                                      SilcPacketError error,
292  *                                      void *callback_context,
293  *                                      void *stream_context);
294  *
295  * DESCRIPTION
296  *
297  *    The error callback that is called by the packet engine if an error
298  *    occurs.  The `error' will indicate the error.  This callback is set
299  *    in the SilcPacketCallbacks structure.
300  *
301  ***/
302 typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
303                                   SilcPacketStream stream,
304                                   SilcPacketError error,
305                                   void *callback_context,
306                                   void *stream_context);
307
308 /****s* silccore/SilcPacketAPI/SilcPacketStream
309  *
310  * NAME
311  *
312  *    typedef struct SilcPacketStreamStruct *SilcPacketStream;
313  *
314  * DESCRIPTION
315  *
316  *    This structure is sent as argument to the silc_packet_engine_start
317  *    function to set the callback functions for the packet engine.  The
318  *    packet engine will call the callbacks when necessary.  Application
319  *    must always be provided for the packet engine.
320  *
321  * SOURCE
322  */
323 typedef struct {
324   SilcPacketReceiveCb packet_receive;    /* Called when packet is received */
325   SilcPacketEosCb eos;                   /* Called on end of stream */
326   SilcPacketErrorCb error;               /* Called on an error */
327 } SilcPacketCallbacks;
328 /***/
329
330 /* Prototypes */
331
332 /****f* silccore/SilcPacketAPI/silc_packet_engine_start
333  *
334  * SYNOPSIS
335  *
336  *    SilcPacketEngine
337  *    silc_packet_engine_start(SilcRng rng, SilcBool router,
338  *                             SilcPacketCallbacks *callbacks,
339  *                             void *callback_context);
340  *
341  * DESCRIPTION
342  *
343  *    Create new packet engine for processing incoming and outgoing packets.
344  *    If `router' is  TRUE then the application is considered to be router
345  *    server, and certain packets are handled differently.  Client and normal
346  *    server must set it to FALSE.  The `callbacks' is a SilcPacketCallbacks
347  *    structure provided by the caller which includes the callbacks that is
348  *    called when for example packet is received, or end of stream is called.
349  *
350  * NOTES
351  *
352  *    The packet engine is thread safe.  You can use one packet engine in
353  *    multi threaded application.
354  *
355  ***/
356 SilcPacketEngine
357 silc_packet_engine_start(SilcRng rng, SilcBool router,
358                          SilcPacketCallbacks *callbacks,
359                          void *callback_context);
360
361 /****f* silccore/SilcPacketAPI/silc_packet_engine_stop
362  *
363  * SYNOPSIS
364  *
365  *    void silc_packet_engine_stop(SilcPacketEngine engine);
366  *
367  * DESCRIPTION
368  *
369  *    Stop the packet engine.  No new packets can be sent or received after
370  *    calling this, and the `engine' will become invalid.
371  *
372  ***/
373 void silc_packet_engine_stop(SilcPacketEngine engine);
374
375 /****f* silccore/SilcPacketAPI/silc_packet_stream_create
376  *
377  * SYNOPSIS
378  *
379  *    SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
380  *                                               SilcSchedule schedule,
381  *                                               SilcStream stream);
382  *
383  * DESCRIPTION
384  *
385  *    Create new packet stream and use the `stream' as underlaying stream.
386  *    Usually the `stream' would be a socket stream, but it can be any
387  *    stream.  After this function returns, packets can immediately be
388  *    sent to or received from the stream.
389  *
390  * NOTES
391  *
392  *    SilcPacketStream cannot be used with silc_stream_* routines (such as
393  *    silc_stream_read and silc_stream_write) because of its special nature.
394  *    Use the silc_packet_send and the silc_packet_send_ext to send packets.
395  *    To read packets you will receive the packet receive callback from
396  *    packet engine.  Destroy the stream with silc_packet_stream_destroy.
397  *
398  *    The SilcPacketStream is thread safe.  Same context can be safely used
399  *    in multi threaded environment.
400  *
401  ***/
402 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
403                                            SilcSchedule schedule,
404                                            SilcStream stream);
405
406 /****f* silccore/SilcPacketAPI/silc_packet_stream_destroy
407  *
408  * SYNOPSIS
409  *
410  *    void silc_packet_stream_destroy(SilcPacketStream stream);
411  *
412  * DESCRIPTION
413  *
414  *    Destroy packet stream and the underlaying stream.  This will also
415  *    send end of stream to the underlaying stream.
416  *
417  ***/
418 void silc_packet_stream_destroy(SilcPacketStream stream);
419
420 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_router
421  *
422  * SYNOPSIS
423  *
424  *    void silc_packet_stream_set_router(SilcPacketStream stream);
425  *
426  * DESCRIPTION
427  *
428  *    When called sets the stream indicates by `stream' as SILC router
429  *    connection stream.  This causes that certain packets are handled
430  *    differently.  This must be called for router connection streams and
431  *    must not be called for any other stream.
432  *
433  ***/
434 void silc_packet_stream_set_router(SilcPacketStream stream);
435
436 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_iv_included
437  *
438  * SYNOPSIS
439  *
440  *    void silc_packet_stream_set_iv_included(SilcPacketStream stream);
441  *
442  * DESCRIPTION
443  *
444  *    Sets an IV Included property for the stream indicated by `stream'.
445  *    This means that the IV used in the encryption will be included in
446  *    the resulted ciphertext.  This makes it possible to send and receive
447  *    packets on unreliable network transport protocol, such as UDP/IP.
448  *    This must be called if the underlaying stream in the `stream' is UDP
449  *    stream.
450  *
451  *    When this is set to the stream the silc_packet_set_sid must be called
452  *    to set new Security ID.  The Security ID will be included with the IV
453  *    in the ciphertext.
454  *
455  ***/
456 void silc_packet_stream_set_iv_included(SilcPacketStream stream);
457
458 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_stream
459  *
460  * SYNOPSIS
461  *
462  *    void silc_packet_stream_set_stream(SilcPacketStream packet_stream,
463  *                                       SilcStream stream,
464  *                                       SilcSchedule schedule);
465  *
466  * DESCRIPTION
467  *
468  *    This function may be used to change the underlaying stream in the
469  *    packet stream indicated by `packet_stream'.  Note that the old
470  *    stream will not be used after calling this function.  The caller is
471  *    responsible destroying the old stream.
472  *
473  ***/
474 void silc_packet_stream_set_stream(SilcPacketStream packet_stream,
475                                    SilcStream stream,
476                                    SilcSchedule schedule);
477
478 /****f* silccore/SilcPacketAPI/silc_packet_stream_get_stream
479  *
480  * SYNOPSIS
481  *
482  *    SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
483  *
484  * DESCRIPTION
485  *
486  *    Returns the actual stream that is associated with the packet stream
487  *    `stream'.  The caller must not free the returned stream.  The returned
488  *    stream is the same pointer that was set for silc_packet_stream_create.
489  *    This function could be used for example when an error callback is
490  *    called by the packet engine to retrieve the actual lower level error
491  *    from the stream.
492  *
493  ***/
494 SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
495
496 /****f* silccore/SilcPacketAPI/silc_packet_stream_link
497  *
498  * SYNOPSIS
499  *
500  *    SilcBool silc_packet_stream_link(SilcPacketStream stream,
501  *                                     SilcPacketCallbacks *callbacks,
502  *                                     void *callback_context,
503  *                                     int priority, ...);
504  *
505  * DESCRIPTION
506  *
507  *    Links the packet processing callbacks indicated by `callbacks' into
508  *    the packet stream indicated by `stream' with priority `priority' for
509  *    the packet types given in the variable argument list.  This function
510  *    can be used to link to the packet stream for specific packet types
511  *    and receive them in the specified callbacks.  This way, a third party,
512  *    for example some library may attach itself into the packet stream
513  *    and receive and process certain packets.  The variable argument
514  *    list is ended with -1.  To link to receive all packets use
515  *    SILC_PACKET_ANY.
516  *
517  *    The default packet processing callbacks given as argument to the
518  *    silc_packet_engine_start has the priority 0.  Any priority higher
519  *    than 0 will then take precedence over the default callbacks.  Any
520  *    priority lower than 0 (negative value) will be processed after the
521  *    default callbacks.
522  *
523  *    Note that setting only the 'packet_receive' callback in the `callbacks'
524  *    is required.
525  *
526  * EXAMPLE
527  *
528  *    // Link to this packet stream, with high priority, for
529  *    // SILC_PACKET_CONNECTION_AUTH and SILC_PACKET_CONNECTION_AUTH_REQUEST
530  *    // packets. We don't care about other packets.
531  *    silc_packet_stream_link(stream, our_callbacks, our_context,
532  *                            1000000, SILC_PACKET_CONNECTION_AUTH,
533  *                            SILC_PACKET_CONNECTION_AUTH_REQUEST, -1);
534  *
535  ***/
536 SilcBool silc_packet_stream_link(SilcPacketStream stream,
537                                  SilcPacketCallbacks *callbacks,
538                                  void *callback_context,
539                                  int priority, ...);
540
541 /****f* silccore/SilcPacketAPI/silc_packet_stream_unlink
542  *
543  * SYNOPSIS
544  *
545  *    void silc_packet_stream_unlink(SilcPacketStream stream,
546  *                                   SilcPacketCallbacks *callbacks,
547  *                                   void *callback_context);
548  *
549  * DESCRIPTION
550  *
551  *    Unlinks the `callbacks' with `callback_context' from the packet stream
552  *    indicated by `stream'.  This function must be called for the callbacks
553  *    that was linked to `stream' when they are not needed anymore.
554  *
555  ***/
556 void silc_packet_stream_unlink(SilcPacketStream stream,
557                                SilcPacketCallbacks *callbacks,
558                                void *callback_context);
559
560 /****f* silccore/SilcPacketAPI/silc_packet_stream_ref
561  *
562  * SYNOPSIS
563  *
564  *    void silc_packet_stream_ref(SilcPacketStream stream);
565  *
566  * DESCRIPTION
567  *
568  *    Increase reference counter for the stream indicated by `stream'.  This
569  *    can be used to take a reference for the stream.  To unreference the
570  *    stream call silc_packet_stream_unref function.
571  *
572  ***/
573 void silc_packet_stream_ref(SilcPacketStream stream);
574
575 /****f* silccore/SilcPacketAPI/silc_packet_stream_unref
576  *
577  * SYNOPSIS
578  *
579  *    void silc_packet_stream_unref(SilcPacketStream stream);
580  *
581  * DESCRIPTION
582  *
583  *    Decrease reference counter for the stream indicated by `stream'.  If
584  *    the counter hits zero the stream will be destroyed automatically.
585  *
586  ***/
587 void silc_packet_stream_unref(SilcPacketStream stream);
588
589 /****f* silccore/SilcPacketAPI/silc_packet_get_engine
590  *
591  * SYNOPSIS
592  *
593  *    SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
594  *
595  * DESCRIPTION
596  *
597  *    Returns the packet engine from the `stream'.
598  *
599  ***/
600 SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
601
602 /****f* silccore/SilcPacketAPI/silc_packet_set_context
603  *
604  * SYNOPSIS
605  *
606  *    void silc_packet_set_context(SilcPacketStream stream,
607  *                                 void *stream_context);
608  *
609  * DESCRIPTION
610  *
611  *    Sets a stream specific context to the stream.  The context will
612  *    be delivered to all callback functions, and it can be retrieved by
613  *    calling silc_packet_get_context function as well.  Note that this is
614  *    separate packet stream specific context, and not the same as
615  *    `callback_context' in silc_packet_engine_start.  Both will be delivered
616  *    to the callbacks, and this context as the `stream_context' argument.
617  *
618  ***/
619 void silc_packet_set_context(SilcPacketStream stream, void *stream_context);
620
621 /****f* silccore/SilcPacketAPI/silc_packet_get_context
622  *
623  * SYNOPSIS
624  *
625  *    void *silc_packet_get_context(SilcPacketStream stream);
626  *
627  * DESCRIPTION
628  *
629  *    Returns the current set application context, or NULL if none is set.
630  *
631  ***/
632 void *silc_packet_get_context(SilcPacketStream stream);
633
634 /****f* silccore/SilcPacketAPI/silc_packet_set_ciphers
635  *
636  * SYNOPSIS
637  *
638  *    void silc_packet_set_ciphers(SilcPacketStream stream, SilcCipher send,
639  *                                 SilcCipher receive);
640  *
641  * DESCRIPTION
642  *
643  *    Set ciphers to be used to encrypt sent packets, and decrypt received
644  *    packets.  This can be called multiple times to change the ciphers.
645  *    In this case if old cipher is set it will be freed.  If ciphers are
646  *    not set packets will not be encrypted or decrypted.
647  *
648  ***/
649 void silc_packet_set_ciphers(SilcPacketStream stream, SilcCipher send,
650                              SilcCipher receive);
651
652 /****f* silccore/SilcPacketAPI/silc_packet_get_ciphers
653  *
654  * SYNOPSIS
655  *
656  *    SilcBool silc_packet_get_ciphers(SilcPacketStream stream,
657  *                                     SilcCipher *send,
658  *                                     SilcCipher *receive);
659  *
660  * DESCRIPTION
661  *
662  *    Returns the pointers of current ciphers from the `stream'.  Returns
663  *    FALSE if ciphers are not set.
664  *
665  ***/
666 SilcBool silc_packet_get_ciphers(SilcPacketStream stream, SilcCipher *send,
667                                  SilcCipher *receive);
668
669 /****f* silccore/SilcPacketAPI/silc_packet_set_hmacs
670  *
671  * SYNOPSIS
672  *
673  *    void silc_packet_set_hmacs(SilcPacketStream stream, SilcHmac send,
674  *                               SilcHmac receive);
675  *
676  * DESCRIPTION
677  *
678  *    Set HMACs to be used to create MACs for sent packets and to check
679  *    MAC for received packets.  This can be called multiple times to change
680  *    the HMACs.  In this case if old HMAC is set it will be freed.  If
681  *    HMACs are not set MACs are not generated or verified for packets.
682  *
683  ***/
684 void silc_packet_set_hmacs(SilcPacketStream stream, SilcHmac send,
685                            SilcHmac receive);
686
687 /****f* silccore/SilcPacketAPI/silc_packet_get_hmacs
688  *
689  * SYNOPSIS
690  *
691  *    SilcBool silc_packet_get_hmacs(SilcPacketStream stream, SilcHmac *send,
692  *                                   SilcHmac *receive);
693  *
694  * DESCRIPTION
695  *
696  *    Returns the pointers of current HMACs from the `stream'.  Returns
697  *    FALSE if HMACs are not set.
698  *
699  ***/
700 SilcBool silc_packet_get_hmacs(SilcPacketStream stream, SilcHmac *send,
701                                SilcHmac *receive);
702
703 /****f* silccore/SilcPacketAPI/silc_packet_set_ids
704  *
705  * SYNOPSIS
706  *
707  *    SilcBool silc_packet_set_ids(SilcPacketStream stream,
708  *                                 SilcIdType src_id_type, const void *src_id
709  *                                 SilcIdType dst_id_type, const void *dst_id);
710  *
711  * DESCRIPTION
712  *
713  *    Set the source ID and destinaion ID to be used when sending packets to
714  *    this packet stream.  The IDs to be used for a packet stream can be
715  *    overridden when sending packets.  However, if the IDs do not ever change
716  *    for the packet stream it is recommended they are set using this function.
717  *    In this case they can be omitted when sending packets to the stream.
718  *    It is also possible to set only source or destination ID.
719  *
720  ***/
721 SilcBool silc_packet_set_ids(SilcPacketStream stream,
722                              SilcIdType src_id_type, const void *src_id,
723                              SilcIdType dst_id_type, const void *dst_id);
724
725 /****f* silccore/SilcPacketAPI/silc_packet_set_sid
726  *
727  * SYNOPSIS
728  *
729  *    SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid);
730  *
731  * DESCRIPTION
732  *
733  *    Sets new Security ID to the packet stream indicated by `stream'.  This
734  *    is called only if the IV Included property was set to the stream
735  *    by calling silc_packet_stream_set_iv_included.  This function sets
736  *    new Security ID to the stream which is then included in the ciphertext
737  *    of a packet.  The `sid' must be 0 when it is set for the very first
738  *    time and must be increased by one after each rekey.  This function must
739  *    be called every time new keys are added to the stream after a rekey.
740  *
741  *    If this function is called when the IV Included property has not been
742  *    set to the stream the `sid' will be ignored.  Returns FALSE if the
743  *    IV Included has not been set, TRUE otherwise.
744  *
745  ***/
746 SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid);
747
748 /****f* silccore/SilcPacketAPI/silc_packet_send
749  *
750  * SYNOPSIS
751  *
752  *    SilcBool silc_packet_send(SilcPacketStream stream,
753  *                              SilcPacketType type, SilcPacketFlags flags,
754  *                              const unsigned char *data,
755  *                              SilcUInt32 data_len);
756  *
757  * DESCRIPTION
758  *
759  *    Send `data' of length of `data_len' to the packet stream indicated by
760  *    `stream'.  If ciphers and HMACs were set using silc_packet_set_ciphers
761  *    and silc_packet_set_hmacs the packet will be encrypted and MAC will be
762  *    computed for it.  If silc_packet_set_ids was used to set source and
763  *    destination ID for the packet stream those IDs are used in the
764  *    packet.  If IDs have not been set and they need to be provided then
765  *    silc_packet_send_ext function should be used.  Otherwise, the packet
766  *    will not have IDs set at all.  Returns FALSE if packet could not be
767  *    sent.
768  *
769  ***/
770 SilcBool silc_packet_send(SilcPacketStream stream,
771                           SilcPacketType type, SilcPacketFlags flags,
772                           const unsigned char *data, SilcUInt32 data_len);
773
774 /****f* silccore/SilcPacketAPI/silc_packet_send_ext
775  *
776  * SYNOPSIS
777  *
778  *    SilcBool
779  *    silc_packet_send_ext(SilcPacketStream stream,
780  *                         SilcPacketType type, SilcPacketFlags flags,
781  *                         SilcIdType src_id_type, void *srd_id,
782  *                         SilcIdType dst_id_type, void *dst_id,
783  *                         const unsigned char *data, SilcUInt32 data_len,
784  *                         SilcCipher cipher, SilcHmac hmac);
785  *
786  * DESCRIPTION
787  *
788  *    Same as silc_packet_send but with this function different sending
789  *    parameters can be sent as argument.  This function can be used to
790  *    set specific IDs, cipher and HMAC to be used in packet sending,
791  *    instead of the ones saved in the `stream'.  If any of the extra
792  *    pointers are NULL, default values set to the stream will apply.
793  *
794  ***/
795 SilcBool silc_packet_send_ext(SilcPacketStream stream,
796                               SilcPacketType type, SilcPacketFlags flags,
797                               SilcIdType src_id_type, void *src_id,
798                               SilcIdType dst_id_type, void *dst_id,
799                               const unsigned char *data, SilcUInt32 data_len,
800                               SilcCipher cipher, SilcHmac hmac);
801
802 /****f* silccore/SilcPacketAPI/silc_packet_wait
803  *
804  * SYNOPSIS
805  *
806  *    void *silc_packet_wait_init(SilcPacketStream stream, ...);
807  *
808  * DESCRIPTION
809  *
810  *    Initializes a packet waiter for the packet stream `stream' and
811  *    for the variable argument list of packet types.  The function
812  *    silc_packet_wait can be used to block the thread until a packet
813  *    has been received.  This function is used to initialize the waiting
814  *    and to give the list of packet types that caller wish to receive.
815  *    The variable argument list must end with -1.  To receive all
816  *    packets use SILC_PACKET_ANY.  Returns a context that must be given
817  *    to the silc_packet_wait function as argument.  Returns NULL on
818  *    error.  To uninitialize the waiting call silc_packet_wait_uninit.
819  *
820  * NOTES
821  *
822  *    Note that packets may be available immediately after calling this
823  *    function and they will be buffered, until silc_packet_wait is called.
824  *
825  * EXAMPLE
826  *
827  *    void *waiter;
828  *
829  *    // Will wait for private message packets
830  *    waiter = silc_packet_wait_init(stream,
831  *                                   SILC_PACKET_PRIVATE_MESSAGE, -1);
832  *
833  *
834  ***/
835 void *silc_packet_wait_init(SilcPacketStream stream, ...);
836
837 /****f* silccore/SilcPacketAPI/silc_packet_wait
838  *
839  * SYNOPSIS
840  *
841  *    void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream);
842  *
843  * DESCRIPTION
844  *
845  *    Uninitializes the waiting context.  This may be called also from
846  *    another thread while other thread is waiting for packets.  This will
847  *    inform the waiting thread to stop waiting.
848  *
849  ***/
850 void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream);
851
852 /****f* silccore/SilcPacketAPI/silc_packet_wait
853  *
854  * SYNOPSIS
855  *
856  *    int silc_packet_wait(void *waiter, int timeout,
857  *                         SilcPacket *return_packet)
858  *
859  * DESCRIPTION
860  *
861  *    A special function that can be used to wait for a packet to arrive.
862  *    This function will block the calling process or thread until either
863  *    a packet is received into the `return_packet' pointer or the specified
864  *    timeout value `timeout', which is in milliseconds, will expire.  If
865  *    the timeout is 0, no timeout exist.  Before calling this function the
866  *    silc_packet_wait_init must be called.  The caller is responsible for
867  *    freeing the returned packet with silc_packet_free.
868  *
869  *    This function can be used for example from a thread that wants to
870  *    block until SILC packet has been received.
871  *
872  *    Returns 1 when packet was received, 0 if timeout occurred and -1 if
873  *    error occurred.
874  *
875  * EXAMPLE
876  *
877  *    static int foo_read_data(FooContext c)
878  *    {
879  *      SilcPacket packet;
880  *      void *waiter;
881  *      ...
882  *
883  *      // Will wait for private message packets
884  *      if (c->initialized == FALSE) {
885  *        waiter = silc_packet_wait_init(stream,
886  *                                       SILC_PACKET_PRIVATE_MESSAGE, -1);
887  *        c->initialized = TRUE;
888  *      }
889  *
890  *      ...
891  *      // Wait here until private message packet is received
892  *      if ((silc_packet_wait(waiter, 0, &packet)) < 0)
893  *        return -1;
894  *
895  *      ... process packet ...
896  *
897  *      return 1;
898  *    }
899  *
900  ***/
901 int silc_packet_wait(void *waiter, int timeout, SilcPacket *return_packet);
902
903 /****f* silccore/SilcPacketAPI/silc_packet_free
904  *
905  * SYNOPSIS
906  *
907  *    void silc_packet_free(SilcPacket packet);
908  *
909  * DESCRIPTION
910  *
911  *    This function is used to free the SilcPacket pointer that application
912  *    receives in the SilcPacketReceive callback.  Application must free
913  *    the packet if it takes it in to processing.
914  *
915  ***/
916 void silc_packet_free(SilcPacket packet);
917
918 #endif /* SILCPACKET_H */