hook LAN shito. open proper pcap device. etc

This commit is contained in:
Arisotura
2019-02-21 03:44:22 +01:00
parent f9822cdd44
commit 0550d22833
3 changed files with 75 additions and 52 deletions

View File

@ -24,6 +24,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <pcap/pcap.h> #include <pcap/pcap.h>
#include "LAN.h" #include "LAN.h"
#include "../Config.h"
#ifdef __WIN32__ #ifdef __WIN32__
#include <iphlpapi.h> #include <iphlpapi.h>
@ -243,23 +244,28 @@ bool Init()
#endif // __WIN32__ #endif // __WIN32__
printf("devices: %d\n", NumAdapters); // open pcap device
dev = (pcap_if_t*)Adapters[0].Internal;
for (int i = 0; i < NumAdapters; i++) for (int i = 0; i < NumAdapters; i++)
{ {
AdapterData* zog = &Adapters[i]; if (!strncmp(Adapters[i].DeviceName, Config::LANDevice, 128))
dev = (pcap_if_t*)Adapters[i].Internal;
}
printf("%s:\n", ((pcap_if_t*)zog->Internal)->name); PCapAdapter = pcap_open_live(dev->name, 2048, PCAP_OPENFLAG_PROMISCUOUS, 1, errbuf);
if (!PCapAdapter)
{
printf("PCap: failed to open adapter\n");
return false;
}
printf("* %s\n", zog->FriendlyName); pcap_freealldevs(alldevs);
printf("* %s\n", zog->Description);
printf("* "); for (int j = 0; j < 6; j++) printf("%02X:", zog->MAC[j]); printf("\n"); if (pcap_setnonblock(PCapAdapter, 1, errbuf) < 0)
printf("* "); for (int j = 0; j < 4; j++) printf("%d.", zog->IP_v4[j]); printf("\n"); {
printf("PCap: failed to set nonblocking mode\n");
for (int k = 0; k < 8; k++) pcap_close(PCapAdapter); PCapAdapter = NULL;
{ return false;
printf("* "); for (int j = 0; j < 4; j++) printf("%d.", zog->DNS[k][j]); printf("\n");
}
} }
return true; return true;
@ -280,4 +286,53 @@ void DeInit()
} }
} }
void RXCallback(u_char* blarg, const struct pcap_pkthdr* header, const u_char* data)
{
while (PCapRXNum > 0);
if (header->len > 2048-64) return;
PCapPacketLen = header->len;
memcpy(PCapPacketBuffer, data, PCapPacketLen);
PCapRXNum = 1;
}
int SendPacket(u8* data, int len)
{
if (PCapAdapter == NULL)
return 0;
if (len > 2048)
{
printf("LAN_SendPacket: error: packet too long (%d)\n", len);
return 0;
}
if (!Config::DirectLAN)
{
// TODO!
}
pcap_sendpacket(PCapAdapter, data, len);
// TODO: check success
return len;
}
int RecvPacket(u8* data)
{
if (PCapAdapter == NULL)
return 0;
int ret = 0;
if (PCapRXNum > 0)
{
memcpy(data, PCapPacketBuffer, PCapPacketLen);
ret = PCapPacketLen;
PCapRXNum = 0;
}
pcap_dispatch(PCapAdapter, 1, RXCallback, NULL);
return ret;
}
} }

View File

@ -45,6 +45,9 @@ extern int NumAdapters;
bool Init(); bool Init();
void DeInit(); void DeInit();
int SendPacket(u8* data, int len);
int RecvPacket(u8* data);
} }
#endif // LAN_H #endif // LAN_H

View File

@ -22,6 +22,7 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "../Platform.h" #include "../Platform.h"
#include "../Config.h" #include "../Config.h"
#include "LAN.h"
#ifdef __WIN32__ #ifdef __WIN32__
#include <winsock2.h> #include <winsock2.h>
@ -260,59 +261,23 @@ int MP_RecvPacket(u8* data, bool block)
bool LAN_Init() bool LAN_Init()
{ {
// welp if (!LAN::Init()) return false;
return true; return true;
} }
void LAN_DeInit() void LAN_DeInit()
{ {
// LAN::DeInit();
} }
int LAN_SendPacket(u8* data, int len) int LAN_SendPacket(u8* data, int len)
{ {
/*if (PCapAdapter == NULL) return LAN::SendPacket(data, len);
return 0;
if (len > 2048)
{
printf("LAN_SendPacket: error: packet too long (%d)\n", len);
return 0;
}
pcap_sendpacket(PCapAdapter, data, len);
// TODO: check success
return len;*/
return len;
} }
/*void LAN_RXCallback(u_char* blarg, const struct pcap_pkthdr* header, const u_char* data)
{
while (PCapRXNum > 0);
if (header->len > 2048-64) return;
PCapPacketLen = header->len;
memcpy(PCapPacketBuffer, data, PCapPacketLen);
PCapRXNum = 1;
}*/
int LAN_RecvPacket(u8* data) int LAN_RecvPacket(u8* data)
{ {
/*if (PCapAdapter == NULL) return LAN::RecvPacket(data);
return 0;
int ret = 0;
if (PCapRXNum > 0)
{
memcpy(data, PCapPacketBuffer, PCapPacketLen);
ret = PCapPacketLen;
PCapRXNum = 0;
}
pcap_dispatch(PCapAdapter, 1, LAN_RXCallback, NULL);
return ret;*/
return 0;
} }