ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-linux.C
Revision: 1.4
Committed: Thu Oct 16 02:28:36 2003 UTC (20 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +31 -0 lines
Log Message:
*** empty log message ***

File Contents

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