During my computer networks lab, I had to read many binary files which contains the packet in IPv4 format. Here is the IPv4 header file format.
The following structure encapsulates all the essential part of the IP header.
struct ip_header {
uint8_t version;
uint8_t header_length;
uint8_t service_type;
uint16_t total_length;
uint16_t identification;
uint8_t flags;
uint16_t fragment_offset;
uint8_t ttl;
uint8_t protocol;
uint16_t checksum;
uint32_t src;
uint32_t dest;
/* other fields for options if needed */
};
One way to read the binary file to get the data in a structured format is to read the file bytes-by-bytes and then specifically typecast each byte field to respective fields for the above structure.
I want to know whether this is the only way to do it, or is there any other nice and magical way to achieve the same. Also, recently I got know that endianness also creates some problem while reading these kinds of files with different sized data types.
Aucun commentaire:
Enregistrer un commentaire