libmnl  1.0.4
genl-family-get.c
1 /* This example is placed in the public domain. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <time.h>
6 
7 #include <libmnl/libmnl.h>
8 #include <linux/genetlink.h>
9 
10 static int parse_mc_grps_cb(const struct nlattr *attr, void *data)
11 {
12  const struct nlattr **tb = data;
13  int type = mnl_attr_get_type(attr);
14 
15  /* skip unsupported attribute in user-space */
16  if (mnl_attr_type_valid(attr, CTRL_ATTR_MCAST_GRP_MAX) < 0)
17  return MNL_CB_OK;
18 
19  switch(type) {
20  case CTRL_ATTR_MCAST_GRP_ID:
21  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
22  perror("mnl_attr_validate");
23  return MNL_CB_ERROR;
24  }
25  break;
26  case CTRL_ATTR_MCAST_GRP_NAME:
27  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
28  perror("mnl_attr_validate");
29  return MNL_CB_ERROR;
30  }
31  break;
32  }
33  tb[type] = attr;
34  return MNL_CB_OK;
35 }
36 
37 static void parse_genl_mc_grps(struct nlattr *nested)
38 {
39  struct nlattr *pos;
40 
41  mnl_attr_for_each_nested(pos, nested) {
42  struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1] = {};
43 
44  mnl_attr_parse_nested(pos, parse_mc_grps_cb, tb);
45  if (tb[CTRL_ATTR_MCAST_GRP_ID]) {
46  printf("id-0x%x ",
47  mnl_attr_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]));
48  }
49  if (tb[CTRL_ATTR_MCAST_GRP_NAME]) {
50  printf("name: %s ",
51  mnl_attr_get_str(tb[CTRL_ATTR_MCAST_GRP_NAME]));
52  }
53  printf("\n");
54  }
55 }
56 
57 static int parse_family_ops_cb(const struct nlattr *attr, void *data)
58 {
59  const struct nlattr **tb = data;
60  int type = mnl_attr_get_type(attr);
61 
62  if (mnl_attr_type_valid(attr, CTRL_ATTR_OP_MAX) < 0)
63  return MNL_CB_OK;
64 
65  switch(type) {
66  case CTRL_ATTR_OP_ID:
67  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
68  perror("mnl_attr_validate");
69  return MNL_CB_ERROR;
70  }
71  break;
72  case CTRL_ATTR_OP_MAX:
73  break;
74  default:
75  return MNL_CB_OK;
76  }
77  tb[type] = attr;
78  return MNL_CB_OK;
79 }
80 
81 static void parse_genl_family_ops(struct nlattr *nested)
82 {
83  struct nlattr *pos;
84 
85  mnl_attr_for_each_nested(pos, nested) {
86  struct nlattr *tb[CTRL_ATTR_OP_MAX+1] = {};
87 
88  mnl_attr_parse_nested(pos, parse_family_ops_cb, tb);
89  if (tb[CTRL_ATTR_OP_ID]) {
90  printf("id-0x%x ",
91  mnl_attr_get_u32(tb[CTRL_ATTR_OP_ID]));
92  }
93  if (tb[CTRL_ATTR_OP_MAX]) {
94  printf("flags ");
95  }
96  printf("\n");
97  }
98 }
99 
100 static int data_attr_cb(const struct nlattr *attr, void *data)
101 {
102  const struct nlattr **tb = data;
103  int type = mnl_attr_get_type(attr);
104 
105  if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
106  return MNL_CB_OK;
107 
108  switch(type) {
109  case CTRL_ATTR_FAMILY_NAME:
110  if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
111  perror("mnl_attr_validate");
112  return MNL_CB_ERROR;
113  }
114  break;
115  case CTRL_ATTR_FAMILY_ID:
116  if (mnl_attr_validate(attr, MNL_TYPE_U16) < 0) {
117  perror("mnl_attr_validate");
118  return MNL_CB_ERROR;
119  }
120  break;
121  case CTRL_ATTR_VERSION:
122  case CTRL_ATTR_HDRSIZE:
123  case CTRL_ATTR_MAXATTR:
124  if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
125  perror("mnl_attr_validate");
126  return MNL_CB_ERROR;
127  }
128  break;
129  case CTRL_ATTR_OPS:
130  case CTRL_ATTR_MCAST_GROUPS:
131  if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
132  perror("mnl_attr_validate");
133  return MNL_CB_ERROR;
134  }
135  break;
136  }
137  tb[type] = attr;
138  return MNL_CB_OK;
139 }
140 
141 static int data_cb(const struct nlmsghdr *nlh, void *data)
142 {
143  struct nlattr *tb[CTRL_ATTR_MAX+1] = {};
144  struct genlmsghdr *genl = mnl_nlmsg_get_payload(nlh);
145 
146  mnl_attr_parse(nlh, sizeof(*genl), data_attr_cb, tb);
147  if (tb[CTRL_ATTR_FAMILY_NAME]) {
148  printf("name=%s\t",
149  mnl_attr_get_str(tb[CTRL_ATTR_FAMILY_NAME]));
150  }
151  if (tb[CTRL_ATTR_FAMILY_ID]) {
152  printf("id=%u\t",
153  mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]));
154  }
155  if (tb[CTRL_ATTR_VERSION]) {
156  printf("version=%u\t",
157  mnl_attr_get_u32(tb[CTRL_ATTR_VERSION]));
158  }
159  if (tb[CTRL_ATTR_HDRSIZE]) {
160  printf("hdrsize=%u\t",
161  mnl_attr_get_u32(tb[CTRL_ATTR_HDRSIZE]));
162  }
163  if (tb[CTRL_ATTR_MAXATTR]) {
164  printf("maxattr=%u\t",
165  mnl_attr_get_u32(tb[CTRL_ATTR_MAXATTR]));
166  }
167  printf("\n");
168  if (tb[CTRL_ATTR_OPS]) {
169  printf("ops:\n");
170  parse_genl_family_ops(tb[CTRL_ATTR_OPS]);
171  }
172  if (tb[CTRL_ATTR_MCAST_GROUPS]) {
173  printf("grps:\n");
174  parse_genl_mc_grps(tb[CTRL_ATTR_MCAST_GROUPS]);
175  }
176  printf("\n");
177  return MNL_CB_OK;
178 }
179 
180 int main(int argc, char *argv[])
181 {
182  struct mnl_socket *nl;
183  char buf[MNL_SOCKET_BUFFER_SIZE];
184  struct nlmsghdr *nlh;
185  struct genlmsghdr *genl;
186  int ret;
187  unsigned int seq, portid;
188 
189  if (argc > 2) {
190  printf("%s [family name]\n", argv[0]);
191  exit(EXIT_FAILURE);
192  }
193 
194  nlh = mnl_nlmsg_put_header(buf);
195  nlh->nlmsg_type = GENL_ID_CTRL;
196  nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
197  nlh->nlmsg_seq = seq = time(NULL);
198 
199  genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
200  genl->cmd = CTRL_CMD_GETFAMILY;
201  genl->version = 1;
202 
203  mnl_attr_put_u32(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
204  if (argc >= 2)
205  mnl_attr_put_strz(nlh, CTRL_ATTR_FAMILY_NAME, argv[1]);
206  else
207  nlh->nlmsg_flags |= NLM_F_DUMP;
208 
209  nl = mnl_socket_open(NETLINK_GENERIC);
210  if (nl == NULL) {
211  perror("mnl_socket_open");
212  exit(EXIT_FAILURE);
213  }
214 
215  if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
216  perror("mnl_socket_bind");
217  exit(EXIT_FAILURE);
218  }
219  portid = mnl_socket_get_portid(nl);
220 
221  if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
222  perror("mnl_socket_sendto");
223  exit(EXIT_FAILURE);
224  }
225 
226  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
227  while (ret > 0) {
228  ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
229  if (ret <= 0)
230  break;
231  ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
232  }
233  if (ret == -1) {
234  perror("error");
235  exit(EXIT_FAILURE);
236  }
237 
238  mnl_socket_close(nl);
239 
240  return 0;
241 }