ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-linux.C
(Generate patch)

Comparing gvpe/src/device-linux.C (file contents):
Revision 1.1 by pcg, Sat Mar 1 15:53:03 2003 UTC vs.
Revision 1.6 by pcg, Thu Oct 16 13:37:10 2003 UTC

1/* 1/*
2 device-linux.C -- Interaction with Linux tun/tap device 2 device-linux.C -- Interaction with Linux tun/tap device
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
3 4
4 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 8 (at your option) any later version.
28#include <fcntl.h> 29#include <fcntl.h>
29#include <net/if.h> 30#include <net/if.h>
30#include <unistd.h> 31#include <unistd.h>
31#include <sys/ioctl.h> 32#include <sys/ioctl.h>
32 33
34#include <net/if.h>
35
33#ifdef LINUX_IF_TUN_H 36#ifdef LINUX_IF_TUN_H
34#include LINUX_IF_TUN_H 37# include LINUX_IF_TUN_H
35#else 38#else
36#include <linux/if_tun.h> 39#include <linux/if_tun.h>
37#endif 40#endif
38#define DEFAULT_DEVICE "/dev/net/tun" 41#define DEFAULT_DEVICE "/dev/net/tun"
39 42
40#include "gettext.h" 43#include "gettext.h"
41 44
42#include "conf.h" 45#include "conf.h"
46
47#if TEST_ETHEREMU
48# define IF_istun
49# include "ether_emu.C"
50#endif
51
52const char *
53tap_device::info ()
54{
55 return _("Linux tun/tap device");
56}
43 57
44tap_device::tap_device () 58tap_device::tap_device ()
45{ 59{
46 struct ifreq ifr; 60 struct ifreq ifr;
47 61
54 slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); 68 slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno));
55 exit (1); 69 exit (1);
56 } 70 }
57 71
58 memset (&ifr, 0, sizeof (ifr)); 72 memset (&ifr, 0, sizeof (ifr));
73#if TEST_ETHEREMU
74 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
75#else
59 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 76 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
77#endif
60 78
61 if (conf.ifname) 79 if (conf.ifname)
62 strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); 80 strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ);
63 else 81 else
64 ifr.ifr_name[0] = 0; 82 ifr.ifr_name[0] = 0;
72 { 90 {
73 slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); 91 slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno));
74 exit (1); 92 exit (1);
75 } 93 }
76 94
77 if (conf.ifpersist)
78 if (ioctl (fd, TUNSETPERSIST, 1)) 95 if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0))
79 slog (L_WARN, _("cannot set persistent mode for device %s: %s"), ifrname, strerror (errno)); 96 slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno));
80 97
81 slog (L_DEBUG, _("%s is a %s"), device, info ()); 98 slog (L_DEBUG, _("%s is a %s"), device, info ());
82} 99}
83 100
84tap_device::~tap_device () 101tap_device::~tap_device ()
89tap_packet * 106tap_packet *
90tap_device::recv () 107tap_device::recv ()
91{ 108{
92 tap_packet *pkt = new tap_packet; 109 tap_packet *pkt = new tap_packet;
93 110
111#if TEST_ETHEREMU
112 pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14);
113#else
94 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); 114 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU);
115#endif
95 116
96 if (pkt->len <= 0) 117 if (pkt->len <= 0)
97 { 118 {
119 delete pkt;
98 slog (L_ERR, _("error while reading from %s %s: %s"), 120 slog (L_ERR, _("error while reading from %s %s: %s"),
99 info (), DEFAULT_DEVICE, strerror (errno)); 121 info (), DEFAULT_DEVICE, strerror (errno));
100 free (pkt);
101 return 0; 122 return 0;
102 } 123 }
124
125#if TEST_ETHEREMU
126 pkt->len += 14;
127
128 // assume ipv4
129 (*pkt)[12] = 0x08;
130 (*pkt)[13] = 0x00;
131
132 if (!ether_emu.tun_to_tap (pkt))
133 {
134 delete pkt;
135 return 0;
136 }
137#endif
103 138
104 return pkt; 139 return pkt;
105} 140}
106 141
107void 142void
108tap_device::send (tap_packet *pkt) 143tap_device::send (tap_packet *pkt)
109{ 144{
145#if TEST_ETHEREMU
146 if (ether_emu.tap_to_tun (pkt) &&
147 write (fd, &((*pkt)[14]), pkt->len - 14) < 0)
148#else
110 if (write (fd, &((*pkt)[0]), pkt->len) < 0) 149 if (write (fd, &((*pkt)[0]), pkt->len) < 0)
150#endif
111 slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, 151 slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE,
112 strerror (errno)); 152 strerror (errno));
113} 153}
114 154

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines