From 7432f4e9d959aef63f10a7f4aa9ebd56b1403f6a Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Sat, 16 Aug 2025 15:55:54 +0200 Subject: [PATCH] [ROUTE] Improvements to the ROUTE command - Show the physical address for ethernet interfaces. - Implement wildcard support for the print command (e.g. route print 192.168.*). --- base/applications/network/route/lang/en-US.rc | 1 + base/applications/network/route/resource.h | 13 +-- base/applications/network/route/route.c | 87 ++++++++++++++++--- 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/base/applications/network/route/lang/en-US.rc b/base/applications/network/route/lang/en-US.rc index 3629d3947e2..e01e6e398fd 100644 --- a/base/applications/network/route/lang/en-US.rc +++ b/base/applications/network/route/lang/en-US.rc @@ -39,6 +39,7 @@ route delete \n\ IDS_SEPARATOR "===========================================================================\n" IDS_INTERFACE_LIST "Interface List\n" IDS_INTERFACE_ENTRY "0x%lu ........................... %hs\n" + IDS_ETHERNET_ENTRY "0x%lu ...%02x %02x %02x %02x %02x %02x ...... %hs\n" IDS_IPV4_ROUTE_TABLE "\nIPv4 Route Table\n" IDS_ACTIVE_ROUTES "Active Routes:\n" IDS_ROUTES_ENTRY "%17s%17s%17s%16ld%9ld\n" diff --git a/base/applications/network/route/resource.h b/base/applications/network/route/resource.h index 646264a7467..d9d47fad518 100644 --- a/base/applications/network/route/resource.h +++ b/base/applications/network/route/resource.h @@ -9,10 +9,11 @@ #define IDS_SEPARATOR 106 #define IDS_INTERFACE_LIST 107 #define IDS_INTERFACE_ENTRY 108 -#define IDS_IPV4_ROUTE_TABLE 109 -#define IDS_ACTIVE_ROUTES 110 -#define IDS_PERSISTENT_ROUTES 111 -#define IDS_ROUTES_ENTRY 112 -#define IDS_DEFAULT_GATEWAY 113 -#define IDS_ROUTES_HEADER 114 +#define IDS_ETHERNET_ENTRY 109 +#define IDS_IPV4_ROUTE_TABLE 110 +#define IDS_ACTIVE_ROUTES 111 +#define IDS_PERSISTENT_ROUTES 112 +#define IDS_ROUTES_ENTRY 113 +#define IDS_DEFAULT_GATEWAY 114 +#define IDS_ROUTES_HEADER 115 #define IDS_NONE 120 diff --git a/base/applications/network/route/route.c b/base/applications/network/route/route.c index c9a660b690c..9d942d95b88 100644 --- a/base/applications/network/route/route.c +++ b/base/applications/network/route/route.c @@ -34,7 +34,56 @@ static int Usage() return 1; } -static int PrintRoutes() +static +BOOL +MatchWildcard( + _In_ PWSTR Text, + _In_ PWSTR Pattern) +{ + size_t TextLength, PatternLength, TextIndex = 0, PatternIndex = 0; + size_t StartIndex = -1, MatchIndex = 0; + + TextLength = wcslen(Text); + PatternLength = wcslen(Pattern); + + while (TextIndex < TextLength) + { + if ((PatternIndex < PatternLength) && + ((Pattern[PatternIndex] == L'?') || + (towlower(Pattern[PatternIndex]) == towlower(Text[TextIndex])))) + { + TextIndex++; + PatternIndex++; + } + else if ((PatternIndex < PatternLength) && + (Pattern[PatternIndex] == L'*')) + { + StartIndex = PatternIndex; + MatchIndex = TextIndex; + PatternIndex++; + } + else if (StartIndex != -1) + { + PatternIndex = StartIndex + 1; + MatchIndex++; + TextIndex = MatchIndex; + } + else + { + return FALSE; + } + } + + while ((PatternIndex < PatternLength) && + (Pattern[PatternIndex] == L'*')) + { + PatternIndex++; + } + + return (PatternIndex == PatternLength); +} + +static int PrintRoutes(PWSTR Filter) { PMIB_IPFORWARDTABLE IpForwardTable = NULL; PIP_ADAPTER_INFO pAdapterInfo = NULL; @@ -44,6 +93,7 @@ static int PrintRoutes() WCHAR DefGate[16]; WCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF]; unsigned int i; + BOOL EntriesFound; /* set required buffer size */ pAdapterInfo = (IP_ADAPTER_INFO *) malloc( adaptOutBufLen ); @@ -82,7 +132,12 @@ static int PrintRoutes() /* FIXME - sort by the index! */ while (pAdapterInfo) { - ConResPrintf(StdOut, IDS_INTERFACE_ENTRY, pAdapterInfo->Index, pAdapterInfo->Description); + if (pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) + ConResPrintf(StdOut, IDS_ETHERNET_ENTRY, pAdapterInfo->Index, pAdapterInfo->Address[0], + pAdapterInfo->Address[1], pAdapterInfo->Address[2], pAdapterInfo->Address[3], + pAdapterInfo->Address[4], pAdapterInfo->Address[5], pAdapterInfo->Description); + else + ConResPrintf(StdOut, IDS_INTERFACE_ENTRY, pAdapterInfo->Index, pAdapterInfo->Description); pAdapterInfo = pAdapterInfo->Next; } ConResPrintf(StdOut, IDS_SEPARATOR); @@ -90,21 +145,31 @@ static int PrintRoutes() ConResPrintf(StdOut, IDS_IPV4_ROUTE_TABLE); ConResPrintf(StdOut, IDS_SEPARATOR); ConResPrintf(StdOut, IDS_ACTIVE_ROUTES); - ConResPrintf(StdOut, IDS_ROUTES_HEADER); + EntriesFound = FALSE; for( i = 0; i < IpForwardTable->dwNumEntries; i++ ) { mbstowcs(Destination, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest)), IPBUF); mbstowcs(Netmask, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask)), IPBUF); mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop)), IPBUF); - ConResPrintf(StdOut, IDS_ROUTES_ENTRY, - Destination, - Netmask, - Gateway, - IpForwardTable->table[i].dwForwardIfIndex, - IpForwardTable->table[i].dwForwardMetric1); + if ((Filter == NULL) || MatchWildcard(Destination, Filter)) + { + if (EntriesFound == FALSE) + ConResPrintf(StdOut, IDS_ROUTES_HEADER); + ConResPrintf(StdOut, IDS_ROUTES_ENTRY, + Destination, + Netmask, + Gateway, + IpForwardTable->table[i].dwForwardIfIndex, + IpForwardTable->table[i].dwForwardMetric1); + EntriesFound = TRUE; + } } - ConResPrintf(StdOut, IDS_DEFAULT_GATEWAY, DefGate); + + if (Filter == NULL) + ConResPrintf(StdOut, IDS_DEFAULT_GATEWAY, DefGate); + else if (EntriesFound == FALSE) + ConResPrintf(StdOut, IDS_NONE); ConResPrintf(StdOut, IDS_SEPARATOR); ConResPrintf(StdOut, IDS_PERSISTENT_ROUTES); @@ -206,7 +271,7 @@ int wmain( int argc, WCHAR **argv ) if( argc < 2 ) return Usage(); else if ( !_wcsicmp( argv[1], L"print" ) ) - return PrintRoutes(); + return PrintRoutes((argc > 2) ? argv[2] : NULL); else if( !_wcsicmp( argv[1], L"add" ) ) return add_route( argc-2, argv+2 ); else if( !_wcsicmp( argv[1], L"delete" ) )