UDPInterface.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef UDPInterface_H
  16. #define UDPInterface_H
  17. #include "exception/Er.h"
  18. #include "interface/addressable/AddrIface.h"
  19. #include "benc/List.h"
  20. #include "util/events/EventBase.h"
  21. #include "net/InterfaceController.h"
  22. #include "util/Gcc.h"
  23. #include "util/Assert.h"
  24. #include "util/log/Log.h"
  25. #include "util/GlobalConfig.h"
  26. #include "memory/Allocator.h"
  27. #include "util/events/UDPAddrIface.h"
  28. #include "util/Linker.h"
  29. Linker_require("interface/UDPInterface.c")
  30. struct UDPInterface_BroadcastHeader
  31. {
  32. /* Magic (0xfffffffc) */
  33. uint32_t fffffffc_be;
  34. /** UDPInterface_CURRENT_VERSION, no communication is possible with different versions. */
  35. uint8_t version;
  36. /** padding and for future use. */
  37. uint8_t zero;
  38. /** Port used for data communication. */
  39. uint16_t commPort_be;
  40. };
  41. #define UDPInterface_BroadcastHeader_SIZE 8
  42. Assert_compileTime(
  43. sizeof(struct UDPInterface_BroadcastHeader) == UDPInterface_BroadcastHeader_SIZE
  44. );
  45. #define UDPInterface_CURRENT_VERSION 0
  46. struct UDPInterface
  47. {
  48. struct AddrIface generic;
  49. };
  50. /**
  51. * Create a new UDPInterface
  52. *
  53. * @param eventBase the event context
  54. * @param bindAddr the address and port to bind the socket to
  55. * @param bcastPort (optional) if specifed, another socket will be created for beacon messages
  56. * if zero then no other socket will be created.
  57. * @param alloc allocator which will be used to create the interface
  58. * @param logger
  59. * @param globalConf for getting the name of the TUN device to avoid bcasting to it
  60. */
  61. Er_DEFUN(struct UDPInterface* UDPInterface_new(struct EventBase* eventBase,
  62. struct Sockaddr* bindAddr,
  63. uint16_t beaconPort,
  64. struct Allocator* alloc,
  65. struct Log* logger,
  66. struct GlobalConfig* globalConf));
  67. /**
  68. * List all devices which can be broadcasted to, this will provide the name of the devices.
  69. *
  70. * @param alloc
  71. */
  72. Er_DEFUN(List* UDPInterface_listDevices(struct Allocator* alloc));
  73. /**
  74. * Specify broadcast devices, this function accepts device names, address names and
  75. * the pseudo-name "all" which means it will use broadcast addresses of all interfaces.
  76. * To broacdast to a different network, you can simply specify the broadcast address
  77. * as if it were a device.
  78. * For example: [ "eth0", "wlan0", "192.168.300.255" ]
  79. *
  80. * @param udpif
  81. * @param devices the list of devices to assign
  82. */
  83. void UDPInterface_setBroadcastDevices(struct UDPInterface* udpif, List* devices);
  84. /**
  85. * Get the list of broadcast devices which is set using UDPInterface_setBroadcastDevices().
  86. * This will return exactly the same list that was set, for the broadcast addresses which
  87. * were computed to send to, use UDPInterface_getBroadcastAddrs().
  88. */
  89. List* UDPInterface_getBroadcastDevices(struct UDPInterface* udpif, struct Allocator* alloc);
  90. /**
  91. * Get the list of broadcast addresses which will be used for beconing, this is computed
  92. * from the list specified by UDPInterface_setBroadcastDevices().
  93. */
  94. List* UDPInterface_getBroadcastAddrs(struct UDPInterface* udpif, struct Allocator* alloc);
  95. /**
  96. * Configure the underlying UDP socket(s) to set DSCP on the traffic they send so that a
  97. * firewall can recognize them and treat them accordingly. This will set DSCP on both the
  98. * data socket and the beacon socket.
  99. */
  100. int UDPInterface_setDSCP(struct UDPInterface* udpif, uint8_t dscp);
  101. int UDPInterface_getFd(struct UDPInterface* udpif);
  102. #endif