⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapi.c

📁 MAPI: API for wireless cards under Linux 提供wireless的API函数。大家可以通过这个程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/**	API for wireless cards under Linux Version 1.0 - Nov, 2001**		Moustafa A. Youssef - MIND Lab '01** This API works for any driver that supports wireless extensions (provided that the driver works correctly)* For the signal strength measurements from all the access points, only the mwavelan_cs driver works (as far as I know)* Read the file: MAPI_README.txt* * Code based on the Wireless Tools By* 	Jean II - HPLB '99*/ 	#include "mapi.h"#include <string.h>/**************************** VARIABLES ****************************/char *	OperationMode[] = { "Auto","Ad-Hoc","Managed","Master","Repeater","Secondary" };int SocketFileDescriptor = -1;		/* generic raw socket desc.	*//************************* Information Routines **************************//*------------------------------------------------------------------*//**   Read /proc/net/wireless to get the latest statistics for the current access point*	MOS*   Input:		- InterfaceName: Interface name (e.g. eth0)	Outpu:Stats		- Stats:	Collected Statistics		- returns: 0 if successful, -1 otherwise 		*/int GetCurrentAPStats(char * InterfaceName, iwstats * Stats){	FILE *	f=fopen("/proc/net/wireless","r");	char		buf[256];	char *	bp;	int		t;	if(f==NULL)		return -1;	/* Loop on all devices */	while(fgets(buf,255,f)){		bp=buf;		while(*bp&&isspace(*bp))					/* MOS: skip blanks	*/			bp++;		/* Is it the good device ? */		if(strncmp(bp, InterfaceName, strlen(InterfaceName))==0 && bp[strlen(InterfaceName)]==':')		{			/* Skip ethX: */			bp=strchr(bp,':');			bp++;			/* -- status -- */			bp = strtok(bp, " ");					/* MOS: tokenize by space */			sscanf(bp, "%X", &t);			Stats->status = (unsigned short) t;			/* -- link quality -- */			bp = strtok(NULL, " ");			if(strchr(bp,'.') != NULL)				/* MOS: '.' means updated */				Stats->qual.updated |= 1;			sscanf(bp, "%d", &t);			Stats->qual.qual = (unsigned char) t;			/* -- signal level -- */			bp = strtok(NULL, " ");			if(strchr(bp,'.') != NULL)				Stats->qual.updated |= 2;			sscanf(bp, "%d", &t);			Stats->qual.level = (unsigned char) t;			/* -- noise level -- */			bp = strtok(NULL, " ");			if(strchr(bp,'.') != NULL)				Stats->qual.updated += 4;			sscanf(bp, "%d", &t);			Stats->qual.noise = (unsigned char) t;			/* -- discarded packets -- */			bp = strtok(NULL, " ");			sscanf(bp, "%d", &Stats->discard.nwid);			bp = strtok(NULL, " ");			sscanf(bp, "%d", &Stats->discard.code);			bp = strtok(NULL, " ");			sscanf(bp, "%d", &Stats->discard.misc);			fclose(f);			return 0;		}    }	fclose(f);	return -1;}/*------------------------------------------------------------------*//** Get wireless informations & config from the device driver* We will call all the classical wireless ioctl on the driver through* the socket to know what is supported and to get the settings... *  MOS *  Input: 		- InterfaceName: Interface name (e.g. eth0) 	Output: 		- Info: Collected information (including flags to indicate if the property is supported or not) 		- returns: -ve if unsuccessful*/ int GetInterfaceInfo(char* InterfaceName, struct wireless_info*	Info){	struct iwreq wrq;		memset((char *) Info, 0, sizeof(struct wireless_info));		/* Get wireless name */	strcpy(wrq.ifr_name, InterfaceName); 	if(ioctl(SocketFileDescriptor, SIOCGIWNAME, &wrq) < 0)		/* If no wireless name : no wireless extensions */		return(-1);	else		strcpy(Info->name, wrq.u.name);		/* Get network ID */	strcpy(wrq.ifr_name, InterfaceName);	if(ioctl(SocketFileDescriptor, SIOCGIWNWID, &wrq) >= 0)    {		Info->has_nwid = 1;		memcpy(&(Info->nwid), &(wrq.u.nwid), sizeof(iwparam));    }		/* Get frequency / channel */	strcpy(wrq.ifr_name, InterfaceName);	if(ioctl(SocketFileDescriptor, SIOCGIWFREQ, &wrq) >= 0)    {		Info->has_freq = 1;		Info->freq = freq2float(&(wrq.u.freq));    }		/* Get sensitivity */	strcpy(wrq.ifr_name, InterfaceName);	if(ioctl(SocketFileDescriptor, SIOCGIWSENS, &wrq) >= 0)    {		Info->has_sens = 1;		memcpy(&(Info->sens), &(wrq.u.sens), sizeof(iwparam));    }		/* Get encryption information */	strcpy(wrq.ifr_name, InterfaceName);	wrq.u.data.pointer = (caddr_t) Info->key;	wrq.u.data.length = 0;	wrq.u.data.flags = 0;	if(ioctl(SocketFileDescriptor, SIOCGIWENCODE, &wrq) >= 0)    {		Info->has_key = 1;		Info->key_size = wrq.u.data.length;		Info->key_flags = wrq.u.data.flags;    }		/* Get ESSID */	strcpy(wrq.ifr_name, InterfaceName);	wrq.u.essid.pointer = (caddr_t) Info->essid;	wrq.u.essid.length = 0;	wrq.u.essid.flags = 0;	if(ioctl(SocketFileDescriptor, SIOCGIWESSID, &wrq) >= 0)    {		Info->has_essid = 1;		Info->essid_on = wrq.u.data.flags;    }		/* Get AP address */	strcpy(wrq.ifr_name, InterfaceName);	if(ioctl(SocketFileDescriptor, SIOCGIWAP, &wrq) >= 0)    {		Info->has_ap_addr = 1;		memcpy(&(Info->ap_addr), &(wrq.u.ap_addr), sizeof (sockaddr));    }		/* Get NickName */	strcpy(wrq.ifr_name, InterfaceName);	wrq.u.essid.pointer = (caddr_t) Info->nickname;	wrq.u.essid.length = 0;	wrq.u.essid.flags = 0;	if(ioctl(SocketFileDescriptor, SIOCGIWNICKN, &wrq) >= 0)		if(wrq.u.data.length > 1)			Info->has_nickname = 1;				/* Get bit rate */		strcpy(wrq.ifr_name, InterfaceName);		if(ioctl(SocketFileDescriptor, SIOCGIWRATE, &wrq) >= 0)		{			Info->has_bitrate = 1;			memcpy(&(Info->bitrate), &(wrq.u.bitrate), sizeof(iwparam));		}				/* Get RTS threshold */		strcpy(wrq.ifr_name, InterfaceName);		if(ioctl(SocketFileDescriptor, SIOCGIWRTS, &wrq) >= 0)		{			Info->has_rts = 1;			memcpy(&(Info->rts), &(wrq.u.rts), sizeof(iwparam));		}				/* Get fragmentation threshold */		strcpy(wrq.ifr_name, InterfaceName);		if(ioctl(SocketFileDescriptor, SIOCGIWFRAG, &wrq) >= 0)		{			Info->has_frag = 1;			memcpy(&(Info->frag), &(wrq.u.frag), sizeof(iwparam));		}				/* Get operation mode */		strcpy(wrq.ifr_name, InterfaceName);		if(ioctl(SocketFileDescriptor, SIOCGIWMODE, &wrq) >= 0)		{			if((wrq.u.mode < 6) && (wrq.u.mode >= 0))				Info->has_mode = 1;			Info->mode = wrq.u.mode;		}				/* Get Power Management settings */		strcpy(wrq.ifr_name, InterfaceName);		if(ioctl(SocketFileDescriptor, SIOCGIWPOWER, &wrq) >= 0)		{			Info->has_power = 1;			memcpy(&(Info->power), &(wrq.u.power), sizeof(iwparam));		}				/* Get Stats */		if(GetCurrentAPStats(InterfaceName, &(Info->stats)) >= 0)		{			Info->has_stats = 1;		}				/* Get ranges */		if(get_range_info(SocketFileDescriptor, InterfaceName, &(Info->range)) >= 0)			Info->has_range = 1;				return(0);} /*------------------------------------------------------------------*/ /* *  MOS * Get Wireless Name *  Input: 		- InterfaceName: Interface name (e.g. eth0) 	Output: 		- Wireless Name 		- returns: -ve if unsuccessful */  int GetWirelessName(char* InterfaceName, char * Name) { 	struct iwreq wrq; 	 	/* Get wireless name */ 	strcpy(wrq.ifr_name, InterfaceName);  	if(ioctl(SocketFileDescriptor, SIOCGIWNAME, &wrq) < 0) 		/* If no wireless name : no wireless extensions */ 		return(-1); 	else 		strcpy(Name, wrq.u.name); 	return(0); } /* 	Rest of Subfunctions */  /*------------------------------------------------------------------*/ /* * MOS * Get Frequency *  Input: 		- InterfaceName: Interface name (e.g. eth0) 	Output: 		- Freq: Frequency in HErtz 		- returns: -ve if unsuccessful */  int GetFrequency(char* InterfaceName, float* Freq) { 	struct iwreq wrq; 	 	/* Get wireless name */ 	strcpy(wrq.ifr_name, InterfaceName);  	if(ioctl(SocketFileDescriptor, SIOCGIWNAME, &wrq) < 0) 		/* If no wireless name : no wireless extensions */ 		return(-1); 	/* Get frequency / channel */ 	strcpy(wrq.ifr_name, InterfaceName); 	if(ioctl(SocketFileDescriptor, SIOCGIWFREQ, &wrq) >= 0)     { 		*Freq = freq2float(&(wrq.u.freq)); 		return 0;     } 	return (-1); }  /*------------------------------------------------------------------*/ /* * MOS * Get Access Point Address *  Input: 		- InterfaceName: Interface name (e.g. eth0) 	Output: 		- Freq: Access point MAC address 		- returns: -ve if unsuccessful */  int GetAPAddress(char* InterfaceName, char* APAddress) { 	struct iwreq wrq; 	 	//	memset((char *) Info, 0, sizeof(struct wireless_info)); 	 	/* Get wireless name */ 	strcpy(wrq.ifr_name, InterfaceName);  	if(ioctl(SocketFileDescriptor, SIOCGIWNAME, &wrq) < 0) 		/* If no wireless name : no wireless extensions */ 		return(-1); 	strcpy(wrq.ifr_name, InterfaceName); 	/* Get AP address */ 	strcpy(wrq.ifr_name, InterfaceName); 	if(ioctl(SocketFileDescriptor, SIOCGIWAP, &wrq) >= 0){		sockaddr Addr;		memcpy(&Addr, &wrq.u.ap_addr, sizeof(sockaddr));  		strcpy(APAddress,pr_ether((unsigned char*)&Addr.sa_data)); 		return 0;     } 	return (-1); }  /*------------------------------------------------------------------*/ /* * MOS * Get Operation Mode *  Input: 		- InterfaceName: Interface name (e.g. eth0) 	Output: 		- Operation Mode: One of the 6 operation modes defined at the begining of this file 		- returns: -ve if unsuccessful */  int GetOperationMode(char* InterfaceName, int* OperationMode) { 	struct iwreq wrq; 	 	//	memset((char *) Info, 0, sizeof(struct wireless_info)); 	 	/* Get wireless name */ 	strcpy(wrq.ifr_name, InterfaceName);  	if(ioctl(SocketFileDescriptor, SIOCGIWNAME, &wrq) < 0) 		/* If no wireless name : no wireless extensions */ 		return(-1); 	/* Get operation mode */ 	strcpy(wrq.ifr_name, InterfaceName); 	if(ioctl(SocketFileDescriptor, SIOCGIWMODE, &wrq) >= 0){ 		if((wrq.u.mode < 6) && (wrq.u.mode >= 0)) 		*OperationMode= wrq.u.mode; 		return 0; 	} 	return (-1); }  /*------------------------------------------------------------------*//** Print on the screen in a neat fashion all the Info we have collected* on a device.*  MOS  *  Input: 		- InterfaceName: Interface name (e.g. eth0) 		- Info: Information about the interface.	Output: 		- None	     */void DisplayInfo(char* InterfaceName, struct wireless_info*	Info){	/* Display device name and wireless name (name of the protocol used) */	printf("%-8.8s  %s  ", InterfaceName, Info->name);		/* Display ESSID (extended network), if any */	if(Info->has_essid)    {		if(Info->essid_on)			printf("ESSID:\"%s\"  ", Info->essid);		else			printf("ESSID:off  ");    }		/* Display NickName (station name), if any */	if(Info->has_nickname)		printf("Nickname:\"%s\"", Info->nickname);		/* Formatting */	if(Info->has_essid || Info->has_nickname)		printf("\n          ");		/* Display Network ID */	if(Info->has_nwid)    {	/* Note : should display right number of digit according to Info		* in range structure */		if(Info->nwid.disabled)			printf("NWID:off/any  ");		else			printf("NWID:%X  ", Info->nwid.value);    }		/* Display frequency / channel */	if(Info->has_freq)    {		if(Info->freq < KILO)			printf("Channel:%g  ", Info->freq);		else		{			if(Info->freq >= GIGA)				printf("Frequency:%gGHz  ", Info->freq / GIGA);			else			{				if(Info->freq >= MEGA)					printf("Frequency:%gMHz  ", Info->freq / MEGA);				else					printf("Frequency:%gkHz  ", Info->freq / KILO);			}		}    }		/* Display sensitivity */	if(Info->has_sens)    {		/* Fixed ? */		if(Info->sens.fixed)			printf("Sensitivity=");		else			printf("Sensitivity:");				if(Info->has_range)			/* Display in dBm ? */			if(Info->sens.value < 0)				printf("%d dBm  ", Info->sens.value);			else				printf("%d/%d  ", Info->sens.value, Info->range.sensitivity);			else				printf("%d  ", Info->sens.value);    }		/* Display the current mode of operation */	if(Info->has_mode)    {		/* A bit of clever formatting */		if((Info->has_nwid + 2*Info->has_freq + 2*Info->has_sens			+ !Info->has_essid) > 4)			printf("\n          ");				printf("Mode:%s  ", OperationMode[Info->mode]);    }		/* Display the address of the current Access Point */	if(Info->has_ap_addr)    {		/* A bit of clever formatting */		if((Info->has_nwid + 2*Info->has_freq + 2*Info->has_sens			+ Info->has_mode + !Info->has_essid) > 3)			printf("\n          ");				printf("Access Point: %s", pr_ether(Info->ap_addr.sa_data));    }		printf("\n          ");		/* Display the currently used/set bit-rate */	if(Info->has_bitrate)    {		/* Fixed ? */		if(Info->bitrate.fixed)			printf("Bit Rate=");		else			printf("Bit Rate:");				if(Info->bitrate.value >= GIGA)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -