📄 packet32.c
字号:
}
SetLastError(err);
return result;
}
/*!
\brief Dumps a registry key to disk in text format. Uses regedit.
\param KeyName Name of the ket to dump. All its subkeys will be saved recursively.
\param FileName Name of the file that will contain the dump.
\return If the function succeeds, the return value is nonzero.
For debugging purposes, we use this function to obtain some registry keys from the user's machine.
*/
#ifdef _DEBUG_TO_FILE
LONG PacketDumpRegistryKey(PCHAR KeyName, PCHAR FileName)
{
CHAR Command[256];
strcpy(Command, "regedit /e ");
strcat(Command, FileName);
strcat(Command, " ");
strcat(Command, KeyName);
/// Let regedit do the dirt work for us
system(Command);
return TRUE;
}
#endif
/*!
\brief Returns the version of a dll or exe file
\param FileName Name of the file whose version has to be retrieved.
\param VersionBuff Buffer that will contain the string with the file version.
\param VersionBuffLen Length of the buffer poited by VersionBuff.
\return If the function succeeds, the return value is TRUE.
\note uses the GetFileVersionInfoSize() and GetFileVersionInfo() WIN32 API functions
*/
BOOL PacketGetFileVersion(LPTSTR FileName, PCHAR VersionBuff, UINT VersionBuffLen)
{
DWORD dwVerInfoSize; // Size of version information block
DWORD dwVerHnd=0; // An 'ignored' parameter, always '0'
LPSTR lpstrVffInfo;
UINT cbTranslate, dwBytes;
TCHAR SubBlock[64];
PVOID lpBuffer;
PCHAR TmpStr;
// Structure used to store enumerated languages and code pages.
struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;
ODS("PacketGetFileVersion\n");
// Now lets dive in and pull out the version information:
dwVerInfoSize = GetFileVersionInfoSize(FileName, &dwVerHnd);
if (dwVerInfoSize)
{
lpstrVffInfo = GlobalAllocPtr(GMEM_MOVEABLE, dwVerInfoSize);
if (lpstrVffInfo == NULL)
{
ODS("PacketGetFileVersion: failed to allocate memory\n");
return FALSE;
}
if(!GetFileVersionInfo(FileName, dwVerHnd, dwVerInfoSize, lpstrVffInfo))
{
ODS("PacketGetFileVersion: failed to call GetFileVersionInfo\n");
GlobalFreePtr(lpstrVffInfo);
return FALSE;
}
// Read the list of languages and code pages.
if(!VerQueryValue(lpstrVffInfo, TEXT("\\VarFileInfo\\Translation"), (LPVOID*)&lpTranslate, &cbTranslate))
{
ODS("PacketGetFileVersion: failed to call VerQueryValue\n");
GlobalFreePtr(lpstrVffInfo);
return FALSE;
}
// Create the file version string for the first (i.e. the only one) language.
wsprintf( SubBlock,
TEXT("\\StringFileInfo\\%04x%04x\\FileVersion"),
(*lpTranslate).wLanguage,
(*lpTranslate).wCodePage);
// Retrieve the file version string for the language.
if(!VerQueryValue(lpstrVffInfo, SubBlock, &lpBuffer, &dwBytes))
{
ODS("PacketGetFileVersion: failed to call VerQueryValue\n");
GlobalFreePtr(lpstrVffInfo);
return FALSE;
}
// Convert to ASCII
TmpStr = WChar2SChar(lpBuffer);
if(strlen(TmpStr) >= VersionBuffLen)
{
ODS("PacketGetFileVersion: Input buffer too small\n");
GlobalFreePtr(lpstrVffInfo);
GlobalFreePtr(TmpStr);
return FALSE;
}
strcpy(VersionBuff, TmpStr);
GlobalFreePtr(lpstrVffInfo);
GlobalFreePtr(TmpStr);
}
else
{
ODSEx("PacketGetFileVersion: failed to call GetFileVersionInfoSize, LastError = %d\n", GetLastError());
return FALSE;
}
return TRUE;
}
/*!
\brief Opens an adapter using the NPF device driver.
\param AdapterName A string containing the name of the device to open.
\return If the function succeeds, the return value is the pointer to a properly initialized ADAPTER object,
otherwise the return value is NULL.
\note internal function used by PacketOpenAdapter() and AddAdapter()
*/
LPADAPTER PacketOpenAdapterNPF(PCHAR AdapterName)
{
LPADAPTER lpAdapter;
BOOLEAN Result;
DWORD error;
SC_HANDLE svcHandle = NULL;
LONG KeyRes;
HKEY PathKey;
SERVICE_STATUS SStat;
BOOLEAN QuerySStat;
WCHAR SymbolicLink[128];
ODS("PacketOpenAdapterNPF\n");
scmHandle = OpenSCManager(NULL, NULL, GENERIC_READ);
if(scmHandle == NULL){
error = GetLastError();
ODSEx("OpenSCManager failed! LastError=%d\n", error);
}
else{
// check if the NPF registry key is already present
// this means that the driver is already installed and that we don't need to call PacketInstallDriver
KeyRes=RegOpenKeyEx(HKEY_LOCAL_MACHINE,
NPFRegistryLocation,
0,
KEY_READ,
&PathKey);
if(KeyRes != ERROR_SUCCESS)
{
Result = PacketInstallDriver(scmHandle,&svcHandle);
}
else
{
Result = TRUE;
RegCloseKey(PathKey);
}
if (Result)
{
srvHandle = OpenService(scmHandle, NPFServiceName, SERVICE_START | SERVICE_QUERY_STATUS );
if (srvHandle != NULL)
{
QuerySStat = QueryServiceStatus(srvHandle, &SStat);
#if defined(_DBG) || defined(_DEBUG_TO_FILE)
switch (SStat.dwCurrentState)
{
case SERVICE_CONTINUE_PENDING:
ODS("The status of the driver is: SERVICE_CONTINUE_PENDING\n");
break;
case SERVICE_PAUSE_PENDING:
ODS("The status of the driver is: SERVICE_PAUSE_PENDING\n");
break;
case SERVICE_PAUSED:
ODS("The status of the driver is: SERVICE_PAUSED\n");
break;
case SERVICE_RUNNING:
ODS("The status of the driver is: SERVICE_RUNNING\n");
break;
case SERVICE_START_PENDING:
ODS("The status of the driver is: SERVICE_START_PENDING\n");
break;
case SERVICE_STOP_PENDING:
ODS("The status of the driver is: SERVICE_STOP_PENDING\n");
break;
case SERVICE_STOPPED:
ODS("The status of the driver is: SERVICE_STOPPED\n");
break;
default:
ODS("The status of the driver is: unknown\n");
break;
}
#endif
if(!QuerySStat || SStat.dwCurrentState != SERVICE_RUNNING)
{
ODS("Calling startservice\n");
if (StartService(srvHandle, 0, NULL)==0)
{
error = GetLastError();
if(error!=ERROR_SERVICE_ALREADY_RUNNING && error!=ERROR_ALREADY_EXISTS)
{
SetLastError(error);
if (scmHandle != NULL)
CloseServiceHandle(scmHandle);
error = GetLastError();
ODSEx("PacketOpenAdapterNPF: StartService failed, LastError=%d\n",error);
SetLastError(error);
return NULL;
}
}
}
CloseServiceHandle( srvHandle );
srvHandle = NULL;
}
else
{
error = GetLastError();
ODSEx("OpenService failed! Error=%d", error);
SetLastError(error);
}
}
else
{
if(KeyRes != ERROR_SUCCESS)
Result = PacketInstallDriver(scmHandle,&svcHandle);
else
Result = TRUE;
if (Result) {
srvHandle = OpenService(scmHandle,NPFServiceName,SERVICE_START);
if (srvHandle != NULL)
{
QuerySStat = QueryServiceStatus(srvHandle, &SStat);
#if defined(_DBG) || defined(_DEBUG_TO_FILE)
switch (SStat.dwCurrentState)
{
case SERVICE_CONTINUE_PENDING:
ODS("The status of the driver is: SERVICE_CONTINUE_PENDING\n");
break;
case SERVICE_PAUSE_PENDING:
ODS("The status of the driver is: SERVICE_PAUSE_PENDING\n");
break;
case SERVICE_PAUSED:
ODS("The status of the driver is: SERVICE_PAUSED\n");
break;
case SERVICE_RUNNING:
ODS("The status of the driver is: SERVICE_RUNNING\n");
break;
case SERVICE_START_PENDING:
ODS("The status of the driver is: SERVICE_START_PENDING\n");
break;
case SERVICE_STOP_PENDING:
ODS("The status of the driver is: SERVICE_STOP_PENDING\n");
break;
case SERVICE_STOPPED:
ODS("The status of the driver is: SERVICE_STOPPED\n");
break;
default:
ODS("The status of the driver is: unknown\n");
break;
}
#endif
if(!QuerySStat || SStat.dwCurrentState != SERVICE_RUNNING){
ODS("Calling startservice\n");
if (StartService(srvHandle, 0, NULL)==0){
error = GetLastError();
if(error!=ERROR_SERVICE_ALREADY_RUNNING && error!=ERROR_ALREADY_EXISTS){
if (scmHandle != NULL) CloseServiceHandle(scmHandle);
ODSEx("PacketOpenAdapterNPF: StartService failed, LastError=%d\n",error);
SetLastError(error);
return NULL;
}
}
}
CloseServiceHandle( srvHandle );
srvHandle = NULL;
}
else{
error = GetLastError();
ODSEx("OpenService failed! LastError=%d", error);
SetLastError(error);
}
}
}
}
if (scmHandle != NULL) CloseServiceHandle(scmHandle);
lpAdapter=(LPADAPTER)GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof(ADAPTER));
if (lpAdapter==NULL)
{
ODS("PacketOpenAdapterNPF: GlobalAlloc Failed\n");
error=GetLastError();
//set the error to the one on which we failed
SetLastError(error);
ODS("PacketOpenAdapterNPF: Failed to allocate the adapter structure\n");
return NULL;
}
lpAdapter->NumWrites=1;
if (LOWORD(GetVersion()) == 4)
wsprintf(SymbolicLink,TEXT("\\\\.\\%s"),&AdapterName[16]);
else
wsprintf(SymbolicLink,TEXT("\\\\.\\Global\\%s"),&AdapterName[16]);
// Copy only the bytes that fit in the adapter structure.
// Note that lpAdapter->SymbolicLink is present for backward compatibility but will
// never be used by the apps
memcpy(lpAdapter->SymbolicLink, (PCHAR)SymbolicLink, MAX_LINK_NAME_LENGTH);
//try if it is possible to open the adapter immediately
lpAdapter->hFile=CreateFile(SymbolicLink,GENERIC_WRITE | GENERIC_READ,
0,NULL,OPEN_EXISTING,0,0);
if (lpAdapter->hFile != INVALID_HANDLE_VALUE)
{
if(PacketSetReadEvt(lpAdapter)==FALSE){
error=GetLastError();
ODS("PacketOpenAdapterNPF: Unable to open the read event\n");
GlobalFreePtr(lpAdapter);
//set the error to the one on which we failed
SetLastError(error);
ODSEx("PacketOpenAdapterNPF: PacketSetReadEvt failed, LastError=%d\n",error);
return NULL;
}
PacketSetMaxLookaheadsize(lpAdapter);
_snprintf(lpAdapter->Name, ADAPTER_NAME_LENGTH, "%S", AdapterName);
return lpAdapter;
}
error=GetLastError();
GlobalFreePtr(lpAdapter);
//set the error to the one on which we failed
ODSEx("PacketOpenAdapterNPF: CreateFile failed, LastError= %d\n",error);
SetLastError(error);
return NULL;
}
/*!
\brief Opens an adapter using the DAG capture API.
\param AdapterName A string containing the name of the device to open.
\return If the function succeeds, the return value is the pointer to a properly initialized ADAPTER object,
otherwise the return value is NULL.
\note internal function used by PacketOpenAdapter()
*/
#ifdef HAVE_DAG_API
LPADAPTER PacketOpenAdapterDAG(PCHAR AdapterName, BOOLEAN IsAFile)
{
CHAR DagEbuf[DAGC_ERRBUF_SIZE];
LPADAPTER lpAdapter;
LONG status;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -