X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fliboping.c;h=d99129ccc3c186d8ba7b8e6b5eec75ed2e0673bc;hb=9b1a6a7f0439e77c90f40b8877d825e1849f6512;hp=83ca9c242c072cb61622ede4c0058d51c9036f69;hpb=db412c89c4d3b899929d58d0eb44845cd029730a;p=liboping.git diff --git a/src/liboping.c b/src/liboping.c index 83ca9c2..d99129c 100644 --- a/src/liboping.c +++ b/src/liboping.c @@ -1,6 +1,6 @@ /** * Object oriented C module to send ICMP and ICMPv6 `echo's. - * Copyright (C) 2006-2011 Florian octo Forster + * Copyright (C) 2006-2016 Florian octo Forster * * 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 @@ -17,6 +17,10 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef __APPLE__ +#define __APPLE_USE_RFC_3542 +#endif + #if HAVE_CONFIG_H # include #endif @@ -138,6 +142,9 @@ struct pingobj char *device; + char set_mark; + int mark; + char errmsg[PING_ERRMSG_LEN]; pinghost_t *head; @@ -545,6 +552,7 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph, 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), @@ -552,6 +560,25 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph, 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); @@ -613,6 +640,9 @@ static int ping_receive_one (pingobj_t *obj, const pinghost_t *ph, 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; @@ -702,7 +732,8 @@ static int ping_receive_all (pingobj_t *obj) if ((status == -1) && (errno == EINTR)) { dprintf ("select was interrupted by signal..\n"); - continue; + ping_set_errno (obj, EINTR); + return (-EINTR); } else if (status < 0) { @@ -740,7 +771,7 @@ static int ping_receive_all (pingobj_t *obj) } /* while (1) */ return (ret); -} +} /* int ping_receive_all */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Sending functions: * @@ -1294,6 +1325,19 @@ int ping_setopt (pingobj_t *obj, int option, void *value) } /* 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) */ @@ -1304,18 +1348,13 @@ int ping_setopt (pingobj_t *obj, int option, void *value) int ping_send (pingobj_t *obj) { - int ret; - 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) @@ -1485,6 +1524,23 @@ int ping_host_add (pingobj_t *obj, const char *host) } } #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) /* {{{ */ { @@ -1540,10 +1596,12 @@ int ping_host_add (pingobj_t *obj, const char *host) { 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;