📄 init.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.
//
/*****************************************************************************/
/** Microsoft Windows **/
/*****************************************************************************/
/*
init.c
DESCRIPTION:
initialization routines for DhcpV6Lite
*/
#include "dhcpv6p.h"
#include "dhcpv6l.h"
//#include "precomp.h"
#ifdef DEBUG
DBGPARAM dpCurSettings = {
TEXT("DHCP6"), {
TEXT("Init"), TEXT("Timer"), TEXT("AutoIP"), TEXT("Unused"),
TEXT("Recv"), TEXT("Send"), TEXT("Request"), TEXT("Media Sense"),
TEXT("Notify"), TEXT("Buffer"), TEXT("Interface"), TEXT("Misc"),
TEXT("Alloc"), TEXT("Function"), TEXT("Warning"), TEXT("Error") },
0xc000
};
#endif
/*
typedef DWORD (*PFNDWORD2)(void *, void *);
PFNDWORD2 v_pfnIPDispatchDeviceControl;
PFNVOID *v_apSocketFns, *v_apAfdFns;
CRITICAL_SECTION v_ProtocolListLock;
PDHCP_PROTOCOL_CONTEXT v_ProtocolList;
HANDLE v_TcpipDriver;
int v_DhcpInitDelay;
extern CTEEvent v_DhcpEvent;
void DhcpEventWorker(CTEEvent * Event, void * Context);
STATUS HandleMediaConnect(unsigned Context, PTSTR pAdapter);
STATUS HandleMediaDisconnect(unsigned Context, PTSTR pAdapter);
*/
#if 0
// Handle by which to notify applications of changes to IP data structures.
PDHCP_PROTOCOL_CONTEXT
DhcpRegister(
PWCHAR wszProtocolName,
PFNSetDHCPNTE pfnSetNTE,
PFNIPSetNTEAddr pfnSetAddr,
PFN_DHCP_NOTIFY *ppDhcpNotify)
//
// If successful, returns a pointer to a DHCP protocol context
// which the caller will pass to subsequent calls to DhcpNotify.
//
{
PDHCP_PROTOCOL_CONTEXT pContext;
DEBUGMSG(ZONE_INIT | ZONE_WARN, (TEXT("+DhcpRegister:\n")));
EnterCriticalSection(&v_ProtocolListLock);
pContext = FindProtocolContextByName(wszProtocolName);
if (pContext == NULL) {
pContext = LocalAlloc(LPTR, sizeof(*pContext));
if (pContext) {
pContext->pNext = v_ProtocolList;
v_ProtocolList = pContext;
wcsncpy(pContext->wszProtocolName, wszProtocolName, MAX_DHCP_PROTOCOL_NAME_LEN);
}
}
if (pContext) {
pContext->pfnSetNTE = pfnSetNTE;
pContext->pfnSetAddr = pfnSetAddr;
*ppDhcpNotify = DhcpNotify;
}
LeaveCriticalSection(&v_ProtocolListLock);
DEBUGMSG(ZONE_INIT | ZONE_WARN, (TEXT("-DhcpRegister: Ret = %x\n"), pContext));
return pContext;
} // DhcpRegister()
#endif
VOID
InitGlobalsAtStartup(
)
{
//
// Init globals that aren't cleared on service stop to make sure
// everything's in a known state on start. This allows us to
// stop/restart without having our DLL unloaded/reloaded first.
//
gbDHCPV6RPCServerUp = FALSE;
ghServiceStopEvent = NULL;
gdwServersListening = 0;
gbServerListenSection = FALSE;
gpDHCPV6SD = NULL;
gbDHCPV6Section = FALSE;
gpDhcpV6IniInterfaceTblRWLock = &gDhcpV6IniInterfaceTblRWLock;
gpIniInterfaceHandle = NULL;
gbAdapterInit = FALSE;
gbAdapterPendingDeleteInit = FALSE;
gbTimerModuleInit = FALSE;
memset(&MCastSockAddr, 0, sizeof(MCastSockAddr));
gbReplyModuleInit = FALSE;
gbEventModule = FALSE;
return;
}
DWORD
InitDHCPV6ThruRegistry(
)
{
DWORD dwError = 0;
gbDHCPV6PDEnabled = TRUE;
return (dwError);
}
DWORD
InitDHCPV6Globals(
)
{
DWORD dwError = 0;
SECURITY_ATTRIBUTES SecurityAttributes;
ULONG uAddressLength = 0;
DhcpV6Trace(DHCPV6_MISC, DHCPV6_LOG_LEVEL_TRACE, ("Initializing Globals"));
dwError = InitializeDHCPV6Security(&gpDHCPV6SD);
BAIL_ON_WIN32_ERROR(dwError);
memset(&SecurityAttributes, 0, sizeof(SECURITY_ATTRIBUTES));
SecurityAttributes.nLength = sizeof(SecurityAttributes);
SecurityAttributes.lpSecurityDescriptor = NULL;
SecurityAttributes.bInheritHandle = TRUE;
ghServiceStopEvent = CreateEvent(
&SecurityAttributes,
TRUE,
FALSE,
NULL
);
if (!ghServiceStopEvent) {
dwError = GetLastError();
BAIL_ON_WIN32_ERROR(dwError);
}
__try {
InitializeCriticalSection(&gcServerListenSection);
gbServerListenSection = TRUE;
}
__except (EXCEPTION_EXECUTE_HANDLER) {
dwError = GetExceptionCode();
BAIL_ON_WIN32_ERROR(dwError);
}
gdwServersListening = 0;
__try {
InitializeCriticalSection(&gcDHCPV6Section);
gbDHCPV6Section = TRUE;
}
__except (EXCEPTION_EXECUTE_HANDLER) {
dwError = GetExceptionCode();
BAIL_ON_WIN32_ERROR(dwError);
}
dwError = InitializeRWLock(gpDhcpV6IniInterfaceTblRWLock);
BAIL_ON_WIN32_ERROR(dwError);
// Adapter List
dwError = InitializeRWLock(gpAdapterRWLock);
BAIL_ON_WIN32_ERROR(dwError);
InitializeListHead(&AdapterList);
gbAdapterInit = TRUE;
// Pending Delete Adapters
dwError = InitializeRWLock(gpAdapterPendingDeleteRWLock);
BAIL_ON_WIN32_ERROR(dwError);
InitializeListHead(&AdapterPendingDeleteList);
gbAdapterPendingDeleteInit = TRUE;
// Init Winsock2
dwError = WSAStartup(0x202, gpWSAData);
BAIL_ON_WIN32_ERROR(dwError);
// MCastSockAddr.sin6_addr.u = { 0xff02, 0, 0, 0, 0, 0, 0x0001, 0x0002};
MCastSockAddr.sin6_addr.u.Word[0] = 0x2ff;
MCastSockAddr.sin6_addr.u.Word[6] = 0x0100;
MCastSockAddr.sin6_addr.u.Word[7] = 0x0200;
/*
dwError = RtlIpv6StringToAddressEx(
All_DHCP_Relay_Agents_and_Servers,
&MCastSockAddr.sin6_addr,
&MCastSockAddr.sin6_scope_id,
&MCastSockAddr.sin6_port
);
*/
BAIL_ON_WIN32_ERROR(dwError);
MCastSockAddr.sin6_family = AF_INET6;
MCastSockAddr.sin6_port = htons(DHCPV6_SERVER_LISTEN_PORT);
dwError = InitDhcpV6EventMgr(gpDhcpV6EventModule);
BAIL_ON_WIN32_ERROR(dwError);
gbEventModule = TRUE;
dwError = InitDhcpV6TimerModule(gpDhcpV6TimerModule);
BAIL_ON_WIN32_ERROR(dwError);
gbTimerModuleInit = TRUE;
dwError = InitDhcpV6ReplyManager(gpDhcpV6ReplyModule);
BAIL_ON_WIN32_ERROR(dwError);
gbReplyModuleInit = TRUE;
dwError = DhcpV6InitWMI();
BAIL_ON_WIN32_ERROR(dwError);
#ifdef UNDER_CE
dwError = DhcpV6InitTdiNotifications();
BAIL_ON_WIN32_ERROR(dwError);
#endif
// srand((unsigned)time(NULL));
DhcpV6Trace(DHCPV6_PNP, DHCPV6_LOG_LEVEL_TRACE, ("End Initializing Globals"));
return dwError;
error:
DhcpV6Trace(DHCPV6_PNP, DHCPV6_LOG_LEVEL_FATAL,
("FAILED Global Init Error: %!status!, EventModule: %x, TimerModule: %x, ReplyModule: %x",
dwError, gbEventModule, gbTimerModuleInit, gbReplyModuleInit));
return dwError;
}
//
// This is the calling function when residing in an existing service
//
DWORD
DhcpV6Startup(
)
{
DWORD dwError = 0;
InitGlobalsAtStartup();
dwError = InitDHCPV6ThruRegistry();
BAIL_ON_WIN32_ERROR(dwError);
dwError = InitDHCPV6Globals();
BAIL_ON_WIN32_ERROR(dwError);
BAIL_ON_WIN32_ERROR( DHCPv6ManagePrefixInit() ? 0 : 1);
dwError = InitDHCPV6AdaptAtStartup();
BAIL_ON_WIN32_ERROR(dwError);
// dwError = DHCPV6StartRPCServer();
// BAIL_ON_WIN32_ERROR(dwError);
return dwError;
error:
DhcpV6Shutdown(dwError);
return dwError;
}
VOID
DhcpV6Shutdown(
DWORD dwErrorCode
)
{
DWORD dwError = 0;
if (gbDHCPV6RPCServerUp) {
gbDHCPV6RPCServerUp = FALSE;
// DHCPV6StopRPCServer();
}
//
// Signal adapters going down
//
if (gbAdapterInit) {
gbAdapterInit = FALSE;
// ShutdownAllDHCPV6Adapt();
}
AcquireSharedLock(gpAdapterPendingDeleteRWLock);
while (!IsListEmpty(&AdapterPendingDeleteList)) {
ReleaseSharedLock(gpAdapterPendingDeleteRWLock);
Sleep(2000);
AcquireSharedLock(gpAdapterPendingDeleteRWLock);
}
ReleaseSharedLock(gpAdapterPendingDeleteRWLock);
DhcpV6DeInitWMI();
if (gbReplyModuleInit) {
gbReplyModuleInit = FALSE;
DeInitDhcpV6ReplyManager(gpDhcpV6ReplyModule);
}
if (gbTimerModuleInit) {
gbTimerModuleInit = FALSE;
DeInitDhcpV6TimerModule(gpDhcpV6TimerModule);
}
// Clean up Winsock2
WSACleanup();
if (gpIniInterfaceHandle) {
DestroyIniInterfaceHandleList(gpIniInterfaceHandle);
gpIniInterfaceHandle = NULL;
}
DestroyRWLock(gpAdapterPendingDeleteRWLock);
DestroyRWLock(gpAdapterRWLock);
DHCPv6ManagePrefixDeinit();
return;
}
BOOL DllEntry (HANDLE hinstDLL, DWORD Op, PVOID lpvReserved) {
BOOL Status = TRUE;
switch (Op) {
case DLL_PROCESS_ATTACH:
DEBUGREGISTER(hinstDLL);
DEBUGMSG (ZONE_INIT|ZONE_WARN,
(TEXT("Dhcp: dllentry() %d\r\n"), hinstDLL));
// InitializeCriticalSection(&v_ProtocolListLock);
// CTEInitEvent(&v_DhcpEvent, DhcpEventWorker);
DisableThreadLibraryCalls ((HMODULE)hinstDLL);
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_DETACH :
break;
case DLL_THREAD_ATTACH :
break;
default :
break;
}
return Status;
} // dllentry()
extern unsigned int v_cXid;
extern BOOL WCE_Initialize();
#define DHCPV6_DEV_PREFIX TEXT("DP6")
#define DHCPV6_DEV_INDEX 0
#define DHCPV6_DEVICE_NAME TEXT("DP60:")
// IMPORTANT: The name of this fn must be the same as the HelperName
// note: the parameters for registration is different: Unused, OpCode,
// pVTable, cVTable, pMTable, cMTable, pIndexNum
BOOL DhcpV6L(DWORD Unused, DWORD OpCode,
PBYTE pName, DWORD cBuf1,
PBYTE pBuf1, DWORD cBuf2,
PDWORD pBuf2) {
STATUS Status = DHCP_SUCCESS;
BOOL fStatus = TRUE;
PWSTR pAdapterName = (PWSTR)pName;
//unsigned int cRand, cRand2;
//FILETIME CurTime;
//__int64 cBigRand;
HKEY hKey;
DWORD Disp;
DWORD Index;
LPCWSTR DHCPV6DevPath = L"TCPIP6\\DHCPV6L";
switch(OpCode) {
case DHCP_REGISTER:
// v_apAfdFns = (PFNVOID *)pName;
// v_apSocketFns = (PFNVOID *)pBuf1;
*pBuf2 = 0;
// CTEInitLock(&v_GlobalListLock);
// InitializeListHead(&v_EstablishedList);
/*
v_ProtocolList = NULL;
GetCurrentFT(&CurTime);
cRand = (CurTime.dwHighDateTime & 0x788) | (CurTime.dwLowDateTime & 0xf800);
cBigRand = CeGetRandomSeed();
cRand2 = (uint)(cBigRand >> 32);
cRand2 ^= (uint)(cBigRand & 0xffFFffFF);
v_cXid = cRand ^ cRand2;
hRes = RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT("Comm\\Tcpip\\Parms"),
0, 0, &hKey);
if (ERROR_SUCCESS == hRes) {
GetRegDWORDValue(hKey, TEXT("DhcpGlobalInitDelayInterval"),
&v_DhcpInitDelay);
RegCloseKey(hKey);
}
DEBUGMSG(ZONE_INIT, (TEXT("\tDhcpV6L:Register: FT %d, Xid %d\r\n"),
cRand, v_cXid));
*/
// Create Reg keys for activate device...
if (ERROR_SUCCESS ==
RegCreateKeyEx(HKEY_LOCAL_MACHINE, DHCPV6DevPath, 0, NULL, 0, 0,
NULL, &hKey, &Disp)) {
RegSetValueEx(hKey, L"Dll", 0, REG_SZ, (BYTE *) L"dhcpv6L.dll", 24);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -