]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldp: add option/command to override hostname
authorJonas Johansson <jonasj76@gmail.com>
Tue, 8 Apr 2014 11:30:24 +0000 (13:30 +0200)
committerJonas Johansson <jonasj76@gmail.com>
Tue, 8 Apr 2014 12:16:53 +0000 (14:16 +0200)
Allow to override the system name (hostname) by using '-N' as parameter to
lldpd, or by using "configure system hostname <NAME>" to lldpcli.

Signed-off-by: Jonas Johansson <jonasj76@gmail.com>
src/client/conf-system.c
src/client/display.c
src/daemon/client.c
src/daemon/lldpd.c
src/lib/atom-private.c
src/lib/lldpctl.h
src/lldpd-structs.c
src/lldpd-structs.h

index a262d364622a429849c1159f029e2df1c6c0818b..3ae0e50804c9f74e80335775e7efc02470fb5431 100644 (file)
@@ -76,6 +76,32 @@ cmd_system_description(struct lldpctl_conn_t *conn, struct writer *w,
        return 1;
 }
 
+static int
+cmd_hostname(struct lldpctl_conn_t *conn, struct writer *w,
+    struct cmd_env *env, void *arg)
+{
+       log_debug("lldpctl", "set system name");
+
+       lldpctl_atom_t *config = lldpctl_get_configuration(conn);
+       if (config == NULL) {
+               log_warnx("lldpctl", "unable to get configuration from lldpd. %s",
+                   lldpctl_last_strerror(conn));
+               return 0;
+       }
+       const char *value = cmdenv_get(env, "hostname");
+
+       if (lldpctl_atom_set_str(config,
+               lldpctl_k_config_hostname, cmdenv_get(env, "hostname")) == NULL) {
+               log_warnx("lldpctl", "unable to set system name. %s",
+                   lldpctl_last_strerror(conn));
+               lldpctl_atom_dec_ref(config);
+               return 0;
+       }
+       log_info("lldpctl", "system name set to new value %s", cmdenv_get(env, "hostname"));
+       lldpctl_atom_dec_ref(config);
+       return 1;
+}
+
 static int
 cmd_update_descriptions(struct lldpctl_conn_t *conn, struct writer *w,
     struct cmd_env *env, void *arg)
@@ -246,6 +272,16 @@ register_commands_configure_system(struct cmd_node *configure,
                NEWLINE, "Override platform description",
                NULL, cmd_system_description, NULL);
 
+       commands_new(
+               commands_new(
+                       commands_new(configure_system,
+                           "hostname", "Override system name",
+                           NULL, NULL, NULL),
+                       NULL, "System name",
+                       NULL, cmd_store_env_value, "hostname"),
+               NEWLINE, "Override system name",
+               NULL, cmd_hostname, NULL);
+
         commands_new(
                commands_new(
                        commands_new(configure_interface,
index ec57fe5f95f28b585f21efe6c1bbc1df9629dedd..28d4c397963eec2a2694b71c4c6b7e9c6bf411a3 100644 (file)
@@ -723,6 +723,8 @@ display_configuration(lldpctl_conn_t *conn, struct writer *w)
            N(lldpctl_atom_get_str(configuration, lldpctl_k_config_description)));
        tag_datatag(w, "platform", "Override platform with",
            N(lldpctl_atom_get_str(configuration, lldpctl_k_config_platform)));
+       tag_datatag(w, "hostname", "Override system name with",
+           N(lldpctl_atom_get_str(configuration, lldpctl_k_config_hostname)));
        tag_datatag(w, "advertise-version", "Advertise version",
            lldpctl_atom_get_int(configuration, lldpctl_k_config_advertise_version)?
            "yes":"no");
index 00247c004c85bf564f63519ba4d7f37044a51b38..674b854ffd1605ec6e569a191235cd64881b4be7 100644 (file)
@@ -112,6 +112,12 @@ client_handle_set_configuration(struct lldpd *cfg, enum hmsg_type *type,
                cfg->g_config.c_platform = strdup(config->c_platform);
                levent_update_now(cfg);
        }
+       if (config->c_hostname) {
+               log_debug("rpc", "change system name to %s", config->c_hostname);
+               free(cfg->g_config.c_hostname);
+               cfg->g_config.c_hostname = strdup(config->c_hostname);
+               levent_update_now(cfg);
+       }
        if (config->c_set_ifdescr != cfg->g_config.c_set_ifdescr) {
                log_debug("rpc", "%s setting of interface description based on discovered neighbors",
                    config->c_set_ifdescr?"enable":"disable");
index 76951c19c43ca123faf8dbec9c099f5154b5efbb..fd09a7c236b2d638ba8d65b0d0353527488a4217 100644 (file)
@@ -88,6 +88,7 @@ usage(void)
        fprintf(stderr, "-k       Disable advertising of kernel release, version, machine.\n");
        fprintf(stderr, "-S descr Override the default system description.\n");
        fprintf(stderr, "-P name  Override the default hardware platform.\n");
+       fprintf(stderr, "-N name  Override the default system name.\n");
        fprintf(stderr, "-m IP    Specify the IPv4 management addresses of this system.\n");
        fprintf(stderr, "-u file  Specify the Unix-domain socket used for communication with lldpctl(8).\n");
        fprintf(stderr, "-H mode  Specify the behaviour when detecting multiple neighbors.\n");
@@ -987,8 +988,13 @@ lldpd_update_localchassis(struct lldpd *cfg)
        /* Set system name and description */
        if (uname(&un) < 0)
                fatal("localchassis", "failed to get system information");
-       if ((hp = priv_gethostbyname()) == NULL)
-               fatal("localchassis", "failed to get system name");
+       if (cfg->g_config.c_hostname) {
+               log_debug("localchassis", "use overridden system name `%s`", cfg->g_config.c_hostname);
+               hp = cfg->g_config.c_hostname;
+       } else {
+               if ((hp = priv_gethostbyname()) == NULL)
+                       fatal("localchassis", "failed to get system name");
+       }
        free(LOCAL_CHASSIS(cfg)->c_name);
        free(LOCAL_CHASSIS(cfg)->c_descr);
        if ((LOCAL_CHASSIS(cfg)->c_name = strdup(hp)) == NULL)
@@ -1269,7 +1275,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
        char *cidp = NULL;
        char *interfaces = NULL;
        char *popt, opts[] =
-               "H:vhkrdD:xX:m:u:4:6:I:C:p:M:P:S:iL:@                    ";
+               "H:vhkrdD:xX:m:u:4:6:I:C:p:M:P:N:S:iL:@                    ";
        int i, found, advertise_version = 1;
 #ifdef ENABLE_LLDPMED
        int lldpmed = 0, noinventory = 0;
@@ -1277,6 +1283,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
 #endif
        char *descr_override = NULL;
        char *platform_override = NULL;
+       char *hostname_override = NULL;
        char *lsb_release = NULL;
        const char *lldpcli = LLDPCLI_PATH;
        int smart = 15;
@@ -1383,6 +1390,9 @@ lldpd_main(int argc, char *argv[], char *envp[])
                        free(platform_override);
                        platform_override = strdup(optarg);
                        break;
+               case 'N':
+                       hostname_override = strdup(optarg);
+                       break;
                case 'H':
                        smart = atoi(optarg);
                        break;
@@ -1543,6 +1553,9 @@ lldpd_main(int argc, char *argv[], char *envp[])
        if (platform_override)
                cfg->g_config.c_platform = platform_override;
 
+       if (hostname_override)
+               cfg->g_config.c_hostname = hostname_override;
+
        /* Set system capabilities */
        log_debug("main", "set system capabilities");
        if ((lchassis = (struct lldpd_chassis*)
index 0d1a2a29da8a876a25280ce685ba8127e46e6ed8..313ec5d2eebffc26dcd9a872c708677151ecb688 100644 (file)
@@ -367,6 +367,8 @@ _lldpctl_atom_get_str_config(lldpctl_atom_t *atom, lldpctl_key_t key)
                res = c->config->c_description; break;
        case lldpctl_k_config_platform:
                res = c->config->c_platform; break;
+       case lldpctl_k_config_hostname:
+               res = c->config->c_hostname; break;
        case lldpctl_k_config_bond_slave_src_mac_type:
                return map_lookup(bond_slave_src_mac_map,
                                c->config->c_bond_slave_src_mac_type);
@@ -418,6 +420,15 @@ _lldpctl_atom_set_str_config(lldpctl_atom_t *atom, lldpctl_key_t key,
                free(c->config->c_platform);
                c->config->c_description = strdup(description);
                break;
+       case lldpctl_k_config_hostname:
+               description = _lldpctl_alloc_in_atom(atom, strlen(value) + 1);
+               if (!description)
+                       return NULL;
+               memcpy(description, value, len);
+               config.c_hostname = description;
+               free(c->config->c_hostname);
+               c->config->c_hostname = strdup(description);
+               break;
        default:
                SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST);
                return NULL;
index a3f5bb751ff730a3b670047316ca420881f12925..306bb5ba77e377df849f5cf3e6869fcc2bf497ca 100644 (file)
@@ -589,6 +589,7 @@ typedef enum {
        lldpctl_k_config_cid_pattern,   /**< `(S)` Interface pattern to choose the chassis ID */
        lldpctl_k_config_description,   /**< `(S,WO)` Chassis description overridden */
        lldpctl_k_config_platform,      /**< `(S,WO)` Platform description overridden (CDP) */
+       lldpctl_k_config_hostname,      /**< `(S,WO)` System name overridden */
        lldpctl_k_config_advertise_version, /**< `(I)` Advertise version */
        lldpctl_k_config_lldpmed_noinventory, /**< `(I)` Disable LLDP-MED inventory */
        lldpctl_k_config_paused,              /**< `(I)` lldpd is paused */
index 12fe3b816f55d4dcd2f3ba6f8982d45970a48514..71d28087bba2fa5fe82c0e5a25ca457505fba4c0 100644 (file)
@@ -177,6 +177,7 @@ lldpd_config_cleanup(struct lldpd_config *config)
        free(config->c_mgmt_pattern);
        free(config->c_cid_pattern);
        free(config->c_iface_pattern);
+       free(config->c_hostname);
        free(config->c_platform);
        free(config->c_description);
 }
index ca34ae26cd49a508c968e660f15aa7736e5b90da..245dd8ccc8f1223db0e2747582d0b2c76aaedfe5 100644 (file)
@@ -323,6 +323,7 @@ struct lldpd_config {
 
        char *c_platform;       /* Override platform description (for CDP) */
        char *c_description;    /* Override chassis description */
+       char *c_hostname;       /* Override system name */
        int c_advertise_version; /* Should the precise version be advertised? */
        int c_set_ifdescr;       /* Set interface description */
 
@@ -340,6 +341,7 @@ MARSHAL_BEGIN(lldpd_config)
 MARSHAL_STR(lldpd_config, c_mgmt_pattern)
 MARSHAL_STR(lldpd_config, c_cid_pattern)
 MARSHAL_STR(lldpd_config, c_iface_pattern)
+MARSHAL_STR(lldpd_config, c_hostname)
 MARSHAL_STR(lldpd_config, c_platform)
 MARSHAL_STR(lldpd_config, c_description)
 MARSHAL_END(lldpd_config);