Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ping: properly iterate addrinfo #153

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ int ping_tcp(host_t *host, ping_t *ping) {
* \return 1 - IPv4, 2 - IPv6, 0 - Unknown
*/
int get_address_type(host_t *host) {
struct addrinfo hints, *res;
struct addrinfo hints, *res, *res_list;
char addrstr[255];
void *ptr;
int addr_found = FALSE;
Expand All @@ -866,15 +866,15 @@ int get_address_type(host_t *host) {
hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
int error;

if ((error = getaddrinfo(host->hostname, NULL, &hints, &res)) != 0) {
if ((error = getaddrinfo(host->hostname, NULL, &hints, &res_list)) != 0) {
SPINE_LOG(("WARNING: Unable to determine address info for %s (%s)", host->hostname, gai_strerror(error)));
if (res != NULL) {
freeaddrinfo(res);
if (res_list != NULL) {
freeaddrinfo(res_list);
}
return SPINE_NONE;
}

while (res) {
for (res = res_list; res != NULL; res = res->ai_next) {
inet_ntop(res->ai_family, res->ai_addr->sa_data, addrstr, 100);

switch(res->ai_family) {
Expand All @@ -893,15 +893,13 @@ int get_address_type(host_t *host) {
SPINE_LOG_HIGH(("Device[%d] IPv%d address %s (%s)\n", host->id, res->ai_family == PF_INET6 ? 6:4, addrstr, res->ai_canonname));

if (res->ai_family != PF_INET6) {
freeaddrinfo(res);
freeaddrinfo(res_list);

return SPINE_IPV4;
}

res = res->ai_next;
}

freeaddrinfo(res);
freeaddrinfo(res_list);

if (addr_found) {
return SPINE_IPV6;
Expand Down