c - 我不明白 inet/netinet/in.h 的宏

标签 c macros

我正在使用 C 语言进行软件驱动程序的 IPv6 迁移,我想了解以下 IN6ADDR_ANY_INITIN6ADDR_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/

相关文章:

c - 动态二维数组转静态数组

C++ 宏汇编错误

macros - 我的第一个 Lisp 宏;漏水了吗?

c - 如何在C中实现变量的dup函数

核心转储,如何增加矩阵维度(可用),在 C 中释放 2d 指针时出错

ms-access - 在 access 中从 VBA 调用嵌入宏

msbuild - 在 msbuild (12.0) 命令行属性分配上引用宏

macros - 在 lisp 中创建等同于 incf 的宏函数

c - 编写C程序来计算根

c - 有什么方法可以在不使用 math.h 和 sqrt() 的情况下获得数字的平方根?