我使用netlink
得到的接口,它的名称,型号等,但我不能得到L2地址(ugly_data
是nlmsghdr*
):如何通过netlink获取网络链接L2地址?
struct ifinfomsg *iface;
struct rtattr *attribute;
int len;
iface = (struct ifinfomsg *) NLMSG_DATA(ugly_data);
len = ugly_data->nlmsg_len - NLMSG_LENGTH(sizeof(*iface));
for (attribute = IFLA_RTA(iface);
RTA_OK(attribute, len);
attribute = RTA_NEXT(attribute, len))
{
id_ = iface->ifi_index;
// get type
switch (iface->ifi_type)
{
case ARPHRD_ETHER:
type_ = "Ethernet";
break;
case ...
}
// get attributes
switch (attribute->rta_type)
{
case IFLA_IFNAME:
name_ = (char *) RTA_DATA(attribute);
break;
case IFLA_ADDRESS:
address_ = (char *) RTA_DATA(attribute);
break;
...
}
}
type_
,id_
和name_
包含正确的价值观,同我从ifconfig
得到,但address_
总是空的。 我在做什么错误以及如何获取地址?