⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tdiqhelp.c

📁 网络驱动开发
💻 C
📖 第 1 页 / 共 2 页
字号:
int
TDIQ_GetIPSNMPInfo(
   IPSNMPInfo  *pIPSnmpInfo,
   PULONG      pInfoSize
   )
{
   int   result = ERROR_SUCCESS;
   TCP_REQUEST_QUERY_INFORMATION_EX tcpRequestQueryInfoEx;
   ULONG tcpRequestBufSize = sizeof( TCP_REQUEST_QUERY_INFORMATION_EX );

   memset(&tcpRequestQueryInfoEx, 0, sizeof(tcpRequestQueryInfoEx));

   tcpRequestQueryInfoEx.ID.toi_entity.tei_entity = CL_NL_ENTITY;
   tcpRequestQueryInfoEx.ID.toi_entity.tei_instance = 0;

   tcpRequestQueryInfoEx.ID.toi_class = INFO_CLASS_PROTOCOL;
   tcpRequestQueryInfoEx.ID.toi_type = INFO_TYPE_PROVIDER;
   tcpRequestQueryInfoEx.ID.toi_id = IP_MIB_STATS_ID;

   result = WsControl(
               IPPROTO_TCP,
               WSCNTL_TCPIP_QUERY_INFO,
               &tcpRequestQueryInfoEx,
               &tcpRequestBufSize,
               pIPSnmpInfo,
               pInfoSize
               );

   if( result )
   {
      fprintf(stderr, "%s(%d) TdiQueryDeviceControl failed (%ld)\n",
         __FILE__,
         __LINE__,
         result
         );

      return( result );
   }

   return( result );
}


int
TDIQ_GetIFEntryForIFIndex(
   ULONG nIFIndex,
   PUCHAR pQueryBuffer,
   PULONG pQueryBufferSize // IFEntry size byte plus MAX_IFDESCR_LEN for description
   )
{
   ULONG       nInstance = 0;
   ULONG       nSize;
   int         nResult = ERROR_SUCCESS;
   IFEntry     *pIFEntry;

   //
   // Walk The List Of IFEntries By Instance
   //
   while( !nResult )
   {
      nSize = *pQueryBufferSize;

      nResult = TDIQ_GetIFEntryForInstance(
                  nInstance++,
                  pQueryBuffer,
                  &nSize
                  );

      if( nResult )
      {
         break;
      }

      //
      // Check For Match On Index
      //
      pIFEntry = (IFEntry * )pQueryBuffer;

      if( pIFEntry->if_index == nIFIndex )
      {
         nResult = ERROR_SUCCESS;
         *pQueryBufferSize = nSize;
         break;
      }
   }

   return( nResult );
}

int
TDIQ_GetIPRouteTable(
   PUCHAR pQueryBuffer,
   PULONG pQueryBufferSize
   )
{
   int   result = ERROR_SUCCESS;
   TCP_REQUEST_QUERY_INFORMATION_EX tcpRequestQueryInfoEx;
   ULONG tcpRequestBufSize = sizeof( TCP_REQUEST_QUERY_INFORMATION_EX );

   memset(&tcpRequestQueryInfoEx, 0, sizeof(tcpRequestQueryInfoEx));

   tcpRequestQueryInfoEx.ID.toi_entity.tei_entity = CL_NL_ENTITY;
   tcpRequestQueryInfoEx.ID.toi_entity.tei_instance = 0;

   tcpRequestQueryInfoEx.ID.toi_class = INFO_CLASS_PROTOCOL;
   tcpRequestQueryInfoEx.ID.toi_type = INFO_TYPE_PROVIDER;
   tcpRequestQueryInfoEx.ID.toi_id = IP_MIB_RTTABLE_ENTRY_ID;

   result = WsControl(
               IPPROTO_TCP,
               WSCNTL_TCPIP_QUERY_INFO,
               &tcpRequestQueryInfoEx,
               &tcpRequestBufSize,
               pQueryBuffer,
               pQueryBufferSize
               );

   if( result )
   {
      fprintf(stderr, "%s(%d) TdiQueryDeviceControl failed (%ld)\n",
         __FILE__,
         __LINE__,
         result
         );

      return( result );
   }

   return( result );
}


int
TDIQ_GetIPAddrTable(
   PUCHAR pQueryBuffer,
   PULONG pQueryBufferSize
   )
{
   int   result = ERROR_SUCCESS;
   TCP_REQUEST_QUERY_INFORMATION_EX tcpRequestQueryInfoEx;
   ULONG tcpRequestBufSize = sizeof( TCP_REQUEST_QUERY_INFORMATION_EX );

   memset(&tcpRequestQueryInfoEx, 0, sizeof(tcpRequestQueryInfoEx));

   tcpRequestQueryInfoEx.ID.toi_entity.tei_entity = CL_NL_ENTITY;
   tcpRequestQueryInfoEx.ID.toi_entity.tei_instance = 0;

   tcpRequestQueryInfoEx.ID.toi_class = INFO_CLASS_PROTOCOL;
   tcpRequestQueryInfoEx.ID.toi_type = INFO_TYPE_PROVIDER;
   tcpRequestQueryInfoEx.ID.toi_id = IP_MIB_ADDRTABLE_ENTRY_ID;

   result = WsControl(
               IPPROTO_TCP,
               WSCNTL_TCPIP_QUERY_INFO,
               &tcpRequestQueryInfoEx,
               &tcpRequestBufSize,
               pQueryBuffer,
               pQueryBufferSize
               );

   if( result )
   {
      fprintf(stderr, "%s(%d) TdiQueryDeviceControl failed (%ld)\n",
         __FILE__,
         __LINE__,
         result
         );

      return( result );
   }

   return( result );
}

BOOLEAN
TDIQ_IsIPInstalled( VOID )
{
   int result = 0;
   TCP_REQUEST_QUERY_INFORMATION_EX tcpRequestQueryInfoEx;
   DWORD       tcpRequestBufSize = sizeof(tcpRequestQueryInfoEx);
   DWORD       entityIdsBufSize;
   DWORD       i;
   TDIEntityID *entityIds;
   DWORD       entityCount;

   memset(&tcpRequestQueryInfoEx, 0, sizeof(tcpRequestQueryInfoEx));

   tcpRequestQueryInfoEx.ID.toi_entity.tei_entity = GENERIC_ENTITY;
   tcpRequestQueryInfoEx.ID.toi_entity.tei_instance = 0;

   tcpRequestQueryInfoEx.ID.toi_class = INFO_CLASS_GENERIC;
   tcpRequestQueryInfoEx.ID.toi_type = INFO_TYPE_PROVIDER;
   tcpRequestQueryInfoEx.ID.toi_id = ENTITY_LIST_ID;

   entityIdsBufSize = MAX_TDI_ENTITIES * sizeof(TDIEntityID);
   entityIds = (TDIEntityID *)calloc(entityIdsBufSize, 1);

   result = WsControl(
                IPPROTO_TCP,
                WSCNTL_TCPIP_QUERY_INFO,
                &tcpRequestQueryInfoEx,
                &tcpRequestBufSize,
                entityIds,
                &entityIdsBufSize);

   if (result)
   {
      free( entityIds );

      fprintf(stderr, "%s(%d) TdiQueryDeviceControl failed (%ld)\n", __FILE__, __LINE__,
         WSAGetLastError());
      return FALSE;
   }

   //...after the call we compute:
   entityCount = entityIdsBufSize / sizeof(TDIEntityID);

   //find the ip interface
   for (i = 0; i < entityCount; i++)
   {
      ULONG entityType;
      DWORD entityTypeSize;

      if (entityIds[i].tei_entity == CL_NL_ENTITY)
      {
         //get ip interface info
         memset(&tcpRequestQueryInfoEx, 0, sizeof(tcpRequestQueryInfoEx));

         tcpRequestQueryInfoEx.ID.toi_entity = entityIds[i];
         tcpRequestQueryInfoEx.ID.toi_class = INFO_CLASS_GENERIC;
         tcpRequestQueryInfoEx.ID.toi_type = INFO_TYPE_PROVIDER;
         tcpRequestQueryInfoEx.ID.toi_id = ENTITY_TYPE_ID;

         entityType;
         entityTypeSize = sizeof(entityType);

         result = WsControl(
                     IPPROTO_TCP,
                     WSCNTL_TCPIP_QUERY_INFO,
                     &tcpRequestQueryInfoEx,
                     &tcpRequestBufSize,
                     &entityType,
                     &entityTypeSize
                     );

         if (result)
         {
            fprintf(stderr, "%s(%d) TdiQueryDeviceControl failed (%ld)\n", __FILE__, __LINE__,
               WSAGetLastError());
            return FALSE;
         }

         if (entityType == CL_NL_IP) // Entity implements IP.
         {
            return( TRUE );
         }
      }
   }

   return( FALSE );
}

/////////////////////////////////////////////////////////////////////////////
//// TDIQ_GetNextIPRouteTableEntry
//
// Purpose
// Return pointer to the next IPRouteEntry given a pointer to a current
// IPRouteEntry.
//
// Parameters
//
// Return Value
// 
// Remarks
// The size of an IPRouteEntry differs between the Windows 9X and Windows
// NT platforms. There are several different approaches that could be
// used to accomodate this difference. The method used in TDIQ is to
// use this function find the next entry by adding a platform-specific
// offset to the current entry.
//

IPRouteEntry *
TDIQ_GetNextIPRouteTableEntry(
   IPRouteEntry *pIPRouteEntry
   )
{
   PUCHAR pEntry = (PUCHAR )pIPRouteEntry;

   pEntry += g_nIPRouteEntrySize;

   return( (IPRouteEntry * )pEntry );
}

// Call AFTER TDIQ_Startup...
BOOLEAN
TDIQ_IsWindows95( VOID )
{
   return( g_bIsWindows9X );
}

int
TDIQ_Startup( VOID )
{
   int         result = ERROR_SUCCESS;
   WSADATA     WSAData;

   g_hModule = LoadLibrary("wsock32.dll");
   if( !g_hModule )
   {
      fprintf(stderr, "LoadLibrary failed for wsock32.dll (%ld)\n", GetLastError());
      return EXIT_FAILURE;
   }

   WsControl = (WsControlProc)GetProcAddress( g_hModule, "WsControl");
   if( WsControl )
   {
      g_bIsWindows9X = TRUE;

      g_nIPRouteEntrySize = sizeof( IPRouteEntry );
      g_nIPRouteEntrySize -= sizeof( ULONG );
   }
   else
   {
      WsControl = WsControlNT;
      g_nIPRouteEntrySize = sizeof( IPRouteEntry );
   }

   result = WSAStartup(MAKEWORD(1, 1), &WSAData);

   if (result)
   {
      fprintf(stderr, "WSAStartup failed (%ld)\n", result);
      FreeLibrary(g_hModule);
      g_hModule = NULL;
      return FALSE;
   }

   return( result );
}

VOID
TDIQ_Cleanup( VOID )
{
   WSACleanup();
   if( g_hModule )
   {
      FreeLibrary( g_hModule );
   }

   g_hModule = NULL;
}

//end of code

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -