ospf6d: fix clang warning, NULL() calls

Avoid show() methods from being a NULL function when it is called.
Meanwhile, I did const'ify the handlers so only its debug field can be changed.

Signed-off-by: Vincent Jardin <vincent.jardin@6wind.com>
This commit is contained in:
Vincent JARDIN 2017-10-08 18:20:12 +02:00
parent 21cf6b211b
commit 3981b5c7f3
5 changed files with 107 additions and 63 deletions

View File

@ -1110,19 +1110,21 @@ void install_element_ospf6_debug_abr(void)
} }
struct ospf6_lsa_handler inter_prefix_handler = { struct ospf6_lsa_handler inter_prefix_handler = {
OSPF6_LSTYPE_INTER_PREFIX, .lh_type = OSPF6_LSTYPE_INTER_PREFIX,
"Inter-Prefix", .lh_name = "Inter-Prefix",
"IAP", .lh_short_name = "IAP",
ospf6_inter_area_prefix_lsa_show, .lh_show = ospf6_inter_area_prefix_lsa_show,
ospf6_inter_area_prefix_lsa_get_prefix_str, .lh_get_prefix_str = ospf6_inter_area_prefix_lsa_get_prefix_str,
.lh_debug = 0
}; };
struct ospf6_lsa_handler inter_router_handler = { struct ospf6_lsa_handler inter_router_handler = {
OSPF6_LSTYPE_INTER_ROUTER, .lh_type = OSPF6_LSTYPE_INTER_ROUTER,
"Inter-Router", .lh_name = "Inter-Router",
"IAR", .lh_short_name = "IAR",
ospf6_inter_area_router_lsa_show, .lh_show = ospf6_inter_area_router_lsa_show,
ospf6_inter_area_router_lsa_get_prefix_str, .lh_get_prefix_str = ospf6_inter_area_router_lsa_get_prefix_str,
.lh_debug = 0
}; };
void ospf6_abr_init(void) void ospf6_abr_init(void)

View File

@ -1234,8 +1234,13 @@ DEFUN (show_ipv6_ospf6_redistribute,
} }
struct ospf6_lsa_handler as_external_handler = { struct ospf6_lsa_handler as_external_handler = {
OSPF6_LSTYPE_AS_EXTERNAL, "AS-External", "ASE", .lh_type = OSPF6_LSTYPE_AS_EXTERNAL,
ospf6_as_external_lsa_show, ospf6_as_external_lsa_get_prefix_str}; .lh_name = "AS-External",
.lh_short_name = "ASE",
.lh_show = ospf6_as_external_lsa_show,
.lh_get_prefix_str = ospf6_as_external_lsa_get_prefix_str,
.lh_debug = 0
};
void ospf6_asbr_init(void) void ospf6_asbr_init(void)
{ {

View File

@ -1630,21 +1630,41 @@ void ospf6_intra_brouter_calculation(struct ospf6_area *oa)
oa->name); oa->name);
} }
struct ospf6_lsa_handler router_handler = {OSPF6_LSTYPE_ROUTER, "Router", "Rtr", struct ospf6_lsa_handler router_handler = {
ospf6_router_lsa_show, .lh_type = OSPF6_LSTYPE_ROUTER,
ospf6_router_lsa_get_nbr_id}; .lh_name = "Router",
.lh_short_name = "Rtr",
.lh_show = ospf6_router_lsa_show,
.lh_get_prefix_str = ospf6_router_lsa_get_nbr_id,
.lh_debug = 0
};
struct ospf6_lsa_handler network_handler = {OSPF6_LSTYPE_NETWORK, "Network", struct ospf6_lsa_handler network_handler = {
"Net", ospf6_network_lsa_show, .lh_type = OSPF6_LSTYPE_NETWORK,
ospf6_network_lsa_get_ar_id}; .lh_name = "Network",
.lh_short_name = "Net",
.lh_show = ospf6_network_lsa_show,
.lh_get_prefix_str = ospf6_network_lsa_get_ar_id,
.lh_debug = 0
};
struct ospf6_lsa_handler link_handler = {OSPF6_LSTYPE_LINK, "Link", "Lnk", struct ospf6_lsa_handler link_handler = {
ospf6_link_lsa_show, .lh_type = OSPF6_LSTYPE_LINK,
ospf6_link_lsa_get_prefix_str}; .lh_name = "Link",
.lh_short_name = "Lnk",
.lh_show = ospf6_link_lsa_show,
.lh_get_prefix_str = ospf6_link_lsa_get_prefix_str,
.lh_debug = 0
};
struct ospf6_lsa_handler intra_prefix_handler = { struct ospf6_lsa_handler intra_prefix_handler = {
OSPF6_LSTYPE_INTRA_PREFIX, "Intra-Prefix", "INP", .lh_type = OSPF6_LSTYPE_INTRA_PREFIX,
ospf6_intra_prefix_lsa_show, ospf6_intra_prefix_lsa_get_prefix_str}; .lh_name = "Intra-Prefix",
.lh_short_name = "INP",
.lh_show = ospf6_intra_prefix_lsa_show,
.lh_get_prefix_str = ospf6_intra_prefix_lsa_get_prefix_str,
.lh_debug = 0
};
void ospf6_intra_init(void) void ospf6_intra_init(void)
{ {

View File

@ -68,19 +68,25 @@ static int ospf6_unknown_lsa_show(struct vty *vty, struct ospf6_lsa *lsa)
return 0; return 0;
} }
struct ospf6_lsa_handler unknown_handler = { static struct ospf6_lsa_handler unknown_handler = {
OSPF6_LSTYPE_UNKNOWN, "Unknown", "Unk", ospf6_unknown_lsa_show, NULL}; .lh_type = OSPF6_LSTYPE_UNKNOWN,
.lh_name = "Unknown",
.lh_short_name = "Unk",
.lh_show = ospf6_unknown_lsa_show,
.lh_get_prefix_str = NULL,
.lh_debug = 0 /* No default debug */
};
void ospf6_install_lsa_handler(struct ospf6_lsa_handler *handler) void ospf6_install_lsa_handler(const struct ospf6_lsa_handler *handler)
{ {
/* type in handler is host byte order */ /* type in handler is host byte order */
int index = handler->type & OSPF6_LSTYPE_FCODE_MASK; int index = handler->lh_type & OSPF6_LSTYPE_FCODE_MASK;
vector_set_index(ospf6_lsa_handler_vector, index, handler); vector_set_index(ospf6_lsa_handler_vector, index, (void *)handler);
} }
struct ospf6_lsa_handler *ospf6_get_lsa_handler(u_int16_t type) const struct ospf6_lsa_handler *ospf6_get_lsa_handler(u_int16_t type)
{ {
struct ospf6_lsa_handler *handler = NULL; const struct ospf6_lsa_handler *handler = NULL;
unsigned int index = ntohs(type) & OSPF6_LSTYPE_FCODE_MASK; unsigned int index = ntohs(type) & OSPF6_LSTYPE_FCODE_MASK;
if (index >= vector_active(ospf6_lsa_handler_vector)) if (index >= vector_active(ospf6_lsa_handler_vector))
@ -97,11 +103,11 @@ struct ospf6_lsa_handler *ospf6_get_lsa_handler(u_int16_t type)
const char *ospf6_lstype_name(u_int16_t type) const char *ospf6_lstype_name(u_int16_t type)
{ {
static char buf[8]; static char buf[8];
struct ospf6_lsa_handler *handler; const struct ospf6_lsa_handler *handler;
handler = ospf6_get_lsa_handler(type); handler = ospf6_get_lsa_handler(type);
if (handler && handler != &unknown_handler) if (handler && handler != &unknown_handler)
return handler->name; return handler->lh_name;
snprintf(buf, sizeof(buf), "0x%04hx", ntohs(type)); snprintf(buf, sizeof(buf), "0x%04hx", ntohs(type));
return buf; return buf;
@ -110,11 +116,11 @@ const char *ospf6_lstype_name(u_int16_t type)
const char *ospf6_lstype_short_name(u_int16_t type) const char *ospf6_lstype_short_name(u_int16_t type)
{ {
static char buf[8]; static char buf[8];
struct ospf6_lsa_handler *handler; const struct ospf6_lsa_handler *handler;
handler = ospf6_get_lsa_handler(type); handler = ospf6_get_lsa_handler(type);
if (handler && handler != &unknown_handler) if (handler && handler != &unknown_handler)
return handler->short_name; return handler->lh_short_name;
snprintf(buf, sizeof(buf), "0x%04hx", ntohs(type)); snprintf(buf, sizeof(buf), "0x%04hx", ntohs(type));
return buf; return buf;
@ -122,7 +128,7 @@ const char *ospf6_lstype_short_name(u_int16_t type)
u_char ospf6_lstype_debug(u_int16_t type) u_char ospf6_lstype_debug(u_int16_t type)
{ {
struct ospf6_lsa_handler *handler; const struct ospf6_lsa_handler *handler;
handler = ospf6_get_lsa_handler(type); handler = ospf6_get_lsa_handler(type);
return handler->debug; return handler->debug;
} }
@ -369,7 +375,7 @@ void ospf6_lsa_show_summary(struct vty *vty, struct ospf6_lsa *lsa)
{ {
char adv_router[16], id[16]; char adv_router[16], id[16];
int type; int type;
struct ospf6_lsa_handler *handler; const struct ospf6_lsa_handler *handler;
char buf[64], tmpbuf[80]; char buf[64], tmpbuf[80];
int cnt = 0; int cnt = 0;
@ -389,14 +395,14 @@ void ospf6_lsa_show_summary(struct vty *vty, struct ospf6_lsa *lsa)
ospf6_lstype_short_name(lsa->header->type), id, ospf6_lstype_short_name(lsa->header->type), id,
adv_router, ospf6_lsa_age_current(lsa), adv_router, ospf6_lsa_age_current(lsa),
(u_long)ntohl(lsa->header->seqnum), (u_long)ntohl(lsa->header->seqnum),
handler->get_prefix_str(lsa, buf, sizeof(buf), 0)); handler->lh_get_prefix_str(lsa, buf, sizeof(buf), 0));
} else if (type != OSPF6_LSTYPE_UNKNOWN) { } else if (type != OSPF6_LSTYPE_UNKNOWN) {
sprintf(tmpbuf, "%-4s %-15s%-15s%4hu %8lx", sprintf(tmpbuf, "%-4s %-15s%-15s%4hu %8lx",
ospf6_lstype_short_name(lsa->header->type), id, ospf6_lstype_short_name(lsa->header->type), id,
adv_router, ospf6_lsa_age_current(lsa), adv_router, ospf6_lsa_age_current(lsa),
(u_long)ntohl(lsa->header->seqnum)); (u_long)ntohl(lsa->header->seqnum));
while (handler->get_prefix_str(lsa, buf, sizeof(buf), cnt) while (handler->lh_get_prefix_str(lsa, buf, sizeof(buf), cnt)
!= NULL) { != NULL) {
vty_out(vty, "%s %30s\n", tmpbuf, buf); vty_out(vty, "%s %30s\n", tmpbuf, buf);
cnt++; cnt++;
@ -465,7 +471,7 @@ void ospf6_lsa_show_internal(struct vty *vty, struct ospf6_lsa *lsa)
void ospf6_lsa_show(struct vty *vty, struct ospf6_lsa *lsa) void ospf6_lsa_show(struct vty *vty, struct ospf6_lsa *lsa)
{ {
char adv_router[64], id[64]; char adv_router[64], id[64];
struct ospf6_lsa_handler *handler; const struct ospf6_lsa_handler *handler;
struct timeval now, res; struct timeval now, res;
char duration[64]; char duration[64];
@ -490,9 +496,13 @@ void ospf6_lsa_show(struct vty *vty, struct ospf6_lsa *lsa)
vty_out(vty, "Duration: %s\n", duration); vty_out(vty, "Duration: %s\n", duration);
handler = ospf6_get_lsa_handler(lsa->header->type); handler = ospf6_get_lsa_handler(lsa->header->type);
if (handler->show == NULL)
handler = &unknown_handler; if (handler->lh_show != NULL)
(*handler->show)(vty, lsa); handler->lh_show(vty, lsa);
else {
assert(unknown_handler.lh_show != NULL);
unknown_handler.lh_show(vty, lsa);
}
vty_out(vty, "\n"); vty_out(vty, "\n");
} }
@ -739,22 +749,22 @@ void ospf6_lsa_terminate(void)
vector_free(ospf6_lsa_handler_vector); vector_free(ospf6_lsa_handler_vector);
} }
static char *ospf6_lsa_handler_name(struct ospf6_lsa_handler *h) static char *ospf6_lsa_handler_name(const struct ospf6_lsa_handler *h)
{ {
static char buf[64]; static char buf[64];
unsigned int i; unsigned int i;
unsigned int size = strlen(h->name); unsigned int size = strlen(h->lh_name);
if (!strcmp(h->name, "unknown") && h->type != OSPF6_LSTYPE_UNKNOWN) { if (!strcmp(h->lh_name, "unknown") && h->lh_type != OSPF6_LSTYPE_UNKNOWN) {
snprintf(buf, sizeof(buf), "%#04hx", h->type); snprintf(buf, sizeof(buf), "%#04hx", h->lh_type);
return buf; return buf;
} }
for (i = 0; i < MIN(size, sizeof(buf)); i++) { for (i = 0; i < MIN(size, sizeof(buf)); i++) {
if (!islower((unsigned char)h->name[i])) if (!islower((unsigned char)h->lh_name[i]))
buf[i] = tolower((unsigned char)h->name[i]); buf[i] = tolower((unsigned char)h->lh_name[i]);
else else
buf[i] = h->name[i]; buf[i] = h->lh_name[i];
} }
buf[size] = '\0'; buf[size] = '\0';
return buf; return buf;
@ -791,7 +801,7 @@ DEFUN (debug_ospf6_lsa_type,
strlen(argv[idx_lsa]->arg)) strlen(argv[idx_lsa]->arg))
== 0) == 0)
break; break;
if (!strcasecmp(argv[idx_lsa]->arg, handler->name)) if (!strcasecmp(argv[idx_lsa]->arg, handler->lh_name))
break; break;
handler = NULL; handler = NULL;
} }
@ -844,7 +854,7 @@ DEFUN (no_debug_ospf6_lsa_type,
strlen(argv[idx_lsa]->arg)) strlen(argv[idx_lsa]->arg))
== 0) == 0)
break; break;
if (!strcasecmp(argv[idx_lsa]->arg, handler->name)) if (!strcasecmp(argv[idx_lsa]->arg, handler->lh_name))
break; break;
} }
@ -875,7 +885,7 @@ void install_element_ospf6_debug_lsa(void)
int config_write_ospf6_debug_lsa(struct vty *vty) int config_write_ospf6_debug_lsa(struct vty *vty)
{ {
u_int i; u_int i;
struct ospf6_lsa_handler *handler; const struct ospf6_lsa_handler *handler;
for (i = 0; i < vector_active(ospf6_lsa_handler_vector); i++) { for (i = 0; i < vector_active(ospf6_lsa_handler_vector); i++) {
handler = vector_slot(ospf6_lsa_handler_vector, i); handler = vector_slot(ospf6_lsa_handler_vector, i);

View File

@ -137,18 +137,25 @@ struct ospf6_lsa {
#define OSPF6_LSA_SEQWRAPPED 0x20 #define OSPF6_LSA_SEQWRAPPED 0x20
struct ospf6_lsa_handler { struct ospf6_lsa_handler {
u_int16_t type; /* host byte order */ const struct {
const char *name; u_int16_t type; /* host byte order */
const char *short_name; const char *name;
int (*show)(struct vty *, struct ospf6_lsa *); const char *short_name;
char *(*get_prefix_str)(struct ospf6_lsa *, char *buf, int buflen, int (*show)(struct vty *, struct ospf6_lsa *);
int pos); char *(*get_prefix_str)(struct ospf6_lsa *, char *buf, int buflen,
int pos);
} s;
#define lh_type s.type
#define lh_name s.name
#define lh_short_name s.short_name
#define lh_show s.show
#define lh_get_prefix_str s.get_prefix_str
u_char debug; u_char debug;
#define lh_debug debug
}; };
extern struct ospf6_lsa_handler unknown_handler; #define OSPF6_LSA_IS_KNOWN(t) \
#define OSPF6_LSA_IS_KNOWN(type) \ (ospf6_get_lsa_handler(t).lh_type != OSPF6_LSTYPE_UNKNOWN ? 1 : 0)
(ospf6_get_lsa_handler(type) != &unknown_handler ? 1 : 0)
extern vector ospf6_lsa_handler_vector; extern vector ospf6_lsa_handler_vector;
@ -237,8 +244,8 @@ extern int ospf6_lsa_checksum_valid(struct ospf6_lsa_header *);
extern int ospf6_lsa_prohibited_duration(u_int16_t type, u_int32_t id, extern int ospf6_lsa_prohibited_duration(u_int16_t type, u_int32_t id,
u_int32_t adv_router, void *scope); u_int32_t adv_router, void *scope);
extern void ospf6_install_lsa_handler(struct ospf6_lsa_handler *handler); extern void ospf6_install_lsa_handler(const struct ospf6_lsa_handler *handler);
extern struct ospf6_lsa_handler *ospf6_get_lsa_handler(u_int16_t type); extern const struct ospf6_lsa_handler *ospf6_get_lsa_handler(u_int16_t type);
extern void ospf6_lsa_init(void); extern void ospf6_lsa_init(void);
extern void ospf6_lsa_terminate(void); extern void ospf6_lsa_terminate(void);