HalosGhost / dstat

A lightweight status program for simple WMs
GNU General Public License v2.0
8 stars 0 forks source link

Add SSID name to wifi widget #7

Closed HalosGhost closed 6 years ago

HalosGhost commented 7 years ago

It would be really handy to know what SSID I am connected to.

Doing this in shell is very simple:

$ iw dev wlp3s0 link | awk '/SSID:/ { print substr($0, index($0, $2)) }'
NETGEAR97-5G

Converting this to C might be a bit intense, but it would be really nice.


Seems like the best way to figure out how to do this is to copylearn from iw

HalosGhost commented 7 years ago

rough proof of concept. Does essentially no error handling, but should return the currently-associated AP's ESSID.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stropts.h>
#include <unistd.h>
#include <linux/if.h>
#include <linux/wireless.h>

signed
get_essid (const char * iface, char * ssid);

signed
main (void) {

    char essid[IW_ESSID_MAX_SIZE] = "";
    signed err = get_essid("wl0", essid);

    if ( !err ) { puts(essid); }

    return EXIT_SUCCESS;
}

signed
get_essid (const char * iface, char * essid) {

    if ( !iface ) {
        return EXIT_FAILURE;
    }

    signed sock = socket(AF_INET, SOCK_DGRAM, 0);
    struct iwreq w;

    strncpy(w.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);

    w.u.essid.pointer = essid;
    w.u.data.length = IW_ESSID_MAX_SIZE;
    w.u.data.flags = 0;

    ioctl(sock, SIOCGIWESSID, &w);
    close(sock);

    return EXIT_SUCCESS;
}
HalosGhost commented 6 years ago

via 5602f1760df315b803f731f6201f87e757ed21f6