From: Vincent Bernat Date: Mon, 9 Dec 2013 21:42:52 +0000 (+0100) Subject: lldp: don't hard-code sysname, sysdescr and portdescr X-Git-Tag: 0.7.8~60 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=12313820fafc98139d2b8cbe389862425e0a66d4;p=thirdparty%2Flldpd.git lldp: don't hard-code sysname, sysdescr and portdescr Instead of using "Not received" when we don't have them, just keep a pointer to NULL. However, we need to handle that gracefully everywhere: don't send them over SNMP, don't display them in lldpcli, be ready for the fact that it should not be sent (even if this is not possible for the local chassis), don't use it on places were we display the neighbor and update tests. --- diff --git a/NEWS b/NEWS index fc13d4e3..f6114604 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,9 @@ +lldpd (0.7.8) + * Fixes: + + Don't hard-code default values for system name, system + description and port description. When the field is not present, + just don't display it. + lldpd (0.7.7) * Features: + Use a locally administered MAC address or an arbitrary one diff --git a/src/daemon/agent.c b/src/daemon/agent.c index 539df21e..0d10dd2e 100644 --- a/src/daemon/agent.c +++ b/src/daemon/agent.c @@ -983,9 +983,11 @@ agent_v_chassis(struct variable *vp, size_t *var_len, *var_len = chassis->c_id_len; return (u_char *)chassis->c_id; case LLDP_SNMP_SYSNAME: + if (!chassis->c_name || *chassis->c_name == '\0') break; *var_len = strlen(chassis->c_name); return (u_char *)chassis->c_name; case LLDP_SNMP_SYSDESCR: + if (!chassis->c_descr || *chassis->c_descr == '\0') break; *var_len = strlen(chassis->c_descr); return (u_char *)chassis->c_descr; case LLDP_SNMP_SYSCAP_SUP: @@ -999,7 +1001,7 @@ agent_v_chassis(struct variable *vp, size_t *var_len, default: break; } - return NULL; + return NULL; } static u_char* agent_h_local_chassis(struct variable *vp, oid *name, size_t *length, @@ -1198,6 +1200,7 @@ agent_v_port(struct variable *vp, size_t *var_len, struct lldpd_port *port) *var_len = port->p_id_len; return (u_char *)port->p_id; case LLDP_SNMP_PORTDESC: + if (!port->p_descr || *port->p_descr == '\0') break; *var_len = strlen(port->p_descr); return (u_char *)port->p_descr; #ifdef ENABLE_DOT3 @@ -1781,7 +1784,7 @@ agent_notify(struct lldpd_hardware *hardware, int type, ASN_OCTET_STR, (u_char *)hardware->h_ifname, strnlen(hardware->h_ifname, IFNAMSIZ)); - if (rport->p_chassis->c_name) { + if (rport->p_chassis->c_name && *rport->p_chassis->c_name != '\0') { snmp_varlist_add_variable(¬ification_vars, sysname_oid, sysname_oid_len, ASN_OCTET_STR, diff --git a/src/daemon/cdp.c b/src/daemon/cdp.c index 3ddbdffb..58a0a84a 100644 --- a/src/daemon/cdp.c +++ b/src/daemon/cdp.c @@ -94,7 +94,9 @@ cdp_send(struct lldpd *global, /* Chassis ID */ if (!( POKE_START_CDP_TLV(CDP_TLV_CHASSIS) && - POKE_BYTES(chassis->c_name, strlen(chassis->c_name)) && + chassis->c_name? + POKE_BYTES(chassis->c_name, strlen(chassis->c_name)): + POKE_BYTES("", 0) && POKE_END_CDP_TLV)) goto toobig; @@ -134,8 +136,10 @@ cdp_send(struct lldpd *global, /* Port ID */ if (!( POKE_START_CDP_TLV(CDP_TLV_PORT) && + hardware->h_lport.p_descr? POKE_BYTES(hardware->h_lport.p_descr, - strlen(hardware->h_lport.p_descr)) && + strlen(hardware->h_lport.p_descr)): + POKE_BYTES("", 0) && POKE_END_CDP_TLV)) goto toobig; @@ -185,7 +189,9 @@ cdp_send(struct lldpd *global, /* Software version */ if (!( POKE_START_CDP_TLV(CDP_TLV_SOFTWARE) && - POKE_BYTES(chassis->c_descr, strlen(chassis->c_descr)) && + chassis->c_descr? + POKE_BYTES(chassis->c_descr, strlen(chassis->c_descr)): + POKE_BYTES("", 0) && POKE_END_CDP_TLV)) goto toobig; diff --git a/src/daemon/lldp.c b/src/daemon/lldp.c index 6e5806ad..5727bd73 100644 --- a/src/daemon/lldp.c +++ b/src/daemon/lldp.c @@ -123,11 +123,13 @@ lldp_send(struct lldpd *global, goto toobig; /* System name */ - if (!( - POKE_START_LLDP_TLV(LLDP_TLV_SYSTEM_NAME) && - POKE_BYTES(chassis->c_name, strlen(chassis->c_name)) && - POKE_END_LLDP_TLV)) - goto toobig; + if (chassis->c_name && *chassis->c_name != '\0') { + if (!( + POKE_START_LLDP_TLV(LLDP_TLV_SYSTEM_NAME) && + POKE_BYTES(chassis->c_name, strlen(chassis->c_name)) && + POKE_END_LLDP_TLV)) + goto toobig; + } /* System description (skip it if empty) */ if (chassis->c_descr && *chassis->c_descr != '\0') { @@ -180,11 +182,13 @@ lldp_send(struct lldpd *global, } /* Port description */ - if (!( - POKE_START_LLDP_TLV(LLDP_TLV_PORT_DESCR) && - POKE_BYTES(port->p_descr, strlen(port->p_descr)) && - POKE_END_LLDP_TLV)) - goto toobig; + if (port->p_descr && *port->p_descr != '\0') { + if (!( + POKE_START_LLDP_TLV(LLDP_TLV_PORT_DESCR) && + POKE_BYTES(port->p_descr, strlen(port->p_descr)) && + POKE_END_LLDP_TLV)) + goto toobig; + } #ifdef ENABLE_DOT1 /* Port VLAN ID */ @@ -1010,28 +1014,6 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, hardware->h_ifname); goto malformed; } -#define NOTRECEIVED "Not received" - if (chassis->c_name == NULL) { - if ((chassis->c_name = (char *)calloc(1, strlen(NOTRECEIVED) + 1)) == NULL) { - log_warnx("lldp", "unable to allocate null chassis name"); - goto malformed; - } - memcpy(chassis->c_name, NOTRECEIVED, strlen(NOTRECEIVED)); - } - if (chassis->c_descr == NULL) { - if ((chassis->c_descr = (char *)calloc(1, strlen(NOTRECEIVED) + 1)) == NULL) { - log_warnx("lldp", "unable to allocate null chassis description"); - goto malformed; - } - memcpy(chassis->c_descr, NOTRECEIVED, strlen(NOTRECEIVED)); - } - if (port->p_descr == NULL) { - if ((port->p_descr = (char *)calloc(1, strlen(NOTRECEIVED) + 1)) == NULL) { - log_warnx("lldp", "unable to allocate null port description"); - goto malformed; - } - memcpy(port->p_descr, NOTRECEIVED, strlen(NOTRECEIVED)); - } *newchassis = chassis; *newport = port; return 1; diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index 499cd519..eff4455c 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -232,7 +232,7 @@ lldpd_display_neighbors(struct lldpd *cfg) if (neighbors == 0) priv_iface_description(hardware->h_ifname, ""); - else if (neighbors == 1 && neighbor) { + else if (neighbors == 1 && neighbor && *neighbor != '\0') { if (asprintf(&description, "%s", neighbor) != -1) { priv_iface_description(hardware->h_ifname, description); @@ -263,7 +263,7 @@ lldpd_count_neighbors(struct lldpd *cfg) neighbors--; if (neighbors == 0) setproctitle("no neighbor"); - else if (neighbors == 1 && neighbor) + else if (neighbors == 1 && neighbor && *neighbor != '\0') setproctitle("connected to %s", neighbor); else setproctitle("%d neighbor%s", neighbors, diff --git a/src/marshal.c b/src/marshal.c index 8a57d305..943eb893 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -102,6 +102,7 @@ marshal_serialize_(struct marshal_info *mi, void *unserialized, void **input, /* Handle special cases. */ size = mi->size; if (!strcmp(mi->name, "null string")) + /* We know we can't be called with NULL */ size = strlen((char *)unserialized) + 1; else if (!strcmp(mi->name, "fixed string")) size = osize; diff --git a/tests/check_lldp.c b/tests/check_lldp.c index 02a20dc9..b5d2a782 100644 --- a/tests/check_lldp.c +++ b/tests/check_lldp.c @@ -491,9 +491,9 @@ Link Layer Discovery Protocol ck_assert_int_eq(nport->p_id_len, ETHER_ADDR_LEN); fail_unless(memcmp(mac2, nport->p_id, ETHER_ADDR_LEN) == 0); ck_assert_int_eq(nchassis->c_ttl, 120); - ck_assert_str_eq(nchassis->c_name, "Not received"); - ck_assert_str_eq(nchassis->c_descr, "Not received"); - ck_assert_str_eq(nport->p_descr, "Not received"); + ck_assert_int_eq(nchassis->c_name, NULL); + ck_assert_int_eq(nchassis->c_descr, NULL); + ck_assert_int_eq(nport->p_descr, NULL); } END_TEST