]> git.sur5r.net Git - ngadmin/blob - raw/include/nsdp/attr.h
Raw: refactor attribute encoding and decoding
[ngadmin] / raw / include / nsdp / attr.h
1
2 #ifndef DEF_ATTR
3 #define DEF_ATTR
4
5
6 #include <stdlib.h>
7 #include <arpa/inet.h>
8
9 #include <nsdp/list.h>
10
11
12
13 struct attr {
14         unsigned short attr;    /* attribute code */
15         unsigned short size;    /* attribute size */
16         void *data;             /* attribute data */
17 };
18
19
20
21 struct attr* newAttr (unsigned short attr, unsigned short size, void *data);
22
23
24 static inline struct attr* newEmptyAttr (unsigned short attr)
25 {
26         return newAttr(attr, 0, NULL);
27 }
28
29
30 static inline struct attr* newByteAttr (unsigned short attr, unsigned char value)
31 {
32         char *v = malloc(sizeof(char));
33         
34         *v = value;
35         
36         return newAttr(attr, sizeof(char), v);
37 }
38
39
40 static inline struct attr* newShortAttr (unsigned short attr, short value)
41 {
42         short *v = malloc(sizeof(short));
43         
44         *v = value;
45         
46         return newAttr(attr, sizeof(short), v);
47 }
48
49
50 static inline struct attr* newIntAttr (unsigned short attr, int value)
51 {
52         int *v = malloc(sizeof(int));
53         
54         *v = value;
55         
56         return newAttr(attr, sizeof(int), v);
57 }
58
59
60 static inline struct attr* newAddrAttr (unsigned short attr, struct in_addr value)
61 {
62         struct in_addr *v = malloc(sizeof(struct in_addr));
63         
64         *v = value;
65         
66         return newAttr(attr, sizeof(struct in_addr), v);
67 }
68
69
70 void freeAttr (struct attr *at);
71
72
73 void filterAttributes (List *attr, ...);
74
75
76 int encodeAttr (struct attr *at);
77
78
79 int decodeAttr (struct attr *at);
80
81
82 #endif
83