BDatagram.h 7.1 KB

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