diff options
-rw-r--r-- | miniwifi.c | 127 |
1 files changed, 57 insertions, 70 deletions
@@ -20,115 +20,102 @@ int main() { 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; + 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); + // 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); + 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()"); - } + if (ioctl_res < 0) { + perror("ioctl()"); + } #endif - close(station_fd); - ip_addr = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); + 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); + 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 = "-"; + 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); + 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; + } 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); + 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); + // 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)); + 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()"); - } + if (connect_res < 0) { + perror("connect()"); + } #endif - fd_set fdset; - FD_ZERO(&fdset); - FD_SET(remote_fd, &fdset); + 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); + 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()"); - } + if (select_res < 0) { + perror("select()"); + } #endif - if (select_res == 1) { + if (select_res == 1) { #ifdef DEBUG - printf("select() returned 1\n"); + 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); + 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) { + // we have a good connection + if (so_error == 0) { #ifdef DEBUG - printf("so_error set to 0, we must have internet\n"); + printf("so_error set to 0, we must have internet\n"); #endif - color = COLOR_CONNECTED; - icon = ICON_CONNECTED; - } + color = COLOR_CONNECTED; + icon = ICON_CONNECTED; } - - close(remote_fd); } - // print results + close(remote_fd); + } + + // print results #ifndef DEBUG - printf(fmt, color, icon, ssid, ip_addr); + printf(fmt, color, icon, ssid, ip_addr); #endif - fflush(stdout); - sleep(1); - } return 0; } |