]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib/bgpd: provide/use API to get hostname/domainname
authorMitesh Kanjariya <mitesh@marvel-07.cumulusnetworks.com>
Mon, 21 Aug 2017 21:56:06 +0000 (14:56 -0700)
committerMitesh Kanjariya <mitesh@marvel-07.cumulusnetworks.com>
Thu, 24 Aug 2017 23:37:16 +0000 (16:37 -0700)
Signed-off-by: Mitesh Kanjariya <mitesh@cumulusnetworks.com>
13 files changed:
bgpd/bgp_open.c
bgpd/bgpd.c
isisd/isis_dynhn.c
isisd/isis_lsp.c
isisd/isis_misc.c
isisd/isis_misc.h
isisd/isisd.c
lib/command.c
lib/command.h
lib/grammar_sandbox_main.c
lib/vty.c
vtysh/vtysh.c
vtysh/vtysh_config.c

index 3ee865e3ba114c0ba642952ee3d17e73e7a09429..2e0cc2a8d072eedd9f136d5227865a7e23a4e1be 100644 (file)
@@ -1271,7 +1271,6 @@ void bgp_open_capability(struct stream *s, struct peer *peer)
        as_t local_as;
        u_int32_t restart_time;
        u_char afi_safi_count = 0;
-       struct utsname names;
        int adv_addpath_tx = 0;
 
        /* Remember current pointer for Opt Parm Len. */
@@ -1441,8 +1440,7 @@ void bgp_open_capability(struct stream *s, struct peer *peer)
        }
 
        /* Hostname capability */
-       uname(&names);
-       if (names.nodename[0] != '\0') {
+       if (hostname_get()) {
                SET_FLAG(peer->cap, PEER_CAP_HOSTNAME_ADV);
                stream_putc(s, BGP_OPEN_OPT_CAP);
                rcapp = stream_get_endp(s); /* Ptr to length placeholder */
@@ -1450,26 +1448,22 @@ void bgp_open_capability(struct stream *s, struct peer *peer)
                stream_putc(s, CAPABILITY_CODE_FQDN);
                capp = stream_get_endp(s);
                stream_putc(s, 0); /* dummy len for now */
-               len = strlen(names.nodename);
+               len = strlen(hostname_get());
                if (len > BGP_MAX_HOSTNAME)
                        len = BGP_MAX_HOSTNAME;
 
                stream_putc(s, len);
-               stream_put(s, names.nodename, len);
-#ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
-               if ((names.domainname[0] != '\0')
-                   && (strcmp(names.domainname, "(none)") != 0)) {
-                       len = strlen(names.domainname);
+               stream_put(s, hostname_get(), len);
+               if ((host.domainname)
+                   && (strcmp(host.domainname, "(none)") != 0)) {
+                       len = strlen(host.domainname);
                        if (len > BGP_MAX_HOSTNAME)
                                len = BGP_MAX_HOSTNAME;
 
                        stream_putc(s, len);
-                       stream_put(s, names.domainname, len);
+                       stream_put(s, host.domainname, len);
                } else
-#endif
-               {
                        stream_putc(s, 0); /* 0 length */
-               }
 
                /* Set the lengths straight */
                len = stream_get_endp(s) - rcapp - 1;
@@ -1478,14 +1472,9 @@ void bgp_open_capability(struct stream *s, struct peer *peer)
                stream_putc_at(s, capp, len);
 
                if (bgp_debug_neighbor_events(peer))
-#ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
                        zlog_debug(
                                "%s Sending hostname cap with hn = %s, dn = %s",
-                               peer->host, names.nodename, names.domainname);
-#else
-                       zlog_debug("%s Sending hostname cap with hn = %s",
-                                  peer->host, names.nodename);
-#endif
+                               peer->host, hostname_get(), host.domainname);
        }
 
        /* Sending base graceful-restart capability irrespective of the config
index 24d4ea36b7012e09ae013ce659287e31cdcf6aec..c64ebf1222a950ff7cf1ed85e9d19146a1496883 100644 (file)
@@ -2738,9 +2738,6 @@ static struct bgp *bgp_create(as_t *as, const char *name,
        struct bgp *bgp;
        afi_t afi;
        safi_t safi;
-       struct utsname names;
-
-       uname(&names);
 
        if ((bgp = XCALLOC(MTYPE_BGP, sizeof(struct bgp))) == NULL)
                return NULL;
@@ -2769,16 +2766,15 @@ static struct bgp *bgp_create(as_t *as, const char *name,
                XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->hostname);
                bgp->peer_self->hostname = NULL;
        }
-       bgp->peer_self->hostname = XSTRDUP(MTYPE_BGP_PEER_HOST, names.nodename);
+       bgp->peer_self->hostname = XSTRDUP(MTYPE_BGP_PEER_HOST,
+                                          hostname_get());
 
        if (bgp->peer_self->domainname != NULL) {
                XFREE(MTYPE_BGP_PEER_HOST, bgp->peer_self->domainname);
                bgp->peer_self->domainname = NULL;
        }
-#ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
        bgp->peer_self->domainname = XSTRDUP(MTYPE_BGP_PEER_HOST,
-                                            names.domainname);
-#endif
+                                            domainname_get());
        bgp->peer = list_new();
        bgp->peer->cmp = (int (*)(void *, void *))peer_cmp;
        bgp->peerhash = hash_create(peer_hash_key_make, peer_hash_same, NULL);
index 6fa7988304263020e6829f82fa4402eb436f7995..40ac1919fdafd914db236222edc5e197b8f4ab63 100644 (file)
@@ -146,6 +146,6 @@ void dynhn_print_all(struct vty *vty)
        }
 
        vty_out(vty, "     * %s %s\n", sysid_print(isis->sysid),
-               unix_hostname());
+               hostname_get());
        return;
 }
index 6bf6e45d9239b6253236ab8f4a3760701bca9dab..bf6968c95561aa3721df2667018669b5c4810aed 100644 (file)
@@ -600,7 +600,7 @@ static void lspid_print(u_char *lsp_id, u_char *trg, char dynhost, char frag)
        if (dyn)
                sprintf((char *)id, "%.14s", dyn->hostname);
        else if (!memcmp(isis->sysid, lsp_id, ISIS_SYS_ID_LEN) && dynhost)
-               sprintf((char *)id, "%.14s", unix_hostname());
+               sprintf((char *)id, "%.14s", hostname_get());
        else
                memcpy(id, sysid_print(lsp_id), 15);
        if (frag)
@@ -880,9 +880,9 @@ static void lsp_build(struct isis_lsp *lsp, struct isis_area *area)
        }
        /* Dynamic Hostname */
        if (area->dynhostname) {
-               isis_tlvs_set_dynamic_hostname(lsp->tlvs, unix_hostname());
+               isis_tlvs_set_dynamic_hostname(lsp->tlvs, hostname_get());
                lsp_debug("ISIS (%s): Adding dynamic hostname '%s'",
-                         area->area_tag, unix_hostname());
+                         area->area_tag, hostname_get());
        } else {
                lsp_debug("ISIS (%s): Not adding dynamic hostname (disabled)",
                          area->area_tag);
index e8888a5f5bc0a379009a0bfa318b106cd9a57b09..37724e17bb36cdb7c86834be7824c58f0449a7c0 100644 (file)
@@ -431,24 +431,6 @@ struct in_addr newprefix2inaddr(u_char *prefix_start, u_char prefix_masklen)
        return new_prefix;
 }
 
-/*
- * Returns host.name if any, otherwise
- * it returns the system hostname.
- */
-const char *unix_hostname(void)
-{
-       static struct utsname names;
-       const char *hostname;
-
-       hostname = host.name;
-       if (!hostname) {
-               uname(&names);
-               hostname = names.nodename;
-       }
-
-       return hostname;
-}
-
 /*
  * Returns the dynamic hostname associated with the passed system ID.
  * If no dynamic hostname found then returns formatted system ID.
@@ -462,7 +444,7 @@ const char *print_sys_hostname(const u_char *sysid)
 
        /* For our system ID return our host name */
        if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0)
-               return unix_hostname();
+               return hostname_get();
 
        dyn = dynhn_find_by_id(sysid);
        if (dyn)
index 5a19a1ffa0debe6bd5d78df4c0e4bd8b3f0da7e9..b81db34df658be773755a736a2f05bea8985ab48 100644 (file)
@@ -56,7 +56,6 @@ void zlog_dump_data(void *data, int len);
  * misc functions
  */
 unsigned long isis_jitter(unsigned long timer, unsigned long jitter);
-const char *unix_hostname(void);
 
 /*
  * macros
index 60b9367da9907baf0cb3dedd82cc19d232e21c63..0283ec3d00d9747f72befd881029eec1116cdc60 100644 (file)
@@ -1440,7 +1440,7 @@ static int show_isis_database(struct vty *vty, const char *argv, int ui_level)
                                                lsp = lsp_search(
                                                        lspid,
                                                        area->lspdb[level]);
-                                       } else if (strncmp(unix_hostname(),
+                                       } else if (strncmp(hostname_get(),
                                                           sysid, 15)
                                                   == 0) {
                                                memcpy(lspid, isis->sysid,
index 45b5593a3e8c675c5368c1b102e6a8978559964b..2d262c11468dcc9a66562406a1acb72508cd08ad 100644 (file)
@@ -124,6 +124,23 @@ vector cmdvec = NULL;
 /* Host information structure. */
 struct host host;
 
+/*
+ * Returns host.name if any, otherwise
+ * it returns the system hostname.
+ */
+const char *hostname_get(void)
+{
+       return host.name;
+}
+
+/*
+ * Returns unix domainname
+ */
+const char *domainname_get(void)
+{
+       return host.domainname;
+}
+
 /* Standard command node structures. */
 static struct cmd_node auth_node = {
        AUTH_NODE, "Password: ",
@@ -470,8 +487,8 @@ static char *zencrypt(const char *passwd)
 /* This function write configuration of this host. */
 static int config_write_host(struct vty *vty)
 {
-       if (host.name)
-               vty_out(vty, "hostname %s\n", host.name);
+       if (hostname_get())
+               vty_out(vty, "hostname %s\n", hostname_get());
 
        if (host.encrypt) {
                if (host.password_encrypt)
@@ -1403,7 +1420,7 @@ DEFUN (show_version,
        "Displays zebra version\n")
 {
        vty_out(vty, "%s %s (%s).\n", FRR_FULL_NAME, FRR_VERSION,
-               host.name ? host.name : "");
+               hostname_get() ? hostname_get() : "");
        vty_out(vty, "%s%s\n", FRR_COPYRIGHT, GIT_INFO);
        vty_out(vty, "configured with:\n    %s\n", FRR_CONFIG_ARGS);
 
@@ -2496,9 +2513,12 @@ void install_default(enum node_type node)
  * terminal = -1 -- watchfrr / no logging, but minimal config control */
 void cmd_init(int terminal)
 {
+       struct utsname names;
+
        if (array_size(node_names) != NODE_TYPE_MAX)
                assert(!"Update the CLI node description array!");
 
+       uname(&names);
        qobj_init();
 
        varhandlers = list_new();
@@ -2507,7 +2527,12 @@ void cmd_init(int terminal)
        cmdvec = vector_init(VECTOR_MIN_SIZE);
 
        /* Default host value settings. */
-       host.name = NULL;
+       host.name = XSTRDUP(MTYPE_HOST, names.nodename);
+#ifdef HAVE_STRUCT_UTSNAME_DOMAINNAME
+       host.domainname = XSTRDUP(MTYPE_HOST, names.domainname);
+#else
+       host.domainname = NULL;
+#endif
        host.password = NULL;
        host.enable = NULL;
        host.logfile = NULL;
@@ -2623,6 +2648,8 @@ void cmd_terminate()
 
        if (host.name)
                XFREE(MTYPE_HOST, host.name);
+       if (host.domainname)
+               XFREE(MTYPE_HOST, host.domainname);
        if (host.password)
                XFREE(MTYPE_HOST, host.password);
        if (host.password_encrypt)
index 8f12e2aabd108bd56b7cb4806c425570644ce9e5..92220dd7b9dee36ba95c7dfb30a2b086a885aae6 100644 (file)
@@ -41,6 +41,9 @@ struct host {
        /* Host name of this router. */
        char *name;
 
+       /* Domain name of this router */
+       char *domainname;
+
        /* Password for vty interface. */
        char *password;
        char *password_encrypt;
@@ -399,6 +402,8 @@ extern void cmd_exit(struct vty *vty);
 extern int cmd_list_cmds(struct vty *vty, int do_permute);
 
 extern int cmd_hostname_set(const char *hostname);
+extern const char *hostname_get(void);
+extern const char *domainname_get(void);
 
 /* NOT safe for general use; call this only if DEV_BUILD! */
 extern void grammar_sandbox_init(void);
index 89b0993d1d0e6642828e69464ccf18dd66987bbc..264c7c48f00ec6a30d91670bf777c0710efce8a4 100644 (file)
@@ -50,6 +50,7 @@ int main(int argc, char **argv)
        /* Library inits. */
        cmd_init(1);
        host.name = strdup("test");
+       host.domainname = strdup("testdomainname");
 
        vty_init(master);
        memory_init();
index 59a8825357b1989e63c8e546e8ab90f47581d588..0caded4a87ad10e811987b7880a9cf163efa98a2 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -250,16 +250,9 @@ void vty_hello(struct vty *vty)
 /* Put out prompt and wait input from user. */
 static void vty_prompt(struct vty *vty)
 {
-       struct utsname names;
-       const char *hostname;
-
        if (vty->type == VTY_TERM) {
-               hostname = host.name;
-               if (!hostname) {
-                       uname(&names);
-                       hostname = names.nodename;
-               }
-               vty_out(vty, cmd_prompt(vty->node), hostname);
+               vty_out(vty, cmd_prompt(vty->node),
+                       hostname_get());
        }
 }
 
index f971c171bc328afcc5dfbab5b8bc2c033cf98570..2ac667f0b00f8e4e1479b4f5889faab823588b3d 100644 (file)
@@ -2879,21 +2879,9 @@ void vtysh_readline_init(void)
 
 char *vtysh_prompt(void)
 {
-       static struct utsname names;
        static char buf[100];
-       const char *hostname;
-       extern struct host host;
-
-       hostname = host.name;
-
-       if (!hostname) {
-               if (!names.nodename[0])
-                       uname(&names);
-               hostname = names.nodename;
-       }
-
-       snprintf(buf, sizeof buf, cmd_prompt(vty->node), hostname);
 
+       snprintf(buf, sizeof buf, cmd_prompt(vty->node), hostname_get());
        return buf;
 }
 
index c561b5222f2bd77ea5b9d8b62c2cf95f3f12aace..c331e45886863d65528c1bd71333c41f3bff973c 100644 (file)
@@ -420,10 +420,9 @@ int vtysh_read_config(const char *config_default_dir)
 void vtysh_config_write()
 {
        char line[81];
-       extern struct host host;
 
-       if (host.name) {
-               sprintf(line, "hostname %s", host.name);
+       if (hostname_get()) {
+               sprintf(line, "hostname %s", hostname_get());
                vtysh_config_parse_line(NULL, line);
        }
        if (vtysh_write_integrated == WRITE_INTEGRATED_NO)