diff options
author | StapleButter <thetotalworm@gmail.com> | 2017-05-11 23:43:57 +0200 |
---|---|---|
committer | StapleButter <thetotalworm@gmail.com> | 2017-05-11 23:43:57 +0200 |
commit | 01361f006e9ef6d8fd61e95a8bc3f1ac4d749903 (patch) | |
tree | 782aa4caac433147eee726001f866fab8922b57d | |
parent | e9a24b4d06d59a9d8cdcfc9be396c9df105f7408 (diff) |
(untested yet) code for sending and receiving packets via sockets
MAC randomization
-rw-r--r-- | src/Platform.h | 2 | ||||
-rw-r--r-- | src/SPI.cpp | 16 | ||||
-rw-r--r-- | src/wx/Platform.cpp | 71 |
3 files changed, 89 insertions, 0 deletions
diff --git a/src/Platform.h b/src/Platform.h index 6f2e252..91eb947 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -26,6 +26,8 @@ namespace Platform bool MP_Init(); void MP_DeInit(); +int MP_SendPacket(u8* data, int len, int rate); +int MP_RecvPacket(u8* data, bool block, int* rate); } diff --git a/src/SPI.cpp b/src/SPI.cpp index 2c88197..0e7ac6d 100644 --- a/src/SPI.cpp +++ b/src/SPI.cpp @@ -18,6 +18,7 @@ #include <stdio.h> #include <string.h> +#include <stdlib.h> #include "NDS.h" #include "SPI.h" @@ -138,6 +139,21 @@ void Reset() *(u16*)&Firmware[userdata+0x72] = CRC16(&Firmware[userdata], 0x70, 0xFFFF); + // replace MAC address with random address + // TODO: make optional? + Firmware[0x36] = 0x00; + Firmware[0x37] = 0x09; + Firmware[0x38] = 0xBF; + Firmware[0x39] = rand()&0xFF; + Firmware[0x3A] = rand()&0xFF; + Firmware[0x3B] = rand()&0xFF; + + printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\n", + Firmware[0x36], Firmware[0x37], Firmware[0x38], + Firmware[0x39], Firmware[0x3A], Firmware[0x3B]); + + *(u16*)&Firmware[0x2A] = CRC16(&Firmware[0x2C], *(u16*)&Firmware[0x2C], 0x0000); + // verify shit printf("FW: WIFI CRC16 = %s\n", VerifyCRC16(0x0000, 0x2C, *(u16*)&Firmware[0x2C], 0x2A)?"GOOD":"BAD"); printf("FW: AP1 CRC16 = %s\n", VerifyCRC16(0x0000, 0x7FA00&FirmwareMask, 0xFE, 0x7FAFE&FirmwareMask)?"GOOD":"BAD"); diff --git a/src/wx/Platform.cpp b/src/wx/Platform.cpp index 697ecf3..71236af 100644 --- a/src/wx/Platform.cpp +++ b/src/wx/Platform.cpp @@ -48,6 +48,9 @@ namespace Platform socket_t MPSocket; sockaddr_t MPSendAddr; +u8 PacketBuffer[2048]; + +#define NIFI_VER 1 bool MP_Init() @@ -114,5 +117,73 @@ void MP_DeInit() #endif // __WXMSW__ } +int MP_SendPacket(u8* data, int len, int rate) +{ + if (MPSocket < 0) + return 0; + + if (len > 2048-8) + { + printf("MP_SendPacket: error: packet too long (%d)\n", len); + return 0; + } + + *(u32*)&PacketBuffer[0] = htonl(0x4946494E); // NIFI + PacketBuffer[4] = NIFI_VER; + PacketBuffer[5] = rate & 0xFF; + *(u16*)&PacketBuffer[6] = htons(len); + memcpy(&PacketBuffer[8], data, len); + + return sendto(MPSocket, (const char*)PacketBuffer, len+8, 0, &MPSendAddr, sizeof(sockaddr_t)); +} + +int MP_RecvPacket(u8* data, bool block, int* rate) +{ + if (MPSocket < 0) + return 0; + + fd_set fd; + struct timeval tv; + + FD_ZERO(&fd); + FD_SET(MPSocket, &fd); + tv.tv_sec = 0; + tv.tv_usec = 0; + + if (!select(1, &fd, 0, 0, &tv)) + { + return 0; + } + + sockaddr_t fromAddr; + socklen_t fromLen = sizeof(sockaddr_t); + int rlen = recvfrom(MPSocket, (char*)PacketBuffer, 2048, 0, &fromAddr, &fromLen); + if (rlen < 8+24) + { + return 0; + } + rlen -= 8; + + if (ntohl(*(u32*)&PacketBuffer[0]) != 0x4946494E) + { + return 0; + } + + if (PacketBuffer[4] != NIFI_VER) + { + return 0; + } + + rlen -= 8; + if (ntohs(*(u16*)&PacketBuffer[6]) != rlen) + { + return 0; + } + + memcpy(data, &PacketBuffer[8], rlen); + if (rate) *rate = PacketBuffer[5]; + return rlen; +} + } |