Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between AF_INET and PF_INET constants?

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)

Other info

This forum post links to this old message, which contains some historical information.

like image 963
Denilson Sá Maia Avatar asked Mar 30 '10 23:03

Denilson Sá Maia


People also ask

What does Af_inet mean?

AF_INET refers to Address from the Internet and it requires a pair of (host, port) where the host can either be a URL of some particular website or its address and the port number is an integer.

What is the use of Af_inet?

This address family provides interprocess communication between processes that run on the same system or on different systems. Addresses for AF_INET sockets are IP addresses and port numbers. You can specify an IP address for an AF_INET socket either as an IP address (such as 130.99.

Where is Af_inet defined?

The families supported are AF_INET and AF_INET6, which is the Internet domain, and AF_UNIX, which is the local socket domain. These constants are defined in the sys/socket. h include file. The type parameter specifies the type of socket created.

Which pair is used for Af_inet address family?

A pair (host, port) is used for the AF_INET address family, where host is a string representing either a hostname in internet domain notation like 'daring.cwi.nl' or an IPv4 address like '100.50. 200.5' , and port is an integer.


1 Answers

I think the Wikipedia notes on this sum it up pretty well:

The original design concept of the socket interface distinguished between protocol types (families) and the specific address types that each may use. It was envisioned that a protocol family may have several address types. Address types were defined by additional symbolic constants, using the prefix AF_ instead of PF_. The AF_-identifiers are intended for all data structures that specifically deal with the address type and not the protocol family. However, this concept of separation of protocol and address type has not found implementation support and the AF_-constants were simply defined by the corresponding protocol identifier, rendering the distinction between AF_ versus PF_ constants a technical argument of no significant practical consequence. Indeed, much confusion exists in the proper usage of both forms.

Even if someone came up with a reason to have a difference today, they'd have to come up with new identifiers or so much stuff would break...

like image 184
Michael Burr Avatar answered Oct 20 '22 15:10

Michael Burr