-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.c
49 lines (40 loc) · 1.24 KB
/
ip.c
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
#include <stddef.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include "matelight.h"
char ip_address[MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)];
void ip_init(void)
{
struct ifaddrs *ifa, *ifptr;
struct sockaddr_in *addr;
struct sockaddr_in6 *addr6;
strcpy(ip_address, "127.0.0.1");
if (getifaddrs(&ifa) != 0)
return;
for (ifptr = ifa; ifptr; ifptr = ifptr->ifa_next) {
if (! (ifptr->ifa_flags & IFF_UP))
continue;
if ((ifptr->ifa_flags & IFF_LOOPBACK))
continue;
if (ifptr->ifa_addr->sa_family != AF_INET)
continue;
switch (ifptr->ifa_addr->sa_family) {
case AF_INET:
addr = (struct sockaddr_in *)ifptr->ifa_addr;
(void)inet_ntop(AF_INET, &addr->sin_addr, ip_address, sizeof(ip_address));
break;
case AF_INET6:
addr6 = (struct sockaddr_in6 *)ifptr->ifa_addr;
(void)inet_ntop(AF_INET6, &addr6->sin6_addr, ip_address, sizeof(ip_address));
break;
default:
break;
}
}
freeifaddrs(ifa);
}