#include "common.h"
#include "ip_reader.h"

static struct ifaddrs *s_ifap, *s_ifa;
static struct sockaddr_in *s_sa;

char g_ip_address[INET_ADDRSTRLEN];

/*获取ip*/
int ipaddr_obtain() {
    if (getifaddrs(&s_ifap) == -1) {
        //perror("getifaddrs() error");
        my_zlog_error("getifaddrs() error");
        return -1;
    }

    for (s_ifa = s_ifap; s_ifa != NULL; s_ifa = s_ifa->ifa_next) {
        if (s_ifa->ifa_addr != NULL && s_ifa->ifa_addr->sa_family == AF_INET) {
            s_sa = (struct sockaddr_in *) s_ifa->ifa_addr;
            inet_ntop(AF_INET, &s_sa->sin_addr, g_ip_address, sizeof(g_ip_address));
        }
    }

    freeifaddrs(s_ifap);
    
    return 0;
}


