Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1 | #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 Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 9 | #define TL16V_GROSS_LEN(x) (x+3) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 10 | |
| 11 | static 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 | |
| 20 | static 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 Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 29 | static 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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 39 | static 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 Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 45 | static 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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 52 | static 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 | |
| 60 | static inline u_int8_t *msgb_tlv_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val) |
| 61 | { |
| 62 | u_int8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 63 | return tlv_put(buf, tag, len, val); |
| 64 | } |
| 65 | |
| 66 | static inline u_int8_t *msgb_tv_put(struct msgb *msg, u_int8_t tag, u_int8_t val) |
| 67 | { |
| 68 | u_int8_t *buf = msgb_put(msg, 2); |
| 69 | return tv_put(buf, tag, val); |
| 70 | } |
| 71 | |
| 72 | static inline u_int8_t *msgb_tlv_push(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val) |
| 73 | { |
| 74 | u_int8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
| 75 | return tlv_put(buf, tag, len, val); |
| 76 | } |
| 77 | |
| 78 | static inline u_int8_t *msgb_tv_push(struct msgb *msg, u_int8_t tag, u_int8_t val) |
| 79 | { |
| 80 | u_int8_t *buf = msgb_push(msg, 2); |
| 81 | return tv_put(buf, tag, val); |
| 82 | } |
| 83 | |
| 84 | #endif /* _TLV_H */ |