Some messages have one or two length-value information elements. The is
no IE type included in the message. These information elements are
mandatory, so their actual IE type is known. The improved parse_tlv()
function allows to parse zero, one, or two length-value elements.
(Andreas Eversberg)
diff --git a/src/tlv_parser.c b/src/tlv_parser.c
index fe6d289..e835f95 100644
--- a/src/tlv_parser.c
+++ b/src/tlv_parser.c
@@ -13,16 +13,47 @@
return 0;
}
+/* dec: output: a caller-allocated pointer to a struct tlv_parsed,
+ * def: input: a structure defining the valid TLV tags / configurations
+ * buf: input: the input data buffer to be parsed
+ * buf_len: input: the length of the input data buffer
+ * lv_tag: input: an initial LV tag at the start of the buffer
+ * lv_tag2: input: a second initial LV tag following lv_tag
+ */
int tlv_parse(struct tlv_parsed *dec, const struct tlv_definition *def,
- const u_int8_t *buf, int buf_len)
+ const u_int8_t *buf, int buf_len, u_int8_t lv_tag,
+ u_int8_t lv_tag2)
{
u_int8_t tag, len = 1;
- const u_int8_t *pos;
+ const u_int8_t *pos = buf;
int num_parsed = 0;
memset(dec, 0, sizeof(*dec));
- for (pos = buf; pos < buf+buf_len; pos += len) {
+ if (lv_tag) {
+ if (pos > buf + buf_len)
+ return -1;
+ dec->lv[lv_tag].val = pos+1;
+ dec->lv[lv_tag].len = *pos;
+ len = dec->lv[lv_tag].len + 1;
+ if (pos + len > buf + buf_len)
+ return -2;
+ num_parsed++;
+ pos += len;
+ }
+ if (lv_tag2) {
+ if (pos > buf + buf_len)
+ return -1;
+ dec->lv[lv_tag2].val = pos+1;
+ dec->lv[lv_tag2].len = *pos;
+ len = dec->lv[lv_tag2].len + 1;
+ if (pos + len > buf + buf_len)
+ return -2;
+ num_parsed++;
+ pos += len;
+ }
+
+ for (; pos < buf+buf_len; pos += len) {
tag = *pos;
/* FIXME: use tables for knwon IEI */
switch (def->def[tag].type) {