Network Byte order not reached reading from a sock RAW

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Fri Oct 12 23:30:19 EDT 2012


On Oct 12, 2012 9:36 PM, "Pietro Paolini" <P.Paolini at ext.adbglobal.com>
wrote:
>
> Hello,
>
> I am struggling with the byte order question on a x86_32 arch, I am doing
some modifications on a program which actually works fine on a MIPS arch.
>
> I do a reading from a RAW socket in this way:
>
>         /* Configure socket */
>         if ((fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP)) < 0) {
>                 perror("Error on socket creation, exit");
>                 exit(1);
>         }
>           ....
>         if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, (void *)&va,
sizeof(va))) {
>                 perror("Error on setsockopt, exit");
>                 exit(1);
>         }
>         va = 0;
>         ....
>         ....
>         struct msghdr mhdr;
>         struct __in_pktinfo *pktinfo = NULL;
>         ...
>         ...
>         nrd = recvmsg(env->mrouter_fd, &mhdr, 0);
>         ...
>         ip = (struct iphdr *)iov.iov_base;
>
> When I print the saddr (or daddr) of the received ip packet it is printed
as host byte order instead of what I am expecting, the network byte order.
I can just use the htonl() family functions for solve the problem but I
would like understand if it is the normal behavior or if there is an issues
on my code, or if the device driver of my NIC can influence the question.

I think you should use ntoh*() functions when accessing data rx'ed from the
n/w. Network byte order is big endian and your host is little endian, so
you'll have convert it to the right order before accessing. You should use
hton*() functions when tx'ing data. CMIIW.

HTH,
-mandeep

> Many thanks,
> Pietro.
>
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20121013/139594d8/attachment.html 


More information about the Kernelnewbies mailing list