From fcb0ec78ea75a651232be636315c16e6c70ea444 Mon Sep 17 00:00:00 2001 From: Alan Antonuk Date: Sun, 8 Nov 2015 23:19:14 -0800 Subject: Lib: clarify return value of amqp_hostcheck Use enum to specify values returned from amqp_hostcheck. --- librabbitmq/amqp_hostcheck.c | 26 +++++++++++++------------- librabbitmq/amqp_hostcheck.h | 13 +++++++++---- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/librabbitmq/amqp_hostcheck.c b/librabbitmq/amqp_hostcheck.c index 68e068d..1b3ce26 100644 --- a/librabbitmq/amqp_hostcheck.c +++ b/librabbitmq/amqp_hostcheck.c @@ -145,15 +145,15 @@ amqp_raw_nequal(const char *first, const char *second, size_t max) * http://tools.ietf.org/html/rfc6125#section-6.4.3 */ -static int -amqp_hostmatch(const char *hostname, const char *pattern) -{ +static amqp_hostcheck_result amqp_hostmatch(const char *hostname, + const char *pattern) { const char *pattern_label_end, *pattern_wildcard, *hostname_label_end; int wildcard_enabled; size_t prefixlen, suffixlen; pattern_wildcard = strchr(pattern, '*'); if (pattern_wildcard == NULL) { - return amqp_raw_equal(pattern, hostname) ? 1 : 0; + return amqp_raw_equal(pattern, hostname) ? AMQP_HCR_MATCH + : AMQP_HCR_NO_MATCH; } /* We require at least 2 dots in pattern to avoid too wide wildcard match. */ wildcard_enabled = 1; @@ -165,37 +165,37 @@ amqp_hostmatch(const char *hostname, const char *pattern) wildcard_enabled = 0; } if (!wildcard_enabled) { - return amqp_raw_equal(pattern, hostname) ? 1 : 0; + return amqp_raw_equal(pattern, hostname) ? AMQP_HCR_MATCH + : AMQP_HCR_NO_MATCH; } hostname_label_end = strchr(hostname, '.'); if (hostname_label_end == NULL || !amqp_raw_equal(pattern_label_end, hostname_label_end)) { - return 0; + return AMQP_HCR_NO_MATCH; } /* The wildcard must match at least one character, so the left-most * label of the hostname is at least as large as the left-most label * of the pattern. */ if (hostname_label_end - hostname < pattern_label_end - pattern) { - return 0; + return AMQP_HCR_NO_MATCH; } prefixlen = pattern_wildcard - pattern; suffixlen = pattern_label_end - (pattern_wildcard + 1); return amqp_raw_nequal(pattern, hostname, prefixlen) && amqp_raw_nequal(pattern_wildcard + 1, hostname_label_end - suffixlen, - suffixlen) ? 1 : 0; + suffixlen) ? AMQP_HCR_MATCH : AMQP_HCR_NO_MATCH; } -int -amqp_hostcheck(const char *match_pattern, const char *hostname) -{ +amqp_hostcheck_result amqp_hostcheck(const char *match_pattern, + const char *hostname) { /* sanity check */ if (!match_pattern || !*match_pattern || !hostname || !*hostname) { - return 0; + return AMQP_HCR_NO_MATCH; } /* trivial case */ if (amqp_raw_equal(hostname, match_pattern)) { - return 1; + return AMQP_HCR_MATCH; } return amqp_hostmatch(hostname, match_pattern); } diff --git a/librabbitmq/amqp_hostcheck.h b/librabbitmq/amqp_hostcheck.h index 9415806..24481e9 100644 --- a/librabbitmq/amqp_hostcheck.h +++ b/librabbitmq/amqp_hostcheck.h @@ -26,6 +26,11 @@ * copyright holder. */ +typedef enum { + AMQP_HCR_NO_MATCH = 0, + AMQP_HCR_MATCH = 1 +} amqp_hostcheck_result; + /** * Determine whether hostname matches match_pattern. * @@ -35,10 +40,10 @@ * http://tools.ietf.org/html/rfc6125#section-6.4.3 * * \param match_pattern RFC6125 compliant pattern - * \param hostname hostname to match against - * \returns 1 if hostname matches, 0 otherwise. + * \param hostname to match against + * \returns AMQP_HCR_MATCH if its a match, AMQP_HCR_NO_MATCH otherwise. */ -int -amqp_hostcheck(const char *match_pattern, const char *hostname); +amqp_hostcheck_result amqp_hostcheck(const char *match_pattern, + const char *hostname); #endif -- cgit v1.2.1