From d6d672aaf930661f502a623d18d7a4abb4f184f2 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Mon, 15 May 2006 16:56:51 +0000 Subject: [lib] Add string mapping table for Zserv commands 2006-05-15 Paul Jakma * log.c: (general) Generalise struct zebra_route_desc into struct zebra_desc_table and, similar to route_types, add a command_types table to describe Zserv protocol commands. (route_types[]) use a macro to use designated initialisers while avoiding tedious duplication. (zserv_command_string) lookup string from zebra_desc_table, similar to zebra_route_string * zebra.h: Add declaration for zserv_command_string, adjust the comments to reflect zebra_desc_table. --- lib/log.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 63 insertions(+), 20 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index 6748dbc0..d55ffb7f 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,5 +1,5 @@ /* - * $Id: log.c,v 1.26 2005/10/01 17:38:07 ajs Exp $ + * $Id: log.c,v 1.27 2006/05/15 16:56:51 paul Exp $ * * Logging of zebra * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro @@ -704,29 +704,61 @@ safe_strerror(int errnum) return (s != NULL) ? s : "Unknown error"; } -/* Note: this table must match the ordering in lib/zebra.h */ -static const struct zebra_route_desc { - u_int zroute; +struct zebra_desc_table +{ + unsigned int type; const char *string; char chr; -} route_types[] = { - { ZEBRA_ROUTE_SYSTEM, "system", 'X' }, - { ZEBRA_ROUTE_KERNEL, "kernel", 'K' }, - { ZEBRA_ROUTE_CONNECT, "connected", 'C' }, - { ZEBRA_ROUTE_STATIC, "static", 'S' }, - { ZEBRA_ROUTE_RIP, "rip", 'R' }, - { ZEBRA_ROUTE_RIPNG, "ripng", 'R' }, - { ZEBRA_ROUTE_OSPF, "ospf", 'O' }, - { ZEBRA_ROUTE_OSPF6, "ospf6", 'O' }, - { ZEBRA_ROUTE_ISIS, "isis", 'I' }, - { ZEBRA_ROUTE_BGP, "bgp", 'B' }, - { ZEBRA_ROUTE_HSLS, "hsls", 'H' }, }; -static const struct zebra_route_desc * +#define DESC_ENTRY(T,S,C) [(T)] = { (T), (S), (C) } +static const struct zebra_desc_table route_types[] = { + DESC_ENTRY (ZEBRA_ROUTE_SYSTEM, "system", 'X' ), + DESC_ENTRY (ZEBRA_ROUTE_KERNEL, "kernel", 'K' ), + DESC_ENTRY (ZEBRA_ROUTE_CONNECT, "connected", 'C' ), + DESC_ENTRY (ZEBRA_ROUTE_STATIC, "static", 'S' ), + DESC_ENTRY (ZEBRA_ROUTE_RIP, "rip", 'R' ), + DESC_ENTRY (ZEBRA_ROUTE_RIPNG, "ripng", 'R' ), + DESC_ENTRY (ZEBRA_ROUTE_OSPF, "ospf", 'O' ), + DESC_ENTRY (ZEBRA_ROUTE_OSPF6, "ospf6", 'O' ), + DESC_ENTRY (ZEBRA_ROUTE_ISIS, "isis", 'I' ), + DESC_ENTRY (ZEBRA_ROUTE_BGP, "bgp", 'B' ), + DESC_ENTRY (ZEBRA_ROUTE_HSLS, "hsls", 'H' ), +}; +#undef DESC_ENTRY + +#define DESC_ENTRY(T) [(T)] = { (T), (#T), '\0' } +static const struct zebra_desc_table command_types[] = { + DESC_ENTRY (ZEBRA_INTERFACE_ADD), + DESC_ENTRY (ZEBRA_INTERFACE_DELETE), + DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_ADD), + DESC_ENTRY (ZEBRA_INTERFACE_ADDRESS_DELETE), + DESC_ENTRY (ZEBRA_INTERFACE_UP), + DESC_ENTRY (ZEBRA_INTERFACE_DOWN), + DESC_ENTRY (ZEBRA_IPV4_ROUTE_ADD), + DESC_ENTRY (ZEBRA_IPV4_ROUTE_DELETE), + DESC_ENTRY (ZEBRA_IPV6_ROUTE_ADD), + DESC_ENTRY (ZEBRA_IPV6_ROUTE_DELETE), + DESC_ENTRY (ZEBRA_REDISTRIBUTE_ADD), + DESC_ENTRY (ZEBRA_REDISTRIBUTE_DELETE), + DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_ADD), + DESC_ENTRY (ZEBRA_REDISTRIBUTE_DEFAULT_DELETE), + DESC_ENTRY (ZEBRA_IPV4_NEXTHOP_LOOKUP), + DESC_ENTRY (ZEBRA_IPV6_NEXTHOP_LOOKUP), + DESC_ENTRY (ZEBRA_IPV4_IMPORT_LOOKUP), + DESC_ENTRY (ZEBRA_IPV6_IMPORT_LOOKUP), + DESC_ENTRY (ZEBRA_INTERFACE_RENAME), + DESC_ENTRY (ZEBRA_ROUTER_ID_ADD), + DESC_ENTRY (ZEBRA_ROUTER_ID_DELETE), + DESC_ENTRY (ZEBRA_ROUTER_ID_UPDATE), +}; +#undef DESC_ENTRY + +static const struct zebra_desc_table unknown = { 0, "unknown", '?' }; + +static const struct zebra_desc_table * zroute_lookup(u_int zroute) { - static const struct zebra_route_desc unknown = { 0, "unknown", '?' }; u_int i; if (zroute >= sizeof(route_types)/sizeof(route_types[0])) @@ -734,11 +766,11 @@ zroute_lookup(u_int zroute) zlog_err("unknown zebra route type: %u", zroute); return &unknown; } - if (zroute == route_types[zroute].zroute) + if (zroute == route_types[zroute].type) return &route_types[zroute]; for (i = 0; i < sizeof(route_types)/sizeof(route_types[0]); i++) { - if (zroute == route_types[i].zroute) + if (zroute == route_types[i].type) { zlog_warn("internal error: route type table out of order " "while searching for %u, please notify developers", zroute); @@ -760,3 +792,14 @@ zebra_route_char(u_int zroute) { return zroute_lookup(zroute)->chr; } + +const char * +zserv_command_string (unsigned int command) +{ + if (command >= sizeof(command_types)/sizeof(command_types[0])) + { + zlog_err ("unknown zserv command type: %u", command); + return unknown.string; + } + return command_types[command].string; +} -- cgit v1.2.3 From fb66b29c6701129f0222275eaa206f7ac8ab30cc Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Sun, 28 May 2006 08:26:15 +0000 Subject: [lib] Add support for Sun libc printstack to zlog_backtrace_sigsafe 2006-05-28 Paul Jakma * configure.ac: Check for Sun libc printstack(), add a general HAVE_STACK_TRACE define for lib/log.c, if any supported stack symbol dumping function is found (glibc backtrace/sun libc printstack). * log.c: (general) Add support for Sun libc printstack(). (hex_append) make the cpp conditional on general HAVE_STACK_TRACE define. (zlog_backtrace_sigsafe) Ditto. Add printstack() version of the the DUMP macro in this function. --- lib/log.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index d55ffb7f..3eb0bd52 100644 --- a/lib/log.c +++ b/lib/log.c @@ -1,5 +1,5 @@ /* - * $Id: log.c,v 1.27 2006/05/15 16:56:51 paul Exp $ + * $Id$ * * Logging of zebra * Copyright (C) 1997, 1998, 1999 Kunihiro Ishiguro @@ -179,7 +179,7 @@ num_append(char *s, int len, u_long x) return str_append(s,len,t); } -#if defined(SA_SIGINFO) || defined(HAVE_GLIBC_BACKTRACE) +#if defined(SA_SIGINFO) || defined(HAVE_STACK_TRACE) static char * hex_append(char *s, int len, u_long x) { @@ -371,7 +371,7 @@ zlog_signal(int signo, const char *action void zlog_backtrace_sigsafe(int priority, void *program_counter) { -#ifdef HAVE_GLIBC_BACKTRACE +#ifdef HAVE_STACK_TRACE static const char pclabel[] = "Program counter: "; void *array[20]; int size; @@ -379,13 +379,10 @@ zlog_backtrace_sigsafe(int priority, void *program_counter) char *s; #define LOC s,buf+sizeof(buf)-s +#ifdef HAVE_GLIBC_BACKTRACE if (((size = backtrace(array,sizeof(array)/sizeof(array[0]))) <= 0) || ((size_t)size > sizeof(array)/sizeof(array[0]))) return; - s = buf; - s = str_append(LOC,"Backtrace for "); - s = num_append(LOC,size); - s = str_append(LOC," stack frames:\n"); #define DUMP(FD) { \ if (program_counter) \ @@ -396,6 +393,19 @@ zlog_backtrace_sigsafe(int priority, void *program_counter) write(FD, buf, s-buf); \ backtrace_symbols_fd(array, size, FD); \ } +#elif defined(HAVE_PRINTSTACK) +#define DUMP(FD) { \ + if (program_counter) \ + write((FD), pclabel, sizeof(pclabel)-1); \ + write((FD), buf, s-buf); \ + printstack((FD)); \ +} +#endif /* HAVE_GLIBC_BACKTRACE, HAVE_PRINTSTACK */ + + s = buf; + s = str_append(LOC,"Backtrace for "); + s = num_append(LOC,size); + s = str_append(LOC," stack frames:\n"); if ((logfile_fd >= 0) || ((logfile_fd = open_crashlog()) >= 0)) DUMP(logfile_fd) @@ -431,7 +441,7 @@ zlog_backtrace_sigsafe(int priority, void *program_counter) } #undef DUMP #undef LOC -#endif /* HAVE_GLIBC_BACKTRACE */ +#endif /* HAVE_STRACK_TRACE */ } void -- cgit v1.2.3 From afb88a669167358ea2be64aa0d10c8c42fa7509c Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Tue, 20 Mar 2007 20:48:27 +0000 Subject: [lib] Make message lookup function more robust. 2007-03-20 Andrew J. Schorr * log.c: (mes_lookup) Make the function more robust: check for cases where the index does not match the key value at that position. If so, give a warning and fall back to a linear search. And improve the error message in cases where even that fails. --- lib/log.c | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index 3eb0bd52..c123f24f 100644 --- a/lib/log.c +++ b/lib/log.c @@ -693,17 +693,32 @@ lookup (struct message *mes, int key) return ""; } -/* Very old hacky version of message lookup function. Still partly - used in bgpd and ospfd. FIXME Seems that it's not used any more. */ +/* Older/faster version of message lookup function, but requires caller to pass + in the array size (instead of relying on a 0 key to terminate the search). */ const char * mes_lookup (struct message *meslist, int max, int index) { - if (index < 0 || index >= max) - { - zlog_err ("message index out of bound: %d", max); - return NULL; - } - return meslist[index].str; + /* first check for best case: index is in range and matches the key + value in that slot */ + if ((index >= 0) && (index < max) && (meslist[index].key == index)) + return meslist[index].str; + + /* fall back to linear search */ + { + int i; + + for (i = 0; i < max; i++, meslist++) + { + if (meslist->key == index) + { + zlog_warn("message index %d [%s] found in position %d (max is %d)", + index, meslist->str, i, max); + return meslist->str; + } + } + } + zlog_err("message index %d not found (max is %d)", index, max); + return NULL; } /* Wrapper around strerror to handle case where it returns NULL. */ -- cgit v1.2.3 From 1ed72e0b3a643fa1be6f1efa904965798a575cd1 Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Sat, 28 Apr 2007 22:14:10 +0000 Subject: [logging] Add new "log timestamp precision" command for subsecond timestamps 2007-04-28 Andrew J. Schorr * command.c: (config_write_host) Save "log timestamp precision" if not default value. (show_logging) Show configured timestamp precision. (config_log_timestamp_precision) Enable configuration of timestamp precision. (no_config_log_timestamp_precision) Restore default timestamp precision. (cmd_init) Install new timestamp precision commands. * log.h: (struct zlog) New timestamp_precision field. (quagga_timestamp) New function to generate a timestamp with the desired precision. (struct timestamp_control) Declare a structure for use in avoiding repeated duplicate calls to quagga_timestamp. * log.c: (quagga_timestamp) New function to generate a timestamp of the desired precision. (time_print) Call quagga_timestamp if the time hasn't already been calculated. (vzlog) Initialize a timestamp_control structure and pass it to time_print and vty_log. (zlog_backtrace) Fix 64-bit problem: cannot print size_t with %u. * vty.h: Must now include "log.h". (vty_log) Takes an additional struct timestamp_control argument. * vty.c: (vty_log_out) Use new struct timestamp_control and new quagga_timestamp function to print timestamps of the desired precision. (vty_time_print) Use new quagga_timestamp function. (vty_log) Accept new struct timestamp_control argument and pass it down to vty_log_out. --- lib/log.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 73 insertions(+), 22 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index c123f24f..76682227 100644 --- a/lib/log.c +++ b/lib/log.c @@ -66,32 +66,81 @@ const char *zlog_priority[] = /* For time string format. */ -#define TIME_BUF 27 -/* Utility routine for current time printing. */ -static void -time_print (FILE *fp) +size_t +quagga_timestamp(int timestamp_precision, char *buf, size_t buflen) { - int ret; - char buf [TIME_BUF]; - time_t clock; - struct tm *tm; - - time (&clock); - tm = localtime (&clock); + static struct { + time_t last; + size_t len; + char buf[28]; + } cache; + struct timeval clock; + + /* would it be sufficient to use global 'recent_time' here? I fear not... */ + gettimeofday(&clock, NULL); + + /* first, we update the cache if the time has changed */ + if (cache.last != clock.tv_sec) + { + struct tm *tm; + cache.last = clock.tv_sec; + tm = localtime(&cache.last); + cache.len = strftime(cache.buf, sizeof(cache.buf), + "%Y/%m/%d %H:%M:%S", tm); + } + /* note: it's not worth caching the subsecond part, because + chances are that back-to-back calls are not sufficiently close together + for the clock not to have ticked forward */ - ret = strftime (buf, TIME_BUF, "%Y/%m/%d %H:%M:%S", tm); - if (ret == 0) { - zlog_warn ("strftime error"); - } + if (buflen > cache.len) + { + memcpy(buf, cache.buf, cache.len); + if ((timestamp_precision > 0) && + (buflen > cache.len+1+timestamp_precision)) + { + /* should we worry about locale issues? */ + long divisor = 100000; + char *p = buf+cache.len; + *p++ = '.'; + do + { + *p++ = '0'+(clock.tv_usec/divisor); + clock.tv_usec %= divisor; + divisor /= 10; + } + while (--timestamp_precision > 0); + *p = '\0'; + return p-buf; + } + buf[cache.len] = '\0'; + return cache.len; + } + if (buflen > 0) + buf[0] = '\0'; + return 0; +} - fprintf (fp, "%s ", buf); +/* Utility routine for current time printing. */ +static void +time_print(FILE *fp, struct timestamp_control *ctl) +{ + if (!ctl->already_rendered) + { + ctl->len = quagga_timestamp(ctl->precision, ctl->buf, sizeof(ctl->buf)); + ctl->already_rendered = 1; + } + fprintf(fp, "%s ", ctl->buf); } + /* va_list version of zlog. */ static void vzlog (struct zlog *zl, int priority, const char *format, va_list args) { + struct timestamp_control tsctl; + tsctl.already_rendered = 0; + /* If zlog is not specified, use default one. */ if (zl == NULL) zl = zlog_default; @@ -99,7 +148,8 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args) /* When zlog_default is also NULL, use stderr for logging. */ if (zl == NULL) { - time_print (stderr); + tsctl.precision = 0; + time_print(stderr, &tsctl); fprintf (stderr, "%s: ", "unknown"); vfprintf (stderr, format, args); fprintf (stderr, "\n"); @@ -108,6 +158,7 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args) /* In this case we return at here. */ return; } + tsctl.precision = zl->timestamp_precision; /* Syslog output */ if (priority <= zl->maxlvl[ZLOG_DEST_SYSLOG]) @@ -122,7 +173,7 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args) if ((priority <= zl->maxlvl[ZLOG_DEST_FILE]) && zl->fp) { va_list ac; - time_print (zl->fp); + time_print (zl->fp, &tsctl); if (zl->record_priority) fprintf (zl->fp, "%s: ", zlog_priority[priority]); fprintf (zl->fp, "%s: ", zlog_proto_names[zl->protocol]); @@ -137,7 +188,7 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args) if (priority <= zl->maxlvl[ZLOG_DEST_STDOUT]) { va_list ac; - time_print (stdout); + time_print (stdout, &tsctl); if (zl->record_priority) fprintf (stdout, "%s: ", zlog_priority[priority]); fprintf (stdout, "%s: ", zlog_proto_names[zl->protocol]); @@ -151,7 +202,7 @@ vzlog (struct zlog *zl, int priority, const char *format, va_list args) /* Terminal monitor. */ if (priority <= zl->maxlvl[ZLOG_DEST_MONITOR]) vty_log ((zl->record_priority ? zlog_priority[priority] : NULL), - zlog_proto_names[zl->protocol], format, args); + zlog_proto_names[zl->protocol], format, &tsctl, args); } static char * @@ -458,8 +509,8 @@ zlog_backtrace(int priority) ((size_t)size > sizeof(array)/sizeof(array[0]))) { zlog_err("Cannot get backtrace, returned invalid # of frames %d " - "(valid range is between 1 and %u)", - size, sizeof(array)/sizeof(array[0])); + "(valid range is between 1 and %lu)", + size, (unsigned long)(sizeof(array)/sizeof(array[0]))); return; } zlog(NULL, priority, "Backtrace for %d stack frames:", size); -- cgit v1.2.3 From bcdda30bae1fef67d998211c54c06270ba42864c Mon Sep 17 00:00:00 2001 From: "Andrew J. Schorr" Date: Sun, 29 Apr 2007 15:48:22 +0000 Subject: [logging] Minor performance tweak 2007-04-29 Andrew J. Schorr * log.c: (quagga_timestamp) Optimize the subsecond timestamp generation. --- lib/log.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index 76682227..21bf3f2f 100644 --- a/lib/log.c +++ b/lib/log.c @@ -100,18 +100,25 @@ quagga_timestamp(int timestamp_precision, char *buf, size_t buflen) (buflen > cache.len+1+timestamp_precision)) { /* should we worry about locale issues? */ - long divisor = 100000; - char *p = buf+cache.len; - *p++ = '.'; + static const int divisor[] = {0, 100000, 10000, 1000, 100, 10, 1}; + int prec; + char *p = buf+cache.len+1+(prec = timestamp_precision); + *p-- = '\0'; + while (prec > 6) + /* this is unlikely to happen, but protect anyway */ + { + *p-- = '0'; + prec--; + } + clock.tv_usec /= divisor[prec]; do { - *p++ = '0'+(clock.tv_usec/divisor); - clock.tv_usec %= divisor; - divisor /= 10; + *p-- = '0'+(clock.tv_usec % 10); + clock.tv_usec /= 10; } - while (--timestamp_precision > 0); - *p = '\0'; - return p-buf; + while (--prec > 0); + *p = '.'; + return cache.len+1+timestamp_precision; } buf[cache.len] = '\0'; return cache.len; -- cgit v1.2.3 From 7514fb7739f74311830e9ddd1381d0d228224f61 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Wed, 2 May 2007 16:05:35 +0000 Subject: [zebra] Routemap support on received routes, with 'set src' command (linux) 2007-05-01 David L Stevens * (general) These changes collectively add route-map and prefix-list support to zebra and fix a bug in "show route-map" (with no argument). * doc/main.texi: added route-map, prefix-list, ip protocol and set src documentation * lib/command.h: added PROTOCOL_NODE type * lib/log.c: (proto_name2num) new function, protocol name to number translation. * lib/routemap.c: (vty_show_route_map) fixed "show route-map" without route-map name * lib/routemap.h: added RMAP_ZEBRA type * lib/zebra.h: added proto_name2num() prototype * vtysh/extract.pl.in: added VTYSH_ZEBRA flag for route-map and plist * vtysh/Makefile.am: added zebra_routemap.c * vtysh/vtysh.h: added VTYSH_ZEBRA flag to VTYSH_RMAP * zebra/connected.c: (connected_up_ipv4) added src preference argument to rib_add_ipv4() * zebra/kernel_socket.c: (rtm_read) ditto * zebra/main.c: added prefix list initialization * zebra/Makefile.am: added zebra_routemap.c source file * zebra/rib.h: added generic address union "g_addr" and use in existing places that had an explicit union. Added "src" to struct nexthop. Added preferred src arg to nexthop_ipv4_add and rib_add_ipv4. * zebra/rt_netlink.c: (netlink_routing_table) set preferred source on netlink messages. (netlink_route_change) ditto (netlink_route_multipath) ditto. * zebra/rtread_getmsg.c: (handle_route_entry) added (NULL) src to rib_add_ipv4() call. * zebra/rtread_proc.c: (proc_route_read) ditto * zebra/zebra_rib.c: (nexthop_ipv4_add) add src argument. (nexthop_ipv4_ifindex_add) ditto (rib_add_ipv4) ditto (nexthop_active_check) Add route-map processing. * zebra/zebra_routemap.c: new file for zebra route-map commands. * zebra/zebra_vty.c: (ip_protocol_cmd) Apply route-map to protocol (vty_show_ip_route_detail) added "src" printing (vty_show_ip_route) ditto (show_ip_protocol_cmd) new command, list routemaps. (config_write_protocol) write out routemap protocl config. (zebra_vty_init) Install the new routemap protocol commands. * zebra/zserv.c: (zread_ipv4_add) added (NULL) src arg (zebra_init) init zebra route-maps. * zebra/zserv.h: add zebra_route_map_init --- lib/log.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index 21bf3f2f..cbf76af9 100644 --- a/lib/log.c +++ b/lib/log.c @@ -886,3 +886,17 @@ zserv_command_string (unsigned int command) } return command_types[command].string; } + +#define RTSIZE (sizeof(route_types)/sizeof(route_types[0])) + +int +proto_name2num(const char *s) +{ + unsigned i; + + for (i=0; i Date: Mon, 6 Aug 2007 15:21:45 +0000 Subject: [bgpd] cleanup, compact and consolidate capability parsing code 2007-07-26 Paul Jakma * (general) Clean up and compact capability parsing slightly. Consolidate validation of length and logging of generic TLV, and memcpy of capability data, thus removing such from cap specifc code (not always present or correct). * bgp_open.h: Add structures for the generic capability TLV header and for the data formats of the various specific capabilities we support. Hence remove the badly named, or else misdefined, struct capability. * bgp_open.c: (bgp_capability_vty_out) Use struct capability_mp_data. Do the length checks *before* memcpy()'ing based on that length (stored capability - should have been validated anyway on input, but..). (bgp_afi_safi_valid_indices) new function to validate (afi,safi) which is about to be used as index into arrays, consolidates several instances of same, at least one of which appeared to be incomplete.. (bgp_capability_mp) Much condensed. (bgp_capability_orf_entry) New, process one ORF entry (bgp_capability_orf) Condensed. Fixed to process all ORF entries. (bgp_capability_restart) Condensed, and fixed to use a cap-specific type, rather than abusing capability_mp. (struct message capcode_str) added to aid generic logging. (size_t cap_minsizes[]) added to aid generic validation of capability length field. (bgp_capability_parse) Generic logging and validation of TLV consolidated here. Code compacted as much as possible. * bgp_packet.c: (bgp_open_receive) Capability parsers now use streams, so no more need here to manually fudge the input stream getp. (bgp_capability_msg_parse) use struct capability_mp_data. Validate lengths /before/ memcpy. Use bgp_afi_safi_valid_indices. (bgp_capability_receive) Exported for use by test harness. * bgp_vty.c: (bgp_show_summary) fix conversion warning (bgp_show_peer) ditto * bgp_debug.h: Fix storage 'extern' after type 'const'. * lib/log.c: (mes_lookup) warning about code not being in same-number array slot should be debug, not warning. E.g. BGP has several discontigious number spaces, allocating from different parts of a space is not uncommon (e.g. IANA assigned versus vendor-assigned code points in some number space). --- lib/log.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index cbf76af9..ff47cae0 100644 --- a/lib/log.c +++ b/lib/log.c @@ -769,7 +769,7 @@ mes_lookup (struct message *meslist, int max, int index) { if (meslist->key == index) { - zlog_warn("message index %d [%s] found in position %d (max is %d)", + zlog_debug ("message index %d [%s] found in position %d (max is %d)", index, meslist->str, i, max); return meslist->str; } -- cgit v1.2.3 From 11486b5265b2e0e2cf8b140018c47bd9a35cba93 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Thu, 28 Feb 2008 23:26:02 +0000 Subject: [lib] Fix the struct message LOOKUP function to be more robust 2008-02-28 Paul Jakma * log.c: (mes_lookup) Sowmini Varadhan diagnosed a problem where this function can cause a NULL dereference, on lookups for unknown indices, or messages with NULL strings. Can occur, e.g., debug logging code when processing received messages. Fixed to accept a pointer to a default string to be used if there is no match. * log.h: LOOKUP adjusted to match --- lib/log.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index ff47cae0..ce00bfbb 100644 --- a/lib/log.c +++ b/lib/log.c @@ -752,14 +752,24 @@ lookup (struct message *mes, int key) } /* Older/faster version of message lookup function, but requires caller to pass - in the array size (instead of relying on a 0 key to terminate the search). */ + * in the array size (instead of relying on a 0 key to terminate the search). + * + * The return value is the message string if found, or the 'none' pointer + * provided otherwise. + */ const char * -mes_lookup (struct message *meslist, int max, int index) +mes_lookup (struct message *meslist, int max, int index, const char *none) { + int pos = index - meslist[0].key; + /* first check for best case: index is in range and matches the key - value in that slot */ - if ((index >= 0) && (index < max) && (meslist[index].key == index)) - return meslist[index].str; + * value in that slot. + * NB: key numbering might be offset from 0. E.g. protocol constants + * often start at 1. + */ + if ((pos >= 0) && (pos < max) + && (meslist[pos].key == index)) + return meslist[pos].str; /* fall back to linear search */ { @@ -769,14 +779,17 @@ mes_lookup (struct message *meslist, int max, int index) { if (meslist->key == index) { + const char *str = (meslist->str ? meslist->str : none); + zlog_debug ("message index %d [%s] found in position %d (max is %d)", - index, meslist->str, i, max); - return meslist->str; + index, str, i, max); + return str; } } } zlog_err("message index %d not found (max is %d)", index, max); - return NULL; + assert (none); + return none; } /* Wrapper around strerror to handle case where it returns NULL. */ -- cgit v1.2.3 From 1423c809cc4ddc2e013ba6264c49a11e5719c6f2 Mon Sep 17 00:00:00 2001 From: Stephen Hemminger Date: Thu, 14 Aug 2008 17:59:25 +0100 Subject: [lib] mes_lookup string lookup table argument should be marked const 2008-08-14 Stephen Hemminger * lib/log.{c,h}: struct message argument should point to const * */*.c: adjust to suit, Signed-off-by: Paul Jakma --- lib/log.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index ce00bfbb..677cf9ad 100644 --- a/lib/log.c +++ b/lib/log.c @@ -740,9 +740,9 @@ zlog_rotate (struct zlog *zl) /* Message lookup function. */ const char * -lookup (struct message *mes, int key) +lookup (const struct message *mes, int key) { - struct message *pnt; + const struct message *pnt; for (pnt = mes; pnt->key != 0; pnt++) if (pnt->key == key) -- cgit v1.2.3 From 30a2231a4881f53deca61ef7a62b225a43dab4c5 Mon Sep 17 00:00:00 2001 From: Paul Jakma Date: Fri, 15 Aug 2008 14:05:22 +0100 Subject: [warnings] Fix various SOS warnings 2008-08-15 Paul Jakma * */*: Fix various problems flagged by Sun Studio compiler. - ' ' obsolescent in declarations - empty statements (';' after ALIAS definitions) - implicit declarations (e.g printstack in lib/log.c) - "\%" in printf string instead of "%%" - loops that return on the first iteration (legitimately, but compiler can't really know), e.g. bgp_routemap.c - internal declarations which mask prototypes. --- lib/log.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib/log.c') diff --git a/lib/log.c b/lib/log.c index 677cf9ad..407904d5 100644 --- a/lib/log.c +++ b/lib/log.c @@ -30,6 +30,10 @@ #ifndef SUNOS_5 #include #endif +/* for printstack on solaris */ +#ifdef HAVE_UCONTEXT_H +#include +#endif static int logfile_fd = -1; /* Used in signal handler. */ -- cgit v1.2.3