sniffing function added

This commit is contained in:
/jdi/ 2015-09-26 03:48:10 +02:00
parent 3716f9267c
commit 699ff4221d
7 changed files with 57 additions and 24 deletions

View file

@ -24,7 +24,7 @@ Program::~Program() {
int Program::list() { int Program::list() {
printf("List:\n"); printf("List:\n");
Device d = Device(); //Device d = Device();
//printf(" %d\n", d.getName()); //printf(" %d\n", d.getName());
bytes b = { 255, 255, 0, 0 }; bytes b = { 255, 255, 0, 0 };
@ -40,19 +40,12 @@ int Program::list() {
Socket s(io_service); Socket s(io_service);
s.init(dst_port, src_port); s.init(dst_port, src_port);
s.callback = [](Packet a) { s.callback = [](Packet a) {
utils::printSets(a.getPayload()); datasets d =a.getPayload();
printf("%s (%s)\tMAC: ", &d[2].value[0], &d[1].value[0]);
/* utils::printHex(d[3].value);
sleep(1); printf("\tIP: ");
bytes b = {255, 255, 0, 0}; utils::printDec(d[4].value);
Host h = Host(); printf("\n");
p = Packet(Packet::DISCOVERY);
p.setBody(b);
p.setHostMac(h.getMac());
bytes a = p.getBytes();
p.encode(a);
send(a);
*/
return 1; return 1;
}; };
s.send(a); s.send(a);
@ -63,3 +56,29 @@ int Program::list() {
return 1; return 1;
} }
int Program::sniff() {
printf("Listening:\n");
try {
asio::io_service io_service;
Socket s(io_service);
s.init(src_port, dst_port);
s.callback = [](Packet p) {
printf("Receive Head:\t");
utils::printHex(p.getHead());
printf("\nReceive Body:\t");
utils::printHex(p.getBody());
printf("\n");
printf("\n");
return 0;
};
s.listen();
io_service.run();
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 1;
}

View file

@ -13,6 +13,7 @@ public:
Program(); Program();
virtual ~Program(); virtual ~Program();
int list(); int list();
int sniff();
void setPort(int); void setPort(int);
void setPort(); void setPort();
int src_port = 29809; int src_port = 29809;

View file

@ -80,8 +80,10 @@ int main(int argc, char *argv[]) {
optind++; optind++;
if (p.list()) if (p.list())
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
else }else if (strcmp(argv[optind], "sniff") == 0) {
exit(EXIT_FAILURE); optind++;
if (p.sniff())
exit(EXIT_SUCCESS);
} else { } else {
printf("Unknown command: %s\n", argv[optind]); printf("Unknown command: %s\n", argv[optind]);
optind++; optind++;
@ -92,6 +94,7 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
exit(EXIT_FAILURE);
} }

View file

@ -64,7 +64,7 @@ void Packet::parse(bytes data) {
pull(head, i, tokenId); pull(head, i, tokenId);
pull(head, i, checkSum); pull(head, i, checkSum);
if (this->getLength() != checkLen) { if (this->getLength() != checkLen) {
printf("Packet Length doesn't match: %hd != %hd\n", this->getLength(), printf("Packet Length doesn't match: %hd != %hd\n", data.size(),
checkLen); checkLen);
} }
i = 0; i = 0;
@ -76,7 +76,14 @@ void Packet::parse(bytes data) {
pull(body, i, d.value, d.len); pull(body, i, d.value, d.len);
payload[d.type] = d; payload[d.type] = d;
} }
}
const bytes& Packet::getBody() const {
return body;
}
const bytes& Packet::getHead() const {
return head;
} }
void Packet::setBody(bytes data) { void Packet::setBody(bytes data) {

View file

@ -16,7 +16,7 @@
class Packet { class Packet {
public: public:
enum OpCode { enum OpCode {
DISCOVERY, GET, SET DISCOVERY, GET, SET, READ
}; };
Packet(OpCode); Packet(OpCode);
virtual ~Packet(){}; virtual ~Packet(){};
@ -27,6 +27,8 @@ public:
int getCheckSum() const; int getCheckSum() const;
short getSequenceId() const; short getSequenceId() const;
const bytes& getSwitchMac() const; const bytes& getSwitchMac() const;
const bytes& getBody() const;
const bytes& getHead() const;
const datasets& getPayload() const; const datasets& getPayload() const;
void setBody(bytes); void setBody(bytes);
void setHostMac(bytes); void setHostMac(bytes);

View file

@ -55,17 +55,18 @@ void Socket::send(bytes data) {
} }
void Socket::listen() { void Socket::listen() {
receive_socket_.async_receive_from(asio::buffer(data_, MAX_LENGTH), data.resize(MAX_LENGTH);
receive_socket_.async_receive_from(asio::buffer(data, MAX_LENGTH),
remote_endpoint_, remote_endpoint_,
[this](asio::error_code ec, std::size_t bytes_recvd) [this](asio::error_code ec, std::size_t bytes_recvd)
{ {
if (ec || bytes_recvd == 0) { if (ec || bytes_recvd == 0) {
listen(); listen();
} else { } else {
data_.resize(bytes_recvd); data.resize(bytes_recvd);
Packet p = Packet(Packet::DISCOVERY); Packet p = Packet(Packet::READ);
p.encode(data_); p.encode(data);
p.parse(data_); p.parse(data);
datasets l = p.getPayload(); datasets l = p.getPayload();
if(!callback(p)) { if(!callback(p)) {
listen(); listen();

View file

@ -33,7 +33,7 @@ private:
asio::ip::udp::endpoint remote_endpoint_; asio::ip::udp::endpoint remote_endpoint_;
asio::ip::udp::endpoint wildcard_endpoint_; asio::ip::udp::endpoint wildcard_endpoint_;
asio::ip::udp::endpoint local_endpoint_; asio::ip::udp::endpoint local_endpoint_;
bytes data_ = bytes(MAX_LENGTH); bytes data = bytes(MAX_LENGTH);
}; };