]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
daemon: name the ints returned by pattern_match() function
authorVincent Bernat <vincent@bernat.ch>
Thu, 27 Oct 2022 21:02:34 +0000 (23:02 +0200)
committerVincent Bernat <vincent@bernat.ch>
Thu, 27 Oct 2022 21:03:11 +0000 (23:03 +0200)
src/daemon/interfaces.c
src/daemon/lldpd.c
src/daemon/lldpd.h
src/daemon/pattern.c

index 9596573b896c48f0a936863a531752cf5c27accc..ae81003104823e7fb745ec7826f7731f49394c2e 100644 (file)
@@ -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;
index 9f844751073234d337a6faebf8dcaf80a32a92a4..d64cf5ffa6a5d8da70468ce95275887cb9512a9f 100644 (file)
@@ -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);
index 2f2bd17f3bb68cfa8a5337b0d1015f537ffac31f..dac545094863b4d60cce81e190de2dfe666da70d 100644 (file)
@@ -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);
index a13817b8cdf51ab805a323ac1346f90206053c27..27c56f83a5646d3443851c1a08af1a6ff6e496fd 100644 (file)
  *                 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;
                        }
                }
        }