Compare commits

..

No commits in common. "4f1cb82f461a0b2d638fc4ea2a8f055361331b43" and "f1e8069afd5c12a67927c20f85247c6c603475de" have entirely different histories.

10 changed files with 39 additions and 171 deletions

View File

@ -4,15 +4,8 @@
#include "./packet.hpp"
#include <string>
class BeaconPacket : public Packet{
public:
BeaconPacket()
{}
BeaconPacket(const Packet &packet)
: Packet(packet)
{}
std::string ssid;
class BeaconPacket : Packet{
std::string SSID;
};
#endif /* FDDB997A_BCD3_4056_BFEA_9FF6A548DACF */

View File

@ -15,18 +15,6 @@ enum PacketType {
NoData,
Unknown
};
const std::array<const char*, 10> PACKET_TYPE_NAMES({{
"Beacon",
"Probe Request",
"Probe Response",
"Data",
"Request to send",
"Clear to send",
"Acknowledgment",
"BlockAcknowledgment",
"NoData",
"Unknown"
}});
struct Packet {
uint64_t timestampMicros;

View File

@ -4,15 +4,8 @@
#include "./packet.hpp"
#include <string>
class ProbeRequestPacket : public Packet{
public:
ProbeRequestPacket()
{}
ProbeRequestPacket(const Packet &packet)
: Packet(packet)
{}
std::string requestSsid;
class ProbeRequestPacket : Packet{
std::string requestSSID;
};
#endif /* CD2BF199_8153_4F10_A85C_50883FAD66A8 */

View File

@ -1,18 +0,0 @@
#ifndef B199B4B3_BE27_4F0C_8DBE_5E78580AB1A9
#define B199B4B3_BE27_4F0C_8DBE_5E78580AB1A9
#include "./packet.hpp"
#include <string>
class ProbeResponsePacket : public Packet{
public:
ProbeResponsePacket()
{}
ProbeResponsePacket(const Packet &packet)
: Packet(packet)
{}
std::string responseSsid;
};
#endif /* B199B4B3_BE27_4F0C_8DBE_5E78580AB1A9 */

17
handler/asyncHandler.hpp Normal file
View File

@ -0,0 +1,17 @@
#ifndef EFFCCB40_3639_4BD4_9649_302F05987909
#define EFFCCB40_3639_4BD4_9649_302F05987909
#include <future>
#include <string.h>
#include "bufHandler.hpp"
void asyncHandler(char *buf){
// Create a copy of buf for our thread
char bufCopy[265];
strcpy(bufCopy, buf);
// \/ Surpress unused warning
(void)std::async(std::launch::async, bufHandler, bufCopy);
}
#endif /* EFFCCB40_3639_4BD4_9649_302F05987909 */

View File

@ -1,28 +1,22 @@
#ifndef C251BA62_6D80_4033_86B6_61F184E6F250
#define C251BA62_6D80_4033_86B6_61F184E6F250
#include <future>
#include <string>
#include "textPacketHandler.hpp"
using namespace std::string_literals;
std::vector<std::string> buffer;
void bufHandler(const char *buf){
std::string line = buf;
// Remove last char which is \n
line = line.substr(0, line.size()-1);
void bufHandler(char *buf){
// When first char of buf has text (no tab), we got a new packet
if(buf[0] != '\t'){
// Submit the just-read text-packet in a new thread
if(buffer.size() != 0) {
(void)std::async(std::launch::async, textPacketHandler, buffer);
}
buffer = {line};
// Submit the just-read text-packet
if(buffer.size() != 0) textPacketHandler(buffer);
buffer = std::vector<std::string>();
}
else
buffer.push_back(line); // Append part-packet
// Append part-packet
buffer.push_back(buf);
}
#endif /* C251BA62_6D80_4033_86B6_61F184E6F250 */

View File

@ -3,20 +3,16 @@
#include <string>
#include "../DTO/packet.hpp"
#include "../DTO/beaconPacket.hpp"
#include "../DTO/probeRequestPacket.hpp"
#include "../DTO/probeResponsePacket.hpp"
#include <vector>
#include <sstream>
#include <locale>
#include <iomanip>
#include "../helper/string-helper.hpp"
#include "../helper/split.hpp"
#include "../helper/timestampConvert.hpp"
#include "../helper/find.hpp"
#include "../helper/vector-stats.hpp"
#include <unordered_map>
using namespace std::string_literals;
const std::unordered_map<std::string, PacketType> PACKET_TYPE_MAP({
{"Beacon", PacketType::Beacon},
{"Probe Request", PacketType::ProbeRequest},
@ -28,20 +24,11 @@ const std::unordered_map<std::string, PacketType> PACKET_TYPE_MAP({
{"BA", PacketType::BlockAcknowledgment}
});
void parseHeader(Packet &packet, const std::vector<std::string> &textPacket);
void parsePayload(Packet &packet, const std::vector<std::string> &textPacket);
void textPacketHandler(const std::vector<std::string> textPacket){
void textPacketHandler(std::vector<std::string> textPacket){
/// Here we have to parse the packet
// Create empty packet
Packet packet;
parseHeader(packet, textPacket);
parsePayload(packet, textPacket);
}
void parseHeader(Packet &packet, const std::vector<std::string> &textPacket){
const std::string textHeader = textPacket[0];
const std::vector<std::string> headerData = split(textHeader, ' ');
@ -57,14 +44,8 @@ void parseHeader(Packet &packet, const std::vector<std::string> &textPacket){
packet.frequency = std::stoi(headerData[frequencyIndex]);
int signalIndex = findIs(headerData, "signal", 1, 1);
if(signalIndex != -1){
std::string signalText = headerData[signalIndex].substr(0, 3);
packet.signal = std::stoi(signalText);
}
else {
fprintf(stderr, "Missing signal-data!\n");
packet.signal = -100;
}
// Addresses seem complicated at first, but just have many fields which might be available.
// SA and DA are src- and dst-Addresses
@ -81,8 +62,8 @@ void parseHeader(Packet &packet, const std::vector<std::string> &textPacket){
int bssidIndex = findContains(headerData, "BSSID:", 1);
std::string bssidAddr = (bssidIndex != -1) ? headerData[bssidIndex].substr("BSSID:"s.length()) : "";
int taIndex = findContains(headerData, "TA:", 1);
std::string tAddr = (taIndex != -1) ? headerData[taIndex].substr("TA:"s.length()) : "";
int taIndex = findContains(headerData, "SA:", 1);
std::string tAddr = (taIndex != -1) ? headerData[taIndex].substr("SA:"s.length()) : "";
int raIndex = findContains(headerData, "RA:", 1);
std::string rAddr = (raIndex != -1) ? headerData[raIndex].substr("RA:"s.length()) : "";
@ -111,72 +92,10 @@ void parseHeader(Packet &packet, const std::vector<std::string> &textPacket){
// If type is in map, use map-value, otherwise keep default
if(PACKET_TYPE_MAP.find(textType) != PACKET_TYPE_MAP.end())
type = PACKET_TYPE_MAP.at(textType);
if(type == PacketType::Unknown){
fprintf(stderr, "Unknown package-type: %s\n", textType.c_str());
}
type = PACKET_TYPE_MAP[textType];
}
packet.type = type;
// Read data for specializations
if(type == PacketType::Beacon){
// Create BeaconPacket from packet
BeaconPacket beaconPacket = BeaconPacket(packet);
packet = beaconPacket; // Overwrite packet
// Find ssid
int start = textHeader.find('(')+1;
std::string ssid = textHeader.substr(start, textHeader.find(')')-start);
// Write to packet
beaconPacket.ssid = ssid;
}
else if (type == PacketType::ProbeRequest){
// Create ProbeRequestPacket from packet
ProbeRequestPacket probeRequestPacket = ProbeRequestPacket(packet);
packet = probeRequestPacket; // Overwrite packet
// Find probe-request
int start = textHeader.find('(')+1;
std::string requestSsid = textHeader.substr(start, textHeader.find(')')-start);
// Write to packet
probeRequestPacket.requestSsid = requestSsid;
}
else if (type == PacketType::ProbeResponse){
// Create ProbeResponsePacket from packet
ProbeResponsePacket probeResponsePacket = ProbeResponsePacket(packet);
packet = probeResponsePacket; // Overwrite packet
// Find probe-request
int start = textHeader.find('(')+1;
std::string responseSsid = textHeader.substr(start, textHeader.find(')')-start);
// Write to packet
probeResponsePacket.responseSsid = responseSsid;
}
}
void parsePayload(Packet &packet, const std::vector<std::string> &textPacket){
// Expect max of 16byte per line of payload
unsigned int payloadSize = 16*(textPacket.size()-1);
// Go through last line
int line = textPacket.size()-1, charPos;
for(int f=0; f<8*2; ++f){
charPos = 10 + (f/2.0*5);
if(textPacket[line][charPos] == ' ') { // When our char is space, no more data is present
// Set size
payloadSize = 16*(textPacket.size()-2)+f;
break;
}
}
packet.payloadSize = payloadSize;
//
}
#endif /* EE781A91_6D07_47AC_B3C4_F99E29F3731F */

View File

@ -8,8 +8,8 @@
/// @param cmd is the command
/// @param handler is the handler(char*)-function
/// @return Return-code form command
int exec(const char* cmd, void (*handler)(const char*) = nullptr){
const int buf_size = 512;
int exec(const char* cmd, void (*handler)(char*) = nullptr){
const int buf_size = 256;
char buf[buf_size];
// Open execution-pipe

View File

@ -1,10 +1,6 @@
#ifndef F7CFE6A7_34BF_4E04_94CF_DB8374980631
#define F7CFE6A7_34BF_4E04_94CF_DB8374980631
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
@ -17,18 +13,4 @@ std::vector<std::string> split(const std::string& s, char delimiter)
return tokens;
}
char hex_char_to_int(const char &c) {
unsigned char result = 0;
if( ('0' <= c) && (c <= '9') ) {
result = c - '0';
}
else if( ('A' <= c) && (c <= 'F') ) {
result = 10 + c - 'A';
}
else if( ('a' <= c) && (c <= 'f') ) {
result = 10 + c - 'a';
}
return result;
}
#endif /* F7CFE6A7_34BF_4E04_94CF_DB8374980631 */

View File

@ -1,7 +1,7 @@
#include <stdio.h>
#include <string>
#include "./helper/exec.hpp"
#include "./handler/bufHandler.hpp"
#include "./handler/asyncHandler.hpp"
const std::string tcpdump_baseCmd = "tcpdump -vvv -e -n -X -s0 -i ";
@ -15,7 +15,7 @@ int main(int argc, char *args[]){
exit(1);
}
int exitCode = exec(tcpdump_cmd.c_str(), &bufHandler);
int exitCode = exec(tcpdump_cmd.c_str(), &asyncHandler);
if(exitCode){
fprintf(stderr, "\ntcpdump exited with non-zero ExitCode: %d\n Something went wrong! Check tcpdump-output for more information.\n", exitCode);