所以我有以下代码用于unix套接字初始化
#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));
我从索引地址开始memcpy
份IETADM_NAMESPACE,即&addr.sun_path + 1
。
我的问题是关于+ 1
expr的 &addr.sun_path + 1
部分。
为什么地址增加并且字符串被复制到那里而不仅仅是
&addr.sun_path
?
答案 0 :(得分:3)
根据 man-page ,有三种类型的地址可以通过sockaddr_un
结构区分:路径名,未命名和抽象。
您提供的代码显示,在memcpy
之后,由于之前的sun_path
,'\0'
成员的第一个字节将为memset
。引用手册页的相关部分:
* 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's 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.
答案 1 :(得分:1)
man 7 unix有答案:
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's 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.