-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.cpp
More file actions
50 lines (42 loc) · 1.45 KB
/
interface.cpp
File metadata and controls
50 lines (42 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "interface.h"
#include "exception.h"
#include "log.h"
#include <boost/utility.hpp>
#include <ifaddrs.h>
#include <netdb.h>
namespace {
class Interfaces : boost::noncopyable {
public:
Interfaces() {
CHECK_CALL(getifaddrs(&interfaces_), "getifaddrs");
}
~Interfaces() {
freeifaddrs(interfaces_);
}
operator const struct ifaddrs*() const {
return interfaces_;
}
private:
struct ifaddrs* interfaces_;
};
}
Interface::Interface() {
Interfaces ifs;
for(const struct ifaddrs* i = ifs; i; i = i->ifa_next) {
// TODO: think about ipv6 i->ifa_addr->sa_family == AF_INET6
if(!(i->ifa_addr && i->ifa_addr->sa_family == AF_INET)) continue;
char host[NI_MAXHOST];
CHECK_CALL_ERROR(getnameinfo(i->ifa_addr, sizeof(sockaddr_in),
// i->ifa_addr->sa_family == AF_INET ?
// sizeof(sockaddr_in) : sizeof(sockaddr_in6),
host, sizeof(host), 0, 0,
NI_NUMERICHOST | NI_NUMERICSERV),
"getnameinfo(" << i->ifa_name << ")", gai_strerror);
DEBUG("Found iface: " << i->ifa_name << " -- " << host);
ifaces_[i->ifa_name] = host;
}
}
const std::string& Interface::host(const std::string& iface) const {
IFaces::const_iterator it = ifaces_.find(iface);
return it == ifaces_.end() ? iface : it->second;
}