Host.cpp 1.06 KB
Newer Older
/jdi/'s avatar
basics  
/jdi/ committed
1
2
3
4
5
6
7
/*
 * Host.cpp
 *
 *  Created on: 11.09.2015
 *      Author: jdi
 */

/jdi/'s avatar
/jdi/ committed
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <cstdio>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <unistd.h>
#include <linux/if_link.h>
#include "Utils.h"
/jdi/'s avatar
basics  
/jdi/ committed
21
22
23
24
25
26
27
#include "Host.h"

Host::Host() {
	// TODO Auto-generated constructor stub

}

/jdi/'s avatar
/jdi/ committed
28
29
30
31
byteArray<6> Host::getMac() {
	byteArray<6> ret = { 0x08, 0x3e, 0x8e, 0x16, 0x17, 0x2c };
	//TODO find actual MAC Address
	return ret;
/jdi/'s avatar
basics  
/jdi/ committed
32
33
}

/jdi/'s avatar
/jdi/ committed
34
byteArray<4> Host::getIp(std::string iface) {
/jdi/'s avatar
/jdi/ committed
35
36
	struct ifaddrs *ifaddr, *ifa;
	int n;
/jdi/'s avatar
/jdi/ committed
37
	byteArray<4> data = { 0, 0, 0, 0 };
/jdi/'s avatar
/jdi/ committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

	if (getifaddrs(&ifaddr) == -1) {
		perror("getifaddrs");
		exit(EXIT_FAILURE);
	}

	for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
		if (ifa->ifa_addr == NULL)
			continue;

		if (ifa->ifa_addr->sa_family == AF_INET) {
			if (iface.compare(ifa->ifa_name) == 0) {
				memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
				return data;
			}
		}
	}

	freeifaddrs(ifaddr);

	return data;
/jdi/'s avatar
basics  
/jdi/ committed
59
60
}