melonDS/src/WifiAP.cpp

420 lines
11 KiB
C++
Raw Normal View History

2017-12-18 18:53:51 +00:00
/*
2024-06-15 15:01:19 +00:00
Copyright 2016-2024 melonDS team
2017-12-18 18:53:51 +00:00
This file is part of melonDS.
melonDS is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with melonDS. If not, see http://www.gnu.org/licenses/.
*/
#include <stdio.h>
#include <string.h>
#include "NDS.h"
2017-12-18 18:53:51 +00:00
#include "Wifi.h"
#include "WifiAP.h"
2017-12-18 22:24:45 +00:00
#include "Platform.h"
2017-12-18 18:53:51 +00:00
#ifndef __WIN32__
#include <stddef.h>
#endif
2017-12-18 18:53:51 +00:00
namespace melonDS
{
using Platform::Log;
using Platform::LogLevel;
2017-12-18 18:53:51 +00:00
2023-11-04 16:00:12 +00:00
const char* WifiAP::APName = "melonAP";
const u8 WifiAP::APMac[6] = {0x00, 0xF0, 0x77, 0x77, 0x77, 0x77};
const u8 WifiAP::APChannel = 6;
#define PWRITE_8(p, v) *p++ = v;
#define PWRITE_16(p, v) *(u16*)p = v; p += 2;
#define PWRITE_32(p, v) *(u32*)p = v; p += 4;
#define PWRITE_64(p, v) *(u64*)p = v; p += 8;
#define PWRITE_MAC(p, a,b,c,d,e,f) \
*p++ = a; *p++ = b; *p++ = c; *p++ = d; *p++ = e; *p++ = f;
#define PWRITE_MAC2(p, m) \
*p++ = m[0]; *p++ = m[1]; *p++ = m[2]; *p++ = m[3]; *p++ = m[4]; *p++ = m[5];
#define PWRITE_SEQNO(p) PWRITE_16(p, SeqNo); SeqNo += 0x10;
#define PWRITE_TXH(p, len, rate) \
PWRITE_16(p, 0); \
PWRITE_16(p, 0); \
PWRITE_16(p, 0); \
PWRITE_16(p, 0); \
PWRITE_8(p, rate); \
PWRITE_8(p, APChannel); \
PWRITE_16(p, len);
//#define PALIGN_4(p, base) p += ((4 - ((ptrdiff_t)(p-base) & 0x3)) & 0x3);
// no idea what is the ideal padding there
// but in the case of management frames the padding shouldn't be counted as an information element
// (theory: the hardware just doesn't touch the space between the frame and the FCS)
#define PLEN(p, base) (int)(ptrdiff_t)(p-base)
#define PALIGN_4(p, base) while (PLEN(p,base) & 0x3) *p++ = 0xFF;
bool MACEqual(const u8* a, const u8* b);
bool MACIsBroadcast(const u8* a);
2017-12-18 22:24:45 +00:00
WifiAP::WifiAP(Wifi* client, void* userdata) : Client(client), UserData(userdata)
{
}
2023-11-04 16:00:12 +00:00
WifiAP::~WifiAP()
{
}
2023-11-04 16:00:12 +00:00
void WifiAP::Reset()
{
// random starting point for the counter
merge local_wifi (#1516) * attempt at betterer wifi * add preliminary sync mechanism * fix gaps in wifi implementation * move local-MP comm to its own module instead of cramping Platform.cpp * remove some stupid cruft * as you wish, Sorer (starting work on shared-memory system) * shared-memory IPC that actually works (albeit Windows-only for now) * shut up logging from NULL writes on ARM7 (ffs Nintendo learn to code) * get this somewhat good * leave client sync mode when host deauths. makes download play actually work. * start implementing MP-comm error handling * * add MP-reply error counters * feeble attempt at fixing slowdown/desync/etc problems * somewhat better exchange/sync method * * when entering power-saving mode, be sure to finish transferring the current frame first * fix misc bug due to old cruft leftover makes for a more stable connection * remove a bunch of cruft * set wifi time interval to 34 cycles instead of 33. games seem sensitive to the general timing of wifi vs the rest of the system, and this seems to make things run better, atleast until I rewrite this to use a proper scheduler. * more graceful handling of disconnects * deal with FIFO overflow more gracefully * BAHAHAHAHAHAHAHAHHHH THE SNEAKY BASTARDS so, when the DS receives a beacon with the right BSSID that beacon's timestamp is copied to USCOUNTER * attempt at making the connection process smoother for weird games * * begin adding POWCNT2, only applies to wifi for now * begin work on wifi scheduler * implement the shitty timers * add the RF wakeup thing * begin work on receiving frames. for now it can just receive melonAP beacons, but hey, it's a start. * add enough TX functionality that online wifi is a possibility again. * there are problems with this scheduler thing. committing it anyway * kind of a rollback... we're gonna work out a compromise on this, I guess * don't transmit shit if RXCNT.bit15 isn't set * move RX-finish to its own function. more accurate filtering. implement RXFILTER. * remove some cruft * fix some of the shittiness when trying to connect more than two players * fix some more shittiness * fix more wifi shittiness (mainly don't try to receive shit while sending a frame) * run wifi every 8µs. improves performance. * fix IRQ14/IRQ15 * make this work under Linux * Make it work on macOS, for now using a custom sem_timedwait implementation. If anyone knows anything about mach ports and have an idea for how to make this work using mach IPC, please do let me know. * 25ms seems like a good timeout * begin work on proper multiplayer UI shito. for now, determine a global instance ID, and derivate the system MAC from it. remove 'randomize MAC' option. * finish removing RandomizeMAC * lay groundwork for instance-unique config * work some on the UI... make it not labelled Fart * more UI work: make it explicit that some things are instance-unique * separate firmware files for multiplayer instances * make instances save to different save files, too * more UI work, make things somewhat less shitty * lay base for the multiplayer settings dialog * actually hook up most of that dialog * actually implement the fun audio settings * ensure all the wifi shit is properly savestated and reset. properly update timings for the wifi region when wifi is disabled. * add more fun labels * * ignore WEP frames if WEP is off * implement RX_LEN_CROP * fake enough of WEP processing to make Inazuma Eleven work * * do not copy more ROM banner data than actually needed * avoid trying to read out of bounds if the banner offset is bad * Fix oversight with the preferences action causing the build to fail on macOS Co-authored-by: Nadia Holmquist Pedersen <nadia@nhp.sh>
2022-09-22 18:32:27 +00:00
USCounter = 0x428888000ULL;
SeqNo = 0x0120;
BeaconDue = false;
memset(PacketBuffer, 0, sizeof(PacketBuffer));
PacketLen = 0;
RXNum = 0;
ClientStatus = 0;
}
2023-11-04 16:00:12 +00:00
void WifiAP::MSTimer()
{
USCounter += 0x400;
u32 chk = (u32)USCounter;
if (!(chk & 0x1FC00))
{
// send beacon every 128ms
BeaconDue = true;
}
}
int WifiAP::HandleManagementFrame(const u8* data, int len)
{
// TODO: perfect this
// noting that frames sent pre-auth/assoc don't have a proper BSSID
//if (!MACEqual(&data[16], (u8*)APMac)) // check BSSID
// return 0;
if (RXNum)
{
Log(LogLevel::Warn, "wifiAP: can't reply!!\n");
return 0;
}
u16 framectl = *(u16*)&data[0];
u8* base = &PacketBuffer[0];
u8* p = base;
switch ((framectl >> 4) & 0xF)
{
case 0x0: // assoc request
{
if (!MACEqual(&data[16], (u8*)APMac)) // check BSSID
return 0;
if (ClientStatus != 1)
{
Log(LogLevel::Error, "wifiAP: bad assoc request, needs auth prior\n");
return 0;
}
ClientStatus = 2;
Assorted portability enhancements (#1800) * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Change includes of "../types.h" to "types.h" - Makes it easier to directly include these headers in downstream projects * Change an include of "../Wifi.h" to "Wifi.h" * Allow CommonFuncs.cpp to compile on Android * Tidy up some logging calls - Use Platform::Log in LAN_Socket.cpp - Soften some warnings to Debug logs (since they don't necessarily represent problems) * Add Platform::EnterGBAMode - Gracefully stop the emulator if trying to enter GBA mode * Soften some logs that most players won't care about * Soften some more logs * Introduce Platform wrappers for file operations * Fix pointer spacing * Fix more style nits * Log the errno when ftruncate fails * Fix FileSeek offset argument - With an s32 offset, we couldn't access files larger than 2GB * Revise Platform::StopEmu to address feedback - Remove Platform::EnterGBAMode in favor of adding a reason to Platform::StopEmu - Also rename Platform::StopEmu to Platform::SignalStop - Add an optional argument to NDS::Stop - Use the new argument everywhere that the console stops itself * Rename FileGetString to FileReadLine - It conveys the meaning better * Rename FileSeekOrigin::Set to Start - It conveys the meaning better * Change definition of FileGetString to FileReadLine - Oops, almost forgot it * Rename FlushFile to FileFlush - To remain consistent with the other File functions * Add a FileType usage * Fix line break in FileSeekOrigin * Document Platform::DeInit * Clarify that StopReason::Unknown doesn't always mean an error * Move and document FileType::HostFile * Remove Platform::OpenDataFile - Nothing currently uses it * Refactor Platform::OpenFile and Platform::OpenLocalFile to accept a FileMode enum instead of a string - The enum is converted to fopen flags under the hood - The file type is used to decide whether to add the "b" flag - Some helper functions are exposed for the benefit of consistent behavior among frontends - Equivalent behavior is maintained * Fix a tab that should be spaces * Use Windows' 64-bit implementations of fseek/ftell * Move Platform::IsBinaryFile to Platform.cpp - It could vary by frontend * Remove an unused FileType * Rename an enum constant * Document various Platform items * Use Platform::DynamicLibrary to load libandroid - And clean it up at the end * Fix a typo * Pass the correct filetype to FATStorage - Since it can be used for DSI NAND images or for SD cards * Remove Platform::FileType
2023-08-18 20:50:57 +00:00
Log(LogLevel::Debug, "wifiAP: client associated\n");
PWRITE_16(p, 0x0010);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC2(p, (&data[10])); // recv
PWRITE_MAC2(p, APMac); // sender
PWRITE_MAC2(p, APMac); // BSSID
PWRITE_SEQNO(p);
PWRITE_16(p, 0x0021); // capability
PWRITE_16(p, 0); // status (success)
PWRITE_16(p, 0xC001); // assoc ID
PWRITE_8(p, 0x01); PWRITE_8(p, 0x02); PWRITE_8(p, 0x82); PWRITE_8(p, 0x84); // rates
PacketLen = PLEN(p, base);
RXNum = 1;
}
return len;
case 0x4: // probe request
{
// Nintendo's WFC setup util sends probe requests when searching for APs
// these should be replied with a probe response, which is almost like a beacon
PWRITE_16(p, 0x0050);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC2(p, (&data[10])); // recv
PWRITE_MAC2(p, APMac); // sender
PWRITE_MAC2(p, APMac); // BSSID (checkme)
PWRITE_SEQNO(p);
PWRITE_64(p, USCounter);
PWRITE_16(p, 128); // beacon interval
PWRITE_16(p, 0x0021); // capability
PWRITE_8(p, 0x01); PWRITE_8(p, 0x02); PWRITE_8(p, 0x82); PWRITE_8(p, 0x84); // rates
PWRITE_8(p, 0x03); PWRITE_8(p, 0x01); PWRITE_8(p, APChannel); // current channel
2023-11-04 16:00:12 +00:00
PWRITE_8(p, 0x00); PWRITE_8(p, strlen(APName));
memcpy(p, APName, strlen(APName)); p += strlen(APName);
PacketLen = PLEN(p, base);
RXNum = 1;
}
return len;
case 0xA: // deassoc
{
if (!MACEqual(&data[16], (u8*)APMac)) // check BSSID
return 0;
ClientStatus = 1;
Assorted portability enhancements (#1800) * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Change includes of "../types.h" to "types.h" - Makes it easier to directly include these headers in downstream projects * Change an include of "../Wifi.h" to "Wifi.h" * Allow CommonFuncs.cpp to compile on Android * Tidy up some logging calls - Use Platform::Log in LAN_Socket.cpp - Soften some warnings to Debug logs (since they don't necessarily represent problems) * Add Platform::EnterGBAMode - Gracefully stop the emulator if trying to enter GBA mode * Soften some logs that most players won't care about * Soften some more logs * Introduce Platform wrappers for file operations * Fix pointer spacing * Fix more style nits * Log the errno when ftruncate fails * Fix FileSeek offset argument - With an s32 offset, we couldn't access files larger than 2GB * Revise Platform::StopEmu to address feedback - Remove Platform::EnterGBAMode in favor of adding a reason to Platform::StopEmu - Also rename Platform::StopEmu to Platform::SignalStop - Add an optional argument to NDS::Stop - Use the new argument everywhere that the console stops itself * Rename FileGetString to FileReadLine - It conveys the meaning better * Rename FileSeekOrigin::Set to Start - It conveys the meaning better * Change definition of FileGetString to FileReadLine - Oops, almost forgot it * Rename FlushFile to FileFlush - To remain consistent with the other File functions * Add a FileType usage * Fix line break in FileSeekOrigin * Document Platform::DeInit * Clarify that StopReason::Unknown doesn't always mean an error * Move and document FileType::HostFile * Remove Platform::OpenDataFile - Nothing currently uses it * Refactor Platform::OpenFile and Platform::OpenLocalFile to accept a FileMode enum instead of a string - The enum is converted to fopen flags under the hood - The file type is used to decide whether to add the "b" flag - Some helper functions are exposed for the benefit of consistent behavior among frontends - Equivalent behavior is maintained * Fix a tab that should be spaces * Use Windows' 64-bit implementations of fseek/ftell * Move Platform::IsBinaryFile to Platform.cpp - It could vary by frontend * Remove an unused FileType * Rename an enum constant * Document various Platform items * Use Platform::DynamicLibrary to load libandroid - And clean it up at the end * Fix a typo * Pass the correct filetype to FATStorage - Since it can be used for DSI NAND images or for SD cards * Remove Platform::FileType
2023-08-18 20:50:57 +00:00
Log(LogLevel::Debug, "wifiAP: client deassociated\n");
PWRITE_16(p, 0x00A0);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC2(p, (&data[10])); // recv
PWRITE_MAC2(p, APMac); // sender
PWRITE_MAC2(p, APMac); // BSSID
PWRITE_SEQNO(p);
PWRITE_16(p, 3); // reason code
PacketLen = PLEN(p, base);
RXNum = 1;
}
return len;
case 0xB: // auth
{
if (!MACEqual(&data[16], (u8*)APMac)) // check BSSID
return 0;
ClientStatus = 1;
Assorted portability enhancements (#1800) * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Change includes of "../types.h" to "types.h" - Makes it easier to directly include these headers in downstream projects * Change an include of "../Wifi.h" to "Wifi.h" * Allow CommonFuncs.cpp to compile on Android * Tidy up some logging calls - Use Platform::Log in LAN_Socket.cpp - Soften some warnings to Debug logs (since they don't necessarily represent problems) * Add Platform::EnterGBAMode - Gracefully stop the emulator if trying to enter GBA mode * Soften some logs that most players won't care about * Soften some more logs * Introduce Platform wrappers for file operations * Fix pointer spacing * Fix more style nits * Log the errno when ftruncate fails * Fix FileSeek offset argument - With an s32 offset, we couldn't access files larger than 2GB * Revise Platform::StopEmu to address feedback - Remove Platform::EnterGBAMode in favor of adding a reason to Platform::StopEmu - Also rename Platform::StopEmu to Platform::SignalStop - Add an optional argument to NDS::Stop - Use the new argument everywhere that the console stops itself * Rename FileGetString to FileReadLine - It conveys the meaning better * Rename FileSeekOrigin::Set to Start - It conveys the meaning better * Change definition of FileGetString to FileReadLine - Oops, almost forgot it * Rename FlushFile to FileFlush - To remain consistent with the other File functions * Add a FileType usage * Fix line break in FileSeekOrigin * Document Platform::DeInit * Clarify that StopReason::Unknown doesn't always mean an error * Move and document FileType::HostFile * Remove Platform::OpenDataFile - Nothing currently uses it * Refactor Platform::OpenFile and Platform::OpenLocalFile to accept a FileMode enum instead of a string - The enum is converted to fopen flags under the hood - The file type is used to decide whether to add the "b" flag - Some helper functions are exposed for the benefit of consistent behavior among frontends - Equivalent behavior is maintained * Fix a tab that should be spaces * Use Windows' 64-bit implementations of fseek/ftell * Move Platform::IsBinaryFile to Platform.cpp - It could vary by frontend * Remove an unused FileType * Rename an enum constant * Document various Platform items * Use Platform::DynamicLibrary to load libandroid - And clean it up at the end * Fix a typo * Pass the correct filetype to FATStorage - Since it can be used for DSI NAND images or for SD cards * Remove Platform::FileType
2023-08-18 20:50:57 +00:00
Log(LogLevel::Debug, "wifiAP: client authenticated\n");
PWRITE_16(p, 0x00B0);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC2(p, (&data[10])); // recv
PWRITE_MAC2(p, APMac); // sender
PWRITE_MAC2(p, APMac); // BSSID
PWRITE_SEQNO(p);
PWRITE_16(p, 0); // auth algorithm (open)
PWRITE_16(p, 2); // auth sequence
PWRITE_16(p, 0); // status code (success)
PacketLen = PLEN(p, base);
RXNum = 1;
}
return len;
case 0xC: // deauth
{
if (!MACEqual(&data[16], (u8*)APMac)) // check BSSID
return 0;
ClientStatus = 0;
Assorted portability enhancements (#1800) * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Introduce some Platform calls for managing dynamic libraries * Add Platform::WriteFATSectors * Change includes of "../types.h" to "types.h" - Makes it easier to directly include these headers in downstream projects * Change an include of "../Wifi.h" to "Wifi.h" * Allow CommonFuncs.cpp to compile on Android * Tidy up some logging calls - Use Platform::Log in LAN_Socket.cpp - Soften some warnings to Debug logs (since they don't necessarily represent problems) * Add Platform::EnterGBAMode - Gracefully stop the emulator if trying to enter GBA mode * Soften some logs that most players won't care about * Soften some more logs * Introduce Platform wrappers for file operations * Fix pointer spacing * Fix more style nits * Log the errno when ftruncate fails * Fix FileSeek offset argument - With an s32 offset, we couldn't access files larger than 2GB * Revise Platform::StopEmu to address feedback - Remove Platform::EnterGBAMode in favor of adding a reason to Platform::StopEmu - Also rename Platform::StopEmu to Platform::SignalStop - Add an optional argument to NDS::Stop - Use the new argument everywhere that the console stops itself * Rename FileGetString to FileReadLine - It conveys the meaning better * Rename FileSeekOrigin::Set to Start - It conveys the meaning better * Change definition of FileGetString to FileReadLine - Oops, almost forgot it * Rename FlushFile to FileFlush - To remain consistent with the other File functions * Add a FileType usage * Fix line break in FileSeekOrigin * Document Platform::DeInit * Clarify that StopReason::Unknown doesn't always mean an error * Move and document FileType::HostFile * Remove Platform::OpenDataFile - Nothing currently uses it * Refactor Platform::OpenFile and Platform::OpenLocalFile to accept a FileMode enum instead of a string - The enum is converted to fopen flags under the hood - The file type is used to decide whether to add the "b" flag - Some helper functions are exposed for the benefit of consistent behavior among frontends - Equivalent behavior is maintained * Fix a tab that should be spaces * Use Windows' 64-bit implementations of fseek/ftell * Move Platform::IsBinaryFile to Platform.cpp - It could vary by frontend * Remove an unused FileType * Rename an enum constant * Document various Platform items * Use Platform::DynamicLibrary to load libandroid - And clean it up at the end * Fix a typo * Pass the correct filetype to FATStorage - Since it can be used for DSI NAND images or for SD cards * Remove Platform::FileType
2023-08-18 20:50:57 +00:00
Log(LogLevel::Debug, "wifiAP: client deauthenticated\n");
PWRITE_16(p, 0x00C0);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC2(p, (&data[10])); // recv
PWRITE_MAC2(p, APMac); // sender
PWRITE_MAC2(p, APMac); // BSSID
PWRITE_SEQNO(p);
PWRITE_16(p, 3); // reason code
PacketLen = PLEN(p, base);
RXNum = 1;
}
return len;
default:
Log(LogLevel::Warn, "wifiAP: unknown management frame type %X\n", (framectl>>4)&0xF);
return 0;
}
}
int WifiAP::SendPacket(const u8* data, int len)
{
if (data[9] != APChannel)
return 0;
data += 12;
u16 framectl = *(u16*)&data[0];
switch ((framectl >> 2) & 0x3)
{
case 0: // management
return HandleManagementFrame(data, len);
case 1: // control
// TODO ???
return 0;
case 2: // data
2017-12-18 22:24:45 +00:00
{
if ((framectl & 0x0300) != 0x0100)
{
Log(LogLevel::Error, "wifiAP: got data frame with bad fromDS/toDS bits %04X\n", framectl);
2017-12-18 22:24:45 +00:00
return 0;
}
// TODO: WFC patch??
if (*(u32*)&data[24] == 0x0003AAAA && *(u16*)&data[28] == 0x0000)
{
if (ClientStatus != 2)
{
Log(LogLevel::Warn, "wifiAP: trying to send shit without being associated\n");
2017-12-18 22:24:45 +00:00
return 0;
}
int lan_len = (len - 30 - 4) + 14;
memcpy(&LANBuffer[0], &data[16], 6); // destination MAC
memcpy(&LANBuffer[6], &data[10], 6); // source MAC
*(u16*)&LANBuffer[12] = *(u16*)&data[30]; // type
memcpy(&LANBuffer[14], &data[32], lan_len - 14);
Platform::Net_SendPacket(LANBuffer, lan_len, UserData);
2017-12-18 22:24:45 +00:00
}
}
return len;
}
return 0;
}
2023-11-04 16:00:12 +00:00
int WifiAP::RecvPacket(u8* data)
{
if (BeaconDue)
{
BeaconDue = false;
// craft beacon
u8* base = data + 12;
u8* p = base;
PWRITE_16(p, 0x0080);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC(p, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF); // recv
PWRITE_MAC2(p, APMac); // sender
PWRITE_MAC2(p, APMac); // BSSID
PWRITE_SEQNO(p);
PWRITE_64(p, USCounter);
PWRITE_16(p, 128); // beacon interval
PWRITE_16(p, 0x0021); // capability
PWRITE_8(p, 0x01); PWRITE_8(p, 0x02); PWRITE_8(p, 0x82); PWRITE_8(p, 0x84); // rates
PWRITE_8(p, 0x03); PWRITE_8(p, 0x01); PWRITE_8(p, APChannel); // current channel
PWRITE_8(p, 0x05); PWRITE_8(p, 0x04); PWRITE_8(p, 0); PWRITE_8(p, 0); PWRITE_8(p, 0); PWRITE_8(p, 0); // TIM
2023-11-04 16:00:12 +00:00
PWRITE_8(p, 0x00); PWRITE_8(p, strlen(APName));
memcpy(p, APName, strlen(APName)); p += strlen(APName);
PALIGN_4(p, base);
PWRITE_32(p, 0xDEADBEEF); // checksum. doesn't matter for now
int len = PLEN(p, base);
p = data;
PWRITE_TXH(p, len, 20);
return len+12;
}
if (RXNum)
{
RXNum = 0;
u8* base = data + 12;
u8* p = base;
memcpy(p, PacketBuffer, PacketLen);
p += PacketLen;
PALIGN_4(p, base);
PWRITE_32(p, 0xDEADBEEF);
int len = PLEN(p, base);
p = data;
PWRITE_TXH(p, len, 20);
return len+12;
}
2017-12-18 22:24:45 +00:00
if (ClientStatus < 2) return 0;
int rxlen = Platform::Net_RecvPacket(LANBuffer, UserData);
while (rxlen > 0)
2017-12-18 22:24:45 +00:00
{
// check destination MAC
if (!MACIsBroadcast(&LANBuffer[0]))
{
2023-11-05 14:38:22 +00:00
if (!MACEqual(&LANBuffer[0], Client->GetMAC()))
{
rxlen = Platform::Net_RecvPacket(LANBuffer, UserData);
continue;
}
}
if (MACEqual(&LANBuffer[6], Client->GetMAC()))
{
rxlen = Platform::Net_RecvPacket(LANBuffer, UserData);
continue;
2017-12-18 22:24:45 +00:00
}
// packet is good
u8* base = data + 12;
u8* p = base;
PWRITE_16(p, 0x0208);
PWRITE_16(p, 0x0000); // duration??
PWRITE_MAC2(p, (&LANBuffer[0])); // recv
PWRITE_MAC2(p, APMac); // BSSID
PWRITE_MAC2(p, (&LANBuffer[6])); // sender
PWRITE_SEQNO(p);
PWRITE_32(p, 0x0003AAAA);
PWRITE_16(p, 0x0000);
PWRITE_16(p, *(u16*)&LANBuffer[12]);
memcpy(p, &LANBuffer[14], rxlen-14); p += rxlen-14;
PALIGN_4(p, base);
PWRITE_32(p, 0xDEADBEEF); // checksum. doesn't matter for now
int len = PLEN(p, base);
p = data;
PWRITE_TXH(p, len, 20);
return len+12;
}
return 0;
}
}