1 | /* |
1 | /* |
2 | device-tincd.C -- include one of the tincd low level implementations. |
2 | device-tincd.C -- include one of the tincd low level implementations. |
|
|
3 | Copyright (C) 2003-2004 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. |
… | |
… | |
66 | // will be casted to data_packet, due to structural similarity |
67 | // will be casted to data_packet, due to structural similarity |
67 | struct vpn_packet_t : net_packet { |
68 | struct vpn_packet_t : net_packet { |
68 | u8 data[MAXSIZE]; |
69 | u8 data[MAXSIZE]; |
69 | }; |
70 | }; |
70 | |
71 | |
71 | static tap_device *self; |
|
|
72 | |
|
|
73 | static bool overwrite_mac; |
72 | static bool overwrite_mac; |
74 | |
73 | |
75 | static bool |
74 | static bool |
76 | get_config_string(const char *key, char **res) |
75 | get_config_string(const char *key, char **res) |
77 | { |
76 | { |
… | |
… | |
90 | |
89 | |
91 | #define netname conf.ifname |
90 | #define netname conf.ifname |
92 | |
91 | |
93 | #if IF_linux |
92 | #if IF_linux |
94 | # include "tincd/linux/device.c" |
93 | # include "tincd/linux/device.c" |
|
|
94 | |
95 | #elif IF_freebsd |
95 | #elif IF_freebsd |
96 | # include "tincd/freebsd/device.c" |
96 | # include "tincd/freebsd/device.c" |
|
|
97 | |
97 | #elif IF_netbsd |
98 | #elif IF_netbsd |
|
|
99 | # define IF_istun 1 |
98 | # include "tincd/netbsd/device.c" |
100 | # include "tincd/netbsd/device.c" |
|
|
101 | |
|
|
102 | #elif IF_openbsd |
|
|
103 | # define IF_istun 1 |
|
|
104 | # include "tincd/openbsd/device.c" |
|
|
105 | |
99 | #elif IF_solaris |
106 | #elif IF_solaris |
|
|
107 | # define IF_istun 1 |
100 | # include "tincd/solaris/device.c" |
108 | # include "tincd/solaris/device.c" |
|
|
109 | |
101 | #elif IF_cygwin |
110 | #elif IF_cygwin |
102 | # include "tincd/cygwin/device.c" |
111 | # include "tincd/cygwin/device.c" |
|
|
112 | |
103 | #elif IF_mingw |
113 | #elif IF_mingw |
104 | # include "tincd/mingw/device.c" |
114 | # include "tincd/mingw/device.c" |
|
|
115 | |
105 | #elif IF_darwin |
116 | #elif IF_darwin |
|
|
117 | # define IF_istun 1 |
106 | # include "tincd/darwin/device.c" |
118 | # include "tincd/darwin/device.c" |
|
|
119 | |
107 | #elif IF_raw_socket |
120 | #elif IF_raw_socket |
|
|
121 | # define IF_istun 1 |
108 | # include "tincd/raw_socket/device.c" |
122 | # include "tincd/raw_socket/device.c" |
|
|
123 | |
109 | #else |
124 | #else |
110 | # error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
125 | # error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
111 | #endif |
126 | #endif |
112 | |
127 | |
|
|
128 | #if IF_istun |
|
|
129 | # include "ether_emu.C" |
|
|
130 | #endif |
|
|
131 | |
113 | const char * |
132 | const char * |
114 | tap_device::info () |
133 | tap_device::info () |
115 | { |
134 | { |
116 | return _("tincd compatibility layer"); |
135 | return _("tincd compatibility layer"); |
117 | } |
136 | } |
118 | |
137 | |
119 | tap_device::tap_device () |
138 | tap_device::tap_device () |
120 | { |
139 | { |
121 | self = this; |
140 | device = "(null)"; |
122 | |
141 | |
123 | if (setup_device ()) |
142 | if (setup_device ()) |
124 | { |
143 | { |
125 | //slog (L_DEBUG, _("%s is a %s"), device, info ()); |
144 | slog (L_DEBUG, _("interface %s on %s initialized"), info (), device); |
126 | fd = device_fd; |
145 | fd = device_fd; |
127 | strcpy (ifrname, iface); |
146 | strcpy (ifrname, iface); |
128 | } |
147 | } |
129 | else |
148 | else |
130 | { |
149 | { |
131 | slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ()); |
150 | slog (L_ERR, _("error while configuring tincd device %s on %s"), info (), device); |
132 | exit (1); |
151 | exit (EXIT_FAILURE); |
133 | } |
152 | } |
134 | } |
153 | } |
135 | |
154 | |
136 | tap_device::~tap_device () |
155 | tap_device::~tap_device () |
137 | { |
156 | { |
… | |
… | |
149 | slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
168 | slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
150 | strerror (errno)); |
169 | strerror (errno)); |
151 | return 0; |
170 | return 0; |
152 | } |
171 | } |
153 | |
172 | |
|
|
173 | #if IF_istun |
|
|
174 | // assume ipv4 |
|
|
175 | (*pkt)[12] = 0x08; |
|
|
176 | (*pkt)[13] = 0x00; |
|
|
177 | |
|
|
178 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
179 | { |
|
|
180 | delete pkt; |
|
|
181 | return 0; |
|
|
182 | } |
|
|
183 | #endif |
|
|
184 | |
154 | return pkt; |
185 | return pkt; |
155 | } |
186 | } |
156 | |
187 | |
157 | void |
188 | void |
158 | tap_device::send (tap_packet *pkt) |
189 | tap_device::send (tap_packet *pkt) |
159 | { |
190 | { |
|
|
191 | if ( |
|
|
192 | #if IF_istun |
|
|
193 | ether_emu.tap_to_tun (pkt) && |
|
|
194 | #endif |
160 | if (!write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
195 | !write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
161 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
196 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
162 | strerror (errno)); |
197 | strerror (errno)); |
163 | } |
198 | } |
164 | |
199 | |
165 | |
200 | |