Wireless Tools包含了一下工具:
iwconfig:设置基本无线参数
iwlist:扫描、列出频率,比特率,密钥等
iwspy:获取每个节点链接的质量(只能查阅与之相连的节点)
iwpriv:操作Wireless Extensions 特定驱动
ifrename: 基于各种静态标准命名接口
大多数 Linux 发行版本都在其网络初始化脚本中集成Wireless Extension,以便启动时配置无线接口。
ifconfig
iwconfig是Linux Wireless Extensions(LWE)的用户层配置工具之一。LWE是Linux下对无线网络配置的工具,包括内核的支持、用户层配置工具和驱动接口的支持三部分
ifconfig用法:
wireless-tools中iwconfig的main函数,内容如下:
/******************************* MAIN ********************************/
/*------------------------------------------------------------------*/
/*
* The main !
*/
int
main(int argc,
char ** argv)
{
int skfd; /* generic raw socket desc. */
int goterr = 0;
/* Create a channel to the NET kernel. */
if((skfd = iw_sockets_open()) < 0)
{
perror("socket");
exit(-1);
}
/* No argument : show the list of all device + info */
if(argc == 1)
iw_enum_devices(skfd, &print_info, NULL, 0);
else
/* Special case for help... */
if((!strcmp(argv[1], "-h")) || (!strcmp(argv[1], "--help")))
iw_usage();
else
/* Special case for version... */
if(!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))
goterr = iw_print_version_info("iwconfig");
else
{
/* '--' escape device name */
if((argc > 2) && !strcmp(argv[1], "--"))
{
argv++;
argc--;
}
/* The device name must be the first argument */
if(argc == 2)
print_info(skfd, argv[1], NULL, 0);
else
/* The other args on the line specify options to be set... */
goterr = set_info(skfd, argv + 2, argc - 2, argv[1]);
}
/* Close the socket. */
iw_sockets_close(skfd);
return(goterr);
}
iw_sockets_open
函数根据不同的协议创建对应的socket,以便和无线设备驱动进行交互。
iw_enum_devices
函数,当输入的一个参入时,如果是网络接口,则输出网络接口状态信息。
print_info
函数,获取参数信息传入后,打印出与参数对应的需求信息。
set_info
函数,设置参数输入后,进行ioctl操作,向无线设备驱动传参,并生效。
/*------------------------------------------------------------------*/
/*
* Enumerate devices and call specified routine
* The new way just use /proc/net/wireless, so get all wireless interfaces,
* whether configured or not. This is the default if available.
* The old way use SIOCGIFCONF, so get only configured interfaces (wireless
* or not).
*/
void
iw_enum_devices(int skfd,
iw_enum_handler fn,
char * args[],
int count)
{
char buff[1024];
FILE * fh;
struct ifconf ifc;
struct ifreq *ifr;
int i;
#ifndef IW_RESTRIC_ENUM
/* Check if /proc/net/dev is available */
fh = fopen(PROC_NET_DEV, "r");
#else
/* Check if /proc/net/wireless is available */
fh = fopen(PROC_NET_WIRELESS, "r");
#endif
if(fh != NULL)
{
/* Success : use data from /proc/net/wireless */
/* Eat 2 lines of header */
fgets(buff, sizeof(buff), fh);
fgets(buff, sizeof(buff), fh);
/* Read each device line */
while(fgets(buff, sizeof(buff), fh))
{
char name[IFNAMSIZ + 1];
char *s;
/* Skip empty or almost empty lines. It seems that in some
* cases fgets return a line with only a newline. */
if((buff[0] == '\0') || (buff[1] == '\0'))
continue;
/* Extract interface name */
s = iw_get_ifname(name, sizeof(name), buff);
if(!s)
{
/* Failed to parse, complain and continue */
#ifndef IW_RESTRIC_ENUM
fprintf(stderr, "Cannot parse " PROC_NET_DEV "\n");
#else
fprintf(stderr, "Cannot parse " PROC_NET_WIRELESS "\n");
#endif
}
else
/* Got it, print info about this interface */
(*fn)(skfd, name, args, count);
}
fclose(fh);
}
else
{
/* Get list of configured devices using "traditional" way */
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
{
fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
return;
}
ifr = ifc.ifc_req;
/* Print them */
for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
(*fn)(skfd, ifr->ifr_name, args, count);
}
}
其中iw_enum_handler
是函数参数,传入了print_info
函数:
/* Prototype for handling display of each single interface o