From 897363971a06acac0552ea2fedc9e518bd09bff2 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 26 Jan 2008 10:35:08 +0100 Subject: [PATCH] src/liboping.c: Added the `username' field which holds the name provided by the user. Alex Brooks has reported that he cannot reliably remove hosts he added without a fully qualified domain name, such as `foobar'. The reason is that the `hostname' the user provided is replaced with the `canonical hostname', if they differ. To fix this problem I introduced a new fields `username' which holds the hostname as it was supplied by the user. This is the only name checked when removing a host now. Applications may access this name using the new `PING_INFO_USERNAME' option for `ping_iterator_get_info'. --- src/liboping.c | 33 +++++++++++++++++++++++++---- src/mans/ping_host_add.pod | 5 ++++- src/mans/ping_iterator_get_info.pod | 8 ++++++- src/oping.h | 3 ++- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/liboping.c b/src/liboping.c index 9b91b6d..fb7be3c 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 Florian octo Forster + * Copyright (C) 2006-2008 Florian octo Forster * * 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 @@ -97,6 +97,9 @@ struct pinghost { + /* username: name passed in by the user */ + char *username; + /* hostname: name returned by the reverse lookup */ char *hostname; struct sockaddr_storage *addr; socklen_t addrlen; @@ -790,6 +793,9 @@ static void ping_free (pinghost_t *ph) if (ph->fd >= 0) close (ph->fd); + if (ph->username != NULL) + free (ph->username); + if (ph->hostname != NULL) free (ph->hostname); @@ -980,7 +986,7 @@ static pinghost_t *ping_host_search (pinghost_t *ph, const char *host) { while (ph != NULL) { - if (strcasecmp (ph->hostname, host) == 0) + if (strcasecmp (ph->username, host) == 0) break; ph = ph->next; @@ -1022,6 +1028,14 @@ int ping_host_add (pingobj_t *obj, const char *host) return (-1); } + if ((ph->username = strdup (host)) == NULL) + { + dprintf ("Out of memory!\n"); + ping_set_error (obj, "strdup", strerror (errno)); + ping_free (ph); + return (-1); + } + if ((ph->hostname = strdup (host)) == NULL) { dprintf ("Out of memory!\n"); @@ -1195,7 +1209,7 @@ int ping_host_remove (pingobj_t *obj, const char *host) while (cur != NULL) { - if (strcasecmp (host, cur->hostname) == 0) + if (strcasecmp (host, cur->username) == 0) break; pre = cur; @@ -1237,6 +1251,18 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, switch (info) { + case PING_INFO_USERNAME: + ret = ENOMEM; + *buffer_len = strlen (iter->username); + if (orig_buffer_len <= *buffer_len) + break; + /* Since (orig_buffer_len > *buffer_len) `strncpy' + * will copy `*buffer_len' and pad the rest of + * `buffer' with null-bytes */ + strncpy (buffer, iter->username, orig_buffer_len); + ret = 0; + break; + case PING_INFO_HOSTNAME: ret = ENOMEM; *buffer_len = strlen (iter->hostname); @@ -1265,7 +1291,6 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, ) ret = ENOMEM; else if (ret == EAI_SYSTEM) - /* XXX: Not thread-safe! */ ret = errno; else ret = EINVAL; diff --git a/src/mans/ping_host_add.pod b/src/mans/ping_host_add.pod index c768be3..d2db982 100644 --- a/src/mans/ping_host_add.pod +++ b/src/mans/ping_host_add.pod @@ -24,6 +24,9 @@ L, the hostname is resolved to an IPv4 or IPv6 address. The B method looks for I within I and remove it if found. It will close the socket and deallocate the memory, too. +The names passed to B and B must match. This +name can be queried using L. + =head1 RETURN VALUE If B succeeds it returns zero. If an error occurs a value less @@ -46,4 +49,4 @@ L liboping is written by Florian octo Forster Eocto at verplant.orgE. It's homepage can be found at L. -(c) 2005, 2006 by Florian octo Forster. +(c) 2005-2008 by Florian octo Forster. diff --git a/src/mans/ping_iterator_get_info.pod b/src/mans/ping_iterator_get_info.pod index d84c111..57322bb 100644 --- a/src/mans/ping_iterator_get_info.pod +++ b/src/mans/ping_iterator_get_info.pod @@ -24,6 +24,12 @@ following defines: =over 4 +=item B + +Return the hostname of the host the iterator points to as supplied by the user. +This is the name you passed to L and which you need to pass +to C, too. + =item B Return the hostname of the host the iterator points to. Since the name is @@ -93,4 +99,4 @@ L liboping is written by Florian octo Forster Eocto at verplant.orgE. It's homepage can be found at L. -(c) 2005, 2006 by Florian octo Forster. +(c) 2005-2008 by Florian octo Forster. diff --git a/src/oping.h b/src/oping.h index b17e659..d450d6c 100644 --- a/src/oping.h +++ b/src/oping.h @@ -1,6 +1,6 @@ /** * Object oriented C module to send ICMP and ICMPv6 `echo's. - * Copyright (C) 2006 Florian octo Forster + * Copyright (C) 2006-2008 Florian octo Forster * * 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 @@ -79,6 +79,7 @@ int ping_host_remove (pingobj_t *obj, const char *host); pingobj_iter_t *ping_iterator_get (pingobj_t *obj); pingobj_iter_t *ping_iterator_next (pingobj_iter_t *iter); +#define PING_INFO_USERNAME 8 #define PING_INFO_HOSTNAME 1 #define PING_INFO_ADDRESS 2 #define PING_INFO_FAMILY 3 -- 2.30.2