From 02f1cdaaf6660e70d3a609f0e0ec397287777303 Mon Sep 17 00:00:00 2001 From: onelin Date: Sun, 19 Apr 2026 23:02:08 +0200 Subject: Limit network interfaces to the ones you specify --- status.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 9 deletions(-) diff --git a/status.c b/status.c index 38ee75a..9499861 100644 --- a/status.c +++ b/status.c @@ -46,6 +46,7 @@ struct battery_status { (INET_ADDRSTRLEN > INET6_ADDRSTRLEN) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN struct interface_status { enum { loopback, ethernet, wifi, wan } type; + bool up; struct { char ip4[INET_ADDRSTRLEN]; char ip6[INET6_ADDRSTRLEN]; @@ -61,6 +62,8 @@ void get_battery1_status(char* buf); void get_battery0_status(char* buf); void get_all_bat_status(char* buf); void get_net_link_status(char* buf); +static void get_link_status(char* link_name, char* buf); +void get_wlp1s0_status(char* buf) { get_link_status("wlp1s0", buf); } /* Data */ static char* battery_level_icon[] = { @@ -83,10 +86,8 @@ static struct element statusbar[] = { /* Add status elements here */ /*{element_function, {seconds, nanoseconds}, {0}, {0}},*/ - {get_net_link_status, {15, 0}, {0}, {0}}, + { get_wlp1s0_status, {15, 0}, {0}, {0}}, {get_battery0_status, {60, 0}, {0}, {0}}, - {get_battery1_status, {60, 0}, {0}, {0}}, - { get_all_bat_status, {60, 0}, {0}, {0}}, { date, {7, MSEC(500)}, {0}, {0}}, }; @@ -192,7 +193,7 @@ void get_essid(char* if_name, char* dst) { /* Get the SSID */ struct iwreq wreq; - const size_t if_namelen = strlen(if_name) - 1; + const size_t if_namelen = strlen(if_name); const size_t l = if_namelen > IFNAMSIZ ? IFNAMSIZ : if_namelen; int sock = socket(AF_INET, SOCK_DGRAM, 0); @@ -203,6 +204,7 @@ get_essid(char* if_name, char* dst) { memset(&wreq, 0, sizeof(struct iwreq)); if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, if_name, l) == -1) { + printf("Err: %s\n", strerror(errno)); close(sock); return; } @@ -212,12 +214,17 @@ get_essid(char* if_name, char* dst) { wreq.u.essid.pointer = dst; wreq.u.essid.length = IW_ESSID_MAX_SIZE; + // May need to open a new socket, rebind, or something in-between to get both + // wireless capabilities and essid?? + + + // Test if we have wireless on this interface - if (ioctl(sock, SIOCGIWNAME, &wreq) != -1) { - // protocol stored in wreq.u.name - close(sock); - return; - } + //if (ioctl(sock, SIOCGIWNAME, &wreq) < 0) { + // // protocol stored in wreq.u.name + // close(sock); + // return; + //} if (ioctl(sock, SIOCGIWESSID, &wreq) < 0) { dst[0] = '\0'; @@ -372,6 +379,80 @@ get_net_link_status(char* buf) { } } +void get_link_status(char* link_name, char* buf) { + struct interface_status if_status; + struct ifaddrs* if_addr; + struct ifaddrs* ifa; + + memset(&if_status, 0, sizeof(struct interface_status)); + const int nlen = strlen(link_name) > IFNAMSIZ + 1 ? IFNAMSIZ : strlen(link_name); + strncpy(if_status.name, link_name, nlen); + + getifaddrs(&if_addr); + + for (ifa = if_addr; ifa != NULL; ifa = ifa->ifa_next) { + size_t i = 0; + const char* name = ifa->ifa_name; + + if (strcmp(if_status.name, name)) continue; + + if (ifa->ifa_flags & IFF_LOWER_UP) { + if_status.up = true; + + if (ifa->ifa_addr != NULL) { + const int family = ifa->ifa_addr->sa_family; + + size_t strsize = 0; + char* dst = NULL; + + if (family == AF_INET) { + strsize = INET_ADDRSTRLEN; + dst = if_status.address.ip4; + } else if (family == AF_INET6) { + strsize = INET6_ADDRSTRLEN; + dst = if_status.address.ip6; + } else if (family == AF_PACKET) { + continue; + } else + /* In this case, there's probably something horribly wrong */ + continue; + + /* Get the IP address */ + inet_ntop(family, (void*)&((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, + dst, strsize); + } + } + + } + + freeifaddrs(if_addr); + + // print interface + if (!if_status.up) return; + + get_essid(if_status.name, if_status.ssid); + + strcat(buf, if_status.name); + + if (if_status.ssid[0] != 0) { + strcat(buf, " ("); + strcat(buf, if_status.ssid); + strcat(buf, ")"); + } + if (if_status.address.ip4[0] != 0 || if_status.address.ip6[0] != 0) { + strcat(buf, " "); + if (if_status.address.ip4[0] != 0) { + strcat(buf, "⁴"); + } + if (if_status.address.ip6[0] != 0) { + strcat(buf, "⁶"); + } + } +} + +/* external commands */ +/* I just couldn't be bothered to learn pipewire */ + static struct timespec time_add(struct timespec a, struct timespec b) { struct timespec dst; -- cgit v1.3