print discovered devices

This commit is contained in:
/jdi/ 2015-09-25 16:40:16 +02:00
parent 5795bc1d32
commit 1ec3b6c4f0
6 changed files with 57 additions and 39 deletions

View file

@ -21,9 +21,11 @@ Program::~Program() {
// TODO Auto-generated destructor stub
}
int Program::run() {
int Program::list() {
printf("List:\n");
Device d = Device();
printf(" %d\n", d.getName());
//printf(" %d\n", d.getName());
bytes b = { 255, 255, 0, 0 };
Host h = Host();
@ -45,7 +47,3 @@ int Program::run() {
return 1;
}
int Program::discover() {
return 0;
}

View file

@ -12,13 +12,11 @@ class Program {
public:
Program();
virtual ~Program();
int run();
int list();
void setPort(int);
void setPort();
int src_port = 29809;
int dst_port = 29808;
private:
int discover();
};
#endif /* PROGRAM_H_ */

View file

@ -9,6 +9,7 @@
#define TYPES_H_
#include <vector>
#include <map>
template <typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B)
@ -37,6 +38,7 @@ struct dataset{
bytes value;
};
typedef std::vector<dataset> datasets;
//typedef std::vector<dataset> datasets;
typedef std::map<short,dataset> datasets;
#endif /* TYPES_H_ */

View file

@ -33,8 +33,8 @@ int main(int argc, char *argv[]) {
const struct option longopts[] = { { "version", no_argument, 0, 'v' }, {
"help", no_argument, 0, 'h' },
{ "port", required_argument, 0, 'p' },
{ "srcport", required_argument, 0, 's' },
{ "port", required_argument, 0, 'p' }, { "srcport",
required_argument, 0, 's' },
{ "dstport", required_argument, 0, 'p' }, { 0, 0, 0, 0 }, };
Program p = Program();
@ -75,18 +75,22 @@ int main(int argc, char *argv[]) {
exit(EXIT_FAILURE);
}
while (optind < argc) {
if (optind < argc) {
if (strcmp(argv[optind], "list") == 0) {
printf("List:\n\tA B C\n");
}else {
printf("->%s\n", argv[optind]);
if (p.list())
exit(EXIT_SUCCESS);
else
exit(EXIT_FAILURE);
} else {
printf("Unknown Command: %s\n", argv[optind]);
while (optind < argc) {
printf("->%s\n", argv[optind]);
optind++;
}
exit(EXIT_FAILURE);
}
optind++;
}
if (p.run())
exit(EXIT_SUCCESS);
else
exit(EXIT_FAILURE);
}

View file

@ -37,7 +37,9 @@ bytes Packet::getBytes() {
push(head, i, tokenId);
push(head, i, checkSum);
bytes data = head + body;
utils::printBytes("Send Head:\t",head);
//printf("Send Header:\t");
//utils::printHex(head);
//printf("\n");
return data;
}
@ -45,7 +47,9 @@ void Packet::parse(bytes data) {
memcpy(&head[0], &data[0], HEADER_LEN);
body.resize(data.size() - HEADER_LEN);
memcpy(&body[0], &data[HEADER_LEN], data.size() - HEADER_LEN);
utils::printBytes("Receive Head:\t",head);
//printf("Receive Header:\t");
//utils::printHex(head);
//printf("\n");
int i = 0;
short checkLen = 0x0;
pull(head, i, version);
@ -59,13 +63,20 @@ void Packet::parse(bytes data) {
pull(head, i, flag);
pull(head, i, tokenId);
pull(head, i, checkSum);
utils::printBytes("MAC: ", switchMac);
utils::printBytes("MAC: ", hostMac);
if(this->getLength()!= checkLen){
printf("Packet Length doesn't match: %hd != %hd\n", this->getLength(), checkLen);
if (this->getLength() != checkLen) {
printf("Packet Length doesn't match: %hd != %hd\n", this->getLength(),
checkLen);
}
i = 0;
dataset d;
payload = {};
while (i < (int) body.size() - 4) {
pull(body, i, d.type);
pull(body, i, d.len);
pull(body, i, d.value, d.len);
payload[d.type] = d;
}
}
void Packet::setBody(bytes data) {
@ -184,13 +195,13 @@ void Packet::pull(bytes &arr, int &index, byte &ret) {
void Packet::pull(bytes &arr, int &index, bytes &ret, unsigned len) {
ret.resize(len);
memcpy(&ret[0], &arr[index], len);
index+=len;
index += len;
}
void Packet::pull(bytes &arr, int &index, short &ret) {
ret = (arr[index++] << 8);
ret |= arr[index++] & 0xFF;
ret &=0xFFFF;
ret &= 0xFFFF;
}
void Packet::pull(bytes &arr, int &index, int &ret) {

View file

@ -15,6 +15,7 @@
#include <asio.hpp>
#include "Socket.h"
#include "../Types.h"
#include "../Utils.h"
#include "../device/Host.h"
#include "Packet.h"
@ -65,20 +66,24 @@ void Socket::listen() {
Packet p = Packet(Packet::DISCOVERY);
p.encode(data_);
p.parse(data_);
datasets l = p.getPayload();
utils::printSets(l);
/*
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);
*/
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);
*/
listen();
}
});
printf("Listen\n");
//printf("Listen\n");
}