本文整理汇总了C++中IFLA_RTA函数的典型用法代码示例。如果您正苦于以下问题:C++ IFLA_RTA函数的具体用法?C++ IFLA_RTA怎么用?C++ IFLA_RTA使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了IFLA_RTA函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: memset
void linux_unicast_router::handle_intf_event(bool isnew, nlmsghdr *hdr) {
ifinfomsg *ifi = (ifinfomsg *)NLMSG_DATA(hdr);
if (isnew) {
rtattr *tb[IFLA_MAX + 1];
memset(tb, 0, sizeof(tb));
netlink_msg::parse_rtatable(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(hdr));
if (tb[IFLA_IFNAME] && tb[IFLA_MTU]) {
interface_desc desc;
desc.index = ifi->ifi_index;
desc.name = (const char *)RTA_DATA(tb[IFLA_IFNAME]);
desc.mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
desc.flags = ifi->ifi_flags;
desc.type = _conv_intf(ifi->ifi_type, ifi->ifi_flags);
desc.up = ((ifi->ifi_flags & IFF_UP) == IFF_UP);
if (rt_dumping) {
_interfaces.push_back(desc);
} else {
_install_interface(desc);
}
}
} else {
g_mrd->lost_interface(ifi->ifi_index);
}
}
示例2: NLMSG_LENGTH
int TunManager::getLinkRespParser(const struct sockaddr_nl *who,
struct nlmsghdr *n,
void *arg) {
// only cares about RTM_NEWLINK
if (n->nlmsg_type != RTM_NEWLINK) {
return 0;
}
struct ifinfomsg *ifi = static_cast<struct ifinfomsg *>(NLMSG_DATA(n));
struct rtattr *tb[IFLA_MAX + 1];
int len = n->nlmsg_len;
len -= NLMSG_LENGTH(sizeof(*ifi));
if (len < 0) {
throw FbossError("Wrong length for RTM_GETLINK response ", len, " vs ",
NLMSG_LENGTH(sizeof(*ifi)));
}
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
if (tb[IFLA_IFNAME] == nullptr) {
throw FbossError("Device @ index ", static_cast<int>(ifi->ifi_index),
" does not have a nmae");
}
const auto name = rta_getattr_str(tb[IFLA_IFNAME]);
// match the interface name against intfPrefix
if (!TunIntf::isTunIntf(name)) {
VLOG(3) << "Ignore interface " << name
<< " because it is not a tun interface";
return 0;
}
// base on the name, get the router ID
auto rid = TunIntf::getRidFromName(name);
TunManager *mgr = static_cast<TunManager *>(arg);
mgr->addIntf(rid, std::string(name), ifi->ifi_index);
return 0;
}
示例3: getlink_callback
static void getlink_callback(unsigned 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;
g_assert_cmpuint(error, ==, 0);
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);
g_main_loop_quit(mainloop);
}
示例4: test_container
static void test_container(void) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
uint16_t type;
uint32_t u32_data;
void *data;
int r;
struct ifinfomsg *ifi;
assert_se(sd_rtnl_message_new_link(NULL, &m, RTM_NEWLINK, 0) >= 0);
assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
assert_se(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -ENOTSUP);
assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) >= 0);
assert_se(sd_rtnl_message_open_container(m, IFLA_INFO_DATA) == -ENOTSUP);
assert_se(sd_rtnl_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
assert_se(sd_rtnl_message_close_container(m) >= 0);
assert_se(sd_rtnl_message_append_string(m, IFLA_INFO_KIND, "kind") >= 0);
assert_se(sd_rtnl_message_close_container(m) >= 0);
assert_se(sd_rtnl_message_close_container(m) == -EINVAL);
assert_se(rtnl_message_seal(NULL, m) >= 0);
assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
assert_se(type == IFLA_LINKINFO);
assert_se(data == NULL);
/*
assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
assert_se(type == IFLA_INFO_KIND);
assert_se(streq("kind", (char *)data));
assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
assert_se(type == IFLA_INFO_DATA);
assert_se(data == NULL);
assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
assert_se(type == IFLA_VLAN_ID);
assert_se(*(uint16_t *)data == 100);
assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
assert_se(sd_rtnl_message_exit_container(m) >= 0);
assert_se(sd_rtnl_message_read(m, &type, &data) >= 0);
assert_se(type == IFLA_INFO_KIND);
assert_se(streq("kind", (char *)data));
assert_se(sd_rtnl_message_read(m, &type, &data) == 0);
assert_se(sd_rtnl_message_exit_container(m) >= 0);
*/
ifi = NLMSG_DATA(m->hdr);
r = rtnl_message_parse(m,
&m->rta_offset_tb,
&m->rta_tb_size,
IFLA_MAX,
IFLA_RTA(ifi),
IFLA_PAYLOAD(m->hdr));
if(r < 0)
return;
assert_se(sd_rtnl_message_read_u32(m, IFLA_LINKINFO, &u32_data) == 0);
assert_se(sd_rtnl_message_exit_container(m) == -EINVAL);
}
示例5: get_ipv6_address
boost::asio::ip::address_v6 get_ipv6_address(const nlmsghdr *in, const nlmsghdr *an)
{
boost::asio::ip::address_v6 unspec;
if (in->nlmsg_type != RTM_NEWLINK)
return unspec;
if (an->nlmsg_type != RTM_NEWADDR)
return unspec;
ifinfomsg* ifi = (ifinfomsg*)NLMSG_DATA(in);
ifaddrmsg* ifa = (ifaddrmsg*)NLMSG_DATA(an);
__u32 ilen = in->nlmsg_len;
if (ilen < NLMSG_LENGTH(sizeof(*ifi)))
return unspec;
ilen -= NLMSG_LENGTH(sizeof(*ifi));
__u32 alen = an->nlmsg_len;
if (alen < NLMSG_LENGTH(sizeof(*ifa)))
return unspec;
alen -= NLMSG_LENGTH(sizeof(*ifa));
/* NOTE: ifi_index and ifa_index should have the same type (int), but for
* some reason they are not... So instead of a normal (in)equality comparison
* we do a bit-wise compare.
*/
if (ifi->ifi_index ^ ifa->ifa_index)
return unspec;
if (ifi->ifi_family != AF_INET6 || ifa->ifa_family != AF_INET6)
return unspec;
rtattr* tbi[IFLA_MAX+1];
memset(tbi, 0, sizeof(tbi));
parse_rtattr(tbi, IFLA_MAX, IFLA_RTA(ifi), ilen);
if (tbi[IFLA_IFNAME] == NULL)
return unspec;
rtattr* tba[IFA_MAX+1];
memset(tba, 0, sizeof(tba));
parse_rtattr(tba, IFA_MAX, IFA_RTA(ifa), alen);
char abuf[256];
rt_addr_n2a(ifa->ifa_family,
RTA_PAYLOAD(tba[IFA_ADDRESS]),
RTA_DATA(tba[IFA_ADDRESS]),
abuf, sizeof(abuf));
std::string ipaddr = abuf;
try {
boost::asio::ip::address_v6 addr(boost::asio::ip::address_v6::from_string(ipaddr));
addr.scope_id(ifi->ifi_index);
return addr;
} catch(...) {
return unspec;
}
}
示例6: ril_mtu_watch_handle_ifinfomsg
static void ril_mtu_watch_handle_ifinfomsg(struct ril_mtu_watch *self,
const struct ifinfomsg *ifi, int len)
{
if (ifi->ifi_flags & IFF_UP) {
const struct rtattr *rta = IFLA_RTA(ifi);
ril_mtu_watch_handle_rtattr(self, rta,
len - ((char*)rta - (char*)ifi));
}
}
示例7: if_info_save_interface
int if_info_save_interface(struct nlmsghdr *hdr, void *arg)
{
struct rtattr *attrs[IFLA_MAX + 1];
struct ifinfomsg *info = NLMSG_DATA(hdr);
parse_rtattrs(attrs, IFLA_MAX, IFLA_RTA(info), IFLA_PAYLOAD(hdr));
return if_info_update_interface(hdr, attrs) ? 0 : -1;
}
示例8: vlan_get_link_parse
/**
* vlan_get_link_parse - parse a get_link rtnl message and extract the important
* data
* @nh: the reply header
* @iface: pointer to the buffer where the link interface has to be stored (it
* is allocated by this function)
*
* Return the vid in case of success or -1 otherwise
*/
static int vlan_get_link_parse(struct nlmsghdr *nh, char **iface)
{
struct ifinfomsg *ifi = NLMSG_DATA(nh);
size_t vlan_len, info_len, len = nh->nlmsg_len;
struct rtattr *rta, *info, *vlan;
int idx = -1, vid = -1;
*iface = NULL;
rta = IFLA_RTA(ifi);
while (RTA_OK(rta, len)) {
/* check if the interface is a vlan */
if (rta->rta_type == IFLA_LINKINFO) {
info = RTA_DATA(rta);
info_len = RTA_PAYLOAD(rta);
while (RTA_OK(info, info_len)) {
if (info->rta_type == IFLA_INFO_KIND &&
strcmp(RTA_DATA(info), "vlan"))
goto err;
if (info->rta_type == IFLA_INFO_DATA) {
vlan = RTA_DATA(info);
vlan_len = RTA_PAYLOAD(info);
while (RTA_OK(vlan, vlan_len)) {
if (vlan->rta_type == IFLA_VLAN_ID)
vid = *(int *)RTA_DATA(vlan);
vlan = RTA_NEXT(vlan, vlan_len);
}
}
info = RTA_NEXT(info, info_len);
}
}
/* extract the name of the "link" interface */
if (rta->rta_type == IFLA_LINK) {
idx = *(int *)RTA_DATA(rta);
*iface = malloc(IFNAMSIZ + 1);
if (!if_indextoname(idx, *iface))
goto err;
}
rta = RTA_NEXT(rta, len);
}
if (vid == -1)
goto err;
if (idx <= 0)
goto err;
return vid;
err:
free(*iface);
return -1;
}
示例9: print_vlan
static int print_vlan(const struct sockaddr_nl *who,
struct nlmsghdr *n,
void *arg)
{
FILE *fp = arg;
struct ifinfomsg *ifm = NLMSG_DATA(n);
int len = n->nlmsg_len;
struct rtattr * tb[IFLA_MAX+1];
if (n->nlmsg_type != RTM_NEWLINK) {
fprintf(stderr, "Not RTM_NEWLINK: %08x %08x %08x\n",
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
return 0;
}
len -= NLMSG_LENGTH(sizeof(*ifm));
if (len < 0) {
fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
return -1;
}
if (ifm->ifi_family != AF_BRIDGE)
return 0;
if (filter_index && filter_index != ifm->ifi_index)
return 0;
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifm), len);
/* if AF_SPEC isn't there, vlan table is not preset for this port */
if (!tb[IFLA_AF_SPEC]) {
fprintf(fp, "%s\tNone\n", ll_index_to_name(ifm->ifi_index));
return 0;
} else {
struct rtattr *i, *list = tb[IFLA_AF_SPEC];
int rem = RTA_PAYLOAD(list);
fprintf(fp, "%s", ll_index_to_name(ifm->ifi_index));
for (i = RTA_DATA(list); RTA_OK(i, rem); i = RTA_NEXT(i, rem)) {
struct bridge_vlan_info *vinfo;
if (i->rta_type != IFLA_BRIDGE_VLAN_INFO)
continue;
vinfo = RTA_DATA(i);
fprintf(fp, "\t %hu", vinfo->vid);
if (vinfo->flags & BRIDGE_VLAN_INFO_PVID)
fprintf(fp, " PVID");
if (vinfo->flags & BRIDGE_VLAN_INFO_UNTAGGED)
fprintf(fp, " Egress Untagged");
fprintf(fp, "\n");
}
}
fprintf(fp, "\n");
fflush(fp);
return 0;
}
示例10: netlink_if_link_filter
/* Netlink interface link lookup filter */
static int
netlink_if_link_filter(struct sockaddr_nl *snl, struct nlmsghdr *h)
{
struct ifinfomsg *ifi;
struct rtattr *tb[IFLA_MAX + 1];
interface_t *ifp;
int len, status;
char *name;
ifi = NLMSG_DATA(h);
if (h->nlmsg_type != RTM_NEWLINK)
return 0;
len = h->nlmsg_len - NLMSG_LENGTH(sizeof (struct ifinfomsg));
if (len < 0)
return -1;
/* Interface name lookup */
memset(tb, 0, sizeof (tb));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
if (tb[IFLA_IFNAME] == NULL)
return -1;
name = (char *) RTA_DATA(tb[IFLA_IFNAME]);
/* Return if loopback */
if (ifi->ifi_type == ARPHRD_LOOPBACK)
return 0;
/* Skip it if already exist */
ifp = if_get_by_ifname(name);
if (ifp) {
#ifdef _HAVE_VRRP_VMAC_
if (!ifp->vmac)
#endif
{
#ifdef _HAVE_VRRP_VMAC_
if_vmac_reflect_flags(ifi->ifi_index, ifi->ifi_flags);
#endif
ifp->flags = ifi->ifi_flags;
}
return 0;
}
/* Fill the interface structure */
ifp = (interface_t *) MALLOC(sizeof(interface_t));
status = netlink_if_link_populate(ifp, tb, ifi);
if (status < 0) {
FREE(ifp);
return -1;
}
/* Queue this new interface_t */
if_add_queue(ifp);
return 0;
}
示例11: print_link_stats
static void print_link_stats(FILE *fp, struct nlmsghdr *n)
{
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct rtattr * tb[IFLA_MAX+1];
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi),
n->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi)));
__print_link_stats(fp, tb);
fprintf(fp, "%s", _SL_);
}
示例12: rtnl_iface_get
static int rtnl_iface_get(struct nlmsghdr *h,
int (*if_callback)(struct iface *))
{
struct rtattr *tb[IFLA_MAX + 1];
struct ifinfomsg *ifi;
int len;
struct iface iface;
if (h->nlmsg_type != RTM_NEWLINK)
return 0;
ifi = NLMSG_DATA(h);
len = h->nlmsg_len - NLMSG_LENGTH(sizeof(struct ifinfomsg));
if (len < 0)
return -1;
netlink_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
if (!tb[IFLA_IFNAME] || !tb[IFLA_ADDRESS]) {
DEBUGP("Error: getting interface info");
return -1;
}
iface.haddr_len = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
if (iface.haddr_len < MAX_ADDR_LEN) {
memcpy(iface.haddr, RTA_DATA(tb[IFLA_ADDRESS]),
iface.haddr_len);
} else {
DEBUGP("Warning: Hardware address is too large: %d",
iface.haddr_len);
return -1;
}
strcpy(iface.name, (char *)RTA_DATA(tb[IFLA_IFNAME]));
strcpy(iface.qdisc, (char *)RTA_DATA(tb[IFLA_QDISC]));
iface.index = ifi->ifi_index;
iface.type = ifi->ifi_type;
iface.flags = ifi->ifi_flags & 0x0000fffff;
iface.mtu = (tb[IFLA_MTU]) ? *(int *)RTA_DATA(tb[IFLA_MTU]) : 0;
iface.weight = (tb[IFLA_WEIGHT]) ? *(int *)RTA_DATA(tb[IFLA_WEIGHT]) : 0;
iface.txqueuelen = *(int *)RTA_DATA(tb[IFLA_TXQLEN]);
iface.stats = *(struct net_device_stats *)RTA_DATA(tb[IFLA_STATS]);
return if_callback(&iface);
}
示例13: ll_remember_index
int ll_remember_index(const struct sockaddr_nl *who,
struct nlmsghdr *n, void *arg)
{
int h;
struct ifinfomsg *ifi = NLMSG_DATA(n);
struct ll_cache *im, **imp;
struct rtattr *tb[IFLA_MAX+1];
UNUSED(who);
UNUSED(arg);
if (n->nlmsg_type != RTM_NEWLINK)
return 0;
if (n->nlmsg_len < NLMSG_LENGTH(sizeof(ifi)))
return -1;
memset(tb, 0, sizeof(tb));
parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
if (tb[IFLA_IFNAME] == NULL)
return 0;
h = ifi->ifi_index & (IDXMAP_SIZE - 1);
for (imp = &idx_head[h]; (im=*imp)!=NULL; imp = &im->idx_next)
if (im->index == ifi->ifi_index)
break;
if (im == NULL) {
im = malloc(sizeof(*im));
if (im == NULL)
return 0;
im->idx_next = *imp;
im->index = ifi->ifi_index;
*imp = im;
}
im->type = ifi->ifi_type;
im->flags = ifi->ifi_flags;
if (tb[IFLA_ADDRESS]) {
size_t alen;
im->alen = alen = RTA_PAYLOAD(tb[IFLA_ADDRESS]);
if (alen > sizeof(im->addr))
alen = sizeof(im->addr);
memcpy(im->addr, RTA_DATA(tb[IFLA_ADDRESS]), alen);
} else {
im->alen = 0;
memset(im->addr, 0, sizeof(im->addr));
}
strcpy(im->name, RTA_DATA(tb[IFLA_IFNAME]));
return 0;
}
示例14: netlink_interface
/* Called from interface_lookup_netlink(). This function is only used
during bootstrap. */
static int
netlink_interface (struct sockaddr_nl *snl, struct nlmsghdr *h)
{
int len;
struct ifinfomsg *ifi;
struct rtattr *tb[IFLA_MAX + 1];
struct interface *ifp;
char *name;
ifi = NLMSG_DATA (h);
if (h->nlmsg_type != RTM_NEWLINK)
return 0;
len = h->nlmsg_len - NLMSG_LENGTH (sizeof (struct ifinfomsg));
if (len < 0)
return -1;
/* Looking up interface name. */
memset (tb, 0, sizeof tb);
netlink_parse_rtattr (tb, IFLA_MAX, IFLA_RTA (ifi), len);
#ifdef IFLA_WIRELESS
/* check for wireless messages to ignore */
if ((tb[IFLA_WIRELESS] != NULL) && (ifi->ifi_change == 0))
{
if (IS_DEBUG_HA(kroute, KROUTE))
zlog_debug ("%s: ignoring IFLA_WIRELESS message", __func__);
return 0;
}
#endif /* IFLA_WIRELESS */
if (tb[IFLA_IFNAME] == NULL)
return -1;
name = (char *) RTA_DATA (tb[IFLA_IFNAME]);
/* Add interface. */
ifp = if_get_by_name (name);
set_ifindex(ifp, ifi->ifi_index);
ifp->flags = ifi->ifi_flags & 0x0000fffff;
ifp->mtu6 = ifp->mtu = *(uint32_t *) RTA_DATA (tb[IFLA_MTU]);
ifp->metric = 1;
/* Hardware type and address. */
ifp->hw_type = ifi->ifi_type;
netlink_interface_update_hw_addr (tb, ifp);
if_add_update (ifp);
return 0;
}
示例15: handle_link_msg
static int handle_link_msg(struct nlmsghdr *nlh, int n)
{
struct ifinfomsg *ifla_msg = NLMSG_DATA(nlh);
struct rtattr *tb[IFLA_MAX];
int atts = parse_rt_attrs(tb, IFLA_MAX, IFLA_RTA(ifla_msg), IFLA_PAYLOAD(nlh));
parse_ifinfomsg(ifla_msg);
handle_link_attrs(ifla_msg, tb, nlh->nlmsg_type);
return 0;
}