【发布时间】:2019-07-17 00:55:43
【问题描述】:
我正在使用 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 } } }
【问题讨论】:
-
它可能被用作某些数组或结构的初始化器。只需将出现的宏名称替换为它的主体即可。
-
您可以 grep 源代码树以在
.c或其他.h文件中使用这些宏。 (我通常使用find、xargs和grep,但我认为grep 也有一个-R选项来递归grep 子目录。) -
这是一个 16 字节/128 位的初始化器,用于“任意”+“环回”地址。 32 位 IPv4 等效值为
#define INADDR_ANY ((unsigned long int) 0x00000000)+#define INPORT_ANY 0 -
宏标识符的
_INIT部分是关于其预期用途的强烈信号:它们旨在用作初始化程序。此外,它们的特定句法形式在 C 中没有其他用途。特别是,它们是适用于struct in6_addr类型对象的初始化器(它们的标识符也强烈暗示了这一点)。