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.11 by pcg, Fri Mar 18 02:32:20 2005 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-2005 Marc Lehmann <gvpe@schmorp.de>
3 4
5 This file is part of GVPE.
6
4 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by 8 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 9 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version. 10 (at your option) any later version.
8 11
9 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details. 15 GNU General Public License for more details.
13 16
14 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/ 20*/
18 21
19#include "config.h" 22#include "config.h"
20 23
28#include <fcntl.h> 31#include <fcntl.h>
29#include <net/if.h> 32#include <net/if.h>
30#include <unistd.h> 33#include <unistd.h>
31#include <sys/ioctl.h> 34#include <sys/ioctl.h>
32 35
36#include <net/if.h>
37
33#ifdef LINUX_IF_TUN_H 38#ifdef LINUX_IF_TUN_H
34#include LINUX_IF_TUN_H 39# include LINUX_IF_TUN_H
35#else 40#else
36#include <linux/if_tun.h> 41#include <linux/if_tun.h>
37#endif 42#endif
38#define DEFAULT_DEVICE "/dev/net/tun" 43#define DEFAULT_DEVICE "/dev/net/tun"
39 44
40#include "gettext.h" 45#include "gettext.h"
41 46
42#include "conf.h" 47#include "conf.h"
48
49#if TEST_ETHEREMU
50# define IF_istun
51# include "ether_emu.C"
52#endif
53
54const char *
55tap_device::info ()
56{
57 return _("Linux tun/tap device");
58}
43 59
44tap_device::tap_device () 60tap_device::tap_device ()
45{ 61{
46 struct ifreq ifr; 62 struct ifreq ifr;
47 63
50 fd = open (device, O_RDWR); 66 fd = open (device, O_RDWR);
51 67
52 if (fd < 0) 68 if (fd < 0)
53 { 69 {
54 slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno)); 70 slog (L_ERR, _("could not open device %s: %s"), device, strerror (errno));
55 exit (1); 71 exit (EXIT_FAILURE);
56 } 72 }
57 73
58 memset (&ifr, 0, sizeof (ifr)); 74 memset (&ifr, 0, sizeof (ifr));
75#if TEST_ETHEREMU
76 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
77#else
59 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; 78 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
79#endif
60 80
61 if (conf.ifname) 81 if (conf.ifname)
62 strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ); 82 strncpy (ifr.ifr_name, conf.ifname, IFNAMSIZ);
63 else 83 else
64 ifr.ifr_name[0] = 0; 84 ifr.ifr_name[0] = 0;
69 ifrname [IFNAMSIZ] = 0; 89 ifrname [IFNAMSIZ] = 0;
70 } 90 }
71 else 91 else
72 { 92 {
73 slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno)); 93 slog (L_CRIT, _("unable to configure tun/tap interface: %s"), strerror (errno));
74 exit (1); 94 exit (EXIT_FAILURE);
75 } 95 }
76 96
77 if (conf.ifpersist)
78 if (ioctl (fd, TUNSETPERSIST, 1)) 97 if (ioctl (fd, TUNSETPERSIST, conf.ifpersist ? 1 : 0))
79 slog (L_WARN, _("cannot set persistent mode for device %s: %s"), ifrname, strerror (errno)); 98 slog (L_WARN, _("cannot set persistency mode for device %s: %s"), ifrname, strerror (errno));
80 99
81 slog (L_DEBUG, _("%s is a %s"), device, info ()); 100 slog (L_DEBUG, _("%s is a %s"), device, info ());
82} 101}
83 102
84tap_device::~tap_device () 103tap_device::~tap_device ()
89tap_packet * 108tap_packet *
90tap_device::recv () 109tap_device::recv ()
91{ 110{
92 tap_packet *pkt = new tap_packet; 111 tap_packet *pkt = new tap_packet;
93 112
113#if TEST_ETHEREMU
114 pkt->len = read (fd, &((*pkt)[14]), MAX_MTU - 14);
115#else
94 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU); 116 pkt->len = read (fd, &((*pkt)[0]), MAX_MTU);
117#endif
95 118
96 if (pkt->len <= 0) 119 if (pkt->len <= 0)
97 { 120 {
121 delete pkt;
98 slog (L_ERR, _("error while reading from %s %s: %s"), 122 slog (L_ERR, _("error while reading from %s %s: %s"),
99 info (), DEFAULT_DEVICE, strerror (errno)); 123 info (), DEFAULT_DEVICE, strerror (errno));
100 free (pkt);
101 return 0; 124 return 0;
102 } 125 }
126
127#if TEST_ETHEREMU
128 pkt->len += 14;
129
130 // assume ipv4
131 (*pkt)[12] = 0x08;
132 (*pkt)[13] = 0x00;
133
134 if (!ether_emu.tun_to_tap (pkt))
135 {
136 delete pkt;
137 return 0;
138 }
139#endif
103 140
104 return pkt; 141 return pkt;
105} 142}
106 143
107void 144void
108tap_device::send (tap_packet *pkt) 145tap_device::send (tap_packet *pkt)
109{ 146{
147#if TEST_ETHEREMU
148 if (ether_emu.tap_to_tun (pkt) &&
149 write (fd, &((*pkt)[14]), pkt->len - 14) < 0)
150#else
110 if (write (fd, &((*pkt)[0]), pkt->len) < 0) 151 if (write (fd, &((*pkt)[0]), pkt->len) < 0)
152#endif
111 slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, 153 slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE,
112 strerror (errno)); 154 strerror (errno));
113} 155}
114 156

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines