My objective is to create a to create a virtual wireless interface and to attach a listener on that interface. I am trying to do this using a C program.
So far I have been able to create the virtual interface by the following methods :
sudo iw phy phy0 interface add mySta1 type station
but I was unable to find the library to do the same using C program.
Using Tun/Tap persistent interfaces :
http://backreference.org/2010/03/26/tuntap-interface-tutorial/
char tun_name[IFNAMSIZ]; strcpy(tun_name, "MyTun"); tunfd = tun_alloc(tun_name, IFF_TUN | IFF_NO_PI);
where tun_alloc is my function which uses ioctl to create the interface
ioctl(fd, TUNSETIFF, (void *) &ifr)
but there is no wireless extensions for this interface
For listening on the interface :
nread = read(tunfd,buffer,sizeof(buffer));
But this only works on tun devices which I create using my program or programs like openvpn. When used with any other interface like wlan0, ioctl gives the error 'Invalid argument'
Is my approach to the problem correct ? Is there any other way to achieve this ? I want to know the following :
If you want to achieve "iw phy..." in C program - just look at "iw" program sources. Probably it is achieved by some ioctls or netlink.
Answers to your questions:
Virtual TUN/TAP device means that this device is independent of any hardware. It it only software solution. You cannot easily in userspace attach TUN/TAP device to wireless interface.
No, as I wrote in answer 1. the TUN/TAP is only virtual device and it cannot be converted to wifi device type.