From 36845477b01ebf88b99077f4e3a595bd74581b9e Mon Sep 17 00:00:00 2001 From: victor Date: Wed, 24 Apr 2019 11:48:20 +0900 Subject: [PATCH] =?UTF-8?q?=EB=84=A4=ED=8A=B8=EC=9B=8C=ED=81=AC=20?= =?UTF-8?q?=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=EC=9D=98=20IP=20?= =?UTF-8?q?=EC=A3=BC=EC=86=8C=EC=99=80=20=EB=84=B7=EB=A7=88=EC=8A=A4?= =?UTF-8?q?=ED=81=AC=EB=A5=BC=20=EC=96=BB=EB=8A=94=20=ED=95=A8=EC=88=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- c/inet.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ c/inet.h | 15 +++++++++ 2 files changed, 125 insertions(+) create mode 100644 c/inet.c create mode 100644 c/inet.h diff --git a/c/inet.c b/c/inet.c new file mode 100644 index 0000000..deb1dde --- /dev/null +++ b/c/inet.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#include "inet.h" + +static struct sockaddr *find_address_of(char *name, struct ifaddrs *from) +{ + struct ifaddrs *ifa; + int n; + for (ifa = from, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { + if (strcmp(ifa->ifa_name, name)) { + continue; + } + + if (ifa->ifa_addr == NULL) { + continue; + } + + int family = ifa->ifa_addr->sa_family; + if (family == AF_INET || family == AF_INET6) { + return ifa->ifa_addr; + } + } + + return NULL; +} + +static struct sockaddr *find_netmask_of(char *name, struct ifaddrs *from) +{ + struct ifaddrs *ifa; + int n; + for (ifa = from, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { + if (strcmp(ifa->ifa_name, name)) { + continue; + } + + if (ifa->ifa_netmask == NULL) { + continue; + } + + int family = ifa->ifa_netmask->sa_family; + if (family == AF_INET || family == AF_INET6) { + return ifa->ifa_netmask; + } + } + + return NULL; +} + +static int address_to_string(struct sockaddr *addr, char *buffer, int size) +{ + return getnameinfo(addr, + (addr->sa_family == AF_INET) + ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6), + buffer, size, + NULL, 0, + NI_NUMERICHOST); +} + +int get_ip_addr(char *p_ip, int sz_ip) +{ + struct ifaddrs *ifaddr; + if (getifaddrs(&ifaddr) == -1) { + return -1; + } + + struct sockaddr *address = find_address_of(IFNAME, ifaddr); + if (address == NULL) { + goto ERROR; + } + + int ret = address_to_string(address, p_ip, sz_ip); + if (ret != 0) { + goto ERROR; + } + + freeifaddrs(ifaddr); + return 0; + +ERROR: + freeifaddrs(ifaddr); + return -1; +} + +int get_netmask(char *p_netmask, int sz_netmask) +{ + struct ifaddrs *ifaddr; + if (getifaddrs(&ifaddr) == -1) { + return -1; + } + + struct sockaddr *netmask = find_netmask_of(IFNAME, ifaddr); + if (netmask == NULL) { + goto ERROR; + } + + int ret = address_to_string(netmask, p_netmask, sz_netmask); + if (ret != 0) { + goto ERROR; + } + + freeifaddrs(ifaddr); + return 0; + +ERROR: + freeifaddrs(ifaddr); + return -1; +} diff --git a/c/inet.h b/c/inet.h new file mode 100644 index 0000000..e709570 --- /dev/null +++ b/c/inet.h @@ -0,0 +1,15 @@ +#ifndef INET_H +#define INET_H + +// 인터페이스 이름 +#define IFNAME "eth0" + +// p_ip: ip를 받는 버퍼 +// sz_ip: 버퍼 크기 +int get_ip_addr(char *p_ip, int sz_ip); + +// p_netmask: 넷마스크를 받는 버퍼 +// sz_netmask: 버퍼 크기 +int get_netmask(char *p_netmask, int sz_netmask); + +#endif /* INET_H */ -- 2.1.4