ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/device-tincd.C
Revision: 1.6
Committed: Thu Oct 16 13:37:52 2003 UTC (20 years, 7 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +12 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 device-tincd.C -- include one of the tincd low level implementations.
3 Copyright (C) 2003 Marc Lehmann <pcg@goof.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <cstdio>
21 #include <cstring>
22 #include <cerrno>
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/ioctl.h>
27 #include <unistd.h>
28 #include <syslog.h>
29 #include <fcntl.h>
30
31 #include "conf.h"
32
33 // following headers used by cygwin (maybe others)
34 #include "netcompat.h"
35 #include <signal.h>
36
37 #define xstrdup(strd) strdup(str)
38
39 /* make the tincd sources feel comfortable in our environment. */
40 /* this was reasonably easy to do. */
41 #define routing_mode 1
42 #define RMODE_ROUTER 0
43
44 #define LOG_TO_L(level) \
45 (level) == LOG_ERR ? L_ERR \
46 : (level) == LOG_DEBUG ? L_DEBUG \
47 : (level) == LOG_WARNING ? L_WARN \
48 : (level) == LOG_INFO ? L_INFO \
49 : L_NOTICE
50
51 #if __STDC_VERSION__ > 199900
52 # define logger(level, ...) slog (LOG_TO_L(level), __VA_ARGS__)
53 #elif __GNUC__
54 # define logger(level, args...) slog (LOG_TO_L(level), ## args)
55 #else
56 # error either need ISO-C 99 compliant compiler or gcc.
57 #endif
58
59 #define ifdebug(subsys) if (0)
60
61 #define cp()
62 #define lookup_config(config_tree,key) (key)
63
64 #define MTU MAXSIZE
65
66 // BIGGEST hack of 'em all
67 // will be casted to data_packet, due to structural similarity
68 struct vpn_packet_t : net_packet {
69 u8 data[MAXSIZE];
70 };
71
72 static tap_device *self;
73
74 static bool overwrite_mac;
75
76 static bool
77 get_config_string(const char *key, char **res)
78 {
79 if (!strcmp (key, "Interface"))
80 *res = conf.ifname;
81 else if (!strcmp (key, "Device"))
82 *res = 0;
83 else
84 {
85 slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key);
86 *res = 0;
87 }
88
89 return *res;
90 }
91
92 #define netname conf.ifname
93
94 #if IF_linux
95 # include "tincd/linux/device.c"
96
97 #elif IF_freebsd
98 # include "tincd/freebsd/device.c"
99
100 #elif IF_netbsd
101 #define IF_istun 1
102 # include "tincd/netbsd/device.c"
103
104 #elif IF_openbsd
105 #define IF_istun 1
106 # include "tincd/openbsd/device.c"
107
108 #elif IF_solaris
109 # include "tincd/solaris/device.c"
110
111 #elif IF_cygwin
112 # include "tincd/cygwin/device.c"
113
114 #elif IF_mingw
115 # include "tincd/mingw/device.c"
116
117 #elif IF_darwin
118 #define IF_istun 1
119 # include "tincd/darwin/device.c"
120
121 #elif IF_raw_socket
122 #define IF_istun 1
123 # include "tincd/raw_socket/device.c"
124
125 #else
126 # error No interface implementation for your IFTYPE/IFSUBTYPE combination.
127 #endif
128
129 #if IF_istun
130 # include "ether_emu.C"
131 #endif
132
133 const char *
134 tap_device::info ()
135 {
136 return _("tincd compatibility layer");
137 }
138
139 tap_device::tap_device ()
140 {
141 self = this;
142
143 if (setup_device ())
144 {
145 //slog (L_DEBUG, _("%s is a %s"), device, info ());
146 fd = device_fd;
147 strcpy (ifrname, iface);
148 }
149 else
150 {
151 slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ());
152 exit (1);
153 }
154 }
155
156 tap_device::~tap_device ()
157 {
158 close_device ();
159 }
160
161 tap_packet *
162 tap_device::recv ()
163 {
164 tap_packet *pkt = new tap_packet;
165
166 if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt)))
167 {
168 delete pkt;
169 slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE,
170 strerror (errno));
171 return 0;
172 }
173
174 #if IF_istun
175 // assume ipv4
176 (*pkt)[12] = 0x08;
177 (*pkt)[13] = 0x00;
178
179 if (!ether_emu.tun_to_tap (pkt))
180 {
181 delete pkt;
182 return 0;
183 }
184 #endif
185
186 return pkt;
187 }
188
189 void
190 tap_device::send (tap_packet *pkt)
191 {
192 if (
193 #if IF_istun
194 ether_emu.tap_to_tun (pkt) &&
195 #endif
196 !write_packet (reinterpret_cast<vpn_packet_t *>(pkt)))
197 slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE,
198 strerror (errno));
199 }
200
201