X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fliboping.c;h=22ab3a8672167b4d53248c4b92b9646ab82c05ee;hb=4c83ef784bdc0d14fd2b1c20853b678ccc4e18ee;hp=4c614f744adfa5bfd7a78a4e7573e5db385adabb;hpb=dfbb70da843d92b3d8935ab5d264554e1f12abf2;p=liboping.git diff --git a/src/liboping.c b/src/liboping.c index 4c614f7..22ab3a8 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; @@ -806,6 +809,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); @@ -935,9 +941,11 @@ int ping_setopt (pingobj_t *obj, int option, void *value) if (status != 0) { ping_set_error (obj, "getaddrinfo", - status == EAI_SYSTEM - ? strerror (errno) - : gai_strerror (status)); +#if defined(EAI_SYSTEM) + (status == EAI_SYSTEM) + ? strerror (errno) : +#endif + gai_strerror (status)); ret = -1; break; } @@ -996,7 +1004,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; @@ -1038,6 +1046,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"); @@ -1059,9 +1075,11 @@ int ping_host_add (pingobj_t *obj, const char *host) { dprintf ("getaddrinfo failed\n"); ping_set_error (obj, "getaddrinfo", - (ai_return == EAI_SYSTEM) - ? strerror (errno) - : gai_strerror (ai_return)); +#if defined(EAI_SYSTEM) + (ai_return == EAI_SYSTEM) + ? strerror (errno) : +#endif + gai_strerror (ai_return)); ping_free (ph); return (-1); } @@ -1211,7 +1229,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; @@ -1253,6 +1271,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) + 1; + 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) + 1; @@ -1280,9 +1310,10 @@ int ping_iterator_get_info (pingobj_iter_t *iter, int info, #endif ) ret = ENOMEM; +#if defined(EAI_SYSTEM) else if (ret == EAI_SYSTEM) - /* XXX: Not thread-safe! */ ret = errno; +#endif else ret = EINVAL; }