title: Suggestions on decoding the UDP packets
body: <p>I'm developing a C application to communicate with one of the real control system device. The device uses well defined protocol structure. For example, consider one of the structure that device sends as UDP packet when it is requested:-</p>
  
  <pre><code>typedef struct poll_request_s {
     uint16             poll_number; /* poll number mirrored from the 
                                      * poll request */
     uint16             length;      /* length of the message */
  
     /* till above it was all header section this is ACTUAL DATA */
     attribute_list_t   attr_list;   /* this attribute list contains 
                                      * various attributes */
  } poll_request_t
  </code></pre>
  
  <p>Now, <code>attribute_list_t</code> is a structure that packs various attributes and each attribute in this list is identified by an identifier number which is <code>uint16</code>(16 bits integer). So, in short protocol works something like this:-</p>
  
  <ul>
  <li>You request some data.</li>
  <li>You get the data in form of attribute list.</li>
  <li>Each attribute in attribute list has object identifier.</li>
  <li>You parse each attribute (convert to host byte order) using this object identifier.</li>
  <li>Attribute itself may contain more attribute list. (attribute-inception)</li>
  </ul>
  
  <p>This <code>atrtribute_list_t</code> structure is something like below:-</p>
  
  <pre><code>typdef struct attribute_list_s {
     uint16  length;       /* length of attribute list */
     uint16  count;        /* number of attributes in this list */
     uint8   attrs_data[]; /* another container to hold attributes' data */
  } attribute_list_t
  </code></pre>
  
  <p>Now, <code>attrs_data</code> is only a place holder for holding all the attributes in the list. In fact this <code>attrs_data</code> must be casted to another structure called <code>ava_type</code> to read the attribute information.</p>
  
  <pre><code>typdef struct ava_type_s {
     uint16 attr_id; /* type of attribute */
     uint16 length;  /* length of this attribute 
                      *(this length of single attribute not whole list*/
     uint8  data[];  /* another flexible array to hold data for this 
                      * attribute type */
  }
  </code></pre>
  
  <p>Now, in order to iterate and parse the attributes within this structure, I'm currently using this algorithm(pseudo code below):</p>
  
  <pre><code>uint8* packet = recv_packet(SOCKET);
  /* this is used as packet iterator pointer */
  unit8* packet_ptr = packet;
  parsed_packet_t parsed_packet = malloc(SOME_SIZE);
  .
  . /* do header un-packing */
  .
  /* dont need attribute_list length so skip this 2 bytes */
  parsed_packet += 2;
  
  /* parsed packet do nee count of attributes */
  parsed_packet.attribute_list-&gt;count = NTOHS(packet_ptr);
  packed_ptr += 2; /* skip count */
  
  /* now packet_ptr is pointer to attr_list */
  offset = 0, i = 0;
  for(i = 0 to attr_list-&gt;count) {
     /* cast the attributes' data to ava_type */
     packet_ptr += offset;
  
     /* parse object identifier */
     parsed_packet.attribute_list-&gt;data[i++].object_id = NTOHS(packet_ptr);
     packet_ptr += 2; /* skip 2 bytes */
  
     /* next offset would be attribute length of this packet */
     attribute_length += 2 + NTOHS(packet_ptr);
     packet_ptr += 2;
  
     /* now we are pointer to actual data of i(th) attribute */
  
     /* I've made this parser structure and hash table to
      * retrieve the parser for particular attr_id */
     parser* p = HASH_TABLE(ava_type-&gt;attr_id);
  
     /* parser has function point for network order to host 
      * order and visa-versa */
     p-&gt;ntoh(ava_type, parsed_packet.attribute_list-&gt;data[i]);
  }
  </code></pre>
  
  <p>Now, my questions are:</p>
  
  <ul>
  <li>Even though, I've show <code>HASH_TABLE</code> approach in above algorithm but in actually, I'm using 20 to 30 <code>IF-ELSE</code>. Since C does not have <code>hash table</code> in <code>stdlib</code>. There are about 600 structure in protocol and I do not want to write 600 <code>if-else</code>. What suggestions and methods do you give about parsing these structure as per their <code>attribute_id</code>.</li>
  <li>Another problem is compiler padding in the structures that I've defined. All my structure are defined with <code>flexible array</code> field for data container. Now, when I receive the message, they contain the <code>length</code> for almost every attribute but this <code>length</code> can not be used for <code>malloc..ing</code> my parsed structure as compiler may magically add some paddings bytes and I get short of bytes. I'm usually <code>malloc..ing</code> about <code>lenght + 300</code> bytes for safety purpose. In fact, this looks to me bad memory management practice. Any suggestions on this problem?</li>
  </ul>
  
  <p><code>malloc..ing</code> the structure to parse the received messages is the biggest problem for me so far. I want some memory efficient and fast way to do it?</p>
  
  <p>Also, if you've already done such kind of projects, would you kindly share your approach? Any suggestions or comments to put me in right direction? I want simple design without complicating things unnecessarily.</p>