How to get IP and MAC address in my C program for connected Wi-Fi user?

孤人 提交于 2019-12-12 02:03:32

问题


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

来源:https://stackoverflow.com/questions/28279887/how-to-get-ip-and-mac-address-in-my-c-program-for-connected-wi-fi-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!