* Network Byte order not reached reading from a sock RAW
@ 2012-10-12 16:04 Pietro Paolini
2012-10-13 3:30 ` Mandeep Sandhu
0 siblings, 1 reply; 7+ messages in thread
From: Pietro Paolini @ 2012-10-12 16:04 UTC (permalink / raw)
To: kernelnewbies
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.
Many thanks,
Pietro.
^ permalink raw reply [flat|nested] 7+ messages in thread* Network Byte order not reached reading from a sock RAW 2012-10-12 16:04 Network Byte order not reached reading from a sock RAW Pietro Paolini @ 2012-10-13 3:30 ` Mandeep Sandhu 2012-10-15 7:46 ` Pietro Paolini 0 siblings, 1 reply; 7+ messages in thread From: Mandeep Sandhu @ 2012-10-13 3:30 UTC (permalink / raw) To: kernelnewbies On Oct 12, 2012 9:36 PM, "Pietro Paolini" <P.Paolini@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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Network Byte order not reached reading from a sock RAW 2012-10-13 3:30 ` Mandeep Sandhu @ 2012-10-15 7:46 ` Pietro Paolini 2012-10-15 8:28 ` Kumar amit mehta 2012-10-15 8:32 ` Mandeep Sandhu 0 siblings, 2 replies; 7+ messages in thread From: Pietro Paolini @ 2012-10-15 7:46 UTC (permalink / raw) To: kernelnewbies Hello, Thanks for your answer, my question is why when I read from the buffer data is already in host byte order and not in network byte order. Thanks Pietro Paolini. From: Mandeep Sandhu [mailto:mandeepsandhu.chd at gmail.com] Sent: sabato 13 ottobre 2012 05:30 To: Pietro Paolini Cc: kernelnewbies at kernelnewbies.org Subject: Re: Network Byte order not reached reading from a sock RAW On Oct 12, 2012 9:36 PM, "Pietro Paolini" <P.Paolini@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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Network Byte order not reached reading from a sock RAW 2012-10-15 7:46 ` Pietro Paolini @ 2012-10-15 8:28 ` Kumar amit mehta 2012-10-15 8:32 ` Mandeep Sandhu 1 sibling, 0 replies; 7+ messages in thread From: Kumar amit mehta @ 2012-10-15 8:28 UTC (permalink / raw) To: kernelnewbies On Mon, Oct 15, 2012 at 07:46:08AM +0000, Pietro Paolini wrote: > Hello, > Thanks for your answer, my question is why when I read from the buffer data is already in host byte order and not in network byte order. > Thanks > Pietro Paolini. > Hi Pietro, The host doesn't care how the data has travelled over wire, he will store that data in memory according to it's own hardware architecture. As an application programmer, __you__ have to take care of this and to make your job easier, you already have those ntoh* hton* APIs or your POSIX compliant OS. -Amit ^ permalink raw reply [flat|nested] 7+ messages in thread
* Network Byte order not reached reading from a sock RAW 2012-10-15 7:46 ` Pietro Paolini 2012-10-15 8:28 ` Kumar amit mehta @ 2012-10-15 8:32 ` Mandeep Sandhu 2012-10-16 13:12 ` Pietro Paolini 1 sibling, 1 reply; 7+ messages in thread From: Mandeep Sandhu @ 2012-10-15 8:32 UTC (permalink / raw) To: kernelnewbies On Mon, Oct 15, 2012 at 1:16 PM, Pietro Paolini <P.Paolini@ext.adbglobal.com> wrote: > Hello, > Thanks for your answer, my question is why when I read from the buffer data is already in host byte order and not in network byte order. I think the kernel n/w stack already translates the TCP/IP fields (protocol headers) to the correct host order so you don't have to. So when you rx the packet in the userspace its already in the right order. The 'raw' payload in your packet might still be in the n/w byte order and you will have to convert it using the ntoh*() functions (I'm not 100% sure about this though). CMIIW, as I haven't touched sockets in a long time! :) HTH, -mandeep > Thanks > Pietro Paolini. > > > From: Mandeep Sandhu [mailto:mandeepsandhu.chd at gmail.com] > Sent: sabato 13 ottobre 2012 05:30 > To: Pietro Paolini > Cc: kernelnewbies at kernelnewbies.org > Subject: Re: Network Byte order not reached reading from a sock RAW > > > On Oct 12, 2012 9:36 PM, "Pietro Paolini" <P.Paolini@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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Network Byte order not reached reading from a sock RAW 2012-10-15 8:32 ` Mandeep Sandhu @ 2012-10-16 13:12 ` Pietro Paolini 2012-10-17 5:18 ` Mandeep Sandhu 0 siblings, 1 reply; 7+ messages in thread From: Pietro Paolini @ 2012-10-16 13:12 UTC (permalink / raw) To: kernelnewbies >I think the kernel n/w stack already translates the TCP/IP fields (protocol headers) to the >correct host order so you don't have to. So when you rx the packet in the userspace its >already in the right order. Hello, Could you provide the piece(s) of code where kernel does this type of operation ? Of course just suggest me some file where search, I am really curios and I would like understand better this question but at the same time I am unable to find the files where look for. Many thanks, Pietro. -----Original Message----- From: Mandeep Sandhu [mailto:mandeepsandhu.chd at gmail.com] Sent: luned? 15 ottobre 2012 10:33 To: Pietro Paolini Cc: kernelnewbies at kernelnewbies.org Subject: Re: Network Byte order not reached reading from a sock RAW On Mon, Oct 15, 2012 at 1:16 PM, Pietro Paolini <P.Paolini@ext.adbglobal.com> wrote: > Hello, > Thanks for your answer, my question is why when I read from the buffer data is already in host byte order and not in network byte order. The 'raw' payload in your packet might still be in the n/w byte order and you will have to convert it using the ntoh*() functions (I'm not 100% sure about this though). CMIIW, as I haven't touched sockets in a long time! :) HTH, -mandeep > Thanks > Pietro Paolini. > > > From: Mandeep Sandhu [mailto:mandeepsandhu.chd at gmail.com] > Sent: sabato 13 ottobre 2012 05:30 > To: Pietro Paolini > Cc: kernelnewbies at kernelnewbies.org > Subject: Re: Network Byte order not reached reading from a sock RAW > > > On Oct 12, 2012 9:36 PM, "Pietro Paolini" <P.Paolini@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 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Network Byte order not reached reading from a sock RAW 2012-10-16 13:12 ` Pietro Paolini @ 2012-10-17 5:18 ` Mandeep Sandhu 0 siblings, 0 replies; 7+ messages in thread From: Mandeep Sandhu @ 2012-10-17 5:18 UTC (permalink / raw) To: kernelnewbies > Could you provide the piece(s) of code where kernel does this type of operation ? > Of course just suggest me some file where search, I am really curios and I would like understand better this question but at the same time I am unable to find the files where look for. I had a written a Realtek ethernet card driver long time back (its been almost 6yrs since) for my own curiosity. So my memory right now is sketchy! :) You can start off by gong through the n/w device driver for some card (eg: drivers/net/8139too.c) and work your way up the stack (next you can look at net/ipv4/ip_input.c). The basic idea is that when a packet is rx'ed by the n/w device, it creates a sk_buff (socket buffer) and passes this up the stack where it's processed by the various protocol layers (IP, TCP etc). Though I'm not certain if the n/w device driver populates the various skb fields in the n/w or host byte order. Only inspecting the code will tell. HTH, -mandeep > > > Many thanks, > Pietro. > > > -----Original Message----- > From: Mandeep Sandhu [mailto:mandeepsandhu.chd at gmail.com] > Sent: luned? 15 ottobre 2012 10:33 > To: Pietro Paolini > Cc: kernelnewbies at kernelnewbies.org > Subject: Re: Network Byte order not reached reading from a sock RAW > > On Mon, Oct 15, 2012 at 1:16 PM, Pietro Paolini <P.Paolini@ext.adbglobal.com> wrote: >> Hello, >> Thanks for your answer, my question is why when I read from the buffer data is already in host byte order and not in network byte order. > > > The 'raw' payload in your packet might still be in the n/w byte order and you will have to convert it using the ntoh*() functions (I'm not 100% sure about this though). > > CMIIW, as I haven't touched sockets in a long time! :) > > HTH, > -mandeep > > >> Thanks >> Pietro Paolini. >> >> >> From: Mandeep Sandhu [mailto:mandeepsandhu.chd at gmail.com] >> Sent: sabato 13 ottobre 2012 05:30 >> To: Pietro Paolini >> Cc: kernelnewbies at kernelnewbies.org >> Subject: Re: Network Byte order not reached reading from a sock RAW >> >> >> On Oct 12, 2012 9:36 PM, "Pietro Paolini" <P.Paolini@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 ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2012-10-17 5:18 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2012-10-12 16:04 Network Byte order not reached reading from a sock RAW Pietro Paolini 2012-10-13 3:30 ` Mandeep Sandhu 2012-10-15 7:46 ` Pietro Paolini 2012-10-15 8:28 ` Kumar amit mehta 2012-10-15 8:32 ` Mandeep Sandhu 2012-10-16 13:12 ` Pietro Paolini 2012-10-17 5:18 ` Mandeep Sandhu
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.