updates.
[silc.git] / lib / silccore / silcpacket.h
1 /*
2
3   silcpacket.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1997 - 2001 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/SilcPacketAPI
21  *
22  * DESCRIPTION
23  *
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.
28  *
29  ***/
30
31 #ifndef SILCPACKET_H
32 #define SILCPACKET_H
33
34 /* Default byte size of the packet. */
35 #define SILC_PACKET_DEFAULT_SIZE SILC_SOCKET_BUF_SIZE
36
37 /* Header length without source and destination ID's. */
38 #define SILC_PACKET_HEADER_LEN 10
39
40 /* Minimum length of SILC Packet Header. This much is decrypted always
41    when packet is received to be able to get all the relevant data out
42    from the header. */
43 #define SILC_PACKET_MIN_HEADER_LEN 16
44
45 /* Maximum padding length */
46 #define SILC_PACKET_MAX_PADLEN 128
47
48 /* Default padding length */
49 #define SILC_PACKET_DEFAULT_PADLEN 16
50
51 /* Minimum packet length */
52 #define SILC_PACKET_MIN_LEN (SILC_PACKET_HEADER_LEN + 1)
53
54 /* Maximum length of ID */
55 #define SILC_PACKET_MAX_ID_LEN 16
56
57 /****d* silccore/SilcPacketAPI/SilcPacketType
58  *
59  * NAME
60  * 
61  *    typedef unsigned char SilcPacketType;
62  *
63  * DESCRIPTION
64  *
65  *    SILC packet type definition and all the packet types.
66  *
67  * SOURCE
68  */
69 typedef unsigned char SilcPacketType;
70
71 /* SILC Packet types. */
72 #define SILC_PACKET_NONE                 0       /* NULL, never sent */
73 #define SILC_PACKET_DISCONNECT           1       /* Disconnection */
74 #define SILC_PACKET_SUCCESS              2       /* Success */
75 #define SILC_PACKET_FAILURE              3       /* Failure */
76 #define SILC_PACKET_REJECT               4       /* Rejected */
77 #define SILC_PACKET_NOTIFY               5       /* Notify message */
78 #define SILC_PACKET_ERROR                6       /* Error message */
79 #define SILC_PACKET_CHANNEL_MESSAGE      7       /* Message for channel */
80 #define SILC_PACKET_CHANNEL_KEY          8       /* Key of the channel */
81 #define SILC_PACKET_PRIVATE_MESSAGE      9       /* Private message */
82 #define SILC_PACKET_PRIVATE_MESSAGE_KEY  10      /* Private message key*/
83 #define SILC_PACKET_COMMAND              11      /* Command */
84 #define SILC_PACKET_COMMAND_REPLY        12      /* Reply to a command */
85 #define SILC_PACKET_KEY_EXCHANGE         13      /* Start of KE */
86 #define SILC_PACKET_KEY_EXCHANGE_1       14      /* KE1 */
87 #define SILC_PACKET_KEY_EXCHANGE_2       15      /* KE2 */
88 #define SILC_PACKET_CONNECTION_AUTH_REQUEST 16   /* Request of auth meth */
89 #define SILC_PACKET_CONNECTION_AUTH      17      /* Connectinon auth */
90 #define SILC_PACKET_NEW_ID               18      /* Sending new ID */
91 #define SILC_PACKET_NEW_CLIENT           19      /* Client registering */
92 #define SILC_PACKET_NEW_SERVER           20      /* Server registering */
93 #define SILC_PACKET_NEW_CHANNEL          21      /* Channel registering */
94 #define SILC_PACKET_REKEY                22      /* Re-key start */
95 #define SILC_PACKET_REKEY_DONE           23      /* Re-key done */
96 #define SILC_PACKET_HEARTBEAT            24      /* Heartbeat */
97 #define SILC_PACKET_KEY_AGREEMENT        25      /* Key Agreement request */
98 #define SILC_PACKET_RESUME_ROUTER        26      /* Backup router resume */
99 #define SILC_PACKET_FTP                  27      /* File Transfer */
100
101 #define SILC_PACKET_PRIVATE              200     /* Private range start  */
102 #define SILC_PACKET_MAX                  255     /* RESERVED */
103 /***/
104
105 /****d* silccore/SilcPacketAPI/SilcPacketVersion
106  *
107  * NAME
108  * 
109  *    typedef unsigned char SilcPacketVersion;
110  *
111  * DESCRIPTION
112  *
113  *    SILC packet version type definition.
114  *
115  ***/
116 typedef unsigned char SilcPacketVersion;
117
118 /****d* silccore/SilcPacketAPI/SilcPacketFlags
119  *
120  * NAME
121  * 
122  *    typedef unsigned char SilcPacketFlags;
123  *
124  * DESCRIPTION
125  *
126  *    SILC packet flags type definition and all the packet flags.
127  *
128  * SOURCE
129  */
130 typedef unsigned char SilcPacketFlags;
131
132 /* All defined packet flags */
133 #define SILC_PACKET_FLAG_NONE             0x00    /* No flags */
134 #define SILC_PACKET_FLAG_PRIVMSG_KEY      0x01    /* Private message key */
135 #define SILC_PACKET_FLAG_LIST             0x02    /* Packet is a list */
136 #define SILC_PACKET_FLAG_BROADCAST        0x04    /* Packet is a broadcast */
137 /***/
138
139 /* Rest of flags still available
140 #define SILC_PACKET_FLAG_XXX              0x08
141 #define SILC_PACKET_FLAG_XXX              0x10
142 #define SILC_PACKET_FLAG_XXX              0x20
143 #define SILC_PACKET_FLAG_XXX              0x40
144 #define SILC_PACKET_FLAG_XXX              0x80
145 */
146
147 /****s* silccore/SilcPacketAPI/SilcPacketContext
148  *
149  * NAME
150  * 
151  *    typedef struct { ... } SilcPacketContext;
152  *
153  * DESCRIPTION
154  *
155  *    In packet sending this is filled and sent to silc_packet_assemble 
156  *    which then uses it to assemble new packet. In packet reception pointer 
157  *    to this context is sent to silc_packet_parse which parses the packet 
158  *    and returns the relevant information to this structure. On packet 
159  *    reception returned ID's are always the hash values of the ID's from 
160  *    the packet. 
161  *
162  *    Short description of the fields following:
163  *
164  *    SilcBuffer buffer
165  *
166  *      The data buffer.
167  *
168  *    SilcPacketType type
169  *
170  *      Type of the packet. Types are defined below.
171  *
172  *    SilcPacketFlags flags
173  *
174  *      Packet flags. Flags are defined above.
175  *
176  *    unsigned char *src_id
177  *    uint16 src_id_len
178  *    unsigned char src_id_type
179  *
180  *      Source ID, its length and type. On packet reception retuned ID's
181  *      are always the hash values of the ID's from the packet.
182  *
183  *    unsigned char *dst_id;
184  *    uint16 dst_id_len;
185  *    unsigned char src_id_type;
186  *
187  *      Destination ID, its length and type. On packet reception retuned
188  *      ID's are always the hash values of the ID's from the packet.
189  *
190  *    uint16 truelen
191  *    uint16 padlen
192  *
193  *      The true lenght of the packet and the padded length of the packet.
194  *      These may be set by the caller before calling any of the 
195  *      silc_packet_* routines. If not provided the library will calculate
196  *      the values.
197  *
198  *    int users;
199  *
200  *      Reference counter for this context. The context is freed only 
201  *      after the reference counter hits zero. The counter is added
202  *      calling silc_packet_context_dup and decreased by calling the
203  *      silc_packet_context_free.
204  *
205  *    uint32 sequence;
206  *
207  *      Packet sequence number.
208  *
209  ***/
210 typedef struct {
211   SilcBuffer buffer;
212
213   uint16 truelen;
214   SilcPacketFlags flags;
215   SilcPacketType type;
216   uint8 padlen;
217
218   unsigned char *src_id;
219   uint8 src_id_len;
220   uint8 src_id_type;
221
222   unsigned char *dst_id;
223   uint8 dst_id_len;
224   uint8 dst_id_type;
225
226   int users;
227   bool long_pad;                /* Set to TRUE to use maximum padding
228                                    in packet (up to 256 bytes). */
229
230   uint32 sequence;
231 } SilcPacketContext;
232
233 /****s* silccore/SilcPacketAPI/SilcPacketParserContext
234  *
235  * NAME
236  * 
237  *    typedef struct { ... } SilcPacketParserContext;
238  *
239  * DESCRIPTION
240  *
241  *    This context is used in packet reception when the function
242  *    silc_packet_receive_process calls parser callback that performs
243  *    the actual packet decryption and parsing. This context is sent as
244  *    argument to the parser function. This context must be free'd by
245  *    the parser callback function.
246  *
247  *    Following description of the fields:
248  *
249  *    SilcPacketContext *packet
250  *
251  *      The actual packet received from the network. In this phase the
252  *      context is not parsed, only the packet->buffer is allocated and
253  *      it includes the raw packet data, which is encrypted.
254  *
255  *    bool normal
256  *
257  *      Indicates whether the received packet is normal or special packet.
258  *      If special the parsing process is special also.
259  *
260  *    SilcSocketConnection sock
261  *
262  *      The associated connection.
263  *
264  *    void *context
265  *
266  *      User context that is sent to the silc_packet_receive_process
267  *      function. This usually includes application and connection specific
268  *      data.
269  *
270  ***/
271 typedef struct {
272   SilcPacketContext *packet;
273   bool normal;
274   SilcSocketConnection sock;
275   void *context;
276 } SilcPacketParserContext;
277
278 /****f* silccore/SilcPacketAPI/SilcPacketParserCallback
279  *
280  * SYNOPSIS
281  *
282  *    typedef void (*SilcPacketParserCallback)(SilcPacketParserContext 
283  *                                             *parse_context);
284  *
285  * DESCRIPTION
286  *
287  *    This callback is given to the silc_packet_receive_process function.
288  *    The callback is called by the library every time a packet is
289  *    received from the network. After the packet has been decrypted
290  *    and at least partially parsed it is passed to the application
291  *    for further parsing using this callback and the SilcPacketParserContext
292  *    context. The application receiving the SilcPacketParserContext
293  *    must free it.
294  *
295  ***/
296 typedef void (*SilcPacketParserCallback)(SilcPacketParserContext 
297                                          *parse_context, void *context);
298
299 /* Macros */
300
301 /****d* silccore/SilcPacketAPI/SILC_PACKET_LENGTH
302  *
303  * NAME
304  * 
305  *    #define SILC_PACKET_LENGTH ...
306  *
307  * DESCRIPTION
308  *
309  *    Returns true length of the packet. This is primarily used by the
310  *    libary in packet parsing phase but the application may use it as
311  *    well if needed.
312  *
313  * SOURCE
314  */
315 #define SILC_PACKET_LENGTH(__packet, __ret_truelen, __ret_paddedlen)    \
316 do {                                                                    \
317   SILC_GET16_MSB((__ret_truelen), (__packet)->data);                    \
318   (__ret_paddedlen) = (__ret_truelen) + (__packet)->data[4];            \
319 } while(0)
320 /***/
321
322 /****d* silccore/SilcPacketAPI/SILC_PACKET_PADLEN
323  *
324  * NAME
325  * 
326  *    #define SILC_PACKET_PADLEN ...
327  *
328  * DESCRIPTION
329  *
330  *    Returns the length of the padding in the packet. This is used
331  *    by various library routines to determine needed padding length.
332  *
333  * SOURCE
334  */
335 #define SILC_PACKET_PADLEN(__packetlen, __blocklen)             \
336   SILC_PACKET_DEFAULT_PADLEN - (__packetlen) %                  \
337     ((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)
338 /***/
339
340 /****d* silccore/SilcPacketAPI/SILC_PACKET_PADLEN_MAX
341  *
342  * NAME
343  * 
344  *    #define SILC_PACKET_PADLEN_MAX ...
345  *
346  * DESCRIPTION
347  *
348  *    Returns the length of the padding up to the maximum length, which
349  *    is 128 butes. This is used by various library routines to determine
350  *    needed padding length.
351  *
352  * SOURCE
353  */
354 #define SILC_PACKET_PADLEN_MAX(__packetlen)                             \
355   SILC_PACKET_MAX_PADLEN - (__packetlen) % SILC_PACKET_MAX_PADLEN
356 /***/
357
358 /* Prototypes */
359
360 /****f* silccore/SilcPacketAPI/silc_packet_send
361  *
362  * SYNOPSIS
363  *
364  *    int silc_packet_send(SilcSocketConnection sock, bool force_send);
365  *
366  * DESCRIPTION
367  *
368  *    Actually sends the packet. This flushes the connections outgoing data
369  *    buffer. If data is sent directly to the network this returns the bytes
370  *    written, if error occured this returns -1 and if the data could not
371  *    be written directly to the network at this time this returns -2, in
372  *    which case the data should be queued by the caller and sent at some
373  *    later time. If `force_send' is TRUE this attempts to write the data
374  *    directly to the network, if FALSE, this returns -2.
375  *
376  ***/
377 int silc_packet_send(SilcSocketConnection sock, bool force_send);
378
379 /****f* silccore/SilcPacketAPI/silc_packet_encrypt
380  *
381  * SYNOPSIS
382  *
383  *    void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, 
384  *                             SilcBuffer buffer, uint32 len);
385  *
386  * DESCRIPTION
387  *
388  *    Encrypts a packet. This also creates HMAC of the packet before 
389  *    encryption and adds the HMAC at the end of the buffer. This assumes
390  *    that there is enough free space at the end of the buffer to add the
391  *    computed HMAC. This is the normal way of encrypting packets, if some
392  *    other process of HMAC computing and encryption is needed this function
393  *    cannot be used. 
394  *
395  ***/
396 void silc_packet_encrypt(SilcCipher cipher, SilcHmac hmac, uint32 sequence,
397                          SilcBuffer buffer, uint32 len);
398
399 /****f* silccore/SilcPacketAPI/silc_packet_assemble
400  *
401  * SYNOPSIS
402  *
403  *    void silc_packet_assemble(SilcPacketContext *ctx);
404  *
405  * DESCRIPTION
406  *
407  *    Assembles a new packet to be ready for send out. The buffer sent as
408  *    argument must include the data to be sent and it must not be encrypted. 
409  *    The packet also must have enough free space so that the SILC header
410  *    and padding maybe added to the packet. The packet is encrypted after 
411  *    this function has returned.
412  *
413  *    The buffer sent as argument should be something like following:
414  *
415  *    --------------------------------------------
416  *    | head             | data           | tail |
417  *    --------------------------------------------
418  *    ^                  ^
419  *    58 bytes           x bytes
420  *
421  *    So that the SILC header and 1 - 16 bytes of padding can fit to
422  *    the buffer. After assembly the buffer might look like this:
423  *
424  *    --------------------------------------------
425  *    | data                              |      |
426  *    --------------------------------------------
427  *    ^                                   ^
428  *    Start of assembled packet
429  *
430  *    Packet construct is as follows (* = won't be encrypted):
431  *
432  *    n bytes       SILC Header
433  *      2 bytes     Payload length  (*)
434  *      1 byte      Flags
435  *      1 byte      Packet type
436  *      2 bytes     Source ID Length
437  *      2 bytes     Destination ID Length
438  *      1 byte      Source ID Type
439  *      n bytes     Source ID
440  *      1 byte      Destination ID Type
441  *      n bytes     Destination ID
442  *
443  *    1 - 16 bytes    Padding
444  *
445  *    n bytes        Data payload
446  *
447  *    All fields in the packet will be authenticated by MAC. The MAC is
448  *    not computed here, it must be computed separately before encrypting
449  *    the packet.
450  *
451  ***/
452 void silc_packet_assemble(SilcPacketContext *ctx, SilcCipher cipher);
453
454 /****f* silccore/SilcPacketAPI/silc_packet_send_prepare
455  *
456  * SYNOPSIS
457  *
458  *    void silc_packet_send_prepare(SilcSocketConnection sock,
459  *                                  uint32 header_len,
460  *                                  uint32 padlen,
461  *                                  uint32 data_len);
462  *
463  * DESCRIPTION
464  *
465  *    Prepare outgoing data buffer for packet sending. This moves the data
466  *    area so that new packet may be added into it. If needed this allocates
467  *    more space to the buffer. This handles directly the connection's
468  *    outgoing buffer in SilcSocketConnection object.
469  *
470  ***/
471 void silc_packet_send_prepare(SilcSocketConnection sock,
472                               uint32 header_len,
473                               uint32 padlen,
474                               uint32 data_len);
475
476 /****f* silccore/SilcPacketAPI/silc_packet_receive
477  *
478  * SYNOPSIS
479  *
480  *    int silc_packet_receive(SilcSocketConnection sock);
481  *
482  * DESCRIPTION
483  *
484  *    Receives packet from network and reads the data into connection's
485  *    incoming data buffer. If the data was read directly this returns the
486  *    read bytes, if error occured this returns -1, if the data could not
487  *    be read directly at this time this returns -2 in which case the data
488  *    should be read again at some later time, or If EOF occured this returns
489  *    0.
490  *
491  ***/
492 int silc_packet_receive(SilcSocketConnection sock);
493
494 /****f* silccore/SilcPacketAPI/silc_packet_receive_process
495  *
496  * SYNOPSIS
497  *
498  *    void silc_packet_receive_process(SilcSocketConnection sock,
499  *                                     bool local_is_router,
500  *                                     SilcCipher cipher, SilcHmac hmac,
501  *                                     SilcPacketParserCallback parser,
502  *                                     void *parser_context);
503  *
504  * DESCRIPTION
505  *
506  *    Processes and decrypts the incmoing data, and calls parser callback
507  *    for each received packet that will handle the actual packet parsing.
508  *    If more than one packet was received this calls the parser multiple
509  *    times.  The parser callback will get context SilcPacketParserContext
510  *    that includes the packet and the `parser_context' sent to this
511  *    function. 
512  *
513  *    The `local_is_router' indicates whether the caller is router server
514  *    in which case the receiving process of a certain packet types may
515  *    be special.  Normal server and client must set it to FALSE.  The
516  *    SilcPacketParserContext will indicate also whether the received
517  *    packet was normal or special packet.
518  *
519  ***/
520 void silc_packet_receive_process(SilcSocketConnection sock,
521                                  bool local_is_router,
522                                  SilcCipher cipher, SilcHmac hmac,
523                                  uint32 sequence,
524                                  SilcPacketParserCallback parser,
525                                  void *parser_context);
526
527 /****f* silccore/SilcPacketAPI/silc_packet_parse
528  *
529  * SYNOPSIS
530  *
531  *    SilcPacketType silc_packet_parse(SilcPacketContext *ctx);
532  *
533  * DESCRIPTION
534  *
535  *    Parses the packet. This is called when a whole packet is ready to be
536  *    parsed. The buffer sent must be already decrypted before calling this 
537  *    function. The len argument must be the true length of the packet. This 
538  *    function returns the type of the packet. The data section of the 
539  *    buffer is parsed, not head or tail sections.
540  *
541  ***/
542 SilcPacketType silc_packet_parse(SilcPacketContext *ctx, SilcCipher cipher);
543
544 /****f* silccore/SilcPacketAPI/silc_packet_parse_special
545  *
546  * SYNOPSIS
547  *
548  *    SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx);
549  *
550  * DESCRIPTION
551  *
552  *    Perform special SILC Packet header parsing. This is required to some
553  *    packet types that have the data payload encrypted with different key
554  *    than the header area plus padding of the packet. Hence, this parses
555  *    the header in a way that it does not take the data area into account
556  *    and parses the header and padding area only.
557  *
558  ***/
559 SilcPacketType silc_packet_parse_special(SilcPacketContext *ctx,
560                                          SilcCipher cipher);
561
562 /****f* silccore/SilcPacketAPI/silc_packet_context_alloc
563  *
564  * SYNOPSIS
565  *
566  *    SilcPacketContext *silc_packet_context_alloc();
567  *
568  * DESCRIPTION
569  *
570  *    Allocates a packet context. Packet contexts are used when 
571  *    packets are assembled and parsed. The context is freed by the
572  *    silc_packet_context_free function.
573  *
574  ***/
575 SilcPacketContext *silc_packet_context_alloc(void);
576
577 /****f* silccore/SilcPacketAPI/silc_packet_context_dup
578  *
579  * SYNOPSIS
580  *
581  *    SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx);
582  *
583  * DESCRIPTION
584  *
585  *    Duplicates the packet context. It actually does not duplicate
586  *    any data, instead a reference counter is increased.
587  *
588  ***/
589 SilcPacketContext *silc_packet_context_dup(SilcPacketContext *ctx);
590
591 /****f* silccore/SilcPacketAPI/silc_packet_context_free
592  *
593  * SYNOPSIS
594  *
595  *    void silc_packet_context_free(SilcPacketContext *ctx);
596  *
597  * DESCRIPTION
598  *
599  *    Frees the packet context. The context is actually freed when the
600  *    reference counter hits zero.
601  *
602  ***/
603 void silc_packet_context_free(SilcPacketContext *ctx);
604
605 #endif