Linux中c语言实现获取wifi状态

Linux中c语言实现获取wifi状态

    获取wifi信息有两种方案,参考ifconfig程序使用c语言实现,或者使用命令抓取关键词。

1、c语言实现

    废话不多说直接上代码。

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/wireless.h>
#include <string.h> 
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>

#define NET_PORT 53
#define NET_IP "8.8.8.8" //谷歌DNS

int main()
{
	int sockfd;
	struct iwreq wreq;
	struct iw_statistics stats;
	char buffer[32];
	int in_len=0;
  	struct sockaddr_in servaddr;
	memset(buffer, 0, 32);
	memset(&stats, 0, sizeof(stats));
	memset(&wreq, 0, sizeof(wreq));
	strcpy(wreq.ifr_name, "wlan0");
	
	if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) //建立socket,用于接收来自ioctl函数的消息。由于是sock_DGRAM,所以是UDP。如果是SOCK_STREAM,则是TCP。
	{
		perror("Could not create simple datagram socket");
		exit(EXIT_FAILURE);
	}

	in_len = sizeof(struct sockaddr_in);
    /*设置默认服务器的信息*/
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(NET_PORT);
	servaddr.sin_addr.s_addr = inet_addr(NET_IP);
	memset(servaddr.sin_zero,0,sizeof(servaddr.sin_zero));
	/*connect 函数*/
	if(connect(sockfd,(struct sockaddr* )&servaddr,in_len) < 0 ) 
	{   

	    printf("not connect to internet!\n ");
	    close(sockfd);
	    return 0; 
	}else{   
	    printf("       connect ok!       \n");
	}  

//ioctl获取Signal level
	wreq.u.data.pointer = &stats;
	wreq.u.data.length = sizeof(iw_statistics);
	if(ioctl(sockfd, SIOCGIWSTATS, &wreq) == -1) 
	{
		perror("Error performing SIOCGIWSTATS");
		close(sockfd);
		exit(EXIT_FAILURE);
	}else{
		// IW_QUAL_DBM表示Level + Noise are dBmm
		printf("Signal level%s is %d%s.\n",(stats.qual.updated & IW_QUAL_DBM ? " (in dBm)" :""),(signed char)stats.qual.level,
		(stats.qual.updated & IW_QUAL_LEVEL_UPDATED ? " (updated)" :""));
	}

//ioctl获取essid
	wreq.u.essid.pointer = buffer;//如果不写这行可能会错误
	wreq.u.essid.length = 32; 
	if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
	 	perror("IOCTL SIOCGIWESSID Failed,error");
	 	exit(2);
	}else {	
	 	//printf("IOCTL SIOCGIWESSID Successfull\n");
		printf("The essid is:%s\n",wreq.u.essid.pointer);        //network name
	}
	close(sockfd);
	return 0;
}

    首先建立socket,函数原型即:int socket(int domain, int type, int protocol),再建立connect连接,我是用的谷歌的DNS:8.8.8.8来测试连接外网。如果连接成功后就可获取wifi名字和强度了。
    接下来使用ioctl函数,ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,它的调用个数如下:
int ioctl(int fd, ind cmd, …);
    其中fd是用户程序打开设备时使用open函数返回的文件标示符,cmd是用户程序对设备的控制命令,至于后面的省略号,那是一些补充参数,一般最多一个,这个参数的有无和cmd的意义相关。
    ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。

2、命令抓取方式

    FILE* fp=NULL;
    char buf[100] = {0};    
    char command[300] = " sudo iwlist wlan0 scan";
    char str1[100];
    char str2[100];
    char *q = NULL;
    memset(str1, '\0', sizeof(str1));
    memset(str2, '\0', sizeof(str2));
    if((fp = popen(command, "r")) != NULL)
    {
     //   printf("popen command success\n");
        while(fgets(buf, sizeof(buf), fp) != NULL)
        {
            //printf("buf=%s\n", buf);
            q = strstr(buf, "ESSID:");
            if (q != NULL)
            {
                //	printf("q =NULL\n");
                sscanf(q, "ESSID:\"%[^\"]\"", str1);
                q = NULL;
            }
            q = strstr(buf, "Signal level=");
            if (q != NULL)
            {
                //	printf("q =NULL\n");
                sscanf(q, "Signal level=%[^/]", str2);
                q = NULL;
                strcat(str1,"");
                strcat(str1,str2);
                wifilist.append(str1);
            }
        }
        pclose(fp);
    }

    popen将命令sudo iwlist wlan0 scan通过管道读到fp,fgets函数从指定的流中读取数据,每次读取一行。其原型为:

 

ioctl查询无线网卡信息

/* -------------------------- IOCTL LIST-------------------------- */
/* Basic operations */

#define SIOCSIWCOMMIT      0x8B00          /* 提交修改结果 */

#define SIOCGIWNAME    0x8B01          /* 获取设备名字 */

#define SIOCSIWNWID     0x8B02          /* 设置网络ID */

#define SIOCGIWNWID     0x8B03          /* 获取网络ID */

#define SIOCSIWFREQ 0x8B04          /* 设置信道(频率) */

#define SIOCGIWFREQ      0x8B05          /*  获取信道(频率)  */

#define SIOCSIWMODE    0x8B06          /* 设置网卡模式*/

#define SIOCGIWMODE    0x8B07          /* 获取网卡模式 */

#define SIOCSIWSENS 0x8B08          /* 设置灵敏度(dBm) */

#define SIOCGIWSENS 0x8B09          /* 获取灵敏度 (dBm) */

/* Informative stuff */

#define SIOCSIWRANGE   0x8B0A         /* Unused */

#define SIOCGIWRANGE  0x8B0B          /* 获取参数范围 */

#define SIOCSIWPRIV  0x8B0C         /* Unused */

#define SIOCGIWPRIV 0x8B0D         /* 获取网卡特有的ioctl接口 */

#define SIOCSIWSTATS     0x8B0E          /* Unused */

#define SIOCGIWSTATS    0x8B0F          /* 获取 /proc/net/wireless的stats */

/* Mobile IP support */

#define SIOCSIWSPY    0x8B10          /* 设置spy 地址*/

#define SIOCGIWSPY   0x8B11          /* 获取spy信息(连接质量) */

/* Access Point manipulation */

#define SIOCSIWAP     0x8B14          /* 设置AP的mac地址 */

#define SIOCGIWAP     0x8B15          /* 获取AP的mac地址 */

#define SIOCGIWAPLIST   0x8B17          /* 获取周围Ap信息列表 */

#define SIOCSIWSCAN      0x8B18          /* 开始扫描 */

#define SIOCGIWSCAN     0x8B19          /* 获取扫描信息 */

/* 802.11 specific support */

#define SIOCSIWESSID      0x8B1A         /* 设置ESSID  */

#define SIOCGIWESSID     0x8B1B          /* 获取ESSID */

#define SIOCSIWNICKN    0x8B1C         /* 设置节点别名 */

#define SIOCGIWNICKN   0x8B1D         /* 获取节点别名 */

/* As the ESSID and NICKN are strings upto 32 bytes long, it doesn't fit

 * within the 'iwreq' structure, sowe need to use the 'data' member to

 * point to a string in user space,like it is done for RANGE...

 * The "flags" memberindicate if the ESSID is active or not (promiscuous).

 */

/* Other parameters useful in 802.11 andsome other devices */

#define SIOCSIWRATE 0x8B20          /* 设置默认传输速率(bps) */

#define SIOCGIWRATE 0x8B21          /* 获取默认传输速率 (bps) */

#define SIOCSIWRTS    0x8B22          /* 设置 RTS/CTS 的临界值(bytes) */

#define SIOCGIWRTS   0x8B23          /* 获取 RTS/CTS 的临界值 (bytes) */

#define SIOCSIWFRAG 0x8B24          /* 设置分片传输的包大小 (bytes) */

#define SIOCGIWFRAG     0x8B25          /*  获取分片传输的包大小 (bytes)*/

#define SIOCSIWTXPOW  0x8B26          /* 设置传输功率 (dBm) */

#define SIOCGIWTXPOW  0x8B27          /* 获取传输功率(dBm) */

#define SIOCSIWRETRY     0x8B28          /* 设置重传次数和生存时间 */

#define SIOCGIWRETRY    0x8B29          /*  获取重传次数和生存时间  */

/* Encoding stuff (scrambling, hardwaresecurity, WEP...) */

#define SIOCSIWENCODE 0x8B2A         /* 设置编码模式 */

#define SIOCGIWENCODE     0x8B2B          /* 获取编码模式 */

/* Power saving stuff (power management,unicast and multicast) */

#define SIOCSIWPOWER  0x8B2C         /* 设置电源管理模式 */

#define SIOCGIWPOWER  0x8B2D         /* 获取电源管理模式*/

/* -------------------- DEV PRIVATE IOCTLLIST -------------------- */

/* These 16 ioctl are wireless deviceprivate.

 * Each driver is free to use themfor whatever purpose it chooses,

 * however the driver *must* exportthe description of those ioctls

 * with SIOCGIWPRIV and *must* usearguments as defined below.

 * If you don't follow those rules,DaveM is going to hate you (reason :

 * it make mixed 32/64bit operationimpossible).

 */

#define SIOCIWFIRSTPRIV      0x8BE0

#define SIOCIWLASTPRIV 0x8BFF

/* Previously, we were usingSIOCDEVPRIVATE, but we now have our

 * separate range because ofcollisions with other tools such as

 * 'mii-tool'.

 * We now have 32 commands, so a bitmore space ;-).

 * Also, all 'odd' commands are onlyusable by root and don't return the

 * content of ifr/iwr to user (butyou are not obliged to use the set/get

 * convention, just use every othertwo command).

 * And I repeat : you are not obligedto use them with iwspy, but you

 * must be compliant with it.

 */

/* ------------------------- IOCTL STUFF------------------------- */

/* The first and the last (range) */

#define SIOCIWFIRST   0x8B00

#define SIOCIWLAST    SIOCIWLASTPRIV          /* 0x8BFF */


 

Logo

更多推荐