LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-09-2010, 09:29 PM   #1
shmilt24
LQ Newbie
 
Registered: Sep 2010
Posts: 5

Rep: Reputation: 0
Problem with ioctl function when openning TUN/TAP device on Linux.


Hi all,

I have just study about using TUN/TAP. I tried to write a small program to open TUN (source code as the following), but the result is always : ioctl on TUNSETIFF failed. That means the function ioctl always return error. I don't know why.

Somebody can help me to solve this problem ? Thank you very much.

int tun_open (char *name)
{
struct ifreq ifr;
int err, fd;
// struct ip_tunnel_parm p;

if ((fd = open ("/dev/net/tun", O_RDWR)) < 0)
{
printf ("%s\n", name);
return -1;
}

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_TUN;
if (*name)
strncpy (ifr.ifr_name, name, IFNAMSIZ);

if ((err = ioctl (fd, TUNSETIFF, (void *) &ifr)) < 0)
{
close (fd);
printf ("ioctl on TUNSETIFF failed %d\n", err);
return err;
}
strncpy (name, ifr.ifr_name, IFNAMSIZ); // the name actually allocated

printf ("Opened %s\n", name);
return fd;
}

int main ()
{
int tun_fd, nread, i;
char name[10] = "tun0";
tun_fd = tun_open (name); /* tun interface */
exit(0);
}
 
Old 09-09-2010, 10:32 PM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
What's the error it's returning? You can check errno or use something like perror
 
Old 09-10-2010, 02:14 AM   #3
alan99
Member
 
Registered: Mar 2010
Distribution: Debian
Posts: 180

Rep: Reputation: 31
I'm not too clear on this code, but why are you typecasting &ifr with (void *). is ifr an enumerated type?
 
  


Reply



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
static route for iptables NAT and tun/tap device j-osh Linux - Networking 2 10-05-2015 06:26 AM
create TUN/TAP device with non default name vofka Linux - Networking 2 04-29-2010 03:11 AM
how to allow tun/tap device in firewall? efm Linux - Networking 12 07-22-2005 04:47 AM
Universal tun/tap device driver support giddyupman Linux - Networking 4 05-14-2004 10:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:10 AM.

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
Open Source Consulting | Domain Registration