1.\" $NetBSD: getifaddrs.3,v 1.19 2017/10/25 16:29:20 abhinav Exp $ 2.\" BSDI getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp 3.\" 4.\" Copyright (c) 1995, 1999 5.\" Berkeley Software Design, Inc. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 13.\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND 14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.\" ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE 17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23.\" SUCH DAMAGE. 24.Dd September 15, 2016 25.Dt GETIFADDRS 3 26.Os 27.Sh NAME 28.Nm getifaddrs , 29.Nm freeifaddrs 30.Nd get interface addresses 31.Sh SYNOPSIS 32.In sys/types.h 33.In sys/socket.h 34.In ifaddrs.h 35.Ft int 36.Fn getifaddrs "struct ifaddrs **ifap" 37.Ft void 38.Fn freeifaddrs "struct ifaddrs *ifp" 39.Sh DESCRIPTION 40The 41.Fn getifaddrs 42function stores a reference to a linked list of the network interfaces 43on the local machine in the memory referenced by 44.Fa ifap . 45The list consists of 46.Nm ifaddrs 47structures, as defined in the include file 48.In ifaddrs.h . 49The 50.Nm ifaddrs 51structure contains at least the following entries: 52.Bd -literal 53 struct ifaddrs *ifa_next; /* Pointer to next struct */ 54 char *ifa_name; /* Interface name */ 55 unsigned int ifa_flags; /* Interface flags */ 56 struct sockaddr *ifa_addr; /* Interface address */ 57 struct sockaddr *ifa_netmask; /* Interface netmask */ 58 struct sockaddr *ifa_broadaddr; /* Interface broadcast address */ 59 struct sockaddr *ifa_dstaddr; /* P2P interface destination */ 60 void *ifa_data; /* Address specific data */ 61 unsigned int ifa_addrflags; /* Address flags */ 62.Ed 63.Pp 64The 65.Li ifa_next 66field contains a pointer to the next structure on the list. 67This field is 68.Dv NULL 69in last structure on the list. 70.Pp 71The 72.Li ifa_name 73field contains the interface name. 74.Pp 75The 76.Li ifa_flags 77field contains the interface flags, as set by 78.Xr ifconfig 8 79utility. 80.Pp 81The 82.Li ifa_addr 83field references either the address of the interface or the link level 84address of the interface, if one exists, otherwise it is 85.Dv NULL . 86(The 87.Li sa_family 88field of the 89.Li ifa_addr 90field should be consulted to determine the format of the 91.Li ifa_addr 92address.) 93.Pp 94The 95.Li ifa_netmask 96field references the netmask associated with 97.Li ifa_addr , 98if one is set, otherwise it is 99.Dv NULL . 100.Pp 101The 102.Li ifa_broadaddr 103field, 104which should only be referenced for non-P2P interfaces, 105references the broadcast address associated with 106.Li ifa_addr , 107if one exists, otherwise it is 108.Dv NULL . 109.Pp 110The 111.Li ifa_dstaddr 112field references the destination address on a P2P interface, 113if one exists, otherwise it is 114.Dv NULL . 115.Pp 116The 117.Li ifa_data 118field references address family specific data. 119For 120.Dv AF_LINK 121addresses it contains a pointer to the 122.Fa struct if_data 123.Pq as defined in include file Aq Pa net/if.h 124which contains various interface attributes and statistics. 125For all other address families, it is 126.Dv NULL . 127.Pp 128The 129.Li ifa_addrflags 130field contains the address flags, which are specific to the address family. 131.Pp 132The data returned by 133.Fn getifaddrs 134is dynamically allocated and should be freed using 135.Fn freeifaddrs 136when no longer needed. 137.Sh RETURN VALUES 138Upon successful completion, a value of 0 is returned. 139Otherwise, a value of -1 is returned and 140.Va errno 141is set to indicate the error. 142.Sh EXAMPLES 143The following example program prints a list of all addresses configured 144on the system. 145.Bd -literal -offset indent 146#include <sys/types.h> 147#include <sys/socket.h> 148#include <stdio.h> 149#include <ifaddrs.h> 150#include <util.h> 151#include <err.h> 152#include <stdlib.h> 153 154int 155main(int argc, char *argv[]) 156{ 157 struct ifaddrs *ifa, *a; 158 159 if (getifaddrs(&ifa) == -1) 160 err(EXIT_FAILURE, "getifaddrs"); 161 162 for (a = ifa; a; a = a->ifa_next) { 163 char buf[1024]; 164 sockaddr_snprintf(buf, sizeof(buf), "%f %a", 165 a->ifa_addr); 166 printf("%s %x %s\\n", a->ifa_name, a->ifa_flags, buf); 167 } 168 freeifaddrs(ifa); 169 return EXIT_SUCCESS; 170} 171.Ed 172.Sh ERRORS 173The 174.Fn getifaddrs 175may fail and set 176.Va errno 177for any of the errors specified for the library routines 178.Xr ioctl 2 , 179.Xr socket 2 , 180.Xr malloc 3 181or 182.Xr sysctl 3 . 183.Sh SEE ALSO 184.Xr ioctl 2 , 185.Xr socket 2 , 186.Xr sysctl 3 , 187.Xr networking 4 , 188.Xr ifconfig 8 189.Sh HISTORY 190The 191.Nm 192implementation first appeared in 193.Bsx . 194.Pp 195.Li ifa_addrflags 196was added in 197.Nx 8.0 . 198.Sh BUGS 199If both 200.In net/if.h 201and 202.In ifaddrs.h 203are being included, 204.In net/if.h 205.Em must 206be included before 207.In ifaddrs.h . 208