mirror of
https://git.proxmox.com/git/mirror_iproute2
synced 2025-12-07 21:38:53 +00:00
utils: Reimplement ll_idx_n2a() and introduce ll_idx_a2n()
Now all users of ll_idx_n2a() replaced with ll_index_to_name() we can move it's functionality to ll_index_to_name() and implement index to name conversion using snprintf() and "if%u". Use %u specifier in "if%..." template consistently: network device indexes are always greather than zero. Also introduce ll_idx_n2a() conterpart: ll_idx_a2n() that is used to translate name of the "if%u" form to index using sscanf(). Signed-off-by: Serhey Popovych <serhe.popovych@gmail.com> Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
parent
d92cc2d087
commit
fe269b6e7c
@ -8,9 +8,11 @@ int ll_remember_index(const struct sockaddr_nl *who,
|
||||
void ll_init_map(struct rtnl_handle *rth);
|
||||
unsigned ll_name_to_index(const char *name);
|
||||
const char *ll_index_to_name(unsigned idx);
|
||||
const char *ll_idx_n2a(unsigned idx, char *buf);
|
||||
int ll_index_to_type(unsigned idx);
|
||||
int ll_index_to_flags(unsigned idx);
|
||||
unsigned namehash(const char *str);
|
||||
|
||||
const char *ll_idx_n2a(unsigned int idx);
|
||||
unsigned int ll_idx_a2n(const char *name);
|
||||
|
||||
#endif /* __LL_MAP_H__ */
|
||||
|
||||
31
lib/ll_map.c
31
lib/ll_map.c
@ -136,8 +136,26 @@ int ll_remember_index(const struct sockaddr_nl *who,
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *ll_idx_n2a(unsigned idx, char *buf)
|
||||
const char *ll_idx_n2a(unsigned int idx)
|
||||
{
|
||||
static char buf[IFNAMSIZ];
|
||||
|
||||
snprintf(buf, sizeof(buf), "if%u", idx);
|
||||
return buf;
|
||||
}
|
||||
|
||||
unsigned int ll_idx_a2n(const char *name)
|
||||
{
|
||||
unsigned int idx;
|
||||
|
||||
if (sscanf(name, "if%u", &idx) != 1)
|
||||
return 0;
|
||||
return idx;
|
||||
}
|
||||
|
||||
const char *ll_index_to_name(unsigned int idx)
|
||||
{
|
||||
static char buf[IFNAMSIZ];
|
||||
const struct ll_cache *im;
|
||||
|
||||
if (idx == 0)
|
||||
@ -148,18 +166,11 @@ const char *ll_idx_n2a(unsigned idx, char *buf)
|
||||
return im->name;
|
||||
|
||||
if (if_indextoname(idx, buf) == NULL)
|
||||
snprintf(buf, IFNAMSIZ, "if%d", idx);
|
||||
snprintf(buf, IFNAMSIZ, "if%u", idx);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
const char *ll_index_to_name(unsigned idx)
|
||||
{
|
||||
static char nbuf[IFNAMSIZ];
|
||||
|
||||
return ll_idx_n2a(idx, nbuf);
|
||||
}
|
||||
|
||||
int ll_index_to_type(unsigned idx)
|
||||
{
|
||||
const struct ll_cache *im;
|
||||
@ -196,7 +207,7 @@ unsigned ll_name_to_index(const char *name)
|
||||
|
||||
idx = if_nametoindex(name);
|
||||
if (idx == 0)
|
||||
sscanf(name, "if%u", &idx);
|
||||
idx = ll_idx_a2n(name);
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user