频道频率号码

时间:2013-10-03 11:00:13

标签: c linux

我已经编写了打印网络接口信息的代码,例如“名称”,“地址”,“网络掩码”,“MAC地址”。

但我想将wlan0的“频道频率”添加到我的代码并打印此信息,但我不知道该怎么做。

这是我的代码:

int wlan(){

     struct ifreq ifr;
        int sock, j, k;
        char *p, addr[32], mask[32], mac[32];

        sock=socket(PF_INET, SOCK_STREAM, 0);
        if (-1==sock) {
            perror("socket() ");
            return 1;
        }    

        strncpy(ifr.ifr_name,"wlan0",sizeof(ifr.ifr_name)-1);
        ifr.ifr_name[sizeof(ifr.ifr_name)-1]='\0';  

        if (-1==ioctl(sock, SIOCGIFADDR, &ifr)) {
            perror("ioctl(SIOCGIFADDR) ");
            return 1;
        }
        p=inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_addr))->sin_addr);
        strncpy(addr,p,sizeof(addr)-1);
        addr[sizeof(addr)-1]='\0';

        if (-1==ioctl(sock, SIOCGIFNETMASK, &ifr)) {
            perror("ioctl(SIOCGIFNETMASK) ");
            return 1;
        }
        p=inet_ntoa(((struct sockaddr_in *)(&ifr.ifr_netmask))->sin_addr);
        strncpy(mask,p,sizeof(mask)-1);
        mask[sizeof(mask)-1]='\0';

        if (-1==ioctl(sock, SIOCGIFHWADDR, &ifr)) {
            perror("ioctl(SIOCGIFHWADDR) ");
            return 1;
        }
        for (j=0, k=0; j<6; j++) {
            k+=snprintf(mac+k, sizeof(mac)-k-1, j ? ":%02X" : "%02X",
                (int)(unsigned int)(unsigned char)ifr.ifr_hwaddr.sa_data[j]);
        }
        mac[sizeof(mac)-1]='\0';

        printf("\n");
        printf("name:    %s\n",ifr.ifr_name);
        printf("address: %s\n",addr);
        printf("netmask: %s\n",mask);
        printf("macaddr: %s\n",mac);
        printf("\n");

        close(sock);
        return 0;
} 

1 个答案:

答案 0 :(得分:0)

尝试使用ioctl()命令SIOCGIWFREQ。它在这里提到: http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/wireless.h?v=2.6.11.8