>  So, you should create socket, fill interface name field in ifr,
>  get flags via ioctl with this socket, and work with derived data.
>  That's what ifconfig does.

I've fixed it now. I misunderstood something. The file descriptor I was 
calling the ioctl(fd ,SIOCSIFFLAGS, &ifr) on before was the file 
descriptor returned by open("/dev/net/tun"). So it was the file 
descriptor to the tunX interface.

I thought I should set the if flags and send them to this device. Using 
a dgram socket to do the job worked.

Here's the code I have now:

/* Returns a fildes to the network device */
if ( (fd_tun = open("/dev/net/tun", O_RDWR) ) < 0)
    return fd_tun;

memset(&ifr, 0, sizeof(ifr));

ifr.ifr_flags = IFF_TUN | IFF_NO_PI; /* Create a TUN device */

if ( *dev )
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);

/* Create the device */
if ( (err = ioctl(fd_tun, TUNSETIFF, (void*) &ifr)) < 0 )
    return err;

/* Copy the actual name of the device that was created back */
strcpy(dev, ifr.ifr_name);

/* Set no check sum - what is this? */
ioctl(fd_tun, TUNSETNOCSUM, 1);

/* Get the flags that are set */
skfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (skfd < 0 )
    return skfd;

err = ioctl(skfd, SIOCGIFFLAGS, (void*) &ifr);
if ( err < 0 )
    return err;

/* Set the flags that bring the device up */
ifr.ifr_flags |= ( IFF_UP | IFF_RUNNING );

err = ioctl(skfd, SIOCSIFFLAGS, (void*) &ifr);
if ( err < 0 )
    return err;

return fd_tun;

It works, but I'm a bit confused. Why do I have to use a new dgram 
socket to get/set the device flags? It seems more intuative to use the 
fd to the device to set the device flags. Is this new dgram socket 
useful for anything else?

Thanks for you help,

Nick Martin

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
VTun-devel mailing list
VTun-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vtun-devel

Reply via email to