How do I get a notification when a station is associated?

I am trying to get a notification every time a client get associated/disassociated, from a C/C++ program. I've tried to make a program which uses the hostapd control interface and it worked great. Except that the resulting api looks quite unstable: it returns a simple string when returning the MAC address of the client getting associated.

Here is the sample program:

#include <stdio.h>

#include "wpa_ctrl.h"

int main() {
  struct wpa_ctrl *ctrl_conn = wpa_ctrl_open("/var/run/hostapd/wlan0");
  if (ctrl_conn == NULL) {
    printf("Impossible to reconnect to /var/run/hostapd/wlan0\n");
    return 1;
  }
  printf("/var/run/hostapd/wlan0 is connected.\n");

  int res = wpa_ctrl_attach(ctrl_conn);
  if (res != 0) {
    printf("Impossible to attach /var/run/hostapd/wlan0.\n");
    return 1;
  }
  printf("/var/run/hostapd/wlan0 is attached.\n");


  char buf[4096];
  size_t len = sizeof(buf) - 1;
  while (wpa_ctrl_recv(ctrl_conn, buf, &len) == -1) {
    printf("Impossible to recv msgs.\n");
    sleep(1);
    // return 1;
  }
  buf[len] = '\0';
  printf("recv a msg : len=%d. buf=%s\n", len, buf);

  return 0;
}

The output is :

...
recv a msg : len=37. buf=<3>AP-STA-CONNECTED 52:14:9f:54:e7:40

As you can see, the ouput is not the kind of stable API one can dream about.

So my question is: is there a better way to get the notifications when someone gets associated/dissociated?

If not so, do you think it is possible to extend hostapd to bring those kind of notifications into ubus? (And support those code modifications into the git repository?)

1 Like

I would recommend you using the package luci-mod-rpc in your openwrt (opkg install luci-mod-rpc) and probing its API: https://openwrt-luci-rpc.readthedocs.io/en/latest/usage.html

1 Like

Actually we found that hostapd is already sending that kind of event into ubus.
You just need to 'subscribe' using the ubus client tool. Subscribe command is not documented, so you use at your own peril. Here is a sample output:

# ubus subscribe hostapd.wlan0
...
{ "probe": {"address":"52:14:9f:54:e7:40","target":"ff:ff:ff:ff:ff:ff","signal":-41,"freq":5765,"ht_capabilities":{"ht_capabilities_info":28416,"supported_mcs_set":{"a_mpdu_params":23,"ht_extended_capabilities":0,"tx_bf_capability_info":0,"asel_capabilities":0,"supported_mcs_set":[255,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}},"vht_capabilities":{"vht_capabilities_info":846233615,"vht_supported_mcs_set":{"rx_map":-257,"rx_highest":0,"tx_map":-257,"tx_highest":0}}} }
{ "auth": {"address":"52:14:9f:54:e7:40","target":"04:f0:21:25:b8:4f","signal":-43,"freq":5765} }
{ "assoc": {"address":"52:14:9f:54:e7:40","target":"04:f0:21:25:b8:4f","signal":-54,"freq":5765} }
...
{ "disassoc": {"address":"52:14:9f:54:e7:40"} }
{ "disassoc": {"address":"52:14:9f:54:e7:40"} }

Hope this may help someone else in the future.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.