From 671a9df9d7763e17edc0504328c06d8f8be11c70 Mon Sep 17 00:00:00 2001 From: /jdi/ Date: Sat, 26 Sep 2015 14:02:24 +0200 Subject: [PATCH] sniffing function added --- src/Program.cpp | 3 +-- src/Utils.h | 34 ++++++++++++++++++++++++++++++++++ src/transfer/Packet.cpp | 7 ++++++- src/transfer/Packet.h | 11 ++++++----- src/transfer/Socket.cpp | 3 --- 5 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/Utils.h diff --git a/src/Program.cpp b/src/Program.cpp index 1e18d9b..ba23ac4 100644 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -27,11 +27,10 @@ int Program::list() { //Device d = Device(); //printf(" %d\n", d.getName()); - bytes b = { 255, 255, 0, 0 }; Host h = Host(); Packet p = Packet(Packet::DISCOVERY); - p.setBody(b); p.setHostMac(h.getMac()); + p.setPayload({}); bytes a = p.getBytes(); p.encode(a); diff --git a/src/Utils.h b/src/Utils.h new file mode 100644 index 0000000..733b746 --- /dev/null +++ b/src/Utils.h @@ -0,0 +1,34 @@ +/* + * Utils.h + * + * Created on: 24.09.2015 + * Author: jdi + */ + +#ifndef UTILS_H_ +#define UTILS_H_ + +#include +#include +#include "Types.h" + +namespace utils { + +static void printHex(bytes d) { + if (d.size() > 0) + printf("%.2X", d[0]); + for (unsigned i = 1; i < d.size(); i++) { + printf(":%.2X", d[i]); + } +} + +static void printDec(bytes d) { + if (d.size() > 0) + printf("%.1d", d[0]); + for (unsigned i = 1; i < d.size(); i++) { + printf(".%.1d", d[i]); + } +} + +} +#endif /* UTILS_H_ */ diff --git a/src/transfer/Packet.cpp b/src/transfer/Packet.cpp index 519066d..49c3a8e 100644 --- a/src/transfer/Packet.cpp +++ b/src/transfer/Packet.cpp @@ -163,7 +163,12 @@ void Packet::encode(bytes &data) { } void Packet::push(bytes &arr, int &index, byte data) { - arr[index++] = data; + if (arr.size() > index) { + arr[index++] = data; + } else { + arr.push_back(data); + index++; + } } void Packet::push(bytes &arr, int &index, bytes data) { diff --git a/src/transfer/Packet.h b/src/transfer/Packet.h index 461177c..d0dd19c 100644 --- a/src/transfer/Packet.h +++ b/src/transfer/Packet.h @@ -9,7 +9,7 @@ #define PACKET_H_ #define HEADER_LEN 32 -#define PACKET_END -65536 +#define PACKET_END 0xFFFF0000 #include "../Types.h" @@ -19,11 +19,12 @@ public: DISCOVERY, GET, SET, READ }; Packet(OpCode); - virtual ~Packet(){}; + virtual ~Packet() { + } void encode(bytes&); bytes getBytes(); void parse(bytes); - short getLength() const ; + short getLength() const; int getCheckSum() const; short getSequenceId() const; const bytes& getSwitchMac() const; @@ -39,13 +40,13 @@ public: private: bytes head = bytes(32); - bytes body; + bytes body = bytes(0); datasets payload; byte version = 1; byte opCode; bytes switchMac = { 0, 0, 0, 0, 0, 0 }; - bytes hostMac = { 0, 0, 0, 0, 0, 0 };// TODO set Mac + bytes hostMac = { 0, 0, 0, 0, 0, 0 }; // TODO set Mac bytes broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; short sequenceId; short tokenId = 0; diff --git a/src/transfer/Socket.cpp b/src/transfer/Socket.cpp index 36bedef..74984fe 100644 --- a/src/transfer/Socket.cpp +++ b/src/transfer/Socket.cpp @@ -73,7 +73,4 @@ void Socket::listen() { } } }); - - //printf("Listen\n"); - }