1 #include "netlink.h"
2 #include <errno.h>
3 #include <string.h>
4 #include <sys/socket.h>
5 #include <unistd.h>
6
__netlink_enumerate(int fd,unsigned int seq,int type,int af,int (* cb)(void * ctx,struct nlmsghdr * h),void * ctx)7 static int __netlink_enumerate(int fd, unsigned int seq, int type, int af,
8 int (*cb)(void* ctx, struct nlmsghdr* h), void* ctx) {
9 struct nlmsghdr* h;
10 union {
11 uint8_t buf[8192];
12 struct {
13 struct nlmsghdr nlh;
14 struct rtgenmsg g;
15 } req;
16 struct nlmsghdr reply;
17 } u;
18 int r, ret;
19
20 memset(&u.req, 0, sizeof(u.req));
21 u.req.nlh.nlmsg_len = sizeof(u.req);
22 u.req.nlh.nlmsg_type = type;
23 u.req.nlh.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST;
24 u.req.nlh.nlmsg_seq = seq;
25 u.req.g.rtgen_family = af;
26 r = send(fd, &u.req, sizeof(u.req), 0);
27 if (r < 0)
28 return r;
29
30 while (1) {
31 r = recv(fd, u.buf, sizeof(u.buf), MSG_DONTWAIT);
32 if (r <= 0)
33 return -1;
34 for (h = &u.reply; NLMSG_OK(h, (void*)&u.buf[r]); h = NLMSG_NEXT(h)) {
35 if (h->nlmsg_type == NLMSG_DONE)
36 return 0;
37 if (h->nlmsg_type == NLMSG_ERROR)
38 return -1;
39 ret = cb(ctx, h);
40 if (ret)
41 return ret;
42 }
43 }
44 }
45
__rtnetlink_enumerate(int link_af,int addr_af,int (* cb)(void * ctx,struct nlmsghdr * h),void * ctx)46 int __rtnetlink_enumerate(int link_af, int addr_af, int (*cb)(void* ctx, struct nlmsghdr* h),
47 void* ctx) {
48 int fd, r;
49
50 fd = socket(PF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
51 if (fd < 0)
52 return -1;
53 r = __netlink_enumerate(fd, 1, RTM_GETLINK, link_af, cb, ctx);
54 if (!r)
55 r = __netlink_enumerate(fd, 2, RTM_GETADDR, addr_af, cb, ctx);
56 close(fd);
57 return r;
58 }
59