Program.cpp 2.68 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
		s.callback = [](Packet p) {
/jdi/'s avatar
/jdi/ committed
70
71
			if (options.flags & FLAG_HEADER) {
				if (options.flags & FLAG_HEX) {
/jdi/'s avatar
/jdi/ committed
72
					std::cout <<"Received Header:\n\t"<< p.getHead() <<"\n";
/jdi/'s avatar
/jdi/ committed
73
				} else {
/jdi/'s avatar
/jdi/ committed
74
					p.printHeader();
/jdi/'s avatar
/jdi/ committed
75
					printf("\n");
/jdi/'s avatar
/jdi/ committed
76
77
78
				}
			}
			if (options.flags & FLAG_HEX) {
/jdi/'s avatar
/jdi/ committed
79
80
81
82
83
84
85
				std::cout <<"Received Payload:\n\t"<<p.getBody()<<"\n";
			} else {
				for(auto a : p.getPayload()) {
					dataset d = a.second;
					std::cout<<"#"<<d.type<<"\tLength: "<<d.len<<"\n";
					std::cout<<std::hex<< "\tHex: " <<d.value<<"\n";
					//std::cout<<std::dec<<"\tDec: " << d.value<<"\n";
/jdi/'s avatar
/jdi/ committed
86
				d.value.push_back(0U);
/jdi/'s avatar
/jdi/ committed
87
				std::cout<<"\tString: " <<&d.value[0]<<"\n";
/jdi/'s avatar
/jdi/ committed
88
			}
/jdi/'s avatar
/jdi/ committed
89
90
91
		}
		return 0;
	};
/jdi/'s avatar
/jdi/ committed
92
93
94
95
96
97
98
99
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

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

/jdi/'s avatar
/jdi/ committed
101
int Program::encode(std::string s) {
/jdi/'s avatar
/jdi/ committed
102
	bytes d(s);
/jdi/'s avatar
/jdi/ committed
103
	Packet p = Packet(Packet::DISCOVERY);
/jdi/'s avatar
/jdi/ committed
104
	p.encode(d);
/jdi/'s avatar
/jdi/ committed
105
	std::cout << d << std::endl;
/jdi/'s avatar
/jdi/ committed
106
107
108
	return 0;
}

/jdi/'s avatar
/jdi/ committed
109
int Program::setProperty() {
/jdi/'s avatar
/jdi/ committed
110
111
	return 0;
}
/jdi/'s avatar
/jdi/ committed
112
int Program::getProperty() {
/jdi/'s avatar
/jdi/ committed
113
114
115

	return 0;
}
/jdi/'s avatar
/jdi/ committed
116
117
int Program::save() {
	//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
118
119
	return 0;
}
/jdi/'s avatar
/jdi/ committed
120
121
int Program::restore() {
	//File = fopen(otions.file)
/jdi/'s avatar
/jdi/ committed
122
123
	return 0;
}
/jdi/'s avatar
/jdi/ committed
124
int Program::flash() {
/jdi/'s avatar
/jdi/ committed
125
126
127

	return 0;
}
/jdi/'s avatar
/jdi/ committed
128
int Program::reboot() {
/jdi/'s avatar
/jdi/ committed
129
130
131

	return 0;
}
/jdi/'s avatar
/jdi/ committed
132
int Program::reset() {
/jdi/'s avatar
/jdi/ committed
133
134

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