diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 8 | ||||
-rw-r--r-- | tools/netlink-test.c | 113 |
3 files changed, 121 insertions, 1 deletions
@@ -55,6 +55,7 @@ tools/stats-tool tools/stats-ringbuffer-dump tools/private-network-test tools/session-test +tools/netlink-test unit/test-ippool unit/test-nat unit/test-pbkdf2-sha1 diff --git a/Makefile.am b/Makefile.am index a51c64f4..2a309ec3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -37,6 +37,9 @@ else gweb_sources += gweb/giognutls.h gweb/gionotls.c endif +shared_sources = src/shared/util.h src/shared/util.c \ + src/shared/netlink.h src/shared/netlink.c + if DATAFILES if NMCOMPAT @@ -271,7 +274,7 @@ noinst_PROGRAMS += tools/supplicant-test \ tools/iptables-test tools/tap-test tools/wpad-test \ tools/stats-tool tools/private-network-test \ tools/session-test tools/iptables-unit \ - tools/dnsproxy-test + tools/dnsproxy-test tools/netlink-test tools_supplicant_test_SOURCES = $(gdbus_sources) tools/supplicant-test.c \ tools/supplicant-dbus.h tools/supplicant-dbus.c \ @@ -319,6 +322,9 @@ tools_iptables_unit_LDADD = @GLIB_LIBS@ @DBUS_LIBS@ @XTABLES_LIBS@ -ldl tools_dnsproxy_test_SOURCES = tools/dnsproxy-test.c tools_dnsproxy_test_LDADD = @GLIB_LIBS@ +tools_netlink_test_SOURCES =$(shared_sources) tools/netlink-test.c +tools_netlink_test_LDADD = @GLIB_LIBS@ + endif test_scripts = test/get-state test/list-services \ diff --git a/tools/netlink-test.c b/tools/netlink-test.c new file mode 100644 index 00000000..d2651f83 --- /dev/null +++ b/tools/netlink-test.c @@ -0,0 +1,113 @@ +/* + * + * Connection Manager + * + * Copyright (C) 2013 BWM CarIT GmbH. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <sys/socket.h> +#include <linux/netlink.h> +#include <linux/rtnetlink.h> +#include <linux/genetlink.h> +#include <linux/netfilter/nfnetlink.h> +#include <net/if.h> + +#include <string.h> +#include <stdio.h> + +#include <glib.h> + +#include "../src/shared/netlink.h" + +static GMainLoop *mainloop; + +static void do_debug(const char *str, void *user_data) +{ + const char *prefix = user_data; + + printf("%s%s\n", prefix, str); +} + +static void getlink_callback(int error, uint16_t type, const void *data, + uint32_t len, void *user_data) +{ + const struct ifinfomsg *ifi = data; + struct rtattr *rta; + int bytes; + char ifname[IF_NAMESIZE]; + uint32_t index, flags; + + if (error) + goto done; + + bytes = len - NLMSG_ALIGN(sizeof(struct ifinfomsg)); + + memset(ifname, 0, sizeof(ifname)); + + index = ifi->ifi_index; + flags = ifi->ifi_flags; + + for (rta = IFLA_RTA(ifi); RTA_OK(rta, bytes); + rta = RTA_NEXT(rta, bytes)) { + switch (rta->rta_type) { + case IFLA_IFNAME: + if (RTA_PAYLOAD(rta) <= IF_NAMESIZE) + strcpy(ifname, RTA_DATA(rta)); + break; + } + } + + printf("index=%d flags=0x%08x name=%s\n", index, flags, ifname); + +done: + g_main_loop_quit(mainloop); +} + +static void test_case_1(void) +{ + struct netlink_info *netlink; + struct ifinfomsg msg; + + netlink = netlink_new(NETLINK_ROUTE); + + printf("\n"); + netlink_set_debug(netlink, do_debug, "[NETLINK] ", NULL); + + memset(&msg, 0, sizeof(msg)); + + netlink_send(netlink, RTM_GETLINK, NLM_F_DUMP, &msg, sizeof(msg), + getlink_callback, NULL, NULL); + + mainloop = g_main_loop_new(NULL, FALSE); + g_main_loop_run(mainloop); + g_main_loop_unref(mainloop); + + netlink_destroy(netlink); +} + +int main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/netlink/Test case 1", test_case_1); + + return g_test_run(); +} |