Commit 699ff422 authored by /jdi/'s avatar /jdi/
Browse files

sniffing function added

parent 3716f926
......@@ -24,7 +24,7 @@ Program::~Program() {
int Program::list() {
printf("List:\n");
Device d = Device();
//Device d = Device();
//printf(" %d\n", d.getName());
bytes b = { 255, 255, 0, 0 };
......@@ -40,19 +40,12 @@ int Program::list() {
Socket s(io_service);
s.init(dst_port, src_port);
s.callback = [](Packet a) {
utils::printSets(a.getPayload());
/*
sleep(1);
bytes b = {255, 255, 0, 0};
Host h = Host();
p = Packet(Packet::DISCOVERY);
p.setBody(b);
p.setHostMac(h.getMac());
bytes a = p.getBytes();
p.encode(a);
send(a);
*/
datasets d =a.getPayload();
printf("%s (%s)\tMAC: ", &d[2].value[0], &d[1].value[0]);
utils::printHex(d[3].value);
printf("\tIP: ");
utils::printDec(d[4].value);
printf("\n");
return 1;
};
s.send(a);
......@@ -63,3 +56,29 @@ int Program::list() {
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;
}
......@@ -13,6 +13,7 @@ public:
Program();
virtual ~Program();
int list();
int sniff();
void setPort(int);
void setPort();
int src_port = 29809;
......
......@@ -80,8 +80,10 @@ int main(int argc, char *argv[]) {
optind++;
if (p.list())
exit(EXIT_SUCCESS);
else
exit(EXIT_FAILURE);
}else if (strcmp(argv[optind], "sniff") == 0) {
optind++;
if (p.sniff())
exit(EXIT_SUCCESS);
} else {
printf("Unknown command: %s\n", argv[optind]);
optind++;
......@@ -92,6 +94,7 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
}
exit(EXIT_FAILURE);
}
......@@ -64,7 +64,7 @@ void Packet::parse(bytes data) {
pull(head, i, tokenId);
pull(head, i, checkSum);
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);
}
i = 0;
......@@ -76,7 +76,14 @@ void Packet::parse(bytes data) {
pull(body, i, d.value, d.len);
payload[d.type] = d;
}
}
const bytes& Packet::getBody() const {
return body;
}
const bytes& Packet::getHead() const {
return head;
}
void Packet::setBody(bytes data) {
......
......@@ -16,7 +16,7 @@
class Packet {
public:
enum OpCode {
DISCOVERY, GET, SET
DISCOVERY, GET, SET, READ
};
Packet(OpCode);
virtual ~Packet(){};
......@@ -27,6 +27,8 @@ public:
int getCheckSum() const;
short getSequenceId() const;
const bytes& getSwitchMac() const;
const bytes& getBody() const;
const bytes& getHead() const;
const datasets& getPayload() const;
void setBody(bytes);
void setHostMac(bytes);
......
......@@ -55,17 +55,18 @@ void Socket::send(bytes data) {
}
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_,
[this](asio::error_code ec, std::size_t bytes_recvd)
{
if (ec || bytes_recvd == 0) {
listen();
} else {
data_.resize(bytes_recvd);
Packet p = Packet(Packet::DISCOVERY);
p.encode(data_);
p.parse(data_);
data.resize(bytes_recvd);
Packet p = Packet(Packet::READ);
p.encode(data);
p.parse(data);
datasets l = p.getPayload();
if(!callback(p)) {
listen();
......
......@@ -33,7 +33,7 @@ private:
asio::ip::udp::endpoint remote_endpoint_;
asio::ip::udp::endpoint wildcard_endpoint_;
asio::ip::udp::endpoint local_endpoint_;
bytes data_ = bytes(MAX_LENGTH);
bytes data = bytes(MAX_LENGTH);
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment