summaryrefslogtreecommitdiffstats
path: root/lib/defaults.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/defaults.c')
-rw-r--r--lib/defaults.c152
1 files changed, 151 insertions, 1 deletions
diff --git a/lib/defaults.c b/lib/defaults.c
index cfc8fa27f..8a1cfebe1 100644
--- a/lib/defaults.c
+++ b/lib/defaults.c
@@ -18,9 +18,20 @@
#include <zebra.h>
#include "defaults.h"
-#include "libfrr.h"
#include "version.h"
+static char df_version[128] = FRR_VER_SHORT, df_profile[128] = DFLT_NAME;
+static struct frr_default *dflt_first = NULL, **dflt_next = &dflt_first;
+
+/* these are global for all FRR daemons. they have to be, since we write an
+ * integrated config with the same value for all daemons.
+ */
+const char *frr_defaults_profiles[] = {
+ "traditional",
+ "datacenter",
+ NULL,
+};
+
static int version_value(int ch)
{
/* non-ASCII shouldn't happen */
@@ -73,3 +84,142 @@ int frr_version_cmp(const char *aa, const char *bb)
}
return 0;
}
+
+static void frr_default_apply_one(struct frr_default *dflt, bool check);
+
+void frr_default_add(struct frr_default *dflt)
+{
+ dflt->next = NULL;
+ *dflt_next = dflt;
+ dflt_next = &dflt->next;
+
+ frr_default_apply_one(dflt, true);
+}
+
+static bool frr_match_version(const char *name, const char *vspec,
+ const char *version, bool check)
+{
+ int cmp;
+ static struct spec {
+ const char *str;
+ bool dir, eq;
+ } *s, specs[] = {
+ {"<=", -1, 1},
+ {">=", 1, 1},
+ {"==", 0, 1},
+ {"<", -1, 0},
+ {">", 1, 0},
+ {"=", 0, 1},
+ {NULL, 0, 0},
+ };
+
+ if (!vspec)
+ /* NULL = all versions */
+ return true;
+
+ for (s = specs; s->str; s++)
+ if (!strncmp(s->str, vspec, strlen(s->str)))
+ break;
+ if (!s->str) {
+ if (check)
+ fprintf(stderr, "invalid version specifier for %s: %s",
+ name, vspec);
+ /* invalid version spec, never matches */
+ return false;
+ }
+
+ vspec += strlen(s->str);
+ while (isspace((unsigned char)*vspec))
+ vspec++;
+
+ cmp = frr_version_cmp(version, vspec);
+ if (cmp == s->dir || (s->eq && cmp == 0))
+ return true;
+
+ return false;
+}
+
+static void frr_default_apply_one(struct frr_default *dflt, bool check)
+{
+ struct frr_default_entry *entry = dflt->entries;
+ struct frr_default_entry *dfltentry = NULL, *saveentry = NULL;
+
+ for (; entry->match_version || entry->match_profile; entry++) {
+ if (entry->match_profile
+ && strcmp(entry->match_profile, df_profile))
+ continue;
+
+ if (!dfltentry && frr_match_version(dflt->name,
+ entry->match_version, df_version, check))
+ dfltentry = entry;
+ if (!saveentry && frr_match_version(dflt->name,
+ entry->match_version, FRR_VER_SHORT, check))
+ saveentry = entry;
+
+ if (dfltentry && saveentry && !check)
+ break;
+ }
+ /* found default or arrived at last entry that has NULL,NULL spec */
+
+ if (!dfltentry)
+ dfltentry = entry;
+ if (!saveentry)
+ saveentry = entry;
+
+ if (dflt->dflt_str)
+ *dflt->dflt_str = dfltentry->val_str;
+ if (dflt->dflt_long)
+ *dflt->dflt_long = dfltentry->val_long;
+ if (dflt->dflt_ulong)
+ *dflt->dflt_ulong = dfltentry->val_ulong;
+ if (dflt->dflt_float)
+ *dflt->dflt_float = dfltentry->val_float;
+ if (dflt->save_str)
+ *dflt->save_str = saveentry->val_str;
+ if (dflt->save_long)
+ *dflt->save_long = saveentry->val_long;
+ if (dflt->save_ulong)
+ *dflt->save_ulong = saveentry->val_ulong;
+ if (dflt->save_float)
+ *dflt->save_float = saveentry->val_float;
+}
+
+void frr_defaults_apply(void)
+{
+ struct frr_default *dflt;
+
+ for (dflt = dflt_first; dflt; dflt = dflt->next)
+ frr_default_apply_one(dflt, false);
+}
+
+bool frr_defaults_profile_valid(const char *profile)
+{
+ const char **p;
+
+ for (p = frr_defaults_profiles; *p; p++)
+ if (!strcmp(profile, *p))
+ return true;
+ return false;
+}
+
+const char *frr_defaults_version(void)
+{
+ return df_version;
+}
+
+const char *frr_defaults_profile(void)
+{
+ return df_profile;
+}
+
+void frr_defaults_version_set(const char *version)
+{
+ strlcpy(df_version, version, sizeof(df_version));
+ frr_defaults_apply();
+}
+
+void frr_defaults_profile_set(const char *profile)
+{
+ strlcpy(df_profile, profile, sizeof(df_profile));
+ frr_defaults_apply();
+}