Forming Local Addresses
This
 address format is used by sockets that are local to your host (your PC 
running Linux). For example, when you queue a file to be printed using 
the lpr(1) command, it uses a local socket to
communicate
 with the spooling service on your PC. While it is also possible to use 
TCP/IP for local communication, it turns out that this is less 
efficient. Traditionally, the local address family has been referred to 
as the AF_UNIX domain (for example, a
UNIX
 socket address). This is because these addresses use local UNIX 
filenames to act as the socket name. Linux kernels 2.2.0 and later 
support abstract socket names, which you'll learn about shortly.
The
 structure name for AF_LOCAL or AF_UNIX addresses is sockaddr_un. This 
structure is defined by including the following statement in your C 
program:
Listing 2.2: The sockaddr_un Address Structure
#include <sys/un.h>
struct sockaddr_un {
sa_family_t sun_family;   /* Address Family */
char sun_path[108];       /* Pathname */
};
The
 structure member sun_family must have the value AF_LOCAL or AF_UNIX 
assigned to it (these macros represent the same value, though usage of 
AF_LOCAL is now being encouraged). This value indicates the structure is
 formatted according to the structure sockaddr_un rules. The structure 
member sun_path[108] contains a valid UNIX pathname. There is no null 
byte required at the end of the character array, as you will find out.
CAUTION
Note
 that the total size for the sockaddr_un address is much larger than the
 16 bytes of the generic address structure. Make sure you allocate 
sufficient storage to accommodate the AF_LOCAL/AF_UNIX address if you 
are working with multiple address families within your code. In the 
further coming  sections, you will learn how to initialize an AF_LOCAL 
address and define its length.
TIP
Information about local socket addresses can be found in the unix(4) man page.
Forming Traditional Local Addresses
The
 address name space for traditional local addresses are file system 
pathnames. A process might name its local socket by any valid pathname. 
To be valid, however, the process naming the socket must have access to 
all directory components of the pathname and  permissions to create the 
final socket object in the directory named. Figure 2.2 shows the 
physical layout of a socket /dev/printer, which you may have active on 
your system. The lpd printer daemon listens on this local socket 
address.
Figure 2.2:
Here is the AF_LOCAL/AF_UNIX Socket Address for /dev/printer.
Notice
 that the first two bytes indicate the address type of AF_LOCAL. The 
remaining bytes are the characters /dev/printer with no null byte 
present. Now you'll turn your attention to the C code
 to initialize such an address. Some programmers like to initialize the 
address structure completely to zero before filling it in. This is often
 done using the memset(3) function and is probably a good idea:
struct sockaddr_un uaddr;
memset(&uaddr,0,sizeof uaddr);
This function call will zero out all bytes of the address structure for you.
NOTE
Zeroing
 out the address structure is not required if you properly initialize 
the mandatory address elements. However, it does make debugging easier 
because it eliminates any leftover data that might otherwise remain. In 
this chapter, memset(3) is used to zero the address structures, as a 
demonstration of how it would be done.

 
 
~
ReplyDelete