1 /* MIT License 2 * 3 * Copyright (c) 2023 Brad House 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 * 24 * SPDX-License-Identifier: MIT 25 */ 26 #ifndef __ARES__IFACE_IPS_H 27 #define __ARES__IFACE_IPS_H 28 29 /*! Flags for interface ip addresses. */ 30 typedef enum { 31 ARES_IFACE_IP_V4 = 1 << 0, /*!< IPv4 address. During enumeration if 32 * this flag is set ARES_IFACE_IP_V6 33 * is not, will only enumerate v4 34 * addresses. */ 35 ARES_IFACE_IP_V6 = 1 << 1, /*!< IPv6 address. During enumeration if 36 * this flag is set ARES_IFACE_IP_V4 37 * is not, will only enumerate v6 38 * addresses. */ 39 ARES_IFACE_IP_LOOPBACK = 1 << 2, /*!< Loopback adapter */ 40 ARES_IFACE_IP_OFFLINE = 1 << 3, /*!< Adapter offline */ 41 ARES_IFACE_IP_LINKLOCAL = 1 << 4, /*!< Link-local ip address */ 42 /*! Default, enumerate all ips for online interfaces, including loopback */ 43 ARES_IFACE_IP_DEFAULT = (ARES_IFACE_IP_V4 | ARES_IFACE_IP_V6 | 44 ARES_IFACE_IP_LOOPBACK | ARES_IFACE_IP_LINKLOCAL) 45 } ares_iface_ip_flags_t; 46 47 struct ares_iface_ips; 48 49 /*! Opaque pointer for holding enumerated interface ip addresses */ 50 typedef struct ares_iface_ips ares_iface_ips_t; 51 52 /*! Destroy ip address enumeration created by ares_iface_ips(). 53 * 54 * \param[in] ips Initialized IP address enumeration structure 55 */ 56 void ares_iface_ips_destroy(ares_iface_ips_t *ips); 57 58 /*! Enumerate ip addresses on interfaces 59 * 60 * \param[out] ips Returns initialized ip address structure 61 * \param[in] flags Flags for enumeration 62 * \param[in] name Interface name to enumerate, or NULL to enumerate all 63 * \return ARES_ENOMEM on out of memory, ARES_ENOTIMP if not supported on 64 * the system, ARES_SUCCESS on success 65 */ 66 ares_status_t ares_iface_ips(ares_iface_ips_t **ips, 67 ares_iface_ip_flags_t flags, const char *name); 68 69 /*! Count of ips enumerated 70 * 71 * \param[in] ips Initialized IP address enumeration structure 72 * \return count 73 */ 74 size_t ares_iface_ips_cnt(const ares_iface_ips_t *ips); 75 76 /*! Retrieve interface name 77 * 78 * \param[in] ips Initialized IP address enumeration structure 79 * \param[in] idx Index of entry to pull 80 * \return interface name 81 */ 82 const char *ares_iface_ips_get_name(const ares_iface_ips_t *ips, size_t idx); 83 84 /*! Retrieve interface address 85 * 86 * \param[in] ips Initialized IP address enumeration structure 87 * \param[in] idx Index of entry to pull 88 * \return interface address 89 */ 90 const struct ares_addr *ares_iface_ips_get_addr(const ares_iface_ips_t *ips, 91 size_t idx); 92 93 /*! Retrieve interface address flags 94 * 95 * \param[in] ips Initialized IP address enumeration structure 96 * \param[in] idx Index of entry to pull 97 * \return interface address flags 98 */ 99 ares_iface_ip_flags_t ares_iface_ips_get_flags(const ares_iface_ips_t *ips, 100 size_t idx); 101 102 /*! Retrieve interface address netmask 103 * 104 * \param[in] ips Initialized IP address enumeration structure 105 * \param[in] idx Index of entry to pull 106 * \return interface address netmask 107 */ 108 unsigned char ares_iface_ips_get_netmask(const ares_iface_ips_t *ips, 109 size_t idx); 110 111 /*! Retrieve interface ipv6 link local scope 112 * 113 * \param[in] ips Initialized IP address enumeration structure 114 * \param[in] idx Index of entry to pull 115 * \return interface ipv6 link local scope 116 */ 117 unsigned int ares_iface_ips_get_ll_scope(const ares_iface_ips_t *ips, 118 size_t idx); 119 120 121 /*! Retrieve the interface index (aka link local scope) from the interface 122 * name. 123 * 124 * \param[in] name Interface name 125 * \return 0 on failure, index otherwise 126 */ 127 unsigned int ares_os_if_nametoindex(const char *name); 128 129 /*! Retrieves the interface name from the index (aka link local scope) 130 * 131 * \param[in] index Interface index (> 0) 132 * \param[in] name Buffer to hold name 133 * \param[in] name_len Length of provided buffer, must be at least IF_NAMESIZE 134 * \return NULL on failure, or pointer to name on success 135 */ 136 const char *ares_os_if_indextoname(unsigned int index, char *name, 137 size_t name_len); 138 139 #endif 140