* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
-#include <assert.h>
+#if STDC_HEADERS
+# include <stdlib.h>
+# include <stdio.h>
+# include <string.h>
+# include <errno.h>
+# include <assert.h>
+#else
+# error "You don't have the standard C99 header files installed"
+#endif /* STDC_HEADERS */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
+#if HAVE_UNISTD_H
+# include <unistd.h>
+#endif
-#include <sys/socket.h>
-#include <netdb.h>
+#if HAVE_FCNTL_H
+# include <fcntl.h>
+#endif
+#if HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+#if HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
-#include <netinet/in_systm.h>
-#include <netinet/in.h>
-#include <netinet/ip.h>
-#include <netinet/ip_icmp.h>
-#ifdef HAVE_NETINET_IP_VAR_H
-# include <netinet/ip_var.h>
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
#endif
-#include <netinet/ip6.h>
-#include <netinet/icmp6.h>
+#if HAVE_SYS_SOCKET_H
+# include <sys/socket.h>
+#endif
+#if HAVE_NETDB_H
+# include <netdb.h>
+#endif
-#include <sys/time.h>
-#include <time.h>
+#if HAVE_NETINET_IN_SYSTM_H
+# include <netinet/in_systm.h>
+#endif
+#if HAVE_NETINET_IN_H
+# include <netinet/in.h>
+#endif
+#if HAVE_NETINET_IP_H
+# include <netinet/ip.h>
+#endif
+#if HAVE_NETINET_IP_ICMP_H
+# include <netinet/ip_icmp.h>
+#endif
+#ifdef HAVE_NETINET_IP_VAR_H
+# include <netinet/ip_var.h>
+#endif
+#if HAVE_NETINET_IP6_H
+# include <netinet/ip6.h>
+#endif
+#if HAVE_NETINET_ICMP6_H
+# include <netinet/icmp6.h>
+#endif
#include "liboping.h"
/*
* private (static) functions
*/
+static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
+ struct timeval *res)
+{
+ res->tv_sec = tv1->tv_sec + tv2->tv_sec;
+ res->tv_usec = tv1->tv_usec + tv2->tv_usec;
+
+ while (res->tv_usec > 1000000)
+ {
+ res->tv_usec -= 1000000;
+ res->tv_sec++;
+ }
+
+ return (0);
+}
+
static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2,
struct timeval *res)
{
return (0);
}
-static int ping_receive_all (pinghost_t *ph)
+static int ping_receive_all (pingobj_t *obj)
{
fd_set readfds;
int num_readfds;
int max_readfds;
+ pinghost_t *ph;
pinghost_t *ptr;
struct timeval endtime;
int ret;
+ ph = obj->head;
ret = 0;
for (ptr = ph; ptr != NULL; ptr = ptr->next)
ptr->latency = -1.0;
- if (gettimeofday (&endtime, NULL) == -1)
+ if (gettimeofday (&nowtime, NULL) == -1)
return (-1);
- endtime.tv_sec += 1;
+
+ /* Set up timeout */
+ timeout.tv_sec = (time_t) obj->timeout;
+ timeout.tv_usec = (suseconds_t) (1000000 * (obj->timeout - ((double) timeout.tv_sec)));
+
+ dprintf ("Set timeout to %i.%06i seconds\n",
+ (int) timeout.tv_sec,
+ (int) timeout.tv_usec);
+
+ ping_timeval_add (&nowtime, &timeout, &endtime);
while (1)
{
return (0);
}
+/*
+ * Set the TTL of a socket protocol independently.
+ */
+static int ping_set_ttl (pinghost_t *ph, int ttl)
+{
+ int ret = -2;
+
+ if (ph->addrfamily == AF_INET)
+ {
+ ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
+ }
+ else if (ph->addrfamily == AF_INET6)
+ {
+ ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
+ }
+
+ return (ret);
+}
+
static int ping_get_ident (void)
{
int fd;
if (ping_send_all (obj->head) < 0)
return (-1);
- if ((ret = ping_receive_all (obj->head)) < 0)
+ if ((ret = ping_receive_all (obj)) < 0)
return (-2);
return (ret);
#ifdef AI_ADDRCONFIG
ai_hints.ai_flags |= AI_ADDRCONFIG;
#endif
- ai_hints.ai_family = PF_UNSPEC;
+ ai_hints.ai_family = obj->addrfamily;
ai_hints.ai_socktype = SOCK_RAW;
if ((ph = ping_alloc ()) == NULL)
ph->next = obj->head;
obj->head = ph;
+ ping_set_ttl (ph, obj->ttl);
+
return (0);
}