blob: 87d29471b49f1fddd57c6947512de697ebd34056 [file] [log] [blame]
Elliott Hughes9cddb482016-01-04 20:38:05 +00001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes462e90c2018-08-21 16:10:48 -070029#pragma once
30
31/**
32 * @file ifaddrs.h
33 * @brief Access to network interface addresses.
34 */
Elliott Hughes9cddb482016-01-04 20:38:05 +000035
36#include <sys/cdefs.h>
37#include <netinet/in.h>
38#include <sys/socket.h>
39
40__BEGIN_DECLS
41
Elliott Hughes462e90c2018-08-21 16:10:48 -070042/**
43 * Returned by getifaddrs() and freed by freeifaddrs().
44 */
Elliott Hughes9cddb482016-01-04 20:38:05 +000045struct ifaddrs {
Elliott Hughes462e90c2018-08-21 16:10:48 -070046 /** Pointer to the next element in the linked list. */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000047 struct ifaddrs* _Nullable ifa_next;
Elliott Hughes462e90c2018-08-21 16:10:48 -070048
49 /** Interface name. */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000050 char* _Nullable ifa_name;
Elliott Hughes462e90c2018-08-21 16:10:48 -070051 /** Interface flags (like `SIOCGIFFLAGS`). */
Elliott Hughes9cddb482016-01-04 20:38:05 +000052 unsigned int ifa_flags;
Elliott Hughes462e90c2018-08-21 16:10:48 -070053 /** Interface address. */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000054 struct sockaddr* _Nullable ifa_addr;
Elliott Hughes462e90c2018-08-21 16:10:48 -070055 /** Interface netmask. */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000056 struct sockaddr* _Nullable ifa_netmask;
Elliott Hughes462e90c2018-08-21 16:10:48 -070057
Elliott Hughes9cddb482016-01-04 20:38:05 +000058 union {
Elliott Hughes462e90c2018-08-21 16:10:48 -070059 /** Interface broadcast address (if IFF_BROADCAST is set). */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000060 struct sockaddr* _Nullable ifu_broadaddr;
Elliott Hughes462e90c2018-08-21 16:10:48 -070061 /** Interface destination address (if IFF_POINTOPOINT is set). */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000062 struct sockaddr* _Nullable ifu_dstaddr;
Elliott Hughes9cddb482016-01-04 20:38:05 +000063 } ifa_ifu;
Elliott Hughes462e90c2018-08-21 16:10:48 -070064
65 /** Unused. */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000066 void* _Nullable ifa_data;
Elliott Hughes9cddb482016-01-04 20:38:05 +000067};
68
Elliott Hughes462e90c2018-08-21 16:10:48 -070069/** Synonym for `ifa_ifu.ifu_broadaddr` in `struct ifaddrs`. */
Elliott Hughes9cddb482016-01-04 20:38:05 +000070#define ifa_broadaddr ifa_ifu.ifu_broadaddr
Elliott Hughes462e90c2018-08-21 16:10:48 -070071/** Synonym for `ifa_ifu.ifu_dstaddr` in `struct ifaddrs`. */
Elliott Hughes9cddb482016-01-04 20:38:05 +000072#define ifa_dstaddr ifa_ifu.ifu_dstaddr
73
Elliott Hughes462e90c2018-08-21 16:10:48 -070074/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000075 * [getifaddrs(3)](https://man7.org/linux/man-pages/man3/getifaddrs.3.html) creates a linked list
Elliott Hughes462e90c2018-08-21 16:10:48 -070076 * of `struct ifaddrs`. The list must be freed by freeifaddrs().
77 *
78 * Returns 0 and stores the list in `*__list_ptr` on success,
79 * and returns -1 and sets `errno` on failure.
80 *
81 * Available since API level 24.
82 */
Dan Albert02ce4012024-10-25 19:13:49 +000083
84#if __BIONIC_AVAILABILITY_GUARD(24)
zijunzhaoe7d41ab2023-03-01 01:36:30 +000085int getifaddrs(struct ifaddrs* _Nullable * _Nonnull __list_ptr) __INTRODUCED_IN(24);
Elliott Hughes9cddb482016-01-04 20:38:05 +000086
Elliott Hughes462e90c2018-08-21 16:10:48 -070087/**
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000088 * [freeifaddrs(3)](https://man7.org/linux/man-pages/man3/freeifaddrs.3.html) frees a linked list
Elliott Hughes462e90c2018-08-21 16:10:48 -070089 * of `struct ifaddrs` returned by getifaddrs().
90 *
91 * Available since API level 24.
92 */
zijunzhaoe7d41ab2023-03-01 01:36:30 +000093void freeifaddrs(struct ifaddrs* _Nullable __ptr) __INTRODUCED_IN(24);
Dan Albert02ce4012024-10-25 19:13:49 +000094#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
95
Elliott Hughes9cddb482016-01-04 20:38:05 +000096
Elliott Hughes462e90c2018-08-21 16:10:48 -070097__END_DECLS