LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-07-2009, 01:13 AM   #1
raj1526
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??

#include<stdio.h>
#include<string.h>
#include<sys/ioctl.h>
#include<net/if.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<linux/if_tun.h>

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

int main()
{
int fd;
char dev[4]= "tun0";
fd = openTun(dev);
readTun(fd);
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)
{
close(fd);
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("fd=%d\n",fd);
printf("Before Select\n");
while(1)
{

FD_CLR(fd, &readFds);
FD_ZERO(&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]);
}
printf("\n");
}
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.
 
  


Reply


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 05:11 PM
/dev/tun not working after a kernel upgrade brokenflea Slackware 1 02-26-2007 01:18 AM
vnuml tun device /dev/net/tun not found. asalford Linux - Networking 1 01-30-2007 11:11 PM
need help: /dev/net/tun boxyou Linux - Networking 1 04-29-2005 03:11 AM
How do I use /dev/net/tun mike8s2 Linux - Networking 2 04-19-2005 09:01 PM


All times are GMT -5. The time now is 02:01 PM.

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