#pragma once #include #include #include #include #include #include #include class INetInfo { public: static std::vector getIpv4List() { return getIpList(); } static std::vector getIpv6List() { return getIpList(AF_INET6); } private: static std::vector getIpList(int ipv4_6 = AF_INET) { int ret_val = 0; std::vector ip_list; struct ifaddrs * ifAddrStruct = NULL; void * tmpAddrPtr = NULL; // 1. ret_val = getifaddrs(&ifAddrStruct); if (0 != ret_val) { ret_val = errno; return ip_list; } // 2. std::string str_ipvX; int padress_buf_len = 0; char addressBuffer[INET6_ADDRSTRLEN] = {0}; if (AF_INET6 == ipv4_6) padress_buf_len = INET6_ADDRSTRLEN; else padress_buf_len = INET_ADDRSTRLEN; while (NULL != ifAddrStruct ) { if (ipv4_6 == ifAddrStruct->ifa_addr->sa_family ) { // is a valid IP4 Address tmpAddrPtr = &((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr; inet_ntop(ipv4_6, tmpAddrPtr, addressBuffer, padress_buf_len); str_ipvX = std::string(addressBuffer); ip_list.push_back(str_ipvX); memset(addressBuffer, 0, padress_buf_len); } ifAddrStruct=ifAddrStruct->ifa_next; } return ip_list; } };