Program.cpp 1.7 KB
Newer Older
/jdi/'s avatar
basics  
/jdi/ committed
1
2
3
4
5
6
7
8
/*
 * Program.cpp
 *
 *  Created on: 04.09.2015
 *      Author: jdi
 */
#include <stdio.h>

/jdi/'s avatar
/jdi/ committed
9
#include "Utils.h"
/jdi/'s avatar
basics  
/jdi/ committed
10
#include "Program.h"
/jdi/'s avatar
/jdi/ committed
11
12
13
14
#include "device/Device.h"
#include "device/Host.h"
#include "transfer/Socket.h"
#include "transfer/Packet.h"
/jdi/'s avatar
basics  
/jdi/ committed
15
16
17
18
19
20
21
22
23

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

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

/jdi/'s avatar
/jdi/ committed
24
25
26
int Program::list() {

	printf("List:\n");
/jdi/'s avatar
/jdi/ committed
27
	//Device d = Device();
/jdi/'s avatar
/jdi/ committed
28
	//printf(" %d\n", d.getName());
/jdi/'s avatar
basics  
/jdi/ committed
29
30
31
32

	Host h = Host();
	Packet p = Packet(Packet::DISCOVERY);
	p.setHostMac(h.getMac());
/jdi/'s avatar
/jdi/ committed
33
	p.setPayload( { });
/jdi/'s avatar
basics  
/jdi/ committed
34
35
36
37
38
	bytes a = p.getBytes();
	p.encode(a);

	try {
		asio::io_service io_service;
/jdi/'s avatar
/jdi/ committed
39
40
		Socket s(io_service);
		s.init(dst_port, src_port);
/jdi/'s avatar
/jdi/ committed
41
		s.callback = [](Packet a) {
/jdi/'s avatar
/jdi/ committed
42
43
44
45
46
47
			datasets d =a.getPayload();
			printf("%s (%s)\tMAC: ", &d[2].value[0], &d[1].value[0]);
			utils::printHex(d[3].value);
			printf("\tIP: ");
			utils::printDec(d[4].value);
			printf("\n");
/jdi/'s avatar
/jdi/ committed
48
49
			return 1;
		};
/jdi/'s avatar
basics  
/jdi/ committed
50
51
52
53
54
55
56
57
		s.send(a);
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}
/jdi/'s avatar
/jdi/ committed
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

int Program::sniff() {

	printf("Listening:\n");

	try {
		asio::io_service io_service;
		Socket s(io_service);
		s.init(src_port, dst_port);
		s.callback = [](Packet p) {
			printf("Receive Head:\t");
			utils::printHex(p.getHead());
			printf("\nReceive Body:\t");
			utils::printHex(p.getBody());
			printf("\n");
/jdi/'s avatar
/jdi/ committed
73
74
75
76
77
78
			for(auto a : p.getPayload()) {
				dataset d = a.second;
				printf("#%d\tLength: %d Value: %s\n",d.type,d.len,&d.value[0]);
			}
			printf("\n");
			printf("\n");
/jdi/'s avatar
/jdi/ committed
79
80
81
82
83
84
85
86
87
88
89
			printf("\n");
			return 0;
		};
		s.listen();
		io_service.run();
	} catch (std::exception& e) {
		std::cerr << "Exception: " << e.what() << "\n";
	}

	return 1;
}