From 48acfcaf0861989025de81209db4eda06976dbc7 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Thu, 22 Sep 2011 22:13:38 +0200 Subject: [PATCH] Some fixes for PPVID and PI. PI are bytes, not a string. Grab bytes when reading and display using hexadecimal. Rework a bit how PPVID are displayed. --- src/display.c | 35 ++++++++++++++++------------------- src/lldp.c | 13 ++++++------- src/lldpd.h | 10 +++++----- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/display.c b/src/display.c index 009872ed..7586a9a9 100644 --- a/src/display.c +++ b/src/display.c @@ -438,7 +438,7 @@ get_pids(int s, struct pids *pids, char *interface, int nb) if (ctl_msg_recv(s, h) == -1) fatalx("get_pids: unable to receive answer"); if (h->hdr.type != HMSG_GET_PIDS) - fatalx("get_ppvids: unknown answer type received"); + fatalx("get_pids: unknown answer type received"); p = &h->data; if (ctl_msg_unpack_list(STRUCT_LLDPD_PI, pids, sizeof(struct lldpd_pi), h, &p) == -1) @@ -1059,23 +1059,12 @@ display_ppvids(struct writer *w, struct lldpd_port *port) struct lldpd_ppvid *ppvid; TAILQ_FOREACH(ppvid, &port->p_ppvids, p_entries) { tag_start(w, "ppvid", "PPVID"); - switch(ppvid->p_cap_status) { - case LLDPD_PPVID_CAP_SUPPORTED: - tag_attr(w, "ppvid-cap-status supported", "", - u2str(ppvid->p_cap_status)); - break; - case LLDPD_PPVID_CAP_ENABLED: - tag_attr(w, "ppvid-cap-status enabled", "", - u2str(ppvid->p_cap_status)); - break; - case LLDPD_PPVID_CAP_SUPPORTED_AND_ENABLED: - tag_attr(w, - "ppvid-cap-status supported, enabled", - "", - u2str(ppvid->p_cap_status)); - break; - } - tag_attr(w, "ppvid", "", u2str(ppvid->p_ppvid)); + if (ppvid->p_ppvid) + tag_attr(w, "value", "", u2str(ppvid->p_ppvid)); + tag_attr(w, "supported", "supported", + (ppvid->p_cap_status & LLDPD_PPVID_CAP_SUPPORTED)?"yes":"no"); + tag_attr(w, "enabled", "enabled", + (ppvid->p_cap_status & LLDPD_PPVID_CAP_ENABLED)?"yes":"no"); tag_end(w); } } @@ -1084,10 +1073,18 @@ static void display_pids(struct writer *w, struct lldpd_port *port) { struct lldpd_pi *pi; + char *hex; TAILQ_FOREACH(pi, &port->p_pids, p_entries) { + if (!pi->p_pi_len) continue; tag_start(w, "pi", "PI"); - tag_data(w, pi->p_pi); + /* Convert to hex for display */ + if ((hex = malloc(pi->p_pi_len * 2 + 1)) == NULL) + fatal(NULL); + for (int i = 0; i < pi->p_pi_len; i++) + snprintf(hex + 2*i, 3, "%02X", (unsigned char)pi->p_pi[i]); + tag_data(w, hex); tag_end(w); + free(hex); } } #endif diff --git a/src/lldp.c b/src/lldp.c index 6ebaab15..d5366915 100644 --- a/src/lldp.c +++ b/src/lldp.c @@ -195,8 +195,8 @@ lldp_send(struct lldpd *global, POKE_START_LLDP_TLV(LLDP_TLV_ORG) && POKE_BYTES(dot1, sizeof(dot1)) && POKE_UINT8(LLDP_TLV_DOT1_PI) && - POKE_UINT8(strlen(pi->p_pi)) && - POKE_BYTES(pi->p_pi, strlen(pi->p_pi)) && + POKE_UINT8(pi->p_pi_len) && + POKE_BYTES(pi->p_pi, pi->p_pi_len) && POKE_END_LLDP_TLV)) goto toobig; } @@ -453,7 +453,6 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, int vlan_len; struct lldpd_ppvid *ppvid; struct lldpd_pi *pi; - int pi_len; #endif if ((chassis = calloc(1, sizeof(struct lldpd_chassis))) == NULL) { @@ -661,16 +660,16 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, hardware->h_ifname); goto malformed; } - pi_len = PEEK_UINT8; - CHECK_TLV_SIZE(1 + pi_len, "PI"); + pi->p_pi_len = PEEK_UINT8; + CHECK_TLV_SIZE(1 + pi->p_pi_len, "PI"); if ((pi->p_pi = - (char *)calloc(1, pi_len + 1)) == NULL) { + (char *)calloc(1, pi->p_pi_len)) == NULL) { LLOG_WARN("unable to alloc pid name for " "tlv received on %s", hardware->h_ifname); goto malformed; } - PEEK_BYTES(pi->p_pi, pi_len); + PEEK_BYTES(pi->p_pi, pi->p_pi_len); TAILQ_INSERT_TAIL(&port->p_pids, pi, p_entries); break; diff --git a/src/lldpd.h b/src/lldpd.h index 119e2065..71a9a167 100644 --- a/src/lldpd.h +++ b/src/lldpd.h @@ -67,9 +67,8 @@ #define USING_AGENTX_SUBAGENT_MODULE 1 #ifdef ENABLE_DOT1 -#define LLDPD_PPVID_CAP_SUPPORTED 0x01 -#define LLDPD_PPVID_CAP_ENABLED 0x02 -#define LLDPD_PPVID_CAP_SUPPORTED_AND_ENABLED 0x03 +#define LLDPD_PPVID_CAP_SUPPORTED (1 << 1) +#define LLDPD_PPVID_CAP_ENABLED (1 << 2) struct lldpd_ppvid { TAILQ_ENTRY(lldpd_ppvid) p_entries; @@ -88,8 +87,9 @@ struct lldpd_vlan { struct lldpd_pi { TAILQ_ENTRY(lldpd_pi) p_entries; char *p_pi; + int p_pi_len; }; -#define STRUCT_LLDPD_PI "(Ls)" +#define STRUCT_LLDPD_PI "(LC)" #endif #ifdef ENABLE_LLDPMED @@ -230,7 +230,7 @@ struct lldpd_port { #endif #ifdef ENABLE_DOT1 -#define STRUCT_LLDPD_PORT_DOT1 "wPPP" +#define STRUCT_LLDPD_PORT_DOT1 "wPPPPPP" u_int16_t p_pvid; TAILQ_HEAD(, lldpd_vlan) p_vlans; TAILQ_HEAD(, lldpd_ppvid) p_ppvids; -- 2.39.5