1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
/*
* Embedded Linux library
* Copyright (C) 2011-2014 Intel Corporation
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/socket.h>
#include <linux/rtnetlink.h>
#include <net/if.h>
#include <assert.h>
#include <ell/ell.h>
static void do_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
l_info("%s%s", 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 l_netlink_attr attr;
char ifname[IF_NAMESIZE];
uint32_t index, flags;
uint16_t rta_type;
uint16_t rta_len;
const void *rta_data;
if (error)
goto done;
if (l_netlink_attr_init(&attr, sizeof(struct ifinfomsg), data, len) < 0)
goto done;
memset(ifname, 0, sizeof(ifname));
while (!l_netlink_attr_next(&attr, &rta_type, &rta_len, &rta_data)) {
if (rta_type != IFLA_IFNAME)
continue;
if (rta_len <= IF_NAMESIZE)
strcpy(ifname, rta_data);
break;
}
index = ifi->ifi_index;
flags = ifi->ifi_flags;
l_info("index=%d flags=0x%08x name=%s", index, flags, ifname);
done:
l_main_quit();
}
static void link_notification(uint16_t type, void const * data,
uint32_t len, void * user_data)
{
}
static void test_netlink(const void *data)
{
struct l_netlink *netlink;
struct ifinfomsg ifi;
struct l_netlink_message *nlm =
l_netlink_message_new_sized(RTM_GETLINK,
NLM_F_DUMP, sizeof(ifi));
unsigned int id, link_id;
int exit_status;
assert(l_main_init());
l_log_set_stderr();
netlink = l_netlink_new(NETLINK_ROUTE);
assert(netlink);
l_netlink_set_debug(netlink, do_debug, "[NETLINK] ", NULL);
memset(&ifi, 0, sizeof(ifi));
l_netlink_message_add_header(nlm, &ifi, sizeof(ifi));
id = l_netlink_send(netlink, nlm, getlink_callback, NULL, NULL);
assert(id > 0);
link_id = l_netlink_register(netlink, RTNLGRP_LINK,
link_notification, NULL, NULL);
assert(link_id > 0);
exit_status = l_main_run();
assert(exit_status == EXIT_SUCCESS);
assert(l_netlink_unregister(netlink, link_id));
l_netlink_destroy(netlink);
assert(l_main_exit());
}
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
l_test_add("netlink", test_netlink, NULL);
return l_test_run();
}
|