如果注释了第二个和第三个ioctl函数,程序能运行,但是程序没法发出数据帧?

#define SIOCAJSMODE SIOCDEVPRIVATE 
#define SIOCAJGMODE SIOCAJSMODE + 1

int channel; 
char ifname[IFNAMSIZ]; 
int sockfd;

static int get_socket (void)
{
struct sockaddr_ll addr;
struct ifreq req; 
struct aj_config aj_conf;
int sockfd, flags;

if((sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
{
return(-1);
}

/* get the interface index */
memset(&req, 0, sizeof(struct ifreq));
memset(&aj_conf, 0, sizeof(struct aj_config));
strcpy(req.ifr_name, ifname);

if(ioctl(sockfd, SIOCGIFINDEX, &req) < 0) // 第一个
{
return(-2);
}
printf("interafce Index:%d\n",req.ifr_ifindex);

/* bind the socket to the interface */
memset(&addr, 0, sizeof(struct sockaddr_ll));
addr.sll_ifindex = req.ifr_ifindex;
addr.sll_protocol = htons(ETH_P_ALL);
addr.sll_family = PF_PACKET;

if(bind(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr_ll)) < 0)
{
return(-3);
}
req.ifr_data = (char *)&aj_conf;

if(ioctl(sockfd, SIOCAJGMODE, &req) < 0) //第二个 参数SIOCAJGMODE定义见上方
{
return(-4);
}

if(channel != -1)
{
aj_conf.channel = channel;
}

memcpy(aj_conf.ownmac, killedmac, 6);
aj_conf.mode = 6;
aj_conf.monitor = 0;

if(ioctl(sockfd, SIOCAJSMODE, &req) < 0) //第三个 参数SIOCAJSMODE定义见上方
{
return(-4);
}

if((flags = fcntl(sockfd, F_GETFL)) < 0)
{
return(-5);
}

if(fcntl(sockfd, F_SETFL, flags|O_NONBLOCK) < 0)
{
return(-6);
} return(sockfd);
}

上面的代码是发送802.11无线数据帧程序的部分代码,其中一共有三个ioctl函数,但是运行的时候会显示get_socket: Operation not supported。

跃然一笑
浏览 61回答 1
1回答

沧海一幻觉

下面的清单介绍了一些最重要的结构,使用 ioctl 套接字命令时常常用到这些结构。清单 1. struct ifreq (/usr/include/net/if.h)/* Interface request structure used for socket* ioctl's. All interface ioctl's must have parameter* definitions which begin with ifr_name. The* remainder may be interface specific.*/struct ifreq {#ifndef IFNAMSIZ#define IFNAMSIZ 16#endifchar ifr_name[IFNAMSIZ];&nbsp;union {struct sockaddr ifru_addr;struct sockaddr ifru_dstaddr;struct sockaddr ifru_broadaddr;__ulong32_t ifru_flags;int ifru_metric;caddr_t ifru_data;u_short ifru_site6;__ulong32_t ifru_mtu;int ifru_baudrate;} ifr_ifru;Following macros are provided for convenience#define ifr_addr ifr_ifru.ifru_addr /* address */#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */#define ifr_flags ifr_ifru.ifru_flags /* flags */#define ifr_metric ifr_ifru.ifru_metric /* metric */#define ifr_data ifr_ifru.ifru_data /* for use by interface */#define ifr_site6 ifr_ifru.ifru_site6 /* IPv6 site index */#define ifr_mtu ifr_ifru.ifru_mtu /* mtu of interface */#define ifr_isno ifr_ifru.ifru_data /* pointer to if_netopts */#define ifr_baudrate ifr_ifru.ifru_baudrate /* baudrate of interface */};清单 2. struct ifconf (/usr/include/net/if.h)/** Structure used in SIOCGIFCONF request.* Used to retrieve interface configuration* for machine (useful for programs which* must know all networks accessible).*/struct ifconf {int ifc_len; /* size of associated buffer */union {caddr_t ifcu_buf;struct ifreq *ifcu_req;} ifc_ifcu;Following macros are provided for convenience#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */#define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */};
打开App,查看更多内容
随时随地看视频慕课网APP