Skip to content

Open raw sockets when adding hosts, not when doing the pinging #35

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 17 additions & 31 deletions src/liboping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1344,41 +1344,10 @@ int ping_send (pingobj_t *obj)
struct timeval nowtime;
struct timeval timeout;

_Bool need_ipv4_socket = 0;
_Bool need_ipv6_socket = 0;

for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
{
ptr->latency = -1.0;
ptr->recv_ttl = -1;

if (ptr->addrfamily == AF_INET)
need_ipv4_socket = 1;
else if (ptr->addrfamily == AF_INET6)
need_ipv6_socket = 1;
}

if (!need_ipv4_socket && !need_ipv6_socket)
{
ping_set_error (obj, "ping_send", "No hosts to ping");
return (-1);
}

if (need_ipv4_socket && obj->fd4 == -1)
{
obj->fd4 = ping_open_socket(obj, AF_INET);
if (obj->fd4 == -1)
return (-1);
ping_set_ttl (obj, obj->ttl);
ping_set_qos (obj, obj->qos);
}
if (need_ipv6_socket && obj->fd6 == -1)
{
obj->fd6 = ping_open_socket(obj, AF_INET6);
if (obj->fd6 == -1)
return (-1);
ping_set_ttl (obj, obj->ttl);
ping_set_qos (obj, obj->qos);
}

if (gettimeofday (&nowtime, NULL) == -1)
Expand Down Expand Up @@ -1701,6 +1670,23 @@ int ping_host_add (pingobj_t *obj, const char *host)
ph->table_next = obj->table[ph->ident % PING_TABLE_LEN];
obj->table[ph->ident % PING_TABLE_LEN] = ph;

if (ph->addrfamily == AF_INET && obj->fd4 == -1)
{
obj->fd4 = ping_open_socket(obj, AF_INET);
if (obj->fd4 == -1)
return (-1);
ping_set_ttl (obj, obj->ttl);
ping_set_qos (obj, obj->qos);
}
if (ph->addrfamily == AF_INET6 && obj->fd6 == -1)
{
obj->fd6 = ping_open_socket(obj, AF_INET6);
if (obj->fd6 == -1)
return (-1);
ping_set_ttl (obj, obj->ttl);
ping_set_qos (obj, obj->qos);
}

return (0);
} /* int ping_host_add */

Expand Down