From: Vincent Bernat Date: Thu, 27 Oct 2022 21:02:34 +0000 (+0200) Subject: daemon: name the ints returned by pattern_match() function X-Git-Tag: 1.0.16~12 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=8b9da8225a33c7dd41969cfd37b26ac3a6b10869;p=thirdparty%2Flldpd.git daemon: name the ints returned by pattern_match() function --- diff --git a/src/daemon/interfaces.c b/src/daemon/interfaces.c index 9596573b..ae810031 100644 --- a/src/daemon/interfaces.c +++ b/src/daemon/interfaces.c @@ -187,11 +187,11 @@ interfaces_helper_allowlist(struct lldpd *cfg, TAILQ_FOREACH(iface, interfaces, next) { int m = pattern_match(iface->name, cfg->g_config.c_iface_pattern, 0); switch (m) { - case 0: + case PATTERN_MATCH_DENIED: log_debug("interfaces", "deny %s", iface->name); iface->ignore = 1; continue; - case 2: + case PATTERN_MATCH_ALLOWED_EXACT: log_debug("interfaces", "allow %s (consider it as a physical interface)", iface->name); iface->type |= IFACE_PHYSICAL_T; diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 9f844751..d64cf5ff 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -451,7 +451,7 @@ lldpd_cleanup(struct lldpd *cfg) pattern_match(hardware->h_ifname, cfg->g_config.c_perm_ifaces, 0): 0; switch (m) { - case 0: + case PATTERN_MATCH_DENIED: log_debug("localchassis", "delete non-permanent interface %s", hardware->h_ifname); TRACE(LLDPD_INTERFACES_DELETE(hardware->h_ifname)); @@ -459,8 +459,8 @@ lldpd_cleanup(struct lldpd *cfg) lldpd_remote_cleanup(hardware, notify_clients_deletion, 1); lldpd_hardware_cleanup(cfg, hardware); break; - case 1: - case 2: + case PATTERN_MATCH_ALLOWED: + case PATTERN_MATCH_ALLOWED_EXACT: log_debug("localchassis", "do not delete %s, permanent", hardware->h_ifname); lldpd_remote_cleanup(hardware, notify_clients_deletion, 1); diff --git a/src/daemon/lldpd.h b/src/daemon/lldpd.h index 2f2bd17f..dac54509 100644 --- a/src/daemon/lldpd.h +++ b/src/daemon/lldpd.h @@ -401,7 +401,12 @@ int ifbpf_phys_init(struct lldpd *, struct lldpd_hardware *); #endif /* pattern.c */ -int pattern_match(char *, char *, int); +enum pattern_match_result { + PATTERN_MATCH_DENIED, + PATTERN_MATCH_ALLOWED, + PATTERN_MATCH_ALLOWED_EXACT +}; +enum pattern_match_result pattern_match(char *, char *, int); /* bitmap.c */ void bitmap_set(uint32_t *bmap, uint16_t vlan_id); diff --git a/src/daemon/pattern.c b/src/daemon/pattern.c index a13817b8..27c56f83 100644 --- a/src/daemon/pattern.c +++ b/src/daemon/pattern.c @@ -29,28 +29,28 @@ * denied. A pattern may begin with `!!`. In this * case, it is allowed back. Each pattern will then be * matched against `fnmatch()` function. - * @param found Value to return if the pattern isn't found. Should be either 0 - * or 1. + * @param found Value to return if the pattern isn't found. Should be either + * PATTERN_MATCH_DENIED or PATTERN_MACTH_DENIED. * * If a pattern is found matching and denied at the same time, it * will be denied. If it is both allowed and denied, it * will be allowed. * - * @return 0 if the string matches a denied pattern which is not + * @return PATTERN_MATCH_DENIED if the string matches a denied pattern which is not * allowed or if the pattern wasn't found and `found` was set to - * 0. Otherwise, return 1 unless the interface match is exact, in this - * case return 2. + * PATTERN_MATCH_DENIED. Otherwise, return PATTERN_MATCH_ALLOWED unless the + * interface match is exact, in this case return PATTERN_MATCH_ALLOWED_EXACT. */ -int +enum pattern_match_result pattern_match(char *string, char *patterns, int found) { char *pattern; int denied = 0; - found = !!found; + found = found ? PATTERN_MATCH_ALLOWED : PATTERN_MATCH_DENIED; if ((patterns = strdup(patterns)) == NULL) { log_warnx("interfaces", "unable to allocate memory"); - return 0; + return PATTERN_MATCH_DENIED; } for (pattern = strtok(patterns, ","); @@ -59,18 +59,19 @@ pattern_match(char *string, char *patterns, int found) if ((pattern[0] == '!') && (pattern[1] == '!') && (fnmatch(pattern + 2, string, 0) == 0)) { /* Allowed. No need to search further. */ - found = (strcmp(pattern + 2, string))?1:2; + found = (strcmp(pattern + 2, string)) ? + PATTERN_MATCH_ALLOWED : PATTERN_MATCH_ALLOWED_EXACT; break; } if ((pattern[0] == '!') && (fnmatch(pattern + 1, string, 0) == 0)) { denied = 1; - found = 0; + found = PATTERN_MATCH_DENIED; } else if (!denied && fnmatch(pattern, string, 0) == 0) { if (!strcmp(pattern, string)) { - found = 2; + found = PATTERN_MATCH_ALLOWED_EXACT; } else if (found < 2) { - found = 1; + found = PATTERN_MATCH_ALLOWED; } } }