我正在使用 C 语言进行软件驱动程序的 IPv6 迁移,我想了解以下 IN6ADDR_ANY_INIT 和 IN6ADDR_LOOPBACK_INIT 宏。请帮忙:
/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
#ifndef _NETINET_IN_H
#define _NETINET_IN_H 1
#include <features.h>
#include <bits/stdint-uintn.h>
#include <sys/socket.h>
#include <bits/types.h>
.
.
.
#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
最佳答案
这些是struct in6_addr
的初始值设定项。 standard要求 struct in6_addr 至少有一个成员 s6_addr
,该成员必须是 uint8_t[16]
。
大多数情况下,struct in6_addr
是作为包含 8、16 和 32 位整数数组的嵌套 union 实现的,以实现优化访问。例如glibc有
struct in6_addr {
union {
uint8_t __u6_addr8[16];
uint16_t __u6_addr16[8];
uint32_t __u6_addr32[4];
} __in6_u;
};
和访问器定义
#define s6_addr __in6_u.__u6_addr8
请记住, union 初始值设定项需要自己的一组大括号并对 union 的第一个成员进行操作。最外面的 {}
是结构所必需的,下一级 {}
是 union 所必需的,最里面的 {}
是数组初始值设定项所必需的。
因此
#define IN6ADDR_ANY_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 } } }
这是
0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0
和
#define IN6ADDR_LOOPBACK_INIT { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 } } }
这是
::1
关于c - 我不明白 inet/netinet/in.h 的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57061773/