BDatagram.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @file BDatagram.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. #ifndef BADVPN_SYSTEM_BDATAGRAM
  30. #define BADVPN_SYSTEM_BDATAGRAM
  31. #include <misc/debug.h>
  32. #include <flow/PacketPassInterface.h>
  33. #include <flow/PacketRecvInterface.h>
  34. #include <system/BAddr.h>
  35. #include <system/BReactor.h>
  36. #include <system/BNetwork.h>
  37. struct BDatagram_s;
  38. /**
  39. * Represents datagram communication. This is usually UDP, but may also be Linux packet sockets.
  40. * Sending and receiving is performed via {@link PacketPassInterface} and {@link PacketRecvInterface},
  41. * respectively.
  42. */
  43. typedef struct BDatagram_s BDatagram;
  44. #define BDATAGRAM_EVENT_ERROR 1
  45. /**
  46. * Handler called when an error occurs with the datagram object.
  47. * The datagram object is no longer usable and must be freed from withing the job closure of
  48. * this handler. No further I/O, interface initialization, binding and send address setting
  49. * must occur.
  50. *
  51. * @param user as in {@link BDatagram_Init}
  52. * @param event always BDATAGRAM_EVENT_ERROR
  53. */
  54. typedef void (*BDatagram_handler) (void *user, int event);
  55. /**
  56. * Checks if the given address family (from {@link BAddr.h}) is supported by {@link BDatagram}
  57. * and related objects.
  58. *
  59. * @param family family to check
  60. * @return 1 if supported, 0 if not
  61. */
  62. int BDatagram_AddressFamilySupported (int family);
  63. /**
  64. * Initializes the object.
  65. * {@link BNetwork_GlobalInit} must have been done.
  66. *
  67. * @param o the object
  68. * @param family address family. Must be supported according to {@link BDatagram_AddressFamilySupported}.
  69. * @param reactor reactor we live in
  70. * @param user argument to handler
  71. * @param handler handler called when an error occurs
  72. * @return 1 on success, 0 on failure
  73. */
  74. int BDatagram_Init (BDatagram *o, int family, BReactor *reactor, void *user,
  75. BDatagram_handler handler) WARN_UNUSED;
  76. /**
  77. * Frees the object.
  78. * The send and receive interfaces must not be initialized.
  79. *
  80. * @param o the object
  81. */
  82. void BDatagram_Free (BDatagram *o);
  83. /**
  84. * Binds to the given local address.
  85. * May initiate I/O.
  86. *
  87. * @param o the object
  88. * @param addr address to bind to. Its family must be supported according to {@link BDatagram_AddressFamilySupported}.
  89. * @return 1 on success, 0 on failure
  90. */
  91. int BDatagram_Bind (BDatagram *o, BAddr addr) WARN_UNUSED;
  92. /**
  93. * Sets addresses for sending.
  94. * May initiate I/O.
  95. *
  96. * @param o the object
  97. * @param remote_addr destination address for sending datagrams. Its family must be supported according
  98. * to {@link BDatagram_AddressFamilySupported}.
  99. * @param local_addr local source IP address. May be an invalid address, otherwise its family must be
  100. * supported according to {@link BDatagram_AddressFamilySupported}.
  101. */
  102. void BDatagram_SetSendAddrs (BDatagram *o, BAddr remote_addr, BIPAddr local_addr);
  103. /**
  104. * Returns the remote and local address of the last datagram received.
  105. * Fails if and only if no datagrams have been received yet.
  106. *
  107. * @param o the object
  108. * @param remote_addr returns the remote source address of the datagram. May be an invalid address, theoretically.
  109. * @param local_addr returns the local destination IP address. May be an invalid address.
  110. * @return 1 on success, 0 on failure
  111. */
  112. int BDatagram_GetLastReceiveAddrs (BDatagram *o, BAddr *remote_addr, BIPAddr *local_addr);
  113. #ifndef BADVPN_USE_WINAPI
  114. /**
  115. * Returns the underlying socket file descriptor of the datagram object.
  116. * Available on Unix-like systems only.
  117. *
  118. * @param o the object
  119. * @return file descriptor
  120. */
  121. int BDatagram_GetFd (BDatagram *o);
  122. #endif
  123. /**
  124. * Sets the SO_REUSEADDR option for the underlying socket.
  125. *
  126. * @param o the object
  127. * @param reuse value of the option. Must be 0 or 1.
  128. */
  129. int BDatagram_SetReuseAddr (BDatagram *o, int reuse);
  130. /**
  131. * Initializes the send interface.
  132. * The send interface must not be initialized.
  133. *
  134. * @param o the object
  135. * @param mtu maximum transmission unit. Must be >=0.
  136. */
  137. void BDatagram_SendAsync_Init (BDatagram *o, int mtu);
  138. /**
  139. * Frees the send interface.
  140. * The send interface must be initialized.
  141. * If the send interface was busy when this is called, the datagram object is no longer usable and must be
  142. * freed before any further I/O or interface initialization.
  143. *
  144. * @param o the object
  145. */
  146. void BDatagram_SendAsync_Free (BDatagram *o);
  147. /**
  148. * Returns the send interface.
  149. * The send interface must be initialized.
  150. * The MTU of the interface will be as in {@link BDatagram_SendAsync_Init}.
  151. *
  152. * @param o the object
  153. * @return send interface
  154. */
  155. PacketPassInterface * BDatagram_SendAsync_GetIf (BDatagram *o);
  156. /**
  157. * Initializes the receive interface.
  158. * The receive interface must not be initialized.
  159. *
  160. * @param o the object
  161. * @param mtu maximum transmission unit. Must be >=0.
  162. */
  163. void BDatagram_RecvAsync_Init (BDatagram *o, int mtu);
  164. /**
  165. * Frees the receive interface.
  166. * The receive interface must be initialized.
  167. * If the receive interface was busy when this is called, the datagram object is no longer usable and must be
  168. * freed before any further I/O or interface initialization.
  169. *
  170. * @param o the object
  171. */
  172. void BDatagram_RecvAsync_Free (BDatagram *o);
  173. /**
  174. * Returns the receive interface.
  175. * The receive interface must be initialized.
  176. * The MTU of the interface will be as in {@link BDatagram_RecvAsync_Init}.
  177. *
  178. * @param o the object
  179. * @return receive interface
  180. */
  181. PacketRecvInterface * BDatagram_RecvAsync_GetIf (BDatagram *o);
  182. #ifdef BADVPN_USE_WINAPI
  183. #include "BDatagram_win.h"
  184. #else
  185. #include "BDatagram_unix.h"
  186. #endif
  187. #endif