-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetIP.cpp
More file actions
38 lines (32 loc) · 1015 Bytes
/
getIP.cpp
File metadata and controls
38 lines (32 loc) · 1015 Bytes
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
#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <string>
#include <iostream>
using std::string;
int main() {
struct ifaddrs* addrStruct = NULL;
struct ifaddrs* iterator = NULL;
void *tmp = NULL;
getifaddrs(&addrStruct);
for (iterator = addrStruct; iterator != NULL; iterator = iterator->ifa_next) {
if (!iterator->ifa_addr) {
continue;
}
if (iterator->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
tmp=&((struct sockaddr_in *)iterator->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmp, addressBuffer, INET_ADDRSTRLEN);
if (string(iterator->ifa_name) == "eth0") {
string ip_address(addressBuffer);
std::cout << "My ip address is: " << ip_address << std::endl;
}
}
}
if (addrStruct!=NULL) freeifaddrs(addrStruct);
return 0;
}