… | |
… | |
56 | #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) |
56 | #define TAP_IOCTL_GET_LASTMAC TAP_CONTROL_CODE(0, METHOD_BUFFERED) |
57 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) |
57 | #define TAP_IOCTL_GET_MAC TAP_CONTROL_CODE(1, METHOD_BUFFERED) |
58 | #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) |
58 | #define TAP_IOCTL_SET_STATISTICS TAP_CONTROL_CODE(2, METHOD_BUFFERED) |
59 | |
59 | |
60 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
60 | static HANDLE device_handle = INVALID_HANDLE_VALUE; |
|
|
61 | static mac my_mac; |
61 | |
62 | |
62 | static const char * |
63 | static const char * |
63 | wstrerror (int err) |
64 | wstrerror (int err) |
64 | { |
65 | { |
65 | static char buf[1024]; |
66 | static char buf[1024]; |
… | |
… | |
127 | RegCloseKey (key2); |
128 | RegCloseKey (key2); |
128 | |
129 | |
129 | if (err) |
130 | if (err) |
130 | continue; |
131 | continue; |
131 | |
132 | |
|
|
133 | if (conf.ifname) |
|
|
134 | { |
132 | if (strcmp (conf.ifname, adapterid)) |
135 | if (strcmp (conf.ifname, adapterid)) |
133 | continue; |
136 | continue; |
134 | |
137 | } |
|
|
138 | else |
|
|
139 | { |
135 | found = true; |
140 | found = true; |
136 | break; |
141 | break; |
|
|
142 | } |
137 | |
143 | |
138 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, |
144 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, adapterid); |
139 | adapterid); |
145 | device_handle = CreateFile(tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
140 | device_handle = CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, indent: Standard input: 237: Error:Stmt nesting error.FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, |
146 | OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
141 | 0); |
|
|
142 | if (device_handle != INVALID_HANDLE_VALUE) |
147 | if (device_handle != INVALID_HANDLE_VALUE) |
143 | { |
148 | { |
144 | found = true; |
149 | found = true; |
145 | break; |
150 | break; |
146 | } |
151 | } |
… | |
… | |
149 | RegCloseKey (key); |
154 | RegCloseKey (key); |
150 | |
155 | |
151 | if (!found) |
156 | if (!found) |
152 | { |
157 | { |
153 | slog (L_ERR, _("No Windows tap device found!")); |
158 | slog (L_ERR, _("No Windows tap device found!")); |
154 | return false; |
159 | exit (1); |
155 | } |
160 | } |
156 | |
161 | |
157 | if (!device) |
162 | strcpy (ifrname, adaptername); |
158 | device = xstrdup (adapterid); |
|
|
159 | |
|
|
160 | if (!iface) |
|
|
161 | iface = xstrdup (adaptername); |
|
|
162 | |
163 | |
163 | /* Try to open the corresponding tap device */ |
164 | /* Try to open the corresponding tap device */ |
164 | |
165 | |
165 | if (device_handle == INVALID_HANDLE_VALUE) |
166 | if (device_handle == INVALID_HANDLE_VALUE) |
166 | { |
167 | { |
167 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, |
168 | snprintf (tapname, sizeof (tapname), USERMODEDEVICEDIR "%s" TAPSUFFIX, device); |
168 | device); |
|
|
169 | device_handle = |
169 | device_handle = |
170 | CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
170 | CreateFile (tapname, GENERIC_WRITE | GENERIC_READ, 0, 0, |
171 | OPEN_EXISTING, |
171 | OPEN_EXISTING, |
172 | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
172 | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, 0); |
173 | } |
173 | } |
174 | |
174 | |
175 | if (device_handle == INVALID_HANDLE_VALUE) |
175 | if (device_handle == INVALID_HANDLE_VALUE) |
176 | { |
176 | { |
177 | slog (L_ERR, _("%s (%s) is not a usable Windows tap device: %s"), |
177 | slog (L_ERR, _("%s is not a usable Windows tap device: %s"), |
178 | device, iface, wstrerror (GetLastError ())); |
178 | adaptername, wstrerror (GetLastError ())); |
179 | return false; |
179 | exit (1); |
180 | } |
180 | } |
|
|
181 | |
|
|
182 | fd = cygwin_attach_handle_to_fd (tapname, -1, device_handle, 1, GENERIC_WRITE | GENERIC_READ); |
181 | |
183 | |
182 | /* Get MAC address from tap device */ |
184 | /* Get MAC address from tap device */ |
183 | |
185 | |
184 | if (!DeviceIoControl |
186 | if (!DeviceIoControl |
185 | (device_handle, TAP_IOCTL_GET_MAC, mymac.x, sizeof (mymac.x), mymac.x, |
187 | (device_handle, TAP_IOCTL_GET_MAC, &mac, sizeof (mac), &mac, sizeof (mac), &len, 0)) |
186 | sizeof (mymac.x), &len, 0)) |
|
|
187 | { |
188 | { |
188 | slog (L_ERR, |
189 | slog (L_ERR, |
189 | _ |
|
|
190 | ("Could not get MAC address from Windows tap device %s (%s): %s"), |
190 | _("Could not get MAC address from Windows tap device %s: %s"), |
191 | device, iface, wstrerror (GetLastError ())); |
|
|
192 | return false; |
|
|
193 | } |
|
|
194 | |
|
|
195 | if (routing_mode == RMODE_ROUTER) |
|
|
196 | { |
|
|
197 | overwrite_mac = 1; |
|
|
198 | } |
|
|
199 | |
|
|
200 | /* Create a listening socket */ |
|
|
201 | |
|
|
202 | err = getaddrinfo (NULL, myport, &hint, &ai); |
|
|
203 | |
|
|
204 | if (err || !ai) |
|
|
205 | { |
|
|
206 | slog (L_ERR, _("System call `%s' failed: %s"), "getaddrinfo", |
|
|
207 | gai_strerror (errno)); |
|
|
208 | return false; |
|
|
209 | } |
|
|
210 | |
|
|
211 | sock = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
|
|
212 | |
|
|
213 | if (sock < 0) |
|
|
214 | { |
|
|
215 | slog (L_ERR, _("System call `%s' failed: %s"), "socket", |
|
|
216 | strerror (errno)); |
|
|
217 | return false; |
|
|
218 | } |
|
|
219 | |
|
|
220 | if (bind (sock, ai->ai_addr, ai->ai_addrlen)) |
|
|
221 | { |
|
|
222 | slog (L_ERR, _("System call `%s' failed: %s"), "bind", |
|
|
223 | strerror (errno)); |
|
|
224 | return false; |
|
|
225 | } |
|
|
226 | |
|
|
227 | freeaddrinfo (ai); |
|
|
228 | |
|
|
229 | if (listen (sock, 1)) |
|
|
230 | { |
|
|
231 | slog (L_ERR, _("System call `%s' failed: %s"), "listen", |
|
|
232 | strerror (errno)); |
|
|
233 | return false; |
|
|
234 | } |
|
|
235 | |
|
|
236 | /* Start the tap reader */ |
|
|
237 | |
|
|
238 | thread = CreateThread (NULL, 0, tapreader, NULL, 0, NULL); |
|
|
239 | |
|
|
240 | if (!thread) |
|
|
241 | { |
|
|
242 | slog (L_ERR, _("System call `%s' failed: %s"), "CreateThread", |
|
|
243 | wstrerror (GetLastError ())); |
191 | adaptername, wstrerror (GetLastError ())); |
244 | return false; |
192 | exit (1); |
245 | } |
|
|
246 | |
|
|
247 | /* Wait for the tap reader to connect back to us */ |
|
|
248 | |
|
|
249 | if ((device_fd = accept (sock, NULL, 0)) == -1) |
|
|
250 | { |
193 | } |
251 | slog (L_ERR, _("System call `%s' failed: %s"), "accept", |
|
|
252 | strerror (errno)); |
|
|
253 | return false; |
|
|
254 | } |
|
|
255 | |
|
|
256 | closesocket (sock); |
|
|
257 | |
|
|
258 | device_info = _("Windows tap device"); |
|
|
259 | |
|
|
260 | slog (L_INFO, _("%s (%s) is a %s"), device, iface, device_info); |
|
|
261 | |
|
|
262 | return true; |
|
|
263 | } |
|
|
264 | |
|
|
265 | } |
194 | } |
266 | |
195 | |
267 | tap_device::~tap_device () |
196 | tap_device::~tap_device () |
268 | { |
197 | { |
269 | close (fd); |
198 | close (fd); |