huangcm
2024-12-18 9d29be7f7249789d6ffd0440067187a9f040c2cd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * nl-list-caches.c    List registered cache types
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation version 2.1
 *    of the License.
 *
 * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
 */
 
#include <netlink-private/netlink.h>
#include <netlink/cli/utils.h>
 
static void print_usage(void)
{
   fprintf(stderr, "Usage: nl-list-caches\n");
   exit(1);
}
 
static char *id_attr_list(struct nl_object_ops *ops, char *buf, size_t len)
{
   if (ops->oo_attrs2str != NULL)
       return ops->oo_attrs2str(ops->oo_id_attrs, buf, len);
   else {
       memset(buf, 0, len);
       return buf;
   }
}
 
static void print(struct nl_cache_ops *ops, void *arg)
{
   char buf[64];
 
   printf("%s:\n" \
          "    hdrsize: %d bytes\n" \
          "    protocol: %s\n" \
          "    request-update: %s\n" \
          "    msg-parser: %s\n",
          ops->co_name, ops->co_hdrsize,
          nl_nlfamily2str(ops->co_protocol, buf, sizeof(buf)),
          ops->co_request_update ? "yes" : "no",
          ops->co_msg_parser ? "yes" : "no");
 
   if (ops->co_obj_ops) {
       struct nl_object_ops *obj_ops = ops->co_obj_ops;
       const char *dump_names[NL_DUMP_MAX+1] = {
           "brief",
           "detailed",
           "stats",
       };
       int i;
 
       printf("    cacheable object:\n" \
              "        name: %s:\n" \
              "        size: %zu bytes\n" \
              "        constructor: %s\n" \
              "        free-data: %s\n" \
              "        clone: %s\n" \
              "        compare: %s\n" \
              "        id attributes: %s\n" \
              "        dump: ",
              obj_ops->oo_name, obj_ops->oo_size,
              obj_ops->oo_constructor ? "yes" : "no",
              obj_ops->oo_free_data ? "yes" : "no",
              obj_ops->oo_clone ? "yes" : "no",
              obj_ops->oo_compare ? "yes" : "no",
              id_attr_list(obj_ops, buf, sizeof(buf)));
 
       for (i = 0; i <= NL_DUMP_MAX; i++)
           if (obj_ops->oo_dump[i])
               printf("%s%s",
               i == 0 ? "" : ", ",
               dump_names[i]);
 
       printf("\n");
   }
 
   if (ops->co_genl) {
       struct genl_ops *genl_ops = ops->co_genl;
 
       printf("    genl:\n" \
              "        name: %s\n" \
              "        user-hdr: %d\n" \
              "        id: %d\n",
              genl_ops->o_name, genl_ops->o_hdrsize, genl_ops->o_id);
 
       if (genl_ops->o_ncmds) {
           int i;
 
           printf("        cmds:\n");
 
           for (i = 0; i < genl_ops->o_ncmds; i++) {
               struct genl_cmd *cmd = &genl_ops->o_cmds[i];
 
               printf("            %s:\n"
                      "                id: %d\n" \
                      "                maxattr: %d\n" \
                      "                msg-parser: %s\n" \
                      "                attr-policy: %s\n",
                      cmd->c_name, cmd->c_id, cmd->c_maxattr,
                      cmd->c_msg_parser ? "yes" : "no",
                      cmd->c_attr_policy ? "yes" : "no");
           }
       }
   }
}
 
int main(int argc, char *argv[])
{
   if (argc > 1 && !strcasecmp(argv[1], "-h"))
       print_usage();
 
   nl_cache_ops_foreach(print, NULL);
 
   return 0;
}