Program.cpp 2.68 KB
Newer Older
/jdi/'s avatar
basics  
/jdi/ committed
1
2
3
4
5
6
/*
 * Program.cpp
 *
 *  Created on: 04.09.2015
 *      Author: jdi
 */
/jdi/'s avatar
/jdi/ committed
7
8
#include <cstdio>
#include <algorithm>
/jdi/'s avatar
basics  
/jdi/ committed
9

/jdi/'s avatar
/jdi/ committed
10
#include "Options.h"
/jdi/'s avatar
basics  
/jdi/ committed
11
#include "Program.h"
/jdi/'s avatar
/jdi/ committed
12
13
#include "Host.h"
#include "Socket.h"
/jdi/'s avatar
/jdi/ committed
14
#include "Switch.h"
/jdi/'s avatar
/jdi/ committed
15
#include "Packet.h"
/jdi/'s avatar
basics  
/jdi/ committed
16

/jdi/'s avatar
/jdi/ committed
17
18
int Program::list() {

/jdi/'s avatar
/jdi/ committed
19
	//Device d = Device();
/jdi/'s avatar
/jdi/ committed
20
	//printf(" %d\n", d.getName());
/jdi/'s avatar
basics  
/jdi/ committed
21
22

	Host h = Host();
/jdi/'s avatar
/jdi/ committed
23
	printf("List:\n");
/jdi/'s avatar
basics  
/jdi/ committed
24
25
	Packet p = Packet(Packet::DISCOVERY);
	p.setHostMac(h.getMac());
/jdi/'s avatar
/jdi/ committed
26
	p.setPayload( { });
/jdi/'s avatar
basics  
/jdi/ committed
27
28
29
30
31
	bytes a = p.getBytes();
	p.encode(a);

	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
32
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
33
		s.setHostIp(h.getIp());
/jdi/'s avatar
/jdi/ committed
34
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
35
		s.callback = [](Packet a) {
/jdi/'s avatar
/jdi/ committed
36
			if (options.flags & FLAG_HEADER) {
/jdi/'s avatar
/jdi/ committed
37
				std::cout <<"Received Header:\t"<< a.getHead() <<"\n";
/jdi/'s avatar
/jdi/ committed
38
39
			}
			if (options.flags & FLAG_HEX) {
/jdi/'s avatar
/jdi/ committed
40
				std::cout <<"Received Payload:\t"<<a.getBody()<<"\n";
/jdi/'s avatar
/jdi/ committed
41
			}
/jdi/'s avatar
/jdi/ committed
42
			datasets d =a.getPayload();
/jdi/'s avatar
/jdi/ committed
43
44
45
46
47
			std::cout <<"\t"<<d[2].value <<"("<< d[1].value<<")\tMAC: "<<d[3].value<<"\tIP: "<<d[4].value<<"\n";
			Switch s = Switch();
			std::cout <<"\t"<<s.settings.hostname<<"("<< s.device.type<<")\tMAC: "<<s.device.mac<<"\tIP: "<<s.settings.ip_addr<<"\n";
				return 1;
			};
/jdi/'s avatar
basics  
/jdi/ committed
48
49
50
51
52
53
54
55
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}
/jdi/'s avatar
/jdi/ committed
56
57
58
59
60

int Program::sniff() {
	printf("Listening:\n");
	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
61
		Host h = Host();
/jdi/'s avatar
/jdi/ committed
62
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
63
		s.setHostIp(h.getIp());
/jdi/'s avatar
/jdi/ committed
64
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
65
		s.callback = [](Packet p) {
/jdi/'s avatar
/jdi/ committed
66
67
			if (options.flags & FLAG_HEADER) {
				if (options.flags & FLAG_HEX) {
/jdi/'s avatar
/jdi/ committed
68
					std::cout <<"Received Header:\t"<< p.getHead() <<"\n";
/jdi/'s avatar
/jdi/ committed
69
				} else {
/jdi/'s avatar
/jdi/ committed
70
					p.printHeader();
/jdi/'s avatar
/jdi/ committed
71
					printf("\n");
/jdi/'s avatar
/jdi/ committed
72
73
74
				}
			}
			if (options.flags & FLAG_HEX) {
/jdi/'s avatar
/jdi/ committed
75
				std::cout <<"Received Payload:\t"<<p.getBody()<<"\n";
/jdi/'s avatar
/jdi/ committed
76
			}
/jdi/'s avatar
/jdi/ committed
77
78
			for(auto a : p.getPayload()) {
				dataset d = a.second;
/jdi/'s avatar
/jdi/ committed
79
				printf("#%d\tLength: %d\n\tHex: ",d.type,d.len);
/jdi/'s avatar
/jdi/ committed
80
				std::cout << d.value;
/jdi/'s avatar
/jdi/ committed
81
				printf("\n\tDec: ");
/jdi/'s avatar
/jdi/ committed
82
				std::cout <<d.value;
/jdi/'s avatar
/jdi/ committed
83
84
				d.value.push_back(0U);
				printf("\n\tString: %s\n",&d.value[0]);
/jdi/'s avatar
/jdi/ committed
85
			}
/jdi/'s avatar
/jdi/ committed
86
87
88
89
90
91
92
93
94
95
			return 0;
		};
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}
/jdi/'s avatar
/jdi/ committed
96

/jdi/'s avatar
/jdi/ committed
97
int Program::encode(std::string s) {
/jdi/'s avatar
/jdi/ committed
98
	bytes d(s);
/jdi/'s avatar
/jdi/ committed
99
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
100
101
102
103
	p.encode(d);
	printf("%x", d[0]);
	for (unsigned i = 1; i < d.size(); i++) {
		printf(":%x", d[i]);
/jdi/'s avatar
/jdi/ committed
104
105
	}
	printf("\n");
/jdi/'s avatar
/jdi/ committed
106
107
108
	return 0;
}

/jdi/'s avatar
/jdi/ committed
109
int Program::setProperty() {
/jdi/'s avatar
/jdi/ committed
110
111
	return 0;
}
/jdi/'s avatar
/jdi/ committed
112
int Program::getProperty() {
/jdi/'s avatar
/jdi/ committed
113
114
115

	return 0;
}
/jdi/'s avatar
/jdi/ committed
116
117
int Program::save() {
	//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
118
119
	return 0;
}
/jdi/'s avatar
/jdi/ committed
120
121
int Program::restore() {
	//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
122
123
	return 0;
}
/jdi/'s avatar
/jdi/ committed
124
int Program::flash() {
/jdi/'s avatar
/jdi/ committed
125
126
127

	return 0;
}
/jdi/'s avatar
/jdi/ committed
128
int Program::reboot() {
/jdi/'s avatar
/jdi/ committed
129
130
131

	return 0;
}
/jdi/'s avatar
/jdi/ committed
132
int Program::reset() {
/jdi/'s avatar
/jdi/ committed
133
134

	return 0;
/jdi/'s avatar
/jdi/ committed
135
}