BBA: Added BuiltIn device that allow BBA emulation without the need of a TapDevice Configuration include a dns server setting

This commit is contained in:
schthack
2022-05-22 12:21:28 -04:00
parent d625c612c4
commit 01ada3850f
16 changed files with 1063 additions and 45 deletions

View File

@ -7,6 +7,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "Common/CommonTypes.h"
@ -22,7 +23,15 @@ enum class MACConsumer
enum
{
MAC_ADDRESS_SIZE = 6
BBA_HARDWARE_TYPE = 1,
MAC_ADDRESS_SIZE = 6,
IPV4_HEADER_TYPE = 8
};
enum DHCPConst
{
MESSAGE_QUERY = 1,
MESSAGE_REPLY = 2
};
using MACAddress = std::array<u8, MAC_ADDRESS_SIZE>;
@ -67,6 +76,7 @@ struct TCPHeader
{
TCPHeader();
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, const u8* data, u16 length);
TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, u32 ack, u16 flags);
u16 Size() const;
u8 IPProto() const;
@ -99,6 +109,54 @@ struct UDPHeader
};
static_assert(sizeof(UDPHeader) == UDPHeader::SIZE);
#pragma pack(push, 1)
struct ARPHeader
{
ARPHeader();
ARPHeader(u32 from_ip, MACAddress from_mac, u32 to_ip, MACAddress to_mac);
u16 Size() const;
static constexpr std::size_t SIZE = 28;
u16 hardware_type = 0;
u16 protocol_type = 0;
u8 hardware_size = 0;
u8 protocol_size = 0;
u16 opcode = 0;
MACAddress sender_address{};
u32 sender_ip = 0;
MACAddress targer_address{};
u32 target_ip = 0;
};
static_assert(sizeof(ARPHeader) == ARPHeader::SIZE);
#pragma pack(pop)
struct DHCPBody
{
DHCPBody();
DHCPBody(u32 transaction, MACAddress client_address, u32 new_ip, u32 serv_ip);
bool AddDHCPOption(u8 size, u8 fnc, const std::vector<u8>& params);
static constexpr std::size_t SIZE = 540;
u8 message_type = 0;
u8 hardware_type = 0;
u8 hardware_addr = 0;
u8 hops = 0;
u32 transaction_id = 0;
u16 secondes = 0;
u16 boot_flag = 0;
u32 client_ip = 0;
u32 your_ip = 0;
u32 server_ip = 0;
u32 relay_ip = 0;
MACAddress client_mac{};
unsigned char padding[10]{};
unsigned char hostname[0x40]{};
unsigned char boot_file[0x80]{};
u32 magic_cookie = 0x63538263;
u8 options[300]{};
};
static_assert(sizeof(DHCPBody) == DHCPBody::SIZE);
struct NetworkErrorState
{
int error;
@ -111,6 +169,8 @@ MACAddress GenerateMacAddress(MACConsumer type);
std::string MacAddressToString(const MACAddress& mac);
std::optional<MACAddress> StringToMacAddress(std::string_view mac_string);
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value = 0);
u16 ComputeTCPNetworkChecksum(const sockaddr_in& from, const sockaddr_in& to, const void* data,
u16 length, u8 protocol);
NetworkErrorState SaveNetworkErrorState();
void RestoreNetworkErrorState(const NetworkErrorState& state);
} // namespace Common