5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1997 - 2005 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/Packet Protocol Interface
24 * Implementation of the packet routines for sending and receiving
25 * SILC Packets. These includes the data sending routines and data
26 * reading routines, encrypting and decrypting routines, packet assembling
27 * and packet parsing routines.
34 /* XXX many of these could go to silcpacket_i.h */
36 /* Maximum packet length */
37 #define SILC_PACKET_MAX_LEN 0xffff
39 /* Maximum length of ID */
40 #define SILC_PACKET_MAX_ID_LEN 28
42 /****d* silccore/SilcPacketAPI/SilcPacketType
46 * typedef SilcUInt8 SilcPacketType;
50 * SILC packet type definition and all the packet types.
54 typedef SilcUInt8 SilcPacketType;
56 /* SILC Packet types. */
57 #define SILC_PACKET_NONE 0 /* NULL, never sent */
58 #define SILC_PACKET_DISCONNECT 1 /* Disconnection */
59 #define SILC_PACKET_SUCCESS 2 /* Success */
60 #define SILC_PACKET_FAILURE 3 /* Failure */
61 #define SILC_PACKET_REJECT 4 /* Rejected */
62 #define SILC_PACKET_NOTIFY 5 /* Notify message */
63 #define SILC_PACKET_ERROR 6 /* Error message */
64 #define SILC_PACKET_CHANNEL_MESSAGE 7 /* Message for channel */
65 #define SILC_PACKET_CHANNEL_KEY 8 /* Key of the channel */
66 #define SILC_PACKET_PRIVATE_MESSAGE 9 /* Private message */
67 #define SILC_PACKET_PRIVATE_MESSAGE_KEY 10 /* Private message key*/
68 #define SILC_PACKET_COMMAND 11 /* Command */
69 #define SILC_PACKET_COMMAND_REPLY 12 /* Reply to a command */
70 #define SILC_PACKET_KEY_EXCHANGE 13 /* Start of KE */
71 #define SILC_PACKET_KEY_EXCHANGE_1 14 /* KE1 */
72 #define SILC_PACKET_KEY_EXCHANGE_2 15 /* KE2 */
73 #define SILC_PACKET_CONNECTION_AUTH_REQUEST 16 /* Request of auth meth */
74 #define SILC_PACKET_CONNECTION_AUTH 17 /* Connectinon auth */
75 #define SILC_PACKET_NEW_ID 18 /* Sending new ID */
76 #define SILC_PACKET_NEW_CLIENT 19 /* Client registering */
77 #define SILC_PACKET_NEW_SERVER 20 /* Server registering */
78 #define SILC_PACKET_NEW_CHANNEL 21 /* Channel registering */
79 #define SILC_PACKET_REKEY 22 /* Re-key start */
80 #define SILC_PACKET_REKEY_DONE 23 /* Re-key done */
81 #define SILC_PACKET_HEARTBEAT 24 /* Heartbeat */
82 #define SILC_PACKET_KEY_AGREEMENT 25 /* Key Agreement request */
83 #define SILC_PACKET_RESUME_ROUTER 26 /* Backup router resume */
84 #define SILC_PACKET_FTP 27 /* File Transfer */
85 #define SILC_PACKET_RESUME_CLIENT 28 /* Client resume */
87 #define SILC_PACKET_PRIVATE 200 /* Private range start */
88 #define SILC_PACKET_MAX 255 /* RESERVED */
91 /****d* silccore/SilcPacketAPI/SilcPacketFlags
95 * typedef SilcUInt8 SilcPacketFlags;
99 * SILC packet flags type definition and all the packet flags.
103 typedef SilcUInt8 SilcPacketFlags;
105 /* All defined packet flags */
106 #define SILC_PACKET_FLAG_NONE 0x00 /* No flags */
107 #define SILC_PACKET_FLAG_PRIVMSG_KEY 0x01 /* Private message key */
108 #define SILC_PACKET_FLAG_LIST 0x02 /* Packet is a list */
109 #define SILC_PACKET_FLAG_BROADCAST 0x04 /* Packet is a broadcast */
110 #define SILC_PACKET_FLAG_COMPRESSED 0x08 /* Payload is compressed */
112 /* Impelemntation specific flags */
113 #define SILC_PACKET_FLAG_LONG_PAD 0x10 /* Use maximum padding */
116 /****s* silccore/SilcPacketAPI/SilcPacketEngine
120 * typedef struct SilcPacketEngineStruct *SilcPacketEngine;
124 * The packet engine context, allocated by silc_packet_engine_start.
125 * The engine is destroyed with silc_packet_engine_stop.
128 typedef struct SilcPacketEngineStruct *SilcPacketEngine;
130 /****s* silccore/SilcPacketAPI/SilcPacketStream
134 * typedef struct SilcPacketStreamStruct *SilcPacketStream;
138 * The packet stream context, allocated by silc_packet_stream_create.
139 * The stream is destroyed with silc_packet_stream_destroy.
142 typedef struct SilcPacketStreamStruct *SilcPacketStream;
144 /****s* silccore/SilcPacketAPI/SilcPacket
148 * typedef struct SilcPacketStruct *SilcPacket;
152 * The SilcPacket is returned by the packet engine in the SilcPacketReceive
153 * callback. The application can parse the data payload from the
154 * SilcPacket. Also packet type, flags, and sender and destination
155 * IDs are available. The application must free the packet with the
156 * silc_packet_free function.
160 typedef struct SilcPacketStruct {
161 struct SilcPacketStruct *next;
162 SilcBufferStruct buffer; /* Packet data payload */
163 unsigned char *src_id; /* Source ID */
164 unsigned char *dst_id; /* Destination ID */
165 unsigned int src_id_len : 6; /* Source ID length */
166 unsigned int src_id_type : 2; /* Source ID type */
167 unsigned int dst_id_len : 6; /* Destination ID length */
168 unsigned int dst_id_type : 2; /* Destination ID type */
169 SilcPacketType type; /* Packet type */
170 SilcPacketFlags flags; /* Packet flags */
174 /****d* silcutil/SilcPacketAPI/SilcPacketError
178 * typedef enum { ... } SilcPacketError
182 * Packet errors. This is returned in the error callback. If application
183 * needs the actual lower level stream error, it needs to retrieve it
184 * from the actual stream.
189 SILC_PACKET_ERR_READ, /* Error while reading */
190 SILC_PACKET_ERR_WRITE, /* Error while writing */
191 SILC_PACKET_ERR_MAC_FAILED, /* Packet MAC check failed */
192 SILC_PACKET_ERR_DECRYPTION_FAILED, /* Packet decryption failed */
193 SILC_PACKET_ERR_MALFORMED, /* Packet is malformed */
194 SILC_PACKET_ERR_NO_MEMORY, /* System out of memory */
198 /****f* silccore/SilcPacketAPI/SilcPacketReceiveCb
202 * typedef void (*SilcPacketReceiveCb)(SilcPacketEngine engine,
203 * SilcPacketStream stream,
205 * void *callback_context,
206 * void *app_context);
210 * The packet receive callback is called by the packet engine when a new
211 * SILC Packet has arrived. The application must free the returned
212 * SilcPacket with silc_packet_free. This callback is set in the
213 * SilcPacketCallbacks structure.
216 typedef void (*SilcPacketReceiveCb)(SilcPacketEngine engine,
217 SilcPacketStream stream,
219 void *callback_context,
222 /****f* silccore/SilcPacketAPI/SilcPacketEosCb
226 * typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
227 * SilcPacketStream stream,
228 * void *callback_context,
229 * void *app_context);
233 * The End Of Stream (EOS) callback, that is called by the packet engine
234 * when the underlaying stream has ended. No more data can be sent to
235 * the stream or read from it. The `stream' must be destroyed by
236 * calling the silc_packet_stream_destroy. This callback is set in the
237 * SilcPacketCallbacks structure.
240 typedef void (*SilcPacketEosCb)(SilcPacketEngine engine,
241 SilcPacketStream stream,
242 void *callback_context,
245 /****f* silccore/SilcPacketAPI/SilcPacketErrorCb
249 * typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
250 * SilcPacketStream stream,
251 * SilcPacketError error,
252 * void *callback_context,
253 * void *app_context);
257 * The error callback that is called by the packet engine if an error
258 * occurs. The `error' will indicate the error. This callback is set
259 * in the SilcPacketCallbacks structure.
262 typedef void (*SilcPacketErrorCb)(SilcPacketEngine engine,
263 SilcPacketStream stream,
264 SilcPacketError error,
265 void *callback_context,
268 /****s* silccore/SilcPacketAPI/SilcPacketStream
272 * typedef struct SilcPacketStreamStruct *SilcPacketStream;
276 * This structure is sent as argument to the silc_packet_engine_start
277 * function to set the callback functions for the packet engine. The
278 * packet engine will call the callbacks when necessary. Application
279 * must always be provided for the packet engine.
284 SilcPacketReceiveCb packet_receive; /* Called when packet is received */
285 SilcPacketEosCb eos; /* Called on end of stream */
286 SilcPacketErrorCb error; /* Called on an error */
287 } SilcPacketCallbacks;
292 /****f* silccore/SilcPacketAPI/silc_packet_engine_start
297 * silc_packet_engine_start(SilcSchedule schedule, SilcRng rng, bool router,
298 * SilcPacketCallbacks *callbacks,
299 * void *callback_context);
303 * Create new packet engine for processing incoming and outgoing packets.
304 * If `rng' is non-NULL that RNG will be used to create necessary random
305 * numbers during packet processing. If NULL, Global RNG will be used.
306 * If `router' is TRUE then the application is considered to be router
307 * server, and certain packets are handled differently. Client and normal
308 * server must set it to FALSE. The `callbacks' is a SilcPacketCallbacks
309 * structure provided by the caller which includes the callbacks that is
310 * called when for example packet is received, or end of stream is called
314 * The packet engine is thread safe. Also the `schedule' and `rng' are
315 * thread safe. You can use one packet engine in multi threaded
320 silc_packet_engine_start(SilcSchedule schedule, SilcRng rng, bool router,
321 SilcPacketCallbacks *callbacks,
322 void *callback_context);
324 /****f* silccore/SilcPacketAPI/silc_packet_engine_stop
328 * void silc_packet_engine_stop(SilcPacketEngine engine);
332 * Stop the packet engine. No new packets can be sent or received after
333 * calling this, and the `engine' will become invalid.
336 void silc_packet_engine_stop(SilcPacketEngine engine);
338 /****f* silccore/SilcPacketAPI/silc_packet_stream_create
342 * SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
343 * SilcStream stream);
347 * Create new packet stream and use the `stream' as underlaying stream.
348 * Usually the `stream' would be a socket stream, but it can be any
349 * stream. After this function returns, packets can immediately be
350 * sent to or received from the stream.
354 * SilcPacketStream cannot be used with silc_stream_* routines (such as
355 * silc_stream_read and silc_stream_write) because of its special nature.
356 * Use the silc_packet_send and the silc_packet_send_ext to send packets.
357 * To read packets you will receive the packet receive callback from
358 * packet engine. Destroy the stream with silc_packet_stream_destroy.
360 * If you need to send only one type of SILC packets, then it is possible
361 * to create SILC Packet Streamer with silc_packet_streamer_create, which
362 * can be used with silc_stream_read and silc_stream_write.
364 * The SilcPacketStream is not thread safe. If you share same stream
365 * with multiple threads concurrency control need to be employed. It
366 * is recommended to create new SilcPacketStream for every thread.
369 SilcPacketStream silc_packet_stream_create(SilcPacketEngine engine,
372 /****f* silccore/SilcPacketAPI/silc_packet_stream_destroy
376 * void silc_packet_stream_destroy(SilcPacketStream stream);
380 * Destroy packet stream and the underlaying stream. This will also
381 * send end of stream to the underlaying stream.
384 void silc_packet_stream_destroy(SilcPacketStream stream);
386 /****f* silccore/SilcPacketAPI/silc_packet_streamer_create
390 * SilcStream silc_packet_streamer_create(SilcPacketStream stream,
391 * SilcPacketType packet_type,
392 * SilcPacketFlags packet_flags);
396 * This function can be used to create a SILC Packet Streamer that will
397 * stream only one type of packet indicated by `packet_type' with packet
398 * flags `packet_flags'. This is special purpose function as usually
399 * multiple different types of packets need to be sent in application.
400 * There are cases however when creating streamer is simpler and more
401 * efficient. Cases such as file transfer stream or other data streams
402 * that only send and receive one type of packet. While it would be
403 * possible to use silc_packet_send function to send packets it is
404 * more efficient to create the SILC Packet Streamer and use the
405 * silc_stream_read and silc_stream_write functions.
407 * The encryption and decryption keys, and other information will be
408 * retrieved from the packet stream indicated by `stream', which must be
409 * created before creating the streamer.
413 * The packet type that is assocated with the packet stream `stream' will
414 * only be available through the returned SilcStream. That packet type
415 * will not be delivered to the packet callbacks. To return to the
416 * normal operation destroy the streamer silc_packet_streamer_destroy.
419 SilcStream silc_packet_streamer_create(SilcPacketStream stream,
420 SilcPacketType packet_type,
421 SilcPacketFlags packet_flags);
423 /****f* silccore/SilcPacketAPI/silc_packet_streamer_destroy
427 * void silc_packet_streamer_destroy(SilcStream stream);
431 * Destroys the created packet streamer. Use this function only for
432 * stream created with silc_packet_streamer_create. The packet type
433 * that was associated with the streamer can be received in the packet
434 * callbacks after the streamer is destroyed.
437 void silc_packet_streamer_destroy(SilcStream stream);
439 /****f* silccore/SilcPacketAPI/silc_packet_stream_get_stream
443 * SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
447 * Returns the actual stream that is associated with the packet stream
448 * `stream'. The caller must not free the returned stream. The returned
449 * stream is the same pointer that was set for silc_packet_stream_create.
450 * This function couled be used for example when an error callback is
451 * called by the packet engine to retrieve the actual lower level error
455 SilcStream silc_packet_stream_get_stream(SilcPacketStream stream);
457 /****f* silccore/SilcPacketAPI/silc_packet_stream_callbacks
461 * void silc_packet_stream_callbacks(SilcPacketStream stream,
462 * SilcPacketCallbacks *callbacks,
463 * void *callback_context);
467 * This is optional function which can be used to set specific callbacks
468 * for the packet stream indicated by `stream'. If these are set then
469 * `callbacks' will be used instead of the ones set for the function
470 * silc_packet_engine_start. To reset the normal behaviour call this
471 * function again with `callbacks' as NULL. Note that the responsibility
472 * of handling end of stream, and error conditions moves to the layer
473 * calling this function since the original callbacks set in the
474 * silc_packet_engine_start will not be called.
477 void silc_packet_stream_callbacks(SilcPacketStream stream,
478 SilcPacketCallbacks *callbacks,
479 void *callback_context);
481 /****f* silccore/SilcPacketAPI/silc_packet_stream_ref
485 * void silc_packet_stream_ref(SilcPacketStream stream);
489 * Increase reference counter for the stream indicated by `stream'. This
490 * can be used to take a reference for the stream. To unreference the
491 * stream call silc_packet_stream_unref function.
494 void silc_packet_stream_ref(SilcPacketStream stream);
496 /****f* silccore/SilcPacketAPI/silc_packet_stream_unref
500 * void silc_packet_stream_unref(SilcPacketStream stream);
504 * Decrease reference counter for the stream indicated by `stream'. If
505 * the counter hits zero the stream will be destroyed automatically.
508 void silc_packet_stream_unref(SilcPacketStream stream);
510 /****f* silccore/SilcPacketAPI/silc_packet_set_context
514 * void silc_packet_set_context(SilcPacketStream stream, void *app_context);
518 * Set an application specific context to the stream. The context will
519 * be delivered to all callback functions, and it can be retrieved by
520 * calling silc_packet_get_context function as well. Note that this is
521 * separate packet stream specific context, and not the same as
522 * `callback_context' in silc_packet_engine_start. Both will be delivered
526 void silc_packet_set_context(SilcPacketStream stream, void *app_context);
528 /****f* silccore/SilcPacketAPI/silc_packet_get_context
532 * void *silc_packet_get_context(SilcPacketStream stream);
536 * Returns the current set application context, or NULL if none is set.
539 void *silc_packet_get_context(SilcPacketStream stream);
541 /****f* silccore/SilcPacketAPI/silc_packet_set_ciphers
545 * void silc_packet_set_ciphers(SilcPacketStream stream, SilcCipher send,
546 * SilcCipher receive);
550 * Set ciphers to be used to encrypt sent packets, and decrypt received
551 * packets. This can be called multiple times to change the ciphers.
552 * In this case if old cipher is set it will be freed. If ciphers are
553 * not set packets will not be encrypted or decrypted.
556 void silc_packet_set_ciphers(SilcPacketStream stream, SilcCipher send,
559 /****f* silccore/SilcPacketAPI/silc_packet_get_ciphers
563 * bool silc_packet_get_ciphers(SilcPacketStream stream, SilcCipher *send,
564 * SilcCipher *receive);
568 * Returns the pointers of current ciphers from the `stream'. Returns
569 * FALSE if ciphers are not set.
572 bool silc_packet_get_ciphers(SilcPacketStream stream, SilcCipher *send,
573 SilcCipher *receive);
575 /****f* silccore/SilcPacketAPI/silc_packet_set_hmacs
579 * void silc_packet_set_hmacs(SilcPacketStream stream, SilcHmac send,
584 * Set HMACs to be used to create MACs for sent packets and to check
585 * MAC for received packets. This can be called multiple times to change
586 * the HMACs. In this case if old HMAC is set it will be freed. If
587 * HMACs are not set MACs are not generated or verified for packets.
590 void silc_packet_set_hmacs(SilcPacketStream stream, SilcHmac send,
593 /****f* silccore/SilcPacketAPI/silc_packet_get_hmacs
597 * bool silc_packet_get_hmacs(SilcPacketStream stream, SilcHmac *send,
598 * SilcHmac *receive);
602 * Returns the pointers of current HMACs from the `stream'. Returns
603 * FALSE if HMACs are not set.
606 bool silc_packet_get_hmacs(SilcPacketStream stream, SilcHmac *send,
609 /****f* silccore/SilcPacketAPI/silc_packet_set_ids
613 * bool silc_packet_set_ids(SilcPacketStream stream,
614 * SilcIdType src_id_type, const void *src_id
615 * SilcIdType dst_id_type, const void *dst_id);
619 * Set the source ID and destinaion ID to be used when sending packets to
620 * this packet stream. The IDs to be used for a packet stream can be
621 * overridden when sending packets. However, if the IDs do not ever change
622 * for the packet stream it is recommended they are set using this function.
623 * In this case they can be omitted when sending packets to the stream.
624 * It is also possible to set only source or destination ID.
627 bool silc_packet_set_ids(SilcPacketStream stream,
628 SilcIdType src_id_type, const void *src_id,
629 SilcIdType dst_id_type, const void *dst_id);
631 /****f* silccore/SilcPacketAPI/silc_packet_send
635 * bool silc_packet_send(SilcPacketStream stream,
636 * SilcPacketType type, SilcPacketFlags flags,
637 * const unsigned char *data, SilcUInt32 data_len);
641 * Send `data' of length of `data_len' to the packet stream indicated by
642 * `stream'. If ciphers and HMACs were set using silc_packet_set_ciphers
643 * and silc_packet_set_hmacs the packet will be encrypted and MAC will be
644 * generated for it. If silc_packet_set_ids was used to set source and
645 * destination ID for the packet stream those IDs are used in the
646 * packet. If IDs have not been set and they need to be provided then
647 * silc_packet_send_ext function should be used. Otherwise, the packet
648 * will not have IDs set at all.
651 bool silc_packet_send(SilcPacketStream stream,
652 SilcPacketType type, SilcPacketFlags flags,
653 const unsigned char *data, SilcUInt32 data_len);
655 /****f* silccore/SilcPacketAPI/silc_packet_send_ext
660 * silc_packet_send_ext(SilcPacketStream stream,
661 * SilcPacketType type, SilcPacketFlags flags,
662 * SilcIdType src_id_type, void *srd_id,
663 * SilcIdType dst_id_type, void *dst_id,
664 * const unsigned char *data, SilcUInt32 data_len,
665 * SilcCipher cipher, SilcHmac hmac);
669 * This function can be used to specificly set different parameters of
670 * the SILC packet to be sent to the stream indicated by `stream'. This
671 * function can be used to set specific IDs, cipher and HMAC to be used
672 * in packet creation. If `truelen' is provided that value is put to the
673 * SILC packet's truelen field, if it is zero the routine will calculate
674 * the truelen field for the packet. If `padlen' is provided that value
675 * will be the length of the padding for the packet, if zero the routine
676 * will calculate necessary amount of padding for the packet. This
677 * function can be used when specific ciphers, HMACs and IDs has not been
678 * set for the stream, or setting them for the stream is not suitable.
681 bool silc_packet_send_ext(SilcPacketStream stream,
682 SilcPacketType type, SilcPacketFlags flags,
683 SilcIdType src_id_type, void *src_id,
684 SilcIdType dst_id_type, void *dst_id,
685 const unsigned char *data, SilcUInt32 data_len,
686 SilcCipher cipher, SilcHmac hmac);
688 /****f* silccore/SilcPacketAPI/silc_packet_free
692 * void silc_packet_free(SilcPacketEngine engine, SilcPacket packet);
696 * This function is used to free the SilcPacket pointer that application
697 * receives in the SilcPacketReceive callback. Application must free
701 void silc_packet_free(SilcPacketEngine engine, SilcPacket packet);
703 #endif /* SILCPACKET_H */