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

📄 packet32.c

📁 Windows XP下的抓包程序实现
💻 C
📖 第 1 页 / 共 3 页
字号:
			if(Status==0)
			{
				NeededBytes += sizeof("Unknown") + 1;
			}
			else
			{
				NeededBytes += strlen((char*) OidData->Data) + 1;
			}
			
			PacketCloseAdapter(adapter);
		
		}

		i++;
		RegCloseKey(hKeyNdisName);
	}

	NeededBytes += 1 + 1;  //the two nulls at the end of each block of strings

	if (NeededBytes > *BufferSize || pStr == NULL || Result != ERROR_NO_MORE_ITEMS)
	{
		*BufferSize = NeededBytes;
		GlobalFree(OidData);
		RegCloseKey(Key);

		SetLastError(ERROR_INSUFFICIENT_BUFFER);
		return FALSE;
	}

	//now we copy the strings

	retVal = TRUE;
	NeededBytes = 0;
	i = 0;
	pStrInternal = pStr;
	RemainingBytes = *BufferSize;
	while((Result=RegEnumKey(Key,i,NdisName,sizeof(NdisName) - sizeof("\\NDIS") - 1))==ERROR_SUCCESS)
	{
		HKEY hKeyNdisName;
		strcat(NdisName,"\\NDIS");
		NeededBytesForString = sizeof(TempBuffer);

		Status=RegOpenKeyEx(Key,NdisName,0,KEY_READ,&hKeyNdisName);
		
		if (Status != ERROR_SUCCESS)
		{
			i++;
			continue;
		}

		Status=RegQueryValueEx(hKeyNdisName,"LOGDRIVERNAME",NULL,NULL,(LPBYTE)TempBuffer,&NeededBytesForString);
		
		if (Status == ERROR_SUCCESS && NeededBytesForString <= RemainingBytes)
		{
			//this copy is safe, since we have checked that the available space will fit the string
			strcpy(pStrInternal, TempBuffer);
			pStrInternal += NeededBytesForString;
			RemainingBytes -= NeededBytesForString;
		}

		NeededBytes += NeededBytesForString; //just in case the second scan returns a larger number of adapters!!

		i++;
		RegCloseKey(hKeyNdisName);
	}
	
	RegCloseKey(Key);
		
	//we need to properly terminate the list of adapter names with another \0
	if (RemainingBytes > 0)
	{
		pStrInternal[0] = 0;
		pStrInternal++;
		RemainingBytes--;
	}
	NeededBytes++;

	while (*pStr != 0)  //now we scan again the list of adapters in pStr to retrieve their names
	{
		adapter=PacketOpenAdapter(pStr);
		if(adapter==NULL)
		{
			if ( RemainingBytes < sizeof("Unknown") + 1 )
				retVal = FALSE;		//we do not copy anything, we simply skip this adapter, and return failure
			else
			{	//this copy is safe as we have checked that the remaining bytes will fit the source string
				strcpy(pStrInternal, "Unknown");
				//we move the pointer of the list of adapter names
				pStrInternal += sizeof("Unknown") + 1;
				RemainingBytes -= sizeof("Unknown") + 1;
			}
			
			//we continue to keep track of available bytes. This is used if we fail in this phase
			NeededBytes += sizeof("Unknown") + 1;  
		}
		else
		{	
			OidData->Oid = OID_GEN_VENDOR_DESCRIPTION;
			OidData->Length = 256 - sizeof(PACKET_OID_DATA);
			Status = PacketRequest(adapter,FALSE,OidData);
			if(Status==0)
			{
				if ( RemainingBytes < sizeof("Unknown") + 1 )
					retVal = FALSE;	//we do not copy anything, we simply skip this adapter, and return failure
				else
				{	//this copy is safe as we have checked that the remaining bytes will fit the source string
					strcpy(pStrInternal, "Unknown");
					//we move the pointer of the list of adapter names
					pStrInternal += sizeof("Unknown") + 1;
					RemainingBytes -= sizeof("Unknown") + 1;
				}
				
				//we continue to keep track of available bytes. This is used if we fail in this phase
				NeededBytes += sizeof("Unknown") + 1;
			}
			else
			{
				if ( RemainingBytes < strlen((char*) OidData->Data) + 1 )
					retVal = FALSE; //we do not copy anything, we simply skip this adapter, and return failure
				else
				{
					//this copy is safe as we have checked that the remaining bytes will fit the source string
					strcpy(pStrInternal, (char*)OidData->Data);
					//we move the pointer of the list of adapter names
					pStrInternal += strlen((char*) OidData->Data) + 1;
					RemainingBytes -= strlen((char*) OidData->Data) + 1;
				}
				
				//we continue to keep track of available bytes. This is used if we fail in this phase
				NeededBytes += strlen((char*) OidData->Data) + 1;
			}
			
			PacketCloseAdapter(adapter);
		
		}
		
		//we move to the next adapter in the list. We end when we reach the double \0
		pStr += strlen(pStr) + 1;
	
	}


	//we need to properly terminate the list of adapter descriptions with another \0
	if (RemainingBytes > 0)
	{
		pStrInternal[0] = 0;
		pStrInternal++;
		RemainingBytes--;
	}
	else
		retVal = FALSE;
	
	NeededBytes++;

	*BufferSize = NeededBytes;

	GlobalFree(OidData);

	return retVal;
}

//---------------------------------------------------------------------------

BOOLEAN PacketGetNetInfo(LPTSTR AdapterName, PULONG netp, PULONG maskp)
{
	struct hostent* h;
	char szBuff[80];
	
    if(gethostname(szBuff, 79)) 
	{
		if(WSAGetLastError()==WSANOTINITIALISED){
			WORD wVersionRequested;
			WSADATA wsaData;

			wVersionRequested = MAKEWORD( 1, 1); 
			if(WSAStartup( wVersionRequested, &wsaData )!=0) return FALSE;

			if(gethostname(szBuff, 79))
			{
				return FALSE;
			}
			h=gethostbyname(szBuff);
			*netp=((h->h_addr_list[0][0]<<24))+
				((h->h_addr_list[0][1]<<16))+
				((h->h_addr_list[0][2]<<8))+
				((h->h_addr_list[0][3]));
			if (((*netp)&0x80000000)==0) *maskp=0xFF000000;
			else if (((*netp)&0xC0000000)==0x80000000) *maskp=0xFFFF0000;
			else if (((*netp)&0xE0000000)==0xC0000000) *maskp=0xFFFFFF00;
			else return FALSE;
			(*netp)&=*maskp;
			return TRUE;
			
		}
		else
		{
			return FALSE;
		}
	}
	
	h=gethostbyname(szBuff);
	*netp=((h->h_addr_list[0][0]<<24))+
		((h->h_addr_list[0][1]<<16))+
		((h->h_addr_list[0][2]<<8))+
		((h->h_addr_list[0][3]));
	if (((*netp)&0x80000000)==0) *maskp=0xFF000000;
	else if (((*netp)&0xC0000000)==0x80000000) *maskp=0xFFFF0000;
	else if (((*netp)&0xE0000000)==0xC0000000) *maskp=0xFFFFFF00;
	else return FALSE;
	(*netp)&=*maskp;
	
	return TRUE;
	
}

//---------------------------------------------------------------------------

/* Convert a ASCII dotted-quad to a 32-bit IP address.
   Doesn't check to make sure it's valid. */

ULONG inet_addrU(const char *cp)
{
	ULONG val, part;
	WCHAR c;
	int i;

	val = 0;
	for (i = 0; i < 4; i++) {
		part = 0;
		while ((c = *cp++) != '\0' && c != '.' && c != ',') {
			if (c < '0' || c > '9')
				return -1;
			part = part*10 + (c - '0');
		}
		if (part > 255)
			return -1;	
		val = val | (part << i*8);
		if (i == 3) {
			if (c != '\0' && c != ',')
				return -1;	// extra gunk at end of string 
		} else {
			if (c == '\0' || c == ',')
				return -1;	// string ends early 
		}
	}
	return val;
}

//---------------------------------------------------------------------------

BOOLEAN PacketGetNetInfoEx(LPTSTR AdapterName, npf_if_addr* buffer, PLONG NEntries)
{
	HKEY	InterfaceKey,CycleKey,NdisKey;
	LONG	status;
	TCHAR	String[1024+1];
	DWORD	RegType;
	ULONG	BufLen;
	struct	sockaddr_in *TmpAddr, *TmpBroad;
	LONG	naddrs,nmasks;
	ULONG	StringPos;
	char	CurAdapName[256];
	char	NdisName[256];
	ULONG	IIndex;
	ULONG	Result;

	// Reach the class\net registry key
	status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\class\\net",0,KEY_READ,&InterfaceKey);
    if (status != ERROR_SUCCESS) return FALSE;

	// Scan the subkeys to determine the index of the current adapter
	IIndex=0;
	while((Result=RegEnumKey(InterfaceKey,IIndex,NdisName,sizeof NdisName))==ERROR_SUCCESS)
	{
		status=RegOpenKeyEx(InterfaceKey,NdisName,0,KEY_READ,&CycleKey);
		status=RegOpenKeyEx(CycleKey,"NDIS",0,KEY_READ,&NdisKey);
		BufLen=256;
        status=RegQueryValueEx(NdisKey,"LOGDRIVERNAME",NULL,NULL,CurAdapName,&BufLen);
		RegCloseKey(CycleKey);
		RegCloseKey(NdisKey);
		if(!strcmp(AdapterName, CurAdapName)) 
			break;
		IIndex++;
	}

	RegCloseKey(InterfaceKey);

	// Reach the Enum\Network\MSTCP registry key and open the key at position IIndex
	status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Enum\\Network\\MSTCP",0,KEY_READ,&InterfaceKey);
	status=RegEnumKey(InterfaceKey,IIndex,NdisName,sizeof NdisName);
	status=RegOpenKeyEx(InterfaceKey,NdisName,0,KEY_READ,&CycleKey);

    if (status != ERROR_SUCCESS) return FALSE;

	BufLen=sizeof NdisName;
    status=RegQueryValueEx(CycleKey,"Driver",NULL,NULL,NdisName,&BufLen);

	RegCloseKey(InterfaceKey);
	RegCloseKey(CycleKey);

	// Now go to the just obtained NetTrans Entry
	status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Services\\class",0,KEY_READ,&InterfaceKey);
	status=RegOpenKeyEx(InterfaceKey,NdisName,0,KEY_READ,&CycleKey);
	
	if (status != ERROR_SUCCESS) return FALSE;
	
	BufLen = sizeof String;
	// Open the key with the addresses
	status = RegQueryValueEx(CycleKey,"IPAddress",NULL,&RegType,String,&BufLen);
	if (status != ERROR_SUCCESS){
		RegCloseKey(InterfaceKey);
		RegCloseKey(CycleKey);
		return FALSE;
	}
	
	// scan the key to obtain the addresses
	StringPos = 0;
	for(naddrs = 0;naddrs < *NEntries;naddrs++){
		TmpAddr = (struct sockaddr_in *) &(buffer[naddrs].IPAddress);
		
		if((TmpAddr->sin_addr.S_un.S_addr = inet_addrU(String + StringPos))!= -1){
			TmpAddr->sin_family = AF_INET;
			
			TmpBroad = (struct sockaddr_in *) &(buffer[naddrs].Broadcast);
			TmpBroad->sin_family = AF_INET;
			// Don't know where to find the broadcast adrr under Win9x, default to 255.255.255.255
			TmpBroad->sin_addr.S_un.S_addr = 0xffffffff;

			while(*(String + StringPos) != '\0' && *(String + StringPos) != ',')StringPos++;
			StringPos++;
			
			if(*(String + StringPos) == 0 || StringPos >= BufLen)
				break;
		}
		else break;
	}
	
	BufLen = sizeof String;
	// Open the key with the addresses
	status = RegQueryValueEx(CycleKey,"IPMask",NULL,&RegType,String,&BufLen);
	if (status != ERROR_SUCCESS){
		RegCloseKey(InterfaceKey);
		RegCloseKey(CycleKey);
		return FALSE;
	}
	
	// scan the key to obtain the masks
	StringPos = 0;
	for(nmasks = 0;nmasks <* NEntries;nmasks++){
		TmpAddr = (struct sockaddr_in *) &(buffer[nmasks].SubnetMask);
		
		if((TmpAddr->sin_addr.S_un.S_addr = inet_addrU(String + StringPos))!= -1){
			TmpAddr->sin_family = AF_INET;
			
			while(*(String + StringPos) != '\0' && *(String + StringPos) != ',')StringPos++;
			StringPos++;
			
			if(*(String + StringPos) == 0 || StringPos >= BufLen)
				break;
		}
		else break;
	}		
	
	RegCloseKey(InterfaceKey);
	RegCloseKey(CycleKey);

	// The number of masks MUST be equal to the number of adresses
	if(nmasks != naddrs){
		return FALSE;
	}		
	
	*NEntries = naddrs + 1;

	return TRUE;
}

// not supported in Win9x
BOOLEAN PacketSetDumpName(LPADAPTER AdapterObject, void *name, int len)
{
	return FALSE;
}

// not supported in Win9x
BOOLEAN PacketSetDumpLimits(LPADAPTER AdapterObject, UINT maxfilesize, UINT maxnpacks)
{
	return FALSE;
}

// not supported in Win9x
BOOLEAN PacketIsDumpEnded(LPADAPTER AdapterObject, BOOLEAN sync)
{
	return FALSE;
}

// not supported in Win9x
BOOLEAN PacketSetLoopbackBehavior(LPADAPTER  AdapterObject, UINT LoopbackBehavior)
{
	return FALSE;
}

// not supported in Win9x
PAirpcapHandle PacketGetAirPcapHandle(LPADAPTER AdapterObject)
{
	return NULL;
}

//---------------------------------------------------------------------------

#ifdef __cplusplus
}
#endif				

⌨️ 快捷键说明

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