Skip to content
Snippets Groups Projects
Commit e6c66e4a authored by Jakob Olsson's avatar Jakob Olsson
Browse files

add interface init helper funcs

To update ipaddrs, bridge info and mediatype
parent cff12e95
No related branches found
No related tags found
1 merge request!45read assoclist from apmld object
Pipeline #202680 passed
...@@ -51,26 +51,9 @@ static void hostmngr_sighandler(int sig) ...@@ -51,26 +51,9 @@ static void hostmngr_sighandler(int sig)
signal_pending = 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); iface->is_bridge = if_isbridge(iface->ifname);
if (!iface->is_bridge) { if (!iface->is_bridge) {
int br_ifindex; int br_ifindex;
...@@ -82,6 +65,13 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname) ...@@ -82,6 +65,13 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname)
iface->br_ifindex = br_ifindex; 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); ret = if_getaddrs(iface->ifname, ips, &num_ipaddrs);
if (!ret && num_ipaddrs > 0) { if (!ret && num_ipaddrs > 0) {
...@@ -91,7 +81,10 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname) ...@@ -91,7 +81,10 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname)
memcpy(iface->ipaddrs, ips, num_ipaddrs * sizeof(struct ip_address)); 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 (strncmp(iface->ifname, "lo", 2)) {
if (is_wifi_interface(iface->ifname)) { if (is_wifi_interface(iface->ifname)) {
iface->mediatype = IF_MEDIA_WIFI; iface->mediatype = IF_MEDIA_WIFI;
...@@ -100,12 +93,33 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname) ...@@ -100,12 +93,33 @@ struct local_interface *hostmngr_alloc_interface(const char *ifname)
iface->is_affiliated_ap = is_affiliated_ap(iface->ifname); iface->is_affiliated_ap = is_affiliated_ap(iface->ifname);
if (iface->is_affiliated_ap) if (iface->is_affiliated_ap)
ap_get_mld_ifname(iface->ifname, iface->mld_ifname); ap_get_mld_ifname(iface->ifname, iface->mld_ifname);
} }
} else { } else {
iface->mediatype = IF_MEDIA_ETH; 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; return iface;
} }
...@@ -121,7 +135,6 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg) ...@@ -121,7 +135,6 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg)
struct rtnl_link *link = NULL; struct rtnl_link *link = NULL;
int num_ipaddrs = 32; int num_ipaddrs = 32;
struct nl_addr *a = NULL; struct nl_addr *a = NULL;
int ret;
nl_object_get(obj); nl_object_get(obj);
...@@ -147,40 +160,10 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg) ...@@ -147,40 +160,10 @@ void enum_interfaces_cb(struct nl_object *obj, void *arg)
iface->ifflags = rtnl_link_get_flags(link); iface->ifflags = rtnl_link_get_flags(link);
iface->operstate = rtnl_link_get_operstate(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); hostmngr_update_interface_bridgeinfo(iface);
if (br_ifindex > 0) { hostmngr_update_interface_ipaddrs(iface);
iface->is_brif = true; hostmngr_update_interface_mediatype(iface);
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;
}
}
list_add_tail(&iface->list, argp->iflist); list_add_tail(&iface->list, argp->iflist);
*argp->n += 1; *argp->n += 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment