📄 adptlst.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
/*****************************************************************************
*
*
* @doc EX_RAS
* adptlst.c Miniport utilities
*
* Date: 2/25/99
*
*/
// Include Files
#include "windows.h"
#include "ndis.h"
#include "ras.h"
#include "raserror.h"
#include "cxport.h"
#include "protocol.h"
#include "ppp.h"
#include "macp.h"
#define COUNTOF(array) sizeof(array)/sizeof(array[0])
//
// This is the RASDEVINFOW structure used in CE 3.0 and earlier.
//
#define RAS_MaxDeviceName_V3 32
typedef struct
{
DWORD dwSize;
WCHAR szDeviceType[RAS_MaxDeviceType+1];
WCHAR szDeviceName[RAS_MaxDeviceName_V3+1];
} RASDEVINFOW_V3, *LPRASDEVINFOW_V3;
// List of the types of miniport media that PPP can bind to
NDIS_MEDIUM MediaArray[1] = {NdisMediumWan};
DWORD
AddAdapter(
IN PNDIS_STRING pAdapterString)
{
PNDISWAN_ADAPTER pNew;
NDIS_STATUS Status; // Status for NDIS calls.
NDIS_STATUS OpenStatus;
UINT SelectedMediumIndex;
uint cAdapterStr;
DEBUGMSG (ZONE_FUNCTION, (TEXT("PPP: +AddAdapter(%s)\n"), pAdapterString->Buffer));
// Try to allocate a new adapter structure.
pNew = (PNDISWAN_ADAPTER)pppAllocateMemory (sizeof (NDISWAN_ADAPTER));
if (NULL == pNew) {
DEBUGMSG (ZONE_ERROR, (TEXT("PPP: -AddAdapter: Unable to allocate new adapter\n")));
return NDIS_STATUS_RESOURCES;
}
cAdapterStr = (wcslen(pAdapterString->Buffer)+1)*sizeof(WCHAR);
// Allocate space for the name
pNew->szAdapterName = (LPTSTR)pppAllocateMemory(cAdapterStr);
if (NULL == pNew->szAdapterName)
{
pppFreeMemory (pNew, sizeof (NDISWAN_ADAPTER));
DEBUGMSG (ZONE_ERROR, (TEXT("PPP: -AddAdapter: Unable to allocate new adaptername\n")));
return NDIS_STATUS_RESOURCES;
}
_tcscpy (pNew->szAdapterName, pAdapterString->Buffer);
pNew->hUnbindContext = NULL;
pNew->bClosingAdapter = FALSE;
// Open the adapter.
NdisOpenAdapter(&Status, &OpenStatus, &pNew->hAdapter, &SelectedMediumIndex,
&MediaArray[0], COUNTOF(MediaArray), v_PROTHandle, (NDIS_HANDLE)pNew, pAdapterString, 0, NULL);
DEBUGMSG (ZONE_INIT, (TEXT("PPP: NdisOpenAdapter returned 0x%X, 0x%X\r\n"), Status, OpenStatus));
if (Status != NDIS_STATUS_SUCCESS)
{
DEBUGMSG (ZONE_ERROR && Status != NDIS_STATUS_UNSUPPORTED_MEDIA, (TEXT("PPP: -AddAdapter:NdisOpenAdapter(%s) failed with 0x%X\n"),
pAdapterString->Buffer, Status));
// we need to clean up
pppFreeMemory(pNew->szAdapterName, cAdapterStr);
pppFreeMemory(pNew, sizeof (NDISWAN_ADAPTER));
return Status;
}
//
// Add the adapter to our global list of adapters
//
EnterCriticalSection (&v_AdapterCS);
// Just insert at head...
pNew->pNext = v_AdapterList;
v_AdapterList = pNew;
pNew->dwRefCnt = 1; // Initial reference count
LeaveCriticalSection (&v_AdapterCS);
// Do the provider initialize stuff
NdisTapiAdapterInitialize(pNew);
DEBUGMSG (ZONE_MAC, (TEXT("PPP: AddAdapter: Added '%s'\n"), pNew->szAdapterName));
return NDIS_STATUS_SUCCESS;
}
BOOL
AdapterAddRef (
IN OUT PNDISWAN_ADAPTER pAdapter)
{
PNDISWAN_ADAPTER pTemp;
BOOL fRetVal = FALSE;
EnterCriticalSection (&v_AdapterCS);
// Search for the adapter
for (pTemp = v_AdapterList; NULL != pTemp; pTemp = pTemp->pNext)
{
if (pTemp == pAdapter)
{
fRetVal = TRUE;
// Increment the reference count.
pAdapter->dwRefCnt++;
break;
}
}
LeaveCriticalSection (&v_AdapterCS);
return fRetVal;
}
extern void PROTCAComplete(NDIS_HANDLE Handle, NDIS_STATUS Status);
BOOL
AdapterDelRef (
IN OUT PNDISWAN_ADAPTER pAdapter)
//
// Delete a reference to the adapter.
// If reference count goes to 0, initiate the closing of the adapter binding.
// If pAdapter is invalid, return FALSE. (Should not happen)
//
{
PNDISWAN_ADAPTER pTemp, pLast;
NDIS_STATUS Status;
BOOL bFound = FALSE;
EnterCriticalSection (&v_AdapterCS);
// Search for the adapter
pLast = NULL;
for (pTemp = v_AdapterList; NULL != pTemp; pTemp = pTemp->pNext)
{
if (pTemp == pAdapter)
{
bFound = TRUE;
// Decrement the reference count.
pAdapter->dwRefCnt--;
if (0 == pAdapter->dwRefCnt)
{
DEBUGMSG(ZONE_MAC, (L"PPP: Closing adapter=%s %x since refcnt is 0\n", pAdapter->szAdapterName, pAdapter));
if (pLast) {
pLast->pNext = pAdapter->pNext;
} else {
v_AdapterList = pAdapter->pNext;
}
NdisCloseAdapter(&Status, pAdapter->hAdapter);
//
// As soon as a protocol calls NdisCloseAdapter,
// the handle at NdisBindingHandle should be considered invalid by the caller.
// It is a programming error to pass this handle in any subsequent call to an NdisXxx function
//
// The ProtocolCloseAdapterComplete function is a required driver
// function that completes processing for an unbinding operation
// for which NdisCloseAdapter returned NDIS_STATUS_PENDING.
if (Status != NDIS_STATUS_PENDING)
{
//
// NdisCloseAdapter completed synchronously, invoke the completion
// handler directly.
//
PROTCAComplete(pAdapter, Status);
}
}
break;
}
pLast = pTemp;
}
LeaveCriticalSection (&v_AdapterCS);
return bFound;
}
static VOID
AdapterAddRefLocked(
IN OUT PNDISWAN_ADAPTER pAdapter)
//
// This function is called to add a reference to an
// adapter known to be on the v_AdapterList while
// the v_AdapterCS is held.
//
{
pAdapter->dwRefCnt++;
}
static VOID
AdapterDelRefLocked(
IN OUT PNDISWAN_ADAPTER pAdapter)
//
// This function is called to delete a reference to an
// adapter known to be on the v_AdapterList while
// the v_AdapterCS is held.
//
{
if (pAdapter->dwRefCnt > 1)
pAdapter->dwRefCnt--;
else
AdapterDelRef(pAdapter);
}
typedef struct _tagEnumDevicesInfo
{
LPRASDEVINFOW pRasDevInfo;
DWORD cbMax; // Initial buffer size
DWORD cb; // End or required size
DWORD cDevices; // Number of devices returned
DWORD dwRetVal;
DWORD dwSizeDevInfo; // Size of RASDEVINFO structure
} ENUMDEVICESINFO, *PENUMDEVICESINFO;
typedef BOOL (WINAPI *PFN_DEVWALK) (PNDISWAN_ADAPTER pAdapter, DWORD dwDevID,
PVOID pContext, LPCTSTR szDeviceName, LPCTSTR szDeviceType);
typedef struct _tagWalkDevicesInfo
{
PFN_DEVWALK pfnDevWalk;
BOOL bOnlyWalkNullModem;
} WALKDEVICESINFO, *PWALKDEVICESINFO;
BOOL
WalkDevEnum (
IN PNDISWAN_ADAPTER pAdapter,
IN DWORD dwDevID,
IN OUT PVOID pContext,
IN LPCTSTR szDeviceName,
IN LPCTSTR szDeviceType)
//
// This function is called by WalkDevices to build the list of device
// info when we are enumerating all the devices available to RAS.
//
{
PENUMDEVICESINFO pEnumDevInfo = (PENUMDEVICESINFO)pContext;
LPRASDEVINFOW pRasDevInfo;
DWORD dwElemSize;
dwElemSize = pEnumDevInfo->dwSizeDevInfo;
ASSERT(dwElemSize == sizeof(RASDEVINFOW) || dwElemSize == sizeof(RASDEVINFOW_V3));
pRasDevInfo = (LPRASDEVINFOW)((PBYTE)(pEnumDevInfo->pRasDevInfo) + pEnumDevInfo->cb);
pEnumDevInfo->cb += dwElemSize;
// Room?
if (pEnumDevInfo->cb <= pEnumDevInfo->cbMax)
{
pRasDevInfo->dwSize = dwElemSize;
if (dwElemSize == sizeof(RASDEVINFOW_V3))
{
LPRASDEVINFOW_V3 pRasDevInfoV3;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -