Added silc_attribute_payload_encode_data, renamed parse_list to
[silc.git] / lib / silccore / silcattrs.h
1 /*
2
3   silcattrs.h 
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2002 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/SILC Attribute Interface
21  *
22  * DESCRIPTION
23  *
24  * Implementation of the Attribute Payload that may be used to send and
25  * retrieve user online precense information in the SILC network.  This
26  * implements the draft-riikonen-precense-attrs draft.
27  *
28  ***/
29
30 #ifndef SILCATTRS_H
31 #define SILCATTRS_H
32
33 /****s* silccore/SilcAttributesAPI/SilcAttributePayload
34  *
35  * NAME
36  * 
37  *    typedef struct SilcAttributePayloadStruct *SilcAttributePayload;
38  *
39  * DESCRIPTION
40  *
41  *    This context is the actual Attribute Payload and is allocated
42  *    by silc_attribute_payload_parse and given as attribute usually to
43  *    all silc_attribute_payload_* functions.  It is freed by the
44  *    silc_attribute_payload_free function.
45  *
46  ***/
47 typedef struct SilcAttributePayloadStruct *SilcAttributePayload;
48
49 /****d* silccore/SilcAttributesAPI/SilcAttribute
50  *
51  * NAME
52  * 
53  *    typedef SilcUInt8 SilcAttribute;
54  *
55  * DESCRIPTION
56  *
57  *    The SilcAttribute type definition and the attributes. The attributes
58  *    listed here are the official attributes defined in the internet
59  *    draft.  They define the contents of the attribute payload and the
60  *    type of the attribute.
61  *
62  * SOURCE
63  */
64 typedef SilcUInt8 SilcAttribute;
65
66 /* All defined attributes.  See the specs for detailed information.  The
67    comment is the structure or data type that must be used with the
68    silc_attribute_get_object function to fetch parsed attribute. */
69 #define SILC_ATTRIBUTE_NONE                     0
70 #define SILC_ATTRIBUTE_USER_INFO                1
71 #define SILC_ATTRIBUTE_SERVICE                  2 /* SilcAttributeObjService */
72 #define SILC_ATTRIBUTE_STATUS_MOOD              3 /* SilcAttributeMood */
73 #define SILC_ATTRIBUTE_STATUS_FREETEXT          4 /* char * (UTF-8 string) */
74 #define SILC_ATTRIBUTE_STATUS_MESSAGE           5 /* SilcAttributeObjMime */
75 #define SILC_ATTRIBUTE_PREFERRED_LANGUAGE       6 /* char * (UTF-8 string) */
76 #define SILC_ATTRIBUTE_PREFERRED_CONTACT        7 /* SilcAttributeContact */
77 #define SILC_ATTRIBUTE_TIMEZONE                 8 /* char * (UTF-8 string) */
78 #define SILC_ATTRIBUTE_GEOLOCATION              9 /* SilcAttributeObjGeo */
79 #define SILC_ATTRIBUTE_DEVICE_INFO              10 /* SilcAttributeObjDevice */
80 #define SILC_ATTRIBUTE_EXTENSION                11 /* SilcAttributeObjMime */
81 #define SILC_ATTRIBUTE_USER_PUBLIC_KEY          12 /* SilcAttributeObjPk */
82 #define SILC_ATTRIBUTE_SERVER_PUBLIC_KEY        13 /* SilcAttributeObjPk */
83 #define SILC_ATTRIBUTE_USER_DIGITAL_SIGNATURE   14 /* SilcAttributeObjPk */
84 #define SILC_ATTRIBUTE_SERVER_DIGITAL_SIGNATURE 15 /* SilcAttributeObjPk */
85 /***/
86
87 /****d* silccore/SilcAttributesAPI/SilcAttributeFlags
88  *
89  * NAME
90  * 
91  *    typedef SilcUInt8 SilcAttributeFlags;
92  *
93  * DESCRIPTION
94  *
95  *    Attribute Payload flags defined by the specification.
96  *
97  * SOURCE
98  */
99 typedef SilcUInt8 SilcAttributeFlags;
100
101 /* All defined flags */
102 #define SILC_ATTRIBUTE_FLAG_NONE          0x00    /* No flags */
103 #define SILC_ATTRIBUTE_FLAG_INVALID       0x01    /* Invalid attribute */
104 #define SILC_ATTRIBUTE_FLAG_VALID         0x02    /* Valid attribute */
105 /***/
106
107 /****d* silccore/SilcAttributesAPI/SilcAttributeMood
108  *
109  * NAME
110  * 
111  *    typedef enum { ... } SilcAttributeMood;
112  *
113  * DESCRIPTION
114  *
115  *    The user mood indicators defined by the specification.  This is
116  *    bit mask.
117  *
118  * SOURCE
119  */
120 typedef enum {
121   SILC_ATTRIBUTE_MOOD_NORMAL      = 0x00000000,   /* normal mood */
122   SILC_ATTRIBUTE_MOOD_HAPPY       = 0x00000001,   /* user feel happy */
123   SILC_ATTRIBUTE_MOOD_SAD         = 0x00000002,   /* user feel sad */
124   SILC_ATTRIBUTE_MOOD_ANGRY       = 0x00000004,   /* user feel angry */
125   SILC_ATTRIBUTE_MOOD_JEALOUS     = 0x00000008,   /* user feel jealous */
126   SILC_ATTRIBUTE_MOOD_ASHAMED     = 0x00000010,   /* user feel ashamed */
127   SILC_ATTRIBUTE_MOOD_INVINCIBLE  = 0x00000020,   /* user feel invincible */
128   SILC_ATTRIBUTE_MOOD_INLOVE      = 0x00000040,   /* user feel in love */
129   SILC_ATTRIBUTE_MOOD_SLEEPY      = 0x00000080,   /* user feel sleepy */
130   SILC_ATTRIBUTE_MOOD_BORED       = 0x00000100,   /* user feel bored */
131   SILC_ATTRIBUTE_MOOD_EXCITED     = 0x00000200,   /* user feel exited */
132   SILC_ATTRIBUTE_MOOD_ANXIOUS     = 0x00000400,   /* user feel anxious */
133 } SilcAttributeMood;
134 /***/
135
136 /****d* silccore/SilcAttributesAPI/SilcAttributeContact
137  *
138  * NAME
139  * 
140  *    typedef enum { ... } SilcAttributeContact;
141  *
142  * DESCRIPTION
143  *
144  *    The defined preferred contact methods defined by the specification.
145  *    This is bit mask.
146  *
147  * SOURCE
148  */
149 typedef enum {
150   SILC_ATTRIBUTE_CONTACT_NONE    = 0x00000000,    /* no specific method */
151   SILC_ATTRIBUTE_CONTACT_EMAIL   = 0x00000001,    /* email preferred */
152   SILC_ATTRIBUTE_CONTACT_CALL    = 0x00000002,    /* phone call preferred */
153   SILC_ATTRIBUTE_CONTACT_PAGE    = 0x00000004,    /* "paging" preferred */
154   SILC_ATTRIBUTE_CONTACT_SMS     = 0x00000008,    /* SMS preferred */
155   SILC_ATTRIBUTE_CONTACT_MMS     = 0x00000010,    /* MMS preferred */
156   SILC_ATTRIBUTE_CONTACT_CHAT    = 0x00000020,    /* chatting preferred */
157 } SilcAttributeContact;
158 /***/
159
160 /****d* silccore/SilcAttributesAPI/SilcAttributeDevice
161  *
162  * NAME
163  * 
164  *    typedef enum { ... } SilcAttributeDevice;
165  *
166  * DESCRIPTION
167  *
168  *    The defined device types defined by the specification.
169  *
170  * SOURCE
171  */
172 typedef enum {
173   SILC_ATTRIBUTE_DEVICE_COMPUTER      = 0,        /* device is computer */
174   SILC_ATTRIBUTE_DEVICE_MOBILE_PHONE  = 1,        /* device is mobile phone */
175   SILC_ATTRIBUTE_DEVICE_PDA           = 2,        /* device is PDA */
176   SILC_ATTRIBUTE_DEVICE_TERMINAL      = 3,        /* device is terminal */
177 } SilcAttributeDevice;
178 /***/
179
180 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_alloc
181  *
182  * SYNOPSIS
183  *
184  *    SilcAttributesPayload
185  *    silc_attribute_payload_alloc(SilcAttribute attribute,
186  *                                 SilcAttributeFlags flags,
187  *                                 void *object,
188  *                                 SilcUInt32 object_size);
189  *
190  * DESCRIPTION
191  *
192  *    Allocates and encodes the attribute indicated by `attribute' and
193  *    returns pointer to the attribute.
194  *
195  *    The `object' must always be the same data type as defined with
196  *    SilcAttribute (see the comments) for all attributes.
197  *
198  ***/
199 SilcAttributePayload silc_attribute_payload_alloc(SilcAttribute attribute,
200                                                   SilcAttributeFlags flags,
201                                                   void *object,
202                                                   SilcUInt32 object_size);
203
204 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_parse
205  *
206  * SYNOPSIS
207  *
208  *    SilcDList
209  *    silc_attribute_payload_parse(const unsigned char *payload,
210  *                                 SilcUInt32 payload_len);
211  *
212  * DESCRIPTION
213  *
214  *    Parses list of Attribute payloads returning list of payloads.
215  *    One entry in the returned list is SilcAttributesPayload.  You
216  *    can produce such a list with silc_attribute_payload_encode
217  *    function.
218  *
219  ***/
220 SilcDList silc_attribute_payload_parse(const unsigned char *payload,
221                                        SilcUInt32 payload_len);
222
223 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_encode
224  *
225  * SYNOPSIS
226  *
227  *    SilcBuffer silc_attribute_payload_encode(SilcBuffer attrs,
228  *                                             SilcAttribute attribute,
229  *                                             SilcAttributeFlags flags,
230  *                                             void *object,
231  *                                             SilcUInt32 object_size);
232  *
233  * DESCRIPTION
234  *
235  *    Encodes one attribute payload into the `attrs' buffer and returns
236  *    pointer to the buffer, which may be different in case the buffer
237  *    was reallocated.  If `attrs' is NULL for first attribute this
238  *    allocates the buffer and returns it.  This can be called multiple
239  *    times to add multiple attributes to the `attrs' buffer.  The `flags'
240  *    indicates the validity of the added attribute.  Returns NULL on
241  *    error.
242  *
243  *    The `object' must always be the same data type as defined with
244  *    SilcAttribute (see the comments) for all attributes.
245  *
246  ***/
247 SilcBuffer silc_attribute_payload_encode(SilcBuffer attrs,
248                                          SilcAttribute attribute,
249                                          SilcAttributeFlags flags,
250                                          void *object,
251                                          SilcUInt32 object_size);
252
253 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_encode_data
254  *
255  * SYNOPSIS
256  *
257  *    SilcBuffer
258  *    silc_attribute_payload_encode_data(SilcBuffer attrs,
259  *                                       SilcAttribute attribute,
260  *                                       SilcAttributeFlags flags,
261  *                                       const unsigned char *data,
262  *                                       SilcUInt32 data_len);
263  *
264  * DESCRIPTION
265  *
266  *    Same function as silc_attribute_payload_encode except the attribute
267  *    is already encoded into `data' of `data_len' bytes in length.
268  *    Encodes the attribute into the `attrs' buffer and returns pointer
269  *    to the buffer, which may be different in case the buffer was
270  *    reallocated.  If `attrs' is NULL for first attribute this allocates
271  *    the buffer and returns it.  Returns NULL on error.
272  *
273  ***/
274 SilcBuffer silc_attribute_payload_encode_data(SilcBuffer attrs,
275                                               SilcAttribute attribute,
276                                               SilcAttributeFlags flags,
277                                               const unsigned char *data,
278                                               SilcUInt32 data_len);
279
280 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_free
281  *
282  * SYNOPSIS
283  *
284  *    void silc_attribute_payload_free(SilcAttributePayload payload);
285  *
286  * DESCRIPTION
287  *
288  *    Frees the Attribute Payload and all data in it.
289  *
290  ***/
291 void silc_attribute_payload_free(SilcAttributePayload payload);
292
293 /****f* silccore/SilcAttributesAPI/silc_attribute_payload_list_free
294  *
295  * SYNOPSIS
296  *
297  *    void silc_attribute_payload_list_free(SilcDList list);
298  *
299  * DESCRIPTION
300  *
301  *    Frees list of Attribute Payloads and all data in them.
302  *
303  ***/
304 void silc_attribute_payload_list_free(SilcDList list);
305
306 /****f* silccore/SilcAttributesAPI/silc_attribute_get_attribute
307  *
308  * SYNOPSIS
309  *
310  *    SilcAttribute silc_attribute_get_attribute(SilcAttributePayload payload);
311  *
312  * DESCRIPTION
313  *
314  *    Return the attribute type from the payload indicated by `payload'.
315  *
316  ***/
317 SilcAttribute silc_attribute_get_attribute(SilcAttributePayload payload);
318
319 /****f* silccore/SilcAttributesAPI/silc_attribute_get_flags
320  *
321  * SYNOPSIS
322  *
323  *    SilcAttributeFlags
324  *    silc_attribute_get_flags(SilcAttributePayload payload);
325  *
326  * DESCRIPTION
327  *
328  *    Return the attribute flags from the payload indicated by `payload'.
329  *
330  ***/
331 SilcAttributeFlags silc_attribute_get_flags(SilcAttributePayload payload);
332
333 /****f* silccore/SilcAttributesAPI/silc_attribute_get_data
334  *
335  * SYNOPSIS
336  *
337  *    const unsigned char *
338  *    silc_attribute_get_data(SilcAttributePayload payload,
339  *                            SilcUInt32 *data_len);
340  *
341  * DESCRIPTION
342  *
343  *    Returns the attribute data from the payload indicated by the `payload'
344  *    The caller must not free the returned data pointer.
345  *
346  ***/
347 const unsigned char *silc_attribute_get_data(SilcAttributePayload payload,
348                                              SilcUInt32 *data_len);
349
350 /* Object structures */
351
352 /****s* silccore/SilcAttributesAPI/SilcAttributesObjService
353  *
354  * NAME
355  * 
356  *    typedef struct { ... } SilcAttributesObjService;
357  *
358  * DESCRIPTION
359  *
360  *    SILC_ATTRIBUTE_SERVICE type object structure.
361  *
362  * SOURCE
363  */
364 typedef struct {
365   SilcUInt32 port;              /* IANA specified service port */
366   char address[256];            /* service address */
367   bool status;                  /* online status (TRUE present in service) */
368 } SilcAttributeObjService;
369 /***/
370
371 /****s* silccore/SilcAttributesAPI/SilcAttributesObjMime
372  *
373  * NAME
374  * 
375  *    typedef struct { ... } SilcAttributesObjMime;
376  *
377  * DESCRIPTION
378  *
379  *    Data type for MIME object as attribute.  The data in the structure
380  *    is valid as long as the payload structure is valid.
381  *
382  * SOURCE
383  */
384 typedef struct {
385   const unsigned char *mime;    /* MIME buffer */
386   SilcUInt32 mime_len;          /* length of the MIME buffer */
387 } SilcAttributeObjMime;
388 /***/
389
390 /****s* silccore/SilcAttributesAPI/SilcAttributesObjGeo
391  *
392  * NAME
393  * 
394  *    typedef struct { ... } SilcAttributesObjGeo;
395  *
396  * DESCRIPTION
397  *
398  *    SILC_ATTRIBUTE_GEOLOCATION type object.  The caller must free the
399  *    strings inside the structure.
400  *
401  * SOURCE
402  */
403 typedef struct {
404   char *longitude;              /* Longitude */
405   char *latitude;               /* Latitude */
406   char *altitude;               /* Altitude */
407   char *accuracy;               /* Accuracy in meters */
408 } SilcAttributeObjGeo;
409 /***/
410
411 /****s* silccore/SilcAttributesAPI/SilcAttributesObjDevice
412  *
413  * NAME
414  * 
415  *    typedef struct { ... } SilcAttributesObjDevice;
416  *
417  * DESCRIPTION
418  *
419  *    SILC_ATTRIBUTE_DEVICE_INFO type object.  The caller must free the
420  *    strings inside the structure.
421  *
422  * SOURCE
423  */
424 typedef struct {
425   SilcAttributeDevice type;     /* device type */
426   char *manufacturer;           /* manufacturer of the device */
427   char *version;                /* device version string */
428   char *model;                  /* device model string */
429   char *language;               /* device language (ISO 639-2/T) */
430 } SilcAttributeObjDevice;
431 /***/
432
433 /****s* silccore/SilcAttributesAPI/SilcAttributesObjPk
434  *
435  * NAME
436  * 
437  *    typedef struct { ... } SilcAttributesObjPk;
438  *
439  * DESCRIPTION
440  *
441  *    Data type for public key, certificate or digital signatures.  The
442  *    caller must free the data inside the structure.
443  *
444  * SOURCE
445  */
446 typedef struct {
447   char *type;                   /* public key/certificate type, NULL
448                                    when contains digital signature. */
449   unsigned char *data;          /* public key/cert/signature data. The
450                                    encoding depends of the `type'. */
451   SilcUInt32 data_len;          /* data length */
452 } SilcAttributeObjPk;
453 /***/
454
455 /****f* silccore/SilcAttributesAPI/silc_attribute_get_object
456  *
457  * SYNOPSIS
458  *
459  *    bool silc_attribute_get_object(SilcAttributePayload payload,
460  *                                   void **object,
461  *                                   SilcUInt32 object_size);
462  *
463  * DESCRIPTION
464  *
465  *    Returns the already parsed attribute object from the payload
466  *    indicated by `payload'.  Copies the data into the `object' which
467  *    must be sent as argument (and must be of correct type and size).
468  *    The `object_size' indicates the size of the `*object' sent.
469  *    Returns TRUE if the `attribute' attribute was found and FALSE
470  *    if such attribute is not present in the `payload', or the `object_size'
471  *    is not sufficient.  See the definition of SilcAttribute for the
472  *    list of attributes and the required object types for attributes.
473  *    You can use silc_attribute_get_attribute to get the SilcAttribute
474  *    type from the `payload'.
475  *
476  ***/
477 bool silc_attribute_get_object(SilcAttributePayload payload,
478                                void **object, SilcUInt32 object_size);
479
480 #endif /* SILCATTRS_H */