diff --git a/src/bin/network/ifconfig/Jamfile b/src/bin/network/ifconfig/Jamfile index a9dabe9cfe..14fa7bb557 100644 --- a/src/bin/network/ifconfig/Jamfile +++ b/src/bin/network/ifconfig/Jamfile @@ -4,5 +4,6 @@ UsePrivateHeaders net ; BinCommand ifconfig : ifconfig.cpp + MediaTypes.cpp : be network bnetapi ; diff --git a/src/bin/network/ifconfig/MediaTypes.cpp b/src/bin/network/ifconfig/MediaTypes.cpp new file mode 100644 index 0000000000..19efc3ef45 --- /dev/null +++ b/src/bin/network/ifconfig/MediaTypes.cpp @@ -0,0 +1,139 @@ +/* + * Copyright 2006-2015, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Axel Dörfler, axeld@pinc-software.de + * Oliver Tappe, zooey@hirschkaefer.de + * Atis Elsts, the.kfx@gmail.com + */ + + +#include "MediaTypes.h" + +#include + + +struct media_type { + int type; + const char* name; + const char* pretty; + struct { + int subtype; + const char* name; + const char* pretty; + } subtypes [6]; + struct { + int option; + bool read_only; + const char* name; + const char* pretty; + } options [6]; +}; + + +const media_type kMediaTypes[] = { + { + 0, // for generic options + "all", + "All", + { + { IFM_AUTO, "auto", "Auto-select" }, + { -1, NULL, NULL } + }, + { + { IFM_FULL_DUPLEX, true, "fullduplex", "Full Duplex" }, + { IFM_HALF_DUPLEX, true, "halfduplex", "Half Duplex" }, + { IFM_LOOP, true, "loop", "Loop" }, + //{ IFM_ACTIVE, false, "active", "Active" }, + { -1, false, NULL, NULL } + } + }, + { + IFM_ETHER, + "ether", + "Ethernet", + { + //{ IFM_AUTO, "auto", "Auto-select" }, + //{ IFM_AUI, "AUI", "10 MBit, AUI" }, + //{ IFM_10_2, "10base2", "10 MBit, 10BASE-2" }, + { IFM_10_T, "10baseT", "10 MBit, 10BASE-T" }, + { IFM_100_TX, "100baseTX", "100 MBit, 100BASE-TX" }, + { IFM_1000_T, "1000baseT", "1 GBit, 1000BASE-T" }, + { IFM_1000_SX, "1000baseSX", "1 GBit, 1000BASE-SX" }, + { IFM_10G_T, "10GbaseT", "10 GBit, 10GBASE-T" }, + { -1, NULL, NULL } + }, + { + { -1, false, NULL, NULL } + } + }, + { -1, NULL, NULL, {{ -1, NULL, NULL }}, {{ -1, false, NULL, NULL }} } +}; + + +const char* +get_media_type_name(size_t index) +{ + if (index < sizeof(kMediaTypes) / sizeof(kMediaTypes[0])) + return kMediaTypes[index].pretty; + + return NULL; +} + + +const char* +get_media_subtype_name(size_t typeIndex, size_t subIndex) +{ + if (typeIndex < sizeof(kMediaTypes) / sizeof(kMediaTypes[0])) { + if (kMediaTypes[typeIndex].subtypes[subIndex].subtype >= 0) + return kMediaTypes[typeIndex].subtypes[subIndex].name; + } + + return NULL; +} + + +bool +media_parse_subtype(const char* string, int media, int* type) +{ + for (size_t i = 0; kMediaTypes[i].type >= 0; i++) { + // only check for generic or correct subtypes + if (kMediaTypes[i].type && + kMediaTypes[i].type != media) + continue; + for (size_t j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) { + if (strcmp(kMediaTypes[i].subtypes[j].name, string) == 0) { + // found a match + *type = kMediaTypes[i].subtypes[j].subtype; + return true; + } + } + } + return false; +} + + +const char* +media_type_to_string(int media) +{ + for (size_t i = 0; kMediaTypes[i].type >= 0; i++) { + // loopback doesn't really have a media anyway + if (IFM_TYPE(media) == 0) + break; + + // only check for generic or correct subtypes + if (kMediaTypes[i].type + && kMediaTypes[i].type != IFM_TYPE(media)) + continue; + + for (size_t j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) { + if (kMediaTypes[i].subtypes[j].subtype == IFM_SUBTYPE(media)) { + // found a match + return kMediaTypes[i].subtypes[j].pretty; + } + } + } + + return NULL; +} diff --git a/src/bin/network/ifconfig/MediaTypes.h b/src/bin/network/ifconfig/MediaTypes.h new file mode 100644 index 0000000000..d5768e3ff8 --- /dev/null +++ b/src/bin/network/ifconfig/MediaTypes.h @@ -0,0 +1,23 @@ +/* + * Copyright 2006-2015, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Axel Dörfler, axeld@pinc-software.de + * Oliver Tappe, zooey@hirschkaefer.de + * Atis Elsts, the.kfx@gmail.com + */ +#ifndef MEDIA_TYPES_H +#define MEDIA_TYPES_H + + +#include + + +const char* get_media_type_name(size_t index); +const char* get_media_subtype_name(size_t typeIndex, size_t subIndex); +bool media_parse_subtype(const char* string, int media, int* type); +const char* media_type_to_string(int media); + + +#endif // MEDIA_TYPES_H diff --git a/src/bin/network/ifconfig/ifconfig.cpp b/src/bin/network/ifconfig/ifconfig.cpp index b9c481dbc2..05ace35abc 100644 --- a/src/bin/network/ifconfig/ifconfig.cpp +++ b/src/bin/network/ifconfig/ifconfig.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2006-2010, Haiku, Inc. All Rights Reserved. + * Copyright 2006-2015, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -25,6 +25,8 @@ #include +#include "MediaTypes.h" + extern const char* __progname; const char* kProgramName = __progname; @@ -35,6 +37,7 @@ enum preferred_output_format { PREFER_OUTPUT_PREFIX_LENGTH, }; + struct address_family { int family; const char* name; @@ -42,23 +45,6 @@ struct address_family { preferred_output_format preferred_format; }; -struct media_type { - int type; - const char* name; - const char* pretty; - struct { - int subtype; - const char* name; - const char* pretty; - } subtypes [6]; - struct { - int option; - bool read_only; - const char* name; - const char* pretty; - } options [6]; -}; - static const address_family kFamilies[] = { { @@ -76,45 +62,6 @@ static const address_family kFamilies[] = { { -1, NULL, {NULL}, PREFER_OUTPUT_MASK } }; -static const media_type kMediaTypes[] = { - { - 0, // for generic options - "all", - "All", - { - { IFM_AUTO, "auto", "Auto-select" }, - { -1, NULL, NULL } - }, - { - { IFM_FULL_DUPLEX, true, "fullduplex", "Full Duplex" }, - { IFM_HALF_DUPLEX, true, "halfduplex", "Half Duplex" }, - { IFM_LOOP, true, "loop", "Loop" }, - //{ IFM_ACTIVE, false, "active", "Active" }, - { -1, false, NULL, NULL } - } - }, - { - IFM_ETHER, - "ether", - "Ethernet", - { - //{ IFM_AUTO, "auto", "Auto-select" }, - //{ IFM_AUI, "AUI", "10 MBit, AUI" }, - //{ IFM_10_2, "10base2", "10 MBit, 10BASE-2" }, - { IFM_10_T, "10baseT", "10 MBit, 10BASE-T" }, - { IFM_100_TX, "100baseTX", "100 MBit, 100BASE-TX" }, - { IFM_1000_T, "1000baseT", "1 GBit, 1000BASE-T" }, - { IFM_1000_SX, "1000baseSX", "1 GBit, 1000BASE-SX" }, - { IFM_10G_T, "10GbaseT", "10 GBit, 10GBASE-T" }, - { -1, NULL, NULL } - }, - { - { -1, false, NULL, NULL } - } - }, - { -1, NULL, NULL, {{ -1, NULL, NULL }}, {{ -1, false, NULL, NULL }} } -}; - static void usage(int status) @@ -134,10 +81,12 @@ usage(int status) " media - media type to use (defaults to auto)\n", kProgramName, kProgramName, kProgramName); - for (int32 i = 0; kMediaTypes[i].type >= 0; i++) { - printf("For %s can be one of: ", kMediaTypes[i].pretty); - for (int32 j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) - printf("%s ", kMediaTypes[i].subtypes[j].name); + for (int32 i = 0; const char* type = get_media_type_name(i); i++) { + printf("For %s can be one of: ", type); + for (int32 j = 0; const char* subType = get_media_subtype_name(i, j); + j++) { + printf("%s ", subType); + } printf("\n"); } printf("And can be: up, down, [-]promisc, [-]allmulti, [-]bcast, " @@ -152,26 +101,6 @@ usage(int status) } -static bool -media_parse_subtype(const char* string, int media, int* type) -{ - for (int32 i = 0; kMediaTypes[i].type >= 0; i++) { - // only check for generic or correct subtypes - if (kMediaTypes[i].type && - kMediaTypes[i].type != media) - continue; - for (int32 j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) { - if (strcmp(kMediaTypes[i].subtypes[j].name, string) == 0) { - // found a match - *type = kMediaTypes[i].subtypes[j].subtype; - return true; - } - } - } - return false; -} - - int get_address_family(const char* argument) { @@ -541,30 +470,8 @@ list_interface(const char* name) int media = interface.Media(); if ((media & IFM_ACTIVE) != 0) { // dump media state in case we're linked - const char* type = "unknown"; - bool show = false; - - for (int32 i = 0; kMediaTypes[i].type >= 0; i++) { - // loopback don't really have a media anyway - if (IFM_TYPE(media) == 0/*IFT_LOOP*/) - break; - - // only check for generic or correct subtypes - if (kMediaTypes[i].type - && kMediaTypes[i].type != IFM_TYPE(media)) - continue; - - for (int32 j = 0; kMediaTypes[i].subtypes[j].subtype >= 0; j++) { - if (kMediaTypes[i].subtypes[j].subtype == IFM_SUBTYPE(media)) { - // found a match - type = kMediaTypes[i].subtypes[j].pretty; - show = true; - break; - } - } - } - - if (show) + const char* type = media_type_to_string(media); + if (type != NULL) printf("\tMedia type: %s\n", type); }