Changeset View
Changeset View
Standalone View
Standalone View
sbin/ifconfig/ifconfig_netlink.c
Show First 20 Lines • Show All 282 Lines • ▼ Show 20 Lines | |||||
sort_iface_ifaddrs(struct snl_state *ss, struct iface *iface) | sort_iface_ifaddrs(struct snl_state *ss, struct iface *iface) | ||||
{ | { | ||||
if (iface->ifa_count == 0) | if (iface->ifa_count == 0) | ||||
return; | return; | ||||
struct ifa **sorted_ifaddrs = snl_allocz(ss, iface->ifa_count * sizeof(void *)); | struct ifa **sorted_ifaddrs = snl_allocz(ss, iface->ifa_count * sizeof(void *)); | ||||
struct ifa *ifa = iface->ifa; | struct ifa *ifa = iface->ifa; | ||||
for (int i = 0; i < iface->ifa_count; i++) { | for (uint32_t i = 0; i < iface->ifa_count; i++) { | ||||
struct ifa *ifa_next = ifa->next; | struct ifa *ifa_next = ifa->next; | ||||
sorted_ifaddrs[i] = ifa; | sorted_ifaddrs[i] = ifa; | ||||
ifa->next = NULL; | ifa->next = NULL; | ||||
ifa = ifa_next; | ifa = ifa_next; | ||||
} | } | ||||
qsort(sorted_ifaddrs, iface->ifa_count, sizeof(void *), cmp_ifaddr); | qsort(sorted_ifaddrs, iface->ifa_count, sizeof(void *), cmp_ifaddr); | ||||
ifa = sorted_ifaddrs[0]; | ifa = sorted_ifaddrs[0]; | ||||
iface->ifa = ifa; | iface->ifa = ifa; | ||||
for (int i = 1; i < iface->ifa_count; i++) { | for (uint32_t i = 1; i < iface->ifa_count; i++) { | ||||
ifa->next = sorted_ifaddrs[i]; | ifa->next = sorted_ifaddrs[i]; | ||||
ifa = sorted_ifaddrs[i]; | ifa = sorted_ifaddrs[i]; | ||||
} | } | ||||
} | } | ||||
static void | static void | ||||
status_nl(struct ifconfig_args *args, struct io_handler *h, struct iface *iface) | status_nl(struct ifconfig_args *args, struct io_handler *h, struct iface *iface) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | get_local_socket(void) | ||||
if (s < 0) | if (s < 0) | ||||
err(1, "socket(family %u,SOCK_DGRAM)", AF_LOCAL); | err(1, "socket(family %u,SOCK_DGRAM)", AF_LOCAL); | ||||
return (s); | return (s); | ||||
} | } | ||||
static void | static void | ||||
set_global_ifname(if_link_t *link) | set_global_ifname(if_link_t *link) | ||||
{ | { | ||||
int iflen = strlcpy(name, link->ifla_ifname, sizeof(name)); | size_t iflen = strlcpy(name, link->ifla_ifname, sizeof(name)); | ||||
if (iflen >= sizeof(name)) | if (iflen >= sizeof(name)) | ||||
errx(1, "%s: cloning name too long", link->ifla_ifname); | errx(1, "%s: cloning name too long", link->ifla_ifname); | ||||
strlcpy(ifr.ifr_name, link->ifla_ifname, sizeof(ifr.ifr_name)); | strlcpy(ifr.ifr_name, link->ifla_ifname, sizeof(ifr.ifr_name)); | ||||
} | } | ||||
void | void | ||||
list_interfaces_nl(struct ifconfig_args *args) | list_interfaces_nl(struct ifconfig_args *args) | ||||
{ | { | ||||
struct snl_state ss = {}; | struct snl_state ss = {}; | ||||
nl_init_socket(&ss); | nl_init_socket(&ss); | ||||
struct ifmap *ifmap = prepare_ifmap(&ss); | struct ifmap *ifmap = prepare_ifmap(&ss); | ||||
struct iface **sorted_ifaces = snl_allocz(&ss, ifmap->count * sizeof(void *)); | struct iface **sorted_ifaces = snl_allocz(&ss, ifmap->count * sizeof(void *)); | ||||
for (int i = 0, num = 0; i < ifmap->size; i++) { | for (uint32_t i = 0, num = 0; i < ifmap->size; i++) { | ||||
if (ifmap->ifaces[i] != NULL) { | if (ifmap->ifaces[i] != NULL) { | ||||
sorted_ifaces[num++] = ifmap->ifaces[i]; | sorted_ifaces[num++] = ifmap->ifaces[i]; | ||||
if (num == ifmap->count) | if (num == ifmap->count) | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
qsort(sorted_ifaces, ifmap->count, sizeof(void *), cmp_iface); | qsort(sorted_ifaces, ifmap->count, sizeof(void *), cmp_iface); | ||||
prepare_ifaddrs(&ss, ifmap); | prepare_ifaddrs(&ss, ifmap); | ||||
struct io_handler h = { | struct io_handler h = { | ||||
.s = get_local_socket(), | .s = get_local_socket(), | ||||
.ss = &ss, | .ss = &ss, | ||||
}; | }; | ||||
for (int i = 0, num = 0; i < ifmap->count; i++) { | for (uint32_t i = 0, num = 0; i < ifmap->count; i++) { | ||||
struct iface *iface = sorted_ifaces[i]; | struct iface *iface = sorted_ifaces[i]; | ||||
if (!match_iface(args, iface)) | if (!match_iface(args, iface)) | ||||
continue; | continue; | ||||
set_global_ifname(&iface->link); | set_global_ifname(&iface->link); | ||||
if (args->namesonly) { | if (args->namesonly) { | ||||
Show All 15 Lines |