Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 03-07-2009, 12:13 AM   #1
LQ Newbie
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
Unhappy problem in reading from /dev/net/tun

Hi all,

I am unable to read from /dev/net/tun... in my program read()... it waits indefinitely untill i press ctrl+c... here is my program... can any of you guru's please pick my flaws??


int openTun(char *);
int readTun(int);

int main()
int fd;
char dev[4]= "tun0";
fd = openTun(dev);
return 0;

int openTun(char *dev)
int fd;
char bridge_config[50];
char eth_config[60];
memset(eth_config, 0, 60);
struct ifreq ifr;
struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0)
perror("Socket Open Fail: ");
return -1;
puts("TUN interface opened\n");
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
if( *dev )
strncpy(ifr.ifr_name, dev, 4);

printf("IFR: %s\n", ifr.ifr_name);
if (ioctl(fd, TUNSETIFF, (void *) &ifr) < 0)
return -1;
system("ifconfig tun0 up");
strcpy(dev, ifr.ifr_name);

return fd;

int readTun(int fd)
char uchRecvBuf[100];
int i;
int unReady = 0;
int nBytes = 0;
fd_set readFds;
printf("Before Select\n");

FD_CLR(fd, &readFds);
FD_SET(fd, &readFds);

unReady = select((fd + 1), &readFds, NULL, NULL, NULL);
if(FD_ISSET(fd, &readFds))
nBytes = read(fd, uchRecvBuf, 100);
printf("Read: %d\n", nBytes);
for (i=0;i<100;i++)
printf("%.2x ", uchRecvBuf[i]);
return nBytes;

If i set the ifr.ifr_flags to IFF_TAP then iam able to read the data.. but when i do it for IFF_TUN then read() waits indefinitely.... what might be the problem?? please can anyone help!!!

thanks in advance.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Changed ISP, problem reading net news (nntp) eagleheart Linux - Software 7 08-05-2008 04:11 PM
/dev/tun not working after a kernel upgrade brokenflea Slackware 1 02-26-2007 12:18 AM
vnuml tun device /dev/net/tun not found. asalford Linux - Networking 1 01-30-2007 10:11 PM
need help: /dev/net/tun boxyou Linux - Networking 1 04-29-2005 02:11 AM
How do I use /dev/net/tun mike8s2 Linux - Networking 2 04-19-2005 08:01 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:00 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration