Q & A on Identifying UNIX protocol family connections

Q: I noticed that there is a special Unix protocol family (PF_UNIX) that allows a socket to correspond to a Unix pipe. However, the socket structure sockaddr_in doesn't have a field for a Unix path name (i.e., a string) so I can specify a path. How is this possible?

A: As you point out, because the socket API is quite general, it allows one to use sockets for local communication as well as Internet communication. However, structure sockaddr_in is only used with the Internet protocol family, which is given by the family constant PF_INET. To use a socket for Unix pipes, one must specify the family constant PF_UNIX and then use structure sockaddr_un instead of sockaddr_in. On BSD Unix systems, the structure is found in /usr/include/sys/un.h and has the form:

        struct  sockaddr_un {
                u_char  sun_len;                /* sockaddr len including null */
                u_char  sun_family;             /* AF_UNIX */
                char    sun_path[104];          /* path name (gag) */
        };

As you can see, the Unix form of the structure includes a provision for a path name.