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