lldp: export opaque TLV type and accessor functions

Export struct tlv_packet as a public opaque sd_lldp_packet type and
make its accessor functions public.
This commit is contained in:
Beniamino Galvani
2015-10-02 17:39:12 +02:00
parent 97030a86f0
commit 176c355b43
8 changed files with 57 additions and 52 deletions
+31 -30
View File
@@ -21,6 +21,7 @@
***/
#include "lldp-internal.h"
#include "sd-lldp.h"
/* We store maximum 1K chassis entries */
#define LLDP_MIB_MAX_CHASSIS 1024
@@ -28,10 +29,10 @@
/* Maximum Ports can be attached to any chassis */
#define LLDP_MIB_MAX_PORT_PER_CHASSIS 32
int lldp_read_chassis_id(tlv_packet *tlv,
uint8_t *type,
uint8_t **data,
uint16_t *length) {
int sd_lldp_packet_read_chassis_id(tlv_packet *tlv,
uint8_t *type,
uint8_t **data,
uint16_t *length) {
uint8_t subtype;
int r;
@@ -67,10 +68,10 @@ int lldp_read_chassis_id(tlv_packet *tlv,
return r;
}
int lldp_read_port_id(tlv_packet *tlv,
uint8_t *type,
uint8_t **data,
uint16_t *length) {
int sd_lldp_packet_read_port_id(tlv_packet *tlv,
uint8_t *type,
uint8_t **data,
uint16_t *length) {
uint8_t subtype;
char *s;
int r;
@@ -119,7 +120,7 @@ int lldp_read_port_id(tlv_packet *tlv,
return r;
}
int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl) {
int sd_lldp_packet_read_ttl(tlv_packet *tlv, uint16_t *ttl) {
int r;
assert_return(tlv, -EINVAL);
@@ -136,9 +137,9 @@ int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl) {
return r;
}
int lldp_read_system_name(tlv_packet *tlv,
char **data,
uint16_t *length) {
int sd_lldp_packet_read_system_name(tlv_packet *tlv,
char **data,
uint16_t *length) {
char *s;
int r;
@@ -160,9 +161,9 @@ int lldp_read_system_name(tlv_packet *tlv,
return r;
}
int lldp_read_system_description(tlv_packet *tlv,
char **data,
uint16_t *length) {
int sd_lldp_packet_read_system_description(tlv_packet *tlv,
char **data,
uint16_t *length) {
char *s;
int r;
@@ -184,9 +185,9 @@ int lldp_read_system_description(tlv_packet *tlv,
return r;
}
int lldp_read_port_description(tlv_packet *tlv,
char **data,
uint16_t *length) {
int sd_lldp_packet_read_port_description(tlv_packet *tlv,
char **data,
uint16_t *length) {
char *s;
int r;
@@ -208,7 +209,7 @@ int lldp_read_port_description(tlv_packet *tlv,
return r;
}
int lldp_read_system_capability(tlv_packet *tlv, uint16_t *data) {
int sd_lldp_packet_read_system_capability(tlv_packet *tlv, uint16_t *data) {
int r;
assert_return(tlv, -EINVAL);
@@ -244,7 +245,7 @@ int lldp_mib_update_objects(lldp_chassis *c, tlv_packet *tlv) {
assert_return(c, -EINVAL);
assert_return(tlv, -EINVAL);
r = lldp_read_port_id(tlv, &type, &data, &length);
r = sd_lldp_packet_read_port_id(tlv, &type, &data, &length);
if (r < 0)
return r;
@@ -253,13 +254,13 @@ int lldp_mib_update_objects(lldp_chassis *c, tlv_packet *tlv) {
if ((p->type == type && p->length == length && !memcmp(p->data, data, p->length))) {
r = lldp_read_ttl(tlv, &ttl);
r = sd_lldp_packet_read_ttl(tlv, &ttl);
if (r < 0)
return r;
p->until = ttl * USEC_PER_SEC + now(clock_boottime_or_monotonic());
tlv_packet_unref(p->packet);
sd_lldp_packet_unref(p->packet);
p->packet = tlv;
prioq_reshuffle(p->c->by_expiry, p, &p->prioq_idx);
@@ -281,7 +282,7 @@ int lldp_mib_remove_objects(lldp_chassis *c, tlv_packet *tlv) {
assert_return(c, -EINVAL);
assert_return(tlv, -EINVAL);
r = lldp_read_port_id(tlv, &type, &data, &length);
r = sd_lldp_packet_read_port_id(tlv, &type, &data, &length);
if (r < 0)
return r;
@@ -312,11 +313,11 @@ int lldp_mib_add_objects(Prioq *by_expiry,
assert_return(neighbour_mib, -EINVAL);
assert_return(tlv, -EINVAL);
r = lldp_read_chassis_id(tlv, &subtype, &data, &length);
r = sd_lldp_packet_read_chassis_id(tlv, &subtype, &data, &length);
if (r < 0)
goto drop;
r = lldp_read_ttl(tlv, &ttl);
r = sd_lldp_packet_read_ttl(tlv, &ttl);
if (r < 0)
goto drop;
@@ -401,7 +402,7 @@ int lldp_mib_add_objects(Prioq *by_expiry,
return 0;
drop:
tlv_packet_unref(tlv);
sd_lldp_packet_unref(tlv);
if (new_chassis)
hashmap_remove(neighbour_mib, &c->chassis_id);
@@ -435,7 +436,7 @@ void lldp_neighbour_port_free(lldp_neighbour_port *p) {
if(!p)
return;
tlv_packet_unref(p->packet);
sd_lldp_packet_unref(p->packet);
free(p->data);
free(p);
@@ -452,11 +453,11 @@ int lldp_neighbour_port_new(lldp_chassis *c,
assert(tlv);
r = lldp_read_port_id(tlv, &type, &data, &length);
r = sd_lldp_packet_read_port_id(tlv, &type, &data, &length);
if (r < 0)
return r;
r = lldp_read_ttl(tlv, &ttl);
r = sd_lldp_packet_read_ttl(tlv, &ttl);
if (r < 0)
return r;
@@ -505,7 +506,7 @@ int lldp_chassis_new(tlv_packet *tlv,
assert(tlv);
r = lldp_read_chassis_id(tlv, &type, &data, &length);
r = sd_lldp_packet_read_chassis_id(tlv, &type, &data, &length);
if (r < 0)
return r;
-8
View File
@@ -86,13 +86,5 @@ int lldp_mib_update_objects(lldp_chassis *c, tlv_packet *tlv);
int lldp_mib_add_objects(Prioq *by_expiry, Hashmap *neighbour_mib, tlv_packet *tlv);
int lldp_mib_remove_objects(lldp_chassis *c, tlv_packet *tlv);
int lldp_read_chassis_id(tlv_packet *tlv, uint8_t *type, uint8_t **data, uint16_t *length);
int lldp_read_port_id(tlv_packet *tlv, uint8_t *type, uint8_t **data, uint16_t *length);
int lldp_read_ttl(tlv_packet *tlv, uint16_t *ttl);
int lldp_read_system_name(tlv_packet *tlv, char **data, uint16_t *length);
int lldp_read_system_description(tlv_packet *tlv, char **data, uint16_t *length);
int lldp_read_system_capability(tlv_packet *tlv, uint16_t *data);
int lldp_read_port_description(tlv_packet *tlv, char **data, uint16_t *length);
int lldp_handle_packet(tlv_packet *m, uint16_t length);
#define log_lldp(fmt, ...) log_internal(LOG_DEBUG, 0, __FILE__, __LINE__, __func__, "LLDP: " fmt, ##__VA_ARGS__)
+1 -1
View File
@@ -84,7 +84,7 @@ int lldp_network_bind_raw_socket(int ifindex) {
}
int lldp_receive_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_tlv_packet_free_ tlv_packet *packet = NULL;
_cleanup_lldp_packet_unref_ tlv_packet *packet = NULL;
tlv_packet *p;
uint16_t length;
int r;
+2 -2
View File
@@ -61,7 +61,7 @@ int tlv_packet_new(tlv_packet **ret) {
return 0;
}
tlv_packet *tlv_packet_ref(tlv_packet *m) {
tlv_packet *sd_lldp_packet_ref(tlv_packet *m) {
if (!m)
return NULL;
@@ -72,7 +72,7 @@ tlv_packet *tlv_packet_ref(tlv_packet *m) {
return m;
}
tlv_packet *tlv_packet_unref(tlv_packet *m) {
tlv_packet *sd_lldp_packet_unref(tlv_packet *m) {
tlv_section *s, *n;
if (!m)
+4 -4
View File
@@ -28,6 +28,8 @@
#include "lldp.h"
#include "list.h"
#include "sd-lldp.h"
typedef struct tlv_packet tlv_packet;
typedef struct tlv_section tlv_section;
@@ -63,11 +65,9 @@ struct tlv_packet {
};
int tlv_packet_new(tlv_packet **ret);
tlv_packet *tlv_packet_ref(tlv_packet *m);
tlv_packet *tlv_packet_unref(tlv_packet *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(tlv_packet*, tlv_packet_unref);
#define _cleanup_tlv_packet_free_ _cleanup_(tlv_packet_unrefp)
DEFINE_TRIVIAL_CLEANUP_FUNC(sd_lldp_packet*, sd_lldp_packet_unref);
#define _cleanup_lldp_packet_unref_ _cleanup_(sd_lldp_packet_unrefp)
int lldp_tlv_packet_open_container(tlv_packet *m, uint16_t type);
int lldp_tlv_packet_close_container(tlv_packet *m);
+5 -5
View File
@@ -338,7 +338,7 @@ int lldp_handle_packet(tlv_packet *tlv, uint16_t length) {
lldp->statistics.stats_frames_in_errors_total ++;
}
tlv_packet_unref(tlv);
sd_lldp_packet_unref(tlv);
return 0;
}
@@ -455,7 +455,7 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
_cleanup_free_ char *s = NULL;
char *k, *t;
r = lldp_read_chassis_id(p->packet, &type, &mac, &length);
r = sd_lldp_packet_read_chassis_id(p->packet, &type, &mac, &length);
if (r < 0)
continue;
@@ -468,7 +468,7 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
goto fail;
}
r = lldp_read_port_id(p->packet, &type, &port_id, &length);
r = sd_lldp_packet_read_port_id(p->packet, &type, &port_id, &length);
if (r < 0)
continue;
@@ -513,7 +513,7 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
free(s);
s = k;
r = lldp_read_system_name(p->packet, &k, &length);
r = sd_lldp_packet_read_system_name(p->packet, &k, &length);
if (r < 0)
k = strappend(s, "'_NAME=N/A' ");
else {
@@ -535,7 +535,7 @@ int sd_lldp_save(sd_lldp *lldp, const char *lldp_file) {
free(s);
s = k;
(void) lldp_read_system_capability(p->packet, &data);
(void) sd_lldp_packet_read_system_capability(p->packet, &data);
sprintf(buf, "'_CAP=%x'", data);
+2 -2
View File
@@ -38,7 +38,7 @@ static struct ether_addr mac_addr = {
};
static int lldp_build_tlv_packet(tlv_packet **ret) {
_cleanup_tlv_packet_free_ tlv_packet *m = NULL;
_cleanup_lldp_packet_unref_ tlv_packet *m = NULL;
const uint8_t lldp_dst[] = LLDP_MULTICAST_ADDR;
struct ether_header ether = {
.ether_type = htons(ETHERTYPE_LLDP),
@@ -216,7 +216,7 @@ static int lldp_parse_tlv_packet(tlv_packet *m, int len) {
}
int main(int argc, char *argv[]) {
_cleanup_tlv_packet_free_ tlv_packet *tlv = NULL;
_cleanup_lldp_packet_unref_ tlv_packet *tlv = NULL;
/* form a packet */
lldp_build_tlv_packet(&tlv);
+12
View File
@@ -29,6 +29,7 @@ enum {
};
typedef struct sd_lldp sd_lldp;
typedef struct tlv_packet sd_lldp_packet;
typedef void (*sd_lldp_cb_t)(sd_lldp *lldp, int event, void *userdata);
@@ -43,3 +44,14 @@ int sd_lldp_detach_event(sd_lldp *lldp);
int sd_lldp_set_callback(sd_lldp *lldp, sd_lldp_cb_t cb, void *userdata);
int sd_lldp_save(sd_lldp *lldp, const char *file);
int sd_lldp_packet_read_chassis_id(sd_lldp_packet *tlv, uint8_t *type, uint8_t **data, uint16_t *length);
int sd_lldp_packet_read_port_id(sd_lldp_packet *tlv, uint8_t *type, uint8_t **data, uint16_t *length);
int sd_lldp_packet_read_ttl(sd_lldp_packet *tlv, uint16_t *ttl);
int sd_lldp_packet_read_system_name(sd_lldp_packet *tlv, char **data, uint16_t *length);
int sd_lldp_packet_read_system_description(sd_lldp_packet *tlv, char **data, uint16_t *length);
int sd_lldp_packet_read_system_capability(sd_lldp_packet *tlv, uint16_t *data);
int sd_lldp_packet_read_port_description(sd_lldp_packet *tlv, char **data, uint16_t *length);
sd_lldp_packet *sd_lldp_packet_ref(sd_lldp_packet *tlv);
sd_lldp_packet *sd_lldp_packet_unref(sd_lldp_packet *tlv);