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 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 |
100 | # include "tincd/solaris/device.c" |
107 | # include "tincd/solaris/device.c" |
|
|
108 | |
101 | #elif IF_cygwin |
109 | #elif IF_cygwin |
102 | # include "tincd/cygwin/device.c" |
110 | # include "tincd/cygwin/device.c" |
|
|
111 | |
103 | #elif IF_mingw |
112 | #elif IF_mingw |
104 | # include "tincd/mingw/device.c" |
113 | # include "tincd/mingw/device.c" |
|
|
114 | |
105 | #elif IF_darwin |
115 | #elif IF_darwin |
|
|
116 | #define IF_istun 1 |
106 | # include "tincd/darwin/device.c" |
117 | # include "tincd/darwin/device.c" |
|
|
118 | |
107 | #elif IF_raw_socket |
119 | #elif IF_raw_socket |
|
|
120 | #define IF_istun 1 |
108 | # include "tincd/raw_socket/device.c" |
121 | # include "tincd/raw_socket/device.c" |
|
|
122 | |
109 | #else |
123 | #else |
110 | # error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
124 | # error No interface implementation for your IFTYPE/IFSUBTYPE combination. |
|
|
125 | #endif |
|
|
126 | |
|
|
127 | #if IF_istun |
|
|
128 | # include "ether_emu.C" |
111 | #endif |
129 | #endif |
112 | |
130 | |
113 | const char * |
131 | const char * |
114 | tap_device::info () |
132 | tap_device::info () |
115 | { |
133 | { |
116 | return _("tincd compatibility layer"); |
134 | return _("tincd compatibility layer"); |
117 | } |
135 | } |
118 | |
136 | |
119 | tap_device::tap_device () |
137 | tap_device::tap_device () |
120 | { |
138 | { |
121 | self = this; |
|
|
122 | |
|
|
123 | if (setup_device ()) |
139 | if (setup_device ()) |
124 | { |
140 | { |
125 | //slog (L_DEBUG, _("%s is a %s"), device, info ()); |
141 | slog (L_DEBUG, _("interface %s initialized"), info ()); |
126 | fd = device_fd; |
142 | fd = device_fd; |
127 | strcpy (ifrname, iface); |
143 | strcpy (ifrname, iface); |
128 | } |
144 | } |
129 | else |
145 | else |
130 | { |
146 | { |
131 | slog (L_ERR, _("error while configuring tincd device (%s/%s)"), device, info ()); |
147 | slog (L_ERR, _("error while configuring tincd device (%s)"), info ()); |
132 | exit (1); |
148 | exit (1); |
133 | } |
149 | } |
134 | } |
150 | } |
135 | |
151 | |
136 | tap_device::~tap_device () |
152 | tap_device::~tap_device () |
… | |
… | |
149 | slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
165 | slog (L_ERR, _("can't read from to %s %s: %s"), info (), DEFAULT_DEVICE, |
150 | strerror (errno)); |
166 | strerror (errno)); |
151 | return 0; |
167 | return 0; |
152 | } |
168 | } |
153 | |
169 | |
|
|
170 | #if IF_istun |
|
|
171 | // assume ipv4 |
|
|
172 | (*pkt)[12] = 0x08; |
|
|
173 | (*pkt)[13] = 0x00; |
|
|
174 | |
|
|
175 | if (!ether_emu.tun_to_tap (pkt)) |
|
|
176 | { |
|
|
177 | delete pkt; |
|
|
178 | return 0; |
|
|
179 | } |
|
|
180 | #endif |
|
|
181 | |
154 | return pkt; |
182 | return pkt; |
155 | } |
183 | } |
156 | |
184 | |
157 | void |
185 | void |
158 | tap_device::send (tap_packet *pkt) |
186 | tap_device::send (tap_packet *pkt) |
159 | { |
187 | { |
|
|
188 | if ( |
|
|
189 | #if IF_istun |
|
|
190 | ether_emu.tap_to_tun (pkt) && |
|
|
191 | #endif |
160 | if (!write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
192 | !write_packet (reinterpret_cast<vpn_packet_t *>(pkt))) |
161 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
193 | slog (L_ERR, _("can't write to %s %s: %s"), info (), DEFAULT_DEVICE, |
162 | strerror (errno)); |
194 | strerror (errno)); |
163 | } |
195 | } |
164 | |
196 | |
165 | |
197 | |