Mstdlib-1.24.0
m_io_net_iface_ips.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2022 Monetra Technologies, LLC.
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 shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_IO_NET_IFACE_IPS_H__
25#define __M_IO_NET_IFACE_IPS_H__
26
27#include <mstdlib/mstdlib.h>
28
29__BEGIN_DECLS
30
31/*! \addtogroup m_io_net_iface_ips Network Interface Enumeration
32 * \ingroup m_io_net
33 *
34 * Network Interface Enumeration
35 *
36 * @{
37 *
38 */
39
41/* Data structure to hold result of M_io_net_iface_ips() */
43
44/* Flags that can be passed to M_io_net_iface_ips() or returned from M_io_net_iface_ips_get_flags() */
45typedef enum {
46 M_NET_IFACE_IPS_FLAG_OFFLINE = 1 << 0, /*!< Interface is currently offline. For enumeration, by default does
47 * not return offline interfaces without this flag. */
48 M_NET_IFACE_IPS_FLAG_LOOPBACK = 1 << 1, /*!< Interface is loopback. For enumeration, by default, does not
49 * return addresses for loopback interfaces. */
50 M_NET_IFACE_IPS_FLAG_IPV4 = 1 << 2, /*!< Address is ipv4. For enumeration, only list interfaces and addresses
51 * containing ipv4 addresses. */
52 M_NET_IFACE_IPS_FLAG_IPV6 = 1 << 3, /*!< Address is ipv6. For enumeration, only list interfaces and addresses
53 * containing ipv6 addresses. */
55
56
57/*! Query OS for network interfaces and ip addresses assigned to interfaces.
58 *
59 * \param[in] flags M_io_net_iface_ips_flags_t flags or 0 for none
60 * \return M_io_net_iface_ips_t * on success or NULL on failure. Must be free'd with
61 * M_io_net_iface_ips_free()
62 */
64
65/*! Return count of all interfaces and ip addresses. Note that not all
66 * interfaces may contain ip addreses.
67 *
68 * \param[in] ips Value returned from M_io_net_iface_ips()
69 * \return count
70 */
72
73/*! Return name of interface associated with specified index.
74 *
75 * \param[in] ips Value returned from M_io_net_iface_ips()
76 * \param[in] idx Index to query
77 * \return NULL on failure otherwise a pointer to the name of the interface
78 */
79M_API const char *M_io_net_iface_ips_get_name(M_io_net_iface_ips_t *ips, size_t idx);
80
81/*! Return ip address of interface associated with specified index.
82 *
83 * \param[in] ips Value returned from M_io_net_iface_ips()
84 * \param[in] idx Index to query
85 * \return NULL on failure (or even possibly if no ip address available for
86 * interface), otherwise a pointer to the name of the interface
87 */
88M_API const char *M_io_net_iface_ips_get_addr(M_io_net_iface_ips_t *ips, size_t idx);
89
90/*! Return the netmask for the ip address of interface associated with specified index.
91 *
92 * \param[in] ips Value returned from M_io_net_iface_ips()
93 * \param[in] idx Index to query
94 * \return netmask, only relevant if there is an ip address
95 */
97
98/*! Return flags on interface associated with specified index.
99 *
100 * \param[in] ips Value returned from M_io_net_iface_ips()
101 * \param[in] idx Index to query
102 * \return flags associated with the interface
103 */
105
106/*! Retrieve a list of ip addresses from the result set matching the query.
107 * Will only return ip addresses and not any flags or interface names.
108 *
109 * \param[in] ips Value returned from M_io_net_iface_ips()
110 * \param[in] flags Must specify at least M_NET_IFACE_IPS_FLAG_IPV4 or M_NET_IFACE_IPS_FLAG_IPV6.
111 * \param[in] name Only enumerate for a specific interface name.
112 * \return list of ip addresses matching query, NULL on no matches.
113 * Must be free'd with M_list_str_destroy().
114 */
115M_API M_list_str_t *M_io_net_iface_ips_get_ips(M_io_net_iface_ips_t *ips, int flags, const char *name);
116
117/*! Retrieve a list of interfaces from the result set matching the query.
118 * Will only return interface names and not any flags or ip addresses.
119 *
120 * \param[in] ips Value returned from M_io_net_iface_ips()
121 * \param[in] flags If either M_NET_IFACE_IPS_FLAG_IPV4 or M_NET_IFACE_IPS_FLAG_IPV6 is specified, will
122 * exclude interfaces that don't have the specified address class.
123 * \param[in] ipaddr Optional. Use NULL if not wanted. Search for interface containing specified
124 * IP address.
125 * \return list of interface names matching query, NULL on no matches.
126 * Must be free'd with M_list_str_destroy().
127 */
128M_API M_list_str_t *M_io_net_iface_ips_get_names(M_io_net_iface_ips_t *ips, int flags, const char *ipaddr);
129
130/*! Free the ip address returned from M_io_net_iface_ips().
131 *
132 * \param[in] ips Value returned from M_io_net_iface_ips()
133 */
135
136/*! Given a set of flags, convert into human-readable form
137 *
138 * \param[in] flags Flags to print
139 * \return human readable string, must be M_free()'d.
140 */
141M_API char *M_io_net_iface_ips_flags_to_str(int flags);
142
143
144/*! @} */
145
146__END_DECLS
147
148#endif /* __M_NET_IFACE_IPS_H__ */
M_io_net_iface_ips_t * M_io_net_iface_ips(int flags)
size_t M_io_net_iface_ips_count(M_io_net_iface_ips_t *ips)
M_list_str_t * M_io_net_iface_ips_get_ips(M_io_net_iface_ips_t *ips, int flags, const char *name)
int M_io_net_iface_ips_get_flags(M_io_net_iface_ips_t *ips, size_t idx)
char * M_io_net_iface_ips_flags_to_str(int flags)
M_uint8 M_io_net_iface_ips_get_netmask(M_io_net_iface_ips_t *ips, size_t idx)
void M_io_net_iface_ips_free(M_io_net_iface_ips_t *ips)
M_io_net_iface_ips_flags_t
Definition: m_io_net_iface_ips.h:45
struct M_io_net_iface_ips M_io_net_iface_ips_t
Definition: m_io_net_iface_ips.h:42
const char * M_io_net_iface_ips_get_addr(M_io_net_iface_ips_t *ips, size_t idx)
const char * M_io_net_iface_ips_get_name(M_io_net_iface_ips_t *ips, size_t idx)
M_list_str_t * M_io_net_iface_ips_get_names(M_io_net_iface_ips_t *ips, int flags, const char *ipaddr)
@ M_NET_IFACE_IPS_FLAG_LOOPBACK
Definition: m_io_net_iface_ips.h:48
@ M_NET_IFACE_IPS_FLAG_IPV6
Definition: m_io_net_iface_ips.h:52
@ M_NET_IFACE_IPS_FLAG_IPV4
Definition: m_io_net_iface_ips.h:50
@ M_NET_IFACE_IPS_FLAG_OFFLINE
Definition: m_io_net_iface_ips.h:46
struct M_list_str M_list_str_t
Definition: m_list_str.h:80