How to get the ESSID of the Wifi network you are connected to? (C Source Code)


NOTE
This article has already been moved to the our new blog. Please update any link to this post as it will be removed in the future. This post is available clicking the link below:

http://papermint-designs.com/dmo-blog/2016-08-how-to-get-the-essid-of-the-wifi-network-you-are-connected-to-

Recently I had to solve the problem of finding the wifi ESSID of the current wifi connection. Well, the solution is nothing special, but I'm writing this, because; first it was not straightforward to get the snippet, and second, because of the way I found the solution. Just for clarification, what I was looking for was C language function to get the ESSID in my own application in order to do something with it.

The closest solution I found was to use an external application and capture its output. That was not really acceptable. Probably, I was using the wrong keywords in my queries, or Google was feeling that I was a different kind of user (or a completely different one), or something else,... The actual fact was that I had to find a solution by other means.

At the end it was quite easy. I just used strace to find out how iwconfig gets the information about my current wireless settings. Below you can find the relevant part.

$ strace -v iwconfig wlan0
...
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3
ioctl(3, SIOCGIWNAME, 0x7fff88f9b130)   = 0
ioctl(3, SIOCGIWNWID, 0x7fff88f9b130)   = -1 EOPNOTSUPP (Operation not supported)
ioctl(3, SIOCGIWFREQ, 0x7fff88f9b130)   = 0
ioctl(3, SIOCGIWENCODE, 0x7fff88f9b130) = -1 EPERM (Operation not permitted)
ioctl(3, SIOCGIWESSID, 0x7fff88f9b130)  = 0
...
So, that is the ioctl call you have to invoke to get the information (and actually a bunch of other interface related information). Then, looking for the ioctl I found a great article here on the topic (http://www.linux-mag.com/id/6206/). Well, and just in case you get here actually looking for that snippet and you want to save one click to the article linked above... here it is the code.
#include <stdio.h>  // printf
#include <string.h> // strdup prototype
#include <stdlib.h> // free protype

/* socket systemcall */
#include <sys/types.h>
#include <sys/socket.h>


#include <linux/if.h>

/* Wireless monitoring */
#include <linux/wireless.h>

char *
get_essid (char *iface)
{
   int           fd;
   struct iwreq  w;
   char          essid[IW_ESSID_MAX_SIZE];

   if (!iface) return NULL;

   fd = socket(AF_INET, SOCK_DGRAM, 0);

   strncpy (w.ifr_ifrn.ifrn_name, iface, IFNAMSIZ);
   memset (essid, 0, IW_ESSID_MAX_SIZE);
   w.u.essid.pointer = (caddr_t *) essid;
   w.u.data.length = IW_ESSID_MAX_SIZE;
   w.u.data.flags = 0;

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

   return strdup (essid);
}

int
main (int argc, char *argv[])
{
  char *essid = NULL;

  printf ("ESSID: %s\n", (essid = get_essid ("wlan0")));
  free (essid); // Remember you have to free the memory.... or change get_essid implementation :)

  return 0;
}

Hope it helps CU