LinuxQuestions.org
Review your favorite Linux distribution.
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 04-05-2005, 08:25 AM   #1
b123coder
Member
 
Registered: Nov 2004
Location: india
Distribution: Fedora core 1
Posts: 67

Rep: Reputation: 15
does each header fields in packet sent on network uses htons?


hello,
I browse the LINUX kernel source code and found that if any integer is to be sent in any header in a packet of size int(8 bytes) to unsigned long int(32 bytes) it has to be converted using htons and similar thing happens in reverse using ntohs is that true?
Also what if any header wants to send string field of its header in packet to network is there any function or sent it as it is?
 
Old 04-05-2005, 09:33 AM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 47
The functions you're referring to are used to deal with byte order between architectures (or "endian'ness").

On x86, the byte order is order is LSB and on the network it's MSB, so you have to swap the data around or the other hosts on the network won't be able to understand what you're sending.
 
Old 04-05-2005, 09:34 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
htons is indeed always used and is needed for interoperability.
there's no need to process a string, as big and little endians machines share the same representation.
 
Old 04-06-2005, 08:36 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
In certain cases it is needed, but in others it is not. For instance, if you are defining your own custom network protocol, say for a game, you could just define that ints are sent in LSB order. Then only machines that use GSB would need to worry about swapping bytes.

It's like with file formats. For instance TGA files have a couple of fields in the header that are 2 byte shorts, but the spec specifically defines that they will be in LSB order.

Things like the port of the sockaddr_in structure MUST be in GSB order, though. But once you get beyond the necessary TCP/IP headers, it is all just bytes and up to whatever protocol you are using to define what order those bytes are sent/received. (By protocol here, I don't mean TCP/UDP, but higher level protocols like RADIUS, HTTP, FTP, your own custom packets, etc)

Also, an unsigned long is NOT 32 bytes. On a 32-bit CPU, with a 32-bit compiler, it is generally 32 bits, which is 4 bytes. An int is also generally 32 bits on this architecture, a short is generally 16 bits (2 bytes), and an unsigned char 8 bits (1 byte)

Last edited by deiussum; 04-06-2005 at 08:40 AM.
 
  


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
unresolved symbol htons mintong Linux - Software 4 08-03-2005 09:53 PM
access IP header in packet linux_lover2005 Programming 3 04-11-2005 01:02 AM
How to get the IP header of packet going through an interface thanh Programming 1 06-12-2004 12:26 PM
problem with htons() abdullahgee Programming 12 05-25-2004 06:11 PM
Edit packet header? mudvayne Linux - Networking 4 03-28-2003 01:43 PM

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

All times are GMT -5. The time now is 03:37 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