我听说在设计 BSD 套接字库时,C 编程缺少某种模式或特性,这导致了我们今天的设计。
例如,当传递struct socketaddr_in
给struct socketaddr
系统调用(如bind
.
从 GNU libc 头文件:
/* POSIX.1g specifies this type name for the `sa_family' member. */
typedef unsigned short int sa_family_t;
/* This macro is used to declare the initial common members
of the data types used for socket addresses, `struct sockaddr',
`struct sockaddr_in', `struct sockaddr_un', etc. */
#define __SOCKADDR_COMMON(sa_prefix) \
sa_family_t sa_prefix##family
#define __SOCKADDR_COMMON_SIZE (sizeof (unsigned short int))
/* Structure describing a generic socket address. */
struct sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
};
/* Structure describing an Internet socket address. */
struct sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
struct in_addr sin_addr; /* Internet address. */
/* Pad to size of `struct sockaddr'. */
unsigned char sin_zero[sizeof (struct sockaddr) -
__SOCKADDR_COMMON_SIZE -
sizeof (in_port_t) -
sizeof (struct in_addr)];
};
/* Internet address. */
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
一个示例程序:
int main(int argc, char *argv[]) {
...
// Initialize the server address
struct sockaddr_in serv_addr;
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr = (struct sockaddr_in) {
.sin_port = htons(port),
.sin_family = AF_INET ,
.sin_addr.s_addr = INADDR_ANY
};
// Bind the server address to the TCP/IP socket
int status = bind(server_socket, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
...
}
我想不出这个通用接口还能如何到位。有任何想法吗?