Program.cpp 4.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
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
/jdi/ committed
20
	std::cout << "List:\n";
/jdi/'s avatar
basics  
/jdi/ committed
21
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
22
	p.setHostMac(host.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(host.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
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}
	return 1;
}
/jdi/'s avatar
/jdi/ committed
59
60
61
62
63
64

int Program::sniff() {
	printf("Listening:\n");
	try {
		asio::io_service io_service;
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
65
		s.setHostIp(host.getIp());
/jdi/'s avatar
/jdi/ committed
66
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		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;
/jdi/'s avatar
/jdi/ committed
84
							if(lookup.exists(d.type)) {
/jdi/'s avatar
/jdi/ committed
85
								if(d.len>0) {
/jdi/'s avatar
/jdi/ committed
86
87
									std::cout<<std::dec<<"\t++"<<std::hex<<d.type<<std::dec<<"++\n";
								} else {
/jdi/'s avatar
/jdi/ committed
88
89
90
91
92
93
94
95
96
97
98
99
									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<<"###"<<lookup[d.type]<<"\n";
							}
						}
					}
					return 0;
				};
/jdi/'s avatar
/jdi/ committed
100
101
102
103
104
105
106
107
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

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

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

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

/jdi/'s avatar
/jdi/ committed
131
132
133
	try {
		asio::io_service io_service;
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
134
		s.setHostIp(host.getIp());
/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
		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
163
}
/jdi/'s avatar
/jdi/ committed
164
int Program::save() {
/jdi/'s avatar
/jdi/ committed
165
166
	Switch sw = Switch();
	std::string str = sw.toString();
/jdi/'s avatar
/jdi/ committed
167
//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
168

/jdi/'s avatar
/jdi/ committed
169
170
	return 0;
}
/jdi/'s avatar
/jdi/ committed
171
int Program::restore() {
/jdi/'s avatar
/jdi/ committed
172
173
174
175
176
177

	const char str[] =
			" { \"hello\" : \"world\", \"t\" : true , \"f\" : false, \"n\": null, \"i\":123, \"pi\": 3.1416, \"a\":[1, 2, 3, 4] } ";
	printf("Original JSON:\n %s\n", str);
	Switch sw = Switch();
	sw.parse(str);
/jdi/'s avatar
/jdi/ committed
178
//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
179
	return 1;
/jdi/'s avatar
/jdi/ committed
180
}
/jdi/'s avatar
/jdi/ committed
181
int Program::flash() {
/jdi/'s avatar
/jdi/ committed
182
183
184

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

	return 0;
}
/jdi/'s avatar
/jdi/ committed
189
int Program::reset() {
/jdi/'s avatar
/jdi/ committed
190
191

	return 0;
/jdi/'s avatar
/jdi/ committed
192
}
/jdi/'s avatar
/jdi/ committed
193
void Program::init() {
/jdi/'s avatar
/jdi/ committed
194
	if (options.interface.compare("") == 0)
/jdi/'s avatar
/jdi/ committed
195
196
		options.interface = host.getIface();
}