Big/Little-endian confusion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

I'm currently doing some (newbie) programming in a
kernel module in C, for Red Hat 7.0 on an Intel based
machine. The kernel module is concerned with modifying
outgoing network packages. One simple method, i.e.,
modify_packet(struct sk_buff *skb), is called each
time a packet is sent. I've tried printing out certain
areas of the IP-header but I'm getting confused.

When I do a:

   printk("Source Address: %x\n", skb->nh.iph->saddr);

I receive the output:
Source Address: d93833f

That source address, translated to decimal, is
13.147.131.63. Since I'm on an Intel Pentium II
machine (little-endian) I must translate this from
big-endian to little-endian? Correct? After some bit
manipulations (ntohl) I get the source address
63.131.147.13. The only thing is that this is _not_ my
correct IP! My correct IP is 147.13.63.131! What am I
doing wrong? Is the endian thing only applied to 16
bits at a time, or what?


Another, more confusing matter is when I try modifying
the source address. I do a:
    skb->nh.iph->saddr = 0x00000000;

My intention is to set the source address to 0.0.0.0.
But what actually happens is that the source address
is set to 147.13.0.0 (only the two last bytes are
zeroed). The two bytes following the source address
are both set to 0 as well, i.e. a sniffer picking up
the package, outputs: ...93 0D 00 00 00 00 ... where
the 4 first bytes is the source address. It seams as
if the code row above operates in memory 16 bits
shifted to the right. How comes? Is this problem
related to big/little-endian? I'm totally lost on this
one.


/
   h_e_w_i

__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux