Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8021609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:02:15+00:00 2026-06-04T22:02:15+00:00

I’m developing a C application to communicate with one of the real control system

  • 0

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:-

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

Now, attribute_list_t is a structure that packs various attributes and each attribute in this list is identified by an identifier number which is uint16(16 bits integer). So, in short protocol works something like this:-

  • You request some data.
  • You get the data in form of attribute list.
  • Each attribute in attribute list has object identifier.
  • You parse each attribute (convert to host byte order) using this object identifier.
  • Attribute itself may contain more attribute list. (attribute-inception)

This atrtribute_list_t structure is something like below:-

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

Now, attrs_data is only a place holder for holding all the attributes in the list. In fact this attrs_data must be casted to another structure called ava_type to read the attribute information.

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 */
}

Now, in order to iterate and parse the attributes within this structure, I’m currently using this algorithm(pseudo code below):

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->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->count) {
   /* cast the attributes' data to ava_type */
   packet_ptr += offset;

   /* parse object identifier */
   parsed_packet.attribute_list->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->attr_id);

   /* parser has function point for network order to host 
    * order and visa-versa */
   p->ntoh(ava_type, parsed_packet.attribute_list->data[i]);
}

Now, my questions are:

  • Even though, I’ve show HASH_TABLE approach in above algorithm but in actually, I’m using 20 to 30 IF-ELSE. Since C does not have hash table in stdlib. There are about 600 structure in protocol and I do not want to write 600 if-else. What suggestions and methods do you give about parsing these structure as per their attribute_id.
  • Another problem is compiler padding in the structures that I’ve defined. All my structure are defined with flexible array field for data container. Now, when I receive the message, they contain the length for almost every attribute but this length can not be used for malloc..ing my parsed structure as compiler may magically add some paddings bytes and I get short of bytes. I’m usually malloc..ing about lenght + 300 bytes for safety purpose. In fact, this looks to me bad memory management practice. Any suggestions on this problem?

malloc..ing 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?

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:02:16+00:00Added an answer on June 4, 2026 at 10:02 pm

    I strongly recommend you don’t use C structs to define your network protocol. C struct layouts are dependent on:

    1. The hardware
    2. The compiler
    3. The compiler version
    4. The source code
    5. The #pragmas embedded in the source code, if any
    6. The compiler options in effect when the source code was compiled.

    Use XDR or something that will give you a standard format. You should be able to define exactly what you have now in XDR and have all the encoding and decoding done for you automatically.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.