forked from manzali/lseb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_ip.h
More file actions
92 lines (76 loc) · 2.34 KB
/
Copy pathlocal_ip.h
File metadata and controls
92 lines (76 loc) · 2.34 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef LOCAL_IP_H
#define LOCAL_IP_H
#include "log/log.hpp"
#include <string>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
std::string getHostIp(const std::string & hostname) {
struct hostent *he;
struct in_addr **addr_list;
//get entry
he = gethostbyname(hostname.c_str());
if (he == NULL) {
fprintf(stderr, "Get error on gethostbyname : %s", hstrerror(h_errno));
abort();
}
//extract addr
addr_list = (struct in_addr **) he->h_addr_list;
//convert first one
char buffer[256];
strcpy(buffer, inet_ntoa(*addr_list[0]));
return buffer;
}
std::string get_local_ip(const std::string & iface) {
//locals
struct ifaddrs * ifAddrStruct = NULL;
struct ifaddrs * ifa = NULL;
void * tmpAddrPtr = NULL;
//if iface is empty, search with hostname
if (iface.empty()) {
char hostname[1024];
gethostname(hostname, sizeof(hostname));
std::string ip = getHostIp(hostname);
LOG_DEBUG<< "Get ip for " << hostname << " = " << ip;
return ip;
}
//get addresses
getifaddrs(&ifAddrStruct);
//loop to seatch
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
//if no address
if (!ifa->ifa_addr) {
continue;
}
//check if requested one
if (iface == ifa->ifa_name) {
//check type
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
LOG_DEBUG<< "Get ip for " << iface << " = " << addressBuffer;
return addressBuffer;
} else if (ifa->ifa_addr->sa_family == AF_INET6) { // check it is IP6
// is a valid IP6 Address
tmpAddrPtr = &((struct sockaddr_in6 *) ifa->ifa_addr)->sin6_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
LOG_DEBUG<< "Get ip for " << iface << " = " << addressBuffer;
return addressBuffer;
}
}
}
//error not found
fprintf(stderr, "Fail to find address of interface %s\n", iface.c_str());
abort();
return "";
}
#endif //LOCAL_IP_H