Host.cpp 2.32 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
//TODO clean up
/jdi/'s avatar
/jdi/ committed
9
10
11
12
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
/jdi/'s avatar
/jdi/ committed
13
#include <fstream>
/jdi/'s avatar
/jdi/ committed
14
15
16
#include <netdb.h>
#include <ifaddrs.h>
#include <unistd.h>
/jdi/'s avatar
/jdi/ committed
17
18
19
20
21
22
23
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_link.h>
#include <net/if.h>

/jdi/'s avatar
/jdi/ committed
24
#include "Options.h"
/jdi/'s avatar
basics  
/jdi/ committed
25
#include "Host.h"
/jdi/'s avatar
/jdi/ committed
26
#include "Types.h"
/jdi/'s avatar
basics  
/jdi/ committed
27

/jdi/'s avatar
/jdi/ committed
28
macAddr Host::getMac() {
/jdi/'s avatar
/jdi/ committed
29
30
	int s;
	struct ifreq buffer;
/jdi/'s avatar
/jdi/ committed
31
	macAddr data { 0, 0, 0, 0, 0, 0 };
/jdi/'s avatar
/jdi/ committed
32
33
34
35
36
37
38
39
	if (options.interface != "") {
		s = socket(PF_INET, SOCK_DGRAM, 0);
		memset(&buffer, 0x00, sizeof(buffer));
		strcpy(buffer.ifr_name, options.interface.c_str());
		ioctl(s, SIOCGIFHWADDR, &buffer);
		close(s);
		memcpy(&data[0], &buffer.ifr_hwaddr.sa_data[0], 6);
	}
/jdi/'s avatar
/jdi/ committed
40
	return data;
/jdi/'s avatar
basics  
/jdi/ committed
41
42
}

/jdi/'s avatar
/jdi/ committed
43
ipAddr Host::getIp() {
/jdi/'s avatar
/jdi/ committed
44
45
	struct ifaddrs *ifaddr, *ifa;
	int n;
/jdi/'s avatar
/jdi/ committed
46
	ipAddr data { 0, 0, 0, 0 };
/jdi/'s avatar
/jdi/ committed
47
48
49
50
51
52
53
	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;
/jdi/'s avatar
/jdi/ committed
54
		if (ifa->ifa_addr->sa_family == AF_INET)
/jdi/'s avatar
/jdi/ committed
55
			if (options.interface.compare(ifa->ifa_name) == 0) {
/jdi/'s avatar
/jdi/ committed
56
57
58
59
				memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
				return data;
			}
	}
/jdi/'s avatar
/jdi/ committed
60
61
62
63
64
65
66
67
68
	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 (getIface().compare(ifa->ifa_name) == 0) {
				memcpy(&data[0], &ifa->ifa_addr->sa_data[2], 4);
				return data;
			}
	}
/jdi/'s avatar
/jdi/ committed
69
70
	freeifaddrs(ifaddr);
	return data;
/jdi/'s avatar
basics  
/jdi/ committed
71
72
}

/jdi/'s avatar
/jdi/ committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
std::string Host::getIface() {
	std::string defaultInterface;
	std::ifstream routeFile("/proc/net/route", std::ios_base::in);
	if (!routeFile.good())
		return "";
	std::string line;
	std::vector<std::string> tokens;
	if (std::getline(routeFile, line))
		while (std::getline(routeFile, line)) {
			std::istringstream stream(line);
			std::string delimiter = "\t";
			size_t pos = 0;
			std::string token;
			while ((pos = line.find(delimiter)) != std::string::npos) {
				token = line.substr(0, pos);
				tokens.push_back(token);
				line.erase(0, pos + delimiter.length());
			}
			if ((tokens.size() >= 2)
					&& (tokens[1] == std::string("00000000"))) {
				defaultInterface = tokens[0];
				break;
			}
			tokens.clear();
		}
	routeFile.close();
	return defaultInterface;
}