how struck.pack() is used in creation packets

2.8k Views Asked by At

i encounter this line in a program for creating icmp ping packets in python

header = struct.pack("bbHHh", ICMP_ECHO_REQUEST, 0, my_checksum, ID, 1)

what does "bbHHh" means here

2

There are 2 best solutions below

0
On BEST ANSWER

The documentation is here: https://docs.python.org/2/library/struct.html - it's a format string. Your particular example means the equivalent of this in C:

 struct Foo {
   signed char a;
   signed char b;
   unsigned short c;
   unsigned short d;
   short e;
 }
0
On

Check here: https://docs.python.org/2/library/struct.html#format-characters

it means that

ICMP_ECHO_REQUEST is a signed char -> integer in python
0 the same
my_checksum is unsigned short -> integer in python
ID the same
h is a short -> integer in python