diff --git a/src/liboping.c b/src/liboping.c
index fd6e94227e4f7ff68d50c8eff308ea9d35a7afd4..8b3ca4da21de250debdd219f444d9094169cba39 100644 (file)
--- a/src/liboping.c
+++ b/src/liboping.c
static void ping_free (pinghost_t *ph)
{
static void ping_free (pinghost_t *ph)
{
- if (ph->username != NULL)
- free (ph->username);
-
- if (ph->hostname != NULL)
- free (ph->hostname);
+ if (ph == NULL)
+ return;
- if (ph->data != NULL)
- free (ph->data);
+ free (ph->username);
+ free (ph->hostname);
+ free (ph->data);
free (ph);
}
free (ph);
}
#ifdef SO_TIMESTAMP
if (1) /* {{{ */
{
#ifdef SO_TIMESTAMP
if (1) /* {{{ */
{
- int status;
- int opt = 1;
-
- status = setsockopt (fd,
- SOL_SOCKET, SO_TIMESTAMP,
- &opt, sizeof (opt));
+ int status = setsockopt (fd, SOL_SOCKET, SO_TIMESTAMP,
+ &(int){1}, sizeof(int));
if (status != 0)
{
ping_set_errno (obj, errno);
if (status != 0)
{
ping_set_errno (obj, errno);
if (addrfam == AF_INET)
{
if (addrfam == AF_INET)
{
- int opt;
-
#ifdef IP_RECVTOS
/* Enable receiving the TOS field */
#ifdef IP_RECVTOS
/* Enable receiving the TOS field */
- opt = 1;
- setsockopt (fd, IPPROTO_IP, IP_RECVTOS,
- &opt, sizeof (opt));
+ setsockopt (fd, IPPROTO_IP, IP_RECVTOS, &(int){1}, sizeof(int));
#endif /* IP_RECVTOS */
/* Enable receiving the TTL field */
#endif /* IP_RECVTOS */
/* Enable receiving the TTL field */
- opt = 1;
- setsockopt (fd, IPPROTO_IP, IP_RECVTTL,
- &opt, sizeof (opt));
+ setsockopt (fd, IPPROTO_IP, IP_RECVTTL, &(int){1}, sizeof(int));
}
#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
else if (addrfam == AF_INET6)
{
}
#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
else if (addrfam == AF_INET6)
{
- int opt;
-
# if defined(IPV6_RECVHOPLIMIT)
/* For details see RFC 3542, section 6.3. */
# if defined(IPV6_RECVHOPLIMIT)
/* For details see RFC 3542, section 6.3. */
- opt = 1;
setsockopt (fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
setsockopt (fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
- &opt, sizeof (opt));
+ &(int){1}, sizeof(int));
# endif /* IPV6_RECVHOPLIMIT */
# if defined(IPV6_RECVTCLASS)
/* For details see RFC 3542, section 6.5. */
# endif /* IPV6_RECVHOPLIMIT */
# if defined(IPV6_RECVTCLASS)
/* For details see RFC 3542, section 6.5. */
- opt = 1;
setsockopt (fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
setsockopt (fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
- &opt, sizeof (opt));
+ &(int){1}, sizeof(int));
# endif /* IPV6_RECVTCLASS */
}
#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
# endif /* IPV6_RECVTCLASS */
}
#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
{
pingobj_t *obj;
{
pingobj_t *obj;
- if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
+ if ((obj = malloc (sizeof (*obj))) == NULL)
return (NULL);
return (NULL);
- memset (obj, 0, sizeof (pingobj_t));
+ memset (obj, 0, sizeof (*obj));
obj->timeout = PING_DEF_TIMEOUT;
obj->ttl = PING_DEF_TTL;
obj->timeout = PING_DEF_TIMEOUT;
obj->ttl = PING_DEF_TTL;
void ping_destroy (pingobj_t *obj)
{
pinghost_t *current;
void ping_destroy (pingobj_t *obj)
{
pinghost_t *current;
- pinghost_t *next;
if (obj == NULL)
return;
current = obj->head;
if (obj == NULL)
return;
current = obj->head;
- next = NULL;
while (current != NULL)
{
while (current != NULL)
{
- next = current->next;
+ pinghost_t *next = current->next;
ping_free (current);
current = next;
}
ping_free (current);
current = next;
}
- if (obj->data != NULL)
- free (obj->data);
-
- if (obj->srcaddr != NULL)
- free (obj->srcaddr);
-
- if (obj->device != NULL)
- free (obj->device);
+ free (obj->data);
+ free (obj->srcaddr);
+ free (obj->device);
if (obj->fd4 != -1)
close(obj->fd4);
if (obj->fd4 != -1)
close(obj->fd4);
struct timeval nowtime;
struct timeval timeout;
struct timeval nowtime;
struct timeval timeout;
- int ret = 0;
-
_Bool need_ipv4_socket = 0;
_Bool need_ipv6_socket = 0;
_Bool need_ipv4_socket = 0;
_Bool need_ipv6_socket = 0;
* receive a "pong" yet. */
int pings_in_flight = 0;
* receive a "pong" yet. */
int pings_in_flight = 0;
+ /* pongs_received is the number of echo replies received. Unless there
+ * is an error, this is used as the return value of ping_send(). */
+ int pongs_received = 0;
+
+ int error_count = 0;
+
while (pings_in_flight > 0 || host_to_ping != NULL)
{
fd_set read_fds;
while (pings_in_flight > 0 || host_to_ping != NULL)
{
fd_set read_fds;
if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
break;
if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
break;
- dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
- (int) timeout.tv_sec,
- (int) timeout.tv_usec);
+ dprintf ("Waiting on %i sockets for %u.%06u seconds\n",
+ ((obj->fd4 != -1) ? 1 : 0) + ((obj->fd6 != -1) ? 1 : 0),
+ (unsigned) timeout.tv_sec,
+ (unsigned) timeout.tv_usec);
int status = select (max_fd + 1, &read_fds, &write_fds, NULL, &timeout);
int status = select (max_fd + 1, &read_fds, &write_fds, NULL, &timeout);
if (obj->fd6 != -1 && FD_ISSET (obj->fd6, &read_fds))
{
if (ping_receive_one (obj, &nowtime, AF_INET6) == 0)
if (obj->fd6 != -1 && FD_ISSET (obj->fd6, &read_fds))
{
if (ping_receive_one (obj, &nowtime, AF_INET6) == 0)
+ {
pings_in_flight--;
pings_in_flight--;
+ pongs_received++;
+ }
continue;
}
if (obj->fd4 != -1 && FD_ISSET (obj->fd4, &read_fds))
{
if (ping_receive_one (obj, &nowtime, AF_INET) == 0)
continue;
}
if (obj->fd4 != -1 && FD_ISSET (obj->fd4, &read_fds))
{
if (ping_receive_one (obj, &nowtime, AF_INET) == 0)
+ {
pings_in_flight--;
pings_in_flight--;
+ pongs_received++;
+ }
continue;
}
continue;
}
if (ping_send_one (obj, host_to_ping, write_fd) == 0)
pings_in_flight++;
else
if (ping_send_one (obj, host_to_ping, write_fd) == 0)
pings_in_flight++;
else
- ret--;
+ error_count++;
host_to_ping = host_to_ping->next;
continue;
}
} /* while (1) */
host_to_ping = host_to_ping->next;
continue;
}
} /* while (1) */
- return (ret);
+ if (error_count)
+ return (-1 * error_count);
+ return (pongs_received);
} /* int ping_send */
static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
} /* int ping_send */
static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
return ((pingobj_iter_t *) iter->next);
}
return ((pingobj_iter_t *) iter->next);
}
+int ping_iterator_count (pingobj_t *obj)
+{
+ if (obj == NULL)
+ return 0;
+
+ int count = 0;
+ pingobj_iter_t *iter = obj->head;
+ while (iter) {
+ count++;
+ iter = iter->next;
+ }
+ return count;
+}
+
int ping_iterator_get_info (pingobj_iter_t *iter, int info,
void *buffer, size_t *buffer_len)
{
int ping_iterator_get_info (pingobj_iter_t *iter, int info,
void *buffer, size_t *buffer_len)
{