getifaddrs, freeifaddrs manual

2023-11-18

GETIFADDRS(3)                 Linux Programmer's Manual                 GETIFADDRS(3)

NAME         top

       getifaddrs, freeifaddrs - get interface addresses

SYNOPSIS         top

       #include <sys/types.h>
       #include <ifaddrs.h>

       int getifaddrs(struct ifaddrs **ifap);

       void freeifaddrs(struct ifaddrs *ifa);

DESCRIPTION         top

       The getifaddrs() function creates a linked list of structures describing the
       network interfaces of the local system, and stores the address of the first
       item of the list in *ifap.  The list consists of ifaddrs structures, defined
       as follows:

           struct ifaddrs {
               struct ifaddrs  *ifa_next;    /* Next item in list */
               char            *ifa_name;    /* Name of interface */
               unsigned int     ifa_flags;   /* Flags from SIOCGIFFLAGS */
               struct sockaddr *ifa_addr;    /* Address of interface */
               struct sockaddr *ifa_netmask; /* Netmask of interface */
               union {
                   struct sockaddr *ifu_broadaddr;
                                    /* Broadcast address of interface */
                   struct sockaddr *ifu_dstaddr;
                                    /* Point-to-point destination address */
               } ifa_ifu;
           #define              ifa_broadaddr ifa_ifu.ifu_broadaddr
           #define              ifa_dstaddr   ifa_ifu.ifu_dstaddr
               void            *ifa_data;    /* Address-specific data */
           };

       The ifa_next field contains a pointer to the next structure on the list, or
       NULL if this is the last item of the list.

       The ifa_name points to the null-terminated interface name.

       The ifa_flags field contains the interface flags, as returned by the
       SIOCGIFFLAGS ioctl(2) operation (see netdevice(7) for a list of these flags).

       The ifa_addr field points to a structure containing the interface address.
       (The sa_family subfield should be consulted to determine the format of the
       address structure.)

       The ifa_netmask field points to a structure containing the netmask associated
       with ifa_addr, if applicable for the address family.

       Depending on whether the bit IFF_BROADCAST or IFF_POINTOPOINT is set in
       ifa_flags (only one can be set at a time), either ifa_broadaddr will contain
       the broadcast address associated with ifa_addr (if applicable for the address
       family) or ifa_dstaddr will contain the destination address of the point-to-
       point interface.

       The ifa_data field points to a buffer containing address-family-specific data;
       this field may be NULL if there is no such data for this interface.

       The data returned by getifaddrs() is dynamically allocated and should be freed
       using freeifaddrs() when no longer needed.

RETURN VALUES         top

       On success, getifaddrs() returns zero; on error, -1 is returned, and errno is
       set appropriately.

ERRORS         top

       getifaddrs() may fail and set errno for any of the errors specified for
       socket(2), bind(2), getsockname(2), recvmsg(2), sendto(2), malloc(3), or
       realloc(3).

VERSIONS         top

       The getifaddrs() function first appeared in glibc 2.3, but before glibc 2.3.3,
       the implementation only supported IPv4 addresses; IPv6 support was added in
       glibc 2.3.3.  Support of address families other than IPv4 is only available on
       kernels that support netlink.

CONFORMING TO         top

       Not in POSIX.1-2001.  This function first appeared in BSDi and is present on
       the BSD systems, but with slightly different semantics documented--returning
       one entry per interface, not per address.  This means ifa_addr and other
       fields can actually be NULL if the interface has no address, and no link-level
       address is returned if the interface has an IP address assigned.  Also, the
       way of choosing either ifa_broadaddr or ifa_dstaddr differs on various
       systems.

NOTES         top

       The addresses returned on Linux will usually be the IPv4 and IPv6 addresses
       assigned to the interface, but also one AF_PACKET address per interface
       containing lower-level details about the interface and its physical layer.  In
       this case, the ifa_data field may contain a pointer to a struct
       net_device_stats, defined in <linux/netdevice.h>, which contains various
       interface attributes and statistics.

EXAMPLE         top

       The program below demonstrates the use of getifaddrs(), freeifaddrs(), and
       getnameinfo(3).  Here is what we see when running this program on one system:

           $ ./a.out
           lo      address family: 17 (AF_PACKET)
           eth0    address family: 17 (AF_PACKET)
           lo      address family: 2 (AF_INET)
                   address: <127.0.0.1>
           eth0    address family: 2 (AF_INET)
                   address: <10.1.1.4>
           lo      address family: 10 (AF_INET6)
                   address: <::1>
           eth0    address family: 10 (AF_INET6)
                   address: <fe80::2d0:59ff:feda:eb51%eth0>

Program source

       #include <arpa/inet.h>
       #include <sys/socket.h>
       #include <netdb.h>
       #include <ifaddrs.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>

       int
       main(int argc, char *argv[])
       {
           struct ifaddrs *ifaddr, *ifa;
           int family, s;
           char host[NI_MAXHOST];

           if (getifaddrs(&ifaddr) == -1) {
               perror("getifaddrs");
               exit(EXIT_FAILURE);
           }

           /* Walk through linked list, maintaining head pointer so we
              can free list later */

           for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
               if (ifa->ifa_addr == NULL)
                   continue;

               family = ifa->ifa_addr->sa_family;

               /* Display interface name and family (including symbolic
                  form of the latter for the common families) */

               printf("%s  address family: %d%s\n",
                       ifa->ifa_name, family,
                       (family == AF_PACKET) ? " (AF_PACKET)" :
                       (family == AF_INET) ?   " (AF_INET)" :
                       (family == AF_INET6) ?  " (AF_INET6)" : "");

               /* For an AF_INET* interface address, display the address */

               if (family == AF_INET || family == AF_INET6) {
                   s = getnameinfo(ifa->ifa_addr,
                           (family == AF_INET) ? sizeof(struct sockaddr_in) :
                                                 sizeof(struct sockaddr_in6),
                           host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
                   if (s != 0) {
                       printf("getnameinfo() failed: %s\n", gai_strerror(s));
                       exit(EXIT_FAILURE);
                   }
                   printf("\taddress: <%s>\n", host);
               }
           }

           freeifaddrs(ifaddr);
           exit(EXIT_SUCCESS);
       }

SEE ALSO         top

       bind(2), getsockname(2), socket(2), packet(7), ifconfig(8)

COLOPHON         top

       This page is part of release 3.32 of the Linux man-pages project.  A
       description of the project, and information about reporting bugs, can be found
       at http://www.kernel.org/doc/man-pages/.

GNU                                   2010-10-06                        GETIFADDRS(3)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

getifaddrs, freeifaddrs manual 的相关文章

  • 如何在 Linux/OS X 上温和地终止 Firefox 进程

    我正在使用 Firefox 进行一些自动化操作 尽管我可以从 shell 打开 Firefox 窗口 但我无法正确终止它 如果我kill火狐进程与kill 3 or kill 2当我下次打开新的 Firefox 窗口时 命令会询问我是否要在
  • 在 Linux 控制台中返回一行?

    我知道我可以返回该行并用以下内容覆盖其内容 r 现在我怎样才能进入上一行来改变它呢 或者有没有办法打印到控制台窗口中的特定光标位置 我的目标是使用 PHP 创建一些自刷新的多行控制台应用程序 Use ANSI 转义码 http en wik
  • 如何反汇编、修改然后重新组装 Linux 可执行文件?

    无论如何 这可以做到吗 我使用过 objdump 但它不会产生我所知道的任何汇编器都可以接受的汇编输出 我希望能够更改可执行文件中的指令 然后对其进行测试 我认为没有任何可靠的方法可以做到这一点 机器代码格式非常复杂 比汇编文件还要复杂 实
  • SwiftUI 从一个列表拖动到另一个列表

    我正在尝试在列表之间拖放 我尝试过的 我找到了一个在 UIKIt 中执行此操作并使用 UIViewControllerRepresentable 的解决方案 但这不是我想要的 另一个解决方案是在列表上使用 onDrag 但这在 iPad 上
  • 在非实时操作系统/内核上执行接近实时任务的最佳方法是什么?

    在一台 GNU Linux 机器上 如果想要执行 实时 亚毫秒级时间关键 任务 您几乎总是必须经历漫长 复杂且容易出现问题的内核补丁过程 以提供足够的支持 1 http en wikipedia org wiki RTLinux Backg
  • 如何在列表的解析参数中解析列表(字符串)而不是列表(字符)?

    我在flask中使用flask restful 我的代码如下 from flask restful import Resource reqparse apilink parser reqparse RequestParser apilink
  • 如何对结构切片而不是切片结构进行范围调整

    稍微玩了一下 Go HTML 模板后 我发现的所有循环模板中对象的示例都是将切片结构传递给模板 有点像这个示例 type UserList struct Id int Name string var templates template M
  • Bash - 比较 2 个文件列表及其 md5 校验和

    我有 2 个列表 其中包含带有 md5sum 检查的文件 即使文件相同 列表也具有不同的路径 我想检查每个文件的 md5 和 我们正在讨论数千个文件 这就是为什么我需要脚本来仅显示差异 第一个列表是普通列表 第二个列表是文件的当前状态 我想
  • 比较linux中的两个未排序列表,列出第二个文件中的唯一项

    我有 2 个包含号码列表 电话号码 的文件 我正在寻找一种列出第二个文件中第一个文件中不存在的数字的方法 我尝试过各种方法 comm getting some weird sorting errors fgrep v x f second
  • 运行 shell 命令并将输出发送到文件?

    我需要能够通过 php 脚本修改我的 openvpn 身份验证文件 我已将我的 http 用户设置为免通 sudoer 因为这台机器仅在我的家庭网络中可用 我目前有以下命令 echo shell exec sudo echo usernam
  • 如何在特定的Java版本上运行应用程序?

    如何运行具有特定 Java 版本的应用程序 我安装了三个 Java 版本 myuser mysystem sudo update alternatives config java There are 3 choices for the al
  • 在Python中创建一个二维矩阵

    我创建了一个 6x5 二维数组 最初每个单元格中只有 None 然后 我读取一个文件 并在读取文件时用数据替换 None 我首先创建空数组 因为数据在我正在读取的文件中的顺序未定义 我的第一次尝试是这样做的 x None 5 6 这导致了一
  • 存储整数列表的最有效方法

    我最近一直在做一个项目 其中一个目标是使用尽可能少的内存来使用 Python 3 存储一系列文件 除了一个整数列表之外 几乎所有文件都占用很少的空间 大致333 000整数长且整数可达约8000在尺寸方面 我目前正在使用pickle存储列表
  • 如何在应用程序目录层次结构中构建接口?

    将它们全部放在一个单独的文件夹结构中还是与实现它们的类一起放置 切勿将接口与实现它们的类放在一起 除非这些类满足以下要求 这样做将引入一个紧耦合在接口和实现者之间 如果不同时引用实现者 您将无法创建该接口的其他实现 你基本上有两个选择 将接
  • 将数字转换为英文字母列表

    我有下面的函数 它将数字输入转换为这些数字的部分翻译的单词输出 使用乘积和商 它将数字的单词表示相加 同时将数字分组 例如 number name 87969087 gt 87 million 969 thousand 87 number
  • 如何使用 go1.6.2 构建 linux 32 位

    有没有任何组合GOARCH and GOOS我可以设置哪些值来构建 ELF 32 位二进制文 件 GOOS linux and GOARCH 386 更多示例 架构 32 bit gt GOARCH 386 64 bit gt GOARCH
  • 发布到列表 MVC3

    我试图让我的视图将列表发布回操作 但它始终为空 所以我的模型有一个 WeightEntry 对象列表 运动模型 public class Exercise public List
  • 将 Python 列表(JSON 或其他)插入 MySQL 数据库

    所以我在Python中有一堆数组数据 嗯 相反 我有一个清单 我试图将此数组存储到 MySQL 数据库中的单个单元格中 我尝试使用 JSON 来序列化我的数据 但也许我不明白 JSON 是如何工作的 因此 在连接到我的数据库后 我尝试了上游
  • 亚马逊 Linux - 安装 openjdk-debuginfo?

    我试图使用jstack在 ec2 实例上amazon linux 所以我安装了openjdk devel包裹 sudo yum install java 1 7 0 openjdk devel x86 64 但是 jstack 引发了异常j
  • 向前和向后迭代

    我们有一个双端结构列表 例如LinkedList 我需要向前和向后迭代元素 例如 向前 4 次 然后向后 2 次 然后向前 5 次 在 C 中它将是 iter iter iter 在 Rust 中 我只看到 next and rev 这很不

随机推荐

  • enum一个最不像class的class

    enum一个最不像class的classjava枚举类型是jdk5出现的 它的出现主要为了解决一些有特殊意义 已经确定的 长度不会改变的集合 月份描述 public class Month 月份名称 private final String
  • SpringMVC之JSR303使用及拦截器使用(带你探索SpringMVC的新领域)

    目录 前言 一 探索JSR303的世界 1 JSR303简介 1 1 什么是JSR303 1 2 JSR303的重要性及使用原因 重要性 原因 1 3 JSR303的常用注解 扩展 2 JSR303快速入门 基本使用 2 1 导入依赖 2
  • 【算法】直接插入排序C语言实现

    不知道大家喜不喜欢打扑克 哈哈 我就挺喜欢的 尤其是三人斗地主 很喜欢 现在我来描述一幅画面看看大家熟不熟悉 我抓牌的习惯是 在抓牌的时候 我要看着我的牌 看看牌的状况 有没有大小鬼 有几个2 有没有长的连 顺便做好基本的排序工作 比如我第
  • http 请求报文响应报文的格式以及Token cookie session 区别

    面试必备 http 请求报文响应报文的格式 Token cookie session 区别 http 请求报文响应报文的格式 HTTP 请求报文和响应报文的格式如下 HTTP 请求报文格式 lt 方法 gt lt 路径 gt lt 协议版本
  • RabbitMQ --- SpringAMQP

    一 简介 SpringAMQP是基于RabbitMQ封装的一套模板 并且还利用SpringBoot对其实现了自动装配 使用起来非常方便 SpringAmqp的官方地址 Spring AMQP SpringAMQP提供了三个功能 自动声明队列
  • gitlab down: redis: 0s, normally up, want up; run:log

    突然发现gitlab不能访问了 报错505 1 查看gitlab的状态 发现redis的状态是down gitlab ctl status down redis 0s normally up want up run log 解决 启动red
  • matlab中global的用法

    Matlab 中子函数不传参直接调用主函数global变量方法 在一个m文件里要调用一个函数 自定义的 但是我希望这个函数能利用并修改workspace中的变量 m文件中的 可是函数的变量全是局部的 无法修改工作区的变量 该怎么办 同时在
  • KMP算法最浅显理解——一看就明白

    说明 KMP算法看懂了觉得特别简单 思路很简单 看不懂之前 查各种资料 看的稀里糊涂 即使网上最简单的解释 依然看的稀里糊涂 我花了半天时间 争取用最短的篇幅大致搞明白这玩意到底是啥 这里不扯概念 只讲算法过程和代码理解 KMP算法求解什么
  • Artec独立三维(3D)扫描软件

    最新版本 Artec Studio 9 1 中文界面 您是否想将自己的Kinect作为 3D 三维扫描仪来使用呢 ArtecStudio9 1为您提供解决方案 它可以和微软的Kinect 华硕的 Xtion XtionProLive以及其他
  • Uncaught SyntaxError: Unexpected end of input

    Uncaught SyntaxError Unexpected end of input 最近做项目遇到这样一个问题Uncaught SyntaxError Unexpected end of input Unexpected end of
  • mysql有没有flashback_Flashback for MySQL 5.7

    实现原理 flashback的概念最早出现于Oracle数据库 用于快速恢复用户的误操作 flashback for MySQL用于恢复由DML语句引起的误操作 目前不支持DDL语句 例如下面的语句 DELETE FROM XXX UPDA
  • xsync 集群同步工具

    前言 在配置集群时 往往需要将文件拷贝到各个机器 一来二去就很麻烦 我们可以使用 xsync 工具同时进行多台机器同步数据 环境准备 我们准备三台虚拟机 他们的 IP 分别为 192 168 56 2 192 168 56 3 192 16
  • python 日期和时间处理(time,datetime模块讲解)

    在现实生活中 我们常常遇到时间序列任务 所以今天讲解下日期和时间处理 Python 日期时间 datetime 1 获取当前时间 import datetime datetime object datetime datetime now p
  • 颜色的 HSL 表示

  • 【vue】图片加载动画效果

    加载后 一种是图片由浅到深 一种是闪光加载效果消失
  • tmux使用

    tmux使用 需求 ssh链接不稳定 若直接在ssh终端中运行某个长时间的程序 会被中断 使用tmux 即使ssh服务中断 tmux中的程序依旧运行着 常用命令汇总 开启一个tmux页面 tmux 开启一个tmux页面 自定义名字 tmux
  • Flutter Divider

    不设置高度 会在线的top和bottom占据一点空间 Divider thickness 1 h color Color 0xFF3D3D3E 设置height之后就正常了 上下没有间距了 Divider thickness 1 h hei
  • Docker未授权访问漏洞(www.hetianlab.com)

    什么是Docker Docker是一个开源的引擎 可以轻松的为任何应用创建一个轻量级的 可移植的 自给自足的容器 开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署 包括VMs 虚拟机 bare metal OpenStack 集
  • Uncaught TypeError: Cannot Read Property

    这是 JavaScript 开发人员最常遇到的错误 当你读取一个属性或调用一个未定义对象的方法时 Chrome 中就会报出这样的错误 导致这个错误发生的原因有很多 常见的一种情况是在渲染 UI 组件时 不正确地初始化状态 我们来看一个真实的
  • getifaddrs, freeifaddrs manual

    GETIFADDRS 3 Linux Programmer s Manual GETIFADDRS 3 NAME top getifaddrs freeifaddrs get interface addresses SYNOPSIS top