Program.cpp 5.56 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
#include "File.h"
/jdi/'s avatar
/jdi/ committed
13
14
#include "Host.h"
#include "Socket.h"
/jdi/'s avatar
/jdi/ committed
15
#include "Switch.h"
/jdi/'s avatar
/jdi/ committed
16
#include "Packet.h"
/jdi/'s avatar
/jdi/ committed
17
#include "Lookup.h"
/jdi/'s avatar
basics  
/jdi/ committed
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
int printHeader(Packet p) {
	if (options.flags & FLAG_HEADER) {
		if (options.flags & FLAG_HEX) {
			std::cout << "Received Header:\n\t" << p.getHead() << "\n";
		} else {
			p.printHeader();
			printf("\n");
		}
	}
	return 0;
}

int printPacket(Packet p) {
	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.exists(d.type)) {
					const table::set *s = lookup.get(d.type);
				if (d.len > 0) {
					switch (s->format) {
					case table::STRING:
						std::cout << std::dec << "+\t" << s->name << " = "
								<< &d.value[0] << std::dec << "\n";
						break;
					case table::HEX:
						std::cout << std::dec << "+\t" << s->name << " = "
								<< std::hex << d.value << std::dec << "\n";
						break;
					case table::DEC:
						std::cout << std::dec << "+\t" << s->name << " = "
								<< std::dec << d.value << std::dec << "\n";
						break;
					default:
						std::cout << std::dec << "+\t" << s->name << " = "
								<< std::hex << d.value << std::dec << "\n";
					}
				} else { //empty
					std::cout << std::dec << ">\t" << s->name << "\n";
				}
			} else {//unknown id
				if (d.len > 0) {
					std::cout << "##\t" << d.type << ":\n\t";
					std::cout << std::hex << d.value << std::dec << "\n";
				} else { //empty
					std::cout << "#>\t" << d.type << "\n";
				}
			}
		}
	}
	return 0;
}

/jdi/'s avatar
/jdi/ committed
75
76
int Program::list() {

/jdi/'s avatar
/jdi/ committed
77
	std::cout << "List:\n";
/jdi/'s avatar
basics  
/jdi/ committed
78
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
79
	p.setHostMac(host.getMac());
/jdi/'s avatar
/jdi/ committed
80
	p.setPayload( { });
/jdi/'s avatar
/jdi/ committed
81
82
	bytes b = p.getBytes();
	p.encode(b);
/jdi/'s avatar
basics  
/jdi/ committed
83
84

	try {
85
86
87
88
89
		sock->setHostIp(host.getIp());
		sock->init(DST_PORT, SRC_PORT);
		sock->callback =
				[this](Packet a) {
					printHeader(a);
/jdi/'s avatar
/jdi/ committed
90
91
92
93
					if (options.flags & FLAG_HEX) {
						std::cout <<"Received Payload:\n"<<a.getBody()<<"\n";
					} else {
						datasets d =a.getPayload();
94
95
						Switch sw = Switch();
						sw.parse(d);
/jdi/'s avatar
/jdi/ committed
96
						File f;
97
98
						f.write(sw.toString());
						std::cout <<"Devices:\n\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";
/jdi/'s avatar
/jdi/ committed
99
100
101
					}
					return 1;
				};
102
103
		sock->send(b);
		io_service->run();
/jdi/'s avatar
basics  
/jdi/ committed
104
105
106
107
108
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}
	return 1;
}
/jdi/'s avatar
/jdi/ committed
109
110
111
112

int Program::sniff() {
	printf("Listening:\n");
	try {
/jdi/'s avatar
/jdi/ committed
113
		boost::asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
114
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
115
		s.setHostIp(host.getIp());
/jdi/'s avatar
/jdi/ committed
116
		s.init(DST_PORT, SRC_PORT);
117
118
119
120
121
122
		s.callback = [](Packet p) {
			std::cout << p.opCodeToString() << "\n";
			printHeader(p);
			printPacket(p);
			return 0;
		};
/jdi/'s avatar
/jdi/ committed
123
124
125
126
127
128
129
130
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

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

/jdi/'s avatar
/jdi/ committed
132
int Program::encode(std::string s) {
/jdi/'s avatar
/jdi/ committed
133
	bytes d(s);
/jdi/'s avatar
/jdi/ committed
134
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
135
	p.encode(d);
/jdi/'s avatar
/jdi/ committed
136
	std::cout << d << std::endl;
/jdi/'s avatar
/jdi/ committed
137
138
139
	return 0;
}

/jdi/'s avatar
/jdi/ committed
140
int Program::setProperty() {
/jdi/'s avatar
/jdi/ committed
141
142
	return 0;
}
/jdi/'s avatar
e    
/jdi/ committed
143

/jdi/'s avatar
/jdi/ committed
144
int Program::getProperty() {
145
146
	std::cout << "List:\n";
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
147
	p.setHostMac(host.getMac());
148
149
150
151
152
	p.setPayload( { });
	bytes b = p.getBytes();
	p.encode(b);
	std::cout << "count-x:" << sock.use_count() << "\n";
	auto s = sock;
/jdi/'s avatar
/jdi/ committed
153
	try {
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
		sock->setHostIp(host.getIp());
		sock->init(DST_PORT, SRC_PORT);

		std::cout << "count-y:" << sock.use_count() << "\n";
		sock->callback =
				[this](Packet a) {
					auto s = sock;
					std::cout<<"count-z:"<<sock.use_count()<<"\n";
					datasets d =a.getPayload();
					Switch sw = Switch();
					sw.parse(d);
					std::cout <<"Devices:\n\t"<<sw.settings.hostname<<" ("<< sw.device.type<<")\tMAC: "<<sw.device.mac<<"\tIP: "<<sw.settings.ip_addr<<"\n";

					Packet p = Packet(Packet::GET);
					p.setSwitchMac(a.getSwitchMac());
					p.setHostMac(host.getMac());
					datasets t = { {2305, 0, {}}};
					p.setPayload(t);
					bytes c = p.getBytes();
					p.encode(c);
					sock->callback =
					[this](Packet a) {
						auto s = sock;
/jdi/'s avatar
/jdi/ committed
177
						datasets d =a.getPayload();
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
						Switch sw = Switch();
						sw.parse(d);
						Packet p = Packet(Packet::LOGIN);
						p.setSwitchMac(a.getSwitchMac());
						p.setHostMac(host.getMac());
						datasets t = { {snd_lookup["login_user"], 0, {}}};
						p.setPayload(t);
						bytes c = p.getBytes();
						p.encode(c);

						sock->callback =
						[this](Packet a) {
							std::cout << a.opCodeToString() << "\n";
							printHeader(a);
							printPacket(a);
							return 0;
						};
						sock->send(c);
						return 0;
					};

					std::cout<<"count-b:"<<sock.use_count()<<"\n";

					sock->send(c);

					std::cout<<"count-a:"<<sock.use_count()<<"\n";
					return 0;
/jdi/'s avatar
/jdi/ committed
205
				};
206
207
		sock->send(b);
		io_service->run();
/jdi/'s avatar
/jdi/ committed
208
209
210
211
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}
	return 1;
/jdi/'s avatar
/jdi/ committed
212
}
/jdi/'s avatar
e    
/jdi/ committed
213

/jdi/'s avatar
/jdi/ committed
214
int Program::save() {
/jdi/'s avatar
/jdi/ committed
215
	Switch sw = Switch();
/jdi/'s avatar
/jdi/ committed
216
217
	sw.settings.hostname = "testname.lan";
	File f;
/jdi/'s avatar
/jdi/ committed
218
	f.write(sw.toString());
/jdi/'s avatar
/jdi/ committed
219
	return 1;
/jdi/'s avatar
/jdi/ committed
220
}
/jdi/'s avatar
e    
/jdi/ committed
221

/jdi/'s avatar
/jdi/ committed
222
int Program::restore() {
/jdi/'s avatar
/jdi/ committed
223
224
	File f;
	Switch sw;
/jdi/'s avatar
/jdi/ committed
225
226
227
228
	sw.parse(f.read());
	std::cout << "Devices:\n\t" << sw.settings.hostname << " ("
			<< sw.device.type << ")\tMAC: " << sw.device.mac << "\tIP: "
			<< sw.settings.ip_addr << "\n";
/jdi/'s avatar
/jdi/ committed
229
	return 1;
/jdi/'s avatar
/jdi/ committed
230
}
/jdi/'s avatar
e    
/jdi/ committed
231

/jdi/'s avatar
/jdi/ committed
232
int Program::flash() {
/jdi/'s avatar
/jdi/ committed
233
234
235

	return 0;
}
/jdi/'s avatar
e    
/jdi/ committed
236

/jdi/'s avatar
/jdi/ committed
237
int Program::reboot() {
/jdi/'s avatar
/jdi/ committed
238
239
240

	return 0;
}
/jdi/'s avatar
e    
/jdi/ committed
241

/jdi/'s avatar
/jdi/ committed
242
int Program::reset() {
/jdi/'s avatar
/jdi/ committed
243
244

	return 0;
/jdi/'s avatar
/jdi/ committed
245
}
/jdi/'s avatar
e    
/jdi/ committed
246

/jdi/'s avatar
/jdi/ committed
247
void Program::init() {
/jdi/'s avatar
/jdi/ committed
248
	if (options.interface.compare("") == 0)
/jdi/'s avatar
/jdi/ committed
249
250
		options.interface = host.getIface();
}