- char Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF],
- Index[IPBUF], Metric[IPBUF];
- int i;
-
- if( (Error = GetIpForwardTable( NULL, &Size, TRUE )) ==
- ERROR_INSUFFICIENT_BUFFER ) {
- IpForwardTable = malloc( Size );
- Error = GetIpForwardTable( IpForwardTable, &Size, TRUE );
+ ULONG adaptOutBufLen;
+ TCHAR DefGate[16];
+ char Destination[IPBUF], Gateway[IPBUF], Netmask[IPBUF];
+ unsigned int i;
+
+ /* set required buffer size */
+ pAdapterInfo = (IP_ADAPTER_INFO *) malloc( sizeof(IP_ADAPTER_INFO) );
+ if (GetAdaptersInfo( pAdapterInfo, &adaptOutBufLen) == ERROR_INSUFFICIENT_BUFFER)
+ pAdapterInfo = (IP_ADAPTER_INFO *) malloc (adaptOutBufLen);
+
+ if( (GetIpForwardTable( NULL, &Size, TRUE )) == ERROR_INSUFFICIENT_BUFFER )
+ IpForwardTable = malloc( Size );
+
+ if ((GetAdaptersInfo(pAdapterInfo, &adaptOutBufLen) == NO_ERROR) &&
+ GetIpForwardTable( IpForwardTable, &Size, TRUE ) == NO_ERROR)
+ {
+ _tcsncpy(DefGate, pAdapterInfo->GatewayList.IpAddress.String, 16);
+ _tprintf(_T("===========================================================================\n"));
+ _tprintf(_T("Interface List\n"));
+ while (pAdapterInfo)
+ {
+ _tprintf(_T("0x%lu ........................... %s\n"), pAdapterInfo->Index, pAdapterInfo->Description);
+ pAdapterInfo = pAdapterInfo->Next;
+ }
+ _tprintf(_T("===========================================================================\n"));
+
+ _tprintf(_T("===========================================================================\n"));
+ _tprintf(_T("Active Routes:\n"));
+ printf( "%-27s%-17s%-14s%-11s%-10s\n",
+ "Network Destination",
+ "Netmask",
+ "Gateway",
+ "Interface",
+ "Metric" );
+ for( i = 0; i < IpForwardTable->dwNumEntries; i++ )
+ {
+ strcpy( Destination, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardDest) ) );
+ strcpy( Netmask, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardMask) ) );
+ strcpy( Gateway, inet_ntoa( IN_ADDR_OF(IpForwardTable->table[i].dwForwardNextHop) ) );
+
+ printf( "%17s%17s%17s%16ld%9ld\n",
+ Destination,
+ Netmask,
+ Gateway,
+ IpForwardTable->table[i].dwForwardIfIndex,
+ IpForwardTable->table[i].dwForwardMetric1 );
+ }
+ _tprintf(_T("Default Gateway:%18s\n"), DefGate);
+ _tprintf(_T("===========================================================================\n"));
+ _tprintf(_T("Persistent Routes:\n"));
+
+ free( IpForwardTable );
+
+ return ERROR_SUCCESS;