From 8feb47511b38bfcc4bdde7cc50e1c2501356b7af Mon Sep 17 00:00:00 2001 From: octo Date: Sat, 4 Feb 2006 17:07:30 +0000 Subject: [PATCH] Implemented first version of `ping_setopt' Many minor fixes and additions.. --- src/liboping/liboping.c | 90 +++++++++++++++++++++++++++++++++++--------- src/liboping/liboping.h | 43 +++++++++------------ src/liboping/test_liboping.c | 2 +- 3 files changed, 92 insertions(+), 43 deletions(-) diff --git a/src/liboping/liboping.c b/src/liboping/liboping.c index 83ab044b..bd733f19 100644 --- a/src/liboping/liboping.c +++ b/src/liboping/liboping.c @@ -42,11 +42,14 @@ #if DEBUG # define dprintf(...) printf ("%s[%4i]: %-20s: ", __FILE__, __LINE__, __FUNCTION__); printf (__VA_ARGS__) #else -# define dprintf(format, ...) /**/ +# define dprintf(...) /**/ #endif #define PING_DATA "Florian Forster http://verplant.org/" +/* + * private (static) functions + */ static int ping_timeval_sub (struct timeval *tv1, struct timeval *tv2, struct timeval *res) { @@ -245,7 +248,7 @@ static pinghost_t *ping_receive_ipv6 (pinghost_t *ph, char *buffer, size_t buffe return (ptr); } -static void ping_receive_one (int fd, pinghost_t *ph) +static int ping_receive_one (int fd, pinghost_t *ph) { char buffer[4096]; size_t buffer_len; @@ -265,7 +268,7 @@ static void ping_receive_one (int fd, pinghost_t *ph) if (buffer_len == -1) { dprintf ("recvfrom: %s\n", strerror (errno)); - return; + return (-1); } dprintf ("Read %i bytes from fd = %i\n", buffer_len, fd); @@ -273,19 +276,19 @@ static void ping_receive_one (int fd, pinghost_t *ph) if (sa.ss_family == AF_INET) { if ((host = ping_receive_ipv4 (ph, buffer, buffer_len)) == NULL) - return; + return (-1); } else if (sa.ss_family == AF_INET6) { if ((host = ping_receive_ipv6 (ph, buffer, buffer_len)) == NULL) - return; + return (-1); } if (gettimeofday (&now, NULL) == -1) { dprintf ("gettimeofday: %s\n", strerror (errno)); timerclear (host->timer); - return; + return (-1); } dprintf ("sent: %12i.%06i\n", @@ -298,7 +301,7 @@ static void ping_receive_one (int fd, pinghost_t *ph) if (ping_timeval_sub (&now, host->timer, &diff) < 0) { timerclear (host->timer); - return; + return (-1); } dprintf ("diff: %12i.%06i\n", @@ -309,6 +312,8 @@ static void ping_receive_one (int fd, pinghost_t *ph) host->latency += ((double) diff.tv_sec) * 1000.0; timerclear (host->timer); + + return (0); } static int ping_receive_all (pinghost_t *ph) @@ -324,6 +329,10 @@ static int ping_receive_all (pinghost_t *ph) struct timeval timeout; int status; + int ret; + + ret = 0; + if (gettimeofday (&endtime, NULL) == -1) return (-1); endtime.tv_sec += 1; @@ -383,12 +392,12 @@ static int ping_receive_all (pinghost_t *ph) for (ptr = ph; ptr != NULL; ptr = ptr->next) { if (FD_ISSET (ptr->fd, &readfds)) - ping_receive_one (ptr->fd, ph); + if (ping_receive_one (ptr->fd, ph) == 0) + ret++; } } - /* FIXME - return correct status */ - return (0); + return (ret); } /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @@ -610,14 +619,13 @@ static void ping_free (pinghost_t *ph) /* * public methods */ -pingobj_t *ping_construct (int flags) +pingobj_t *ping_construct (void) { pingobj_t *obj; if ((obj = (pingobj_t *) malloc (sizeof (pingobj_t))) == NULL) return (NULL); - obj->flags = flags; obj->head = NULL; return (obj); @@ -643,15 +651,60 @@ void ping_destroy (pingobj_t *obj) return; } +int ping_setopt (pingobj_t *obj, int option, void *value) +{ + int ret = 0; + + switch (option) + { + case PING_OPT_TIMEOUT: + obj->timeout = *((double *) value); + if (obj->timeout < 0.0) + { + obj->timeout = PING_DEF_TIMEOUT; + ret = -1; + } + break; + + case PING_OPT_TTL: + obj->ttl = *((int *) value); + if ((obj->ttl < 1) || (obj->ttl > 255)) + { + obj->ttl = PING_DEF_TTL; + ret = -1; + } + break; + + case PING_OPT_AF: + obj->addrfamily = *((int *) value); + if ((obj->addrfamily != AF_UNSPEC) + && (obj->addrfamily != AF_INET) + && (obj->addrfamily != AF_INET6)) + { + obj->addrfamily = PING_DEF_AF; + ret = -1; + } + break; + + default: + ret = -2; + } /* switch (option) */ + + return (ret); +} /* int ping_setopt */ + + int ping_send (pingobj_t *obj) { + int ret; + if (ping_send_all (obj->head) < 0) return (-1); - if (ping_receive_all (obj->head) < 0) + if ((ret = ping_receive_all (obj->head)) < 0) return (-2); - return (0); + return (ret); } static pinghost_t *ping_host_search (pinghost_t *ph, const char *host) @@ -684,9 +737,12 @@ int ping_host_add (pingobj_t *obj, const char *host) return (0); memset (&ai_hints, '\0', sizeof (ai_hints)); - ai_hints.ai_flags = AI_ADDRCONFIG; - ai_hints.ai_family = PF_UNSPEC; - ai_hints.ai_socktype = SOCK_RAW; + ai_hints.ai_flags = 0; +#ifdef AI_ADDRCONFIG + ai_hints.ai_flags |= AI_ADDRCONFIG; +#endif + ai_hints.ai_family = PF_UNSPEC; + ai_hints.ai_socktype = SOCK_RAW; if ((ph = ping_alloc ()) == NULL) { diff --git a/src/liboping/liboping.h b/src/liboping/liboping.h index f650abb0..73d878c7 100644 --- a/src/liboping/liboping.h +++ b/src/liboping/liboping.h @@ -20,30 +20,9 @@ #ifndef OCTO_PING_H #define OCTO_PING_H 1 -#ifndef AI_ADDRCONFIG -#define AI_ADDRCONFIG 0 -#endif - -#include -#include - -/* FIXME BEGIN */ #include -#include #include -#include - -#include -#include -#include -#include -#include - -#include -#include -/* FIXME END */ - /* * Type definitions */ @@ -58,23 +37,37 @@ typedef struct pinghost int sequence; struct timeval *timer; double latency; + struct pinghost *next; } pinghost_t; -typedef struct +typedef pinghost_t pingobj_iter_t; + +typedef struct pingobj { - int flags; + double timeout; + int ttl; + int addrfamily; + pinghost_t *head; } pingobj_t; -typedef pinghost_t pingobj_iter_t; +#define PING_OPT_TIMEOUT 0x01 +#define PING_OPT_TTL 0x02 +#define PING_OPT_AF 0x04 + +#define PING_DEF_TIMEOUT 1.0 +#define PING_DEF_TTL 255 +#define PING_DEF_AF AF_UNSPEC /* * Method definitions */ -pingobj_t *ping_construct (int flags); +pingobj_t *ping_construct (void); void ping_destroy (pingobj_t *obj); +int ping_setopt (pingobj_t *obj, int option, void *value); + int ping_send (pingobj_t *obj); int ping_host_add (pingobj_t *obj, const char *host); diff --git a/src/liboping/test_liboping.c b/src/liboping/test_liboping.c index 81698200..cd7a781e 100644 --- a/src/liboping/test_liboping.c +++ b/src/liboping/test_liboping.c @@ -16,7 +16,7 @@ int main (int argc, char **argv) return (1); } - if ((ping = ping_construct (0)) == NULL) + if ((ping = ping_construct ()) == NULL) { fprintf (stderr, "ping_construct failed\n"); return (-1); -- 2.11.0