From 8caf434164527b252390e821e218a9ad83bb7a8f Mon Sep 17 00:00:00 2001 From: Alexandru Ardelean Date: Mon, 18 May 2015 10:01:56 +0300 Subject: [PATCH] lldpd: convert 'oui_info' member to dynamic array Signed-off-by: Alexandru Ardelean --- src/daemon/client.c | 13 +++++++++++-- src/daemon/protocols/lldp.c | 6 +++++- src/lib/atoms/custom.c | 9 +++++++-- src/lldpd-structs.c | 1 + src/lldpd-structs.h | 3 ++- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/daemon/client.c b/src/daemon/client.c index bc7c317c..7a328b7f 100644 --- a/src/daemon/client.c +++ b/src/daemon/client.c @@ -379,7 +379,13 @@ client_handle_set_port(struct lldpd *cfg, enum hmsg_type *type, log_debug("rpc", "requested custom TLV add"); if ((custom = malloc(sizeof(struct lldpd_custom)))) { memcpy(custom, set->custom, sizeof(struct lldpd_custom)); - TAILQ_INSERT_TAIL(&port->p_custom_list, custom, next); + if ((custom->oui_info = malloc(custom->oui_info_len))) { + memcpy(custom->oui_info, set->custom->oui_info, custom->oui_info_len); + TAILQ_INSERT_TAIL(&port->p_custom_list, custom, next); + } else { + free(custom); + log_warn("rpc", "could not allocate memory for custom TLV info"); + } } else log_warn("rpc", "could not allocate memory for custom TLV"); } @@ -409,7 +415,10 @@ set_port_finished: free(set->dot3_power); #endif #ifdef ENABLE_CUSTOM - free(set->custom); + if (set->custom) { + free(set->custom->oui_info); + free(set->custom); + } #endif return 0; } diff --git a/src/daemon/protocols/lldp.c b/src/daemon/protocols/lldp.c index 7ff08fc0..5df6c42c 100644 --- a/src/daemon/protocols/lldp.c +++ b/src/daemon/protocols/lldp.c @@ -1106,8 +1106,12 @@ lldp_decode(struct lldpd *cfg, char *frame, int s, custom->oui_info_len = tlv_size > 4 ? tlv_size - 4 : 0; memcpy(custom->oui, orgid, sizeof(custom->oui)); custom->subtype = tlv_subtype; - if (custom->oui_info_len > 0) + if (custom->oui_info_len > 0) { + custom->oui_info = malloc(custom->oui_info_len); + if (!custom->oui_info) + return ENOMEM; PEEK_BYTES(custom->oui_info, custom->oui_info_len); + } TAILQ_INSERT_TAIL(&port->p_custom_list, custom, next); #endif } diff --git a/src/lib/atoms/custom.c b/src/lib/atoms/custom.c index 0b22fa21..9d6cdb7e 100644 --- a/src/lib/atoms/custom.c +++ b/src/lib/atoms/custom.c @@ -133,7 +133,7 @@ _lldpctl_atom_get_buffer_custom(lldpctl_atom_t *atom, lldpctl_key_t key, size_t return (const uint8_t *)&custom->tlv->oui; case lldpctl_k_custom_tlv_oui_info_string: *n = custom->tlv->oui_info_len; - return (const uint8_t *)&custom->tlv->oui_info; + return (const uint8_t *)custom->tlv->oui_info; default: SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); return NULL; @@ -157,7 +157,12 @@ _lldpctl_atom_set_buffer_custom(lldpctl_atom_t *atom, lldpctl_key_t key, return NULL; } custom->tlv->oui_info_len = n; - memcpy(&custom->tlv->oui_info, buf, n); + if (!(custom->tlv->oui_info = _lldpctl_alloc_in_atom(atom, n))) { + custom->tlv->oui_info_len = 0; + SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); + return NULL; + } + memcpy(custom->tlv->oui_info, buf, n); return atom; default: SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); diff --git a/src/lldpd-structs.c b/src/lldpd-structs.c index f983b7c2..6e85f9a7 100644 --- a/src/lldpd-structs.c +++ b/src/lldpd-structs.c @@ -112,6 +112,7 @@ lldpd_custom_list_cleanup(struct lldpd_port *port) custom != NULL; custom = custom_next) { custom_next = TAILQ_NEXT(custom, next); + free(custom->oui_info); free(custom); } TAILQ_INIT(&port->p_custom_list); diff --git a/src/lldpd-structs.h b/src/lldpd-structs.h index a892fd6a..a96801ab 100644 --- a/src/lldpd-structs.h +++ b/src/lldpd-structs.h @@ -224,12 +224,13 @@ struct lldpd_custom { /* Organizationally Defined Subtype */ u_int8_t subtype; /* Organizationally Defined Information String; for now/simplicity static array */ - u_int8_t oui_info[LLDP_TLV_ORG_OUI_INFO_MAXLEN]; + u_int8_t *oui_info; /* Organizationally Defined Information String length */ int oui_info_len; }; MARSHAL_BEGIN(lldpd_custom) MARSHAL_TQE(lldpd_custom, next) +MARSHAL_FSTR(lldpd_custom, oui_info, oui_info_len) MARSHAL_END(lldpd_custom); #endif -- 2.39.5