BTap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /**
  2. * @file BTap.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <stdio.h>
  24. #ifdef BADVPN_USE_WINAPI
  25. #include <windows.h>
  26. #include <winioctl.h>
  27. #include <objbase.h>
  28. #include <wtypes.h>
  29. #include "wintap-common.h"
  30. #else
  31. #include <linux/if_tun.h>
  32. #include <net/if.h>
  33. #include <net/if_arp.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <sys/socket.h>
  38. #include <fcntl.h>
  39. #include <unistd.h>
  40. #include <errno.h>
  41. #endif
  42. #include <tuntap/BTap.h>
  43. #define REGNAME_SIZE 256
  44. static void report_error (BTap *o);
  45. static void input_handler_send (BTap *o, uint8_t *data, int data_len);
  46. static void input_handler_cancel (BTap *o);
  47. static void output_handler_recv (BTap *o, uint8_t *data);
  48. #ifdef BADVPN_USE_WINAPI
  49. static int try_send (BTap *o, uint8_t *data, int data_len)
  50. {
  51. // setup overlapped
  52. memset(&o->input_ol, 0, sizeof(o->input_ol));
  53. o->input_ol.hEvent = o->input_event;
  54. // attempt write
  55. if (!WriteFile(o->device, data, data_len, NULL, &o->input_ol)) {
  56. DWORD error = GetLastError();
  57. if (error == ERROR_IO_PENDING) {
  58. // write pending
  59. return 0;
  60. }
  61. DEBUG("WARNING: WriteFile failed (%u)", error);
  62. return 1;
  63. }
  64. // read result
  65. DWORD bytes;
  66. if (!GetOverlappedResult(o->device, &o->input_ol, &bytes, FALSE)) {
  67. DEBUG("WARNING: GetOverlappedResult failed (%u)", GetLastError());
  68. }
  69. else if (bytes != data_len) {
  70. DEBUG("WARNING: written %d expected %d", (int)bytes, data_len);
  71. }
  72. // reset event
  73. ASSERT_FORCE(ResetEvent(o->input_event))
  74. return 1;
  75. }
  76. static int try_recv (BTap *o, uint8_t *data, int *data_len)
  77. {
  78. // setup overlapped
  79. memset(&o->output_ol, 0, sizeof(o->output_ol));
  80. o->output_ol.hEvent = o->output_event;
  81. // attempt read
  82. if (!ReadFile(o->device, data, o->frame_mtu, NULL, &o->output_ol)) {
  83. DWORD error = GetLastError();
  84. if (error == ERROR_IO_PENDING) {
  85. // read pending
  86. return 0;
  87. }
  88. DEBUG("ReadFile failed (%u)", error);
  89. // fatal error
  90. return -1;
  91. }
  92. // read result
  93. DWORD bytes;
  94. if (!GetOverlappedResult(o->device, &o->output_ol, &bytes, FALSE)) {
  95. DEBUG("GetOverlappedResult (output) failed (%u)", GetLastError());
  96. // fatal error
  97. return -1;
  98. }
  99. ASSERT_FORCE(bytes <= o->frame_mtu)
  100. // reset event
  101. ASSERT_FORCE(ResetEvent(o->output_event))
  102. *data_len = bytes;
  103. return 1;
  104. }
  105. static void write_handle_handler (BTap *o)
  106. {
  107. ASSERT(o->input_packet_len >= 0)
  108. DebugObject_Access(&o->d_obj);
  109. // disable handle event
  110. BReactor_DisableHandle(o->reactor, &o->input_bhandle);
  111. // read result
  112. DWORD bytes;
  113. if (!GetOverlappedResult(o->device, &o->input_ol, &bytes, FALSE)) {
  114. DEBUG("WARNING: GetOverlappedResult (input) failed (%u)", GetLastError());
  115. } else if (bytes != o->input_packet_len) {
  116. DEBUG("WARNING: written %d expected %d", (int)bytes, o->input_packet_len);
  117. }
  118. // set no input packet
  119. o->input_packet_len = -1;
  120. // reset event
  121. ASSERT_FORCE(ResetEvent(o->input_event))
  122. // inform sender we finished the packet
  123. PacketPassInterface_Done(&o->input);
  124. }
  125. static void read_handle_handler (BTap *o)
  126. {
  127. ASSERT(o->output_packet)
  128. DebugObject_Access(&o->d_obj);
  129. int bytes;
  130. // read result
  131. DWORD dbytes;
  132. if (!GetOverlappedResult(o->device, &o->output_ol, &dbytes, FALSE)) {
  133. DWORD error = GetLastError();
  134. DEBUG("GetOverlappedResult (output) failed (%u)", error);
  135. // handle accidental cancelation from input_handler_cancel
  136. if (error == ERROR_OPERATION_ABORTED) {
  137. DEBUG("retrying read");
  138. // reset event
  139. ASSERT_FORCE(ResetEvent(o->output_event))
  140. // try receiving
  141. int res;
  142. if ((res = try_recv(o, o->output_packet, &bytes)) < 0) {
  143. goto fatal_error;
  144. }
  145. if (!res) {
  146. // keep waiting
  147. return;
  148. }
  149. goto done;
  150. }
  151. fatal_error:
  152. // set no output packet (so that BTap_Free doesn't try getting the result again)
  153. o->output_packet = NULL;
  154. // report fatal error
  155. report_error(o);
  156. return;
  157. }
  158. // reset event
  159. ASSERT_FORCE(ResetEvent(o->output_event))
  160. bytes = dbytes;
  161. done:
  162. ASSERT_FORCE(bytes <= o->frame_mtu)
  163. // disable handle event
  164. BReactor_DisableHandle(o->reactor, &o->output_bhandle);
  165. // set no output packet
  166. o->output_packet = NULL;
  167. // inform receiver we finished the packet
  168. PacketRecvInterface_Done(&o->output, bytes);
  169. }
  170. #else
  171. static void fd_handler (BTap *o, int events)
  172. {
  173. DebugObject_Access(&o->d_obj);
  174. DEAD_DECLARE
  175. if (events&BREACTOR_ERROR) {
  176. DEBUG("WARNING: device fd reports error?");
  177. }
  178. if (events&BREACTOR_WRITE) do {
  179. ASSERT(o->input_packet_len >= 0)
  180. int bytes = write(o->fd, o->input_packet, o->input_packet_len);
  181. if (bytes < 0) {
  182. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  183. // retry later
  184. break;
  185. }
  186. // malformed packets will cause errors, ignore them and act like
  187. // the packet was accepeted
  188. } else {
  189. if (bytes != o->input_packet_len) {
  190. DEBUG("WARNING: written %d expected %d", bytes, o->input_packet_len);
  191. }
  192. }
  193. // set no input packet
  194. o->input_packet_len = -1;
  195. // update events
  196. o->poll_events &= ~BREACTOR_WRITE;
  197. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, o->poll_events);
  198. // inform sender we finished the packet
  199. PacketPassInterface_Done(&o->input);
  200. } while (0);
  201. if (events&BREACTOR_READ) do {
  202. ASSERT(o->output_packet)
  203. // try reading into the buffer
  204. int bytes = read(o->fd, o->output_packet, o->frame_mtu);
  205. if (bytes < 0) {
  206. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  207. // retry later
  208. break;
  209. }
  210. // report fatal error
  211. report_error(o);
  212. return;
  213. }
  214. ASSERT_FORCE(bytes <= o->frame_mtu)
  215. // set no output packet
  216. o->output_packet = NULL;
  217. // update events
  218. o->poll_events &= ~BREACTOR_READ;
  219. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, o->poll_events);
  220. // inform receiver we finished the packet
  221. PacketRecvInterface_Done(&o->output, bytes);
  222. } while (0);
  223. }
  224. #endif
  225. void report_error (BTap *o)
  226. {
  227. #ifndef NDEBUG
  228. DEAD_ENTER(o->dead)
  229. #endif
  230. o->handler_error(o->handler_error_user);
  231. #ifndef NDEBUG
  232. ASSERT(DEAD_KILLED)
  233. DEAD_LEAVE(o->dead);
  234. #endif
  235. }
  236. void input_handler_send (BTap *o, uint8_t *data, int data_len)
  237. {
  238. ASSERT(data_len >= 0)
  239. ASSERT(data_len <= o->frame_mtu)
  240. ASSERT(o->input_packet_len == -1)
  241. DebugObject_Access(&o->d_obj);
  242. #ifdef BADVPN_USE_WINAPI
  243. if (!try_send(o, data, data_len)) {
  244. // write pending
  245. o->input_packet = data;
  246. o->input_packet_len = data_len;
  247. BReactor_EnableHandle(o->reactor, &o->input_bhandle);
  248. return;
  249. }
  250. #else
  251. int bytes = write(o->fd, data, data_len);
  252. if (bytes < 0) {
  253. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  254. // retry later in fd_handler
  255. // remember packet
  256. o->input_packet = data;
  257. o->input_packet_len = data_len;
  258. // update events
  259. o->poll_events |= BREACTOR_WRITE;
  260. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, o->poll_events);
  261. return;
  262. }
  263. // malformed packets will cause errors, ignore them and act like
  264. // the packet was accepeted
  265. } else {
  266. if (bytes != data_len) {
  267. DEBUG("WARNING: written %d expected %d", bytes, data_len);
  268. }
  269. }
  270. #endif
  271. PacketPassInterface_Done(&o->input);
  272. }
  273. void input_handler_cancel (BTap *o)
  274. {
  275. DebugObject_Access(&o->d_obj);
  276. ASSERT(o->input_packet_len >= 0)
  277. #ifdef BADVPN_USE_WINAPI
  278. // disable handle event
  279. BReactor_DisableHandle(o->reactor, &o->input_bhandle);
  280. // cancel I/O on the device
  281. // this also cancels reading, so handle the aborted error code in read_handle_handler
  282. ASSERT_FORCE(CancelIo(o->device))
  283. // wait for it
  284. DWORD bytes;
  285. if (!GetOverlappedResult(o->device, &o->input_ol, &bytes, TRUE)) {
  286. DWORD error = GetLastError();
  287. if (error != ERROR_OPERATION_ABORTED) {
  288. DEBUG("WARNING: GetOverlappedResult (input) failed (%u)", error);
  289. }
  290. } else if (bytes != o->input_packet_len) {
  291. DEBUG("WARNING: written %d expected %d", (int)bytes, o->input_packet_len);
  292. }
  293. // reset event
  294. ASSERT_FORCE(ResetEvent(o->input_event))
  295. #else
  296. // update events
  297. o->poll_events &= ~BREACTOR_WRITE;
  298. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, o->poll_events);
  299. #endif
  300. // set no input packet
  301. o->input_packet_len = -1;
  302. }
  303. void output_handler_recv (BTap *o, uint8_t *data)
  304. {
  305. ASSERT(data)
  306. ASSERT(!o->output_packet)
  307. DebugObject_Access(&o->d_obj);
  308. #ifdef BADVPN_USE_WINAPI
  309. int bytes;
  310. int res;
  311. if ((res = try_recv(o, data, &bytes)) < 0) {
  312. // report fatal error
  313. report_error(o);
  314. return;
  315. }
  316. if (!res) {
  317. // read pending
  318. o->output_packet = data;
  319. BReactor_EnableHandle(o->reactor, &o->output_bhandle);
  320. return;
  321. }
  322. #else
  323. // attempt read
  324. int bytes = read(o->fd, data, o->frame_mtu);
  325. if (bytes < 0) {
  326. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  327. // retry later in fd_handler
  328. // remember packet
  329. o->output_packet = data;
  330. // update events
  331. o->poll_events |= BREACTOR_READ;
  332. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, o->poll_events);
  333. return;
  334. }
  335. // report fatal error
  336. report_error(o);
  337. return;
  338. }
  339. #endif
  340. ASSERT_FORCE(bytes <= o->frame_mtu)
  341. PacketRecvInterface_Done(&o->output, bytes);
  342. }
  343. int BTap_Init (BTap *o, BReactor *reactor, char *devname, BTap_handler_error handler_error, void *handler_error_user)
  344. {
  345. // init arguments
  346. o->reactor = reactor;
  347. o->handler_error = handler_error;
  348. o->handler_error_user = handler_error_user;
  349. #ifdef BADVPN_USE_WINAPI
  350. char *device_component_id;
  351. char *device_name;
  352. char strdata[(devname ? strlen(devname) + 1 : 0)];
  353. if (!devname) {
  354. device_component_id = TAP_COMPONENT_ID;
  355. device_name = NULL;
  356. } else {
  357. strcpy(strdata, devname);
  358. char *colon = strstr(strdata, ":");
  359. if (!colon) {
  360. DEBUG("No colon in device string");
  361. goto fail0;
  362. }
  363. *colon = '\0';
  364. device_component_id = strdata;
  365. device_name = colon + 1;
  366. if (strlen(device_component_id) == 0) {
  367. device_component_id = TAP_COMPONENT_ID;
  368. }
  369. if (strlen(device_name) == 0) {
  370. device_name = NULL;
  371. }
  372. }
  373. DEBUG("Opening component ID %s, name %s", device_component_id, device_name);
  374. // open adapter key
  375. // used to find all devices with the given ComponentId
  376. HKEY adapter_key;
  377. if (RegOpenKeyEx(
  378. HKEY_LOCAL_MACHINE,
  379. ADAPTER_KEY,
  380. 0,
  381. KEY_READ,
  382. &adapter_key
  383. ) != ERROR_SUCCESS) {
  384. DEBUG("Error opening adapter key");
  385. goto fail0;
  386. }
  387. char net_cfg_instance_id[REGNAME_SIZE];
  388. int found = 0;
  389. DWORD i;
  390. for (i = 0;; i++) {
  391. DWORD len;
  392. DWORD type;
  393. char key_name[REGNAME_SIZE];
  394. len = sizeof(key_name);
  395. if (RegEnumKeyEx(adapter_key, i, key_name, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) {
  396. break;
  397. }
  398. char unit_string[REGNAME_SIZE];
  399. snprintf(unit_string, sizeof(unit_string), "%s\\%s", ADAPTER_KEY, key_name);
  400. HKEY unit_key;
  401. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, unit_string, 0, KEY_READ, &unit_key) != ERROR_SUCCESS) {
  402. continue;
  403. }
  404. char component_id[REGNAME_SIZE];
  405. len = sizeof(component_id);
  406. if (RegQueryValueEx(unit_key, "ComponentId", NULL, &type, component_id, &len) != ERROR_SUCCESS || type != REG_SZ) {
  407. ASSERT_FORCE(RegCloseKey(unit_key) == ERROR_SUCCESS)
  408. continue;
  409. }
  410. len = sizeof(net_cfg_instance_id);
  411. if (RegQueryValueEx(unit_key, "NetCfgInstanceId", NULL, &type, net_cfg_instance_id, &len) != ERROR_SUCCESS || type != REG_SZ) {
  412. ASSERT_FORCE(RegCloseKey(unit_key) == ERROR_SUCCESS)
  413. continue;
  414. }
  415. RegCloseKey(unit_key);
  416. // check if ComponentId matches
  417. if (!strcmp(component_id, device_component_id)) {
  418. // if no name was given, use the first device with the given ComponentId
  419. if (!device_name) {
  420. found = 1;
  421. break;
  422. }
  423. // open connection key
  424. char conn_string[REGNAME_SIZE];
  425. snprintf(conn_string, sizeof(conn_string), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, net_cfg_instance_id);
  426. HKEY conn_key;
  427. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, conn_string, 0, KEY_READ, &conn_key) != ERROR_SUCCESS) {
  428. continue;
  429. }
  430. // read name
  431. char name[REGNAME_SIZE];
  432. len = sizeof(name);
  433. if (RegQueryValueEx(conn_key, "Name", NULL, &type, name, &len) != ERROR_SUCCESS || type != REG_SZ) {
  434. ASSERT_FORCE(RegCloseKey(conn_key) == ERROR_SUCCESS)
  435. continue;
  436. }
  437. ASSERT_FORCE(RegCloseKey(conn_key) == ERROR_SUCCESS)
  438. // check name
  439. if (!strcmp(name, device_name)) {
  440. found = 1;
  441. break;
  442. }
  443. }
  444. }
  445. ASSERT_FORCE(RegCloseKey(adapter_key) == ERROR_SUCCESS)
  446. if (!found) {
  447. DEBUG("Could not find TAP device");
  448. goto fail0;
  449. }
  450. char device_path[REGNAME_SIZE];
  451. snprintf(device_path, sizeof(device_path), "%s%s%s", USERMODEDEVICEDIR, net_cfg_instance_id, TAPSUFFIX);
  452. DEBUG("Opening device %s", device_path);
  453. if ((o->device = CreateFile(
  454. device_path,
  455. GENERIC_READ | GENERIC_WRITE,
  456. 0,
  457. 0,
  458. OPEN_EXISTING,
  459. FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED,
  460. 0
  461. )) == INVALID_HANDLE_VALUE) {
  462. DEBUG("CreateFile failed");
  463. goto fail0;
  464. }
  465. // get MTU
  466. ULONG umtu;
  467. DWORD len;
  468. if (!DeviceIoControl(o->device, TAP_IOCTL_GET_MTU, NULL, 0, &umtu, sizeof(umtu), &len, NULL)) {
  469. DEBUG("DeviceIoControl(TAP_IOCTL_GET_MTU) failed");
  470. goto fail1;
  471. }
  472. o->dev_mtu = umtu;
  473. o->frame_mtu = o->dev_mtu + BTAP_ETHERNET_HEADER_LENGTH;
  474. // set connected
  475. ULONG upstatus = TRUE;
  476. if (!DeviceIoControl(o->device, TAP_IOCTL_SET_MEDIA_STATUS, &upstatus, sizeof(upstatus), &upstatus, sizeof(upstatus), &len, NULL)) {
  477. DEBUG("DeviceIoControl(TAP_IOCTL_SET_MEDIA_STATUS) failed");
  478. goto fail1;
  479. }
  480. DEBUG("Device opened");
  481. // init input/output
  482. if (!(o->input_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
  483. DEBUG("CreateEvent failed");
  484. goto fail1;
  485. }
  486. if (!(o->output_event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
  487. DEBUG("CreateEvent failed");
  488. goto fail2;
  489. }
  490. BHandle_Init(&o->input_bhandle, o->input_event, (BHandle_handler)write_handle_handler, o);
  491. BHandle_Init(&o->output_bhandle, o->output_event, (BHandle_handler)read_handle_handler, o);
  492. if (!BReactor_AddHandle(o->reactor, &o->input_bhandle)) {
  493. goto fail3;
  494. }
  495. if (!BReactor_AddHandle(o->reactor, &o->output_bhandle)) {
  496. goto fail4;
  497. }
  498. goto success;
  499. fail4:
  500. BReactor_RemoveHandle(o->reactor, &o->input_bhandle);
  501. fail3:
  502. ASSERT_FORCE(CloseHandle(o->output_event))
  503. fail2:
  504. ASSERT_FORCE(CloseHandle(o->input_event))
  505. fail1:
  506. ASSERT_FORCE(CloseHandle(o->device))
  507. fail0:
  508. return 0;
  509. #else
  510. // open device
  511. if ((o->fd = open("/dev/net/tun", O_RDWR)) < 0) {
  512. DEBUG("error opening device");
  513. return 0;
  514. }
  515. // configure device
  516. struct ifreq ifr;
  517. memset(&ifr, 0, sizeof(ifr));
  518. ifr.ifr_flags |= IFF_TAP | IFF_NO_PI;
  519. if (devname) {
  520. snprintf(ifr.ifr_name, IFNAMSIZ, "%s", devname);
  521. }
  522. if (ioctl(o->fd, TUNSETIFF, (void *)&ifr) < 0) {
  523. DEBUG("error configuring device");
  524. goto fail1;
  525. }
  526. strcpy(o->devname, ifr.ifr_name);
  527. // open dummy socket for ioctls
  528. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  529. if (sock < 0) {
  530. DEBUG("socket failed");
  531. goto fail1;
  532. }
  533. // get MTU
  534. memset(&ifr, 0, sizeof(ifr));
  535. strcpy(ifr.ifr_name, o->devname);
  536. if (ioctl(sock, SIOCGIFMTU, (void *)&ifr) < 0) {
  537. DEBUG("error getting MTU");
  538. close(sock);
  539. goto fail1;
  540. }
  541. o->dev_mtu = ifr.ifr_mtu;
  542. o->frame_mtu = o->dev_mtu + BTAP_ETHERNET_HEADER_LENGTH;
  543. close(sock);
  544. // set non-blocking
  545. if (fcntl(o->fd, F_SETFL, O_NONBLOCK) < 0) {
  546. DEBUG("cannot set non-blocking");
  547. goto fail1;
  548. }
  549. // init file descriptor object
  550. BFileDescriptor_Init(&o->bfd, o->fd, (BFileDescriptor_handler)fd_handler, o);
  551. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  552. DEBUG("BReactor_AddFileDescriptor failed");
  553. goto fail1;
  554. }
  555. o->poll_events = 0;
  556. goto success;
  557. fail1:
  558. ASSERT_FORCE(close(o->fd) == 0)
  559. return 0;
  560. #endif
  561. success:
  562. // init dead var
  563. DEAD_INIT(o->dead);
  564. // init input
  565. PacketPassInterface_Init(&o->input, o->frame_mtu, (PacketPassInterface_handler_send)input_handler_send, o, BReactor_PendingGroup(o->reactor));
  566. PacketPassInterface_EnableCancel(&o->input, (PacketPassInterface_handler_cancel)input_handler_cancel);
  567. // init output
  568. PacketRecvInterface_Init(&o->output, o->frame_mtu, (PacketRecvInterface_handler_recv)output_handler_recv, o, BReactor_PendingGroup(o->reactor));
  569. // set no input packet
  570. o->input_packet_len = -1;
  571. // set no output packet
  572. o->output_packet = NULL;
  573. DebugObject_Init(&o->d_obj);
  574. return 1;
  575. }
  576. void BTap_Free (BTap *o)
  577. {
  578. DebugObject_Free(&o->d_obj);
  579. // free output
  580. PacketRecvInterface_Free(&o->output);
  581. // free input
  582. PacketPassInterface_Free(&o->input);
  583. // kill dead variable
  584. DEAD_KILL(o->dead);
  585. #ifdef BADVPN_USE_WINAPI
  586. // wait for pending i/o
  587. ASSERT_FORCE(CancelIo(o->device))
  588. DWORD bytes;
  589. DWORD error;
  590. if (o->input_packet_len >= 0) {
  591. if (!GetOverlappedResult(o->device, &o->input_ol, &bytes, TRUE)) {
  592. error = GetLastError();
  593. if (error != ERROR_OPERATION_ABORTED) {
  594. DEBUG("WARNING: GetOverlappedResult (input) failed (%u)", error);
  595. }
  596. }
  597. }
  598. if (o->output_packet) {
  599. if (!GetOverlappedResult(o->device, &o->output_ol, &bytes, TRUE)) {
  600. error = GetLastError();
  601. if (error != ERROR_OPERATION_ABORTED) {
  602. DEBUG("WARNING: GetOverlappedResult (output) failed (%u)", error);
  603. }
  604. }
  605. }
  606. // free stuff
  607. BReactor_RemoveHandle(o->reactor, &o->input_bhandle);
  608. BReactor_RemoveHandle(o->reactor, &o->output_bhandle);
  609. ASSERT_FORCE(CloseHandle(o->output_event))
  610. ASSERT_FORCE(CloseHandle(o->input_event))
  611. ASSERT_FORCE(CloseHandle(o->device))
  612. #else
  613. // free BFileDescriptor
  614. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  615. // close file descriptor
  616. ASSERT_FORCE(close(o->fd) == 0)
  617. #endif
  618. }
  619. int BTap_GetDeviceMTU (BTap *o)
  620. {
  621. DebugObject_Access(&o->d_obj);
  622. return o->dev_mtu;
  623. }
  624. PacketPassInterface * BTap_GetInput (BTap *o)
  625. {
  626. DebugObject_Access(&o->d_obj);
  627. return &o->input;
  628. }
  629. PacketRecvInterface * BTap_GetOutput (BTap *o)
  630. {
  631. DebugObject_Access(&o->d_obj);
  632. return &o->output;
  633. }