diff --git a/src/liboping.c b/src/liboping.c
index 411d8669202baf07c57df77e0f8bfb7b5a0cdc87..a272e2cb92f7051ad964be0f20f67874fefc2b21 100644 (file)
--- a/src/liboping.c
+++ b/src/liboping.c
/**
* 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-2010 Florian octo Forster <octo at verplant.org>
*
- * 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
*/
#if HAVE_CONFIG_H
double latency;
uint32_t dropped;
int recv_ttl;
+ uint8_t recv_qos;
char *data;
void *context;
double timeout;
int ttl;
int addrfamily;
+ uint8_t qos;
char *data;
struct sockaddr *srcaddr;
ident, seq);
}
- if (ptr != NULL)
- ptr->recv_ttl = ip_hdr->ip_ttl;
-
+ if (ptr != NULL){
+ ptr->recv_ttl = (int) ip_hdr->ip_ttl;
+ ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
+ }
return (ptr);
}
struct timeval diff;
pinghost_t *host = NULL;
int recv_ttl;
+ uint8_t recv_qos;
/*
* Set up the receive buffer..
/* Iterate over all auxiliary data in msghdr */
recv_ttl = -1;
+ recv_qos = 0xff;
for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
cmsg != NULL;
cmsg = CMSG_NXTHDR (&msghdr, cmsg))
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),
if (cmsg->cmsg_level != IPPROTO_IPV6)
continue;
+ if (cmsg->cmsg_type == IPV6_TCLASS)
+ {
+ memcpy (&recv_qos, CMSG_DATA (cmsg),
+ sizeof (recv_qos));
+ dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
+ } else
if (cmsg->cmsg_type == IPV6_HOPLIMIT)
{
memcpy (&recv_ttl, CMSG_DATA (cmsg),
if (recv_ttl >= 0)
host->recv_ttl = recv_ttl;
+ if (recv_qos != 0xffff)
+ host->recv_qos = recv_qos;
host->latency = ((double) diff.tv_usec) / 1000.0;
host->latency += ((double) diff.tv_sec) * 1000.0;
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;
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;
- FD_SET (ptr->fd, &readfds);
- num_readfds++;
+ 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)
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);
- 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)
{
for (ptr = ph; ptr != NULL; ptr = ptr->next)
{
- if (FD_ISSET (ptr->fd, &readfds))
+ if (FD_ISSET (ptr->fd, &read_fds))
+ {
if (ping_receive_one (obj, ptr, &nowtime) == 0)
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);
}
+/*
+ * 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);
+}
+
static int ping_get_ident (void)
{
int fd;
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->qos = 0;
return (obj);
}
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)
if (ph->addrfamily == AF_INET)
{
- int opt = 1;
+ int opt;
+ /* Enable receiving the TOS field */
+ opt = 1;
+ setsockopt (ph->fd, IPPROTO_IP, IP_RECVTOS,
+ &opt, sizeof (opt));
+
+ /* Enable receiving the TTL field */
+ opt = 1;
setsockopt (ph->fd, IPPROTO_IP, IP_RECVTTL,
&opt, sizeof (opt));
}
-#if defined(IPPROTO_IPV6) && defined(IPV6_RECVHOPLIMIT)
+#if defined(IPV6_RECVHOPLIMIT) || defined(IPV6_RECVTCLASS)
else if (ph->addrfamily == AF_INET6)
{
- int opt = 1;
+ 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
+#endif /* IPV6_RECVHOPLIMIT || IPV6_RECVTCLASS */
break;
- }
+ } /* for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next) */
freeaddrinfo (ai_list);
}
ping_set_ttl (ph, obj->ttl);
+ ping_set_qos (obj, ph, obj->qos);
return (0);
} /* int ping_host_add */
*((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);