Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

wvtundev.cc

Go to the documentation of this file.
00001 /* 
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc. 
00004  * 
00005  * WvTunDev provides a convenient way of using Linux tunnel devices.
00006  *
00007  * If you don't have the /dev/net/tun device, try doing: 
00008  *          mknod /dev/net/tun c 10 200
00009  */
00010 #include <sys/ioctl.h>
00011 #include <sys/socket.h> 
00012 #include "if_tun.h"
00013 #include <linux/if.h>
00014 #include <string.h> 
00015 
00016 #include "wvlog.h"
00017 #include "wvtundev.h"
00018 
00019 WvTunDev::WvTunDev(const WvIPNet &addr, int mtu) :
00020     WvFile("/dev/net/tun", O_RDWR)
00021 {
00022     init(addr, mtu);
00023 }
00024 
00025 void WvTunDev::init(const WvIPNet &addr, int mtu)
00026 {
00027     WvLog log("New tundev", WvLog::Debug2);
00028     if (getfd() < 0)
00029     {
00030         log("Could not open /dev/net/tun: %s\n", strerror(errno)); 
00031         seterr(errno);
00032         return;
00033     }
00034 
00035     struct ifreq ifr;
00036     memset(&ifr, 0, sizeof(ifr));
00037     ifr.ifr_flags = IFF_NO_PI | IFF_TUN;
00038 
00039     if (ioctl(getfd(), TUNSETIFF, (void *) &ifr) < 0 ||
00040         ioctl(getfd(), TUNSETNOCSUM, 1) < 0)
00041     {
00042         log("Could not initialize the interface: %s\n", strerror(errno));
00043         seterr(errno);
00044         return;
00045     }
00046     
00047     WvInterface iface(ifr.ifr_name);
00048     iface.setipaddr(addr);
00049     iface.setmtu(mtu);
00050     iface.up(true);
00051     ifcname = ifr.ifr_name;
00052     log.app = ifcname;
00053 
00054     log(WvLog::Debug2, "Now up (%s).\n", addr);
00055 }    

Generated on Sat Mar 13 14:56:00 2004 for WvStreams by doxygen 1.3.6-20040222