Skip to main content
Sign in
Snippets Groups Projects
Commit a73b04bc authored by Jakob Olsson's avatar Jakob Olsson
Browse files

add interface init helper funcs

To update ipaddrs, bridge info and mediatype
parent 30f4f024
No related branches found
No related tags found
No related merge requests found
Pipeline #202641 failed
......@@ -51,26 +51,9 @@ static void hostmngr_sighandler(int sig)
signal_pending = sig;
}
struct local_interface *hostmngr_alloc_interface(const char *ifname)
{
struct local_interface *iface = NULL;
struct ip_address ips[32] = {0};
int num_ipaddrs = 32;
int ret;
iface = calloc(1, sizeof(*iface));
if (!iface) {
return NULL;
}
snprintf(iface->ifname, 16, "%s", ifname);
iface->ifindex = if_nametoindex(ifname);
dbg("%s: %s (%d)\n", __func__, iface->ifname, iface->ifindex);
if_gethwaddr(ifname, iface->macaddr);
if_getflags(ifname, &iface->ifflags);
if_getoperstate(ifname, &iface->operstate);
if_getcarrier(ifname, &iface->carrier);
static void hostmngr_update_interface_bridgeinfo(struct local_interface *iface)
{
iface->is_bridge = if_isbridge(iface->ifname);
if (!iface->is_bridge) {
int br_ifindex;
......@@ -82,6 +65,13 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname)
iface->br_ifindex = br_ifindex;
}
}
}
static void hostmngr_update_interface_ipaddrs(struct local_interface *iface)
{
struct ip_address ips[32] = {0};
int num_ipaddrs = 32;
int ret;
ret = if_getaddrs(iface->ifname, ips, &num_ipaddrs);
if (!ret && num_ipaddrs > 0) {
......@@ -91,7 +81,10 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname)
memcpy(iface->ipaddrs, ips, num_ipaddrs * sizeof(struct ip_address));
}
}
}
static void hostmngr_update_interface_mediatype(struct local_interface *iface)
{
if (strncmp(iface->ifname, "lo", 2)) {
if (is_wifi_interface(iface->ifname)) {
iface->mediatype = IF_MEDIA_WIFI;
......@@ -100,12 +93,35 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname)
iface->is_affiliated_ap = is_affiliated_ap(iface->ifname);
if (iface->is_affiliated_ap)
ap_get_mld_ifname(iface->ifname, iface->mld_ifname);
}
} else {
iface->mediatype = IF_MEDIA_ETH;
}
}
}
struct local_interface *hostmngr_alloc_interface(const char *ifname)
{
struct local_interface *iface = NULL;
int ret;
iface = calloc(1, sizeof(*iface));
if (!iface) {
return NULL;
}
snprintf(iface->ifname, 16, "%s", ifname);
iface->ifindex = if_nametoindex(ifname);
dbg("%s: %s (%d)\n", __func__, iface->ifname, iface->ifindex);
if_gethwaddr(ifname, iface->macaddr);
if_getflags(ifname, &iface->ifflags);
if_getoperstate(ifname, &iface->operstate);
if_getcarrier(ifname, &iface->carrier);
hostmngr_update_interface_bridgeinfo(iface);
hostmngr_update_interface_ipaddrs(iface);
hostmngr_update_interface_mediatype(iface);
return iface;
}
......@@ -147,40 +163,10 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg)
iface->ifflags = rtnl_link_get_flags(link);
iface->operstate = rtnl_link_get_operstate(link);
iface->is_bridge = if_isbridge(iface->ifname);
if (!iface->is_bridge) {
int br_ifindex;
br_ifindex = if_isbridge_interface(iface->ifname);
if (br_ifindex > 0) {
iface->is_brif = true;
iface->brport = if_brportnum(iface->ifname);
iface->br_ifindex = br_ifindex;
}
}
ret = if_getaddrs(iface->ifname, ips, &num_ipaddrs);
if (!ret && num_ipaddrs > 0) {
iface->ipaddrs = calloc(num_ipaddrs, sizeof(struct ip_address));
if (iface->ipaddrs) {
iface->num_ipaddrs = num_ipaddrs;
memcpy(iface->ipaddrs, ips, num_ipaddrs * sizeof(struct ip_address));
}
}
if (strncmp(iface->ifname, "lo", 2)) {
if (is_wifi_interface(iface->ifname)) {
iface->mediatype = IF_MEDIA_WIFI;
if (is_ap_interface(iface->ifname)) {
iface->is_ap = true;
iface->is_affiliated_ap = is_affiliated_ap(iface->ifname);
if (iface->is_affiliated_ap)
ap_get_mld_ifname(iface->ifname, iface->mld_ifname);
}
} else {
iface->mediatype = IF_MEDIA_ETH;
}
}
hostmngr_update_interface_bridgeinfo(iface);
hostmngr_update_interface_ipaddrs(iface);
hostmngr_update_interface_mediatype(iface);
list_add_tail(&iface->list, argp->iflist);
*argp->n += 1;
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment