我使用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_
總是空的。 我在做什麼錯誤以及如何獲取地址?