Unix Network Programming Episode 20

Socket地址结构解析
本文深入解析了socket地址结构,特别是sockaddr_storage结构,探讨其如何提供最严格的对齐要求及足够的空间来容纳系统支持的任何socket地址类型。文章对比了不同socket地址结构的特点,如固定长度与可变长度,并解释了在向内核传递socket地址结构时,如何正确处理长度字段。
struct sockaddr_storage {
	uint8_t ss_len; /* length of this struct (implementation dependent) */
	sa_family_t ss_family; /* address family: AF_xxx value */
	/* implementation-dependent elements to provide:
	* a) alignment sufficient to fulfill the alignment requirements of
	* all socket address types that the system supports.
	* b) enough storage to hold any type of socket address that the
	* system supports.
	*/
};

The storage socket address structure: sockaddr_storage.

The sockaddr_storage type provides a generic socket address structure that is different from struct sockaddr in two ways:

a. If any socket address structures that the system supports have alignment requirements, the sockaddr_storage provides the strictest alignment requirement.
b. The sockaddr_storage is large enough to contain any socket address structure that the system supports.

Comparison of Socket Address Structures

Two of the socket address structures are fixed-length, while the Unix domain structure and the datalink structure are variable-length. To handle variable-length structures, whenever we pass a pointer to a socket address structure as an argument to one of the socket functions, we pass its length as another argument. We show the size in bytes (for the 4.4BSD implementation) of the fixed-length structures beneath each structure.

The sockaddr_un structure itself is not variable-length (Figure 15.1(See 9.4.2)), but the amount of information—the pathname within the structure—is variable-length. When passing pointers to these structures, we must be careful how we handle the length field, both the length field in the socket address structure itself (if supported by the implementation) and the length to and from the kernel.

This figure shows the style that we follow throughout the text: structure names are always shown in a bolder font, followed by braces, as in sockaddr_in{}.

We noted earlier that the length field was added to all the socket address structures with the 4.3BSD Reno release. Had the length field been present with the original release of sockets, there would be no need for the length argument to all the socket functions: the third argument to bind and connect, for example. Instead, the size of the structure could be contained in the length field of the structure.

Value-Result Arguments

We mentioned that when a socket address structure is passed to any socket function, it is always passed by reference. That is, a pointer to the structure is passed. The length of the structure is also passed as an argument. But the way in which the length is passed depends on which direction the structure is being passed: from the process to the kernel, or vice versa.

1.Three functions, bind, connect, and sendto, pass a socket address structure from the process to the kernel. One argument to these three functions is the pointer to the socket address structure and another argument is the integer size of the structure, as in

struct sockaddr_in serv;

/* fill in serv{} */
connect (sockfd, (SA *) &serv, sizeof(serv));

Since the kernel is passed both the pointer and the size of what the pointer points to, it knows exactly how much data to copy from the process into the kernel.

Four functions, accept, recvfrom, getsockname, and getpeername, pass a socket address structure from the kernel to the process, the reverse direction from the previous scenario. Two of the arguments to these four functions are the pointer to the socket address structure along with a pointer to an integer containing the size of the structure, as in

struct sockaddr_un cli; /* Unix domain */
socklen_t len;

len = sizeof(cli); /* len is a value */
getpeername(unixfd, (SA *) &cli, &len);
/* len may have changed */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值