meep
meep

Reputation: 334

iwlib.h Print BSSID of nearby Access Points

So recently I've been looking into Beacon Frames and 802.11 packets in C and came across iwlib.h in Linux. I made a tiny snippet of code to show all nearby networks and their SSID. Here is the code:

#include <stdio.h>
#include <iwlib.h>

int main() {
    wireless_scan_head head;
    wireless_scan *result;

    int sockfd = iw_sockets_open();
    iw_get_range_info(sockfd "wlan0", &range);
    result = head.result

    do {
        printf ("%s\n", result->b.essid);
        result = result->next;
    } while(result != NULL);

    return 0;
}

Is there any way of extracting the BSSID/AP MAC address using this code in such a way I can print it like FF:12:34:56:AB:CD or FF123456ABCD? Any help will be much appreciated! Many thanks.

Upvotes: 1

Views: 651

Answers (1)

Gaurav Pathak
Gaurav Pathak

Reputation: 1143

Unfortunately, the support to read the Mac Address is disabled in iwlib, there is an API iw_get_mac_addr() but it's disabled using #if 0

Refer:

However the MAC address of a specific interface can be easily pulled using its socket descriptor.
The below C example code assumes the interface for WiFi has name "wlp3s0".

#include <stdio.h>
#include <time.h>
#include <iwlib.h>

int main(void) {
  wireless_scan_head head;
  wireless_scan *result;
  iwrange range;
  int sock;
  struct ifreq s;

  sock = iw_sockets_open();

  if (iw_get_range_info(sock, "wlp3s0", &range) < 0) {
    printf("Error during iw_get_range_info.\n");
    exit(2);
  }

  if (iw_scan(sock, "wlp3s0", range.we_version_compiled, &head) < 0) {
    printf("Error during iw_scan.\n");
    exit(2);
  }
  
  strcpy(s.ifr_name, "wlp3s0");
  if (0 == ioctl(sock, SIOCGIFHWADDR, &s)) {
    int i;
    for (i = 0; i < 6; ++i)
      printf("%02x", (unsigned char) s.ifr_addr.sa_data[i]);
      puts("\n");
  }

  result = head.result;
  while (NULL != result) {
    printf("%s\n", result->b.essid);
    result = result->next;
  }

  exit(0);
}

Upvotes: 0

Related Questions