I have a binary file where a IP Packet was written. I have to create a Python program that has to validate each field in the IP Packet, so I'm trying to parse each field form the file (Ip Packet). The problem that I have is there are two fields that are 4 bits long (the first 2 fields of the packet), but the smallest data I can unpack (struct.pack) is 1 byte. So I was trying to bit-mask the bits to extract them form the packet, but I have an error indicating that the flow of bits that are have is considered as a string and the AND operation that I'm doing with the bitmask is with integer. Does someone has a clue of how two extract the first and second 4-bits words from a long string of bits?
This is my code so far:
while True:
try:
filename=raw_input('Enter the name of the file where the packet is: ')
break
except EOFError as e:
pass
print "You didn't enter a valid file name"
#Reading packet from file and converting to little endian accordingly
with open(filename, 'rb') as f:
for line in f.readlines():
mask1=0b1111
mask2=0b00001111
bit_hl=line & mask1
bit_v= line & mask2
pk_hl= struct.unpack('@B', bit_hl) #ip_hl
print('\n',pk_hl)
pk_v = struct.unpack('@B', bit_v) #ip_v
print('\n',pk_v)
....
The packet is generated from a C++ program and the structure of the packet is:
struct cip {
uint8_t ip_hl:4, /* both fields are 4 bits */
ip_v:4;
uint8_t ip_tos;
uint16_t ip_len;
uint16_t ip_id;
uint16_t ip_off;
uint8_t ip_ttl;
uint8_t ip_p;
uint16_t ip_sum;
struct in_addr ip_src;
struct in_addr ip_dst;
char head[100];
};
Thank you.