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

struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;

char g_ip_address[INET_ADDRSTRLEN];

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

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

    freeifaddrs(ifap);
    
    return 0;
}


