Commit 36845477b01ebf88b99077f4e3a595bd74581b9e
1 parent
6cb3e01888
Exists in
master
네트워크 인터페이스의 IP 주소와 넷마스크를 얻는 함수
Showing
2 changed files
with
125 additions
and
0 deletions
Show diff stats
c/inet.c
@@ -0,0 +1,110 @@ | @@ -0,0 +1,110 @@ | ||
1 | +#include <netdb.h> | ||
2 | +#include <string.h> | ||
3 | +#include <ifaddrs.h> | ||
4 | + | ||
5 | +#include "inet.h" | ||
6 | + | ||
7 | +static struct sockaddr *find_address_of(char *name, struct ifaddrs *from) | ||
8 | +{ | ||
9 | + struct ifaddrs *ifa; | ||
10 | + int n; | ||
11 | + for (ifa = from, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { | ||
12 | + if (strcmp(ifa->ifa_name, name)) { | ||
13 | + continue; | ||
14 | + } | ||
15 | + | ||
16 | + if (ifa->ifa_addr == NULL) { | ||
17 | + continue; | ||
18 | + } | ||
19 | + | ||
20 | + int family = ifa->ifa_addr->sa_family; | ||
21 | + if (family == AF_INET || family == AF_INET6) { | ||
22 | + return ifa->ifa_addr; | ||
23 | + } | ||
24 | + } | ||
25 | + | ||
26 | + return NULL; | ||
27 | +} | ||
28 | + | ||
29 | +static struct sockaddr *find_netmask_of(char *name, struct ifaddrs *from) | ||
30 | +{ | ||
31 | + struct ifaddrs *ifa; | ||
32 | + int n; | ||
33 | + for (ifa = from, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { | ||
34 | + if (strcmp(ifa->ifa_name, name)) { | ||
35 | + continue; | ||
36 | + } | ||
37 | + | ||
38 | + if (ifa->ifa_netmask == NULL) { | ||
39 | + continue; | ||
40 | + } | ||
41 | + | ||
42 | + int family = ifa->ifa_netmask->sa_family; | ||
43 | + if (family == AF_INET || family == AF_INET6) { | ||
44 | + return ifa->ifa_netmask; | ||
45 | + } | ||
46 | + } | ||
47 | + | ||
48 | + return NULL; | ||
49 | +} | ||
50 | + | ||
51 | +static int address_to_string(struct sockaddr *addr, char *buffer, int size) | ||
52 | +{ | ||
53 | + return getnameinfo(addr, | ||
54 | + (addr->sa_family == AF_INET) | ||
55 | + ? sizeof(struct sockaddr_in) | ||
56 | + : sizeof(struct sockaddr_in6), | ||
57 | + buffer, size, | ||
58 | + NULL, 0, | ||
59 | + NI_NUMERICHOST); | ||
60 | +} | ||
61 | + | ||
62 | +int get_ip_addr(char *p_ip, int sz_ip) | ||
63 | +{ | ||
64 | + struct ifaddrs *ifaddr; | ||
65 | + if (getifaddrs(&ifaddr) == -1) { | ||
66 | + return -1; | ||
67 | + } | ||
68 | + | ||
69 | + struct sockaddr *address = find_address_of(IFNAME, ifaddr); | ||
70 | + if (address == NULL) { | ||
71 | + goto ERROR; | ||
72 | + } | ||
73 | + | ||
74 | + int ret = address_to_string(address, p_ip, sz_ip); | ||
75 | + if (ret != 0) { | ||
76 | + goto ERROR; | ||
77 | + } | ||
78 | + | ||
79 | + freeifaddrs(ifaddr); | ||
80 | + return 0; | ||
81 | + | ||
82 | +ERROR: | ||
83 | + freeifaddrs(ifaddr); | ||
84 | + return -1; | ||
85 | +} | ||
86 | + | ||
87 | +int get_netmask(char *p_netmask, int sz_netmask) | ||
88 | +{ | ||
89 | + struct ifaddrs *ifaddr; | ||
90 | + if (getifaddrs(&ifaddr) == -1) { | ||
91 | + return -1; | ||
92 | + } | ||
93 | + | ||
94 | + struct sockaddr *netmask = find_netmask_of(IFNAME, ifaddr); | ||
95 | + if (netmask == NULL) { | ||
96 | + goto ERROR; | ||
97 | + } | ||
98 | + | ||
99 | + int ret = address_to_string(netmask, p_netmask, sz_netmask); | ||
100 | + if (ret != 0) { | ||
101 | + goto ERROR; | ||
102 | + } | ||
103 | + | ||
104 | + freeifaddrs(ifaddr); | ||
105 | + return 0; | ||
106 | + | ||
107 | +ERROR: | ||
108 | + freeifaddrs(ifaddr); | ||
109 | + return -1; | ||
110 | +} |
c/inet.h
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +#ifndef INET_H | ||
2 | +#define INET_H | ||
3 | + | ||
4 | +// 인터페이스 이름 | ||
5 | +#define IFNAME "eth0" | ||
6 | + | ||
7 | +// p_ip: ip를 받는 버퍼 | ||
8 | +// sz_ip: 버퍼 크기 | ||
9 | +int get_ip_addr(char *p_ip, int sz_ip); | ||
10 | + | ||
11 | +// p_netmask: 넷마스크를 받는 버퍼 | ||
12 | +// sz_netmask: 버퍼 크기 | ||
13 | +int get_netmask(char *p_netmask, int sz_netmask); | ||
14 | + | ||
15 | +#endif /* INET_H */ |