BTap.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file BTap.h
  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. * @section DESCRIPTION
  23. *
  24. * TAP device abstraction.
  25. */
  26. #ifndef BADVPN_TUNTAP_BTAP_H
  27. #define BADVPN_TUNTAP_BTAP_H
  28. #if (defined(BADVPN_USE_WINAPI) + defined(BADVPN_LINUX) + defined(BADVPN_FREEBSD)) != 1
  29. #error Unknown TAP backend or too many TAP backends
  30. #endif
  31. #include <stdint.h>
  32. #ifdef BADVPN_USE_WINAPI
  33. #else
  34. #include <net/if.h>
  35. #endif
  36. #include <misc/debug.h>
  37. #include <misc/debugerror.h>
  38. #include <system/DebugObject.h>
  39. #include <system/BReactor.h>
  40. #include <flow/PacketRecvInterface.h>
  41. #include <flow/PacketPassInterface.h>
  42. #define BTAP_ETHERNET_HEADER_LENGTH 14
  43. typedef void (*BTap_handler_error) (void *used);
  44. /**
  45. * TAP device abstraction.
  46. *
  47. * Frames are written to the device using {@link PacketPassInterface}
  48. * and read from the device using {@link PacketRecvInterface}.
  49. */
  50. typedef struct {
  51. BReactor *reactor;
  52. BTap_handler_error handler_error;
  53. void *handler_error_user;
  54. int frame_mtu;
  55. PacketPassInterface input;
  56. PacketRecvInterface output;
  57. uint8_t *input_packet;
  58. int input_packet_len;
  59. uint8_t *output_packet;
  60. #ifdef BADVPN_USE_WINAPI
  61. HANDLE device;
  62. HANDLE input_event;
  63. HANDLE output_event;
  64. BHandle input_bhandle;
  65. BHandle output_bhandle;
  66. OVERLAPPED input_ol;
  67. OVERLAPPED output_ol;
  68. #else
  69. int fd;
  70. BFileDescriptor bfd;
  71. char devname[IFNAMSIZ];
  72. int poll_events;
  73. #endif
  74. DebugObject d_obj;
  75. DebugError d_err;
  76. } BTap;
  77. /**
  78. * Initializes the TAP device.
  79. *
  80. * @param o the object
  81. * @param BReactor {@link BReactor} we live in
  82. * @param devname name of the devece to open.
  83. * On Linux: a network interface name. If it is NULL, no
  84. * specific device will be requested, and the operating system
  85. * may create a new device.
  86. * On Windows: a string "component_id:device_name", where
  87. * component_id is a string identifying the driver, and device_name
  88. * is the name of the network interface. If component_id is empty,
  89. * a hardcoded default will be used instead. If device_name is empty,
  90. * the first device found with a matching component_id will be used.
  91. * Specifying a NULL devname is equivalent to specifying ":".
  92. * @param handler_error error handler function
  93. * @param handler_error_user value passed to error handler
  94. * @param tun whether to create a TUN (IP) device or a TAP (Ethernet) device. Must be 0 or 1.
  95. * @return 1 on success, 0 on failure
  96. */
  97. int BTap_Init (BTap *o, BReactor *bsys, char *devname, BTap_handler_error handler_error, void *handler_error_user, int tun) WARN_UNUSED;
  98. /**
  99. * Frees the TAP device.
  100. *
  101. * @param o the object
  102. */
  103. void BTap_Free (BTap *o);
  104. /**
  105. * Returns the device's maximum transmission unit (including any protocol headers).
  106. *
  107. * @param o the object
  108. * @return device's MTU
  109. */
  110. int BTap_GetMTU (BTap *o);
  111. /**
  112. * Returns a {@link PacketPassInterface} for writing packets to the device.
  113. * The MTU of the interface will be {@link BTap_GetMTU}.
  114. *
  115. * @param o the object
  116. * @return input interface
  117. */
  118. PacketPassInterface * BTap_GetInput (BTap *o);
  119. /**
  120. * Returns a {@link PacketRecvInterface} for reading packets from the device.
  121. * The MTU of the interface will be {@link BTap_GetMTU}.
  122. *
  123. * @param o the object
  124. * @return output interface
  125. */
  126. PacketRecvInterface * BTap_GetOutput (BTap *o);
  127. #endif