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