1 /* This example is placed in the public domain. */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <time.h>
7 #include <arpa/inet.h>
8
9 #include <libmnl/libmnl.h>
10 #include <linux/if.h>
11 #include <linux/if_link.h>
12 #include <linux/rtnetlink.h>
13
data_attr_cb(const struct nlattr * attr,void * data)14 static int data_attr_cb(const struct nlattr *attr, void *data)
15 {
16 const struct nlattr **tb = data;
17 int type = mnl_attr_get_type(attr);
18
19 /* skip unsupported attribute in user-space */
20 if (mnl_attr_type_valid(attr, IFA_MAX) < 0)
21 return MNL_CB_OK;
22
23 switch(type) {
24 case IFA_ADDRESS:
25 if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) {
26 perror("mnl_attr_validate");
27 return MNL_CB_ERROR;
28 }
29 break;
30 }
31 tb[type] = attr;
32 return MNL_CB_OK;
33 }
34
data_cb(const struct nlmsghdr * nlh,void * data)35 static int data_cb(const struct nlmsghdr *nlh, void *data)
36 {
37 struct nlattr *tb[IFA_MAX + 1] = {};
38 struct ifaddrmsg *ifa = mnl_nlmsg_get_payload(nlh);
39
40 printf("index=%d family=%d ", ifa->ifa_index, ifa->ifa_family);
41
42 mnl_attr_parse(nlh, sizeof(*ifa), data_attr_cb, tb);
43 printf("addr=");
44 if (tb[IFA_ADDRESS]) {
45 void *addr = mnl_attr_get_payload(tb[IFA_ADDRESS]);
46 char out[INET6_ADDRSTRLEN];
47
48 if (inet_ntop(ifa->ifa_family, addr, out, sizeof(out)))
49 printf("%s ", out);
50 }
51 printf("scope=");
52 switch(ifa->ifa_scope) {
53 case 0:
54 printf("global ");
55 break;
56 case 200:
57 printf("site ");
58 break;
59 case 253:
60 printf("link ");
61 break;
62 case 254:
63 printf("host ");
64 break;
65 case 255:
66 printf("nowhere ");
67 break;
68 default:
69 printf("%d ", ifa->ifa_scope);
70 break;
71 }
72
73 printf("\n");
74 return MNL_CB_OK;
75 }
76
main(int argc,char * argv[])77 int main(int argc, char *argv[])
78 {
79 char buf[MNL_SOCKET_DUMP_SIZE];
80 unsigned int seq, portid;
81 struct mnl_socket *nl;
82 struct nlmsghdr *nlh;
83 struct rtgenmsg *rt;
84 int ret;
85
86 if (argc != 2) {
87 fprintf(stderr, "Usage: %s <inet|inet6>\n", argv[0]);
88 exit(EXIT_FAILURE);
89 }
90
91 nlh = mnl_nlmsg_put_header(buf);
92 nlh->nlmsg_type = RTM_GETADDR;
93 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
94 nlh->nlmsg_seq = seq = time(NULL);
95 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
96 if (strcmp(argv[1], "inet") == 0)
97 rt->rtgen_family = AF_INET;
98 else if (strcmp(argv[1], "inet6") == 0)
99 rt->rtgen_family = AF_INET6;
100
101 nl = mnl_socket_open(NETLINK_ROUTE);
102 if (nl == NULL) {
103 perror("mnl_socket_open");
104 exit(EXIT_FAILURE);
105 }
106
107 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
108 perror("mnl_socket_bind");
109 exit(EXIT_FAILURE);
110 }
111 portid = mnl_socket_get_portid(nl);
112
113 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
114 perror("mnl_socket_sendto");
115 exit(EXIT_FAILURE);
116 }
117
118 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
119 while (ret > 0) {
120 ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL);
121 if (ret <= MNL_CB_STOP)
122 break;
123 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
124 }
125 if (ret == -1) {
126 perror("error");
127 exit(EXIT_FAILURE);
128 }
129
130 mnl_socket_close(nl);
131
132 return 0;
133 }
134