blob: 70d0319e8cc5ae8eca1c392563aaa387b9628d25 [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)
Harald Welte702d8702008-12-26 20:25:35 +00009#define TL16V_GROSS_LEN(x) (x+3)
Harald Welte52b1f982008-12-23 20:25:15 +000010
11static inline u_int8_t *tlv_put(u_int8_t *buf, u_int8_t tag, u_int8_t len,
12 const u_int8_t *val)
13{
14 *buf++ = tag;
15 *buf++ = len;
16 memcpy(buf, val, len);
17 return buf + len;
18}
19
20static inline u_int8_t *tlv16_put(u_int8_t *buf, u_int8_t tag, u_int8_t len,
21 const u_int16_t *val)
22{
23 *buf++ = tag;
24 *buf++ = len;
25 memcpy(buf, val, len*2);
26 return buf + len*2;
27}
28
Harald Welte702d8702008-12-26 20:25:35 +000029static inline u_int8_t *tl16v_put(u_int8_t *buf, u_int8_t tag, u_int16_t len,
30 const u_int8_t *val)
31{
32 *buf++ = tag;
33 *buf++ = len >> 8;
34 *buf++ = len & 0xff;
35 memcpy(buf, val, len);
36 return buf + len*2;
37}
38
Harald Welte52b1f982008-12-23 20:25:15 +000039static inline u_int8_t *msgb_tlv16_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int16_t *val)
40{
41 u_int8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len));
42 return tlv16_put(buf, tag, len, val);
43}
44
Harald Welte702d8702008-12-26 20:25:35 +000045static inline u_int8_t *msgb_tl16v_put(struct msgb *msg, u_int8_t tag, u_int16_t len,
46 const u_int8_t *val)
47{
48 u_int8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len));
49 return tl16v_put(buf, tag, len, val);
50}
51
Harald Welte52b1f982008-12-23 20:25:15 +000052static inline u_int8_t *tv_put(u_int8_t *buf, u_int8_t tag,
53 u_int8_t val)
54{
55 *buf++ = tag;
56 *buf++ = val;
57 return buf;
58}
59
Harald Welte4b634542008-12-27 01:55:51 +000060static inline u_int8_t *tv16_put(u_int8_t *buf, u_int8_t tag,
61 u_int16_t val)
62{
63 *buf++ = tag;
64 *buf++ = val >> 8;
65 *buf++ = val & 0xff;
66 return buf;
67}
68
Harald Welte52b1f982008-12-23 20:25:15 +000069static inline u_int8_t *msgb_tlv_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val)
70{
71 u_int8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len));
72 return tlv_put(buf, tag, len, val);
73}
74
75static inline u_int8_t *msgb_tv_put(struct msgb *msg, u_int8_t tag, u_int8_t val)
76{
77 u_int8_t *buf = msgb_put(msg, 2);
78 return tv_put(buf, tag, val);
79}
80
Harald Welte4b634542008-12-27 01:55:51 +000081static inline u_int8_t *msgb_tv16_put(struct msgb *msg, u_int8_t tag, u_int16_t val)
82{
83 u_int8_t *buf = msgb_put(msg, 3);
84 return tv16_put(buf, tag, val);
85}
86
Harald Welte52b1f982008-12-23 20:25:15 +000087static inline u_int8_t *msgb_tlv_push(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val)
88{
89 u_int8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len));
90 return tlv_put(buf, tag, len, val);
91}
92
93static inline u_int8_t *msgb_tv_push(struct msgb *msg, u_int8_t tag, u_int8_t val)
94{
95 u_int8_t *buf = msgb_push(msg, 2);
96 return tv_put(buf, tag, val);
97}
98
Harald Welte4b634542008-12-27 01:55:51 +000099static inline u_int8_t *msgb_tv16_push(struct msgb *msg, u_int8_t tag, u_int16_t val)
100{
101 u_int8_t *buf = msgb_push(msg, 3);
102 return tv16_put(buf, tag, val);
103}
104
105
Harald Welte52b1f982008-12-23 20:25:15 +0000106#endif /* _TLV_H */