Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 1 | /* |
| 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 Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file ifaddrs.h |
| 33 | * @brief Access to network interface addresses. |
| 34 | */ |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 35 | |
| 36 | #include <sys/cdefs.h> |
| 37 | #include <netinet/in.h> |
| 38 | #include <sys/socket.h> |
| 39 | |
| 40 | __BEGIN_DECLS |
| 41 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Returned by getifaddrs() and freed by freeifaddrs(). |
| 44 | */ |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 45 | struct ifaddrs { |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 46 | /** Pointer to the next element in the linked list. */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 47 | struct ifaddrs* _Nullable ifa_next; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 48 | |
| 49 | /** Interface name. */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 50 | char* _Nullable ifa_name; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 51 | /** Interface flags (like `SIOCGIFFLAGS`). */ |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 52 | unsigned int ifa_flags; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 53 | /** Interface address. */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 54 | struct sockaddr* _Nullable ifa_addr; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 55 | /** Interface netmask. */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 56 | struct sockaddr* _Nullable ifa_netmask; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 57 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 58 | union { |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 59 | /** Interface broadcast address (if IFF_BROADCAST is set). */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 60 | struct sockaddr* _Nullable ifu_broadaddr; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 61 | /** Interface destination address (if IFF_POINTOPOINT is set). */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 62 | struct sockaddr* _Nullable ifu_dstaddr; |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 63 | } ifa_ifu; |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 64 | |
| 65 | /** Unused. */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 66 | void* _Nullable ifa_data; |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 69 | /** Synonym for `ifa_ifu.ifu_broadaddr` in `struct ifaddrs`. */ |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 70 | #define ifa_broadaddr ifa_ifu.ifu_broadaddr |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 71 | /** Synonym for `ifa_ifu.ifu_dstaddr` in `struct ifaddrs`. */ |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 72 | #define ifa_dstaddr ifa_ifu.ifu_dstaddr |
| 73 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 74 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 75 | * [getifaddrs(3)](https://man7.org/linux/man-pages/man3/getifaddrs.3.html) creates a linked list |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 76 | * 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 Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 83 | |
| 84 | #if __BIONIC_AVAILABILITY_GUARD(24) |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 85 | int getifaddrs(struct ifaddrs* _Nullable * _Nonnull __list_ptr) __INTRODUCED_IN(24); |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 86 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 87 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 88 | * [freeifaddrs(3)](https://man7.org/linux/man-pages/man3/freeifaddrs.3.html) frees a linked list |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 89 | * of `struct ifaddrs` returned by getifaddrs(). |
| 90 | * |
| 91 | * Available since API level 24. |
| 92 | */ |
zijunzhao | e7d41ab | 2023-03-01 01:36:30 +0000 | [diff] [blame] | 93 | void freeifaddrs(struct ifaddrs* _Nullable __ptr) __INTRODUCED_IN(24); |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame] | 94 | #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ |
| 95 | |
Elliott Hughes | 9cddb48 | 2016-01-04 20:38:05 +0000 | [diff] [blame] | 96 | |
Elliott Hughes | 462e90c | 2018-08-21 16:10:48 -0700 | [diff] [blame] | 97 | __END_DECLS |