diff options
author | Paul Oliver <contact@pauloliver.dev> | 2025-05-06 18:30:39 +0200 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2025-05-06 21:29:05 +0200 |
commit | 965bc45937fdfe2a20bc284c3ccc590d01eee389 (patch) | |
tree | e471c8a5da2f09b069dd77f09515b32efbf52f3d /miniwifi.c |
Initial
Diffstat (limited to 'miniwifi.c')
-rw-r--r-- | miniwifi.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/miniwifi.c b/miniwifi.c new file mode 100644 index 0000000..b85c38a --- /dev/null +++ b/miniwifi.c @@ -0,0 +1,134 @@ +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include <arpa/inet.h> +#include <net/if.h> +#include <sys/ioctl.h> + +#include "wifi_scan.h" + +#define COLOR_CONNECTED "#859900" +#define COLOR_DISCONNECTED "#dc322f" +#define ICON_CONNECTED "f05a9" +#define ICON_DISCONNECTED "f16bc" + +int main() { + const char *ip_addr = NULL; + const char *ssid = NULL; + const char *color = NULL; + const char *icon = NULL; + const char *fmt = "<span color=\"%s\"><span font=\"Symbols Nerd Font\">&#x%s;</span> %s %s</span>\n"; + +#ifdef DEBUG + for (int i = 0; true; ++i) { +#else + for (;;) { +#endif + +#ifdef DEBUG + printf("\nloop #%d:\n", ++i); +#endif + + color = COLOR_DISCONNECTED; + icon = ICON_DISCONNECTED; + + // check if wlan0 has an IP address + struct ifreq ifr; + ifr.ifr_addr.sa_family = AF_INET; + strncpy(ifr.ifr_name, "wlan0", IFNAMSIZ - 1); + + int station_fd = socket(AF_INET, SOCK_DGRAM, 0); + int ioctl_res = ioctl(station_fd, SIOCGIFADDR, &ifr); + +#ifdef DEBUG + if (ioctl_res < 0) { + perror("ioctl()"); + } +#endif + + close(station_fd); + ip_addr = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); + +#ifdef DEBUG + printf("ip_addr set to: %s\n", ip_addr); +#endif + + if (ioctl_res != 0 || strcmp(ip_addr, "0.0.0.0") == 0) { + ssid = "-"; + ip_addr = "-"; + +#ifdef DEBUG + printf("ioctl() failed or ip set to 0:0:0:0, ssid set to: %s, ip_addr set to: %s\n", ssid, ip_addr); +#endif + } else { + // get ssid in case we have an IP + struct station_info station; + wifi_scan_station(wifi_scan_init("wlan0"), &station); + ssid = station.ssid; + +#ifdef DEBUG + printf("ssid set to: %s\n", ssid); +#endif + + // lastly, check if we can ping google + struct sockaddr_in sock_in; + sock_in.sin_family = AF_INET; + sock_in.sin_port = htons(443); + inet_pton(AF_INET, "8.8.8.8", &sock_in.sin_addr); + + int remote_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); + int connect_res = connect(remote_fd, (struct sockaddr *)&sock_in, sizeof(sock_in)); + +#ifdef DEBUG + if (connect_res < 0) { + perror("connect()"); + } +#endif + + fd_set fdset; + FD_ZERO(&fdset); + FD_SET(remote_fd, &fdset); + + struct timeval tv = { .tv_sec = 1, .tv_usec = 0 }; + int select_res = select(remote_fd + 1, NULL, &fdset, NULL, &tv); + +#ifdef DEBUG + if (select_res < 0) { + perror("select()"); + } +#endif + + if (select_res == 1) { +#ifdef DEBUG + printf("select() returned 1\n"); +#endif + + int so_error; + socklen_t len = sizeof so_error; + getsockopt(remote_fd, SOL_SOCKET, SO_ERROR, &so_error, &len); + + // we have a good connection + if (so_error == 0) { +#ifdef DEBUG + printf("so_error set to 0, we must have internet\n"); +#endif + + color = COLOR_CONNECTED; + icon = ICON_CONNECTED; + } + } + + close(remote_fd); + } + + // print results +#ifndef DEBUG + printf(fmt, color, icon, ssid, ip_addr); +#endif + fflush(stdout); + sleep(1); + } + + return 0; +} |