How to get IP and MAC address in my C program for

2019-08-21 19:44发布

问题:

I try to create client application that can be installed into Wi-Fi Router(OpenWRT Attitude Adjustment 12.09) Application must be written in C and implement OpenWRT daemon approach. When any visitor turns Wi-Fi and connects to my SSID I need use his IP and MAC address in my C program. How can I get IP and MAC address in my C program for any new connected users(devise)? I started to try use arp command for any IP which already connected to router:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() 
{
    char* ip = "192.168.1.101";
    char output[255];
    char command [255];
    //system("arp -a");
    sprintf(command,"%s %s %s","arp","-a",ip);
    FILE* arp = popen(command, "r" );

    int i = 1;

    while (fgets(output, sizeof(output), arp) != 0) {
        i++;
        if ( i == 2 ) {
            printf("%s",output);
            char macAddress[18];
            int index = (int)(strchr(output,'-')-output);
            printf( "\n%d",index);
            printf ("\n----%c", output[index-2]);
            memcpy(macAddress, &output[index-2], 17);
            macAddress[17] = '\0';
            printf("\n%s",macAddress);
        }
    }

    pclose(arp);

    return 0;
}

system("arp -a"); works in Ubuntu but not for OpenWRT after Cross compile! Maybe I need to choose other way?

thanks in advance for any help

标签: c wifi openwrt