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