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 | |
Harald Welte | 91b5b0d | 2009-02-06 12:51:39 +0000 | [diff] [blame] | 7 | #include <openbsc/msgb.h> |
| 8 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 9 | #define TLV_GROSS_LEN(x) (x+2) |
| 10 | #define TLV16_GROSS_LEN(x) ((2*x)+2) |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 11 | #define TL16V_GROSS_LEN(x) (x+3) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 12 | |
Harald Welte | 91b5b0d | 2009-02-06 12:51:39 +0000 | [diff] [blame] | 13 | /* TLV generation */ |
| 14 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 15 | static inline u_int8_t *tlv_put(u_int8_t *buf, u_int8_t tag, u_int8_t len, |
| 16 | const u_int8_t *val) |
| 17 | { |
| 18 | *buf++ = tag; |
| 19 | *buf++ = len; |
| 20 | memcpy(buf, val, len); |
| 21 | return buf + len; |
| 22 | } |
| 23 | |
| 24 | static inline u_int8_t *tlv16_put(u_int8_t *buf, u_int8_t tag, u_int8_t len, |
| 25 | const u_int16_t *val) |
| 26 | { |
| 27 | *buf++ = tag; |
| 28 | *buf++ = len; |
| 29 | memcpy(buf, val, len*2); |
| 30 | return buf + len*2; |
| 31 | } |
| 32 | |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 33 | static inline u_int8_t *tl16v_put(u_int8_t *buf, u_int8_t tag, u_int16_t len, |
| 34 | const u_int8_t *val) |
| 35 | { |
| 36 | *buf++ = tag; |
| 37 | *buf++ = len >> 8; |
| 38 | *buf++ = len & 0xff; |
| 39 | memcpy(buf, val, len); |
| 40 | return buf + len*2; |
| 41 | } |
| 42 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 43 | static inline u_int8_t *msgb_tlv16_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int16_t *val) |
| 44 | { |
| 45 | u_int8_t *buf = msgb_put(msg, TLV16_GROSS_LEN(len)); |
| 46 | return tlv16_put(buf, tag, len, val); |
| 47 | } |
| 48 | |
Harald Welte | 702d870 | 2008-12-26 20:25:35 +0000 | [diff] [blame] | 49 | static inline u_int8_t *msgb_tl16v_put(struct msgb *msg, u_int8_t tag, u_int16_t len, |
| 50 | const u_int8_t *val) |
| 51 | { |
| 52 | u_int8_t *buf = msgb_put(msg, TL16V_GROSS_LEN(len)); |
| 53 | return tl16v_put(buf, tag, len, val); |
| 54 | } |
| 55 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 56 | static inline u_int8_t *tv_put(u_int8_t *buf, u_int8_t tag, |
| 57 | u_int8_t val) |
| 58 | { |
| 59 | *buf++ = tag; |
| 60 | *buf++ = val; |
| 61 | return buf; |
| 62 | } |
| 63 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 64 | static inline u_int8_t *tv16_put(u_int8_t *buf, u_int8_t tag, |
| 65 | u_int16_t val) |
| 66 | { |
| 67 | *buf++ = tag; |
| 68 | *buf++ = val >> 8; |
| 69 | *buf++ = val & 0xff; |
| 70 | return buf; |
| 71 | } |
| 72 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 73 | static inline u_int8_t *msgb_tlv_put(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val) |
| 74 | { |
| 75 | u_int8_t *buf = msgb_put(msg, TLV_GROSS_LEN(len)); |
| 76 | return tlv_put(buf, tag, len, val); |
| 77 | } |
| 78 | |
| 79 | static inline u_int8_t *msgb_tv_put(struct msgb *msg, u_int8_t tag, u_int8_t val) |
| 80 | { |
| 81 | u_int8_t *buf = msgb_put(msg, 2); |
| 82 | return tv_put(buf, tag, val); |
| 83 | } |
| 84 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 85 | static inline u_int8_t *msgb_tv16_put(struct msgb *msg, u_int8_t tag, u_int16_t val) |
| 86 | { |
| 87 | u_int8_t *buf = msgb_put(msg, 3); |
| 88 | return tv16_put(buf, tag, val); |
| 89 | } |
| 90 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 91 | static inline u_int8_t *msgb_tlv_push(struct msgb *msg, u_int8_t tag, u_int8_t len, const u_int8_t *val) |
| 92 | { |
| 93 | u_int8_t *buf = msgb_push(msg, TLV_GROSS_LEN(len)); |
| 94 | return tlv_put(buf, tag, len, val); |
| 95 | } |
| 96 | |
| 97 | static inline u_int8_t *msgb_tv_push(struct msgb *msg, u_int8_t tag, u_int8_t val) |
| 98 | { |
| 99 | u_int8_t *buf = msgb_push(msg, 2); |
| 100 | return tv_put(buf, tag, val); |
| 101 | } |
| 102 | |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 103 | static inline u_int8_t *msgb_tv16_push(struct msgb *msg, u_int8_t tag, u_int16_t val) |
| 104 | { |
| 105 | u_int8_t *buf = msgb_push(msg, 3); |
| 106 | return tv16_put(buf, tag, val); |
| 107 | } |
| 108 | |
Harald Welte | 91b5b0d | 2009-02-06 12:51:39 +0000 | [diff] [blame] | 109 | /* TLV parsing */ |
| 110 | |
| 111 | struct tlv_p_entry { |
Harald Welte | e0590df | 2009-02-15 03:34:15 +0000 | [diff] [blame] | 112 | u_int16_t len; |
Harald Welte | 7bc4cbc | 2009-02-21 12:59:22 +0000 | [diff] [blame] | 113 | const u_int8_t *val; |
Harald Welte | 91b5b0d | 2009-02-06 12:51:39 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
Harald Welte | e0590df | 2009-02-15 03:34:15 +0000 | [diff] [blame] | 116 | enum tlv_type { |
| 117 | TLV_TYPE_FIXED, |
| 118 | TLV_TYPE_T, |
| 119 | TLV_TYPE_TV, |
| 120 | TLV_TYPE_TLV, |
| 121 | TLV_TYPE_TL16V, |
| 122 | }; |
| 123 | |
| 124 | struct tlv_def { |
| 125 | enum tlv_type type; |
| 126 | u_int8_t fixed_len; |
| 127 | }; |
| 128 | |
| 129 | struct tlv_definition { |
| 130 | struct tlv_def def[0xff]; |
| 131 | }; |
| 132 | |
Harald Welte | 2fa7934 | 2009-02-14 19:07:10 +0000 | [diff] [blame] | 133 | struct tlv_parsed { |
Harald Welte | 91b5b0d | 2009-02-06 12:51:39 +0000 | [diff] [blame] | 134 | struct tlv_p_entry lv[0xff]; |
| 135 | }; |
| 136 | |
Harald Welte | 7bc4cbc | 2009-02-21 12:59:22 +0000 | [diff] [blame] | 137 | int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def, |
| 138 | const u_int8_t *buf, int buf_len); |
Harald Welte | 2fa7934 | 2009-02-14 19:07:10 +0000 | [diff] [blame] | 139 | |
| 140 | #define TLVP_PRESENT(x, y) ((x)->lv[y].val) |
| 141 | #define TLVP_LEN(x, y) (x)->lv[y].len |
| 142 | #define TLVP_VAL(x, y) (x)->lv[y].val |
Harald Welte | 4b63454 | 2008-12-27 01:55:51 +0000 | [diff] [blame] | 143 | |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 144 | #endif /* _TLV_H */ |