嗨,我正在写一些关于网络接口管理的事情,只是为了花时间。 我在c中编写了一个函数,使我能够放下或者放置一个接口,或者谈论wlan接口,改变模式,如监视器管理等。现在我想改变wlan接口的监听通道,所以我冲浪了网和我找到了不同的解决方案,如使用nl80211等。在我看来,最有趣的是ioctl。所以我写了这些代码
int changeChannel(int handler,int channel){
getChannel(handler);
wreq.u.freq.m= RETRIEVE_FREQ(channel);
wreq.u.freq.e = 6;
short app = ioctl(sock,SIOCSIWFREQ,&wreq);
if(app<0){
fprintf(stderr,"Error to change the on %i channel to %s\n",channel,interfaces[handler]);
return app;
}
}
int getChannel(int handler){
iw_get(handler);
register short app =ioctl(sock,SIOCGIWFREQ,&wreq);
if(app<0){
fprintf(stderr,"Error to get the channel from %s\n",interfaces[handler]);
return app;
}
return RETRIEVE_CHANNEL(wreq.u.freq.m);
}
其中RETRIEVE_CHANNEL和RETRIEVE_FREQ是用于从频率获取频道和从频道获取频率的宏。 定义如下
#define RETRIEVE_CHANNEL(freq) ({\
int _channel = -1;\
switch(freq){\
case CHANNEL1:\
_channel = 1;\
break;\
case CHANNEL2:\
_channel = 2;\
break;\
case CHANNEL3:\
_channel = 3;\
break;\
case CHANNEL4:\
_channel = 4;\
break;\
case CHANNEL5:\
_channel = 5;\
break;\
case CHANNEL6:\
_channel = 6;\
break;\
case CHANNEL7:\
_channel = 7;\
break;\
case CHANNEL8:\
_channel = 8;\
break;\
case CHANNEL9 :\
_channel = 9;\
break;\
case CHANNEL10:\
_channel = 10;\
break;\
case CHANNEL11:\
_channel = 11;\
break;\
case CHANNEL12:\
_channel = 12;\
break;\
case CHANNEL13:\
_channel = 13;\
break;\
case CHANNEL14:\
_channel = 14;\
break;\
}\
_channel;\
});
#define RETRIEVE_FREQ(channel) ({\
int _freq = -1;\
switch(channel){ \
case 1:\
_freq = CHANNEL1;\
break;\
case 2:\
_freq = CHANNEL2;\
break;\
case 3:\
_freq = CHANNEL3;\
break;\
case 4:\
_freq = CHANNEL4;\
break;\
case 5:\
_freq = CHANNEL5;\
break;\
case 6:\
_freq = CHANNEL6;\
break;\
case 7:\
_freq = CHANNEL7;\
break;\
case 8:\
_freq = CHANNEL8;\
break;\
case 9:\
_freq = CHANNEL9;\
break;\
case 10:\
_freq = CHANNEL10;\
break;\
case 11:\
_freq = CHANNEL11;\
break;\
case 12:\
_freq = CHANNEL12;\
break;\
case 13:\
_freq = CHANNEL13;\
break;\
case 14:\
_freq = CHANNEL14;\
break;\
}\
_freq;\
});
CHANNELNUM是我在枚举中定义的常量值,其中频道值与频道相关
现在我的代码可以运行,或者几乎就是getChannel。使用changeChannel ioctl返回0但是如果我启动
iw interface info
命令显示我始终是同一个频道。 我的错误在哪里?对不起英语,感谢所有人的耐心