From: Ben Pfaff Date: Wed, 17 Dec 2008 00:36:25 +0000 (-0800) Subject: New function ofp_message_type_to_string(). X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=532bf7a07cea9c9a3ad6e34fdd5275c96944dd48;p=openvswitch New function ofp_message_type_to_string(). --- diff --git a/lib/ofp-print.c b/lib/ofp-print.c index 41aed2fd..07f5b67c 100644 --- a/lib/ofp-print.c +++ b/lib/ofp-print.c @@ -1458,6 +1458,33 @@ ofp_to_string(const void *oh_, size_t len, int verbosity) } return ds_cstr(&string); } + +/* Returns the name for the specified OpenFlow message type as a string, + * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a + * hex number, e.g. "0x55". + * + * The caller must free the returned string when it is no longer needed. */ +char * +ofp_message_type_to_string(uint8_t type) +{ + struct ds s = DS_EMPTY_INITIALIZER; + const struct openflow_packet *pkt; + for (pkt = packets; ; pkt++) { + if (pkt >= &packets[ARRAY_SIZE(packets)]) { + ds_put_format(&s, "0x%02"PRIx8, type); + break; + } else if (type == pkt->type) { + const char *p; + + ds_put_cstr(&s, "OFPT_"); + for (p = pkt->name; *p; p++) { + ds_put_char(&s, toupper((unsigned char) *p)); + } + break; + } + } + return ds_cstr(&s); +} static void print_and_free(FILE *stream, char *string) diff --git a/lib/ofp-print.h b/lib/ofp-print.h index b71212cd..0d7e06b6 100644 --- a/lib/ofp-print.h +++ b/lib/ofp-print.h @@ -36,6 +36,7 @@ #ifndef OFP_PRINT_H #define OFP_PRINT_H 1 +#include #include struct ofp_flow_mod; @@ -49,6 +50,7 @@ void ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_l char *ofp_to_string(const void *, size_t, int verbosity); char *ofp_packet_to_string(const void *data, size_t len, size_t total_len); +char *ofp_message_type_to_string(uint8_t type); #ifdef __cplusplus }