Results 1 to 1 of 1
When I use the below code to try to use the TUN/TAP driver the read(.. call blocks and never returns from the read...The code is as follows:
Code:
#include <linux/if.h>
...
- 10-25-2005 #1Just Joined!
- Join Date
- Oct 2005
- Posts
- 4
tun/tap never returns from read
When I use the below code to try to use the TUN/TAP driver the read(.. call blocks and never returns from the read...The code is as follows:
Code:#include <linux/if.h> #include <linux/if_tun.h> #include <linux/ioctl.h> #include <linux/fcntl.h> //for O_RDWR int tun_alloc(char *dev); //declare that there will be such a function //const O_RDWR=2; int main(int argc, char **argv) { char *buf[2048], dev[10]="tun0\0"; int fd, len; fd = tun_alloc(dev); printf("\r\n%s\r\n",dev); while(1) { len = read(fd,buf,sizeof(buf)); //locks up here if(len>=0) printf("\r\nreceived %d Bytes",len); else printf("\rfailed"); } return 0; } int tun_alloc(char *dev) { struct ifreq ifr; int fd, err ,r; char buf[2048]; if( (fd = open("/dev/net/tun", O_RDWR)) < 0 ) { printf("\r\n Error opening tun device\r\n"); return -1; //return tun_alloc_old(dev); } else printf("\r\n Successfully opened TUN device.\r\n"); memset(&ifr, 0, sizeof(ifr)); /* Flags: IFF_TUN - TUN device (no Ethernet headers) * IFF_TAP - TAP device * * IFF_NO_PI - Do not provide packet information */ ifr.ifr_flags = IFF_TAP; if( *dev ) strncpy(ifr.ifr_name, dev, IFNAMSIZ); if((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ) { close(fd); printf("\r\n Errot ioctl tun device\r\n"); return err; } else printf("\r\n ioctl successful\r\n"); strcpy(dev, ifr.ifr_name); return fd; }
I have been stuck here for a while now. All help and any advice would greatly be appreciated. My project is coming to a halt due to this issue.
Thanks in advance,
Jordan



