Jump to content
  • Advertisement
Sign in to follow this  
Krohm

Why is sockaddr' s_addr member in network order?

This topic is 3772 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm not 100% sure why sockaddr_in fields (such as sin_port and sin_addr) must be in network order. After all, they look used in local scopes only. Couldn't they just convert by themselves? Maybe this is a cheap optimization from the past to avoid re-swapping the bytes when using the structures?

Share this post


Link to post
Share on other sites
Advertisement
I can think of three somewhat subtle reasons.

First, the sockets layer doesn't know about sockaddr_in -- it just knows about sockaddr, which may be any kind of address (you know, Banyan Vines, Novell Netware, that kind of thing -- or even IPv6).

Second, the name servers don't know the byte order of the destination machine. Thus, when you ask for the address(es) of a given name, the reply will always be in network order.

Third, the network driver layer also doesn't know about IP addresses (vs other addresses), as it only does MAC addresses (or VC identifiers, or whatever). Thus, the swapping would have to happen somewhere midway through the network stack. Having different versions of packets floating around with different byte orders sounds like a source of bugs to me -- probably better to always keep it in network byte order.

Share this post


Link to post
Share on other sites
Yeah, I can see the third one is really a Good Thing for anyone developing a TCPIP stack. I understand they had no other "sane" choice.
Thank you!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!