const char* lldpctl_atom_get_str(lldpctl_atom_t *atom, lldpctl_key_t key) { char *strresult = NULL; const uint8_t *bufresult = NULL; long int intresult = -1; int n1; size_t n2; if (atom == NULL) return NULL; RESET_ERROR(atom->conn); if (atom->get_str != NULL) { strresult = (char *)atom->get_str(atom, key); if (strresult) return strresult; if (lldpctl_last_error(atom->conn) != LLDPCTL_ERR_NOT_EXIST) return NULL; } RESET_ERROR(atom->conn); if (atom->get_int != NULL) { intresult = atom->get_int(atom, key); if (lldpctl_last_error(atom->conn) != LLDPCTL_ERR_NOT_EXIST) { strresult = _lldpctl_alloc_in_atom(atom, 21); if (!strresult) return NULL; n1 = snprintf(strresult, 21, "%ld", intresult); if (n1 > -1 && n1 < 21) return strresult; SET_ERROR(atom->conn, LLDPCTL_ERR_NOMEM); /* Not really true... */ return NULL; } } RESET_ERROR(atom->conn); if (atom->get_buffer != NULL) { bufresult = atom->get_buffer(atom, key, &n2); if (bufresult) return _lldpctl_dump_in_atom(atom, bufresult, n2, ' ', 0); if (lldpctl_last_error(atom->conn) != LLDPCTL_ERR_NOT_EXIST) return NULL; } SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); return NULL; }
static const char* _lldpctl_atom_get_str_port(lldpctl_atom_t *atom, lldpctl_key_t key) { struct _lldpctl_atom_port_t *p = (struct _lldpctl_atom_port_t *)atom; struct lldpd_port *port = p->port; struct lldpd_hardware *hardware = p->hardware; char *ipaddress = NULL; size_t len; /* Local port only */ switch (key) { case lldpctl_k_port_name: if (hardware != NULL) return hardware->h_ifname; break; case lldpctl_k_port_status: if (p->local) return map_lookup(port_status_map.map, LLDPD_RXTX_FROM_PORT(port)); break; default: break; } if (!port) return NULL; /* Local and remote port */ switch (key) { case lldpctl_k_port_protocol: return map_lookup(lldpd_protocol_map.map, port->p_protocol); case lldpctl_k_port_id_subtype: return map_lookup(port_id_subtype_map, port->p_id_subtype); case lldpctl_k_port_id: switch (port->p_id_subtype) { case LLDP_PORTID_SUBTYPE_IFNAME: case LLDP_PORTID_SUBTYPE_IFALIAS: case LLDP_PORTID_SUBTYPE_LOCAL: return port->p_id; case LLDP_PORTID_SUBTYPE_LLADDR: return _lldpctl_dump_in_atom(atom, (uint8_t*)port->p_id, port->p_id_len, ':', 0); case LLDP_PORTID_SUBTYPE_ADDR: switch (port->p_id[0]) { case LLDP_MGMT_ADDR_IP4: len = INET_ADDRSTRLEN + 1; break; case LLDP_MGMT_ADDR_IP6: len = INET6_ADDRSTRLEN + 1; break; default: len = 0; } if (len > 0) { ipaddress = _lldpctl_alloc_in_atom(atom, len); if (!ipaddress) return NULL; if (inet_ntop((port->p_id[0] == LLDP_MGMT_ADDR_IP4)? AF_INET:AF_INET6, &port->p_id[1], ipaddress, len) == NULL) break; return ipaddress; } break; } SET_ERROR(atom->conn, LLDPCTL_ERR_NOT_EXIST); return NULL; case lldpctl_k_port_descr: return port->p_descr; #ifdef ENABLE_DOT3 case lldpctl_k_port_dot3_mautype: return map_lookup(operational_mau_type_values, port->p_macphy.mau_type); #endif default: /* Compatibility: query the associated chassis too */ return lldpctl_atom_get_str(p->chassis, key); } }