this post was submitted on 07 Oct 2024
4 points (100.0% liked)

The C Programming Language

632 readers
6 users here now

Everything related to the C programming language.

founded 5 years ago
MODERATORS
 

Inside <arpa/inet.h>, there are two functions to convert host byte order to network byte order. Why are there two options? I understand that htons() is used for port address related to IPv4 and IPv6, but what use does htonl() serve?

you are viewing a single comment's thread
view the rest of the comments
[–] Cofeiini@sopuli.xyz 1 points 4 hours ago (1 children)

If I understood correctly, the difference is that the returned value is either uint16_t or uint32_t. Otherwise they look the same based on the headers and documentation.

[–] velox_vulnus@lemmy.ml 1 points 3 hours ago (1 children)

But why two different functions? If I recall correctly, port address can be between 0 to 65535, which will fit under uint16_t. What use would uint32_t have? Is there any other use for htonl()?

[–] Cofeiini@sopuli.xyz 2 points 3 hours ago

I don't usually use networking in C or C++, so I can't say for sure. But I did I find this quote that might explain it:

The reasons for having both 16 and 32 bit functions is that IP and TCP headers use a mix of 16-bit fields like port numbers along with 32-bit IPv4 addresses.