BTap.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #include <stdint.h>
  29. #ifdef BADVPN_USE_WINAPI
  30. #else
  31. #include <net/if.h>
  32. #endif
  33. #include <misc/dead.h>
  34. #include <misc/debug.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. dead_t dead;
  49. BReactor *reactor;
  50. BTap_handler_error handler_error;
  51. void *handler_error_user;
  52. int dev_mtu;
  53. int frame_mtu;
  54. PacketPassInterface input;
  55. PacketRecvInterface output;
  56. uint8_t *input_packet;
  57. int input_packet_len;
  58. uint8_t *output_packet;
  59. #ifdef BADVPN_USE_WINAPI
  60. HANDLE device;
  61. HANDLE input_event;
  62. HANDLE output_event;
  63. BHandle input_bhandle;
  64. BHandle output_bhandle;
  65. OVERLAPPED input_ol;
  66. OVERLAPPED output_ol;
  67. #else
  68. int fd;
  69. BFileDescriptor bfd;
  70. char devname[IFNAMSIZ];
  71. int poll_events;
  72. #endif
  73. DebugObject d_obj;
  74. } BTap;
  75. /**
  76. * Initializes the TAP device.
  77. *
  78. * @param o the object
  79. * @param BReactor {@link BReactor} we live in
  80. * @param devname name of the devece to open.
  81. * On Linux: a network interface name. If it is NULL, no
  82. * specific device will be requested, and the operating system
  83. * may create a new device.
  84. * On Windows: a string "component_id:device_name", where
  85. * component_id is a string identifying the driver, and device_name
  86. * is the name of the network interface. If component_id is empty,
  87. * a hardcoded default will be used instead. If device_name is empty,
  88. * the first device found with a matching component_id will be used.
  89. * Specifying a NULL devname is equivalent to specifying ":".
  90. * @param handler_error error handler function
  91. * @param handler_error_user value passed to error handler
  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) 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, excluding
  103. * the Ethernet header.
  104. *
  105. * @param o the object
  106. * @return device's MTU, excluding the Ethernet header
  107. */
  108. int BTap_GetDeviceMTU (BTap *o);
  109. /**
  110. * Returns a {@link PacketPassInterface} for writing packets to the device.
  111. * The MTU of the interface will be {@link BTap_GetDeviceMTU} + BTAP_ETHERNET_HEADER_LENGTH.
  112. *
  113. * @param o the object
  114. * @return input interface
  115. */
  116. PacketPassInterface * BTap_GetInput (BTap *o);
  117. /**
  118. * Returns a {@link PacketRecvInterface} for reading packets from the device.
  119. * The MTU of the interface will be {@link BTap_GetDeviceMTU} + BTAP_ETHERNET_HEADER_LENGTH.
  120. * The interface will support cancel functionality.
  121. *
  122. * @param o the object
  123. * @return output interface
  124. */
  125. PacketRecvInterface * BTap_GetOutput (BTap *o);
  126. #endif