I need to convert one C structure into TLV format and send it across. Can someone help me with same? My structure is a really nested structure with lot of in-directions (pointers) to different structures and lot of void *. So, i am stuck and could use help.
struct a {
char a[100];
char b;
struct b *tag;
struct c *value;
void *data;
};
struct b {
void *data;
int a;
};
struct c {
void *data;
char arr[100];
};
TLV stands for Tag Length Value, and that's exactly what it is. You might have some confusion on this point. Normal usage of this is when you get a large buffer of bytes, perhaps being filled from some serial device, and you need to know what is in that buffer.
Take for example the communication between a SIM card and a phone. There are a number of well defined Tags each corresponding to a type of message that will be decoded. (These are all defined in specification such as ISO 7816-4) and for example if you want to write to a binary file in the SIM card you need to tell the SIM card how many bytes of data you want to send it. So you'd construct some message like:
So in this case we used TLV to send a buffer full of bytes, the receiving device (SIM card in this case) will parse the Tag, known to expect the Length then know how many bytes of "Value" are coming before the transmission is complete. Note this isn't a full true TLV because each piece of data doesn't have it's own TL some are just known (such as those
00
's between the "tag" and "length", those are parameters and they're set to be 1 byte and always follow the instruction is it's not needed to have a tag or length)So that's the overview. Now where does that leave us with your issue? First, as I hope you can see now, we need to know what is going to be Tagged. That depends on who is expecting the data, this is something you should know. Looking at your problem I'd think it was something like this:
In order of Client B to be able to read these values we need to define the Tags:
Ideally since you have embedded data within each structure you would have sub tags as well:
So each of your structs would be filled with data as normal then when you go to send the data you'd deconstruct the values into a buffer. The following pseudo code gives you the idea
So when Client B gets the large buffer of data they can construct their own local
struct a
,struct b
, andstruct c
then parse out the fields and populate.