BTap.h 4.0 KB

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