UDPInterface.h 4.2 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 "interface/addressable/AddrIface.h"
  18. #include "benc/List.h"
  19. #include "util/events/EventBase.h"
  20. #include "net/InterfaceController.h"
  21. #include "util/Gcc.h"
  22. #include "util/Assert.h"
  23. #include "util/log/Log.h"
  24. #include "util/GlobalConfig.h"
  25. #include "memory/Allocator.h"
  26. #include "util/events/UDPAddrIface.h"
  27. #include "util/Linker.h"
  28. Linker_require("interface/UDPInterface.c");
  29. struct UDPInterface_BroadcastHeader
  30. {
  31. /* Magic (0xfffffffc) */
  32. uint32_t fffffffc_be;
  33. /** UDPInterface_CURRENT_VERSION, no communication is possible with different versions. */
  34. uint8_t version;
  35. /** padding and for future use. */
  36. uint8_t zero;
  37. /** Port used for data communication. */
  38. uint16_t commPort_be;
  39. };
  40. #define UDPInterface_BroadcastHeader_SIZE 8
  41. Assert_compileTime(
  42. sizeof(struct UDPInterface_BroadcastHeader) == UDPInterface_BroadcastHeader_SIZE
  43. );
  44. #define UDPInterface_CURRENT_VERSION 0
  45. struct UDPInterface
  46. {
  47. struct AddrIface generic;
  48. };
  49. /**
  50. * Create a new UDPInterface
  51. *
  52. * @param eventBase the event context
  53. * @param bindAddr the address and port to bind the socket to
  54. * @param bcastPort (optional) if specifed, another socket will be created for beacon messages
  55. * if zero then no other socket will be created.
  56. * @param alloc allocator which will be used to create the interface
  57. * @param exHandler in case setup fails
  58. * @param logger
  59. * @param globalConf for getting the name of the TUN device to avoid bcasting to it
  60. */
  61. struct UDPInterface* UDPInterface_new(struct EventBase* eventBase,
  62. struct Sockaddr* bindAddr,
  63. uint16_t bcastPort,
  64. struct Allocator* alloc,
  65. struct Except* exHandler,
  66. struct Log* logger,
  67. struct GlobalConfig* globalConf);
  68. /**
  69. * List all devices which can be broadcasted to, this will provide the name of the devices.
  70. *
  71. * @param alloc
  72. * @param eh exception in case libuv is unable to get the device list
  73. */
  74. List* UDPInterface_listDevices(struct Allocator* alloc, struct Except* eh);
  75. /**
  76. * Specify broadcast devices, this function accepts device names, address names and
  77. * the pseudo-name "all" which means it will use broadcast addresses of all interfaces.
  78. * To broacdast to a different network, you can simply specify the broadcast address
  79. * as if it were a device.
  80. * For example: [ "eth0", "wlan0", "192.168.300.255" ]
  81. *
  82. * @param udpif
  83. * @param devices the list of devices to assign
  84. */
  85. void UDPInterface_setBroadcastDevices(struct UDPInterface* udpif, List* devices);
  86. /**
  87. * Get the list of broadcast devices which is set using UDPInterface_setBroadcastDevices().
  88. * This will return exactly the same list that was set, for the broadcast addresses which
  89. * were computed to send to, use UDPInterface_getBroadcastAddrs().
  90. */
  91. List* UDPInterface_getBroadcastDevices(struct UDPInterface* udpif, struct Allocator* alloc);
  92. /**
  93. * Get the list of broadcast addresses which will be used for beconing, this is computed
  94. * from the list specified by UDPInterface_setBroadcastDevices().
  95. */
  96. List* UDPInterface_getBroadcastAddrs(struct UDPInterface* udpif, struct Allocator* alloc);
  97. /**
  98. * Configure the underlying UDP socket(s) to set DSCP on the traffic they send so that a
  99. * firewall can recognize them and treat them accordingly. This will set DSCP on both the
  100. * data socket and the beacon socket.
  101. */
  102. int UDPInterface_setDSCP(struct UDPInterface* udpif, uint8_t dscp);
  103. #endif