- Fix some typos from my last commit
[reactos.git] / reactos / dll / win32 / iphlpapi / ipstats_reactos.c
index 14a3ab3..7fb7cbf 100644 (file)
@@ -19,7 +19,6 @@
  * tcpip.sys
  */
 
-#include <roscfg.h>
 #include <stdio.h>
 
 #include "iphlpapi_private.h"
@@ -63,30 +62,8 @@ WINE_DEFAULT_DEBUG_CHANNEL(iphlpapi);
 #endif
 
 BOOL isIpEntity( HANDLE tcpFile, TDIEntityID *ent ) {
-    DWORD entityType, returnedLen;
-    NTSTATUS status;
-    TCP_REQUEST_QUERY_INFORMATION_EX req;    
-
-    req.ID.toi_class = INFO_CLASS_GENERIC;
-    req.ID.toi_type = INFO_TYPE_PROVIDER;
-    req.ID.toi_id = ENTITY_TYPE_ID;
-    req.ID.toi_entity = *ent;
-
-    status = 
-        DeviceIoControl
-        ( tcpFile,
-          IOCTL_TCP_QUERY_INFORMATION_EX,
-          &req,
-          sizeof(req),
-          &entityType,
-          sizeof(entityType),
-          &returnedLen,
-          NULL );
-
-    TRACE("Ent: %04x:d -> %04x\n", 
-           ent->tei_entity, ent->tei_instance, entityType );
-
-    return NT_SUCCESS(status) && entityType == CL_NL_IP;
+    return (ent->tei_entity == CL_NL_ENTITY ||
+            ent->tei_entity == CO_NL_ENTITY);
 }
 
 NTSTATUS getNthIpEntity( HANDLE tcpFile, DWORD index, TDIEntityID *ent ) {
@@ -108,7 +85,7 @@ NTSTATUS getNthIpEntity( HANDLE tcpFile, DWORD index, TDIEntityID *ent ) {
     }
 
     if( numRoutes == index && i < numEntities ) {
-        TRACE("Index %d is entity #%d - %04x:%08x\n", index, i, 
+        TRACE("Index %d is entity #%d - %04x:%08x\n", index, i,
                entitySet[i].tei_entity, entitySet[i].tei_instance );
         memcpy( ent, &entitySet[i], sizeof(*ent) );
         tdiFreeThingSet( entitySet );
@@ -169,8 +146,8 @@ NTSTATUS tdiGetMibForIpEntity
            entry->ipsi_numaddr,
            entry->ipsi_numroutes,
            status);
-        
-    return status;    
+
+    return status;
 }
 
 NTSTATUS tdiGetRoutesForIpEntity
@@ -183,15 +160,15 @@ NTSTATUS tdiGetRoutesForIpEntity
     status = tdiGetSetOfThings( tcpFile,
                                 INFO_CLASS_PROTOCOL,
                                 INFO_TYPE_PROVIDER,
-                                IP_MIB_ROUTETABLE_ENTRY_ID,
-                                ent->tei_entity,
-                               0,
+                                IP_MIB_ARPTABLE_ENTRY_ID,
+                                CL_NL_ENTITY,
+                               ent->tei_instance,
                                 0,
                                 sizeof(IPRouteEntry),
                                 (PVOID *)routes,
                                 numRoutes);
 
-    return status;    
+    return status;
 }
 
 NTSTATUS tdiGetIpAddrsForIpEntity
@@ -201,18 +178,18 @@ NTSTATUS tdiGetIpAddrsForIpEntity
     TRACE("TdiGetIpAddrsForIpEntity(tcpFile %x,entityId %x)\n",
            (DWORD)tcpFile, ent->tei_instance);
 
-    status = tdiGetSetOfThings( tcpFile, 
-                                INFO_CLASS_PROTOCOL, 
+    status = tdiGetSetOfThings( tcpFile,
+                                INFO_CLASS_PROTOCOL,
                                 INFO_TYPE_PROVIDER,
                                 IP_MIB_ADDRTABLE_ENTRY_ID,
-                                ent->tei_entity,
-                               0,
+                                CL_NL_ENTITY,
+                               ent->tei_instance,
                                 0,
                                 sizeof(IPAddrEntry),
                                 (PVOID *)addrs,
                                 numAddrs );
 
-    return status;    
+    return status;
 }
 
 DWORD getInterfaceStatsByName(const char *name, PMIB_IFROW entry)
@@ -404,6 +381,7 @@ DWORD getNumRoutes(void)
 
     if( !NT_SUCCESS(status) ) {
         TRACE("failure: %08x\n", (int)status );
+        closeTcpFile( tcpFile );
         return 0;
     }
 
@@ -414,6 +392,7 @@ DWORD getNumRoutes(void)
             status = tdiGetMibForIpEntity( tcpFile, &entitySet[i], &isnmp );
             if( !NT_SUCCESS(status) ) {
                 tdiFreeThingSet( entitySet );
+                closeTcpFile( tcpFile );
                 return status;
             }
             numRoutes += isnmp.ipsi_numroutes;
@@ -444,7 +423,6 @@ RouteTable *getRouteTable(void)
 {
     RouteTable *out_route_table;
     DWORD numRoutes = getNumRoutes(), routesAdded = 0;
-    IPSNMPInfo snmpInfo;
     TDIEntityID ent;
     HANDLE tcpFile;
     NTSTATUS status = openTcpFile( &tcpFile );
@@ -455,39 +433,40 @@ RouteTable *getRouteTable(void)
 
     TRACE("GETTING ROUTE TABLE\n");
 
-    out_route_table = HeapAlloc( GetProcessHeap(), 0, 
-                                 sizeof(RouteTable) + 
+    out_route_table = HeapAlloc( GetProcessHeap(), 0,
+                                 sizeof(RouteTable) +
                                  (sizeof(RouteEntry) * (numRoutes - 1)) );
-    
+    if (!out_route_table) {
+        closeTcpFile(tcpFile);
+        return NULL;
+    }
+
     out_route_table->numRoutes = numRoutes;
 
-    for( i = 0; routesAdded < numRoutes; i++ ) {
+    for( i = 0; routesAdded < out_route_table->numRoutes; i++ ) {
         int j;
         IPRouteEntry *route_set;
 
         getNthIpEntity( tcpFile, i, &ent );
-        tdiGetMibForIpEntity( tcpFile, &ent, &snmpInfo );
-
-        TRACE( "%d routes in instance %d\n", snmpInfo.ipsi_numroutes, i );
 
         tdiGetRoutesForIpEntity( tcpFile, &ent, &route_set, &numRoutes );
-
+        
         if( !route_set ) {
             closeTcpFile( tcpFile );
             HeapFree( GetProcessHeap(), 0, out_route_table );
             return 0;
         }
 
-        TRACE("Route set returned\n");
+        TRACE( "%d routes in instance %d\n", numRoutes, i );
 #if 0
-        HexDump( route_set, 
-                 sizeof( IPRouteEntry ) * 
+        HexDump( route_set,
+                 sizeof( IPRouteEntry ) *
                  snmpInfo.ipsi_numroutes );
 #endif
 
-        for( j = 0; j < snmpInfo.ipsi_numroutes; j++ ) {
+        for( j = 0; j < numRoutes; j++ ) {
             int routeNum = j + routesAdded;
-            out_route_table->routes[routeNum].dest = 
+            out_route_table->routes[routeNum].dest =
                 route_set[j].ire_dest;
             out_route_table->routes[routeNum].mask =
                 route_set[j].ire_mask;
@@ -501,9 +480,11 @@ RouteTable *getRouteTable(void)
 
         if( route_set ) tdiFreeThingSet( route_set );
 
-        routesAdded += snmpInfo.ipsi_numroutes;
+        routesAdded += numRoutes;
     }
 
+    closeTcpFile( tcpFile );
+
     TRACE("Return: %08x, %08x\n", status, out_route_table);
 
     return out_route_table;
@@ -531,8 +512,9 @@ DWORD getNumArpEntries(void)
     status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
 
     for( i = 0; i < numEntities; i++ ) {
-        if( isInterface( &entitySet[i] ) && 
-           hasArp( tcpFile, &entitySet[i] ) ) {
+        if( isInterface( &entitySet[i] ) &&
+           hasArp( tcpFile, &entitySet[i] ) &&
+           !isLoopback( tcpFile, &entitySet[i] ) ) {
 
            status = tdiGetSetOfThings( tcpFile,
                                        INFO_CLASS_PROTOCOL,
@@ -546,7 +528,10 @@ DWORD getNumArpEntries(void)
                                        &returnSize );
 
            if( status == STATUS_SUCCESS ) totalNumber += returnSize;
-           if( IpArpTable ) tdiFreeThingSet( IpArpTable );
+               if( IpArpTable ) {
+                       tdiFreeThingSet( IpArpTable );
+                       IpArpTable = NULL;
+               }
        }
     }
 
@@ -561,7 +546,7 @@ PMIB_IPNETTABLE getArpTable(void)
     DWORD numEntities, returnSize;
     TDIEntityID *entitySet;
     HANDLE tcpFile;
-    int i, row = 0, totalNumber;
+    int i, totalNumber, TmpIdx, CurrIdx = 0;
     NTSTATUS status;
     PMIB_IPNETTABLE IpArpTable = NULL;
     PMIB_IPNETROW AdapterArpTable = NULL;
@@ -578,14 +563,19 @@ PMIB_IPNETTABLE getArpTable(void)
     }
 
     IpArpTable = HeapAlloc
-       ( GetProcessHeap(), 0, 
+       ( GetProcessHeap(), 0,
          sizeof(DWORD) + (sizeof(MIB_IPNETROW) * totalNumber) );
+    if (!IpArpTable) {
+        closeTcpFile(tcpFile);
+        return NULL;
+    }
 
     status = tdiGetEntityIDSet( tcpFile, &entitySet, &numEntities );
 
     for( i = 0; i < numEntities; i++ ) {
-        if( isIpEntity( tcpFile, &entitySet[i] ) && 
-           hasArp( tcpFile, &entitySet[i] ) ) {
+        if( isInterface( &entitySet[i] ) &&
+           hasArp( tcpFile, &entitySet[i] ) &&
+           !isLoopback( tcpFile, &entitySet[i] ) ) {
 
            status = tdiGetSetOfThings( tcpFile,
                                        INFO_CLASS_PROTOCOL,
@@ -599,16 +589,18 @@ PMIB_IPNETTABLE getArpTable(void)
                                        &returnSize );
 
            if( status == STATUS_SUCCESS ) {
-               for( row = 0; row < returnSize; row++ )
-                   IpArpTable->table[row] = AdapterArpTable[row];
+               for( TmpIdx = 0; TmpIdx < returnSize; TmpIdx++, CurrIdx++ )
+                   IpArpTable->table[CurrIdx] = AdapterArpTable[TmpIdx];
            }
 
            if( AdapterArpTable ) tdiFreeThingSet( AdapterArpTable );
        }
     }
 
+    closeTcpFile( tcpFile );
+
     tdiFreeThingSet( entitySet );
-    IpArpTable->dwNumEntries = row;
+    IpArpTable->dwNumEntries = CurrIdx;
 
     return IpArpTable;
 }