| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <array> |
| 7 |
#include <initializer_list> |
| 8 |
#include <optional> |
| 9 |
#include <span> |
| 10 |
#include <string> |
| 11 |
#include <string_view> |
| 12 |
#include <type_traits> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#if defined(__linux__) || defined(__HAIKU__) |
| 16 |
#include <sys/socket.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include "Common/CommonTypes.h" |
| 20 |
|
| 21 |
struct sockaddr_in; |
| 22 |
|
| 23 |
namespace Common |
| 24 |
{ |
| 25 |
enum class MACConsumer |
| 26 |
{ |
| 27 |
BBA, |
| 28 |
IOS |
| 29 |
}; |
| 30 |
|
| 31 |
enum |
| 32 |
{ |
| 33 |
BBA_HARDWARE_TYPE = 1, |
| 34 |
MAC_ADDRESS_SIZE = 6, |
| 35 |
IPV4_HEADER_TYPE = 8 |
| 36 |
}; |
| 37 |
|
| 38 |
enum DHCPConst |
| 39 |
{ |
| 40 |
MESSAGE_QUERY = 1, |
| 41 |
MESSAGE_REPLY = 2 |
| 42 |
}; |
| 43 |
|
| 44 |
using MACAddress = std::array<u8, MAC_ADDRESS_SIZE>; |
| 45 |
|
| 46 |
|
| 47 |
struct BluetoothAddress : std::array<u8, MAC_ADDRESS_SIZE> |
| 48 |
{ |
| 49 |
}; |
| 50 |
|
| 51 |
constexpr std::size_t IPV4_ADDR_LEN = 4; |
| 52 |
using IPAddress = std::array<u8, IPV4_ADDR_LEN>; |
| 53 |
constexpr IPAddress IP_ADDR_ANY = {0, 0, 0, 0}; |
| 54 |
constexpr IPAddress IP_ADDR_BROADCAST = {255, 255, 255, 255}; |
| 55 |
constexpr IPAddress IP_ADDR_SSDP = {239, 255, 255, 250}; |
| 56 |
constexpr u16 SSDP_PORT = 1900; |
| 57 |
constexpr u16 IPV4_ETHERTYPE = 0x800; |
| 58 |
constexpr u16 ARP_ETHERTYPE = 0x806; |
| 59 |
|
| 60 |
struct EthernetHeader |
| 61 |
{ |
| 62 |
EthernetHeader(); |
| 63 |
explicit EthernetHeader(u16 ether_type); |
| 64 |
EthernetHeader(const MACAddress& dest, const MACAddress& src, u16 ether_type); |
| 65 |
u16 Size() const; |
| 66 |
|
| 67 |
static constexpr std::size_t SIZE = 14; |
| 68 |
|
| 69 |
MACAddress destination = {}; |
| 70 |
MACAddress source = {}; |
| 71 |
u16 ethertype = 0; |
| 72 |
}; |
| 73 |
static_assert(sizeof(EthernetHeader) == EthernetHeader::SIZE); |
| 74 |
static_assert(std::is_standard_layout_v<EthernetHeader>); |
| 75 |
|
| 76 |
struct IPv4Header |
| 77 |
{ |
| 78 |
IPv4Header(); |
| 79 |
IPv4Header(u16 data_size, u8 ip_proto, const sockaddr_in& from, const sockaddr_in& to); |
| 80 |
u16 Size() const; |
| 81 |
u8 DefinedSize() const; |
| 82 |
|
| 83 |
static constexpr std::size_t SIZE = 20; |
| 84 |
|
| 85 |
u8 version_ihl = 0; |
| 86 |
u8 dscp_esn = 0; |
| 87 |
u16 total_len = 0; |
| 88 |
u16 identification = 0; |
| 89 |
u16 flags_fragment_offset = 0; |
| 90 |
u8 ttl = 0; |
| 91 |
u8 protocol = 0; |
| 92 |
u16 header_checksum = 0; |
| 93 |
IPAddress source_addr{}; |
| 94 |
IPAddress destination_addr{}; |
| 95 |
}; |
| 96 |
static_assert(sizeof(IPv4Header) == IPv4Header::SIZE); |
| 97 |
static_assert(std::is_standard_layout_v<IPv4Header>); |
| 98 |
|
| 99 |
struct TCPHeader |
| 100 |
{ |
| 101 |
TCPHeader(); |
| 102 |
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, const u8* data, u16 length); |
| 103 |
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, u32 ack, u16 flags); |
| 104 |
u8 GetHeaderSize() const; |
| 105 |
u16 Size() const; |
| 106 |
u8 IPProto() const; |
| 107 |
|
| 108 |
static constexpr std::size_t SIZE = 20; |
| 109 |
|
| 110 |
u16 source_port = 0; |
| 111 |
u16 destination_port = 0; |
| 112 |
u32 sequence_number = 0; |
| 113 |
u32 acknowledgement_number = 0; |
| 114 |
u16 properties = 0; |
| 115 |
u16 window_size = 0; |
| 116 |
u16 checksum = 0; |
| 117 |
u16 urgent_pointer = 0; |
| 118 |
}; |
| 119 |
static_assert(sizeof(TCPHeader) == TCPHeader::SIZE); |
| 120 |
static_assert(std::is_standard_layout_v<TCPHeader>); |
| 121 |
|
| 122 |
struct UDPHeader |
| 123 |
{ |
| 124 |
UDPHeader(); |
| 125 |
UDPHeader(const sockaddr_in& from, const sockaddr_in& to, u16 data_length); |
| 126 |
u16 Size() const; |
| 127 |
u8 IPProto() const; |
| 128 |
|
| 129 |
static constexpr std::size_t SIZE = 8; |
| 130 |
|
| 131 |
u16 source_port = 0; |
| 132 |
u16 destination_port = 0; |
| 133 |
u16 length = 0; |
| 134 |
u16 checksum = 0; |
| 135 |
}; |
| 136 |
static_assert(sizeof(UDPHeader) == UDPHeader::SIZE); |
| 137 |
static_assert(std::is_standard_layout_v<UDPHeader>); |
| 138 |
|
| 139 |
#pragma pack(push, 1) |
| 140 |
struct ARPHeader |
| 141 |
{ |
| 142 |
ARPHeader(); |
| 143 |
ARPHeader(u32 from_ip, const MACAddress& from_mac, u32 to_ip, const MACAddress& to_mac); |
| 144 |
u16 Size() const; |
| 145 |
|
| 146 |
static constexpr std::size_t SIZE = 28; |
| 147 |
|
| 148 |
u16 hardware_type = 0; |
| 149 |
u16 protocol_type = 0; |
| 150 |
u8 hardware_size = 0; |
| 151 |
u8 protocol_size = 0; |
| 152 |
u16 opcode = 0; |
| 153 |
MACAddress sender_address{}; |
| 154 |
u32 sender_ip = 0; |
| 155 |
MACAddress targer_address{}; |
| 156 |
u32 target_ip = 0; |
| 157 |
}; |
| 158 |
static_assert(sizeof(ARPHeader) == ARPHeader::SIZE); |
| 159 |
#pragma pack(pop) |
| 160 |
|
| 161 |
struct DHCPBody |
| 162 |
{ |
| 163 |
DHCPBody(); |
| 164 |
DHCPBody(u32 transaction, const MACAddress& client_address, u32 new_ip, u32 serv_ip); |
| 165 |
static constexpr std::size_t SIZE = 240; |
| 166 |
u8 message_type = 0; |
| 167 |
u8 hardware_type = 0; |
| 168 |
u8 hardware_addr = 0; |
| 169 |
u8 hops = 0; |
| 170 |
u32 transaction_id = 0; |
| 171 |
u16 seconds = 0; |
| 172 |
u16 boot_flag = 0; |
| 173 |
u32 client_ip = 0; |
| 174 |
u32 your_ip = 0; |
| 175 |
u32 server_ip = 0; |
| 176 |
u32 relay_ip = 0; |
| 177 |
MACAddress client_mac{}; |
| 178 |
unsigned char padding[10]{}; |
| 179 |
unsigned char hostname[0x40]{}; |
| 180 |
unsigned char boot_file[0x80]{}; |
| 181 |
u8 magic_cookie[4] = {0x63, 0x82, 0x53, 0x63}; |
| 182 |
}; |
| 183 |
static_assert(sizeof(DHCPBody) == DHCPBody::SIZE); |
| 184 |
|
| 185 |
struct DHCPPacket |
| 186 |
{ |
| 187 |
DHCPPacket(); |
| 188 |
DHCPPacket(std::span<const u8> data); |
| 189 |
void AddOption(u8 fnc, std::span<const u8> params); |
| 190 |
void AddOption(u8 fnc, std::initializer_list<u8> params) |
| 191 |
{ |
| 192 |
AddOption(fnc, {params.begin(), params.end()}); |
| 193 |
} |
| 194 |
std::vector<u8> Build() const; |
| 195 |
|
| 196 |
DHCPBody body; |
| 197 |
std::vector<std::vector<u8>> options; |
| 198 |
}; |
| 199 |
|
| 200 |
|
| 201 |
#pragma pack(push, 1) |
| 202 |
struct ARPPacket |
| 203 |
{ |
| 204 |
ARPPacket(); |
| 205 |
ARPPacket(const MACAddress& destination, const MACAddress& source); |
| 206 |
std::vector<u8> Build() const; |
| 207 |
u16 Size() const; |
| 208 |
|
| 209 |
EthernetHeader eth_header; |
| 210 |
ARPHeader arp_header; |
| 211 |
|
| 212 |
static constexpr std::size_t SIZE = EthernetHeader::SIZE + ARPHeader::SIZE; |
| 213 |
}; |
| 214 |
static_assert(sizeof(ARPPacket) == ARPPacket::SIZE); |
| 215 |
#pragma pack(pop) |
| 216 |
|
| 217 |
struct TCPPacket |
| 218 |
{ |
| 219 |
TCPPacket(); |
| 220 |
TCPPacket(const MACAddress& destination, const MACAddress& source, const sockaddr_in& from, |
| 221 |
const sockaddr_in& to, u32 seq, u32 ack, u16 flags); |
| 222 |
std::vector<u8> Build() const; |
| 223 |
u16 Size() const; |
| 224 |
|
| 225 |
EthernetHeader eth_header; |
| 226 |
IPv4Header ip_header; |
| 227 |
TCPHeader tcp_header; |
| 228 |
std::vector<u8> ipv4_options; |
| 229 |
std::vector<u8> tcp_options; |
| 230 |
std::vector<u8> data; |
| 231 |
|
| 232 |
static constexpr std::size_t MIN_SIZE = EthernetHeader::SIZE + IPv4Header::SIZE + TCPHeader::SIZE; |
| 233 |
}; |
| 234 |
|
| 235 |
struct UDPPacket |
| 236 |
{ |
| 237 |
UDPPacket(); |
| 238 |
UDPPacket(const MACAddress& destination, const MACAddress& source, const sockaddr_in& from, |
| 239 |
const sockaddr_in& to, const std::vector<u8>& payload); |
| 240 |
std::vector<u8> Build() const; |
| 241 |
u16 Size() const; |
| 242 |
|
| 243 |
EthernetHeader eth_header; |
| 244 |
IPv4Header ip_header; |
| 245 |
UDPHeader udp_header; |
| 246 |
|
| 247 |
std::vector<u8> ipv4_options; |
| 248 |
std::vector<u8> data; |
| 249 |
|
| 250 |
static constexpr std::size_t MIN_SIZE = EthernetHeader::SIZE + IPv4Header::SIZE + UDPHeader::SIZE; |
| 251 |
}; |
| 252 |
|
| 253 |
class PacketView |
| 254 |
{ |
| 255 |
public: |
| 256 |
PacketView(const u8* ptr, std::size_t size); |
| 257 |
|
| 258 |
std::optional<u16> GetEtherType() const; |
| 259 |
std::optional<ARPPacket> GetARPPacket() const; |
| 260 |
std::optional<u8> GetIPProto() const; |
| 261 |
std::optional<TCPPacket> GetTCPPacket() const; |
| 262 |
std::optional<UDPPacket> GetUDPPacket() const; |
| 263 |
|
| 264 |
private: |
| 265 |
const u8* m_ptr; |
| 266 |
std::size_t m_size; |
| 267 |
}; |
| 268 |
|
| 269 |
struct NetworkErrorState |
| 270 |
{ |
| 271 |
int error; |
| 272 |
#ifdef _WIN32 |
| 273 |
int wsa_error; |
| 274 |
#endif |
| 275 |
}; |
| 276 |
|
| 277 |
struct IPv4Port |
| 278 |
{ |
| 279 |
IPAddress ip_address; |
| 280 |
u16 port; |
| 281 |
|
| 282 |
|
| 283 |
u32 GetIPAddressValue() const; |
| 284 |
u16 GetPortValue() const; |
| 285 |
}; |
| 286 |
|
| 287 |
struct IPv4PortRange |
| 288 |
{ |
| 289 |
IPv4Port first; |
| 290 |
IPv4Port last; |
| 291 |
|
| 292 |
bool IsMatch(IPv4Port subject) const; |
| 293 |
std::string ToString() const; |
| 294 |
}; |
| 295 |
|
| 296 |
std::string IPAddressToString(IPAddress ip_address); |
| 297 |
|
| 298 |
|
| 299 |
std::optional<IPv4PortRange> StringToIPv4PortRange(std::string_view subject); |
| 300 |
|
| 301 |
MACAddress GenerateMacAddress(MACConsumer type); |
| 302 |
|
| 303 |
std::string MacAddressToString(const MACAddress& mac); |
| 304 |
std::optional<MACAddress> StringToMacAddress(std::string_view mac_string); |
| 305 |
|
| 306 |
std::string BluetoothAddressToString(BluetoothAddress bdaddr); |
| 307 |
std::optional<BluetoothAddress> StringToBluetoothAddress(std::string_view str); |
| 308 |
|
| 309 |
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value = 0); |
| 310 |
u16 ComputeTCPNetworkChecksum(const IPAddress& from, const IPAddress& to, const void* data, |
| 311 |
u16 length, u8 protocol); |
| 312 |
NetworkErrorState SaveNetworkErrorState(); |
| 313 |
void RestoreNetworkErrorState(const NetworkErrorState& state); |
| 314 |
const char* DecodeNetworkError(s32 error_code); |
| 315 |
const char* StrNetworkError(); |
| 316 |
|
| 317 |
|
| 318 |
bool SetPlatformSocketOptions(int fd); |
| 319 |
|
| 320 |
|
| 321 |
#if defined(__linux__) || defined(__HAIKU__) |
| 322 |
static constexpr int SEND_FLAGS = MSG_NOSIGNAL; |
| 323 |
#else |
| 324 |
static constexpr int SEND_FLAGS = 0; |
| 325 |
#endif |
| 326 |
|
| 327 |
} |