Skip to content

Instantly share code, notes, and snippets.

@sepfy
Created February 14, 2021 14:00
Show Gist options
  • Save sepfy/25c1a34e9b418de230a1d97c56de88a6 to your computer and use it in GitHub Desktop.
Save sepfy/25c1a34e9b418de230a1d97c56de88a6 to your computer and use it in GitHub Desktop.
libwpa_client monitor example
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/time.h>
#include <wpa_ctrl.h>
#define CTRL_INTERFACE_DIR "/var/run/wpa_supplicant/wlan0"
int main(int argc, char *argv[]) {
int ret;
int conn_fd = -1;
fd_set rfds;
char buf[256];
size_t len = sizeof(buf) - 1;
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
struct wpa_ctrl *monitor_conn = NULL;
monitor_conn = wpa_ctrl_open(CTRL_INTERFACE_DIR);
if(monitor_conn == NULL) {
fprintf(stderr, "Open monitor conn failed");
return -1;
}
if(wpa_ctrl_attach(monitor_conn) != 0) {
fprintf(stderr, "Attach monitor conn failed");
return -1;
}
if((conn_fd = wpa_ctrl_get_fd(monitor_conn)) < 0) {
fprintf(stderr, "Monitor conn get failed");
return -1;
}
while(monitor_conn) {
FD_ZERO(&rfds);
FD_SET(conn_fd, &rfds);
if(select(conn_fd + 1, &rfds, NULL, NULL, &tv) < 0) {
break;
}
if(FD_ISSET(conn_fd, &rfds)) {
len = sizeof(buf) - 1;
ret = wpa_ctrl_recv(monitor_conn, buf, &len);
if(ret < 0) {
break;
}
buf[len] = '\0';
printf("%s\n", buf);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment