Memcpy: starting index of sun_path address for unix socket
So I have code as below to initialize unix socket
#define IETADM_NAMESPACE "IET_ABSTRACT_NAMESPACE"
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_LOCAL;
memcpy((char *) &addr.sun_path + 1, IETADM_NAMESPACE, strlen(IETADM_NAMESPACE));
I got that memcpy
copies the IETADM_NAMESPACE starting from the index address ie &addr.sun_path + 1
.
My question is about the expr part . + 1
&addr.sun_path + 1
Why is the address incremented and the line copied there and not just
&addr.sun_path
?
According to the man-page, there are three types of addresses that can be distinguished using the structure: pathname, unnamed, and abstract. sockaddr_un
The code you provided shows that after the memcpy
member sun_path
will have its first byte as '\0'
from the previous one memset
. Quoting the relevant part of the man page:
* abstract: an abstract socket address is distinguished (from a
pathname socket) by the fact that sun_path[0] is a null byte
('\0'). The socket address in this namespace is given by the
additional bytes in sun_path that are covered by the specified
length of the address structure. (Null bytes in the name have no
special significance.) The name has no connection with filesystem
pathnames. When the address of an abstract socket is returned,
the returned addrlen is greater than sizeof(sa_family_t) (i.e.,
greater than 2), and the name of the socket is contained in the
first (addrlen - sizeof(sa_family_t)) bytes of sun_path. The
abstract socket namespace is a nonportable Linux extension.
source to share
man 7 unix has an answer:
Address format
[...]
abstract: an abstract socket address is distinguished (from a
pathname socket) by the fact that sun_path[0] is a null byte
('\0'). The socket address in this namespace is given by the
additional bytes in sun_path that are covered by the specified
length of the address structure. (Null bytes in the name have no
special significance.) The name has no connection with filesystem
pathnames. When the address of an abstract socket is returned,
the returned addrlen is greater than sizeof(sa_family_t) (i.e.,
greater than 2), and the name of the socket is contained in the
first (addrlen - sizeof(sa_family_t)) bytes of sun_path. The
abstract socket namespace is a nonportable Linux extension.
source to share