Friday, August 5, 2011

Domain Argument of the Socket

Specifying the Domain of a Socket

You read that for the socketpair(2) function, the domain argument must be the value AF_LOCAL or AF_UNIX (remember, these were equivalent). Then you might have noticed that there was a socket(2) function called that gave an AF_INET value in the domain argument there (although we haven't properly covered socket(2) just yet). In these and other cases, you've probably concluded that the domain argument in some way specifies the protocol to be used.
Technically, the domain argument actually specifies the protocol family to be used rather than a specific protocol. A bit of history is required, by way of explanation. The BSD socket interface went through a series of evolutionary changes. In early socket implementations, it was imagined that there might be the following breakdowns possible when specifying a socket:
  • One or more protocols of a protocol family
  • One or more address formats for one or more protocols
Given this perceived possibility, the original socket interface provided ways to define the following before a socket was created:
  1. The protocol family to use. For example, the C macro constant PF_INET would indicate that the Internet IP family of protocols should be used.
  2. The specific protocol within the family to use. For example, the macro IPPROTO_UDP would specify that the UDP protocol should be used.
  3. The address family to use. For example, the macro constant AF_INET would indicate that an Internet IP address is to be used with the specified protocol.
Experience later showed that there is never more than one address format defined for a given family of protocols. The result of this is the inherited modern socket interface Linux uses today. What does this mean to you? It means simply that the socket interface accepts either the PF_INET macro or the AF_INET macro to specify the domain to be used.

Choosing PF_INET or AF_INET

Standards are encouraging that the PF_INET macro be used in preference over the AF_INET macro to specify the domain (that is, the protocol family to use). However, such a large amount of
existing C program code has conformed to the older convention, and many programmers have resisted making the change.

In the previous post, the AF_UNIX, AF_LOCAL, and AF_INET macros were used in the domain argument of the socketpair(2) and the socket(2) function calls. This works because of AF_UNIX=PF_UNIX and AF_INET=PF_INET and so on. However, this might not always be the case in the future. To foster new standards and practices, the examples and illustrations in the remainder of this book will follow the new standard. What this means is that socketpair(2) when called will be given PF_LOCAL instead of AF_LOCAL for the domain argument value. Likewise, calls to socket(2) will make a similar shift in practice.

See an Example using AF_INET here

Note that the AF_UNIX constant is defined to be the same value as the PF_UNIX constant under Linux. The same applies to all other constants of this series (AF_INET is the same as PF_INET, for example). You will see valid program code that uses the AF_INET value for the domain argument of the socketpair(2) and socket(2) functions. Still other programs will use the PF_INET value
instead. This is confusing, but presently, both are valid.
The best advice seems to be that the PF_LOCAL series of macros should be used in the socketpair(2) and socket(2) domain argument. The AF_LOCAL series of macros should be used when initializing the socket address structures.

Using the PF_LOCAL and AF_LOCAL Macros

You should note that socket addresses themselves should still be initialized with the correct address family constants such as AF_INET. The PF_INET chooses the protocol family in the socket
creation function, whereas the AF_INET macro chooses an address family in the socket address structure. The following code shows how the C macro constants PF_LOCAL and AF_LOCAL are applied:

See an example using AF_LOCAL with socketpair here


int z; /* Status Code */
int sp[2]; /* Socket Pair */
struct sockaddr_un adr_unix; /* AF_LOCAL */
z = socketpair (PF_LOCAL,SOCK_STREAM,0,sp);
. . .
adr_unix.sun_family = AF_LOCAL;

The PF_LOCAL macro is used in the socketpair(2) function to specify the protocol family to use in the domain argument. Note that when the socket address is established in structure adr_unix, the AF_LOCAL address family macro is used instead.

No comments:

Post a Comment