Problems with Enterianism on Raspberry Pi

I just started with some C ++ network software programs and compiled my raspberry Pi itself (without cross-compiling). It makes everything a little endian.

After creating my IP header, I calculate the IP checksum, but it always turned out incorrectly (for example, here http://www.thegeekstuff.com/2012/05/ip-header-checksum/ ).

By flipping gdb, I handled my problem to the order of the first 32 bits in the IP header. The example uses 0x4500003C , which means version 4 ( 0x4 ), IHL 5 ( 0x5 ), TOS 0 ( 0x00 ) and tot_length 60 ( 0x003C ). So I installed my package the same.

 struct iphdr* ip; // Also some mallocing ip->version = 4; ip->ihl = 5; ip->tos = 0; ip->tot_len = 60; 

Now in gdb I reviewed the first 32 bits, expecting 0x3C000045 due to endianness, but instead I get the following:

 (gdb) print ip $1 = (iphdr *) 0x11018 (gdb) x/1xw 0x11018 0x11018: 0x003c0045 

The first 16 bits have a small value ( 0x0045 ), but the second one containing a decimal 60 seems to have a large size ( 0x003C )!

What does it give? I'm crazy? Am I completely mistaken in byte order inside structures? (This is a definite opportunity)

+6
source share
2 answers

Here is the order of the fields within the structure, and then the byte order in the multibyte field.

0x003C is not finite at all, it is a hexadecimal value for 60. Sure, it is stored in memory with some degree of accuracy, but the order you used to write the field and the order you used to read it are the same - both are native byte Raspberry Pi order, and they are canceled.

Usually you need to write:

 ip->tot_len = htons(60); 

when saving a 16-bit field in a packet. There is also htonl for 32-bit fields and ntohs and ntohl for reading fields from network packets.

+7
source

ARM architecture can work with both little and great enthusiasm, but the Android platform is little oriented.

+2
source

Source: https://habr.com/ru/post/926713/


All Articles