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 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 60 | static 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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 69 | static 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 | |
| 75 | static 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 Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 81 | static 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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 87 | static 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 | |
| 93 | static 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 Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 99 | static 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 Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 106 | #endif /* _TLV_H */ |