2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-03-06 18:09:12 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-06-10 18:32:33 +00:00
|
|
|
#include <array>
|
|
|
|
#include <optional>
|
2014-03-06 18:09:12 +00:00
|
|
|
#include <string>
|
2019-12-06 14:43:42 +00:00
|
|
|
#include <string_view>
|
2014-03-06 18:09:12 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
2017-08-29 21:14:01 +00:00
|
|
|
struct sockaddr_in;
|
|
|
|
|
2017-01-18 00:19:20 +00:00
|
|
|
namespace Common
|
2014-03-06 18:09:12 +00:00
|
|
|
{
|
2017-01-18 00:19:20 +00:00
|
|
|
enum class MACConsumer
|
|
|
|
{
|
|
|
|
BBA,
|
|
|
|
IOS
|
2014-03-06 18:09:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
MAC_ADDRESS_SIZE = 6
|
|
|
|
};
|
|
|
|
|
2018-06-10 18:32:33 +00:00
|
|
|
using MACAddress = std::array<u8, MAC_ADDRESS_SIZE>;
|
2017-08-29 21:14:01 +00:00
|
|
|
constexpr std::size_t IPV4_ADDR_LEN = 4;
|
|
|
|
|
|
|
|
struct EthernetHeader
|
|
|
|
{
|
|
|
|
EthernetHeader();
|
|
|
|
explicit EthernetHeader(u16 ether_type);
|
|
|
|
u16 Size() const;
|
|
|
|
|
|
|
|
static constexpr std::size_t SIZE = 14;
|
|
|
|
|
|
|
|
MACAddress destination = {};
|
|
|
|
MACAddress source = {};
|
|
|
|
u16 ethertype = 0;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(EthernetHeader) == EthernetHeader::SIZE);
|
|
|
|
|
|
|
|
struct IPv4Header
|
|
|
|
{
|
|
|
|
IPv4Header();
|
|
|
|
IPv4Header(u16 data_size, u8 ip_proto, const sockaddr_in& from, const sockaddr_in& to);
|
|
|
|
u16 Size() const;
|
|
|
|
|
|
|
|
static constexpr std::size_t SIZE = 20;
|
|
|
|
|
|
|
|
u8 version_ihl = 0;
|
|
|
|
u8 dscp_esn = 0;
|
|
|
|
u16 total_len = 0;
|
|
|
|
u16 identification = 0;
|
|
|
|
u16 flags_fragment_offset = 0;
|
|
|
|
u8 ttl = 0;
|
|
|
|
u8 protocol = 0;
|
|
|
|
u16 header_checksum = 0;
|
|
|
|
u8 source_addr[IPV4_ADDR_LEN]{};
|
|
|
|
u8 destination_addr[IPV4_ADDR_LEN]{};
|
|
|
|
};
|
|
|
|
static_assert(sizeof(IPv4Header) == IPv4Header::SIZE);
|
|
|
|
|
|
|
|
struct TCPHeader
|
|
|
|
{
|
|
|
|
TCPHeader();
|
|
|
|
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, const u8* data, u16 length);
|
|
|
|
u16 Size() const;
|
|
|
|
u8 IPProto() const;
|
|
|
|
|
|
|
|
static constexpr std::size_t SIZE = 20;
|
|
|
|
|
|
|
|
u16 source_port = 0;
|
|
|
|
u16 destination_port = 0;
|
|
|
|
u32 sequence_number = 0;
|
|
|
|
u32 acknowledgement_number = 0;
|
|
|
|
u16 properties = 0;
|
|
|
|
u16 window_size = 0;
|
|
|
|
u16 checksum = 0;
|
|
|
|
u16 urgent_pointer = 0;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(TCPHeader) == TCPHeader::SIZE);
|
|
|
|
|
|
|
|
struct UDPHeader
|
|
|
|
{
|
|
|
|
UDPHeader();
|
|
|
|
UDPHeader(const sockaddr_in& from, const sockaddr_in& to, u16 data_length);
|
|
|
|
u16 Size() const;
|
|
|
|
u8 IPProto() const;
|
|
|
|
|
|
|
|
static constexpr std::size_t SIZE = 8;
|
|
|
|
|
|
|
|
u16 source_port = 0;
|
|
|
|
u16 destination_port = 0;
|
|
|
|
u16 length = 0;
|
|
|
|
u16 checksum = 0;
|
|
|
|
};
|
|
|
|
static_assert(sizeof(UDPHeader) == UDPHeader::SIZE);
|
2018-06-10 18:32:33 +00:00
|
|
|
|
|
|
|
MACAddress GenerateMacAddress(MACConsumer type);
|
|
|
|
std::string MacAddressToString(const MACAddress& mac);
|
2019-12-06 14:43:42 +00:00
|
|
|
std::optional<MACAddress> StringToMacAddress(std::string_view mac_string);
|
2017-08-29 21:14:01 +00:00
|
|
|
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value = 0);
|
2017-01-18 00:19:20 +00:00
|
|
|
} // namespace Common
|