BDatagram.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. /**
  114. * Determines the local address.
  115. *
  116. * This calls getsockname() to determine the local address and returns the result as
  117. * BAddr. This function fails if the address cannot be determined or translated to
  118. * BAddr (it never succeeds but returns a BADDR_TYPE_NONE address).
  119. *
  120. * @param o the object
  121. * @param local_addr returns the local bound address.
  122. * @return 1 on success, 0 on failure
  123. */
  124. int BDatagram_GetLocalAddr (BDatagram *o, BAddr *local_addr);
  125. /**
  126. * Returns the local port.
  127. *
  128. * This is a convenience function implemented based on BDatagram_GetLocalAddr.
  129. *
  130. * @param o the object
  131. * @param local_port returns the local bound port.
  132. * @return 1 on success, 0 on failure
  133. */
  134. int BDatagram_GetLocalPort (BDatagram *o, uint16_t *local_port);
  135. #ifndef BADVPN_USE_WINAPI
  136. /**
  137. * Returns the underlying socket file descriptor of the datagram object.
  138. * Available on Unix-like systems only.
  139. *
  140. * @param o the object
  141. * @return file descriptor
  142. */
  143. int BDatagram_GetFd (BDatagram *o);
  144. #endif
  145. /**
  146. * Sets the SO_REUSEADDR option for the underlying socket.
  147. *
  148. * @param o the object
  149. * @param reuse value of the option. Must be 0 or 1.
  150. */
  151. int BDatagram_SetReuseAddr (BDatagram *o, int reuse);
  152. /**
  153. * Initializes the send interface.
  154. * The send interface must not be initialized.
  155. *
  156. * @param o the object
  157. * @param mtu maximum transmission unit. Must be >=0.
  158. */
  159. void BDatagram_SendAsync_Init (BDatagram *o, int mtu);
  160. /**
  161. * Frees the send interface.
  162. * The send interface must be initialized.
  163. * If the send interface was busy when this is called, the datagram object is no longer usable and must be
  164. * freed before any further I/O or interface initialization.
  165. *
  166. * @param o the object
  167. */
  168. void BDatagram_SendAsync_Free (BDatagram *o);
  169. /**
  170. * Returns the send interface.
  171. * The send interface must be initialized.
  172. * The MTU of the interface will be as in {@link BDatagram_SendAsync_Init}.
  173. *
  174. * @param o the object
  175. * @return send interface
  176. */
  177. PacketPassInterface * BDatagram_SendAsync_GetIf (BDatagram *o);
  178. /**
  179. * Initializes the receive interface.
  180. * The receive interface must not be initialized.
  181. *
  182. * @param o the object
  183. * @param mtu maximum transmission unit. Must be >=0.
  184. */
  185. void BDatagram_RecvAsync_Init (BDatagram *o, int mtu);
  186. /**
  187. * Frees the receive interface.
  188. * The receive interface must be initialized.
  189. * If the receive interface was busy when this is called, the datagram object is no longer usable and must be
  190. * freed before any further I/O or interface initialization.
  191. *
  192. * @param o the object
  193. */
  194. void BDatagram_RecvAsync_Free (BDatagram *o);
  195. /**
  196. * Returns the receive interface.
  197. * The receive interface must be initialized.
  198. * The MTU of the interface will be as in {@link BDatagram_RecvAsync_Init}.
  199. *
  200. * @param o the object
  201. * @return receive interface
  202. */
  203. PacketRecvInterface * BDatagram_RecvAsync_GetIf (BDatagram *o);
  204. #ifdef BADVPN_USE_WINAPI
  205. #include "BDatagram_win.h"
  206. #else
  207. #include "BDatagram_unix.h"
  208. #endif
  209. #endif