]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
lldp: don't hard-code sysname, sysdescr and portdescr
authorVincent Bernat <bernat@luffy.cx>
Mon, 9 Dec 2013 21:42:52 +0000 (22:42 +0100)
committerVincent Bernat <bernat@luffy.cx>
Mon, 9 Dec 2013 21:42:52 +0000 (22:42 +0100)
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.

NEWS
src/daemon/agent.c
src/daemon/cdp.c
src/daemon/lldp.c
src/daemon/lldpd.c
src/marshal.c
tests/check_lldp.c

diff --git a/NEWS b/NEWS
index fc13d4e309eedfe3ad6392c7a9732500d850e4af..f6114604f10d016768b9ce6e98021da22f67af52 100644 (file)
--- 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
index 539df21e9a5cc94c993a72f88a1e62032030a122..0d10dd2eb70aa0d4297337fe99c9e9e01fd62a10 100644 (file)
@@ -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(&notification_vars,
                            sysname_oid, sysname_oid_len,
                            ASN_OCTET_STR,
index 3ddbdffb687b002eb8814e820fad5575b3ca56f1..58a0a84ac8a81efa133c703bef3eefc073abf289 100644 (file)
@@ -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;
 
index 6e5806ad4e04154755d434e2000b4bce84711acf..5727bd738d4fe004ff0fa6c01a45ee729dc8f997 100644 (file)
@@ -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;
index 499cd519bbf77f7626f381979e1de54e2f75d667..eff4455c8d04900195dc2ba5efb9e9e359bf0284 100644 (file)
@@ -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,
index 8a57d30528421462d1eeb5446adf88dd9a6227b5..943eb893ecc48d7dba2db2f29ceb40232d75beb7 100644 (file)
@@ -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;
index 02a20dc9070328848598fe7a34e69f076ea2c26b..b5d2a782a9c6dc0cebfe5225bc15c3ee9c427565 100644 (file)
@@ -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