]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldpd: check for defined string before use chassis name for chassis id
authorvic_lin <vic_lin@fcdb0b513411>
Thu, 28 Dec 2017 06:57:29 +0000 (06:57 +0000)
committerVincent Bernat <vincent@bernat.im>
Sat, 30 Dec 2017 11:31:25 +0000 (12:31 +0100)
src/client/display.c
src/daemon/lldpd.c
src/lib/atoms/config.c
src/lib/lldpctl.h
src/lldpd-structs.c
src/lldpd-structs.h

index 056a917f4c763cc33f716362a9b36b409b351140..240d4773024de2bd840e6858f4aaca2c686a50f2 100644 (file)
@@ -895,6 +895,8 @@ display_configuration(lldpctl_conn_t *conn, struct writer *w)
            N(lldpctl_atom_get_str(configuration, lldpctl_k_config_iface_pattern)));
        tag_datatag(w, "cid-pattern", "Interface pattern for chassis ID",
            N(lldpctl_atom_get_str(configuration, lldpctl_k_config_cid_pattern)));
+       tag_datatag(w, "cid-string", "User defined string for chassis ID",
+           N(lldpctl_atom_get_str(configuration, lldpctl_k_config_cid_string)));
        tag_datatag(w, "description", "Override description with",
            N(lldpctl_atom_get_str(configuration, lldpctl_k_config_description)));
        tag_datatag(w, "platform", "Override platform with",
index 35d71174dc81735331a38412cb479f8c26f61631..3f3f8f90261814e823af5e13acd32e4815c4bd73 100644 (file)
@@ -1202,11 +1202,19 @@ lldpd_update_localchassis(struct lldpd *cfg)
           interface for example)
        */
        if (LOCAL_CHASSIS(cfg)->c_id == NULL) {
-               log_debug("localchassis", "no chassis ID is currently set, use chassis name");
-               if (!(LOCAL_CHASSIS(cfg)->c_id = strdup(LOCAL_CHASSIS(cfg)->c_name)))
+               if (cfg->g_config.c_cid_string != NULL) {
+                  log_debug("localchassis", "no chassis ID is currently set, use chassis id string");
+                  if (!(LOCAL_CHASSIS(cfg)->c_id = strdup(cfg->g_config.c_cid_string)))
                        fatal("localchassis", NULL);
-               LOCAL_CHASSIS(cfg)->c_id_len = strlen(LOCAL_CHASSIS(cfg)->c_name);
-               LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
+                  LOCAL_CHASSIS(cfg)->c_id_len = strlen(cfg->g_config.c_cid_string);
+                  LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
+               } else {
+                       log_debug("localchassis", "no chassis ID is currently set, use chassis name");
+                       if (!(LOCAL_CHASSIS(cfg)->c_id = strdup(LOCAL_CHASSIS(cfg)->c_name)))
+                            fatal("localchassis", NULL);
+                       LOCAL_CHASSIS(cfg)->c_id_len = strlen(LOCAL_CHASSIS(cfg)->c_name);
+                       LOCAL_CHASSIS(cfg)->c_id_subtype = LLDP_CHASSISID_SUBTYPE_LOCAL;
+               }
        }
 }
 
@@ -1475,13 +1483,13 @@ lldpd_main(int argc, char *argv[], char *envp[])
 #endif
        const char *ctlname = NULL;
        char *mgmtp = NULL;
-       char *cidp = NULL;
+       char *cidp = NULL, *cids = NULL;
        char *interfaces = NULL;
        /* We do not want more options here. Please add them in lldpcli instead
         * unless there is a very good reason. Most command-line options will
         * get deprecated at some point. */
        char *popt, opts[] =
-           "H:vhkrdD:p:xX:m:u:4:6:I:C:p:M:P:S:iL:O:@                    ";
+           "H:vhkrdD:p:xX:m:u:4:6:I:C:c:p:M:P:S:iL:O:@                    ";
        int i, found, advertise_version = 1;
 #ifdef ENABLE_LLDPMED
        int lldpmed = 0, noinventory = 0;
@@ -1574,6 +1582,13 @@ lldpd_main(int argc, char *argv[], char *envp[])
                        }
                        cidp = strdup(optarg);
                        break;
+               case 'c':
+                       if (cids) {
+                               fprintf(stderr, "-c can only be used once\n");
+                               usage();
+                       }
+                       cids = strdup(optarg);
+                       break;
                case 'L':
                        if (strlen(optarg)) lldpcli = optarg;
                        else lldpcli = NULL;
@@ -1803,6 +1818,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
        cfg->g_ctl = ctl;
        cfg->g_config.c_mgmt_pattern = mgmtp;
        cfg->g_config.c_cid_pattern = cidp;
+       cfg->g_config.c_cid_string = cids;
        cfg->g_config.c_iface_pattern = interfaces;
        cfg->g_config.c_smart = smart;
        if (lldpcli)
index fc64cb362edc598490296e38f70a3ac17851a5cd..7cd4f2ee7d507de19da819a31816496eb1776864 100644 (file)
@@ -91,6 +91,8 @@ _lldpctl_atom_get_str_config(lldpctl_atom_t *atom, lldpctl_key_t key)
                res = c->config->c_iface_pattern; break;
        case lldpctl_k_config_cid_pattern:
                res = c->config->c_cid_pattern; break;
+       case lldpctl_k_config_cid_string:
+               res = c->config->c_cid_string; break;
        case lldpctl_k_config_description:
                res = c->config->c_description; break;
        case lldpctl_k_config_platform:
index 08a4dba97d5d9bee88414b800e6bfc2e2902cc51..07b86d441d5c9b01bf30e61170a1720db01eb574 100644 (file)
@@ -653,6 +653,7 @@ typedef enum {
        lldpctl_k_config_mgmt_pattern, /**< `(S,WON)` Pattern to choose the management address */
        lldpctl_k_config_iface_pattern, /**< `(S,WON)` Pattern of enabled interfaces */
        lldpctl_k_config_cid_pattern,   /**< `(S)` Interface pattern to choose the chassis ID */
+       lldpctl_k_config_cid_string,    /**< `(S)` User defined string for the chassis ID */
        lldpctl_k_config_description,   /**< `(S,WON)` Chassis description overridden */
        lldpctl_k_config_platform,      /**< `(S,WON)` Platform description overridden (CDP) */
        lldpctl_k_config_hostname,      /**< `(S,WON)` System name overridden */
index 2e7efebb6f69025cb4ec28bea9197cfac03d962e..24b13527d35d5a92ecfd772b3058c74b5b7ed3ce 100644 (file)
@@ -233,6 +233,7 @@ lldpd_config_cleanup(struct lldpd_config *config)
        log_debug("alloc", "general configuration cleanup");
        free(config->c_mgmt_pattern);
        free(config->c_cid_pattern);
+       free(config->c_cid_string);
        free(config->c_iface_pattern);
        free(config->c_hostname);
        free(config->c_platform);
index fd9f1ae07725c9c6ecbec335373cb10c7d64b35f..b44a43f34035389bc25e33ea7e50d7d1fb5c9bb3 100644 (file)
@@ -378,6 +378,7 @@ struct lldpd_config {
 
        char *c_mgmt_pattern;   /* Pattern to match a management address */
        char *c_cid_pattern;    /* Pattern to match interfaces to use for chassis ID */
+       char *c_cid_string;     /* User defined string for chassis ID */
        char *c_iface_pattern;  /* Pattern to match interfaces to use */
 
        char *c_platform;       /* Override platform description (for CDP) */
@@ -404,6 +405,7 @@ struct lldpd_config {
 MARSHAL_BEGIN(lldpd_config)
 MARSHAL_STR(lldpd_config, c_mgmt_pattern)
 MARSHAL_STR(lldpd_config, c_cid_pattern)
+MARSHAL_STR(lldpd_config, c_cid_string)
 MARSHAL_STR(lldpd_config, c_iface_pattern)
 MARSHAL_STR(lldpd_config, c_hostname)
 MARSHAL_STR(lldpd_config, c_platform)