does each header fields in packet sent on network uses htons?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
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.
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.