From 46c2a3cd0903a30be1b034e71873e9e7cec659a7 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Sun, 17 Aug 2025 21:02:00 +0200 Subject: [PATCH] [ROUTE] Some improvements - Improvements to the usage text - Use GetAdapterAdresses instead of GetAdaperInfo - Show the physical address for ethernet adapters --- base/applications/network/route/lang/en-US.rc | 32 ++++---- base/applications/network/route/resource.h | 6 +- base/applications/network/route/route.c | 73 ++++++++++++------- 3 files changed, 70 insertions(+), 41 deletions(-) diff --git a/base/applications/network/route/lang/en-US.rc b/base/applications/network/route/lang/en-US.rc index e01e6e398fd..8257a1bcfed 100644 --- a/base/applications/network/route/lang/en-US.rc +++ b/base/applications/network/route/lang/en-US.rc @@ -8,16 +8,22 @@ LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US STRINGTABLE BEGIN - IDS_USAGE "\n\ -route usage:\n\ -route print\n\ - prints the route table\n\ -route add [mask ] [metric ]\n\ - adds a route\n\ -route delete \n\ - deletes a route\n" + IDS_USAGE1 "\n\ +Manipulates the network routing tables.\n\n\ +ROUTE [command] [destination]\n\ + [MASK netmask] [gateway] [METRIC metric]\n\n\ + command One of the following options:\n\ + PRINT Prints the routes.\n\ + ADD Add a new route.\n\ + DELETE Deletes a route.\n\ + destination Specifies the Host.\n\ + netmask Specifies a network mask value for thei route entry.\n\ + If not specified, it defaults to 255.255.255.255.\n\ + gateway Specifies the Gateway.\n\ + metric Specifies the Metric, ie. cost for the destination.\n\ + interface Specifies the interface number for the specifies route.\n" - IDS_ROUTE_ADD_HELP "\n\ + IDS_USAGE2 "\n\ route add usage:\n\ route add [mask ] [metric ]\n\ Adds a route to the IP route table.\n\ @@ -26,7 +32,7 @@ route add [mask ] [metric ]\n\ is the gateway to use to access the network\n\ is the metric to use (lower is preferred)\n" - IDS_ROUTE_DEL_HELP "\n\ + IDS_USAGE3 "\n\ route delete usage:\n\ route delete \n\ Removes a route from the IP route table.\n\ @@ -38,11 +44,11 @@ route delete \n\ IDS_ROUTE_ENUM_ERROR "Route enumeration failed\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_INTERFACE_ENTRY "0x%-2lx ........................... %s\n" + IDS_ETHERNET_ENTRY "0x%-2lx ...%s...... %s\n" IDS_IPV4_ROUTE_TABLE "\nIPv4 Route Table\n" IDS_ACTIVE_ROUTES "Active Routes:\n" - IDS_ROUTES_ENTRY "%17s%17s%17s%16ld%9ld\n" + IDS_ROUTES_ENTRY "%17s%17s%17s%16ld%8ld\n" IDS_PERSISTENT_ROUTES "Persistent Routes:\n" IDS_DEFAULT_GATEWAY "Default Gateway:%18s\n" IDS_ROUTES_HEADER "Network Destination Netmask Gateway Interface Metric\n" diff --git a/base/applications/network/route/resource.h b/base/applications/network/route/resource.h index d9d47fad518..e4805d7fbf2 100644 --- a/base/applications/network/route/resource.h +++ b/base/applications/network/route/resource.h @@ -1,8 +1,8 @@ #pragma once -#define IDS_USAGE 100 -#define IDS_ROUTE_ADD_HELP 101 -#define IDS_ROUTE_DEL_HELP 102 +#define IDS_USAGE1 100 +#define IDS_USAGE2 101 +#define IDS_USAGE3 102 #define IDS_ROUTE_ADD_ERROR 103 #define IDS_ROUTE_DEL_ERROR 104 #define IDS_ROUTE_ENUM_ERROR 105 diff --git a/base/applications/network/route/route.c b/base/applications/network/route/route.c index 9d942d95b88..f0e44e58b97 100644 --- a/base/applications/network/route/route.c +++ b/base/applications/network/route/route.c @@ -30,7 +30,7 @@ static int Usage() { - ConResPrintf(StdErr, IDS_USAGE); + ConResPrintf(StdErr, IDS_USAGE1); return 1; } @@ -83,30 +83,42 @@ MatchWildcard( return (PatternIndex == PatternLength); } +static +VOID +PrintMacAddress( + PBYTE Mac, + PWSTR Buffer) +{ + swprintf(Buffer, L"%02X %02X %02X %02X %02X %02X ", + Mac[0], Mac[1], Mac[2], Mac[3], Mac[4], Mac[5]); +} + static int PrintRoutes(PWSTR Filter) { PMIB_IPFORWARDTABLE IpForwardTable = NULL; - PIP_ADAPTER_INFO pAdapterInfo = NULL; + PIP_ADAPTER_ADDRESSES pAdapterAddresses = NULL; ULONG Size = 0; DWORD Error = 0; - ULONG adaptOutBufLen = sizeof(IP_ADAPTER_INFO); - WCHAR DefGate[16]; + ULONG adaptOutBufLen = 15000; WCHAR Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF]; unsigned int i; BOOL EntriesFound; + ULONG Flags = GAA_FLAG_SKIP_UNICAST | GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | + GAA_FLAG_SKIP_DNS_SERVER; /* set required buffer size */ - pAdapterInfo = (IP_ADAPTER_INFO *) malloc( adaptOutBufLen ); - if (pAdapterInfo == NULL) + pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen); + if (pAdapterAddresses == NULL) { Error = ERROR_NOT_ENOUGH_MEMORY; goto Error; } - if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW) + + if (GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen) == ERROR_BUFFER_OVERFLOW) { - free (pAdapterInfo); - pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen); - if (pAdapterInfo == NULL) + free (pAdapterAddresses); + pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)malloc(adaptOutBufLen); + if (pAdapterAddresses == NULL) { Error = ERROR_NOT_ENOUGH_MEMORY; goto Error; @@ -122,23 +134,25 @@ static int PrintRoutes(PWSTR Filter) } } - if (((Error = GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen)) == NO_ERROR) && + if (((Error = GetAdaptersAddresses(AF_INET, Flags, NULL, pAdapterAddresses, &adaptOutBufLen)) == NO_ERROR) && ((Error = GetIpForwardTable(IpForwardTable, &Size, TRUE)) == NO_ERROR)) { - mbstowcs(DefGate, pAdapterInfo->GatewayList.IpAddress.String, 16); - ConResPrintf(StdOut, IDS_SEPARATOR); ConResPrintf(StdOut, IDS_INTERFACE_LIST); /* FIXME - sort by the index! */ - while (pAdapterInfo) + while (pAdapterAddresses) { - 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); + if (pAdapterAddresses->IfType == IF_TYPE_ETHERNET_CSMACD) + { + WCHAR PhysicalAddress[20]; + PrintMacAddress(pAdapterAddresses->PhysicalAddress, PhysicalAddress); + ConResPrintf(StdOut, IDS_ETHERNET_ENTRY, pAdapterAddresses->IfIndex, PhysicalAddress, pAdapterAddresses->Description); + } else - ConResPrintf(StdOut, IDS_INTERFACE_ENTRY, pAdapterInfo->Index, pAdapterInfo->Description); - pAdapterInfo = pAdapterInfo->Next; + { + ConResPrintf(StdOut, IDS_INTERFACE_ENTRY, pAdapterAddresses->IfIndex, pAdapterAddresses->Description); + } + pAdapterAddresses = pAdapterAddresses->Next; } ConResPrintf(StdOut, IDS_SEPARATOR); @@ -167,7 +181,16 @@ static int PrintRoutes(PWSTR Filter) } if (Filter == NULL) - ConResPrintf(StdOut, IDS_DEFAULT_GATEWAY, DefGate); + { + for( i = 0; i < IpForwardTable->dwNumEntries; i++ ) + { + if (IpForwardTable->table[i].dwForwardDest == 0) + { + mbstowcs(Gateway, inet_ntoa(IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop)), IPBUF); + ConResPrintf(StdOut, IDS_DEFAULT_GATEWAY, Gateway); + } + } + } else if (EntriesFound == FALSE) ConResPrintf(StdOut, IDS_NONE); ConResPrintf(StdOut, IDS_SEPARATOR); @@ -176,14 +199,14 @@ static int PrintRoutes(PWSTR Filter) ConResPrintf(StdOut, IDS_NONE); free(IpForwardTable); - free(pAdapterInfo); + free(pAdapterAddresses); return ERROR_SUCCESS; } else { Error: - if (pAdapterInfo) free(pAdapterInfo); + if (pAdapterAddresses) free(pAdapterAddresses); if (IpForwardTable) free(IpForwardTable); ConResPrintf(StdErr, IDS_ROUTE_ENUM_ERROR); return Error; @@ -234,7 +257,7 @@ static int add_route( int argc, WCHAR **argv ) { if( argc < 2 || !convert_add_cmd_line( &RowToAdd, argc, argv ) ) { - ConResPrintf(StdErr, IDS_ROUTE_ADD_HELP); + ConResPrintf(StdErr, IDS_USAGE2); return 1; } @@ -252,7 +275,7 @@ static int del_route( int argc, WCHAR **argv ) if( argc < 2 || !convert_add_cmd_line( &RowToDel, argc, argv ) ) { - ConResPrintf(StdErr, IDS_ROUTE_ADD_HELP); + ConResPrintf(StdErr, IDS_USAGE3); return 1; }