diff --git a/src/liboping.c b/src/liboping.c
index c7bdaecb08396855abd87e15a75d975ea0fcece8..4626e6d26e55710ca89c8fee94dc0106184c9e69 100644 (file)
--- a/src/liboping.c
+++ b/src/liboping.c
/**
* Object oriented C module to send ICMP and ICMPv6 `echo's.
/**
* Object oriented C module to send ICMP and ICMPv6 `echo's.
- * Copyright (C) 2006-2009 Florian octo Forster <octo at verplant.org>
+ * Copyright (C) 2006-2016 Florian octo Forster <ff at octo.it>
*
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; only version 2 of the License is
- * applicable.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by the
+ * Free Software Foundation; either version 2.1 of the License, or (at your
+ * option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
*/
+#ifdef __APPLE__
+#define __APPLE_USE_RFC_3542
+#endif
+
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if HAVE_CONFIG_H
# include <config.h>
#endif
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
-# include <stdint.h>
# include <inttypes.h>
# include <errno.h>
# include <assert.h>
# include <inttypes.h>
# include <errno.h>
# include <assert.h>
# error "You don't have the standard C99 header files installed"
#endif /* STDC_HEADERS */
# error "You don't have the standard C99 header files installed"
#endif /* STDC_HEADERS */
+#ifdef HAVE_STDINT_H
+# include <stdint.h>
+#endif
+
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#if HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
+
#if HAVE_NETDB_H
# include <netdb.h>
#endif
#if HAVE_NETDB_H
# include <netdb.h>
#endif
struct timeval *timer;
double latency;
uint32_t dropped;
struct timeval *timer;
double latency;
uint32_t dropped;
+ int recv_ttl;
+ uint8_t recv_qos;
char *data;
void *context;
char *data;
void *context;
double timeout;
int ttl;
int addrfamily;
double timeout;
int ttl;
int addrfamily;
+ uint8_t qos;
char *data;
char *data;
- struct sockaddr_storage *srcaddr;
+ struct sockaddr *srcaddr;
socklen_t srcaddrlen;
socklen_t srcaddrlen;
+ char *device;
+
+ char set_mark;
+ int mark;
+
char errmsg[PING_ERRMSG_LEN];
pinghost_t *head;
char errmsg[PING_ERRMSG_LEN];
pinghost_t *head;
/*
* private (static) functions
*/
/*
* private (static) functions
*/
+/* Even though Posix requires "strerror_r" to return an "int",
+ * some systems (e.g. the GNU libc) return a "char *" _and_
+ * ignore the second argument ... -tokkee */
+static char *sstrerror (int errnum, char *buf, size_t buflen)
+{
+ buf[0] = 0;
+
+#if !HAVE_STRERROR_R
+ {
+ snprintf (buf, buflen, "Error %i (%#x)", errnum, errnum);
+ }
+/* #endif !HAVE_STRERROR_R */
+
+#elif STRERROR_R_CHAR_P
+ {
+ char *temp;
+ temp = strerror_r (errnum, buf, buflen);
+ if (buf[0] == 0)
+ {
+ if ((temp != NULL) && (temp != buf) && (temp[0] != 0))
+ strncpy (buf, temp, buflen);
+ else
+ strncpy (buf, "strerror_r did not return "
+ "an error message", buflen);
+ }
+ }
+/* #endif STRERROR_R_CHAR_P */
+
+#else
+ if (strerror_r (errnum, buf, buflen) != 0)
+ {
+ snprintf (buf, buflen, "Error %i (%#x); "
+ "Additionally, strerror_r failed.",
+ errnum, errnum);
+ }
+#endif /* STRERROR_R_CHAR_P */
+
+ buf[buflen - 1] = 0;
+
+ return (buf);
+} /* char *sstrerror */
+
static void ping_set_error (pingobj_t *obj, const char *function,
const char *message)
{
static void ping_set_error (pingobj_t *obj, const char *function,
const char *message)
{
- snprintf (obj->errmsg, PING_ERRMSG_LEN, "%s: %s", function, message);
- obj->errmsg[PING_ERRMSG_LEN - 1] = '\0';
+ snprintf (obj->errmsg, sizeof (obj->errmsg),
+ "%s: %s", function, message);
+ obj->errmsg[sizeof (obj->errmsg) - 1] = 0;
+}
+
+static void ping_set_errno (pingobj_t *obj, int error_number)
+{
+ sstrerror (error_number, obj->errmsg, sizeof (obj->errmsg));
}
static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
}
static int ping_timeval_add (struct timeval *tv1, struct timeval *tv2,
return (ret);
}
return (ret);
}
-static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffer_len)
+static pinghost_t *ping_receive_ipv4 (pingobj_t *obj, char *buffer,
+ size_t buffer_len)
{
struct ip *ip_hdr;
struct icmp *icmp_hdr;
{
struct ip *ip_hdr;
struct icmp *icmp_hdr;
@@ -233,27 +298,25 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
buffer += ip_hdr_len;
buffer_len -= ip_hdr_len;
buffer += ip_hdr_len;
buffer_len -= ip_hdr_len;
- if (buffer_len < sizeof (struct icmp))
+ if (buffer_len < ICMP_MINLEN)
return (NULL);
icmp_hdr = (struct icmp *) buffer;
return (NULL);
icmp_hdr = (struct icmp *) buffer;
- buffer += sizeof (struct icmp);
- buffer_len -= sizeof (struct icmp);
-
if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
{
if (icmp_hdr->icmp_type != ICMP_ECHOREPLY)
{
- dprintf ("Unexpected ICMP type: %i\n", icmp_hdr->icmp_type);
+ dprintf ("Unexpected ICMP type: %"PRIu8"\n", icmp_hdr->icmp_type);
return (NULL);
}
recv_checksum = icmp_hdr->icmp_cksum;
return (NULL);
}
recv_checksum = icmp_hdr->icmp_cksum;
+ /* This writes to buffer. */
icmp_hdr->icmp_cksum = 0;
icmp_hdr->icmp_cksum = 0;
- calc_checksum = ping_icmp4_checksum ((char *) icmp_hdr,
- sizeof (struct icmp) + buffer_len);
+ calc_checksum = ping_icmp4_checksum (buffer, buffer_len);
if (recv_checksum != calc_checksum)
{
if (recv_checksum != calc_checksum)
{
- dprintf ("Checksum missmatch: Got 0x%04x, calculated 0x%04x\n",
+ dprintf ("Checksum missmatch: Got 0x%04"PRIx16", "
+ "calculated 0x%04"PRIx16"\n",
recv_checksum, calc_checksum);
return (NULL);
}
recv_checksum, calc_checksum);
return (NULL);
}
@@ -261,7 +324,9 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
ident = ntohs (icmp_hdr->icmp_id);
seq = ntohs (icmp_hdr->icmp_seq);
ident = ntohs (icmp_hdr->icmp_id);
seq = ntohs (icmp_hdr->icmp_seq);
- for (ptr = ph; ptr != NULL; ptr = ptr->next)
+ /* We have to iterate over all hosts, since ICMPv4 packets may
+ * be received on any raw v4 socket. */
+ for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
{
dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
{
dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
@@ -278,7 +343,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
if (((ptr->sequence - 1) & 0xFFFF) != seq)
continue;
if (((ptr->sequence - 1) & 0xFFFF) != seq)
continue;
- dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
+ dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
+ "seq = %"PRIu16"\n",
ptr->hostname, ident, seq);
break;
ptr->hostname, ident, seq);
break;
@@ -286,10 +352,14 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
if (ptr == NULL)
{
if (ptr == NULL)
{
- dprintf ("No match found for ident = 0x%04x, seq = %i\n",
+ dprintf ("No match found for ident = 0x%04"PRIx16", seq = %"PRIu16"\n",
ident, seq);
}
ident, seq);
}
+ if (ptr != NULL){
+ ptr->recv_ttl = (int) ip_hdr->ip_ttl;
+ ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
+ }
return (ptr);
}
return (ptr);
}
@@ -309,7 +379,8 @@ static pinghost_t *ping_receive_ipv4 (pinghost_t *ph, char *buffer, size_t buffe
# endif
#endif
# endif
#endif
-static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffer_len)
+static pinghost_t *ping_receive_ipv6 (pingobj_t *obj, char *buffer,
+ size_t buffer_len)
{
struct icmp6_hdr *icmp_hdr;
{
struct icmp6_hdr *icmp_hdr;
@@ -318,12 +389,12 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
pinghost_t *ptr;
pinghost_t *ptr;
- if (buffer_len < sizeof (struct icmp6_hdr))
+ if (buffer_len < ICMP_MINLEN)
return (NULL);
icmp_hdr = (struct icmp6_hdr *) buffer;
return (NULL);
icmp_hdr = (struct icmp6_hdr *) buffer;
- buffer += sizeof (struct icmp);
- buffer_len -= sizeof (struct icmp);
+ buffer += ICMP_MINLEN;
+ buffer_len -= ICMP_MINLEN;
if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
{
if (icmp_hdr->icmp6_type != ICMP6_ECHO_REPLY)
{
@@ -340,7 +411,9 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
ident = ntohs (icmp_hdr->icmp6_id);
seq = ntohs (icmp_hdr->icmp6_seq);
ident = ntohs (icmp_hdr->icmp6_id);
seq = ntohs (icmp_hdr->icmp6_seq);
- for (ptr = ph; ptr != NULL; ptr = ptr->next)
+ /* We have to iterate over all hosts, since ICMPv6 packets may
+ * be received on any raw v6 socket. */
+ for (ptr = obj->head; ptr != NULL; ptr = ptr->next)
{
dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
{
dprintf ("hostname = %s, ident = 0x%04x, seq = %i\n",
ptr->hostname, ptr->ident, ((ptr->sequence - 1) & 0xFFFF));
@@ -357,7 +430,8 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
if (((ptr->sequence - 1) & 0xFFFF) != seq)
continue;
if (((ptr->sequence - 1) & 0xFFFF) != seq)
continue;
- dprintf ("Match found: hostname = %s, ident = 0x%04x, seq = %i\n",
+ dprintf ("Match found: hostname = %s, ident = 0x%04"PRIx16", "
+ "seq = %"PRIu16"\n",
ptr->hostname, ident, seq);
break;
ptr->hostname, ident, seq);
break;
@@ -365,56 +439,183 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe
if (ptr == NULL)
{
if (ptr == NULL)
{
- dprintf ("No match found for ident = 0x%04x, seq = %i\n",
+ dprintf ("No match found for ident = 0x%04"PRIx16", "
+ "seq = %"PRIu16"\n",
ident, seq);
}
return (ptr);
}
ident, seq);
}
return (ptr);
}
-static int ping_receive_one (int fd, pinghost_t *ph, struct timeval *now)
+static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph,
+ struct timeval *now)
{
{
- char buffer[4096];
- ssize_t buffer_len;
-
- struct timeval diff;
+ /* Note: 'ph' is not necessarily the host object for which we receive a
+ * reply. The right object will be returned by ping_receive_ipv*(). For
+ * now, we can only rely on ph->fd and ph->addrfamily. */
+ struct timeval diff, pkt_now = *now;
pinghost_t *host = NULL;
pinghost_t *host = NULL;
+ int recv_ttl;
+ uint8_t recv_qos;
+
+ /*
+ * Set up the receive buffer..
+ */
+ struct msghdr msghdr;
+ struct cmsghdr *cmsg;
+ char payload_buffer[4096];
+ ssize_t payload_buffer_len;
+ char control_buffer[4096];
+ struct iovec payload_iovec;
+
+ memset (&payload_iovec, 0, sizeof (payload_iovec));
+ payload_iovec.iov_base = payload_buffer;
+ payload_iovec.iov_len = sizeof (payload_buffer);
+
+ memset (&msghdr, 0, sizeof (msghdr));
+ /* unspecified source address */
+ msghdr.msg_name = NULL;
+ msghdr.msg_namelen = 0;
+ /* output buffer vector, see readv(2) */
+ msghdr.msg_iov = &payload_iovec;
+ msghdr.msg_iovlen = 1;
+ /* output buffer for control messages */
+ msghdr.msg_control = control_buffer;
+ msghdr.msg_controllen = sizeof (control_buffer);
+ /* flags; this is an output only field.. */
+ msghdr.msg_flags = 0;
+#ifdef MSG_XPG4_2
+ msghdr.msg_flags |= MSG_XPG4_2;
+#endif
- struct sockaddr_storage sa;
- socklen_t sa_len;
-
- sa_len = sizeof (sa);
-
- buffer_len = recvfrom (fd, buffer, sizeof (buffer), 0,
- (struct sockaddr *) &sa, &sa_len);
- if (buffer_len < 0)
+ payload_buffer_len = recvmsg (ph->fd, &msghdr, /* flags = */ 0);
+ if (payload_buffer_len < 0)
{
{
- dprintf ("recvfrom: %s\n", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("recvfrom: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
return (-1);
}
return (-1);
}
+ dprintf ("Read %zi bytes from fd = %i\n", payload_buffer_len, ph->fd);
+
+ /* Iterate over all auxiliary data in msghdr */
+ recv_ttl = -1;
+ recv_qos = 0;
+ for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
+ cmsg != NULL;
+ cmsg = CMSG_NXTHDR (&msghdr, cmsg))
+ {
+ if (cmsg->cmsg_level == SOL_SOCKET)
+ {
+#ifdef SO_TIMESTAMP
+ if (cmsg->cmsg_type == SO_TIMESTAMP)
+ memcpy (&pkt_now, CMSG_DATA (cmsg), sizeof (pkt_now));
+#endif /* SO_TIMESTAMP */
+ }
+ else if (ph->addrfamily == AF_INET) /* {{{ */
+ {
+ if (cmsg->cmsg_level != IPPROTO_IP)
+ continue;
+
+ if (cmsg->cmsg_type == IP_TOS)
+ {
+ memcpy (&recv_qos, CMSG_DATA (cmsg),
+ sizeof (recv_qos));
+ dprintf ("TOSv4 = 0x%02"PRIx8";\n", recv_qos);
+ } else
+ if (cmsg->cmsg_type == IP_TTL)
+ {
+ memcpy (&recv_ttl, CMSG_DATA (cmsg),
+ sizeof (recv_ttl));
+ dprintf ("TTLv4 = %i;\n", recv_ttl);
+ }
+ else
+ {
+ dprintf ("Not handling option %i.\n",
+ cmsg->cmsg_type);
+ }
+ } /* }}} */
+ else if (ph->addrfamily == AF_INET6) /* {{{ */
+ {
+ if (cmsg->cmsg_level != IPPROTO_IPV6)
+ continue;
- dprintf ("Read %zi bytes from fd = %i\n", buffer_len, fd);
+ if (cmsg->cmsg_type == IPV6_TCLASS)
+ {
+ memcpy (&recv_qos, CMSG_DATA (cmsg),
+ sizeof (recv_qos));
+ dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
+ } else
+#ifdef IPV6_HOPLIMIT
+ if (cmsg->cmsg_type == IPV6_HOPLIMIT)
+ {
+ memcpy (&recv_ttl, CMSG_DATA (cmsg),
+ sizeof (recv_ttl));
+ dprintf ("TTLv6 = %i;\n", recv_ttl);
+ }
+ else
+#endif
+#ifdef IPV6_UNICAST_HOPS
+ if (cmsg->cmsg_type == IPV6_UNICAST_HOPS)
+ {
+ memcpy (&recv_ttl, CMSG_DATA (cmsg),
+ sizeof (recv_ttl));
+ dprintf ("TTLv6 = %i;\n", recv_ttl);
+ }
+ else
+#endif
+#ifdef IPV6_MULTICAST_HOPS
+ if (cmsg->cmsg_type == IPV6_MULTICAST_HOPS)
+ {
+ memcpy (&recv_ttl, CMSG_DATA (cmsg),
+ sizeof (recv_ttl));
+ dprintf ("TTLv6 = %i;\n", recv_ttl);
+ }
+ else
+#endif
+ {
+ dprintf ("Not handling option %i.\n",
+ cmsg->cmsg_type);
+ }
+ } /* }}} */
+ else
+ {
+ dprintf ("Don't know how to handle "
+ "unknown protocol %i.\n",
+ cmsg->cmsg_level);
+ }
+ } /* }}} for (cmsg) */
- if (sa.ss_family == AF_INET)
+ if (ph->addrfamily == AF_INET)
{
{
- if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL)
+ host = ping_receive_ipv4 (obj, payload_buffer, payload_buffer_len);
+ if (host == NULL)
return (-1);
}
return (-1);
}
- else if (sa.ss_family == AF_INET6)
+ else if (ph->addrfamily == AF_INET6)
{
{
- if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL)
+ host = ping_receive_ipv6 (obj, payload_buffer, payload_buffer_len);
+ if (host == NULL)
return (-1);
}
return (-1);
}
+ else
+ {
+ dprintf ("ping_receive_one: Unknown address family %i.\n",
+ ph->addrfamily);
+ return (-1);
+ }
dprintf ("rcvd: %12i.%06i\n",
dprintf ("rcvd: %12i.%06i\n",
- (int) now->tv_sec,
- (int) now->tv_usec);
+ (int) pkt_now.tv_sec,
+ (int) pkt_now.tv_usec);
dprintf ("sent: %12i.%06i\n",
(int) host->timer->tv_sec,
(int) host->timer->tv_usec);
dprintf ("sent: %12i.%06i\n",
(int) host->timer->tv_sec,
(int) host->timer->tv_usec);
- if (ping_timeval_sub (now, host->timer, &diff) < 0)
+ if (ping_timeval_sub (&pkt_now, host->timer, &diff) < 0)
{
timerclear (host->timer);
return (-1);
{
timerclear (host->timer);
return (-1);
(int) diff.tv_sec,
(int) diff.tv_usec);
(int) diff.tv_sec,
(int) diff.tv_usec);
+ if (recv_ttl >= 0)
+ host->recv_ttl = recv_ttl;
+ host->recv_qos = recv_qos;
+
host->latency = ((double) diff.tv_usec) / 1000.0;
host->latency += ((double) diff.tv_sec) * 1000.0;
host->latency = ((double) diff.tv_usec) / 1000.0;
host->latency += ((double) diff.tv_sec) * 1000.0;
return (0);
}
return (0);
}
+/* Blocks until a packet was received from all hosts or the timeout is reached.
+ * When interrupted, (-EINTR) is returned. On error, -1 is returned. On
+ * success, returns zero. */
static int ping_receive_all (pingobj_t *obj)
{
static int ping_receive_all (pingobj_t *obj)
{
- fd_set readfds;
- int num_readfds;
- int max_readfds;
+ fd_set read_fds;
+ fd_set err_fds;
+ int num_fds;
+ int max_fd;
pinghost_t *ph;
pinghost_t *ptr;
pinghost_t *ph;
pinghost_t *ptr;
ret = 0;
for (ptr = ph; ptr != NULL; ptr = ptr->next)
ret = 0;
for (ptr = ph; ptr != NULL; ptr = ptr->next)
- ptr->latency = -1.0;
+ {
+ ptr->latency = -1.0;
+ ptr->recv_ttl = -1;
+ }
if (gettimeofday (&nowtime, NULL) == -1)
{
if (gettimeofday (&nowtime, NULL) == -1)
{
- ping_set_error (obj, "gettimeofday", strerror (errno));
+ ping_set_errno (obj, errno);
return (-1);
}
return (-1);
}
while (1)
{
while (1)
{
- FD_ZERO (&readfds);
- num_readfds = 0;
- max_readfds = -1;
+ FD_ZERO (&read_fds);
+ FD_ZERO (&err_fds);
+ num_fds = 0;
+ max_fd = -1;
for (ptr = ph; ptr != NULL; ptr = ptr->next)
{
if (!timerisset (ptr->timer))
continue;
for (ptr = ph; ptr != NULL; ptr = ptr->next)
{
if (!timerisset (ptr->timer))
continue;
- FD_SET (ptr->fd, &readfds);
- num_readfds++;
+ assert (ptr->fd < FD_SETSIZE);
+ FD_SET (ptr->fd, &read_fds);
+ FD_SET (ptr->fd, &err_fds);
+ num_fds++;
- if (max_readfds < ptr->fd)
- max_readfds = ptr->fd;
+ if (max_fd < ptr->fd)
+ max_fd = ptr->fd;
}
}
- if (num_readfds == 0)
+ if (num_fds == 0)
break;
if (gettimeofday (&nowtime, NULL) == -1)
{
break;
if (gettimeofday (&nowtime, NULL) == -1)
{
- ping_set_error (obj, "gettimeofday", strerror (errno));
+ ping_set_errno (obj, errno);
return (-1);
}
if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
break;
return (-1);
}
if (ping_timeval_sub (&endtime, &nowtime, &timeout) == -1)
break;
- dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_readfds,
+ dprintf ("Waiting on %i sockets for %i.%06i seconds\n", num_fds,
(int) timeout.tv_sec,
(int) timeout.tv_usec);
(int) timeout.tv_sec,
(int) timeout.tv_usec);
- status = select (max_readfds + 1, &readfds, NULL, NULL, &timeout);
+ status = select (max_fd + 1, &read_fds, NULL, &err_fds, &timeout);
if (gettimeofday (&nowtime, NULL) == -1)
{
if (gettimeofday (&nowtime, NULL) == -1)
{
- ping_set_error (obj, "gettimeofday", strerror (errno));
+ ping_set_errno (obj, errno);
return (-1);
}
if ((status == -1) && (errno == EINTR))
{
dprintf ("select was interrupted by signal..\n");
return (-1);
}
if ((status == -1) && (errno == EINTR))
{
dprintf ("select was interrupted by signal..\n");
- continue;
+ ping_set_errno (obj, EINTR);
+ return (-EINTR);
}
else if (status < 0)
{
}
else if (status < 0)
{
- dprintf ("select: %s\n", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("select: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
break;
}
else if (status == 0)
break;
}
else if (status == 0)
for (ptr = ph; ptr != NULL; ptr = ptr->next)
{
for (ptr = ph; ptr != NULL; ptr = ptr->next)
{
- if (FD_ISSET (ptr->fd, &readfds))
- if (ping_receive_one (ptr->fd, ph, &nowtime) == 0)
+ if (FD_ISSET (ptr->fd, &read_fds))
+ {
+ if (ping_receive_one (obj, ptr, &nowtime) == 0)
ret++;
ret++;
+ }
+ else if (FD_ISSET (ptr->fd, &err_fds))
+ {
+ /* clear the timer in this case so that we
+ * don't run into an endless loop. */
+ /* TODO: Set an error flag in this case. */
+ timerclear (ptr->timer);
+ }
}
} /* while (1) */
return (ret);
}
} /* while (1) */
return (ret);
-}
+} /* int ping_receive_all */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Sending functions: *
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Sending functions: *
if (errno == ENETUNREACH)
return (0);
#endif
if (errno == ENETUNREACH)
return (0);
#endif
- ping_set_error (obj, "sendto", strerror (errno));
+ ping_set_errno (obj, errno);
}
return (ret);
}
return (ret);
struct icmp *icmp4;
int status;
struct icmp *icmp4;
int status;
- char buf[4096];
- int buflen;
+ char buf[4096] = {0};
+ size_t buflen;
char *data;
char *data;
- int datalen;
+ size_t datalen;
dprintf ("ph->hostname = %s\n", ph->hostname);
dprintf ("ph->hostname = %s\n", ph->hostname);
- memset (buf, '\0', sizeof (buf));
icmp4 = (struct icmp *) buf;
icmp4 = (struct icmp *) buf;
- data = (char *) (icmp4 + 1);
-
- icmp4->icmp_type = ICMP_ECHO;
- icmp4->icmp_code = 0;
- icmp4->icmp_cksum = 0;
- icmp4->icmp_id = htons (ph->ident);
- icmp4->icmp_seq = htons (ph->sequence);
+ *icmp4 = (struct icmp) {
+ .icmp_type = ICMP_ECHO,
+ .icmp_id = htons (ph->ident),
+ .icmp_seq = htons (ph->sequence),
+ };
- buflen = 4096 - sizeof (struct icmp);
- strncpy (data, ph->data, buflen);
- datalen = strlen (data);
+ datalen = strlen (ph->data);
+ buflen = ICMP_MINLEN + datalen;
+ if (sizeof (buf) < buflen)
+ return (EINVAL);
- buflen = datalen + sizeof (struct icmp);
+ data = buf + ICMP_MINLEN;
+ memcpy (data, ph->data, datalen);
icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
icmp4->icmp_cksum = ping_icmp4_checksum (buf, buflen);
struct icmp6_hdr *icmp6;
int status;
struct icmp6_hdr *icmp6;
int status;
- char buf[4096];
+ char buf[4096] = {0};
int buflen;
char *data;
int buflen;
char *data;
dprintf ("ph->hostname = %s\n", ph->hostname);
dprintf ("ph->hostname = %s\n", ph->hostname);
- memset (buf, '\0', sizeof (buf));
icmp6 = (struct icmp6_hdr *) buf;
icmp6 = (struct icmp6_hdr *) buf;
- data = (char *) (icmp6 + 1);
+ *icmp6 = (struct icmp6_hdr) {
+ .icmp6_type = ICMP6_ECHO_REQUEST,
+ .icmp6_id = htons (ph->ident),
+ .icmp6_seq = htons (ph->sequence),
+ };
- icmp6->icmp6_type = ICMP6_ECHO_REQUEST;
- icmp6->icmp6_code = 0;
- /* The checksum will be calculated by the TCP/IP stack. */
- /* FIXME */
- icmp6->icmp6_cksum = 0;
- icmp6->icmp6_id = htons (ph->ident);
- icmp6->icmp6_seq = htons (ph->sequence);
+ datalen = strlen (ph->data);
+ buflen = sizeof (*icmp6) + datalen;
+ if (sizeof (buf) < buflen)
+ return (EINVAL);
- buflen = 4096 - sizeof (struct icmp6_hdr);
- strncpy (data, ph->data, buflen);
- datalen = strlen (data);
+ data = buf + ICMP_MINLEN;
+ memcpy (data, ph->data, datalen);
- buflen = datalen + sizeof (struct icmp6_hdr);
+ /* The checksum will be calculated by the TCP/IP stack. */
dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
dprintf ("Sending ICMPv6 package with ID 0x%04x\n", ph->ident);
* sending the packet, so I will do that too */
if (gettimeofday (ptr->timer, NULL) == -1)
{
* sending the packet, so I will do that too */
if (gettimeofday (ptr->timer, NULL) == -1)
{
- dprintf ("gettimeofday: %s\n", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("gettimeofday: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
timerclear (ptr->timer);
ret--;
continue;
timerclear (ptr->timer);
ret--;
continue;
if (ph->addrfamily == AF_INET)
{
if (ph->addrfamily == AF_INET)
{
- ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL, &ttl, sizeof (ttl));
+ dprintf ("Setting TTLv4 to %i\n", ttl);
+ ret = setsockopt (ph->fd, IPPROTO_IP, IP_TTL,
+ &ttl, sizeof (ttl));
}
else if (ph->addrfamily == AF_INET6)
{
}
else if (ph->addrfamily == AF_INET6)
{
- ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof (ttl));
+ dprintf ("Setting TTLv6 to %i\n", ttl);
+ ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
+ &ttl, sizeof (ttl));
+ }
+
+ return (ret);
+}
+
+/*
+ * Set the TOS of a socket protocol independently.
+ *
+ * Using SOL_SOCKET / SO_PRIORITY might be a protocol independent way to
+ * set this. See socket(7) for details.
+ */
+static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
+{
+ int ret = EINVAL;
+ char errbuf[PING_ERRMSG_LEN];
+
+ if (ph->addrfamily == AF_INET)
+ {
+ dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
+ ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
+ &qos, sizeof (qos));
+ if (ret != 0)
+ {
+ ret = errno;
+ ping_set_error (obj, "ping_set_qos",
+ sstrerror (ret, errbuf, sizeof (errbuf)));
+ dprintf ("Setting TP_TOS failed: %s\n", errbuf);
+ }
+ }
+ else if (ph->addrfamily == AF_INET6)
+ {
+ /* IPV6_TCLASS requires an "int". */
+ int tmp = (int) qos;
+
+ dprintf ("Setting IPV6_TCLASS to %#04"PRIx8" (%i)\n", qos, tmp);
+ ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
+ &tmp, sizeof (tmp));
+ if (ret != 0)
+ {
+ ret = errno;
+ ping_set_error (obj, "ping_set_qos",
+ sstrerror (ret, errbuf, sizeof (errbuf)));
+ dprintf ("Setting IPV6_TCLASS failed: %s\n", errbuf);
+ }
}
return (ret);
}
return (ret);
close (fd);
}
close (fd);
}
+#if WITH_DEBUG
else
{
else
{
- dprintf ("open (/dev/urandom): %s\n", strerror (errno));
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("open (/dev/urandom): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
}
}
+#endif
}
retval = (int) random ();
}
retval = (int) random ();
*/
const char *ping_get_error (pingobj_t *obj)
{
*/
const char *ping_get_error (pingobj_t *obj)
{
+ if (obj == NULL)
+ return (NULL);
return (obj->errmsg);
}
return (obj->errmsg);
}
if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
return (NULL);
if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL)
return (NULL);
- memset (obj, '\0', sizeof (pingobj_t));
+ memset (obj, 0, sizeof (pingobj_t));
obj->timeout = PING_DEF_TIMEOUT;
obj->ttl = PING_DEF_TTL;
obj->addrfamily = PING_DEF_AF;
obj->data = strdup (PING_DEF_DATA);
obj->timeout = PING_DEF_TIMEOUT;
obj->ttl = PING_DEF_TTL;
obj->addrfamily = PING_DEF_AF;
obj->data = strdup (PING_DEF_DATA);
+ obj->qos = 0;
return (obj);
}
return (obj);
}
pinghost_t *current;
pinghost_t *next;
pinghost_t *current;
pinghost_t *next;
+ if (obj == NULL)
+ return;
+
current = obj->head;
next = NULL;
current = obj->head;
next = NULL;
if (obj->srcaddr != NULL)
free (obj->srcaddr);
if (obj->srcaddr != NULL)
free (obj->srcaddr);
+ if (obj->device != NULL)
+ free (obj->device);
+
free (obj);
return;
free (obj);
return;
{
int ret = 0;
{
int ret = 0;
+ if ((obj == NULL) || (value == NULL))
+ return (-1);
+
switch (option)
{
switch (option)
{
+ case PING_OPT_QOS:
+ {
+ pinghost_t *ph;
+
+ obj->qos = *((uint8_t *) value);
+ for (ph = obj->head; ph != NULL; ph = ph->next)
+ ping_set_qos (obj, ph, obj->qos);
+ break;
+ }
+
case PING_OPT_TIMEOUT:
obj->timeout = *((double *) value);
if (obj->timeout < 0.0)
case PING_OPT_TIMEOUT:
obj->timeout = *((double *) value);
if (obj->timeout < 0.0)
obj->ttl = PING_DEF_TTL;
ret = -1;
}
obj->ttl = PING_DEF_TTL;
ret = -1;
}
+ else
+ {
+ pinghost_t *ph;
+
+ for (ph = obj->head; ph != NULL; ph = ph->next)
+ ping_set_ttl (ph, obj->ttl);
+ }
break;
case PING_OPT_AF:
break;
case PING_OPT_AF:
status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
if (status != 0)
{
status = getaddrinfo (hostname, NULL, &ai_hints, &ai_list);
if (status != 0)
{
+#if defined(EAI_SYSTEM)
+ char errbuf[PING_ERRMSG_LEN];
+#endif
ping_set_error (obj, "getaddrinfo",
#if defined(EAI_SYSTEM)
(status == EAI_SYSTEM)
ping_set_error (obj, "getaddrinfo",
#if defined(EAI_SYSTEM)
(status == EAI_SYSTEM)
- ? strerror (errno) :
+ ? sstrerror (errno, errbuf, sizeof (errbuf)) :
#endif
gai_strerror (status));
ret = -1;
#endif
gai_strerror (status));
ret = -1;
if (obj->srcaddr == NULL)
{
obj->srcaddrlen = 0;
if (obj->srcaddr == NULL)
{
obj->srcaddrlen = 0;
- obj->srcaddr = (struct sockaddr_storage *) malloc (sizeof (struct sockaddr_storage));
+ obj->srcaddr = malloc (sizeof (struct sockaddr_storage));
if (obj->srcaddr == NULL)
{
if (obj->srcaddr == NULL)
{
- ping_set_error (obj, "malloc",
- strerror (errno));
+ ping_set_errno (obj, errno);
ret = -1;
freeaddrinfo (ai_list);
break;
}
}
ret = -1;
freeaddrinfo (ai_list);
break;
}
}
- memset ((void *) obj->srcaddr, '\0', sizeof (struct sockaddr_storage));
+ memset ((void *) obj->srcaddr, 0, sizeof (struct sockaddr_storage));
assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
ai_list->ai_addrlen);
assert (ai_list->ai_addrlen <= sizeof (struct sockaddr_storage));
memcpy ((void *) obj->srcaddr, (const void *) ai_list->ai_addr,
ai_list->ai_addrlen);
} /* case PING_OPT_SOURCE */
break;
} /* case PING_OPT_SOURCE */
break;
+ case PING_OPT_DEVICE:
+ {
+#ifdef SO_BINDTODEVICE
+ char *device = strdup ((char *) value);
+
+ if (device == NULL)
+ {
+ ping_set_errno (obj, errno);
+ ret = -1;
+ break;
+ }
+
+ if (obj->device != NULL)
+ free (obj->device);
+ obj->device = device;
+#else /* ! SO_BINDTODEVICE */
+ ping_set_errno (obj, ENOTSUP);
+ ret = -1;
+#endif /* ! SO_BINDTODEVICE */
+ } /* case PING_OPT_DEVICE */
+ break;
+
+ case PING_OPT_MARK:
+ {
+#ifdef SO_MARK
+ obj->mark = *(int*)(value);
+ obj->set_mark = 1;
+#else /* SO_MARK */
+ ping_set_errno (obj, ENOTSUP);
+ ret = -1;
+#endif /* !SO_MARK */
+
+ } /* case PING_OPT_MARK */
+ break;
+
default:
ret = -2;
} /* switch (option) */
default:
ret = -2;
} /* switch (option) */
int ping_send (pingobj_t *obj)
{
int ping_send (pingobj_t *obj)
{
- int ret;
+ if (obj == NULL)
+ return (-1);
if (ping_send_all (obj) < 0)
return (-1);
if (ping_send_all (obj) < 0)
return (-1);
- if ((ret = ping_receive_all (obj)) < 0)
- return (-2);
-
- return (ret);
+ return (ping_receive_all (obj));
}
static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
}
static pinghost_t *ping_host_search (pinghost_t *ph, const char *host)
{
pinghost_t *ph;
{
pinghost_t *ph;
- struct sockaddr_storage sockaddr;
- socklen_t sockaddr_len;
-
struct addrinfo ai_hints;
struct addrinfo *ai_list, *ai_ptr;
int ai_return;
struct addrinfo ai_hints;
struct addrinfo *ai_list, *ai_ptr;
int ai_return;
+ if ((obj == NULL) || (host == NULL))
+ return (-1);
+
dprintf ("host = %s\n", host);
if (ping_host_search (obj->head, host) != NULL)
dprintf ("host = %s\n", host);
if (ping_host_search (obj->head, host) != NULL)
if ((ph->username = strdup (host)) == NULL)
{
dprintf ("Out of memory!\n");
if ((ph->username = strdup (host)) == NULL)
{
dprintf ("Out of memory!\n");
- ping_set_error (obj, "strdup", strerror (errno));
+ ping_set_errno (obj, errno);
ping_free (ph);
return (-1);
}
ping_free (ph);
return (-1);
}
if ((ph->hostname = strdup (host)) == NULL)
{
dprintf ("Out of memory!\n");
if ((ph->hostname = strdup (host)) == NULL)
{
dprintf ("Out of memory!\n");
- ping_set_error (obj, "strdup", strerror (errno));
+ ping_set_errno (obj, errno);
ping_free (ph);
return (-1);
}
ping_free (ph);
return (-1);
}
if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
{
dprintf ("Out of memory!\n");
if ((ph->data = strdup (obj->data == NULL ? PING_DEF_DATA : obj->data)) == NULL)
{
dprintf ("Out of memory!\n");
- ping_set_error (obj, "strdup", strerror (errno));
+ ping_set_errno (obj, errno);
ping_free (ph);
return (-1);
}
if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
{
ping_free (ph);
return (-1);
}
if ((ai_return = getaddrinfo (host, NULL, &ai_hints, &ai_list)) != 0)
{
+#if defined(EAI_SYSTEM)
+ char errbuf[PING_ERRMSG_LEN];
+#endif
dprintf ("getaddrinfo failed\n");
ping_set_error (obj, "getaddrinfo",
#if defined(EAI_SYSTEM)
(ai_return == EAI_SYSTEM)
dprintf ("getaddrinfo failed\n");
ping_set_error (obj, "getaddrinfo",
#if defined(EAI_SYSTEM)
(ai_return == EAI_SYSTEM)
- ? strerror (errno) :
+ ? sstrerror (errno, errbuf, sizeof (errbuf)) :
#endif
gai_strerror (ai_return));
ping_free (ph);
#endif
gai_strerror (ai_return));
ping_free (ph);
{
ph->fd = -1;
{
ph->fd = -1;
- sockaddr_len = sizeof (sockaddr);
- memset (&sockaddr, '\0', sockaddr_len);
-
if (ai_ptr->ai_family == AF_INET)
{
if (ai_ptr->ai_family == AF_INET)
{
- struct sockaddr_in *si;
-
- si = (struct sockaddr_in *) &sockaddr;
- si->sin_family = AF_INET;
- si->sin_port = htons (ph->ident);
- si->sin_addr.s_addr = htonl (INADDR_ANY);
-
ai_ptr->ai_socktype = SOCK_RAW;
ai_ptr->ai_protocol = IPPROTO_ICMP;
}
else if (ai_ptr->ai_family == AF_INET6)
{
ai_ptr->ai_socktype = SOCK_RAW;
ai_ptr->ai_protocol = IPPROTO_ICMP;
}
else if (ai_ptr->ai_family == AF_INET6)
{
- struct sockaddr_in6 *si;
-
- si = (struct sockaddr_in6 *) &sockaddr;
- si->sin6_family = AF_INET6;
- si->sin6_port = htons (ph->ident);
- si->sin6_addr = in6addr_any;
-
ai_ptr->ai_socktype = SOCK_RAW;
ai_ptr->ai_protocol = IPPROTO_ICMPV6;
}
ai_ptr->ai_socktype = SOCK_RAW;
ai_ptr->ai_protocol = IPPROTO_ICMPV6;
}
snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
errmsg[PING_ERRMSG_LEN - 1] = '\0';
snprintf (errmsg, PING_ERRMSG_LEN, "Unknown `ai_family': %i", ai_ptr->ai_family);
errmsg[PING_ERRMSG_LEN - 1] = '\0';
- dprintf (errmsg);
+ dprintf ("%s", errmsg);
ping_set_error (obj, "getaddrinfo", errmsg);
continue;
}
ping_set_error (obj, "getaddrinfo", errmsg);
continue;
}
ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
if (ph->fd == -1)
{
ph->fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
if (ph->fd == -1)
{
- dprintf ("socket: %s\n", strerror (errno));
- ping_set_error (obj, "socket", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("socket: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, errno);
+ continue;
+ }
+ else if (ph->fd >= FD_SETSIZE)
+ {
+ dprintf("socket(2) returned file descriptor %d, which is above the file "
+ "descriptor limit for select(2) (FD_SETSIZE = %d)\n",
+ ph->fd, FD_SETSIZE);
+ close(ph->fd);
+ ph->fd = -1;
+ ping_set_errno(obj, EMFILE);
continue;
}
continue;
}
assert (obj->srcaddrlen > 0);
assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
assert (obj->srcaddrlen > 0);
assert (obj->srcaddrlen <= sizeof (struct sockaddr_storage));
- if (bind (ph->fd, (struct sockaddr *) obj->srcaddr, obj->srcaddrlen) == -1)
+ if (bind (ph->fd, obj->srcaddr, obj->srcaddrlen) == -1)
{
{
- dprintf ("bind: %s\n", strerror (errno));
- ping_set_error (obj, "bind", strerror (errno));
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("bind: %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, errno);
close (ph->fd);
ph->fd = -1;
continue;
}
}
close (ph->fd);
ph->fd = -1;
continue;
}
}
+#ifdef SO_BINDTODEVICE
+ if (obj->device != NULL)
+ {
+ if (setsockopt (ph->fd, SOL_SOCKET, SO_BINDTODEVICE,
+ obj->device, strlen (obj->device) + 1) != 0)
+ {
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, errno);
+ close (ph->fd);
+ ph->fd = -1;
+ continue;
+ }
+ }
+#endif /* SO_BINDTODEVICE */
+#ifdef SO_MARK
+ if(obj->set_mark)
+ {
+ if(setsockopt(ph->fd, SOL_SOCKET, SO_MARK, &(obj->mark), sizeof(obj->mark)) != 0)
+ {
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("setsockopt (SO_MARK): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, errno);
+ close (ph->fd);
+ ph->fd = -1;
+ continue;
+ }
+ }
+#endif
+#ifdef SO_TIMESTAMP
+ if (1) /* {{{ */
+ {
+ int status;
+ int opt = 1;
+
+ status = setsockopt (ph->fd,
+ SOL_SOCKET, SO_TIMESTAMP,
+ &opt, sizeof (opt));
+ if (status != 0)
+ {
+#if WITH_DEBUG
+ char errbuf[PING_ERRMSG_LEN];
+ dprintf ("setsockopt (SO_TIMESTAMP): %s\n",
+ sstrerror (errno, errbuf, sizeof (errbuf)));
+#endif
+ ping_set_errno (obj, errno);
+ close (ph->fd);
+ ph->fd = -1;
+ continue;
+ }
+ } /* }}} if (1) */
+#endif /* SO_TIMESTAMP */
assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
assert (sizeof (struct sockaddr_storage) >= ai_ptr->ai_addrlen);
memset (ph->addr, '\0', sizeof (struct sockaddr_storage));
memcpy (ph->addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
}
#endif /* AI_CANONNAME */
}
#endif /* AI_CANONNAME */
+ if (ph->addrfamily == AF_INET)
+ {
+ int opt;
+
+#ifdef IP_RECVTOS
+ /* Enable receiving the TOS field */
+ opt = 1;
+ setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
+ &opt, sizeof (opt));
+#endif /* IP_RECVTOS */
+
+ /* Enable receiving the TTL field */
+ opt = 1;
+ setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
+ &opt, sizeof (opt));
+ }
+#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
+ else if (ph->addrfamily == AF_INET6)
+ {
+ int opt;
+
+# if defined(IPV6_RECVHOPLIMIT)
+ /* For details see RFC 3542, section 6.3. */
+ opt = 1;
+ setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT,
+ &opt, sizeof (opt));
+# endif /* IPV6_RECVHOPLIMIT */
+
+# if defined(IPV6_RECVTCLASS)
+ /* For details see RFC 3542, section 6.5. */
+ opt = 1;
+ setsockopt (ph->fd, IPPROTO_IPV6, IPV6_RECVTCLASS,
+ &opt, sizeof (opt));
+# endif /* IPV6_RECVTCLASS */
+ }
+#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
+
break;
break;
- }
+ } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
freeaddrinfo (ai_list);
freeaddrinfo (ai_list);
}
ping_set_ttl (ph, obj->ttl);
}
ping_set_ttl (ph, obj->ttl);
+ ping_set_qos (obj, ph, obj->qos);
return (0);
return (0);
-}
+} /* int ping_host_add */
int ping_host_remove (pingobj_t *obj, const char *host)
{
pinghost_t *pre, *cur;
int ping_host_remove (pingobj_t *obj, const char *host)
{
pinghost_t *pre, *cur;
+ if ((obj == NULL) || (host == NULL))
+ return (-1);
+
pre = NULL;
cur = obj->head;
pre = NULL;
cur = obj->head;
pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
{
pingobj_iter_t *ping_iterator_get (pingobj_t *obj)
{
+ if (obj == NULL)
+ return (NULL);
return ((pingobj_iter_t *) obj->head);
}
pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
{
return ((pingobj_iter_t *) obj->head);
}
pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter)
{
+ if (iter == NULL)
+ return (NULL);
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)
{
size_t orig_buffer_len = *buffer_len;
size_t orig_buffer_len = *buffer_len;
+ if ((iter == NULL) || (buffer_len == NULL))
+ return (-1);
+
+ if ((buffer == NULL) && (*buffer_len != 0 ))
+ return (-1);
+
switch (info)
{
case PING_INFO_USERNAME:
switch (info)
{
case PING_INFO_USERNAME:
strncpy ((char *) buffer, iter->data, orig_buffer_len);
ret = 0;
break;
strncpy ((char *) buffer, iter->data, orig_buffer_len);
ret = 0;
break;
+
+ case PING_INFO_RECV_TTL:
+ ret = ENOMEM;
+ *buffer_len = sizeof (int);
+ if (orig_buffer_len < sizeof (int))
+ break;
+ *((int *) buffer) = iter->recv_ttl;
+ ret = 0;
+ break;
+
+ case PING_INFO_RECV_QOS:
+ ret = ENOMEM;
+ if (*buffer_len>sizeof(unsigned)) *buffer_len=sizeof(unsigned);
+ if (!*buffer_len) *buffer_len=1;
+ if (orig_buffer_len < *buffer_len)
+ break;
+ memcpy(buffer,&iter->recv_qos,*buffer_len);
+ ret = 0;
+ break;
}
return (ret);
}
return (ret);
void *ping_iterator_get_context (pingobj_iter_t *iter)
{
void *ping_iterator_get_context (pingobj_iter_t *iter)
{
+ if (iter == NULL)
+ return (NULL);
return (iter->context);
}
void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
{
return (iter->context);
}
void ping_iterator_set_context (pingobj_iter_t *iter, void *context)
{
+ if (iter == NULL)
+ return;
iter->context = context;
}
iter->context = context;
}