11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 2023 Brad House
41cb0ef41Sopenharmony_ci *
51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights
81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next
131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
141cb0ef41Sopenharmony_ci * Software.
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
221cb0ef41Sopenharmony_ci * SOFTWARE.
231cb0ef41Sopenharmony_ci *
241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT
251cb0ef41Sopenharmony_ci */
261cb0ef41Sopenharmony_ci#ifndef __ARES__IFACE_IPS_H
271cb0ef41Sopenharmony_ci#define __ARES__IFACE_IPS_H
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci/*! Flags for interface ip addresses. */
301cb0ef41Sopenharmony_citypedef enum {
311cb0ef41Sopenharmony_ci  ARES_IFACE_IP_V4 = 1 << 0,        /*!< IPv4 address. During enumeration if
321cb0ef41Sopenharmony_ci                                     *   this flag is set ARES_IFACE_IP_V6
331cb0ef41Sopenharmony_ci                                     *   is not, will only enumerate v4
341cb0ef41Sopenharmony_ci                                     *   addresses. */
351cb0ef41Sopenharmony_ci  ARES_IFACE_IP_V6 = 1 << 1,        /*!< IPv6 address. During enumeration if
361cb0ef41Sopenharmony_ci                                     *   this flag is set ARES_IFACE_IP_V4
371cb0ef41Sopenharmony_ci                                     *   is not, will only enumerate v6
381cb0ef41Sopenharmony_ci                                     *   addresses. */
391cb0ef41Sopenharmony_ci  ARES_IFACE_IP_LOOPBACK  = 1 << 2, /*!< Loopback adapter */
401cb0ef41Sopenharmony_ci  ARES_IFACE_IP_OFFLINE   = 1 << 3, /*!< Adapter offline */
411cb0ef41Sopenharmony_ci  ARES_IFACE_IP_LINKLOCAL = 1 << 4, /*!< Link-local ip address */
421cb0ef41Sopenharmony_ci  /*! Default, enumerate all ips for online interfaces, including loopback */
431cb0ef41Sopenharmony_ci  ARES_IFACE_IP_DEFAULT = (ARES_IFACE_IP_V4 | ARES_IFACE_IP_V6 |
441cb0ef41Sopenharmony_ci                           ARES_IFACE_IP_LOOPBACK | ARES_IFACE_IP_LINKLOCAL)
451cb0ef41Sopenharmony_ci} ares__iface_ip_flags_t;
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cistruct ares__iface_ips;
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci/*! Opaque pointer for holding enumerated interface ip addresses */
501cb0ef41Sopenharmony_citypedef struct ares__iface_ips ares__iface_ips_t;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci/*! Destroy ip address enumeration created by ares__iface_ips().
531cb0ef41Sopenharmony_ci *
541cb0ef41Sopenharmony_ci *  \param[in]  ips   Initialized IP address enumeration structure
551cb0ef41Sopenharmony_ci */
561cb0ef41Sopenharmony_civoid                           ares__iface_ips_destroy(ares__iface_ips_t *ips);
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci/*! Enumerate ip addresses on interfaces
591cb0ef41Sopenharmony_ci *
601cb0ef41Sopenharmony_ci *  \param[out]  ips   Returns initialized ip address structure
611cb0ef41Sopenharmony_ci *  \param[in]   flags Flags for enumeration
621cb0ef41Sopenharmony_ci *  \param[in]   name  Interface name to enumerate, or NULL to enumerate all
631cb0ef41Sopenharmony_ci *  \return ARES_ENOMEM on out of memory, ARES_ENOTIMP if not supported on
641cb0ef41Sopenharmony_ci *          the system, ARES_SUCCESS on success
651cb0ef41Sopenharmony_ci */
661cb0ef41Sopenharmony_ciares_status_t                  ares__iface_ips(ares__iface_ips_t    **ips,
671cb0ef41Sopenharmony_ci                                               ares__iface_ip_flags_t flags, const char *name);
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci/*! Count of ips enumerated
701cb0ef41Sopenharmony_ci *
711cb0ef41Sopenharmony_ci * \param[in]  ips   Initialized IP address enumeration structure
721cb0ef41Sopenharmony_ci * \return count
731cb0ef41Sopenharmony_ci */
741cb0ef41Sopenharmony_cisize_t      ares__iface_ips_cnt(const ares__iface_ips_t *ips);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci/*! Retrieve interface name
771cb0ef41Sopenharmony_ci *
781cb0ef41Sopenharmony_ci * \param[in]  ips   Initialized IP address enumeration structure
791cb0ef41Sopenharmony_ci * \param[in]  idx   Index of entry to pull
801cb0ef41Sopenharmony_ci * \return interface name
811cb0ef41Sopenharmony_ci */
821cb0ef41Sopenharmony_ciconst char *ares__iface_ips_get_name(const ares__iface_ips_t *ips, size_t idx);
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci/*! Retrieve interface address
851cb0ef41Sopenharmony_ci *
861cb0ef41Sopenharmony_ci * \param[in]  ips   Initialized IP address enumeration structure
871cb0ef41Sopenharmony_ci * \param[in]  idx   Index of entry to pull
881cb0ef41Sopenharmony_ci * \return interface address
891cb0ef41Sopenharmony_ci */
901cb0ef41Sopenharmony_ciconst struct ares_addr *ares__iface_ips_get_addr(const ares__iface_ips_t *ips,
911cb0ef41Sopenharmony_ci                                                 size_t                   idx);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci/*! Retrieve interface address flags
941cb0ef41Sopenharmony_ci *
951cb0ef41Sopenharmony_ci * \param[in]  ips   Initialized IP address enumeration structure
961cb0ef41Sopenharmony_ci * \param[in]  idx   Index of entry to pull
971cb0ef41Sopenharmony_ci * \return interface address flags
981cb0ef41Sopenharmony_ci */
991cb0ef41Sopenharmony_ciares__iface_ip_flags_t  ares__iface_ips_get_flags(const ares__iface_ips_t *ips,
1001cb0ef41Sopenharmony_ci                                                  size_t                   idx);
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci/*! Retrieve interface address netmask
1031cb0ef41Sopenharmony_ci *
1041cb0ef41Sopenharmony_ci * \param[in]  ips   Initialized IP address enumeration structure
1051cb0ef41Sopenharmony_ci * \param[in]  idx   Index of entry to pull
1061cb0ef41Sopenharmony_ci * \return interface address netmask
1071cb0ef41Sopenharmony_ci */
1081cb0ef41Sopenharmony_ciunsigned char ares__iface_ips_get_netmask(const ares__iface_ips_t *ips,
1091cb0ef41Sopenharmony_ci                                          size_t                   idx);
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci/*! Retrieve interface ipv6 link local scope
1121cb0ef41Sopenharmony_ci *
1131cb0ef41Sopenharmony_ci * \param[in]  ips   Initialized IP address enumeration structure
1141cb0ef41Sopenharmony_ci * \param[in]  idx   Index of entry to pull
1151cb0ef41Sopenharmony_ci * \return interface ipv6 link local scope
1161cb0ef41Sopenharmony_ci */
1171cb0ef41Sopenharmony_ciunsigned int  ares__iface_ips_get_ll_scope(const ares__iface_ips_t *ips,
1181cb0ef41Sopenharmony_ci                                           size_t                   idx);
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci
1211cb0ef41Sopenharmony_ci/*! Retrieve the interface index (aka link local scope) from the interface
1221cb0ef41Sopenharmony_ci *  name.
1231cb0ef41Sopenharmony_ci *
1241cb0ef41Sopenharmony_ci * \param[in] name  Interface name
1251cb0ef41Sopenharmony_ci * \return 0 on failure, index otherwise
1261cb0ef41Sopenharmony_ci */
1271cb0ef41Sopenharmony_ciunsigned int  ares__if_nametoindex(const char *name);
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci/*! Retrieves the interface name from the index (aka link local scope)
1301cb0ef41Sopenharmony_ci *
1311cb0ef41Sopenharmony_ci * \param[in] index  Interface index (> 0)
1321cb0ef41Sopenharmony_ci * \param[in] name   Buffer to hold name
1331cb0ef41Sopenharmony_ci * \param[in] name_len Length of provided buffer, must be at least IF_NAMESIZE
1341cb0ef41Sopenharmony_ci * \return NULL on failure, or pointer to name on success
1351cb0ef41Sopenharmony_ci */
1361cb0ef41Sopenharmony_ciconst char   *ares__if_indextoname(unsigned int index, char *name,
1371cb0ef41Sopenharmony_ci                                   size_t name_len);
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci#endif
140