-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLowLatSocket.h
More file actions
222 lines (193 loc) · 6.09 KB
/
LowLatSocket.h
File metadata and controls
222 lines (193 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// This file is part of the Open Audio Live System project, a live audio environment
// Copyright (c) 2026 - Mathis DELGADO
//
// This project is distributed under the Creative Commons CC-BY-NC-SA licence. https://creativecommons.org/licenses/by-nc-sa/4.0
#ifndef LOWLATSOCKET_H
#define LOWLATSOCKET_H
#ifdef __linux__
#include <iostream>
#endif //__linux__
#include <string>
#include <cstring>
#include <cassert>
#include <optional>
#include <cstdint>
#include <memory>
#ifdef __linux__
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <netinet/in.h>
#include <net/if.h>
#include <unistd.h>
#include <sys/ioctl.h>
#else
// Defining linux-like structs for compat
struct EthernetHeader {
uint8_t h_dest[6];
uint8_t h_source[6];
uint16_t h_proto;
} __attribute__((packed));
typedef EthernetHeader ethhdr;
#define htons(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
#endif //__linux__
/**
* @enum EthProtocol
* @brief Enum containing different values for the Ethernet protocol field, corresponding to different OAN stream types.
*/
enum EthProtocol : uint16_t {
ETH_PROTO_OANAUDIO = 0x0681,
ETH_PROTO_OANDISCO = 0x0682,
ETH_PROTO_OANCONTROL = 0x0683,
ETH_PROTO_OANSYNC = 0x0684
};
/**
* @struct LowLatHeader
* @brief This header contains the layer 2 header of the protocol
*/
struct LowLatHeader {
uint16_t sender_uid; /**< Sender device UID */
uint16_t dest_uid; /**< Receiver device UID */
uint16_t psize; /**< Packet size */
} __attribute__((packed));
/**
* @struct LowLatPacket
* @brief Layer 2 packet containing ethernet and addressing headers plus encapsulated data
* @tparam T Type of the encapsulated data
*/
template<class T>
struct LowLatPacket {
ethhdr eth_header; /**< Ethernet header, linux kernel structure */
LowLatHeader llhdr; /**< LowLatHeader header, custom addressing scheme */
T payload; /**< Encapsulated data */
} __attribute__((packed));
/**
* @struct INT_LLP
* @brief Custom-sized LowLatPacket with no raw data for encapsulated data. For internal use only
* @tparam payload_size__ Custom size
*/
template<int payload_size__>
struct INT_LLP : public LowLatPacket<char[payload_size__]> {};
/**
* @struct IfaceMeta
* @brief This struct stores physical network interface info.
*/
struct IfaceMeta {
char mac[6]; /**< MAC Address */
int idx; /**< Interface ID (kernel parameter) */
};
/**
* Function to retreive a given network interface infos.
* @param name Interface name
* @return Interface infos if found. All zeros if not found.
*/
IfaceMeta get_iface_meta(const std::string& name);
class NetworkMapper;
/**
* @class LowLatSocket
* @brief Wrapper for raw sockets that implements a layer 2 addressing protocol.
*/
class LowLatSocket {
public:
/**
* Constructor
* @param self_uid Host UID
* @param mapper Local OAN network mapper
*/
LowLatSocket(uint16_t self_uid, std::shared_ptr<NetworkMapper> mapper);
~LowLatSocket();
/**
* Initializes the socket
* @param interface Physical network interface name to attach the socket to
* @param proto Ethernet protocol used. @see EthProtocol
* @return true if initialization succeeds
*/
bool init_socket(std::string interface, EthProtocol proto);
/**
* Sends some data on the network
* @tparam T Sent data type
* @param data Pointer to the data
* @param dest_uid Package receiver UID
* @return Sent byte count. Less than 0 if error.
*/
template<class T>
int send_data(const T& data, uint16_t dest_uid) {
INT_LLP<sizeof(T)> llpck;
llpck.eth_header = m_hdr;
llpck.llhdr.dest_uid = dest_uid;
llpck.llhdr.sender_uid = m_self_uid;
llpck.llhdr.psize = sizeof(T);
memcpy(llpck.payload, &data, sizeof(T));
if (dest_uid != 0) {
auto mac = get_mac(dest_uid);
if (mac.has_value()) {
uint64_t& v = mac.value();
memcpy(llpck.eth_header.h_dest, &v, 6);
} else {
//std::cerr << "Trying to send data to unknown UID (" << (int)dest_uid << ")." << std::endl;
return 0;
}
}
#ifdef __linux__
return sendto(
m_socket,
&llpck, sizeof(llpck),
MSG_DONTWAIT,
(sockaddr*)&m_iface_addr,
sizeof(m_iface_addr)
);
#else
return send_data_internal((uint8_t*)&llpck, sizeof(INT_LLP<sizeof(T)>));
#endif // __linux__
}
/**
* Receive some data
* @tparam T Data type received
* @param data Pointer to the data buffer
* @param async This flag set the call as non-blocking if set to true
* @return Received byte count
*/
template<class T>
int receive_data(T* data, bool async = true) {
#ifdef __linux__
return recv(m_socket, data, sizeof(T), async ? MSG_DONTWAIT : 0);
#else
return recv_data_internal((uint8_t*)data, sizeof(T));
#endif // __linux__
}
/**
* Receive raw data. @see LowLatSocket::receive_data
* @param data Pointer to the data buffer
* @param size Amount of data expected
* @param async This flag set the call as non-blocking if set to true
* @return Received byte count
*/
int receive_data_raw(char* data, size_t size, bool async = true) const {
#ifdef __linux__
return recv(m_socket, data, size, async ? MSG_DONTWAIT : 0);
#else
return recv_data_internal((uint8_t*)data, size);
#endif // __linux__
}
private:
/**
* Finds a device MAC address based on its ID.
* @param id ID to search for
* @return If found, the corresponding MAC address
*/
std::optional<uint64_t> get_mac(uint16_t id);
#ifndef __linux__
int send_data_internal(uint8_t* data, size_t size);
int recv_data_internal(uint8_t* data, size_t size) const;
#endif // __linux__
#ifdef __linux__
sockaddr_ll m_iface_addr{};
#else
uint8_t m_iface_addr[6];
#endif // __linux__
ethhdr m_hdr{};
int m_socket;
uint16_t m_self_uid;
EthProtocol m_self_proto;
std::shared_ptr<NetworkMapper> m_mapper;
};
#endif //LOWLATSOCKET_H