diff --git a/src/liboping.c b/src/liboping.c
index 2e7f2889ac13077ed4666211a352b15253eff155..d99129ccc3c186d8ba7b8e6b5eec75ed2e0673bc 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-2010 Florian octo Forster <octo at verplant.org>
+ * Copyright (C) 2006-2016 Florian octo Forster <ff at octo.it>
*
* 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
*
* 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
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
* 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
double latency;
uint32_t dropped;
int recv_ttl;
double latency;
uint32_t dropped;
int recv_ttl;
- unsigned recv_tos;
+ uint8_t recv_qos;
char *data;
void *context;
char *data;
void *context;
double timeout;
int ttl;
int addrfamily;
double timeout;
int ttl;
int addrfamily;
- unsigned tos;
+ uint8_t qos;
char *data;
struct sockaddr *srcaddr;
char *data;
struct sockaddr *srcaddr;
char *device;
char *device;
+ char set_mark;
+ int mark;
+
char errmsg[PING_ERRMSG_LEN];
pinghost_t *head;
char errmsg[PING_ERRMSG_LEN];
pinghost_t *head;
}
if (ptr != NULL){
}
if (ptr != NULL){
- ptr->recv_ttl = ip_hdr->ip_ttl;
- ptr->recv_tos = ip_hdr->ip_tos;
+ ptr->recv_ttl = (int) ip_hdr->ip_ttl;
+ ptr->recv_qos = (uint8_t) ip_hdr->ip_tos;
}
return (ptr);
}
}
return (ptr);
}
* reply. The right object will be returned by ping_receive_ipv*(). For
* now, we can only rely on ph->fd and ph->addrfamily. */
* 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;
+ struct timeval diff, pkt_now = *now;
pinghost_t *host = NULL;
int recv_ttl;
pinghost_t *host = NULL;
int recv_ttl;
- unsigned recv_tos;
+ uint8_t recv_qos;
/*
* Set up the receive buffer..
/*
* Set up the receive buffer..
/* Iterate over all auxiliary data in msghdr */
recv_ttl = -1;
/* Iterate over all auxiliary data in msghdr */
recv_ttl = -1;
- recv_tos = 0xffff;
+ recv_qos = 0;
for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
cmsg != NULL;
cmsg = CMSG_NXTHDR (&msghdr, cmsg))
{
for (cmsg = CMSG_FIRSTHDR (&msghdr); /* {{{ */
cmsg != NULL;
cmsg = CMSG_NXTHDR (&msghdr, cmsg))
{
- if (ph->addrfamily == AF_INET) /* {{{ */
+ 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)
{
{
if (cmsg->cmsg_level != IPPROTO_IP)
continue;
if (cmsg->cmsg_type == IP_TOS)
{
- memcpy (&recv_tos, CMSG_DATA (cmsg),
- sizeof (recv_tos));
- dprintf ("TOSv4 = %u;\n", recv_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)
{
} else
if (cmsg->cmsg_type == IP_TTL)
{
if (cmsg->cmsg_level != IPPROTO_IPV6)
continue;
if (cmsg->cmsg_level != IPPROTO_IPV6)
continue;
- if (cmsg->cmsg_type == IPV6_RECVTCLASS)
+ if (cmsg->cmsg_type == IPV6_TCLASS)
{
{
- memcpy (&recv_tos, CMSG_DATA (cmsg),
- sizeof (recv_tos));
- dprintf ("TOSv6 = %u;\n", recv_tos);
+ memcpy (&recv_qos, CMSG_DATA (cmsg),
+ sizeof (recv_qos));
+ dprintf ("TOSv6 = 0x%02"PRIx8";\n", recv_qos);
} else
} else
+#ifdef IPV6_HOPLIMIT
if (cmsg->cmsg_type == IPV6_HOPLIMIT)
{
memcpy (&recv_ttl, CMSG_DATA (cmsg),
if (cmsg->cmsg_type == IPV6_HOPLIMIT)
{
memcpy (&recv_ttl, CMSG_DATA (cmsg),
dprintf ("TTLv6 = %i;\n", recv_ttl);
}
else
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);
{
dprintf ("Not handling option %i.\n",
cmsg->cmsg_type);
}
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);
if (recv_ttl >= 0)
host->recv_ttl = recv_ttl;
if (recv_ttl >= 0)
host->recv_ttl = recv_ttl;
- if (recv_tos != 0xffff)
- host->recv_tos = recv_tos;
+ 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)
{
fd_set read_fds;
static int ping_receive_all (pingobj_t *obj)
{
fd_set read_fds;
if ((status == -1) && (errno == EINTR))
{
dprintf ("select was interrupted by signal..\n");
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)
{
} /* while (1) */
return (ret);
} /* while (1) */
return (ret);
-}
+} /* int ping_receive_all */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Sending functions: *
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Sending functions: *
/*
* Set the TOS of a socket protocol independently.
/*
* 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_tos (pinghost_t *ph, unsigned tos)
+static int ping_set_qos (pingobj_t *obj, pinghost_t *ph, uint8_t qos)
{
{
- int ret = -2;
+ int ret = EINVAL;
+ char errbuf[PING_ERRMSG_LEN];
if (ph->addrfamily == AF_INET)
{
if (ph->addrfamily == AF_INET)
{
- dprintf ("Setting TP_TOS to %i\n", ttl);
+ dprintf ("Setting TP_TOS to %#04"PRIx8"\n", qos);
ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
ret = setsockopt (ph->fd, IPPROTO_IP, IP_TOS,
- &tos, sizeof (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)
{
}
else if (ph->addrfamily == AF_INET6)
{
- dprintf ("Setting IPV6_TCLASS to %i\n", ttl);
+ /* 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,
ret = setsockopt (ph->fd, IPPROTO_IPV6, IPV6_TCLASS,
- &tos, sizeof (tos));
+ &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);
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->tos = 0;
+ obj->qos = 0;
return (obj);
}
return (obj);
}
switch (option)
{
switch (option)
{
- case PING_OPT_TOS:{
- obj->tos=*(unsigned *)value;
+ case PING_OPT_QOS:
+ {
pinghost_t *ph;
pinghost_t *ph;
+
+ obj->qos = *((uint8_t *) value);
for (ph = obj->head; ph != NULL; ph = ph->next)
for (ph = obj->head; ph != NULL; ph = ph->next)
- ping_set_tos (ph, obj->tos);
+ ping_set_qos (obj, ph, obj->qos);
break;
}
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)
} /* case PING_OPT_DEVICE */
break;
} /* 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 (obj == NULL)
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)
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;
}
{
#if WITH_DEBUG
char errbuf[PING_ERRMSG_LEN];
{
#if WITH_DEBUG
char errbuf[PING_ERRMSG_LEN];
- dprintf ("setsockopt: %s\n",
+ dprintf ("setsockopt (SO_BINDTODEVICE): %s\n",
sstrerror (errno, errbuf, sizeof (errbuf)));
#endif
ping_set_errno (obj, errno);
sstrerror (errno, errbuf, sizeof (errbuf)));
#endif
ping_set_errno (obj, errno);
}
}
#endif /* SO_BINDTODEVICE */
}
}
#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);
if (ph->addrfamily == AF_INET)
{
if (ph->addrfamily == AF_INET)
{
- int opt = 1;
+ 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));
}
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)
{
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));
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;
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_tos (ph, obj->tos);
+ ping_set_qos (obj, ph, obj->qos);
return (0);
} /* int ping_host_add */
return (0);
} /* int ping_host_add */
ret = 0;
break;
ret = 0;
break;
- case PING_INFO_TOS:
+ 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;
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_tos,*buffer_len);
+ memcpy(buffer,&iter->recv_qos,*buffer_len);
ret = 0;
break;
}
ret = 0;
break;
}