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