Skip to content

Commit

Permalink
[nasa/nos3#483] Update Deprecated gethostbyname to getaddrinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbrown2 committed Jan 30, 2025
1 parent 792c016 commit 9d73827
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions sim/src/generic_radio_hardware_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,36 @@ namespace Nos3

int32_t Generic_radioHardwareModel::host_to_ip(const char * hostname, char* ip)
{
struct hostent *he;
struct in_addr **addr_list;

if ( (he = gethostbyname( hostname ) ) == NULL )
{
return 1;
}
struct addrinfo hints, *res, *p;
int status;
void *addr;

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET; // Uses IPV4 only. AF_UNSPEC for IPV6 Support
hints.ai_socktype = SOCK_STREAM;

addr_list = (struct in_addr **) he->h_addr_list;
if ((status = getaddrinfo(hostname, NULL, &hints, &res)) != 0)
{
return 1;
}

for(int i=0; addr_list[i] != NULL; i++)
for (p = res; p != NULL; p = p->ai_next)
{
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);

// Convert IP to String
if (inet_ntop(p->ai_family, addr, ip, INET_ADDRSTRLEN) == NULL)
{
strcpy(ip, inet_ntoa(*addr_list[i]) );
return 0;
freeaddrinfo(res);
return 1;
}
return 1;

freeaddrinfo(res);
return 0; // IP Found
}
freeaddrinfo(res);
return 1; // IP NOT Found
}


Expand Down

0 comments on commit 9d73827

Please sign in to comment.