8 #include <libmnl/libmnl.h>
10 #include <linux/if_link.h>
11 #include <linux/rtnetlink.h>
13 static int data_attr_cb2(
const struct nlattr *attr,
void *data)
20 perror(
"mnl_attr_validate");
26 static void attributes_show_ipv4(
struct nlattr *tb[])
33 printf(
"dst=%s ", inet_ntoa(*addr));
37 printf(
"src=%s ", inet_ntoa(*addr));
45 if (tb[RTA_PREFSRC]) {
47 printf(
"prefsrc=%s ", inet_ntoa(*addr));
49 if (tb[RTA_GATEWAY]) {
51 printf(
"gw=%s ", inet_ntoa(*addr));
53 if (tb[RTA_METRICS]) {
55 struct nlattr *tbx[RTAX_MAX+1] = {};
59 for (i=0; i<RTAX_MAX; i++) {
61 printf(
"metrics[%d]=%u ",
69 static int data_attr_cb(
const struct nlattr *attr,
void *data)
71 const struct nlattr **tb = data;
87 perror(
"mnl_attr_validate");
93 perror(
"mnl_attr_validate");
102 static int data_cb(
const struct nlmsghdr *nlh,
void *data)
104 struct nlattr *tb[RTA_MAX+1] = {};
108 printf(
"family=%u ", rm->rtm_family);
111 printf(
"dst_len=%u ", rm->rtm_dst_len);
114 printf(
"src_len=%u ", rm->rtm_src_len);
117 printf(
"tos=%u ", rm->rtm_tos);
132 printf(
"table=%u ", rm->rtm_table);
149 printf(
"type=%u ", rm->rtm_type);
161 printf(
"scope=%u ", rm->rtm_scope);
173 printf(
"proto=%u ", rm->rtm_protocol);
181 printf(
"flags=%x\n", rm->rtm_flags);
185 switch(rm->rtm_family) {
187 attributes_show_ipv4(tb);
197 char buf[MNL_SOCKET_BUFFER_SIZE];
198 struct nlmsghdr *nlh;
201 unsigned int seq, portid;
204 nlh->nlmsg_type = RTM_GETROUTE;
205 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
206 nlh->nlmsg_seq = seq = time(NULL);
208 rtm->rtm_family = AF_INET;
212 perror(
"mnl_socket_open");
217 perror(
"mnl_socket_bind");
223 perror(
"mnl_socket_send");
229 ret =
mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
230 if (ret <= MNL_CB_STOP)