5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2007 Pekka Riikonen
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.
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.
20 /****h* silccore/SILC Packet Engine Interface
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.
29 * The packet engine and packet stream are thread safe. They can be safely
30 * used in multi threaded environment.
37 /* XXX many of these could go to silcpacket_i.h */
39 /* Maximum packet length */
40 #define SILC_PACKET_MAX_LEN 0xffff
42 /* Maximum length of ID */
43 #define SILC_PACKET_MAX_ID_LEN 28
45 /****d* silccore/SilcPacketAPI/SilcPacketType
49 * typedef SilcUInt8 SilcPacketType;
53 * SILC packet type definition and all the packet types.
57 typedef SilcUInt8 SilcPacketType;
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 */
90 #define SILC_PACKET_PRIVATE 200 /* Private range start */
91 #define SILC_PACKET_MAX 255 /* RESERVED */
93 #define SILC_PACKET_NONE 0 /* RESERVED */
94 #define SILC_PACKET_ANY 0
97 /****d* silccore/SilcPacketAPI/SilcPacketFlags
101 * typedef SilcUInt8 SilcPacketFlags;
105 * SILC packet flags type definition and all the packet flags.
109 typedef SilcUInt8 SilcPacketFlags;
111 /* All defined packet flags */
112 #define SILC_PACKET_FLAG_NONE 0x00 /* No flags */
113 #define SILC_PACKET_FLAG_PRIVMSG_KEY 0x01 /* Private message key */
114 #define SILC_PACKET_FLAG_LIST 0x02 /* Packet is a list */
115 #define SILC_PACKET_FLAG_BROADCAST 0x04 /* Packet is a broadcast */
116 #define SILC_PACKET_FLAG_COMPRESSED 0x08 /* Payload is compressed */
117 #define SILC_PACKET_FLAG_ACK 0x10 /* Acknowledge packet */
119 /* Impelemntation specific flags */
120 #define SILC_PACKET_FLAG_LONG_PAD 0x12 /* Use maximum padding */
123 /****s* silccore/SilcPacketAPI/SilcPacketEngine
127 * typedef struct SilcPacketEngineStruct *SilcPacketEngine;
131 * The packet engine context, allocated by silc_packet_engine_start.
132 * The engine is destroyed with silc_packet_engine_stop.
135 typedef struct SilcPacketEngineStruct *SilcPacketEngine;
137 /****s* silccore/SilcPacketAPI/SilcPacketStream
141 * typedef struct SilcPacketStreamStruct *SilcPacketStream;
145 * The packet stream context, allocated by silc_packet_stream_create.
146 * The stream is destroyed with silc_packet_stream_destroy.
149 typedef struct SilcPacketStreamStruct *SilcPacketStream;
151 /****s* silccore/SilcPacketAPI/SilcPacket
155 * typedef struct SilcPacketStruct *SilcPacket;
159 * The SilcPacket is returned by the packet engine in the SilcPacketReceive
160 * callback. The application can parse the data payload from the
161 * SilcPacket. Also packet type, flags, and sender and destination
162 * IDs are available. The application must free the packet with the
163 * silc_packet_free function if it takes it in for processing.
165 * The `buffer' field contains the parsed packet payload and the start
166 * of the data area will point to the start of the packet payload.
168 * The list pointer `next' can be used by the application to put the
169 * packet context in a list during processing, if needed.
173 typedef struct SilcPacketStruct {
174 struct SilcPacketStruct *next; /* List pointer, application may set */
175 SilcPacketStream stream; /* Packet stream this packet is from */
176 SilcBufferStruct buffer; /* Packet data payload */
177 unsigned char *src_id; /* Source ID */
178 unsigned char *dst_id; /* Destination ID */
179 unsigned int src_id_len : 6; /* Source ID length */
180 unsigned int src_id_type : 2; /* Source ID type */
181 unsigned int dst_id_len : 6; /* Destination ID length */
182 unsigned int dst_id_type : 2; /* Destination ID type */
183 SilcPacketType type; /* Packet type */
184 SilcPacketFlags flags; /* Packet flags */
188 /****d* silcutil/SilcPacketAPI/SilcPacketError
192 * typedef enum { ... } SilcPacketError
196 * Packet errors. This is returned in the error callback. If application
197 * needs the actual lower level stream error, it needs to retrieve it
198 * from the actual stream. It can retrieve the underlaying stream from
199 * the packet stream by calling silc_packet_stream_get_stream function.
204 SILC_PACKET_ERR_READ, /* Error while reading */
205 SILC_PACKET_ERR_WRITE, /* Error while writing */
206 SILC_PACKET_ERR_MAC_FAILED, /* Packet MAC check failed */
207 SILC_PACKET_ERR_DECRYPTION_FAILED, /* Packet decryption failed */
208 SILC_PACKET_ERR_UNKNOWN_SID, /* Unknown SID (with IV included) */
209 SILC_PACKET_ERR_MALFORMED, /* Packet is malformed */
210 SILC_PACKET_ERR_NO_MEMORY, /* System out of memory */
214 /****f* silccore/SilcPacketAPI/SilcPacketReceiveCb
218 * typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
219 * SilcPacketStream stream,
221 * void *callback_context,
222 * void *stream_context);
226 * The packet receive callback is called by the packet engine when a new
227 * SILC Packet has arrived. The application must free the returned
228 * SilcPacket with silc_packet_free if it takes the packet in for
229 * processing. This callback is set in the SilcPacketCallbacks structure.
230 * The `callback_context' is the context set as argument in the
231 * silc_packet_engine_start function. The `stream_context' is stream
232 * specific context that was set by calling silc_packet_set_context.
234 * If the application takes the received packet `packet' into processing
235 * TRUE must be returned. If FALSE is returned the packet engine will
236 * pass the packet to other packet processor, if one has been linked
237 * to the stream with silc_packet_stream_link function. If no extra
238 * processor is linked the packet is dropped.
243 * silc_foo_packet_receive_cb(SilcPacketEngine engine,
244 * SilcPacketStream stream, SilcPacket packet,
245 * void *callback_context, void *stream_context)
247 * Application ctx = callback_context;
249 * // If we're not up yet, let's not process the packet
250 * if (ctx->initialized == FALSE)
253 * // Process the incoming packet...
256 * // It's our packet now, no one else will get it
261 typedef SilcBool (*SilcPacketReceiveCb)(SilcPacketEngine engine,
262 SilcPacketStream stream,
264 void *callback_context,
265 void *stream_context);
267 /****f* silccore/SilcPacketAPI/SilcPacketEosCb
271 * typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
272 * SilcPacketStream stream,
273 * void *callback_context,
274 * void *stream_context);
278 * The End Of Stream (EOS) callback, that is called by the packet engine
279 * when the underlaying stream has ended. No more data can be sent to
280 * the stream or read from it. The `stream' must be destroyed by
281 * calling the silc_packet_stream_destroy. This callback is set in the
282 * SilcPacketCallbacks structure.
285 typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
286 SilcPacketStream stream,
287 void *callback_context,
288 void *stream_context);
290 /****f* silccore/SilcPacketAPI/SilcPacketErrorCb
294 * typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
295 * SilcPacketStream stream,
296 * SilcPacketError error,
297 * void *callback_context,
298 * void *stream_context);
302 * The error callback that is called by the packet engine if an error
303 * occurs. The `error' will indicate the error. This callback is set
304 * in the SilcPacketCallbacks structure.
307 typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
308 SilcPacketStream stream,
309 SilcPacketError error,
310 void *callback_context,
311 void *stream_context);
313 /****s* silccore/SilcPacketAPI/SilcPacketCallbacks
317 * typedef struct { ... } *SilcPacketCallbacks;
321 * This structure is sent as argument to the silc_packet_engine_start
322 * function to set the callback functions for the packet engine. The
323 * packet engine will call the callbacks when necessary. Application
324 * must always be provided for the packet engine.
329 SilcPacketReceiveCb packet_receive; /* Called when packet is received */
330 SilcPacketEosCb eos; /* Called on end of stream */
331 SilcPacketErrorCb error; /* Called on an error */
332 } SilcPacketCallbacks;
337 /****f* silccore/SilcPacketAPI/silc_packet_engine_start
342 * silc_packet_engine_start(SilcRng rng, SilcBool router,
343 * SilcPacketCallbacks *callbacks,
344 * void *callback_context);
348 * Create new packet engine for processing incoming and outgoing packets.
349 * If `router' is TRUE then the application is considered to be router
350 * server, and certain packets are handled differently. Client and normal
351 * server must set it to FALSE. The `callbacks' is a SilcPacketCallbacks
352 * structure provided by the caller which includes the callbacks that is
353 * called when for example packet is received, or end of stream is called.
357 * The packet engine is thread safe. You can use one packet engine in
358 * multi threaded application.
362 silc_packet_engine_start(SilcRng rng, SilcBool router,
363 SilcPacketCallbacks *callbacks,
364 void *callback_context);
366 /****f* silccore/SilcPacketAPI/silc_packet_engine_stop
370 * void silc_packet_engine_stop(SilcPacketEngine engine);
374 * Stop the packet engine. No new packets can be sent or received after
375 * calling this, and the `engine' will become invalid.
378 void silc_packet_engine_stop(SilcPacketEngine engine);
380 /****f* silccore/SilcPacketAPI/silc_packet_engine_get_streams
384 * SilcDList silc_packet_engine_get_streams(SilcPacketEngine engine);
388 * Returns list of packet streams added to the packet engine. The caller
389 * must free the list with silc_dlist_uninit.
392 SilcDList silc_packet_engine_get_streams(SilcPacketEngine engine);
394 /****f* silccore/SilcPacketAPI/silc_packet_stream_create
398 * SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
399 * SilcSchedule schedule,
400 * SilcStream stream);
404 * Create new packet stream and use the `stream' as underlaying stream.
405 * Usually the `stream' would be a socket stream, but it can be any
406 * stream. After this function returns, packets can immediately be
407 * sent to and received from the stream.
411 * SilcPacketStream cannot be used with silc_stream_* routines (such as
412 * silc_stream_read and silc_stream_write) because of its special nature.
413 * Use the silc_packet_send and the silc_packet_send_ext to send packets.
414 * To read packets you will receive the packet receive callback from
415 * packet engine. Destroy the stream with silc_packet_stream_destroy.
417 * The SilcPacketStream is thread safe. Same context can be safely used
418 * in multi threaded environment.
421 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
422 SilcSchedule schedule,
425 /****f* silccore/SilcPacketAPI/silc_packet_stream_add_remote
429 * SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
430 * const char *remote_ip,
431 * SilcUInt16 remote_port,
432 * SilcPacket packet);
436 * This function is used to add remote receivers in packet stream `stream'
437 * that has UDP/IP socket stream as the underlaying stream. This function
438 * cannot be used with other type of streams. This returns new packet
439 * stream context that can be used to send to and receive packets from
440 * the specified remote IP and remote port, or NULL on error. The `stream'
441 * is the actual stream that is used to send and receive the data.
443 * When the parent `stream' receives packets from remote IP address
444 * and port that does not have its own remote packet stream, it returns
445 * the packet to the packet callback set for `stream'. The sender's
446 * IP address and port can then be retrieved by using the
447 * silc_packet_get_sender function and to create new packet stream by
448 * calling this function. After that, all packets from that IP address
449 * and port will be received by the new packet stream.
451 * If the `packet' is non-NULL it will be injected into the new packet
452 * stream as soon as the scheduler associated with `stream' schedules
453 * new tasks. It can be used to inject an incoming packet to the stream.
455 * This interface is for connectionless UDP streams. If it is possible
456 * to create connected stream it should be done for performance reasons.
460 * // Create parent packet stream, it can receive packets from anywhere
461 * listener = silc_net_udp_connect("0.0.0.0", 500, NULL, 0, schedule);
462 * parent = silc_packet_stream_create(engine, schedule, listener);
465 * // Received a packet to the parent stream, get the sender information.
466 * silc_packet_get_sender(packet, &ip, &port);
468 * // Create new packet stream for this remote location.
469 * remote = silc_packet_stream_add_remote(parent, ip, port, packet);
472 SilcPacketStream silc_packet_stream_add_remote(SilcPacketStream stream,
473 const char *remote_ip,
474 SilcUInt16 remote_port,
477 /****f* silccore/SilcPacketAPI/silc_packet_stream_destroy
481 * void silc_packet_stream_destroy(SilcPacketStream stream);
485 * Destroy packet stream and the underlaying stream. This will also
486 * close and destroy the underlaying stream.
489 void silc_packet_stream_destroy(SilcPacketStream stream);
491 /****f* silccore/SilcPacketAPI/silc_packet_stream_is_valid
495 * SilcBool silc_packet_stream_is_valid(SilcPacketStream stream);
499 * Returns TRUE if the packet stream indicated by `stream' is valid and
500 * has not been disconnected or destroyed.
503 SilcBool silc_packet_stream_is_valid(SilcPacketStream stream);
505 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_router
509 * void silc_packet_stream_set_router(SilcPacketStream stream);
513 * When called sets the stream indicates by `stream' as SILC router
514 * connection stream. This causes that certain packets are handled
515 * differently. This must be called for router connection streams and
516 * must not be called for any other stream.
519 void silc_packet_stream_set_router(SilcPacketStream stream);
521 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_iv_included
525 * void silc_packet_stream_set_iv_included(SilcPacketStream stream);
529 * Sets an IV Included property for the stream indicated by `stream'.
530 * This means that the IV used in the encryption will be included in
531 * the resulted ciphertext. This makes it possible to send and receive
532 * packets on unreliable network transport protocol, such as UDP/IP.
533 * This must be called if the underlaying stream in the `stream' is UDP
536 * When this is set to the stream the silc_packet_set_sid must be called
537 * to set new Security ID. The Security ID will be included with the IV
541 void silc_packet_stream_set_iv_included(SilcPacketStream stream);
543 /****f* silccore/SilcPacketAPI/silc_packet_stream_set_stream
547 * void silc_packet_stream_set_stream(SilcPacketStream packet_stream,
548 * SilcStream stream);
552 * This function may be used to change the underlaying stream in the
553 * packet stream indicated by `packet_stream'. Note that the old
554 * stream will not be used after calling this function. The caller is
555 * responsible destroying the old stream. The `stream' will use
556 * the same scheduler as the `packet_stream'.
559 void silc_packet_stream_set_stream(SilcPacketStream packet_stream,
562 /****f* silccore/SilcPacketAPI/silc_packet_stream_get_stream
566 * SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
570 * Returns the actual stream that is associated with the packet stream
571 * `stream'. The caller must not free the returned stream. The returned
572 * stream is the same pointer that was set for silc_packet_stream_create.
573 * This function could be used for example when an error callback is
574 * called by the packet engine to retrieve the actual lower level error
578 SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
580 /****f* silccore/SilcPacketAPI/silc_packet_stream_link
584 * SilcBool silc_packet_stream_link(SilcPacketStream stream,
585 * SilcPacketCallbacks *callbacks,
586 * void *callback_context,
587 * int priority, ...);
591 * Links the packet processing callbacks indicated by `callbacks' into
592 * the packet stream indicated by `stream' with priority `priority' for
593 * the packet types given in the variable argument list. This function
594 * can be used to link to the packet stream for specific packet types
595 * and receive them in the specified callbacks. This way, a third party,
596 * for example some library may attach itself into the packet stream
597 * and receive and process certain packets. The variable argument
598 * list is ended with -1. To link to receive all packets use
601 * The default packet processing callbacks given as argument to the
602 * silc_packet_engine_start has the priority 0. Any priority higher
603 * than 0 will then take precedence over the default callbacks. Any
604 * priority lower than 0 (negative value) will be processed after the
607 * Note that setting only the 'packet_receive' callback in the `callbacks'
612 * // Link to this packet stream, with high priority, for
613 * // SILC_PACKET_CONNECTION_AUTH and SILC_PACKET_CONNECTION_AUTH_REQUEST
614 * // packets. We don't care about other packets.
615 * silc_packet_stream_link(stream, our_callbacks, our_context,
616 * 1000000, SILC_PACKET_CONNECTION_AUTH,
617 * SILC_PACKET_CONNECTION_AUTH_REQUEST, -1);
620 SilcBool silc_packet_stream_link(SilcPacketStream stream,
621 SilcPacketCallbacks *callbacks,
622 void *callback_context,
625 /****f* silccore/SilcPacketAPI/silc_packet_stream_unlink
629 * void silc_packet_stream_unlink(SilcPacketStream stream,
630 * SilcPacketCallbacks *callbacks,
631 * void *callback_context);
635 * Unlinks the `callbacks' with `callback_context' from the packet stream
636 * indicated by `stream'. This function must be called for the callbacks
637 * that was linked to `stream' when they are not needed anymore.
640 void silc_packet_stream_unlink(SilcPacketStream stream,
641 SilcPacketCallbacks *callbacks,
642 void *callback_context);
644 /****f* silccore/SilcPacketAPI/SilcPacketWrapCoder
648 * typedef SilcBool (*SilcPacketWrapCoder)(SilcStream stream,
649 * SilcStreamStatus status,
655 * The encoder/decoder callback for silc_packet_stream_wrap. If the
656 * `status' is SILC_STREAM_CAN_WRITE then additional data can be added
657 * to `buffer'. It is added before the data that is written with
658 * silc_stream_write. The silc_buffer_enlarge should be called to verify
659 * there is enough room in `buffer' before adding data to it. The `buffer'
662 * If the `status' is SILC_STREAM_CAN_READ then data from the `buffer'
663 * may be read before it is passed to readed when silc_stream_read is
664 * called. The `buffer' may be advanced also to hide data in it.
666 * This function returns FALSE in case of error.
669 typedef SilcBool (*SilcPacketWrapCoder)(SilcStream stream,
670 SilcStreamStatus status,
674 /****f* silccore/SilcPacketAPI/silc_packet_stream_wrap
678 * SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
679 * SilcPacketType type,
680 * SilcPacketFlags flags,
681 * SilcBool blocking_mode,
682 * SilcPacketWrapCoder coder,
687 * Wraps the packet stream indicated by `stream' into a SilcStream for
688 * the packet type indicated by `type' with packet flags indicated by
689 * `flags'. The returned SilcStream can be used to read and write the
690 * specified SILC packets with the specified packet flags, by calling
691 * silc_stream_read and silc_stream_write, respectively. The returned
692 * stream can be destroyed by calling silc_stream_destroy. It does not
693 * destroy the wrapped packet stream.
695 * If the `blocking_mode' mode is TRUE then the silc_stream_read and
696 * silc_stream_write may block the calling process or thread until SILC
697 * packet is read or written. If it is FALSE the stream is in non-blocking
698 * mode and the calls never block. The returned stream is thread-safe and
699 * packets may be read and written in multi-threaded environment.
701 * In non-blocking mode the silc_stream_set_notifier must be called before
702 * the returned stream can be used to read packets. The stream status
703 * SILC_STREAM_CAN_READ will be returned to the notifier callback to
704 * indicate that a packet is ready for reading. Calling silc_stream_read
705 * once returns one complete SILC packet data payload (which is of type of
708 * The `coder' is optional encoder/decoder callback which the packet engine
709 * will call if it is non-NULL. It can be used to encode additional data
710 * into each packet when silc_stream_write is called or decode data before
711 * it is passed to reader when silc_stream_read is called. The `context'
712 * is passed to `coder'.
714 * The returned SilcStream can be used as any normal stream and all
715 * SilcStream API functions may be used with the stream. This returns
719 SilcStream silc_packet_stream_wrap(SilcPacketStream stream,
721 SilcPacketFlags flags,
722 SilcBool blocking_mode,
723 SilcPacketWrapCoder coder,
726 /****f* silccore/SilcPacketAPI/silc_packet_stream_is_udp
730 * SilcBool silc_packet_stream_is_udp(SilcPacketStream stream);
734 * Returns TRUE if the packet stream indicated by `stream' is using
738 SilcBool silc_packet_stream_is_udp(SilcPacketStream stream);
740 /****f* silccore/SilcPacketAPI/silc_packet_get_sender
744 * SilcBool silc_packet_get_sender(SilcPacket packet,
745 * const char **sender_ip,
746 * SilcUInt16 *sender_port);
750 * Returns the packet sender's IP address and port from UDP packet
751 * indicated by `packet'. This can be called only from the packet
752 * callback to retrieve the information of the packet's sender. Returns
753 * FALSE if the information is not available.
756 SilcBool silc_packet_get_sender(SilcPacket packet,
757 const char **sender_ip,
758 SilcUInt16 *sender_port);
760 /****f* silccore/SilcPacketAPI/silc_packet_stream_ref
764 * void silc_packet_stream_ref(SilcPacketStream stream);
768 * Increase reference counter for the stream indicated by `stream'. This
769 * can be used to take a reference for the stream. To unreference the
770 * stream call silc_packet_stream_unref function.
773 void silc_packet_stream_ref(SilcPacketStream stream);
775 /****f* silccore/SilcPacketAPI/silc_packet_stream_unref
779 * void silc_packet_stream_unref(SilcPacketStream stream);
783 * Decrease reference counter for the stream indicated by `stream'. If
784 * the counter hits zero the stream will be destroyed automatically.
787 void silc_packet_stream_unref(SilcPacketStream stream);
789 /****f* silccore/SilcPacketAPI/silc_packet_get_engine
793 * SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
797 * Returns the packet engine from the `stream'.
800 SilcPacketEngine silc_packet_get_engine(SilcPacketStream stream);
802 /****f* silccore/SilcPacketAPI/silc_packet_set_context
806 * void silc_packet_set_context(SilcPacketStream stream,
807 * void *stream_context);
811 * Sets a stream specific context to the stream. The context will
812 * be delivered to all callback functions, and it can be retrieved by
813 * calling silc_packet_get_context function as well. Note that this is
814 * separate packet stream specific context, and not the same as
815 * `callback_context' in silc_packet_engine_start. Both will be delivered
816 * to the callbacks, and this context as the `stream_context' argument.
819 void silc_packet_set_context(SilcPacketStream stream, void *stream_context);
821 /****f* silccore/SilcPacketAPI/silc_packet_get_context
825 * void *silc_packet_get_context(SilcPacketStream stream);
829 * Returns the current set application context, or NULL if none is set.
832 void *silc_packet_get_context(SilcPacketStream stream);
834 /****f* silccore/SilcPacketAPI/silc_packet_set_keys
838 * void silc_packet_set_keys(SilcPacketStream stream, SilcCipher send_key,
839 * SilcCipher receive_key, SilcHmac send_hmac,
840 * SilcHmac receive_hmac, SilcBool rekey);
844 * Set ciphers and HMACs to be used to encrypt sent packets, and decrypt
845 * received packets. This can be called multiple times to change the
848 * If the `rekey' is TRUE this function will send SILC_PACKET_REKEY_DONE
849 * to the `stream' and will set the new keys. If it is FALSE the keys
850 * are changed but the packet is not changed.
852 * When changing keys the old cipher and HMACs will be freed. If the keys
853 * are not set at all, packets will not be encrypted or decrypted.
856 SilcBool silc_packet_set_keys(SilcPacketStream stream, SilcCipher send_key,
857 SilcCipher receive_key, SilcHmac send_hmac,
858 SilcHmac receive_hmac, SilcBool rekey);
860 /****f* silccore/SilcPacketAPI/silc_packet_get_keys
864 * SilcBool silc_packet_get_keys(SilcPacketStream stream,
865 * SilcCipher *send_key,
866 * SilcCipher *receive_key,
867 * SilcHmac *send_hmac,
868 * SilcHmac *receive_hmac);
872 * Returns the pointers of current ciphers and HMACs from the `stream'.
873 * Returns FALSE if keys are not set.
876 SilcBool silc_packet_get_keys(SilcPacketStream stream,
877 SilcCipher *send_key, SilcCipher *receive_key,
878 SilcHmac *send_hmac, SilcHmac *receive_hmac);
880 /****f* silccore/SilcPacketAPI/silc_packet_set_ids
884 * SilcBool silc_packet_set_ids(SilcPacketStream stream,
885 * SilcIdType src_id_type, const void *src_id
886 * SilcIdType dst_id_type, const void *dst_id);
890 * Set the source ID and destination ID to be used when sending packets to
891 * this packet stream. The IDs to be used for a packet stream can be
892 * overridden when sending packets. However, if the IDs do not ever change
893 * for the packet stream it is recommended they are set using this function.
894 * In this case they can be omitted when sending packets to the stream.
895 * It is also possible to set only source or destination ID.
898 SilcBool silc_packet_set_ids(SilcPacketStream stream,
899 SilcIdType src_id_type, const void *src_id,
900 SilcIdType dst_id_type, const void *dst_id);
902 /****f* silccore/SilcPacketAPI/silc_packet_set_ids
906 * SilcBool silc_packet_get_ids(SilcPacketStream stream,
907 * SilcBool *src_id_set, SilcID *src_id,
908 * SilcBool *dst_id_set, SilcID *dst_id);
912 * Returns source and destination IDs from the packet stream. The
913 * `src_id_set' is set to TRUE if the source ID was returned. The
914 * `dst_id_set' is set to TRUE if the destination ID was returned.
917 SilcBool silc_packet_get_ids(SilcPacketStream stream,
918 SilcBool *src_id_set, SilcID *src_id,
919 SilcBool *dst_id_set, SilcID *dst_id);
921 /****f* silccore/SilcPacketAPI/silc_packet_set_sid
925 * SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid);
929 * Sets new Security ID to the packet stream indicated by `stream'. This
930 * is called only if the IV Included property was set to the stream
931 * by calling silc_packet_stream_set_iv_included. This function sets
932 * new Security ID to the stream which is then included in the ciphertext
933 * of a packet. The `sid' must be 0 when it is set for the very first
934 * time and must be increased by one after each rekey. This function must
935 * be called every time new keys are added to the stream after a rekey.
937 * If this function is called when the IV Included property has not been
938 * set to the stream the `sid' will be ignored. Returns FALSE if the
939 * IV Included has not been set, TRUE otherwise.
942 SilcBool silc_packet_set_sid(SilcPacketStream stream, SilcUInt8 sid);
944 /****f* silccore/SilcPacketAPI/silc_packet_send
948 * SilcBool silc_packet_send(SilcPacketStream stream,
949 * SilcPacketType type, SilcPacketFlags flags,
950 * const unsigned char *data,
951 * SilcUInt32 data_len);
955 * Send `data' of length of `data_len' to the packet stream indicated by
956 * `stream'. If ciphers and HMACs were set using silc_packet_set_keys
957 * the packet will be encrypted and MAC will be computed for it. If
958 * silc_packet_set_ids was used to set source and destination ID for the
959 * packet stream those IDs are used in the packet. If IDs have not been
960 * set and they need to be provided then silc_packet_send_ext function
961 * should be used. Otherwise, the packet will not have IDs set at all.
962 * Returns FALSE if packet could not be sent.
965 SilcBool silc_packet_send(SilcPacketStream stream,
966 SilcPacketType type, SilcPacketFlags flags,
967 const unsigned char *data, SilcUInt32 data_len);
969 /****f* silccore/SilcPacketAPI/silc_packet_send_ext
974 * silc_packet_send_ext(SilcPacketStream stream,
975 * SilcPacketType type, SilcPacketFlags flags,
976 * SilcIdType src_id_type, void *srd_id,
977 * SilcIdType dst_id_type, void *dst_id,
978 * const unsigned char *data, SilcUInt32 data_len,
979 * SilcCipher cipher, SilcHmac hmac);
983 * Same as silc_packet_send but with this function different sending
984 * parameters can be sent as argument. This function can be used to
985 * set specific IDs, cipher and HMAC to be used in packet sending,
986 * instead of the ones saved in the `stream'. If any of the extra
987 * pointers are NULL, default values set to the stream will apply.
990 SilcBool silc_packet_send_ext(SilcPacketStream stream,
991 SilcPacketType type, SilcPacketFlags flags,
992 SilcIdType src_id_type, void *src_id,
993 SilcIdType dst_id_type, void *dst_id,
994 const unsigned char *data, SilcUInt32 data_len,
995 SilcCipher cipher, SilcHmac hmac);
997 /****f* silccore/SilcPacketAPI/silc_packet_send_va
1001 * SilcBool silc_packet_send_va(SilcPacketStream stream,
1002 * SilcPacketType type,
1003 * SilcPacketFlags flags, ...);
1007 * Same as silc_packet_send but takes the data in as variable argument
1008 * formatted buffer (see silcbuffmt.h). The arguments must be ended
1009 * with SILC_STR_END. Returns FALSE if packet could not be sent or
1010 * the buffer could not be formatted.
1014 * // Send NEW_CLIENT packet
1015 * silc_packet_send_va(stream, SILC_PACKET_NEW_CLIENT, 0,
1016 * SILC_STR_UI_SHORT(username_len),
1017 * SILC_STR_DATA(username, username_len),
1018 * SILC_STR_UI_SHORT(realname_len),
1019 * SILC_STR_DATA(realname, realname_len),
1023 SilcBool silc_packet_send_va(SilcPacketStream stream,
1024 SilcPacketType type, SilcPacketFlags flags, ...);
1026 /****f* silccore/SilcPacketAPI/silc_packet_send_va_ext
1031 * silc_packet_send_va_ext(SilcPacketStream stream,
1032 * SilcPacketType type, SilcPacketFlags flags,
1033 * SilcIdType src_id_type, void *srd_id,
1034 * SilcIdType dst_id_type, void *dst_id,
1035 * SilcCipher cipher, SilcHmac hmac, ...);
1039 * Same as silc_packet_send_va but with this function different sending
1040 * parameters can be sent as argument. This function can be used to
1041 * set specific IDs, cipher and HMAC to be used in packet sending,
1042 * instead of the ones saved in the `stream'. If any of the extra
1043 * pointers are NULL, default values set to the stream will apply.
1046 SilcBool silc_packet_send_va_ext(SilcPacketStream stream,
1047 SilcPacketType type, SilcPacketFlags flags,
1048 SilcIdType src_id_type, void *src_id,
1049 SilcIdType dst_id_type, void *dst_id,
1050 SilcCipher cipher, SilcHmac hmac, ...);
1052 /****f* silccore/SilcPacketAPI/silc_packet_wait
1056 * void *silc_packet_wait_init(SilcPacketStream stream, ...);
1060 * Initializes a packet waiter for the packet stream `stream' and
1061 * for the variable argument list of packet types. The function
1062 * silc_packet_wait can be used to block the thread until a packet
1063 * has been received. This function is used to initialize the waiting
1064 * and to give the list of packet types that caller wish to receive.
1065 * The variable argument list must end with -1. To receive all
1066 * packets use SILC_PACKET_ANY. Returns a context that must be given
1067 * to the silc_packet_wait function as argument. Returns NULL on
1068 * error. To uninitialize the waiting call silc_packet_wait_uninit.
1072 * Note that packets may be available immediately after calling this
1073 * function and they will be buffered, until silc_packet_wait is called.
1079 * // Will wait for private message packets
1080 * waiter = silc_packet_wait_init(stream,
1081 * SILC_PACKET_PRIVATE_MESSAGE, -1);
1085 void *silc_packet_wait_init(SilcPacketStream stream, ...);
1087 /****f* silccore/SilcPacketAPI/silc_packet_wait_uninit
1091 * void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream);
1095 * Uninitializes the waiting context. This may be called also from
1096 * another thread while other thread is waiting for packets. This will
1097 * inform the waiting thread to stop waiting.
1100 void silc_packet_wait_uninit(void *waiter, SilcPacketStream stream);
1102 /****f* silccore/SilcPacketAPI/silc_packet_wait
1106 * int silc_packet_wait(void *waiter, int timeout,
1107 * SilcPacket *return_packet)
1111 * A special function that can be used to wait for a packet to arrive.
1112 * This function will block the calling process or thread until either
1113 * a packet is received into the `return_packet' pointer or the specified
1114 * timeout value `timeout', which is in milliseconds, will expire. If
1115 * the timeout is 0, no timeout exist. Before calling this function the
1116 * silc_packet_wait_init must be called. The caller is responsible for
1117 * freeing the returned packet with silc_packet_free.
1119 * This function can be used for example from a thread that wants to
1120 * block until SILC packet has been received.
1122 * Returns 1 when packet was received, 0 if timeout occurred and -1 if
1127 * static int foo_read_data(FooContext c)
1129 * SilcPacket packet;
1133 * // Will wait for private message packets
1134 * if (c->initialized == FALSE) {
1135 * waiter = silc_packet_wait_init(stream,
1136 * SILC_PACKET_PRIVATE_MESSAGE, -1);
1137 * c->initialized = TRUE;
1141 * // Wait here until private message packet is received
1142 * if ((silc_packet_wait(waiter, 0, &packet)) < 0)
1145 * ... process packet ...
1151 int silc_packet_wait(void *waiter, int timeout, SilcPacket *return_packet);
1153 /****f* silccore/SilcPacketAPI/silc_packet_free
1157 * void silc_packet_free(SilcPacket packet);
1161 * This function is used to free the SilcPacket pointer that application
1162 * receives in the SilcPacketReceive callback. Application must free
1163 * the packet if it takes it in to processing.
1166 void silc_packet_free(SilcPacket packet);
1168 #endif /* SILCPACKET_H */