-
Notifications
You must be signed in to change notification settings - Fork 2
/
my_ip.h
65 lines (57 loc) · 1.82 KB
/
my_ip.h
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
/*--------------------------------------------
Return char* of IP address
------------------------------------------*/
#ifndef __MY_IP_H__
#define __MY_IP_H__
/* With reference to StackOverflow: http://stackoverflow.com/questions/212528/Linux-c-get-the-ip-address-of-local-computer */
#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
char* getMyIP(void)
{
static char str_myip[INET_ADDRSTRLEN]={0};
struct ifaddrs *ifaddr;
struct ifaddrs *ifAddrStruct=NULL;
void *tmpAddrPtr=NULL;
getifaddrs(&ifaddr);
ifAddrStruct=ifaddr;
/* -1 return SUCCESS ?????????
if(getifaddrs(&ifAddrStruct) == -1);//---!!!MUST free it later
{
perror("getifaddrs");
strcpy(str_myip,"Cont WiFi Fails!");
return str_myip;
}
*/
while(ifAddrStruct!=NULL)
{
if(ifAddrStruct->ifa_addr->sa_family==AF_INET) //check if it's valid IP4 address
{
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
//char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET,tmpAddrPtr,str_myip,INET_ADDRSTRLEN);// convert to Dotted Decimal Notation
if(!strcmp(ifAddrStruct->ifa_name,"wlan0")) //--return wlan0 IP
{
printf("%s IP Address %s\n", ifAddrStruct->ifa_name,str_myip);
freeifaddrs(ifaddr);
return str_myip;
}
}
else if(ifAddrStruct->ifa_addr->sa_family==AF_INET6) //check if it's valid IP6 address
{
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6,tmpAddrPtr,addressBuffer,INET6_ADDRSTRLEN);
printf("%s IPv6 Address %s\n", ifAddrStruct->ifa_name,addressBuffer);
}
usleep(100000);
ifAddrStruct=ifAddrStruct->ifa_next;
}
strcpy(str_myip,"Cont WiFi Fails!");
if(ifaddr != NULL)
freeifaddrs(ifaddr); //!!!! free ifaddr
return str_myip;
}
#endif