@@ -9,6 +9,7 @@ const char *rtnl_rtscope_n2a(int id, char *buf, int len);
const char *rtnl_rttable_n2a(__u32 id, char *buf, int len);
const char *rtnl_rtrealm_n2a(int id, char *buf, int len);
const char *rtnl_dsfield_n2a(int id, char *buf, int len);
+const char *rtnl_dsfield_get_name(int id);
const char *rtnl_group_n2a(int id, char *buf, int len);
int rtnl_rtprot_a2n(__u32 *id, const char *arg);
@@ -479,18 +479,30 @@ static void rtnl_rtdsfield_initialize(void)
const char *rtnl_dsfield_n2a(int id, char *buf, int len)
{
+ const char *name;
+
if (id < 0 || id >= 256) {
snprintf(buf, len, "%d", id);
return buf;
}
+ if (!numeric) {
+ name = rtnl_dsfield_get_name(id);
+ if (name != NULL)
+ return name;
+ }
+ snprintf(buf, len, "0x%02x", id);
+ return buf;
+}
+
+const char *rtnl_dsfield_get_name(int id)
+{
+ if (id < 0 || id >= 256)
+ return NULL;
if (!rtnl_rtdsfield_tab[id]) {
if (!rtnl_rtdsfield_init)
rtnl_rtdsfield_initialize();
}
- if (!numeric && rtnl_rtdsfield_tab[id])
- return rtnl_rtdsfield_tab[id];
- snprintf(buf, len, "0x%02x", id);
- return buf;
+ return rtnl_rtdsfield_tab[id];
}
For formatting DSCP (not full dsfield), it would be handy to be able to just get the name from the name table, and not get any of the remaining cruft related to formatting. Add a new entry point to just fetch the name table string uninterpreted. Use it from rtnl_dsfield_n2a(). Signed-off-by: Petr Machata <me@pmachata.org> --- include/rt_names.h | 1 + lib/rt_names.c | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-)