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) |
| 9 | |
| 10 | static 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 | |
| 19 | static 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 | |
| 28 | static 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 | |
| 34 | static 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 | |
| 42 | static 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 | |
| 48 | static 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 | |
| 54 | static 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 | |
| 60 | static 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 */ |