BTap.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file BTap.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * TAP device abstraction.
  32. */
  33. #ifndef BADVPN_TUNTAP_BTAP_H
  34. #define BADVPN_TUNTAP_BTAP_H
  35. #if (defined(BADVPN_USE_WINAPI) + defined(BADVPN_LINUX) + defined(BADVPN_FREEBSD)) != 1
  36. #error Unknown TAP backend or too many TAP backends
  37. #endif
  38. #include <stdint.h>
  39. #ifdef BADVPN_USE_WINAPI
  40. #else
  41. #include <net/if.h>
  42. #endif
  43. #include <misc/debug.h>
  44. #include <misc/debugerror.h>
  45. #include <base/DebugObject.h>
  46. #include <system/BReactor.h>
  47. #include <flow/PacketRecvInterface.h>
  48. #define BTAP_ETHERNET_HEADER_LENGTH 14
  49. /**
  50. * Handler called when an error occurs on the device.
  51. * The object must be destroyed from the job context of this
  52. * handler, and no further I/O may occur.
  53. *
  54. * @param user as in {@link BTap_Init}
  55. */
  56. typedef void (*BTap_handler_error) (void *used);
  57. typedef struct {
  58. BReactor *reactor;
  59. BTap_handler_error handler_error;
  60. void *handler_error_user;
  61. int frame_mtu;
  62. PacketRecvInterface output;
  63. uint8_t *output_packet;
  64. #ifdef BADVPN_USE_WINAPI
  65. HANDLE device;
  66. BReactorIOCPOverlapped send_olap;
  67. BReactorIOCPOverlapped recv_olap;
  68. #else
  69. int fd;
  70. BFileDescriptor bfd;
  71. int poll_events;
  72. #endif
  73. DebugError d_err;
  74. DebugObject d_obj;
  75. } BTap;
  76. /**
  77. * Initializes the TAP device.
  78. *
  79. * @param o the object
  80. * @param BReactor {@link BReactor} we live in
  81. * @param devname name of the devece to open.
  82. * On Linux: a network interface name. If it is NULL, no
  83. * specific device will be requested, and the operating system
  84. * may create a new device.
  85. * On Windows: a string "component_id:device_name", where
  86. * component_id is a string identifying the driver, and device_name
  87. * is the name of the network interface. If component_id is empty,
  88. * a hardcoded default will be used instead. If device_name is empty,
  89. * the first device found with a matching component_id will be used.
  90. * Specifying a NULL devname is equivalent to specifying ":".
  91. * @param handler_error error handler function
  92. * @param handler_error_user value passed to error handler
  93. * @param tun whether to create a TUN (IP) device or a TAP (Ethernet) device. Must be 0 or 1.
  94. * @return 1 on success, 0 on failure
  95. */
  96. int BTap_Init (BTap *o, BReactor *bsys, char *devname, BTap_handler_error handler_error, void *handler_error_user, int tun) WARN_UNUSED;
  97. /**
  98. * Frees the TAP device.
  99. *
  100. * @param o the object
  101. */
  102. void BTap_Free (BTap *o);
  103. /**
  104. * Returns the device's maximum transmission unit (including any protocol headers).
  105. *
  106. * @param o the object
  107. * @return device's MTU
  108. */
  109. int BTap_GetMTU (BTap *o);
  110. /**
  111. * Sends a packet to the device.
  112. * Any errors will be reported via a job.
  113. *
  114. * @param o the object
  115. * @param data packet to send
  116. * @param data_len length of packet. Must be >=0 and <=MTU, as reported by {@link BTap_GetMTU}.
  117. */
  118. void BTap_Send (BTap *o, uint8_t *data, int data_len);
  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