Added SILC Server library.
[silc.git] / lib / silccore / silcpacket.h
1 /*
2
3   silcpacket.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2005 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 engine,
26  * that can be used to receive packets from packet streams, and routines
27  * 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_MALFORMED,             /* Packet is malformed */
204   SILC_PACKET_ERR_NO_MEMORY,             /* System out of memory */
205 } SilcPacketError;
206 /***/
207
208 /****f* silccore/SilcPacketAPI/SilcPacketReceiveCb
209  *
210  * SYNOPSIS
211  *
212  *    typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
213  *                                            SilcPacketStream stream,
214  *                                            SilcPacket packet,
215  *                                            void *callback_context,
216  *                                            void *stream_context);
217  *
218  * DESCRIPTION
219  *
220  *    The packet receive callback is called by the packet engine when a new
221  *    SILC Packet has arrived.  The application must free the returned
222  *    SilcPacket with silc_packet_free if it takes the packet in for
223  *    processing.  This callback is set in the SilcPacketCallbacks structure.
224  *    The `callback_context' is the context set as argument in the
225  *    silc_packet_engine_start function.  The `stream_context' is stream
226  *    specific context that was set by calling silc_packet_set_context.
227  *
228  *    If the application takes the received packet `packet' into processing
229  *    TRUE must be returned.  If FALSE is returned the packet engine will
230  *    pass the packet to other packet processor, if one has been linked
231  *    to the stream with silc_packet_stream_link function.  If no extra
232  *    processor is linked the packet is dropped.
233  *
234  * EXAMPLE
235  *
236  *    SilcBool
237  *    silc_foo_packet_receive_cb(SilcPacketEngine engine,
238  *                               SilcPacketStream stream, SilcPacket packet,
239  *                               void *callback_context, void *stream_context)
240  *    {
241  *      Application ctx = callback_context;
242  *
243  *      // If we're not up yet, let's not process the packet
244  *      if (ctx->initialized == FALSE)
245  *        return FALSE;
246  *
247  *      // Process the incoming packet...
248  *      ...
249  *
250  *      // It's our packet now, no one else will get it
251  *      return TRUE;
252  *    }
253  *
254  ***/
255 typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
256                                         SilcPacketStream stream,
257                                         SilcPacket packet,
258                                         void *callback_context,
259                                         void *stream_context);
260
261 /****f* silccore/SilcPacketAPI/SilcPacketEosCb
262  *
263  * SYNOPSIS
264  *
265  *    typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
266  *                                    SilcPacketStream stream,
267  *                                    void *callback_context,
268  *                                    void *stream_context);
269  *
270  * DESCRIPTION
271  *
272  *    The End Of Stream (EOS) callback, that is called by the packet engine
273  *    when the underlaying stream has ended.  No more data can be sent to
274  *    the stream or read from it.  The `stream' must be destroyed by
275  *    calling the silc_packet_stream_destroy.  This callback is set in the
276  *    SilcPacketCallbacks structure.
277  *
278  ***/
279 typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
280                                 SilcPacketStream stream,
281                                 void *callback_context,
282                                 void *stream_context);
283
284 /****f* silccore/SilcPacketAPI/SilcPacketErrorCb
285  *
286  * SYNOPSIS
287  *
288  *    typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
289  *                                      SilcPacketStream stream,
290  *                                      SilcPacketError error,
291  *                                      void *callback_context,
292  *                                      void *stream_context);
293  *
294  * DESCRIPTION
295  *
296  *    The error callback that is called by the packet engine if an error
297  *    occurs.  The `error' will indicate the error.  This callback is set
298  *    in the SilcPacketCallbacks structure.
299  *
300  ***/
301 typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
302                                   SilcPacketStream stream,
303                                   SilcPacketError error,
304                                   void *callback_context,
305                                   void *stream_context);
306
307 /****s* silccore/SilcPacketAPI/SilcPacketStream
308  *
309  * NAME
310  *
311  *    typedef struct SilcPacketStreamStruct *SilcPacketStream;
312  *
313  * DESCRIPTION
314  *
315  *    This structure is sent as argument to the silc_packet_engine_start
316  *    function to set the callback functions for the packet engine.  The
317  *    packet engine will call the callbacks when necessary.  Application
318  *    must always be provided for the packet engine.
319  *
320  * SOURCE
321  */
322 typedef struct {
323   SilcPacketReceiveCb packet_receive;    /* Called when packet is received */
324   SilcPacketEosCb eos;                   /* Called on end of stream */
325   SilcPacketErrorCb error;               /* Called on an error */
326 } SilcPacketCallbacks;
327 /***/
328
329 /* Prototypes */
330
331 /****f* silccore/SilcPacketAPI/silc_packet_engine_start
332  *
333  * SYNOPSIS
334  *
335  *    SilcPacketEngine
336  *    silc_packet_engine_start(SilcRng rng, SilcBool router,
337  *                             SilcPacketCallbacks *callbacks,
338  *                             void *callback_context);
339  *
340  * DESCRIPTION
341  *
342  *    Create new packet engine for processing incoming and outgoing packets.
343  *    If `router' is  TRUE then the application is considered to be router
344  *    server, and certain packets are handled differently.  Client and normal
345  *    server must set it to FALSE.  The `callbacks' is a SilcPacketCallbacks
346  *    structure provided by the caller which includes the callbacks that is
347  *    called when for example packet is received, or end of stream is called.
348  *
349  * NOTES
350  *
351  *    The packet engine is thread safe.  You can use one packet engine in
352  *    multi threaded application.
353  *
354  ***/
355 SilcPacketEngine
356 silc_packet_engine_start(SilcRng rng, SilcBool router,
357                          SilcPacketCallbacks *callbacks,
358                          void *callback_context);
359
360 /****f* silccore/SilcPacketAPI/silc_packet_engine_stop
361  *
362  * SYNOPSIS
363  *
364  *    void silc_packet_engine_stop(SilcPacketEngine engine);
365  *
366  * DESCRIPTION
367  *
368  *    Stop the packet engine.  No new packets can be sent or received after
369  *    calling this, and the `engine' will become invalid.
370  *
371  ***/
372 void silc_packet_engine_stop(SilcPacketEngine engine);
373
374 /****f* silccore/SilcPacketAPI/silc_packet_stream_create
375  *
376  * SYNOPSIS
377  *
378  *    SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
379  *                                               SilcSchedule schedule,
380  *                                               SilcStream stream);
381  *
382  * DESCRIPTION
383  *
384  *    Create new packet stream and use the `stream' as underlaying stream.
385  *    Usually the `stream' would be a socket stream, but it can be any
386  *    stream.  After this function returns, packets can immediately be
387  *    sent to or received from the stream.
388  *
389  * NOTES
390  *
391  *    SilcPacketStream cannot be used with silc_stream_* routines (such as
392  *    silc_stream_read and silc_stream_write) because of its special nature.
393  *    Use the silc_packet_send and the silc_packet_send_ext to send packets.
394  *    To read packets you will receive the packet receive callback from
395  *    packet engine.  Destroy the stream with silc_packet_stream_destroy.
396  *
397  *    If you need to send only one type of SILC packets, then it is possible
398  *    to create SILC Packet Streamer with silc_packet_streamer_create, which
399  *    can be used with silc_stream_read and silc_stream_write.
400  *
401  *    The SilcPacketStream is thread safe.  Same context can be safely used
402  *    in multi threaded environment.
403  *
404  ***/
405 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
406                                            SilcSchedule schedule,
407                                            SilcStream stream);
408
409 /****f* silccore/SilcPacketAPI/silc_packet_stream_destroy
410  *
411  * SYNOPSIS
412  *
413  *    void silc_packet_stream_destroy(SilcPacketStream stream);
414  *
415  * DESCRIPTION
416  *
417  *    Destroy packet stream and the underlaying stream.  This will also
418  *    send end of stream to the underlaying stream.
419  *
420  ***/
421 void silc_packet_stream_destroy(SilcPacketStream stream);
422
423 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_router
424  *
425  * SYNOPSIS
426  *
427  *    void silc_packet_stream_set_router(SilcPacketStream stream);
428  *
429  * DESCRIPTION
430  *
431  *    When called sets the stream indicates by `stream' as SILC router
432  *    connection stream.  This causes that certain packets are handled
433  *    differently.  This must be called for router connection streams and
434  *    must not be called for any other stream.
435  *
436  ***/
437 void silc_packet_stream_set_router(SilcPacketStream stream);
438
439 /****f* silccore/SilcPacketAPI/silc_packet_streamer_create
440  *
441  * SYNOPSIS
442  *
443  *    SilcStream silc_packet_streamer_create(SilcPacketStream stream,
444  *                                           SilcPacketType packet_type,
445  *                                           SilcPacketFlags packet_flags);
446  *
447  * DESCRIPTION
448  *
449  *    This function can be used to create a SILC Packet Streamer that will
450  *    stream only one type of packet indicated by `packet_type' with packet
451  *    flags `packet_flags'.  This is special purpose function as usually
452  *    multiple different types of packets need to be sent in application.
453  *    There are cases however when creating streamer is simpler and more
454  *    efficient.  Cases such as file transfer stream or other data streams
455  *    that only send and receive one type of packet.  While it would be
456  *    possible to use silc_packet_send function to send packets it is
457  *    more efficient to create the SILC Packet Streamer and use the
458  *    silc_stream_read and silc_stream_write functions.
459  *
460  *    The encryption and decryption keys, and other information will be
461  *    retrieved from the packet stream indicated by `stream', which must be
462  *    created before creating the streamer.
463  *
464  * NOTES
465  *
466  *    The packet type that is assocated with the packet stream `stream' will
467  *    only be available through the returned SilcStream.  That packet type
468  *    will not be delivered to the packet callbacks.  To return to the
469  *    normal operation destroy the streamer silc_packet_streamer_destroy.
470  *
471  ***/
472 SilcStream silc_packet_streamer_create(SilcPacketStream stream,
473                                        SilcPacketType packet_type,
474                                        SilcPacketFlags packet_flags);
475
476 /****f* silccore/SilcPacketAPI/silc_packet_streamer_destroy
477  *
478  * SYNOPSIS
479  *
480  *    void silc_packet_streamer_destroy(SilcStream stream);
481  *
482  * DESCRIPTION
483  *
484  *    Destroys the created packet streamer.  Use this function only for
485  *    stream created with silc_packet_streamer_create.  The packet type
486  *    that was associated with the streamer can be received in the packet
487  *    callbacks after the streamer is destroyed.
488  *
489  ***/
490 void silc_packet_streamer_destroy(SilcStream stream);
491
492 /****f* silccore/SilcPacketAPI/silc_packet_stream_get_stream
493  *
494  * SYNOPSIS
495  *
496  *    SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
497  *
498  * DESCRIPTION
499  *
500  *    Returns the actual stream that is associated with the packet stream
501  *    `stream'.  The caller must not free the returned stream.  The returned
502  *    stream is the same pointer that was set for silc_packet_stream_create.
503  *    This function could be used for example when an error callback is
504  *    called by the packet engine to retrieve the actual lower level error
505  *    from the stream.
506  *
507  ***/
508 SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
509
510 /****f* silccore/SilcPacketAPI/silc_packet_stream_link
511  *
512  * SYNOPSIS
513  *
514  *    SilcBool silc_packet_stream_link(SilcPacketStream stream,
515  *                                     SilcPacketCallbacks *callbacks,
516  *                                     void *callback_context,
517  *                                     int priority, ...);
518  *
519  * DESCRIPTION
520  *
521  *    Links the packet processing callbacks indicated by `callbacks' into
522  *    the packet stream indicated by `stream' with priority `priority' for
523  *    the packet types given in the variable argument list.  This function
524  *    can be used to link to the packet stream for specific packet types
525  *    and receive them in the specified callbacks.  This way, a third party,
526  *    for example some library may attach itself into the packet stream
527  *    and receive and process certain packets.  The variable argument
528  *    list is ended with -1.  To link to receive all packets use
529  *    SILC_PACKET_ANY.
530  *
531  *    The default packet processing callbacks given as argument to the
532  *    silc_packet_engine_start has the priority 0.  Any priority higher
533  *    than 0 will then take precedence over the default callbacks.  Any
534  *    priority lower than 0 (negative value) will be processed after the
535  *    default callbacks.
536  *
537  *    Note that setting only the 'packet_receive' callback in the `callbacks'
538  *    is required.
539  *
540  * EXAMPLE
541  *
542  *    // Link to this packet stream, with high priority, for
543  *    // SILC_PACKET_CONNECTION_AUTH and SILC_PACKET_CONNECTION_AUTH_REQUEST
544  *    // packets. We don't care about other packets.
545  *    silc_packet_stream_link(stream, our_callbacks, our_context,
546  *                            1000000, SILC_PACKET_CONNECTION_AUTH,
547  *                            SILC_PACKET_CONNECTION_AUTH_REQUEST, -1);
548  *
549  ***/
550 SilcBool silc_packet_stream_link(SilcPacketStream stream,
551                                  SilcPacketCallbacks *callbacks,
552                                  void *callback_context,
553                                  int priority, ...);
554
555 /****f* silccore/SilcPacketAPI/silc_packet_stream_unlink
556  *
557  * SYNOPSIS
558  *
559  *    void silc_packet_stream_unlink(SilcPacketStream stream,
560  *                                   SilcPacketCallbacks *callbacks,
561  *                                   void *callback_context);
562  *
563  * DESCRIPTION
564  *
565  *    Unlinks the `callbacks' with `callback_context' from the packet stream
566  *    indicated by `stream'.  This function must be called for the callbacks
567  *    that was linked to `stream' when they are not needed anymore.
568  *
569  ***/
570 void silc_packet_stream_unlink(SilcPacketStream stream,
571                                SilcPacketCallbacks *callbacks,
572                                void *callback_context);
573
574 /****f* silccore/SilcPacketAPI/silc_packet_stream_ref
575  *
576  * SYNOPSIS
577  *
578  *    void silc_packet_stream_ref(SilcPacketStream stream);
579  *
580  * DESCRIPTION
581  *
582  *    Increase reference counter for the stream indicated by `stream'.  This
583  *    can be used to take a reference for the stream.  To unreference the
584  *    stream call silc_packet_stream_unref function.
585  *
586  ***/
587 void silc_packet_stream_ref(SilcPacketStream stream);
588
589 /****f* silccore/SilcPacketAPI/silc_packet_stream_unref
590  *
591  * SYNOPSIS
592  *
593  *    void silc_packet_stream_unref(SilcPacketStream stream);
594  *
595  * DESCRIPTION
596  *
597  *    Decrease reference counter for the stream indicated by `stream'.  If
598  *    the counter hits zero the stream will be destroyed automatically.
599  *
600  ***/
601 void silc_packet_stream_unref(SilcPacketStream stream);
602
603 /****f* silccore/SilcPacketAPI/silc_packet_get_engine
604  *
605  * SYNOPSIS
606  *
607  *    SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
608  *
609  * DESCRIPTION
610  *
611  *    Returns the packet engine from the `stream'.
612  *
613  ***/
614 SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
615
616 /****f* silccore/SilcPacketAPI/silc_packet_set_context
617  *
618  * SYNOPSIS
619  *
620  *    void silc_packet_set_context(SilcPacketStream stream,
621  *                                 void *stream_context);
622  *
623  * DESCRIPTION
624  *
625  *    Sets a stream specific context to the stream.  The context will
626  *    be delivered to all callback functions, and it can be retrieved by
627  *    calling silc_packet_get_context function as well.  Note that this is
628  *    separate packet stream specific context, and not the same as
629  *    `callback_context' in silc_packet_engine_start.  Both will be delivered
630  *    to the callbacks, and this context as the `stream_context' argument.
631  *
632  ***/
633 void silc_packet_set_context(SilcPacketStream stream, void *stream_context);
634
635 /****f* silccore/SilcPacketAPI/silc_packet_get_context
636  *
637  * SYNOPSIS
638  *
639  *    void *silc_packet_get_context(SilcPacketStream stream);
640  *
641  * DESCRIPTION
642  *
643  *    Returns the current set application context, or NULL if none is set.
644  *
645  ***/
646 void *silc_packet_get_context(SilcPacketStream stream);
647
648 /****f* silccore/SilcPacketAPI/silc_packet_set_ciphers
649  *
650  * SYNOPSIS
651  *
652  *    void silc_packet_set_ciphers(SilcPacketStream stream, SilcCipher send,
653  *                                 SilcCipher receive);
654  *
655  * DESCRIPTION
656  *
657  *    Set ciphers to be used to encrypt sent packets, and decrypt received
658  *    packets.  This can be called multiple times to change the ciphers.
659  *    In this case if old cipher is set it will be freed.  If ciphers are
660  *    not set packets will not be encrypted or decrypted.
661  *
662  ***/
663 void silc_packet_set_ciphers(SilcPacketStream stream, SilcCipher send,
664                              SilcCipher receive);
665
666 /****f* silccore/SilcPacketAPI/silc_packet_get_ciphers
667  *
668  * SYNOPSIS
669  *
670  *    SilcBool silc_packet_get_ciphers(SilcPacketStream stream,
671  *                                     SilcCipher *send,
672  *                                     SilcCipher *receive);
673  *
674  * DESCRIPTION
675  *
676  *    Returns the pointers of current ciphers from the `stream'.  Returns
677  *    FALSE if ciphers are not set.
678  *
679  ***/
680 SilcBool silc_packet_get_ciphers(SilcPacketStream stream, SilcCipher *send,
681                                  SilcCipher *receive);
682
683 /****f* silccore/SilcPacketAPI/silc_packet_set_hmacs
684  *
685  * SYNOPSIS
686  *
687  *    void silc_packet_set_hmacs(SilcPacketStream stream, SilcHmac send,
688  *                               SilcHmac receive);
689  *
690  * DESCRIPTION
691  *
692  *    Set HMACs to be used to create MACs for sent packets and to check
693  *    MAC for received packets.  This can be called multiple times to change
694  *    the HMACs.  In this case if old HMAC is set it will be freed.  If
695  *    HMACs are not set MACs are not generated or verified for packets.
696  *
697  ***/
698 void silc_packet_set_hmacs(SilcPacketStream stream, SilcHmac send,
699                            SilcHmac receive);
700
701 /****f* silccore/SilcPacketAPI/silc_packet_get_hmacs
702  *
703  * SYNOPSIS
704  *
705  *    SilcBool silc_packet_get_hmacs(SilcPacketStream stream, SilcHmac *send,
706  *                                   SilcHmac *receive);
707  *
708  * DESCRIPTION
709  *
710  *    Returns the pointers of current HMACs from the `stream'.  Returns
711  *    FALSE if HMACs are not set.
712  *
713  ***/
714 SilcBool silc_packet_get_hmacs(SilcPacketStream stream, SilcHmac *send,
715                                SilcHmac *receive);
716
717 /****f* silccore/SilcPacketAPI/silc_packet_set_ids
718  *
719  * SYNOPSIS
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  * DESCRIPTION
726  *
727  *    Set the source ID and destinaion ID to be used when sending packets to
728  *    this packet stream.  The IDs to be used for a packet stream can be
729  *    overridden when sending packets.  However, if the IDs do not ever change
730  *    for the packet stream it is recommended they are set using this function.
731  *    In this case they can be omitted when sending packets to the stream.
732  *    It is also possible to set only source or destination ID.
733  *
734  ***/
735 SilcBool silc_packet_set_ids(SilcPacketStream stream,
736                              SilcIdType src_id_type, const void *src_id,
737                              SilcIdType dst_id_type, const void *dst_id);
738
739 /****f* silccore/SilcPacketAPI/silc_packet_send
740  *
741  * SYNOPSIS
742  *
743  *    SilcBool silc_packet_send(SilcPacketStream stream,
744  *                              SilcPacketType type, SilcPacketFlags flags,
745  *                              const unsigned char *data,
746  *                              SilcUInt32 data_len);
747  *
748  * DESCRIPTION
749  *
750  *    Send `data' of length of `data_len' to the packet stream indicated by
751  *    `stream'.  If ciphers and HMACs were set using silc_packet_set_ciphers
752  *    and silc_packet_set_hmacs the packet will be encrypted and MAC will be
753  *    computed for it.  If silc_packet_set_ids was used to set source and
754  *    destination ID for the packet stream those IDs are used in the
755  *    packet.  If IDs have not been set and they need to be provided then
756  *    silc_packet_send_ext function should be used.  Otherwise, the packet
757  *    will not have IDs set at all.  Returns FALSE if packet could not be
758  *    sent.
759  *
760  ***/
761 SilcBool silc_packet_send(SilcPacketStream stream,
762                           SilcPacketType type, SilcPacketFlags flags,
763                           const unsigned char *data, SilcUInt32 data_len);
764
765 /****f* silccore/SilcPacketAPI/silc_packet_send_ext
766  *
767  * SYNOPSIS
768  *
769  *    SilcBool
770  *    silc_packet_send_ext(SilcPacketStream stream,
771  *                         SilcPacketType type, SilcPacketFlags flags,
772  *                         SilcIdType src_id_type, void *srd_id,
773  *                         SilcIdType dst_id_type, void *dst_id,
774  *                         const unsigned char *data, SilcUInt32 data_len,
775  *                         SilcCipher cipher, SilcHmac hmac);
776  *
777  * DESCRIPTION
778  *
779  *    Same as silc_packet_send but with this function different sending
780  *    parameters can be sent as argument.  This function can be used to
781  *    set specific IDs, cipher and HMAC to be used in packet sending,
782  *    instead of the ones saved in the `stream'.
783  *
784  ***/
785 SilcBool silc_packet_send_ext(SilcPacketStream stream,
786                               SilcPacketType type, SilcPacketFlags flags,
787                               SilcIdType src_id_type, void *src_id,
788                               SilcIdType dst_id_type, void *dst_id,
789                               const unsigned char *data, SilcUInt32 data_len,
790                               SilcCipher cipher, SilcHmac hmac);
791
792 /****f* silccore/SilcPacketAPI/silc_packet_free
793  *
794  * SYNOPSIS
795  *
796  *    void silc_packet_free(SilcPacket packet);
797  *
798  * DESCRIPTION
799  *
800  *    This function is used to free the SilcPacket pointer that application
801  *    receives in the SilcPacketReceive callback.  Application must free
802  *    the packet if it takes it in to processing.
803  *
804  ***/
805 void silc_packet_free(SilcPacket packet);
806
807 #endif /* SILCPACKET_H */