luci-lib-ip: add license tag
[project/luci.git] / libs / luci-lib-luaneightbl / src / neightbl.c
1 /*
2 License:
3 Copyright 2013 Steven Barth <steven@midlink.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */
17
18 #include <stdlib.h>
19 #include <stdbool.h>
20 #include <lua.h>
21 #include <lualib.h>
22 #include <lauxlib.h>
23 #include <linux/rtnetlink.h>
24 #include <sys/socket.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <net/if.h>
29 #include <arpa/inet.h>
30 #include <netinet/ether.h>
31
32 char *ether_ntoa_l (const struct ether_addr *addr, char *buf)
33 {
34   sprintf (buf, "%02x:%02x:%02x:%02x:%02x:%02x",
35            addr->ether_addr_octet[0], addr->ether_addr_octet[1],
36            addr->ether_addr_octet[2], addr->ether_addr_octet[3],
37            addr->ether_addr_octet[4], addr->ether_addr_octet[5]);
38   return buf;
39 }
40
41 static int neightbl_get(lua_State *L) {
42         int sock = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
43         struct sockaddr_nl kernel = {AF_NETLINK, 0, 0, 0};
44         if (connect(sock, (struct sockaddr*)&kernel, sizeof(kernel)))
45                 goto error;
46
47         const char *ifname = luaL_checkstring(L, 1);
48         int ifindex = if_nametoindex(ifname);
49         if (ifindex <= 0)
50                 goto error;
51
52         struct {
53                 struct nlmsghdr hdr;
54                 struct ndmsg ndm;
55         } req = {
56                 {sizeof(req), RTM_GETNEIGH, NLM_F_REQUEST | NLM_F_DUMP, 1, 0},
57                 {AF_INET6, 0, 0, ifindex, 0, 0, 0},
58         };
59
60         if (send(sock, &req, sizeof(req), 0) != sizeof(req))
61                 goto error;
62
63         lua_newtable(L);
64
65         char buf[8192];
66         struct nlmsghdr *nh = (struct nlmsghdr*)buf;
67         do {
68                 ssize_t len = recv(sock, buf, sizeof(buf), 0);
69                 if (len < 0) {
70                         lua_pop(L, 1);
71                         goto error;
72                 }
73
74
75                 for (;NLMSG_OK(nh, (size_t)len) && nh->nlmsg_type == RTM_NEWNEIGH;
76                                 nh = NLMSG_NEXT(nh, len)) {
77                         struct ndmsg *ndm = NLMSG_DATA(nh);
78                         if (NLMSG_PAYLOAD(nh, 0) < sizeof(*ndm) || ndm->ndm_ifindex != ifindex)
79                                 continue;
80
81                         ssize_t alen = NLMSG_PAYLOAD(nh, sizeof(*ndm));
82                         char buf[INET6_ADDRSTRLEN] = {0}, *mac = NULL, str_buf[ETH_ALEN];
83                         for (struct rtattr *rta = (struct rtattr*)&ndm[1]; RTA_OK(rta, alen);
84                                         rta = RTA_NEXT(rta, alen)) {
85                                 if (rta->rta_type == NDA_DST && RTA_PAYLOAD(rta) >= sizeof(struct in6_addr))
86                                         inet_ntop(AF_INET6, RTA_DATA(rta), buf, sizeof(buf));
87                                 else if (rta->rta_type == NDA_LLADDR && RTA_PAYLOAD(rta) >= 6)
88                                         mac = ether_ntoa_l(RTA_DATA(rta),str_buf);
89                         }
90
91                         if (mac)
92                                 lua_pushstring(L, mac);
93                         else
94                                 lua_pushboolean(L, false);
95
96                         lua_setfield(L, -2, buf);
97                 }
98         } while (nh->nlmsg_type == RTM_NEWNEIGH);
99
100         close(sock);
101         return 1;
102
103 error:
104         close(sock);
105         lua_pushnil(L);
106         lua_pushinteger(L, errno);
107         lua_pushstring(L, strerror(errno));
108         return 3;
109 }
110
111
112 static const luaL_reg R[] = {
113         {"get", neightbl_get},
114         {NULL, NULL}
115 };
116
117
118 int luaopen_neightbl(lua_State *l)
119 {
120         luaL_register(l, "neightbl", R);
121         return 1;
122 }