DEV9: Make use of MAC_Address struct

This commit is contained in:
TheLastRar 2023-01-16 20:34:19 +00:00 committed by refractionpcsx2
parent 8fbecbcdd7
commit a85a2a3cc5
7 changed files with 56 additions and 52 deletions

View File

@ -27,8 +27,8 @@ namespace PacketReader
EthernetFrame::EthernetFrame(NetPacket* pkt)
{
int offset = 0;
NetLib::ReadByteArray((u8*)pkt->buffer, &offset, 6, destinationMAC);
NetLib::ReadByteArray((u8*)pkt->buffer, &offset, 6, sourceMAC);
NetLib::ReadMACAddress((u8*)pkt->buffer, &offset, &destinationMAC);
NetLib::ReadMACAddress((u8*)pkt->buffer, &offset, &sourceMAC);
headerLength = 14; //(6+6+2)
@ -51,8 +51,8 @@ namespace PacketReader
int counter = 0;
pkt->size = headerLength + payload->GetLength();
NetLib::WriteByteArray((u8*)pkt->buffer, &counter, 6, destinationMAC);
NetLib::WriteByteArray((u8*)pkt->buffer, &counter, 6, sourceMAC);
NetLib::WriteMACAddress((u8*)pkt->buffer, &counter, destinationMAC);
NetLib::WriteMACAddress((u8*)pkt->buffer, &counter, sourceMAC);
//
NetLib::WriteUInt16((u8*)pkt->buffer, &counter, protocol);
//

View File

@ -16,6 +16,7 @@
#pragma once
#include "DEV9/net.h"
#include "MAC_Address.h"
#include "Payload.h"
namespace PacketReader
@ -34,8 +35,8 @@ namespace PacketReader
class EthernetFrame
{
public:
u8 destinationMAC[6] = {0};
u8 sourceMAC[6] = {0};
MAC_Address destinationMAC{};
MAC_Address sourceMAC{};
u16 protocol = 0;
int headerLength = 14;

View File

@ -37,6 +37,8 @@
#include <wil/com.h>
#include <wil/resource.h>
#include "DEV9/PacketReader/MAC_Address.h"
//=============
// TAP IOCTLs
//=============
@ -206,7 +208,7 @@ AdapterOptions TAPAdapter::GetAdapterOptions()
return AdapterOptions::None;
}
static int TAPGetMACAddress(HANDLE handle, u8* addr)
static int TAPGetMACAddress(HANDLE handle, PacketReader::MAC_Address* addr)
{
DWORD len = 0;
@ -551,17 +553,17 @@ TAPAdapter::TAPAdapter()
cancel = CreateEvent(NULL, TRUE, FALSE, NULL);
u8 hostMAC[6];
u8 newMAC[6];
PacketReader::MAC_Address hostMAC;
PacketReader::MAC_Address newMAC;
TAPGetMACAddress(htap, hostMAC);
memcpy(newMAC, ps2MAC, 6);
TAPGetMACAddress(htap, &hostMAC);
newMAC = ps2MAC;
//Lets take the hosts last 2 bytes to make it unique on Xlink
newMAC[5] = hostMAC[4];
newMAC[4] = hostMAC[5];
newMAC.bytes[5] = hostMAC.bytes[4];
newMAC.bytes[4] = hostMAC.bytes[5];
SetMACAddress(newMAC);
SetMACAddress(&newMAC);
IP_ADAPTER_ADDRESSES adapter;
std::unique_ptr<IP_ADAPTER_ADDRESSES[]> buffer;

View File

@ -178,8 +178,8 @@ using namespace PacketReader::IP;
using namespace PacketReader::IP::UDP;
const IP_Address NetAdapter::internalIP{{{192, 0, 2, 1}}};
const u8 NetAdapter::broadcastMAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
const u8 NetAdapter::internalMAC[6] = {0x76, 0x6D, 0xF4, 0x63, 0x30, 0x31};
const MAC_Address NetAdapter::broadcastMAC{{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}};
const MAC_Address NetAdapter::internalMAC{{{0x76, 0x6D, 0xF4, 0x63, 0x30, 0x31}}};
NetAdapter::NetAdapter()
{
@ -268,15 +268,14 @@ void NetAdapter::InspectRecv(NetPacket* pkt)
}
}
void NetAdapter::SetMACAddress(u8* mac)
void NetAdapter::SetMACAddress(MAC_Address* mac)
{
if (mac == nullptr)
memcpy(ps2MAC, defaultMAC, 6);
ps2MAC = defaultMAC;
else
memcpy(ps2MAC, mac, 6);
ps2MAC = *mac;
for (int i = 0; i < 3; i++)
dev9.eeprom[i] = ((u16*)ps2MAC)[i];
*(MAC_Address*)&dev9.eeprom[0] = ps2MAC;
//The checksum seems to be all the values of the mac added up in 16bit chunks
dev9.eeprom[3] = (dev9.eeprom[0] + dev9.eeprom[1] + dev9.eeprom[2]) & 0xffff;
@ -284,13 +283,13 @@ void NetAdapter::SetMACAddress(u8* mac)
bool NetAdapter::VerifyPkt(NetPacket* pkt, int read_size)
{
if ((memcmp(pkt->buffer, ps2MAC, 6) != 0) && (memcmp(pkt->buffer, &broadcastMAC, 6) != 0))
if ((*(MAC_Address*)&pkt->buffer[0] != ps2MAC) && (*(MAC_Address*)&pkt->buffer[0] != broadcastMAC))
{
//ignore strange packets
return false;
}
if (memcmp(pkt->buffer + 6, ps2MAC, 6) == 0)
if (*(MAC_Address*)&pkt->buffer[6] == ps2MAC)
{
//avoid pcap looping packets
return false;
@ -347,8 +346,8 @@ bool NetAdapter::InternalServerRecv(NetPacket* pkt)
ippkt->destinationIP = {{{255, 255, 255, 255}}};
ippkt->sourceIP = internalIP;
EthernetFrame frame(ippkt);
memcpy(frame.sourceMAC, internalMAC, 6);
memcpy(frame.destinationMAC, ps2MAC, 6);
frame.sourceMAC = internalMAC;
frame.destinationMAC = ps2MAC;
frame.protocol = (u16)EtherType::IPv4;
frame.WritePacket(pkt);
return true;
@ -361,8 +360,8 @@ bool NetAdapter::InternalServerRecv(NetPacket* pkt)
ippkt->destinationIP = ps2IP;
ippkt->sourceIP = internalIP;
EthernetFrame frame(ippkt);
memcpy(frame.sourceMAC, internalMAC, 6);
memcpy(frame.destinationMAC, ps2MAC, 6);
frame.sourceMAC = internalMAC;
frame.destinationMAC = ps2MAC;
frame.protocol = (u16)EtherType::IPv4;
frame.WritePacket(pkt);
InspectRecv(pkt);

View File

@ -34,6 +34,7 @@
#include "Config.h"
#include "PacketReader/MAC_Address.h"
#include "PacketReader/IP/IP_Address.h"
#include "InternalServers/DHCP_Server.h"
#include "InternalServers/DNS_Logger.h"
@ -42,7 +43,7 @@
struct ConfigDEV9;
// first three recognized by Xlink as Sony PS2
const u8 defaultMAC[6] = {0x00, 0x04, 0x1F, 0x82, 0x30, 0x31};
const PacketReader::MAC_Address defaultMAC = {{{0x00, 0x04, 0x1F, 0x82, 0x30, 0x31}}};
struct NetPacket
{
@ -93,9 +94,9 @@ public:
static const PacketReader::IP::IP_Address internalIP;
protected:
u8 ps2MAC[6];
static const u8 broadcastMAC[6];
static const u8 internalMAC[6];
PacketReader::MAC_Address ps2MAC;
static const PacketReader::MAC_Address broadcastMAC;
static const PacketReader::MAC_Address internalMAC;
private:
//Only set if packet sent to the internal IP address
@ -126,7 +127,7 @@ public:
virtual ~NetAdapter();
protected:
void SetMACAddress(u8* mac);
void SetMACAddress(PacketReader::MAC_Address* mac);
bool VerifyPkt(NetPacket* pkt, int read_size);
void InspectRecv(NetPacket* pkt);

View File

@ -39,6 +39,7 @@
#include "DEV9.h"
#include "AdapterUtils.h"
#include "net.h"
#include "DEV9/PacketReader/MAC_Address.h"
#ifndef PCAP_NETMASK_UNKNOWN
#define PCAP_NETMASK_UNKNOWN 0xffffffff
#endif
@ -293,13 +294,13 @@ PCAPAdapter::PCAPAdapter()
mac_address newMAC;
GetMACAddress(EmuConfig.DEV9.EthDevice, &hostMAC);
memcpy(&newMAC, ps2MAC, 6);
memcpy(&newMAC, &ps2MAC, 6);
//Lets take the hosts last 2 bytes to make it unique on Xlink
newMAC.bytes[5] = hostMAC.bytes[4];
newMAC.bytes[4] = hostMAC.bytes[5];
SetMACAddress((u8*)&newMAC);
SetMACAddress((PacketReader::MAC_Address*)&newMAC);
host_mac = hostMAC;
ps2_mac = newMAC; //Needed outside of this class

View File

@ -239,34 +239,34 @@ SocketAdapter::SocketAdapter()
InitInternalServer(&adapter, true, ps2IP, subnet, gateway);
u8 hostMAC[6];
u8 newMAC[6];
MAC_Address hostMAC;
MAC_Address newMAC;
#ifdef _WIN32
memcpy(hostMAC, adapter.PhysicalAddress, 6);
hostMAC = *(MAC_Address*)adapter.PhysicalAddress;
#elif defined(__linux__)
struct ifreq ifr;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
strcpy(ifr.ifr_name, adapter.ifa_name);
if (0 == ioctl(fd, SIOCGIFHWADDR, &ifr))
memcpy(hostMAC, ifr.ifr_hwaddr.sa_data, 6);
hostMAC = *(MAC_Address*)ifr.ifr_hwaddr.sa_data;
else
{
memcpy(hostMAC, ps2MAC, 6);
hostMAC = ps2MAC;
Console.Error("Could not get MAC address for adapter: %s", adapter.ifa_name);
}
::close(fd);
#else
memcpy(hostMAC, ps2MAC, 6);
hostMAC = ps2MAC;
Console.Error("Could not get MAC address for adapter, OS not supported");
#endif
memcpy(newMAC, ps2MAC, 6);
newMAC = ps2MAC;
//Lets take the hosts last 2 bytes to make it unique on Xlink
newMAC[5] = hostMAC[4];
newMAC[4] = hostMAC[5];
newMAC.bytes[5] = hostMAC.bytes[4];
newMAC.bytes[4] = hostMAC.bytes[5];
SetMACAddress(newMAC);
SetMACAddress(&newMAC);
#ifdef _WIN32
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
@ -322,8 +322,8 @@ bool SocketAdapter::recv(NetPacket* pkt)
ipPkt->sourceIP = session->destIP;
EthernetFrame frame(ipPkt);
memcpy(frame.sourceMAC, internalMAC, 6);
memcpy(frame.destinationMAC, ps2MAC, 6);
frame.sourceMAC = internalMAC;
frame.destinationMAC = ps2MAC;
frame.protocol = (u16)EtherType::IPv4;
frame.WritePacket(pkt);
@ -380,17 +380,17 @@ bool SocketAdapter::send(NetPacket* pkt)
//it's trying to resolve the virtual gateway's mac addr
{
ARP_Packet* arpRet = new ARP_Packet(6, 4);
memcpy(arpRet->targetHardwareAddress.get(), arpPkt.senderHardwareAddress.get(), 6);
memcpy(arpRet->senderHardwareAddress.get(), internalMAC, 6);
memcpy(arpRet->targetProtocolAddress.get(), arpPkt.senderProtocolAddress.get(), 4);
memcpy(arpRet->senderProtocolAddress.get(), arpPkt.targetProtocolAddress.get(), 4);
*(MAC_Address*)arpRet->targetHardwareAddress.get() = *(MAC_Address*)arpPkt.senderHardwareAddress.get();
*(MAC_Address*)arpRet->senderHardwareAddress.get() = internalMAC;
*(IP_Address*)arpRet->targetProtocolAddress.get() = *(IP_Address*)arpPkt.senderProtocolAddress.get();
*(IP_Address*)arpRet->senderProtocolAddress.get() = *(IP_Address*)arpPkt.targetProtocolAddress.get();
arpRet->op = 2,
arpRet->protocol = arpPkt.protocol;
arpRet->hardwareType = arpPkt.hardwareType;
EthernetFrame* retARP = new EthernetFrame(arpRet);
memcpy(retARP->destinationMAC, ps2MAC, 6);
memcpy(retARP->sourceMAC, internalMAC, 6);
retARP->destinationMAC = ps2MAC;
retARP->sourceMAC = internalMAC;
retARP->protocol = (u16)EtherType::ARP;
vRecBuffer.Enqueue(retARP);