Program.cpp 4.15 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
/jdi/ committed
16
#include "Lookup.h"
/jdi/'s avatar
basics  
/jdi/ committed
17

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

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

	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
30
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
31
		s.setHostIp(h.getIp());
/jdi/'s avatar
/jdi/ committed
32
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
		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
53
54
55
56
57
58
59
60
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

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

int Program::sniff() {
	printf("Listening:\n");
	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
66
		Host h = Host();
/jdi/'s avatar
/jdi/ committed
67
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
68
		s.setHostIp(h.getIp());
/jdi/'s avatar
/jdi/ committed
69
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
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
103
		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
104
105
106
107
108
109
110
111
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

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

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

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

/jdi/'s avatar
/jdi/ committed
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
167
	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
168
}
/jdi/'s avatar
/jdi/ committed
169
int Program::save() {
/jdi/'s avatar
/jdi/ committed
170
//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
171
172
	return 0;
}
/jdi/'s avatar
/jdi/ committed
173
int Program::restore() {
/jdi/'s avatar
/jdi/ committed
174
//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
175
176
	return 0;
}
/jdi/'s avatar
/jdi/ committed
177
int Program::flash() {
/jdi/'s avatar
/jdi/ committed
178
179
180

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

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

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