Skip to content

Commit

Permalink
Update IPFinder.cpp
Browse files Browse the repository at this point in the history
Signed-off-by: advancenOO <[email protected]>
  • Loading branch information
advancenXX committed Jun 10, 2024
1 parent eaeb0f5 commit bb6423a
Showing 1 changed file with 27 additions and 51 deletions.
78 changes: 27 additions & 51 deletions src/cpp/utils/IPFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,70 +157,46 @@ bool IPFinder::getIPs(
std::vector<info_IP>* vec_name,
bool return_loopback)
{
struct ifaddrs* ifaddr, * ifa;
int family, s;
char host[NI_MAXHOST];
int sockfd = 0;
unsigned int num = 0;
std::string ipv4addr;
struct ifreq buf[INET_ADDRSTRLEN];
struct ifconf ifc;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
return false;
}

// TODO arm64 doesn't seem to support getifaddrs
if (getifaddrs(&ifaddr) == -1)
{
perror("getifaddrs");
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = (caddr_t)buf;

if (ioctl(sockfd, SIOCGIFCONF, (char*)&ifc)) {
close(sockfd);
return false;
}

for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr == NULL || (ifa->ifa_flags & IFF_RUNNING) == 0)
{
for (num = 0; num < (ifc.ifc_len / sizeof(struct ifreq)); num++) {
if (ioctl(sockfd, SIOCGIFADDR, (char*)&buf[num])) {
continue;
}

family = ifa->ifa_addr->sa_family;
ipv4addr = std::string(inet_ntoa(((struct sockaddr_in*)(&buf[num].ifr_addr))->sin_addr));

if (family == AF_INET)
{
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
logWarning(UTILS, "getnameinfo() failed: " << gai_strerror(s));
continue;
}
info_IP info;
info.type = IP4;
info.name = std::string(host);
info.dev = std::string(ifa->ifa_name);
parseIP4(info);
// TODO ipv6.
info_IP info;
info.type = IP4;
info.name = ipv4addr;
info.dev = std::string(buf[num].ifr_name);
parseIP4(info);

if (return_loopback || info.type != IP4_LOCAL)
{
vec_name->push_back(info);
}
}
else if (family == AF_INET6)
if (return_loopback || info.type != IP4_LOCAL)
{
s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in6),
host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
if (s != 0)
{
logWarning(UTILS, "getnameinfo() failed: " << gai_strerror(s));
continue;
}
info_IP info;
info.type = IP6;
info.name = std::string(host);
info.dev = std::string(ifa->ifa_name);
if (parseIP6(info))
{
if (return_loopback || info.type != IP6_LOCAL)
{
vec_name->push_back(info);
}
}
vec_name->push_back(info);
}
}

freeifaddrs(ifaddr);
close(sockfd);
return true;
}

Expand Down

0 comments on commit bb6423a

Please sign in to comment.