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