BDatagram.h 5.9 KB

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