WvStreams
wvtundev.cc
1/*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * WvTunDev provides a convenient way of using Linux tunnel devices.
6 *
7 * If you don't have the /dev/net/tun device, try doing:
8 * mknod /dev/net/tun c 10 200
9 */
10#include <sys/ioctl.h>
11#include <sys/socket.h>
12#include "if_tun.h"
13#include <string.h>
14
15#include "wvlog.h"
16#include "wvtundev.h"
17
18WvTunDev::WvTunDev(const WvIPNet &addr, int mtu) :
19 WvFile("/dev/net/tun", O_RDWR)
20{
21 init(addr, mtu);
22}
23
24void WvTunDev::init(const WvIPNet &addr, int mtu)
25{
26 WvLog log("New tundev", WvLog::Debug2);
27 if (getfd() < 0)
28 {
29 log("Could not open /dev/net/tun: %s\n", strerror(errno));
30 seterr(errno);
31 return;
32 }
33
34 struct ifreq ifr;
35 memset(&ifr, 0, sizeof(ifr));
36 ifr.ifr_flags = IFF_NO_PI | IFF_TUN;
37
38 if (ioctl(getfd(), TUNSETIFF, (void *) &ifr) < 0 ||
39 ioctl(getfd(), TUNSETNOCSUM, 1) < 0)
40 {
41 log("Could not initialize the interface: %s\n", strerror(errno));
42 seterr(errno);
43 return;
44 }
45
46 WvInterface iface(ifr.ifr_name);
47 iface.setipaddr(addr);
48 iface.setmtu(mtu);
49 iface.up(true);
50 ifcname = ifr.ifr_name;
51 log.app = ifcname;
52
53 log(WvLog::Debug2, "Now up (%s).\n", addr);
54}
static WvString strerror(int errnum)
A replacement for the operating system ::strerror() function that can map more kinds of error strings...
Definition: wverror.cc:91
int getfd() const
Returns the Unix file descriptor for reading and writing.
Definition: wvfdstream.h:81
WvFile implements a stream connected to a file or Unix device.
Definition: wvfile.h:29
An IP network comprises two WvIPAddr structures: an address and a netmask.
Definition: wvaddr.h:313
A WvInterface manages a particular network interface.
Definition: wvinterface.h:25
A WvLog stream accepts log messages from applications and forwards them to all registered WvLogRcv's.
Definition: wvlog.h:57
virtual void seterr(int _errnum)
Override seterr() from WvError so that it auto-closes the stream.
Definition: wvstream.cc:451
WvTunDev(const WvIPNet &addr, int mtu=1400)
Creates a tunnel device and its associated interface.
Definition: wvtundev.cc:18
WvString ifcname
Contains the name of the interface associated with the device.
Definition: wvtundev.h:33