From c46b619e7fb06c397d12a446b9146dc80ffca6e9 Mon Sep 17 00:00:00 2001 From: darkcoven Date: Tue, 8 Apr 2014 20:53:16 +0200 Subject: [PATCH] Factorize string related functions --- cli/src/Makefile.am | 4 +- cli/src/com_bitrate.c | 4 +- cli/src/com_ports.c | 35 +----- cli/src/com_qos.c | 22 +--- cli/src/com_stormfilter.c | 6 +- cli/src/com_vlan.c | 27 +--- cli/src/common.c | 114 +---------------- cli/src/common.h | 9 +- emu/src/emu.c | 2 +- lib/include/ngadmin.h | 9 ++ lib/src/misc.c | 26 +++- lib/src/network.c | 2 +- raw/include/nsdp/Makefile.am | 2 +- raw/include/nsdp/misc.h | 19 --- raw/include/nsdp/protocol.h | 1 + raw/include/nsdp/str.h | 73 +++++++++++ raw/src/Makefile.am | 2 +- raw/src/misc.c | 42 ------- raw/src/str.c | 146 +++++++++++++++++++++ spy/src/spy.c | 238 +++-------------------------------- 20 files changed, 293 insertions(+), 490 deletions(-) delete mode 100644 raw/include/nsdp/misc.h create mode 100644 raw/include/nsdp/str.h delete mode 100644 raw/src/misc.c create mode 100644 raw/src/str.c diff --git a/cli/src/Makefile.am b/cli/src/Makefile.am index 65b471c..c756408 100644 --- a/cli/src/Makefile.am +++ b/cli/src/Makefile.am @@ -6,6 +6,6 @@ ngcli_SOURCES = admin.c com_bitrate.c com_cabletest.c com_defaults.c com_firmwar com_help.c com_igmp.c com_list.c com_login.c commands.c com_mirror.c \ common.c com_name.c com_netconf.c com_password.c com_ports.c com_qos.c \ com_quit.c com_restart.c com_scan.c com_stormfilter.c com_tree.c com_vlan.c -ngcli_CPPFLAGS = -I$(top_srcdir)/lib/include/ -ngcli_LDADD = $(top_builddir)/lib/src/libngadmin.la $(READLINE_LIBS) +ngcli_CPPFLAGS = -I$(top_srcdir)/raw/include/ -I$(top_srcdir)/lib/include/ +ngcli_LDADD = $(top_builddir)/raw/src/librawnsdp.la $(top_builddir)/lib/src/libngadmin.la $(READLINE_LIBS) diff --git a/cli/src/com_bitrate.c b/cli/src/com_bitrate.c index 7085b82..dddeac3 100644 --- a/cli/src/com_bitrate.c +++ b/cli/src/com_bitrate.c @@ -9,7 +9,7 @@ static int bitrate_analyse (int argc, const char **argv, int *ports) while (i < argc - 1) { - s = parseBitrate(argv[i + 1]); + s = parseBitrateStr(argv[i + 1]); if (strcmp(argv[i], "inout") == 0) { ports[0] = s; ports[1] = s; @@ -109,7 +109,7 @@ int do_bitrate_show (int argc, const char **argv UNUSED, struct ngadmin *nga) } for (i = 0; i < sa->ports; i++) - printf("port %i: in %s, out %s\n", i + 1, bitrates[ports[2 * i + 0]], bitrates[ports[2 * i + 1]]); + printf("port %i: in %s, out %s\n", i + 1, safeStr(getBitrateStr(ports[2 * i + 0])), safeStr(getBitrateStr(ports[2 * i + 1]))); end: free(ports); diff --git a/cli/src/com_ports.c b/cli/src/com_ports.c index 6cf0c81..fc49772 100644 --- a/cli/src/com_ports.c +++ b/cli/src/com_ports.c @@ -30,39 +30,8 @@ int do_ports_state (int argc, const char **argv UNUSED, struct ngadmin *nga) goto end; } - for (i = 0; i < sa->ports; i++) { - printf("port %i: ", i + 1); - switch (ports[i]) { - - case 0: - printf("down"); - break; - - case SPEED_10_HD: - printf("up, 10M half-duplex"); - break; - - case SPEED_10_FD: - printf("up, 10M full-duplex"); - break; - - case SPEED_100_HD: - printf("up, 100M half-duplex"); - break; - - case SPEED_100_FD: - printf("up, 100M full-duplex"); - break; - - case SPEED_1000_FD: - printf("up, 1000M full-duplex"); - break; - - default: - printf("unknown (%i)", ports[i]); - } - putchar('\n'); - } + for (i = 0; i < sa->ports; i++) + printf("port %i: %s\n", i + 1, safeStr(getSpeedStr(ports[i]))); end: free(ports); diff --git a/cli/src/com_qos.c b/cli/src/com_qos.c index 7b51bd2..f659835 100644 --- a/cli/src/com_qos.c +++ b/cli/src/com_qos.c @@ -63,7 +63,7 @@ int do_qos_set (int argc, const char **argv, struct ngadmin *nga) /* read defaults */ if (strcmp(argv[0], "all") == 0) { - d = parsePrio(argv[1]); + d = parseQosPrioStr(argv[1]); argv += 2; argc -= 2; } @@ -77,7 +77,7 @@ int do_qos_set (int argc, const char **argv, struct ngadmin *nga) p = strtol(argv[i], NULL, 0); if (p < 1 || p > sa->ports) continue; - ports[p - 1] = parsePrio(argv[i + 1]); + ports[p - 1] = parseQosPrioStr(argv[i + 1]); } /* send the new configuration to the switch */ @@ -118,21 +118,9 @@ int do_qos_show (int argc, const char **argv UNUSED, struct ngadmin *nga) goto end; } - printf("QoS mode: "); - switch (s) { - - case QOS_DOT: - printf("802.1p\n"); - goto end; - - case QOS_PORT: - printf("port based\n"); - break; - - default: - printf("unknown (%i)\n", s); + printf("QoS mode: %s\n", safeStr(getQosTypeStr(s))); + if (s != QOS_PORT) goto end; - } ports = malloc(sa->ports * sizeof(char)); i = ngadmin_getQOSValues(nga, ports); @@ -143,7 +131,7 @@ int do_qos_show (int argc, const char **argv UNUSED, struct ngadmin *nga) } for (i = 0; i < sa->ports; i++) - printf("port %i: %s\n", i + 1, prio[(int)ports[i]]); + printf("port %i: %s\n", i + 1, safeStr(getQosPrioStr(ports[i]))); end: free(ports); diff --git a/cli/src/com_stormfilter.c b/cli/src/com_stormfilter.c index dbd91f8..81ff11e 100644 --- a/cli/src/com_stormfilter.c +++ b/cli/src/com_stormfilter.c @@ -75,7 +75,7 @@ int do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga) /* read defaults */ if (strcmp(argv[0], "all") == 0) { - d = parseBitrate(argv[1]); + d = parseBitrateStr(argv[1]); argv += 2; argc -= 2; } @@ -89,7 +89,7 @@ int do_stormfilter_set (int argc, const char **argv, struct ngadmin *nga) p = strtol(argv[i], NULL, 0); if (p < 1 || p > sa->ports) continue; - ports[p - 1] = parseBitrate(argv[i + 1]); + ports[p - 1] = parseBitrateStr(argv[i + 1]); } /* send the new configuration to the switch */ @@ -145,7 +145,7 @@ int do_stormfilter_show (int argc, const char **argv UNUSED, struct ngadmin *nga } for (i = 0; i < sa->ports; i++) - printf("port %i: %s\n", i + 1, bitrates[ports[i]]); + printf("port %i: %s\n", i + 1, safeStr(getBitrateStr(ports[i]))); end: free(ports); diff --git a/cli/src/com_vlan.c b/cli/src/com_vlan.c index 0baa1d5..cad3334 100644 --- a/cli/src/com_vlan.c +++ b/cli/src/com_vlan.c @@ -391,32 +391,7 @@ int do_vlan_mode_show (int argc, const char **argv UNUSED, struct ngadmin *nga) goto end; } - printf("VLAN type: "); - switch (t) { - - case VLAN_DISABLED: - printf("disabled\n"); - break; - - case VLAN_PORT_BASIC: - printf("port basic\n"); - break; - - case VLAN_PORT_ADV: - printf("port advanced\n"); - break; - - case VLAN_DOT_BASIC: - printf("802.1Q basic\n"); - break; - - case VLAN_DOT_ADV: - printf("802.1Q advanced\n"); - break; - - default: - printf("unknown (%i)\n", t); - } + printf("VLAN type: %s\n", safeStr(getVlanTypeStr(t))); end: diff --git a/cli/src/common.c b/cli/src/common.c index 6fe12ed..28a50d0 100644 --- a/cli/src/common.c +++ b/cli/src/common.c @@ -4,104 +4,14 @@ void printErrCode (int err) { - switch (err) { - case ERR_OK: - break; + const char *str; - case ERR_NET: - printf("network error\n"); - break; + str = ngadmin_errorStr(err); - case ERR_NOTLOG: - printf("no switch selected\n"); - break; - - case ERR_DENIED: - printf("access denied\n"); - break; - - case ERR_BADPASS: - printf("wrong password\n"); - break; - - case ERR_BADID: - printf("bad switch id\n"); - break; - - case ERR_INVARG: - printf("invalid argument\n"); - break; - - case ERR_TIMEOUT: - printf("timeout\n"); - break; - - case ERR_NOTIMPL: - printf("not implemented\n"); - break; - - case ERR_BADREPLY: - printf("bad reply from switch\n"); - break; - - case ERR_INVOP: - printf("invalid operation\n"); - break; - - case ERR_UNKNOWN: - printf("unknown error\n"); - break; - - default: + if (str == NULL) printf("unknown status code (%i)\n", err); - } -} - - -const char* const bitrates[] = { - "nl", - "512K", - "1M", - "2M", - "4M", - "8M", - "16M", - "32M", - "64M", - "128M", - "256M", - "512M", - NULL -}; - - -const char* const prio[]={ - NULL, - "high", - "medium", - "normal", - "low", - NULL -}; - - -int parseBitrate (const char *s) -{ - int i; - - for (i = 0; bitrates[i] != NULL && strcasecmp(bitrates[i], s) != 0; i++); - - return i; -} - - -char parsePrio (const char *s) -{ - int i; - - for (i = 1; prio[i] != NULL && strcasecmp(prio[i], s) != 0; i++); - - return (char)i; + else if (err != ERR_OK) + puts(str); } @@ -123,20 +33,6 @@ void displaySwitchTab (const struct swi_attr *sa, int nb) } -int trim (char *txt, int start) -{ - char *p; - - if (txt == NULL) - return 0; - - p = txt + start; - for (p--; p >= txt && (*p == ' ' || *p == '\n'); *p-- = 0); - - return p - txt + 1; -} - - int explode (const char *commande, char** tab, int maximum) { const char *start, *end; diff --git a/cli/src/common.h b/cli/src/common.h index e50ae1e..a8e614e 100644 --- a/cli/src/common.h +++ b/cli/src/common.h @@ -9,6 +9,7 @@ #include #include +#include #include @@ -20,16 +21,8 @@ extern int main_loop_continue; extern struct termios current_term; -extern const char * const bitrates[], * const prio[]; - - void displaySwitchTab (const struct swi_attr *sa, int nb); void printErrCode (int err); -int parseBitrate (const char *s); -char parsePrio (const char *s); - - -int trim (char *txt, int start); int explode (const char *commande, char** tab, int maximum); diff --git a/emu/src/emu.c b/emu/src/emu.c index ecf8d6f..11ae134 100644 --- a/emu/src/emu.c +++ b/emu/src/emu.c @@ -14,7 +14,7 @@ #include #include #include -#include +#include #define MAX_STR_SIZE 64 diff --git a/lib/include/ngadmin.h b/lib/include/ngadmin.h index 7f8a504..50fffa9 100644 --- a/lib/include/ngadmin.h +++ b/lib/include/ngadmin.h @@ -246,6 +246,15 @@ extern "C" { struct ngadmin* ngadmin_init (const char *iface); +/** + * Convert error to string. + * This function returns a string corresponding to the numerical error code. + * @param error The numerical error code to convert. + * @return A pointer to a static string or NULL if the error code is invalid. + */ +const char* ngadmin_errorStr (int error); + + /** * Close NgAdmin library. * This function frees the resources used by the library. You really should diff --git a/lib/src/misc.c b/lib/src/misc.c index 39abde3..5baee9e 100644 --- a/lib/src/misc.c +++ b/lib/src/misc.c @@ -3,12 +3,36 @@ #include #include -#include +#include #include "lib.h" #include "network.h" +static const char* const liberror_str_tab[] = { + [-ERR_OK] = "no error", + [-ERR_NET] = "network error", + [-ERR_NOTLOG] "not logged", + [-ERR_DENIED] = "access denied", + [-ERR_BADPASS] = "bad password", + [-ERR_BADID] = "bad id", + [-ERR_INVARG] = "invalid argument", + [-ERR_TIMEOUT] = "timeout", + [-ERR_MEM] = "out of memory", + [-ERR_NOTIMPL] = "not implemented", + [-ERR_BADREPLY] = "bad reply", + [-ERR_INVOP] = "invalid operation", + [-ERR_UNKNOWN] = "unknown error", + NULL +}; + + +const char* ngadmin_errorStr (int error) +{ + return getValueStr(liberror_str_tab, -ERR_OK, -ERR_UNKNOWN, -error); +} + + int ngadmin_setName (struct ngadmin *nga, const char *name) { List *attr; diff --git a/lib/src/network.c b/lib/src/network.c index be91126..f53b475 100644 --- a/lib/src/network.c +++ b/lib/src/network.c @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/raw/include/nsdp/Makefile.am b/raw/include/nsdp/Makefile.am index 5a903a8..56d4b77 100644 --- a/raw/include/nsdp/Makefile.am +++ b/raw/include/nsdp/Makefile.am @@ -1,3 +1,3 @@ -noinst_HEADERS = attr.h list.h misc.h net.h packet.h protocol.h +noinst_HEADERS = attr.h list.h net.h packet.h protocol.h str.h diff --git a/raw/include/nsdp/misc.h b/raw/include/nsdp/misc.h deleted file mode 100644 index 6aaa412..0000000 --- a/raw/include/nsdp/misc.h +++ /dev/null @@ -1,19 +0,0 @@ - -#ifndef DEF_MISC -#define DEF_MISC - - -void passwordEndecode (char *buf, unsigned int len); - - -int trim (char *txt, int start); - - -static inline int min (int a, int b) -{ - return a < b ? a : b; -} - - -#endif - diff --git a/raw/include/nsdp/protocol.h b/raw/include/nsdp/protocol.h index 5ab83c0..def40c4 100644 --- a/raw/include/nsdp/protocol.h +++ b/raw/include/nsdp/protocol.h @@ -17,6 +17,7 @@ #define CODE_WRITE_REQ 3 #define CODE_WRITE_REP 4 +#define ERROR_NONE 0 #define ERROR_READONLY 3 #define ERROR_WRITEONLY 4 #define ERROR_INVALID_VALUE 5 diff --git a/raw/include/nsdp/str.h b/raw/include/nsdp/str.h new file mode 100644 index 0000000..d9a0a28 --- /dev/null +++ b/raw/include/nsdp/str.h @@ -0,0 +1,73 @@ + +#ifndef DEF_STR +#define DEF_STR + + +#include +#include /* FIXME */ + + +extern const char passwordKey[]; + +extern const char* const speed_str_tab[]; +extern const char* const vlan_type_str_tab[]; +extern const char* const vlan_code_str_tab[]; +extern const char* const qos_type_str_tab[]; +extern const char* const qos_prio_str_tab[]; +extern const char* const bitrate_str_tab[]; +extern const char* const code_str_tab[]; +extern const char* const error_str_tab[]; + + +static inline const char* safeStr (const char *s) +{ + return (s == NULL) ? "unknown" : s; +} + + +static inline const char* getValueStr (const char* const* tab, unsigned char mini, unsigned char maxi, unsigned char value) +{ + return (value >= mini && value <= maxi) ? tab[value] : NULL; +} + + +#define getSpeedStr(speed) getValueStr(speed_str_tab, SPEED_DOWN, SPEED_1000_FD, speed) +#define getVlanTypeStr(type) getValueStr(vlan_type_str_tab, VLAN_DISABLED, VLAN_DOT_ADV, type) +#define getVlanCodeStr(code) getValueStr(vlan_code_str_tab, VLAN_NO, VLAN_TAGGED, code) +#define getQosTypeStr(type) getValueStr(qos_type_str_tab, QOS_PORT, QOS_DOT, type) +#define getQosPrioStr(prio) getValueStr(qos_prio_str_tab, PRIO_HIGH, PRIO_LOW, prio) +#define getBitrateStr(bitrate) getValueStr(bitrate_str_tab, BITRATE_NOLIMIT, BITRATE_512M, bitrate) +#define getCodeStr(code) getValueStr(code_str_tab, CODE_READ_REQ, CODE_WRITE_REP, code) +#define getErrorStr(error) getValueStr(error_str_tab, ERROR_NONE, ERROR_DENIED, error) + + +int parseValueStr (const char* const* tab, unsigned char mini, unsigned char maxi, const char *str); + + +#define parseSpeedStr(str) parseValueStr(speed_str_tab, SPEED_DOWN, SPEED_1000_FD, str) +#define parseVlanTypeStr(str) parseValueStr(vlan_type_str_tab, VLAN_DISABLED, VLAN_DOT_ADV, str) +#define parseVlanCodeStr(str) parseValueStr(vlan_code_str_tab, VLAN_NO, VLAN_TAGGED, str) +#define parseQosTypeStr(str) parseValueStr(qos_type_str_tab, QOS_PORT, QOS_DOT, str) +#define parseQosPrioStr(str) parseValueStr(qos_prio_str_tab, PRIO_HIGH, PRIO_LOW, str) +#define parseBitrateStr(str) parseValueStr(bitrate_str_tab, BITRATE_NOLIMIT, BITRATE_512M, str) +#define parseCodeStr(str) parseValueStr(code_str_tab, CODE_READ_REQ, CODE_WRITE_REP, str) +#define parseErrorStr(str) parseValueStr(error_str_tab, ERROR_NONE, ERROR_DENIED, str) + + +bool isStringPrintable (const char *str, unsigned int len); + + +void passwordEndecode (char *buf, unsigned int len); + + +int trim (char *txt, int start); + + +static inline int min (int a, int b) +{ + return a < b ? a : b; +} + + +#endif + diff --git a/raw/src/Makefile.am b/raw/src/Makefile.am index f0c2a91..641897e 100644 --- a/raw/src/Makefile.am +++ b/raw/src/Makefile.am @@ -1,7 +1,7 @@ noinst_LTLIBRARIES = librawnsdp.la -librawnsdp_la_SOURCES = attr.c list.c misc.c net.c packet.c +librawnsdp_la_SOURCES = attr.c list.c net.c packet.c str.c librawnsdp_la_CPPFLAGS = -I$(top_srcdir)/raw/include/ -I$(top_srcdir)/lib/include/ librawnsdp_la_CFLAGS = -fno-strict-aliasing librawnsdp_la_LIBADD = $(RT_LIBS) diff --git a/raw/src/misc.c b/raw/src/misc.c deleted file mode 100644 index 2ccec49..0000000 --- a/raw/src/misc.c +++ /dev/null @@ -1,42 +0,0 @@ - -#include - -#include - - -static const char passwordKey[] = "NtgrSmartSwitchRock"; - - -void passwordEndecode (char *buf, unsigned int len) -{ - const char *k = passwordKey; - unsigned int i; - - if (buf == NULL || len <= 0) - return; - - for (i = 0; i < len; i++) { - if (*k == '\0') - k = passwordKey; - buf[i] ^= *k++; - } -} - - -int trim (char *txt, int start) -{ - char *p; - - if (txt == NULL) - return 0; - - p = txt + start; - while (p >= txt && (*p == ' ' || *p == '\n')) { - *p = '\0'; - p--; - } - - return p - txt + 1; -} - - diff --git a/raw/src/str.c b/raw/src/str.c new file mode 100644 index 0000000..1ca2c44 --- /dev/null +++ b/raw/src/str.c @@ -0,0 +1,146 @@ + +#include +#include + +#include + + +const char passwordKey[] = "NtgrSmartSwitchRock"; + + +const char* const speed_str_tab[] = { + [SPEED_DOWN] = "down", + [SPEED_10_HD] = "10M half-duplex", + [SPEED_10_FD] = "10M full-duplex", + [SPEED_100_HD] = "100M half-duplex", + [SPEED_100_FD] = "100M full-duplex", + [SPEED_1000_FD] = "1000M full-duplex", + NULL +}; + + +const char* const vlan_type_str_tab[] = { + [VLAN_DISABLED] = "disabled", + [VLAN_PORT_BASIC] = "basic port based", + [VLAN_PORT_ADV] = "advanced port based", + [VLAN_DOT_BASIC] = "basic 802.1Q", + [VLAN_DOT_ADV] = "advanced 802.1Q", + NULL +}; + + +const char* const vlan_code_str_tab[] = { + [VLAN_NO] = "no", + [VLAN_UNTAGGED] = "untagged", + [VLAN_TAGGED] = "tagged", + NULL +}; + + +const char* const qos_type_str_tab[] = { + [QOS_PORT] = "port", + [QOS_DOT] = "802.1p", + NULL +}; + + +const char* const qos_prio_str_tab[] = { + [PRIO_HIGH] = "high", + [PRIO_MED] = "medium", + [PRIO_NORM] = "normal", + [PRIO_LOW] = "low", + NULL +}; + + +const char* const bitrate_str_tab[] = { + [BITRATE_NOLIMIT] = "nl", + [BITRATE_512K] = "512K", + [BITRATE_1M] = "1M", + [BITRATE_2M] = "2M", + [BITRATE_4M] = "4M", + [BITRATE_8M] = "8M", + [BITRATE_16M] = "16M", + [BITRATE_32M] = "32M", + [BITRATE_64M] = "64M", + [BITRATE_128M] = "128M", + [BITRATE_256M] = "256M", + [BITRATE_512M] = "512M", + NULL +}; + + +const char* const code_str_tab[] = { + [CODE_READ_REQ] = "read request", + [CODE_READ_REP] = "read reply", + [CODE_WRITE_REQ] = "write request", + [CODE_WRITE_REP] = "write reply", + NULL +}; + + +const char* const error_str_tab[] = { + [ERROR_NONE] = "none", + [ERROR_READONLY] = "read only", + [ERROR_INVALID_VALUE] = "invalid value", + [ERROR_DENIED] = "access denied", + NULL +}; + + +int parseValueStr (const char* const* tab, unsigned char mini, unsigned char maxi, const char *str) +{ + unsigned char i; + + for (i = mini; i <= maxi; i++) { + if (tab[i] != NULL && strcasecmp(str, tab[i]) == 0) + return (int)i; + } + + return -1; +} + + +bool isStringPrintable (const char *str, unsigned int len) +{ + const char *p; + + for (p = str; len > 0; len--) { + if (!isprint(*p)) + return false; + } + + return true; +} + + +void passwordEndecode (char *buf, unsigned int len) +{ + const char *k = passwordKey; + unsigned int i; + + if (buf == NULL || len <= 0) + return; + + for (i = 0; i < len; i++) { + if (*k == '\0') + k = passwordKey; + buf[i] ^= *k++; + } +} + + +int trim (char *txt, int start) +{ + char *p; + + if (txt == NULL) + return 0; + + p = txt + start; + for (p--; p >= txt && (*p == ' ' || *p == '\n'); *p-- = '\0'); + + return p - txt + 1; +} + + diff --git a/spy/src/spy.c b/spy/src/spy.c index 70a3eb9..7c842cc 100644 --- a/spy/src/spy.c +++ b/spy/src/spy.c @@ -10,7 +10,7 @@ #include #include #include -#include +#include static void print_password (const char *pass, unsigned int len) @@ -19,12 +19,7 @@ static void print_password (const char *pass, unsigned int len) char *s; - for (i = 0; i < len; i++) { - if (!isprint(pass[i])) - break; - } - - if (i == len) { + if (isStringPrintable(pass, len)) { /* all characters are printable, cleartext password assumed */ printf("(clear) \"%s\"", pass); return; @@ -38,12 +33,7 @@ static void print_password (const char *pass, unsigned int len) passwordEndecode(s, len); - for (i = 0; i < len; i++) { - if (!isprint(s[i])) - break; - } - - if (i == len) { + if (isStringPrintable(s, len)) { /* all characters are printable, encrypted password assumed */ printf("(encrypted) \"%s\"", s); free(s); @@ -59,162 +49,6 @@ static void print_password (const char *pass, unsigned int len) } -static const char* port_status_str (unsigned char status) -{ - switch (status) { - - case SPEED_DOWN: - return "down"; - - case SPEED_10_HD: - return "10M half-duplex"; - - case SPEED_10_FD: - return "10M full-duplex"; - - case SPEED_100_HD: - return "100M half-duplex"; - - case SPEED_100_FD: - return "100M full-duplex"; - - case SPEED_1000_FD: - return "1000M full-duplex"; - - default: - return "unknown"; - } -} - - -static const char* vlan_type_code (unsigned char type) -{ - switch (type) { - - case VLAN_DISABLED: - return "disabled"; - - case VLAN_PORT_BASIC: - return "basic port based"; - - case VLAN_PORT_ADV: - return "advanced port based"; - - case VLAN_DOT_BASIC: - return "basic 802.1Q"; - - case VLAN_DOT_ADV: - return "advanced 802.1Q"; - - default: - return "unknown"; - } -} - - -static const char* vlan_code_str (unsigned char code) -{ - switch (code) { - - case VLAN_NO: - return "no"; - - case VLAN_UNTAGGED: - return "untagged"; - - case VLAN_TAGGED: - return "tagged"; - - default: - return "unknown"; - } -} - - -static const char* qos_type_str (unsigned char type) -{ - switch (type) { - - case QOS_PORT: - return "port"; - - case QOS_DOT: - return "802.1p"; - - default: - return "unknown"; - } -} - - -static const char* qos_prio_str (unsigned char prio) -{ - switch (prio) { - - case PRIO_HIGH: - return "high"; - - case PRIO_MED: - return "medium"; - - case PRIO_NORM: - return "normal"; - - case PRIO_LOW: - return "low"; - - default: - return "unknown"; - } -} - - -static const char* bitrate_str (unsigned int bitrate) -{ - switch (bitrate) { - - case BITRATE_NOLIMIT: - return "unlimited"; - - case BITRATE_512K: - return "512K"; - - case BITRATE_1M: - return "1M"; - - case BITRATE_2M: - return "2M"; - - case BITRATE_4M: - return "4M"; - - case BITRATE_8M: - return "8M"; - - case BITRATE_16M: - return "16M"; - - case BITRATE_32M: - return "32M"; - - case BITRATE_64M: - return "64M"; - - case BITRATE_128M: - return "128M"; - - case BITRATE_256M: - return "256M"; - - case BITRATE_512M: - return "512M"; - - default: - return "unknown"; - } -} - - static void print_attr (const struct attr *at) { unsigned char p, ports, *byte = at->data; @@ -309,7 +143,7 @@ static void print_attr (const struct attr *at) case ATTR_PORT_STATUS: printf("\tport status\n"); printf("\tport = %u\n", apsu->port); - printf("\tstate = %s\n", port_status_str(apsu->status)); + printf("\tstate = %s\n", safeStr(getSpeedStr(apsu->status))); break; case ATTR_PORT_STATISTICS: @@ -341,7 +175,7 @@ static void print_attr (const struct attr *at) break; case ATTR_VLAN_TYPE: - printf("\tVLAN type = %s\n", vlan_type_code(*byte)); + printf("\tVLAN type = %s\n", safeStr(getVlanTypeStr(*byte))); break; case ATTR_VLAN_PORT_CONF: @@ -349,7 +183,7 @@ static void print_attr (const struct attr *at) printf("\tVLAN = %u\n", avc->vlan); ports = at->size - sizeof(struct attr_vlan_conf); for (p = 0; p < ports; p++) - printf("\tport %d = %s\n", p + 1, vlan_code_str(avc->ports[p])); + printf("\tport %d = %s\n", p + 1, safeStr(getVlanCodeStr(avc->ports[p]))); break; case ATTR_VLAN_DOT_CONF: @@ -357,7 +191,7 @@ static void print_attr (const struct attr *at) printf("\tVLAN = %u\n", avc->vlan); ports = at->size - sizeof(struct attr_vlan_conf); for (p = 0; p < ports; p++) - printf("\tport %d = %s\n", p + 1, vlan_code_str(avc->ports[p])); + printf("\tport %d = %s\n", p + 1, safeStr(getVlanCodeStr(avc->ports[p]))); break; case ATTR_VLAN_DESTROY: @@ -371,25 +205,25 @@ static void print_attr (const struct attr *at) break; case ATTR_QOS_TYPE: - printf("\tQoS type = %s\n", qos_type_str(*byte)); + printf("\tQoS type = %s\n", safeStr(getQosTypeStr(*byte))); break; case ATTR_QOS_CONFIG: printf("\tQoS configuration\n"); printf("\tport = %u\n", aq->port); - printf("\tpriority = %s\n", qos_prio_str(aq->prio)); + printf("\tpriority = %s\n", safeStr(getQosPrioStr(aq->prio))); break; case ATTR_BITRATE_INPUT: printf("\tinput bitrate\n"); printf("\tport = %u\n", ab->port); - printf("\tbitrate = %s\n", bitrate_str(ab->bitrate)); + printf("\tbitrate = %s\n", safeStr(getBitrateStr(ab->bitrate))); break; case ATTR_BITRATE_OUTPUT: printf("\toutput bitrate\n"); printf("\tport = %u\n", ab->port); - printf("\tbitrate = %s\n", bitrate_str(ab->bitrate)); + printf("\tbitrate = %s\n", safeStr(getBitrateStr(ab->bitrate))); break; case ATTR_STORM_ENABLE: @@ -399,7 +233,7 @@ static void print_attr (const struct attr *at) case ATTR_STORM_BITRATE: printf("\tstorm filtering bitrate\n"); printf("\tport = %u\n", ab->port); - printf("\tbitrate = %s\n", bitrate_str(ab->bitrate)); + printf("\tbitrate = %s\n", safeStr(getBitrateStr(ab->bitrate))); break; case ATTR_MIRROR: @@ -441,50 +275,6 @@ static void print_attr (const struct attr *at) } -static const char* code_str (unsigned char code) -{ - switch (code) { - - case CODE_READ_REQ: - return "read request"; - - case CODE_READ_REP: - return "read reply"; - - case CODE_WRITE_REQ: - return "write request"; - - case CODE_WRITE_REP: - return "write reply"; - - default: - return "unknown"; - } -} - - -static const char* error_str (unsigned char err) -{ - switch (err) { - - case 0: - return "none"; - - case ERROR_READONLY: - return "read only"; - - case ERROR_INVALID_VALUE: - return "invalid value"; - - case ERROR_DENIED: - return "access denied"; - - default: - return "unknown"; - } -} - - static void print_packet (const List *attr, const struct nsdp_cmd *nc) { const ListNode *ln; @@ -492,8 +282,8 @@ static void print_packet (const List *attr, const struct nsdp_cmd *nc) printf("---------------------------------\n"); - printf("code = %s (%u)\n", code_str(nc->code), nc->code); - printf("error = %s (%u)\n", error_str(nc->error), nc->error); + printf("code = %s (%u)\n", safeStr(getCodeStr(nc->code)), nc->code); + printf("error = %s (%u)\n", safeStr(getErrorStr(nc->error)), nc->error); if (nc->attr_error != 0) printf("erroneous attribute = %04X\n", nc->attr_error); printf("source address = %s:%u\n", inet_ntoa(nc->remote_addr.sin_addr), ntohs(nc->remote_addr.sin_port)); -- 2.39.5