BTap.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <base/DebugObject.h>
  39. #include <system/BReactor.h>
  40. #include <flow/PacketRecvInterface.h>
  41. #define BTAP_ETHERNET_HEADER_LENGTH 14
  42. /**
  43. * Handler called when an error occurs on the device.
  44. * The object must be destroyed from the job context of this
  45. * handler, and no further I/O may occur.
  46. *
  47. * @param user as in {@link BTap_Init}
  48. */
  49. typedef void (*BTap_handler_error) (void *used);
  50. typedef struct {
  51. BReactor *reactor;
  52. BTap_handler_error handler_error;
  53. void *handler_error_user;
  54. int frame_mtu;
  55. PacketRecvInterface output;
  56. uint8_t *output_packet;
  57. #ifdef BADVPN_USE_WINAPI
  58. HANDLE device;
  59. BReactorIOCPOverlapped send_olap;
  60. BReactorIOCPOverlapped recv_olap;
  61. #else
  62. int fd;
  63. BFileDescriptor bfd;
  64. int poll_events;
  65. #endif
  66. DebugError d_err;
  67. DebugObject d_obj;
  68. } BTap;
  69. /**
  70. * Initializes the TAP device.
  71. *
  72. * @param o the object
  73. * @param BReactor {@link BReactor} we live in
  74. * @param devname name of the devece to open.
  75. * On Linux: a network interface name. If it is NULL, no
  76. * specific device will be requested, and the operating system
  77. * may create a new device.
  78. * On Windows: a string "component_id:device_name", where
  79. * component_id is a string identifying the driver, and device_name
  80. * is the name of the network interface. If component_id is empty,
  81. * a hardcoded default will be used instead. If device_name is empty,
  82. * the first device found with a matching component_id will be used.
  83. * Specifying a NULL devname is equivalent to specifying ":".
  84. * @param handler_error error handler function
  85. * @param handler_error_user value passed to error handler
  86. * @param tun whether to create a TUN (IP) device or a TAP (Ethernet) device. Must be 0 or 1.
  87. * @return 1 on success, 0 on failure
  88. */
  89. int BTap_Init (BTap *o, BReactor *bsys, char *devname, BTap_handler_error handler_error, void *handler_error_user, int tun) WARN_UNUSED;
  90. /**
  91. * Frees the TAP device.
  92. *
  93. * @param o the object
  94. */
  95. void BTap_Free (BTap *o);
  96. /**
  97. * Returns the device's maximum transmission unit (including any protocol headers).
  98. *
  99. * @param o the object
  100. * @return device's MTU
  101. */
  102. int BTap_GetMTU (BTap *o);
  103. /**
  104. * Sends a packet to the device.
  105. * Any errors will be reported via a job.
  106. *
  107. * @param o the object
  108. * @param data packet to send
  109. * @param data_len length of packet. Must be >=0 and <=MTU, as reported by {@link BTap_GetMTU}.
  110. */
  111. void BTap_Send (BTap *o, uint8_t *data, int data_len);
  112. /**
  113. * Returns a {@link PacketRecvInterface} for reading packets from the device.
  114. * The MTU of the interface will be {@link BTap_GetMTU}.
  115. *
  116. * @param o the object
  117. * @return output interface
  118. */
  119. PacketRecvInterface * BTap_GetOutput (BTap *o);
  120. #endif