1 |
/* |
2 |
device-tincd.C -- include one of the tincd low level implementations. |
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 <cstdio> |
20 |
#include <cstring> |
21 |
#include <cerrno> |
22 |
|
23 |
#include <sys/types.h> |
24 |
#include <sys/stat.h> |
25 |
#include <sys/ioctl.h> |
26 |
#include <unistd.h> |
27 |
#include <syslog.h> |
28 |
#include <fcntl.h> |
29 |
|
30 |
#include "conf.h" |
31 |
|
32 |
#define routing_mode 1 |
33 |
#define RMODE_ROUTER 0 |
34 |
|
35 |
/* need iso c-90 or ugly workaround :( */ |
36 |
#define logger(level, ...) slog ( \ |
37 |
(level) == LOG_ERR ? L_ERR \ |
38 |
: (level) == LOG_DEBUG ? L_DEBUG \ |
39 |
: (level) == LOG_WARNING ? L_WARN \ |
40 |
: (level) == LOG_INFO ? L_INFO \ |
41 |
: L_NOTICE, __VA_ARGS__) |
42 |
|
43 |
#define ifdebug(subsys) if (0) |
44 |
|
45 |
#define cp() |
46 |
#define lookup_config(config_tree,key) (key) |
47 |
|
48 |
#define MTU MAXSIZE |
49 |
|
50 |
// BIGGEST hack of 'em all |
51 |
// will be casted to data_packet, due to structural similarity |
52 |
struct vpn_packet_t : net_packet { |
53 |
u8 data[MAXSIZE]; |
54 |
}; |
55 |
|
56 |
static tap_device *self; |
57 |
|
58 |
static bool overwrite_mac; |
59 |
|
60 |
static bool |
61 |
get_config_string(const char *key, char **res) |
62 |
{ |
63 |
if (!strcmp (key, "Interface")) |
64 |
*res = conf.ifname; |
65 |
else if (!strcmp (key, "Device")) |
66 |
*res = 0; |
67 |
else |
68 |
{ |
69 |
slog (L_ERR, _("tincd layer asking for unknown config '%s'"), key); |
70 |
*res = 0; |
71 |
} |
72 |
|
73 |
return *res; |
74 |
} |
75 |
|
76 |
#define netname conf.ifname |
77 |
|
78 |
#if IF_linux |
79 |
# include "tincd/linux/device.c" |
80 |
#elif IF_freebsd |
81 |
# include "tincd/freebsd/device.c" |
82 |
#elif IF_netbsd |
83 |
# include "tincd/netbsd/device.c" |
84 |
#elif IF_solaris |
85 |
# include "tincd/solaris/device.c" |
86 |
#elif IF_cygwin |
87 |
# include "tincd/cygwin/device.c" |
88 |
#elif IF_mingw |
89 |
# include "tincd/mingw/device.c" |
90 |
#elif IF_darwin |
91 |
# include "tincd/darwin/device.c" |
92 |
#elif IF_raw_socket |
93 |
# include "tincd/raw_socket/device.c" |
94 |
#else |
95 |
# error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
96 |
#endif |
97 |
|
98 |
const char * |
99 |
tap_device::info () |
100 |
{ |
101 |
return _("tincd compatibility layer"); |
102 |
} |
103 |
|
104 |
tap_device::tap_device () |
105 |
{ |
106 |
self = this; |
107 |
|
108 |
if (setup_device ()) |
109 |
{ |
110 |
//slog (L_DEBUG, _("%s is a %s"), device, info ()); |
111 |
fd = device_fd; |
112 |
strcpy (ifrname, iface); |
113 |
} |
114 |
else |
115 |
{ |
116 |
slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ()); |
117 |
exit (1); |
118 |
} |
119 |
} |
120 |
|
121 |
tap_device::~tap_device () |
122 |
{ |
123 |
close_device (); |
124 |
} |
125 |
|
126 |
tap_packet * |
127 |
tap_device::recv () |
128 |
{ |
129 |
tap_packet *pkt = new tap_packet; |
130 |
|
131 |
if (!read_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
132 |
{ |
133 |
delete pkt; |
134 |
slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
135 |
strerror (errno)); |
136 |
return 0; |
137 |
} |
138 |
|
139 |
return pkt; |
140 |
} |
141 |
|
142 |
void |
143 |
tap_device::send (tap_packet *pkt) |
144 |
{ |
145 |
if (!write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
146 |
slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
147 |
strerror (errno)); |
148 |
} |
149 |
|
150 |
|