Program.cpp 2.81 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
8
#include <cstdio>
#include <algorithm>
/jdi/'s avatar
basics  
/jdi/ committed
9

/jdi/'s avatar
/jdi/ committed
10
#include "Utils.h"
/jdi/'s avatar
/jdi/ committed
11
#include "Options.h"
/jdi/'s avatar
basics  
/jdi/ committed
12
#include "Program.h"
/jdi/'s avatar
/jdi/ committed
13
14
15
#include "Host.h"
#include "Socket.h"
#include "Packet.h"
/jdi/'s avatar
basics  
/jdi/ committed
16
17
18
19
20

Program::Program() {
	// TODO Auto-generated constructor stub
}

/jdi/'s avatar
/jdi/ committed
21
22
int Program::list() {

/jdi/'s avatar
/jdi/ committed
23
	//Device d = Device();
/jdi/'s avatar
/jdi/ committed
24
	//printf(" %d\n", d.getName());
/jdi/'s avatar
basics  
/jdi/ committed
25
26

	Host h = Host();
/jdi/'s avatar
/jdi/ committed
27
	printf("List:\n");
/jdi/'s avatar
basics  
/jdi/ committed
28
29
	Packet p = Packet(Packet::DISCOVERY);
	p.setHostMac(h.getMac());
/jdi/'s avatar
/jdi/ committed
30
	p.setPayload( { });
/jdi/'s avatar
basics  
/jdi/ committed
31
32
33
34
35
	bytes a = p.getBytes();
	p.encode(a);

	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
36
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
37
		s.setHostIp(h.getIp(options.interface));
/jdi/'s avatar
/jdi/ committed
38
		s.init(DST_PORT, SRC_PORT);
/jdi/'s avatar
/jdi/ committed
39
		s.callback = [](Packet a) {
/jdi/'s avatar
/jdi/ committed
40
41
42
43
44
45
46
47
48
49
			if (options.flags & FLAG_HEADER) {
				printf("Received Header:\t");
				utils::printHex(a.getHead());
				printf("\n");
			}
			if (options.flags & FLAG_HEX) {
				printf("Received Payload:\t");
				utils::printHex(a.getBody());
				printf("\n");
			}
/jdi/'s avatar
/jdi/ committed
50
			datasets d =a.getPayload();
/jdi/'s avatar
/jdi/ committed
51
			printf("\t%s (%s)\tMAC: ", &d[2].value[0], &d[1].value[0]);
/jdi/'s avatar
/jdi/ committed
52
53
54
55
			utils::printHex(d[3].value);
			printf("\tIP: ");
			utils::printDec(d[4].value);
			printf("\n");
/jdi/'s avatar
/jdi/ committed
56
57
			return 1;
		};
/jdi/'s avatar
basics  
/jdi/ committed
58
59
60
61
62
63
64
65
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}
/jdi/'s avatar
/jdi/ committed
66
67
68
69
70

int Program::sniff() {
	printf("Listening:\n");
	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
71
		Host h = Host();
/jdi/'s avatar
/jdi/ committed
72
		Socket s(io_service);
/jdi/'s avatar
/jdi/ committed
73
		s.setHostIp(h.getIp(options.interface));
/jdi/'s avatar
/jdi/ committed
74
		s.init(DST_PORT,SRC_PORT);
/jdi/'s avatar
/jdi/ committed
75
		s.callback = [](Packet p) {
/jdi/'s avatar
/jdi/ committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
			if (options.flags & FLAG_HEADER) {
				if (options.flags & FLAG_HEX) {
				printf("Received Header:\t");
				utils::printHex(p.getHead());
				}else{
					p.printHeader();
				}
				printf("\n");
			}
			if (options.flags & FLAG_HEX) {
				printf("Received Payload:\t");
				utils::printHex(p.getBody());
				printf("\n");
			}
/jdi/'s avatar
/jdi/ committed
90
91
			for(auto a : p.getPayload()) {
				dataset d = a.second;
/jdi/'s avatar
/jdi/ committed
92
93
94
95
96
97
				printf("#%d\tLength: %d\n\tHex: ",d.type,d.len);
				utils::printHex(d.value);
				printf("\n\tDec: ");
				utils::printDec(d.value);
				d.value.push_back(0U);
				printf("\n\tString: %s\n",&d.value[0]);
/jdi/'s avatar
/jdi/ committed
98
			}
/jdi/'s avatar
/jdi/ committed
99
100
101
102
103
104
105
106
107
108
			return 0;
		};
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}
/jdi/'s avatar
/jdi/ committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135

int Program::encode(std::string s){
	std::string delimiter = ":";
	std::string token;
	size_t pos = 0;
	bytes arr = { };
	int hex;
	byte b;
	while ((pos = s.find(delimiter)) != std::string::npos) {
		token = s.substr(0, pos);
		sscanf(token.c_str(), "%x", &hex);
		s.erase(0, pos + delimiter.length());
		b = hex & 0xFF;
		arr.push_back(b);
	}
	sscanf(s.c_str(), "%x", &hex);
	b = hex & 0xFF;
	arr.push_back(b);

	Packet p = Packet(Packet::DISCOVERY);
	p.encode(arr);
	printf("%x", arr[0]);
	for (unsigned i = 1; i < arr.size(); i++) {
		printf(":%x", arr[i]);
	}
	printf("\n");
}