LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-08-2006, 05:41 PM   #1
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Rep: Reputation: 15
raw sockets and byte swapping


I am working on a raw socket ICMP message receiver and I am having a bit of trouble with knowing when to byte swap. I already know that any field longer than a byte in the IP header have to be swapped from network byte order to hosts byte order. The thing that confuses me is that the ICMP header appears to be byte swapped by word. Does that mean all the data in the IP payload is byte swapped by word? Basically the way I found out that the ICMP header was getting swapped was that the Code and Type are coming up in the wrong place in the destination unknown messages. Any help you guys could throw my way would be ... helpful.
 
Old 12-08-2006, 09:24 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Just look at the RFC, and use htons/ntohs (16-bit) and htonl/ntohl (32-bit) as needed.

For example, here is RFC792:
http://www.ietf.org/rfc/rfc792.txt
 
Old 12-11-2006, 08:17 AM   #3
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Original Poster
Rep: Reputation: 15
I have already looked into the RFC for both IP and ICMP. Neither mention why the header for ICMP is being byte swapped by word. Neighter RFC mentiones byte swapping in the case I mentioned above. IP doesn't mention byte swapping payload data, only that in multi-byte numbers have the most signifigant bytes first. And of ICMP doesn't mention byte order at all.
http://www.faqs.org/rfcs/rfc791.html
http://www.faqs.org/rfcs/rfc792.html

Last edited by shamgar03; 12-11-2006 at 08:23 AM.
 
Old 12-11-2006, 11:11 AM   #4
shamgar03
Member
 
Registered: Jul 2004
Distribution: Gentoo, CentOS
Posts: 92

Original Poster
Rep: Reputation: 15
Fixed. I'm sorry, my ignorance of sockets caused me to place the blame on unknown bytes being swapped when in fact my IP header struct had 1 short as a long which resulted in a shift in the entire structure. As a result, when I byte swapped the IP addresses, I was really byte swapping the first 4 bytes of my header.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Raw Sockets Srikanth0210 Programming 2 12-05-2005 03:22 AM
Pblm in Raw Sockets fpfernando Linux - Software 1 10-08-2005 01:04 AM
byte swapping for floats hypnoticSpectre Programming 1 11-08-2004 10:48 PM
raw sockets and C wrongman Programming 3 05-04-2004 02:17 PM
raw sockets BashTin Programming 1 06-07-2003 06:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration