]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
lib: refactor ll_proto functions
authorWojciech Drewek <wojciech.drewek@intel.com>
Fri, 29 Jul 2022 08:50:33 +0000 (10:50 +0200)
committerDavid Ahern <dsahern@kernel.org>
Fri, 29 Jul 2022 17:22:42 +0000 (11:22 -0600)
Move core logic of ll_proto_n2a and ll_proto_a2n
to utils.c and make it more generic by allowing to
pass table of protocols as argument (proto_tb).
Introduce struct proto with protocol ID and name to
allow this. This wil allow to use those functions by
other use cases.

Signed-off-by: Wojciech Drewek <wojciech.drewek@intel.com>
Acked-by: Guillaume Nault <gnault@redhat.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/utils.h
lib/ll_proto.c
lib/utils.c

index 9765fdd231df0c13bdf458d9421feb1176a48220..eeb23a64f008b5f31522783ed5d8e076c68c6fa1 100644 (file)
@@ -369,4 +369,14 @@ void inc_indent(struct indent_mem *mem);
 void dec_indent(struct indent_mem *mem);
 void print_indent(struct indent_mem *mem);
 
+struct proto {
+       int id;
+       const char *name;
+};
+
+int proto_a2n(unsigned short *id, const char *buf,
+             const struct proto *proto_tb, size_t tb_len);
+const char *proto_n2a(unsigned short id, char *buf, int len,
+                     const struct proto *proto_tb, size_t tb_len);
+
 #endif /* __UTILS_H__ */
index 342ea2eefa4cc9353ff7e22d7a188c1c8dd54e20..925e2caa05e5f4af1834799b9a0a8ac47ac22ecb 100644 (file)
 
 
 #define __PF(f,n) { ETH_P_##f, #n },
-static const struct {
-       int id;
-       const char *name;
-} llproto_names[] = {
+
+static const struct proto llproto_names[] = {
 __PF(LOOP,loop)
 __PF(PUP,pup)
 __PF(PUPAT,pupat)
@@ -90,31 +88,16 @@ __PF(TEB,teb)
 };
 #undef __PF
 
-
-const char * ll_proto_n2a(unsigned short id, char *buf, int len)
+const char *ll_proto_n2a(unsigned short id, char *buf, int len)
 {
-        int i;
+       size_t len_tb = ARRAY_SIZE(llproto_names);
 
-       id = ntohs(id);
-
-        for (i=0; !numeric && i<sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
-                 if (llproto_names[i].id == id)
-                       return llproto_names[i].name;
-       }
-        snprintf(buf, len, "[%d]", id);
-        return buf;
+       return proto_n2a(id, buf, len, llproto_names, len_tb);
 }
 
 int ll_proto_a2n(unsigned short *id, const char *buf)
 {
-        int i;
-        for (i=0; i < sizeof(llproto_names)/sizeof(llproto_names[0]); i++) {
-                 if (strcasecmp(llproto_names[i].name, buf) == 0) {
-                        *id = htons(llproto_names[i].id);
-                        return 0;
-                }
-       }
-       if (get_be16(id, buf, 0))
-               return -1;
-       return 0;
+       size_t len_tb = ARRAY_SIZE(llproto_names);
+
+       return proto_a2n(id, buf, llproto_names, len_tb);
 }
index 53d3100602848472e42a37c718285be4326bde6c..dd3cdb31239c231665aef681926f41c48ec9ed74 100644 (file)
@@ -1925,3 +1925,37 @@ void print_indent(struct indent_mem *mem)
        if (mem->indent_level)
                printf("%s", mem->indent_str);
 }
+
+const char *proto_n2a(unsigned short id, char *buf, int len,
+                     const struct proto *proto_tb, size_t tb_len)
+{
+       int i;
+
+       id = ntohs(id);
+
+       for (i = 0; !numeric && i < tb_len; i++) {
+               if (proto_tb[i].id == id)
+                       return proto_tb[i].name;
+       }
+
+       snprintf(buf, len, "[%d]", id);
+
+       return buf;
+}
+
+int proto_a2n(unsigned short *id, const char *buf,
+             const struct proto *proto_tb, size_t tb_len)
+{
+       int i;
+
+       for (i = 0; i < tb_len; i++) {
+               if (strcasecmp(proto_tb[i].name, buf) == 0) {
+                       *id = htons(proto_tb[i].id);
+                       return 0;
+               }
+       }
+       if (get_be16(id, buf, 0))
+               return -1;
+
+       return 0;
+}