Host.cpp 1.03 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

//TODO clean up
/jdi/'s avatar
/jdi/ committed
10
#include <cstdio>
/jdi/'s avatar
/jdi/ committed
11
//#include <cerrno>
/jdi/'s avatar
/jdi/ committed
12
13
14
15
16
17
18
19
20
#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>
/jdi/'s avatar
/jdi/ committed
21
22
//#include <linux/if_link.h>
#include "Options.h"
/jdi/'s avatar
basics  
/jdi/ committed
23
#include "Host.h"
/jdi/'s avatar
/jdi/ committed
24
25
#include "Types/Types.h"
//#include "bytes.h"
/jdi/'s avatar
basics  
/jdi/ committed
26

/jdi/'s avatar
/jdi/ committed
27
macAddr Host::getMac() {
/jdi/'s avatar
/jdi/ committed
28
	macAddr ret { 0x6a,0x49,0x16,0x17,0x2e,0x8d }; //TODO find actual MAC Address
/jdi/'s avatar
/jdi/ committed
29
	return ret;
/jdi/'s avatar
basics  
/jdi/ committed
30
31
}

/jdi/'s avatar
/jdi/ committed
32
ipAddr Host::getIp() {
/jdi/'s avatar
/jdi/ committed
33
34
	struct ifaddrs *ifaddr, *ifa;
	int n;
/jdi/'s avatar
/jdi/ committed
35
	ipAddr data { 0, 0, 0, 0 };
/jdi/'s avatar
/jdi/ committed
36
37
38
39
40
41
42
43
44
45
46

	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) {
/jdi/'s avatar
/jdi/ committed
47
			if (options.interface.compare(ifa->ifa_name) == 0) {
/jdi/'s avatar
/jdi/ committed
48
49
50
51
52
53
54
55
				memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
				return data;
			}
		}
	}

	freeifaddrs(ifaddr);
	return data;
/jdi/'s avatar
basics  
/jdi/ committed
56
57
}