What is the difference between AF_INET and PF_INET constants?

Posted by Denilson Sá on Stack Overflow See other posts from Stack Overflow or by Denilson Sá
Published on 2010-03-30T23:43:54Z Indexed on 2010/03/30 23:53 UTC
Read the original article Hit count: 515

Filed under:
|
|
|

Looking at examples about socket programming, we can see that some people use AF_INET while others use PF_INET. In addition, sometimes both of them are used at the same example. The question is: Is there any difference between them? Which one should we use?

If you can answer that, another question would be... Why there are these two similar (but equal) constants?


What I've discovered, so far:

The socket manpage

In (Unix) socket programming, we have the socket() function that receives the following parameters:

int socket(int domain, int type, int protocol);

The manpage says:

The domain argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>.

And the manpage cites AF_INET as well as some other AF_ constants for the domain parameter. Also, at the NOTES section of the same manpage, we can read:

The manifest constants used under 4.x BSD for protocol families are PF_UNIX, PF_INET, etc., while AF_UNIX etc. are used for address families. However, already the BSD man page promises: "The protocol family generally is the same as the address family", and subsequent standards use AF_* everywhere.

The C headers

The sys/socket.h does not actually define those constants, but instead includes bits/socket.h. This file defines around 38 AF_ constants and 38 PF_ constants like this:

#define PF_INET     2   /* IP protocol family.  */
#define AF_INET     PF_INET

Python

The Python socket module is very similar to the C API. However, there are many AF_ constants but only one PF_ constant (PF_PACKET). Thus, in Python we have no choice but use AF_INET.

I think this decision to include only the AF_ constants follows one of the guiding principles: "There should be one-- and preferably only one --obvious way to do it." (The Zen of Python)

© Stack Overflow or respective owner

Related posts about c

    Related posts about sockets