Changeset View
Changeset View
Standalone View
Standalone View
share/examples/libifconfig/status.c
Show First 20 Lines • Show All 400 Lines • ▼ Show 20 Lines | print_media(ifconfig_handle_t *lifh, struct ifaddrs *ifa) | ||||
* | * | ||||
* How to get the top-level interface subtype | * How to get the top-level interface subtype | ||||
* 1) Shift ifmw right by 0x20, index into ifmedia_types_to_subtypes | * 1) Shift ifmw right by 0x20, index into ifmedia_types_to_subtypes | ||||
* 2) Iterate through the resulting table's subtypes table, ignoring | * 2) Iterate through the resulting table's subtypes table, ignoring | ||||
* aliases. Iterate through the resulting ifmedia_description | * aliases. Iterate through the resulting ifmedia_description | ||||
* tables, finding an entry with the right media subtype | * tables, finding an entry with the right media subtype | ||||
*/ | */ | ||||
struct ifmediareq *ifmr; | struct ifmediareq *ifmr; | ||||
char opts[80]; | |||||
if (ifconfig_media_get_mediareq(lifh, ifa->ifa_name, &ifmr) != 0) { | if (ifconfig_media_get_mediareq(lifh, ifa->ifa_name, &ifmr) != 0) { | ||||
if (ifconfig_err_errtype(lifh) != OK) { | if (ifconfig_err_errtype(lifh) != OK) { | ||||
err(1, "Failed to get media info"); | err(1, "Failed to get media info"); | ||||
} else { | } else { | ||||
return; /* Interface doesn't support media info */ | return; /* Interface doesn't support media info */ | ||||
} | } | ||||
} | } | ||||
printf("\tmedia: %s %s", ifconfig_media_get_type(ifmr->ifm_current), | printf("\tmedia: %s %s", ifconfig_media_get_type(ifmr->ifm_current), | ||||
ifconfig_media_get_subtype(ifmr->ifm_current)); | ifconfig_media_get_subtype(ifmr->ifm_current)); | ||||
if (ifmr->ifm_active != ifmr->ifm_current) { | if (ifmr->ifm_active != ifmr->ifm_current) { | ||||
const char **options; | |||||
printf(" (%s", ifconfig_media_get_subtype(ifmr->ifm_active)); | printf(" (%s", ifconfig_media_get_subtype(ifmr->ifm_active)); | ||||
ifconfig_media_get_options_string(ifmr->ifm_active, opts, | options = ifconfig_media_get_options(ifmr->ifm_active); | ||||
sizeof(opts)); | if (options != NULL && options[0] != NULL) { | ||||
if (opts[0] != '\0') { | printf(" <%s", options[0]); | ||||
printf(" <%s>)\n", opts); | for (size_t i = 1; options[i] != NULL; ++i) | ||||
printf(",%s", options[i]); | |||||
printf(">)\n"); | |||||
} else { | } else { | ||||
printf(")\n"); | printf(")\n"); | ||||
} | } | ||||
free(options); | |||||
} else { | } else { | ||||
printf("\n"); | printf("\n"); | ||||
} | } | ||||
if (ifmr->ifm_status & IFM_AVALID) { | if (ifmr->ifm_status & IFM_AVALID) { | ||||
printf("\tstatus: %s\n", | printf("\tstatus: %s\n", | ||||
ifconfig_media_get_status(ifmr)); | ifconfig_media_get_status(ifmr)); | ||||
} | } | ||||
printf("\tsupported media:\n"); | printf("\tsupported media:\n"); | ||||
for (i = 0; i < ifmr->ifm_count; i++) { | for (i = 0; i < ifmr->ifm_count; i++) { | ||||
const char **options; | |||||
printf("\t\tmedia %s", | printf("\t\tmedia %s", | ||||
ifconfig_media_get_subtype(ifmr->ifm_ulist[i])); | ifconfig_media_get_subtype(ifmr->ifm_ulist[i])); | ||||
ifconfig_media_get_options_string(ifmr->ifm_ulist[i], opts, | options = ifconfig_media_get_options(ifmr->ifm_ulist[i]); | ||||
sizeof(opts)); | if (options != NULL && options[0] != NULL) { | ||||
if (opts[0] != '\0') { | printf(" mediaopt %s", options[0]); | ||||
printf(" mediaopt %s\n", opts); | for (size_t i = 1; options[i] != NULL; ++i) | ||||
printf(",%s", options[i]); | |||||
printf("\n"); | |||||
} else { | } else { | ||||
printf("\n"); | printf("\n"); | ||||
} | } | ||||
free(options); | |||||
} | } | ||||
free(ifmr); | free(ifmr); | ||||
} | } | ||||
static void | static void | ||||
print_iface(ifconfig_handle_t *lifh, struct ifaddrs *ifa, void *udata __unused) | print_iface(ifconfig_handle_t *lifh, struct ifaddrs *ifa, void *udata __unused) | ||||
{ | { | ||||
int metric, mtu; | int metric, mtu; | ||||
▲ Show 20 Lines • Show All 73 Lines • Show Last 20 Lines |