diff --git a/src/hostmngr.c b/src/hostmngr.c index c9f6cd306532f69aa465bc9cac33a9b3891caa43..c6afc12b4ea49796b5e01374c1eda18445c8b6a2 100644 --- a/src/hostmngr.c +++ b/src/hostmngr.c @@ -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,33 @@ 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; + + 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; } @@ -121,7 +135,6 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg) struct rtnl_link *link = NULL; int num_ipaddrs = 32; struct nl_addr *a = NULL; - int ret; nl_object_get(obj); @@ -147,40 +160,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;