| Start | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Manually Converting C Structs to Pack Syntax If you're ever dealing with C Binary API's from Perl Code, via the For instance, if you were ever dealing with some function that expected a You do a dance with So its a (signed) int And it takes 8 bytes. So 64bit signed int. And I'm on a 64Bit Processor. =) Perldoc So to pack a timespec: And to unpack a timespec: Now you can just use those functions instead. |
||||||||||||||||||
| Constructing an IPv4 header Sometimes you have to deal with structures defined in terms of C data types from Perl. One such application is the creation of raw network packets, in case you want to do something fancier than what the regular socket API has to offer. This is just what The obligatory part of an IP header (opens new window) is 20 octets (AKA "bytes") long. As you can see behind this link, source and destination IP address make up the last two 32-bit values in the header. Among the other fields are some with 16 bits, some with 8 bits, and a few smaller chunks between 2 and 13 bits. Assuming we have the following variables to stuff into our header: Note that three fields from the header are missing:
We could try and use bit operations to construct e.g. the first 32 bits: This approach only works up to the size of an integer though, which is usually 64 bits but can be as low as 32. Worse, it depends on the CPU's endianness (opens new window) so it will work on some CPUs and fail on others. Let's try The template first specifies That's the first 32 bits of the header. The rest can be built similarly:
So the complete call to pack an IPv4 header would be: |
Read Article
https://develop.horje.com/learn/1434/reference