Program.cpp 4.14 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
#include <iostream>
/jdi/'s avatar
/jdi/ committed
8
#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
basics  
/jdi/ committed
19
	Host h = Host();
/jdi/'s avatar
/jdi/ committed
20
	printf("List:\n");
/jdi/'s avatar
basics  
/jdi/ committed
21
22
	Packet p = Packet(Packet::DISCOVERY);
	p.setHostMac(h.getMac());
/jdi/'s avatar
/jdi/ committed
23
	p.setPayload( { });
/jdi/'s avatar
basics  
/jdi/ committed
24
25
26
27
28
	bytes a = p.getBytes();
	p.encode(a);

	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
29
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
30
		s.setHostIp(h.getIp());
/jdi/'s avatar
/jdi/ committed
31
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
		s.callback =
				[](Packet a) {
					if (options.flags & FLAG_HEADER) {
						if (options.flags & FLAG_HEX) {
							std::cout <<"Received Header:\n"<< a.getHead() <<"\n";
						} else {
							a.printHeader();
							std::cout<<"\n";
						}
					}
					if (options.flags & FLAG_HEX) {
						std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
					} else {
						datasets d =a.getPayload();
						Switch s = Switch();
						s.parse(d);
						std::cout <<"Devices:\n\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
52
53
54
55
56
57
58
59
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}
/jdi/'s avatar
/jdi/ committed
60
61
62
63
64

int Program::sniff() {
	printf("Listening:\n");
	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
65
		Host h = Host();
/jdi/'s avatar
/jdi/ committed
66
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
67
		s.setHostIp(h.getIp());
/jdi/'s avatar
/jdi/ committed
68
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
		s.callback =
				[](Packet p) {
					std::cout <<"Packet:\n\t"<<p.opCodeToString()<<"\n";
					if (options.flags & FLAG_HEADER) {
						if (options.flags & FLAG_HEX) {
							std::cout <<"Received Header:\n\t"<< p.getHead() <<"\n";
						} else {
							p.printHeader();
							printf("\n");
						}
					}
					if (options.flags & FLAG_HEX) {
						std::cout <<"Received Payload:\n\t"<<p.getBody()<<"\n";
					} else {
						for(auto a : p.getPayload()) {
							dataset d = a.second;
							auto lookup=(options.flags & FLAG_REVERSE)?snd_lookup:rcv_lookup;
							if(lookup.find(d.type) ==lookup.end()) {
								if(d.len>0) {
									std::cout<<std::dec<<"#"<<d.type<<"\tLength: "<<d.len<<"\n";
									std::cout<<std::hex<< "\tHex: " <<d.value<<"\n";
									d.value.push_back(0U);
									std::cout<<"\tString: " <<&d.value[0]<<"\n";
								} else {
									std::cout<<std::dec<<"\t++"<<std::hex<<d.type<<std::dec<<"++\n";
								}
							} else {
								std::cout<<"###"<<lookup[d.type]<<"\n";
							}
						}
					}

					return 0;
				};
/jdi/'s avatar
/jdi/ committed
103
104
105
106
107
108
109
110
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

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

/jdi/'s avatar
/jdi/ committed
112
int Program::encode(std::string s) {
/jdi/'s avatar
/jdi/ committed
113
	bytes d(s);
/jdi/'s avatar
/jdi/ committed
114
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
115
	p.encode(d);
/jdi/'s avatar
/jdi/ committed
116
	std::cout << d << std::endl;
/jdi/'s avatar
/jdi/ committed
117
118
119
	return 0;
}

/jdi/'s avatar
/jdi/ committed
120
int Program::setProperty() {
/jdi/'s avatar
/jdi/ committed
121
122
	return 0;
}
/jdi/'s avatar
/jdi/ committed
123
int Program::getProperty() {
/jdi/'s avatar
/jdi/ committed
124
125
126
127
128
129
130
131
132
133
	Host h = Host();
	printf("Get:\n");
	Packet p = Packet(Packet::DISCOVERY);
	macAddr d  = {0x14,0xcc,0x20,0x49,0x5e,0x07};
	p.setSwitchMac(d);
	p.setHostMac(h.getMac());
	datasets t = { { 2200, 0, { } } };
	p.setPayload(t);
	bytes a = p.getBytes();
	p.encode(a);
/jdi/'s avatar
/jdi/ committed
134

/jdi/'s avatar
/jdi/ committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
	try {
		asio::io_service io_service;
		Socket s(io_service);
		s.setHostIp(h.getIp());
		s.init(DST_PORT, SRC_PORT);
		s.callback =
				[](Packet a) {
					if (options.flags & FLAG_HEADER) {
						if (options.flags & FLAG_HEX) {
							std::cout <<"Received Header:\n"<< a.getHead() <<"\n";
						} else {
							a.printHeader();
							std::cout<<"\n";
						}
					}
					if (options.flags & FLAG_HEX) {
						std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
					} else {
						datasets d =a.getPayload();
						Switch s = Switch();
						s.parse(d);
						std::cout <<"Devices:\n\t"<<s.settings.hostname<<" ("<< s.device.type<<")\tMAC: "<<s.device.mac<<"\tIP: "<<s.settings.ip_addr<<"\n";
					}
					return 1;
				};
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
/jdi/'s avatar
/jdi/ committed
167
}
/jdi/'s avatar
/jdi/ committed
168
int Program::save() {
/jdi/'s avatar
/jdi/ committed
169
//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
170
171
	return 0;
}
/jdi/'s avatar
/jdi/ committed
172
int Program::restore() {
/jdi/'s avatar
/jdi/ committed
173
//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
174
175
	return 0;
}
/jdi/'s avatar
/jdi/ committed
176
int Program::flash() {
/jdi/'s avatar
/jdi/ committed
177
178
179

	return 0;
}
/jdi/'s avatar
/jdi/ committed
180
int Program::reboot() {
/jdi/'s avatar
/jdi/ committed
181
182
183

	return 0;
}
/jdi/'s avatar
/jdi/ committed
184
int Program::reset() {
/jdi/'s avatar
/jdi/ committed
185
186

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