5 * Copyright (C) 2013-2014 BMW Car IT GmbH.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/socket.h>
27 #include <linux/netlink.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/genetlink.h>
30 #include <linux/netfilter/nfnetlink.h>
40 #include "../src/shared/netlink.h"
42 #define NFGEN_DATA(nlh) ((void *)((char *)(nlh) + \
43 NLMSG_ALIGN(sizeof(struct nfgenmsg))))
44 #define NLA_DATA(nla) ((void *)((char*)(nla) + NLA_HDRLEN))
45 #define NLA_OK(nla,len) ((len) >= (int)sizeof(struct nlattr) && \
46 (nla)->nla_len >= sizeof(struct nlattr) && \
47 (nla)->nla_len <= (len))
48 #define NLA_NEXT(nla,attrlen) ((attrlen) -= NLA_ALIGN((nla)->nla_len), \
49 (struct nlattr*)(((char*)(nla)) + \
50 NLA_ALIGN((nla)->nla_len)))
52 static GMainLoop *mainloop;
54 static void do_debug(const char *str, void *user_data)
56 const char *prefix = user_data;
58 printf("%s%s\n", prefix, str);
61 static void getlink_callback(unsigned int error, uint16_t type, const void *data,
62 uint32_t len, void *user_data)
64 const struct ifinfomsg *ifi = data;
67 char ifname[IF_NAMESIZE];
68 uint32_t index, flags;
70 g_assert_cmpuint(error, ==, 0);
72 bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg));
74 memset(ifname, 0, sizeof(ifname));
76 index = ifi->ifi_index;
77 flags = ifi->ifi_flags;
79 for (rta = IFLA_RTA(ifi); RTA_OK(rta, bytes);
80 rta = RTA_NEXT(rta, bytes)) {
81 switch (rta->rta_type) {
83 if (RTA_PAYLOAD(rta) <= IF_NAMESIZE)
84 strcpy(ifname, RTA_DATA(rta));
89 printf("index=%d flags=0x%08x name=%s\n", index, flags, ifname);
91 g_main_loop_quit(mainloop);
94 static void test_case_1(void)
96 struct netlink_info *netlink;
99 netlink = netlink_new(NETLINK_ROUTE);
102 netlink_set_debug(netlink, do_debug, "[NETLINK] ", NULL);
104 memset(&msg, 0, sizeof(msg));
106 netlink_send(netlink, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg),
107 getlink_callback, NULL, NULL);
109 mainloop = g_main_loop_new(NULL, FALSE);
110 g_main_loop_run(mainloop);
111 g_main_loop_unref(mainloop);
113 netlink_destroy(netlink);
116 int main(int argc, char *argv[])
118 g_test_init(&argc, &argv, NULL);
120 g_test_add_func("/netlink/Test case 1", test_case_1);