Program.cpp 5.49 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
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 {
35
		for (dataset d : p.getPayload()) {
36
37
38
39
40
41
42
			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:
43
						std::cout << "+\t" << s->name << " = "
44
45
46
								<< &d.value[0] << std::dec << "\n";
						break;
					case table::HEX:
47
						std::cout << "+\t" << s->name << " = "
48
49
50
								<< std::hex << d.value << std::dec << "\n";
						break;
					case table::DEC:
51
						std::cout << "+\t" << s->name << " = "
52
53
54
								<< std::dec << d.value << std::dec << "\n";
						break;
					default:
55
						std::cout << "+\t" << s->name << " = "
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
								<< 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
74
75
int Program::list() {

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

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

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

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

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

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

/jdi/'s avatar
/jdi/ committed
143
int Program::getProperty() {
144
145
	std::cout << "List:\n";
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
146
	p.setHostMac(host.getMac());
147
148
149
150
151
	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
152
	try {
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
		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
176
						datasets d =a.getPayload();
177
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
						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
204
				};
205
206
		sock->send(b);
		io_service->run();
/jdi/'s avatar
/jdi/ committed
207
208
209
210
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}
	return 1;
/jdi/'s avatar
/jdi/ committed
211
}
/jdi/'s avatar
e    
/jdi/ committed
212

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

/jdi/'s avatar
/jdi/ committed
221
int Program::restore() {
/jdi/'s avatar
/jdi/ committed
222
223
	File f;
	Switch sw;
/jdi/'s avatar
/jdi/ committed
224
225
226
227
	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
228
	return 1;
/jdi/'s avatar
/jdi/ committed
229
}
/jdi/'s avatar
e    
/jdi/ committed
230

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

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

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

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

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

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

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