add netgetdev api

This commit is contained in:
zhao maosheng 2024-09-14 11:10:22 +08:00
parent bb91502465
commit 4c2222fde7
2 changed files with 41 additions and 0 deletions

View File

@ -157,6 +157,9 @@ struct netdev *netdev_get_by_name(const char *name);
struct netdev *netdev_get_by_family(int family);
int netdev_family_get(struct netdev *netdev);
#endif /* RT_USING_SAL */
#if defined(SAL_USING_AF_NETLINK)
int netdev_getnetdev(struct msg_buf *msg, int (*cb)(struct msg_buf *m_buf, struct netdev *nd, int nd_num, int index, int ipvx));
#endif
/* Set default network interface device in list */
void netdev_set_default(struct netdev *netdev);

View File

@ -394,6 +394,44 @@ int netdev_family_get(struct netdev *netdev)
#endif /* RT_USING_SAL */
#if defined(SAL_USING_AF_NETLINK)
int netdev_getnetdev(struct msg_buf *msg, int (*cb)(struct msg_buf *m_buf, struct netdev *nd, int nd_num, int index, int ipvx))
{
struct netdev *cur_nd_list = netdev_list;
struct netdev *nd_node;
int nd_num = 0;
int err = 0;
if (cur_nd_list == RT_NULL)
return 0;
rt_spin_lock(&_spinlock);
nd_num = rt_slist_len(&cur_nd_list->list) + 1;
rt_spin_unlock(&_spinlock);
err = cb(msg, cur_nd_list, nd_num, nd.ifindex, ROUTE_IPV4_TRUE);
if (err < 0)
return err;
rt_spin_lock(&_spinlock);
rt_slist_for_each_entry(nd_node, &(cur_nd_list->list), list)
{
rt_spin_unlock(&_spinlock);
err = cb(msg, nd_node, nd_num, nd.ifindex, ROUTE_IPV4_TRUE);
if (err < 0)
{
return err;
}
rt_spin_lock(&_spinlock);
}
rt_spin_unlock(&_spinlock);
return 0;
}
#endif
/**
* This function will set default network interface device.
*