blob: 4c007725d8bb4b530c94e0e9fa5a947290f74ef3 [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001#ifndef _TLV_H
2#define _TLV_H
3
4#include <sys/types.h>
5#include <string.h>
6
7#define TLV_GROSS_LEN(x) (x+2)
8#define TLV16_GROSS_LEN(x) ((2*x)+2)
9
10static inline u_int8_t *tlv_put(u_int8_t *buf, u_int8_t tag, u_int8_t len,
11 const u_int8_t *val)
12{
13 *buf++ = tag;
14 *buf++ = len;
15 memcpy(buf, val, len);
16 return buf + len;
17}
18
19static inline u_int8_t *tlv16_put(u_int8_t *buf, u_int8_t tag, u_int8_t len,
20 const u_int16_t *val)
21{
22 *buf++ = tag;
23 *buf++ = len;
24 memcpy(buf, val, len*2);
25 return buf + len*2;
26}
27
28static inline u_int8_t *msgb_tlv16_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int16_t *val)
29{
30 u_int8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
31 return tlv16_put(buf, tag, len, val);
32}
33
34static inline u_int8_t *tv_put(u_int8_t *buf, u_int8_t tag,
35 u_int8_t val)
36{
37 *buf++ = tag;
38 *buf++ = val;
39 return buf;
40}
41
42static inline u_int8_t *msgb_tlv_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val)
43{
44 u_int8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
45 return tlv_put(buf, tag, len, val);
46}
47
48static inline u_int8_t *msgb_tv_put(struct msgb *msg, u_int8_t tag, u_int8_t val)
49{
50 u_int8_t *buf = msgb_put(msg, 2);
51 return tv_put(buf, tag, val);
52}
53
54static inline u_int8_t *msgb_tlv_push(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val)
55{
56 u_int8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
57 return tlv_put(buf, tag, len, val);
58}
59
60static inline u_int8_t *msgb_tv_push(struct msgb *msg, u_int8_t tag, u_int8_t val)
61{
62 u_int8_t *buf = msgb_push(msg, 2);
63 return tv_put(buf, tag, val);
64}
65
66#endif /* _TLV_H */