The unix domain sockets address structure is defined as:
struct sockaddr_un {
sa_family_t sun_family; /* AF_UNIX */
char sun_path[108]; /* pathname */
};
And I see 2 ways of calculating the size, which later on is passed to bind
- From APUE and http://beej.us/guide/bgipc/html/multi/unixsock.html
//beej's guide
struct sockaddr_un local;
int len;
len = strlen(local.sun_path) + sizeof(local.sun_family);
bind(s, (struct sockaddr *)&local, len);
//APUE
struct sockaddr_un un;
size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path);
bind(fd, (struct sockaddr *)&un, size)
- The Linux Progamming Interface
const char *SOCKNAME = "/tmp/mysock";
struct sockaddr_un addr;
addr.sun_family = AF_UNIX; /* UNIX domain address */
strncpy(addr.sun_path, SOCKNAME, sizeof(addr.sun_path) - 1);
bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un))
Which way is correct? (Maybe both are correct?)