-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.c
More file actions
149 lines (116 loc) · 3.12 KB
/
interface.c
File metadata and controls
149 lines (116 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2025, Muhammad Saheed <saheed@FreeBSD.org>
*/
#include <sys/types.h>
#include <sys/pciio.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <getopt.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "interface.h"
#include "libifconfig.h"
const char *connection_state_to_string[] = {
[CONNECTED] = "Connected",
[DISCONNECTED] = "Disconnected",
[UNPLUGGED] = "Unplugged",
[DISABLED] = "Disabled",
[NA] = "N/A",
};
void
is_ifaddr_af_inet(ifconfig_handle_t *lifh, struct ifaddrs *ifa, void *udata)
{
bool *is_af_inet = udata;
(void)lifh;
if (is_af_inet == NULL)
return;
if (ifa->ifa_addr->sa_family == AF_INET ||
ifa->ifa_addr->sa_family == AF_INET6) {
*is_af_inet = true;
}
}
enum connection_state
get_connection_state(struct ifconfig_handle *lifh, struct ifaddrs *ifa)
{
bool is_interface_online = false;
struct ifmediareq *ifmr;
enum connection_state state = NA;
const char *status;
if (lifh == NULL || ifa == NULL)
return (NA);
ifconfig_foreach_ifaddr(lifh, ifa, is_ifaddr_af_inet,
&is_interface_online);
if (ifconfig_media_get_mediareq(lifh, ifa->ifa_name, &ifmr) != 0)
return (NA);
status = ifconfig_media_get_status(ifmr);
if (strncmp("wlan", ifa->ifa_name, strlen("wlan")) == 0) {
state = (ifa->ifa_flags & IFF_UP) == 0 ? DISABLED :
strcmp(status, "associated") == 0 && is_interface_online ?
CONNECTED :
DISCONNECTED;
} else if (strcmp(status, "active") == 0) {
state = is_interface_online ? CONNECTED : DISCONNECTED;
} else {
state = UNPLUGGED;
}
free(ifmr);
return (state);
}
void
get_mac_addr(ifconfig_handle_t *lifh, struct ifaddrs *ifa, void *udata)
{
struct ether_addr *ea = udata;
struct sockaddr_dl *sdl = (void *)ifa->ifa_addr;
(void)lifh;
if (ea == NULL)
return;
if (sdl->sdl_family == AF_LINK && sdl->sdl_alen == ETHER_ADDR_LEN)
memcpy(ea, LLADDR(sdl), ETHER_ADDR_LEN);
}
bool
is_wlan_group(struct ifconfig_handle *lifh, const char *ifname)
{
struct ifgroupreq ifgr;
if (ifname == NULL)
return (false);
if (if_nametoindex(ifname) == 0) /* returns 0 if invalid i.e false */
return (false);
if (ifconfig_get_groups(lifh, ifname, &ifgr) == -1)
return (false);
for (size_t i = 0; i < ifgr.ifgr_len / sizeof(struct ifg_req); i++) {
struct ifg_req *ifg = &ifgr.ifgr_groups[i];
if (strcmp(ifg->ifgrq_group, "wlan") == 0)
return (true);
}
return (false);
}
int
get_iface_parent(const char *ifname, int ifname_len, char *buf, int buf_len)
{ /* assumes ifname[ifname_len] == '\0' */
char name[32];
int group_len = sizeof("wlan") - 1;
size_t len = buf_len;
if (ifname_len - group_len <= 0)
return (1);
if (snprintf(name, sizeof(name), "net.wlan.%s.%%parent",
ifname + group_len) >= (int)sizeof(name))
return (1);
if (sysctlbyname(name, buf, &len, NULL, 0) == -1)
return (1);
if ((int)len >= buf_len)
len = buf_len - 1;
buf[len] = '\0';
return (0);
}