summaryrefslogtreecommitdiff
path: root/Source/Core/Common
diff options
context:
space:
mode:
authorschthack <schthack@yahoo.com>2022-05-22 12:21:28 -0400
committerschthack <schthack@yahoo.com>2022-07-02 18:02:50 -0400
commit01ada3850ff07c0225819fee2898f355aaf3707a (patch)
tree16175a35b4060a0c788eff71fa5004c4c4c3c01a /Source/Core/Common
parentd625c612c4a40a8c8db3d90b313f5643aec9c890 (diff)
BBA: Added BuiltIn device that allow BBA emulation without the need of a TapDevice Configuration include a dns server setting
Diffstat (limited to 'Source/Core/Common')
-rw-r--r--Source/Core/Common/Network.cpp80
-rw-r--r--Source/Core/Common/Network.h62
2 files changed, 141 insertions, 1 deletions
diff --git a/Source/Core/Common/Network.cpp b/Source/Core/Common/Network.cpp
index faa2a33c05..756dd0381e 100644
--- a/Source/Core/Common/Network.cpp
+++ b/Source/Core/Common/Network.cpp
@@ -5,6 +5,7 @@
#include <algorithm>
#include <string_view>
+#include <vector>
#ifndef _WIN32
#include <netinet/in.h>
@@ -141,6 +142,18 @@ TCPHeader::TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, co
checksum = htons(static_cast<u16>(tcp_checksum));
}
+TCPHeader::TCPHeader(const sockaddr_in& from, const sockaddr_in& to, u32 seq, u32 ack, u16 flags)
+{
+ source_port = from.sin_port;
+ destination_port = to.sin_port;
+ sequence_number = htonl(seq);
+ acknowledgement_number = htonl(ack);
+ properties = 0x50 | flags;
+
+ window_size = 0x7c;
+ checksum = 0;
+}
+
u16 TCPHeader::Size() const
{
return static_cast<u16>(SIZE);
@@ -170,6 +183,59 @@ u8 UDPHeader::IPProto() const
return static_cast<u8>(IPPROTO_UDP);
}
+ARPHeader::ARPHeader() = default;
+
+ARPHeader::ARPHeader(u32 from_ip, MACAddress from_mac, u32 to_ip, MACAddress to_mac)
+{
+ hardware_type = htons(BBA_HARDWARE_TYPE);
+ protocol_type = IPV4_HEADER_TYPE;
+ hardware_size = MAC_ADDRESS_SIZE;
+ protocol_size = IPV4_ADDR_LEN;
+ opcode = 0x200;
+ sender_ip = from_ip;
+ target_ip = to_ip;
+ targer_address = to_mac;
+ sender_address = from_mac;
+}
+
+u16 ARPHeader::Size() const
+{
+ return static_cast<u16>(SIZE);
+}
+
+DHCPBody::DHCPBody() = default;
+
+DHCPBody::DHCPBody(u32 transaction, MACAddress client_address, u32 new_ip, u32 serv_ip)
+{
+ transaction_id = transaction;
+ message_type = DHCPConst::MESSAGE_REPLY;
+ hardware_type = BBA_HARDWARE_TYPE;
+ hardware_addr = MAC_ADDRESS_SIZE;
+ client_mac = client_address;
+ your_ip = new_ip;
+ server_ip = serv_ip;
+}
+
+// Add an option to the DHCP Body
+bool DHCPBody::AddDHCPOption(u8 size, u8 fnc, const std::vector<u8>& params)
+{
+ int i = 0;
+ while (options[i] != 0)
+ {
+ i += options[i + 1] + 2;
+ if (i >= std::size(options))
+ {
+ return false;
+ }
+ }
+
+ options[i++] = fnc;
+ options[i++] = size;
+ for (auto val : params)
+ options[i++] = val;
+ return true;
+}
+
// Compute the network checksum with a 32-bit accumulator using the
// "Normal" order, see RFC 1071 for more details.
u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value)
@@ -187,6 +253,20 @@ u16 ComputeNetworkChecksum(const void* data, u16 length, u32 initial_value)
return ~static_cast<u16>(checksum);
}
+// Compute the TCP network checksum with a fake header
+u16 ComputeTCPNetworkChecksum(const sockaddr_in& from, const sockaddr_in& to, const void* data,
+ u16 length, u8 protocol)
+{
+ // Compute the TCP checksum with its pseudo header
+ const u32 source_addr = ntohl(from.sin_addr.s_addr);
+ const u32 destination_addr = ntohl(to.sin_addr.s_addr);
+ const u32 initial_value = (source_addr >> 16) + (source_addr & 0xFFFF) +
+ (destination_addr >> 16) + (destination_addr & 0xFFFF) + protocol +
+ length;
+ const u32 tcp_checksum = ComputeNetworkChecksum(data, length, initial_value);
+ return htons(static_cast<u16>(tcp_checksum));
+}
+
NetworkErrorState SaveNetworkErrorState()
{
return {
diff --git a/Source/Core/Common/Network.h b/Source/Core/Common/Network.h
index 0b56e09d34..eec09234a7 100644
--- a/Source/Core/Common/Network.h
+++ b/Source/Core/Common/Network.h
@@ -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