ptnet.c

来自「PGP8.0源码 请认真阅读您的文件包然后写出其具体功能」· C语言 代码 · 共 1,751 行 · 第 1/3 页

C
1,751
字号

	PGPclPlaySound (ppias->soundToPlay, FALSE);

	if (ppias->notifyEnabled)
	{
		if (sShouldFlash ())
		{
			s_uAttackType = ppias->attackType;
			s_dwAttackerIP = ppias->intruderAddr;
			s_iFlashCount = -1;

			KillTimer (hwnd, PGPNET_TIMER_ID);
			SetTimer (hwnd, PGPNET_TIMER_ID, FLASH_TIMER_MS, NULL);

			PTUpdateTrayIconAndText (hwnd, TRUE, FALSE);
		}
	}
}


//	________________________
//
//	prefs may have changed, do anything we need to do 

VOID
PTHandleNetPrefsChange (
		HWND		hwnd,
		PGPPrefRef	prefref,
		BOOL		bNetPref)
{
	if (bNetPref)
	{
		PGPGetPrefBoolean (prefref, 
				kPGPNetPrefEnablePGPnet, &s_bNetEnabled);

		if (s_pHostList)
		{
			PGPFreeData (s_pHostList);
			s_pHostList = NULL;
		}

		s_bMenuStructsValid = FALSE;
		PGPGetNetHostPrefs (prefref, &s_pHostList, &s_uHostCount);
		sSortHosts ();
	}
	else
	{
		if (IsntPGPError (PGPGetPrefBoolean (prefref, 
				kPGPPrefShowTrayIcon, &s_bIconEnabled)))
		{
			PTUpdateTrayIconAndText (hwnd, TRUE, FALSE);
		}
	}
}


//	________________________
//
//	check incoming message for something pertaining to PGPnet

VOID
PTCheckForNetMsg (
		HWND	hwnd,
		UINT	msg,
		WPARAM	wParam,
		LPARAM	lParam)
{
	switch (msg) 
	{
		case WM_DESTROY :
			if (s_pHostList)
			{
				PGPFreeData (s_pHostList);
				s_pHostList = NULL;
			}
			break;

		case PGPNET_M_APPMESSAGE :
			switch (wParam) {
			case PGPNET_SERVICESTART :
				#if NETTRAYDEBUGLOG
				sLogText ("got PGPNET_SERVICESTART");
				#endif	// NETTRAYDEBUGLOG

				sSendServiceMessage (PGPNET_M_APPMESSAGE, 
						PGPNET_USERPROCESSID, (LPARAM)GetCurrentProcessId ());
				break;

			case PGPNET_DISABLEGUI :
				#if NETTRAYDEBUGLOG
				sLogText ("got PGPNET_DISABLEGUI");
				#endif	// NETTRAYDEBUGLOG

				// relenquish the foreground window to the PGPnet service
				SetForegroundWindow ((HWND)lParam);
				s_bGUIEnabled = FALSE;
				break;

			case PGPNET_ENABLEGUI :
				#if NETTRAYDEBUGLOG
				sLogText ("got PGPNET_ENABLEGUI");
				#endif	// NETTRAYDEBUGLOG

				s_bGUIEnabled = TRUE;
				break;

			case PGPNET_PLAYSOUND :
				#if NETTRAYDEBUGLOG
				sLogText ("got PGPNET_PLAYSOUND");
				#endif	// NETTRAYDEBUGLOG

				PGPclPlaySound ((PGPclSoundIndex)lParam, FALSE);
				break;

			case PGPNET_FLASHICON :
				#if NETTRAYDEBUGLOG
				{
					CHAR	sz[64];
					wsprintf (sz, "got PGPNET_FLASHICON; lParam = %li; s_iFlashCount = %li",
									lParam, s_iFlashCount);
					sLogText (sz);
				}
				#endif	// NETTRAYDEBUGLOG

				if (lParam)
				{
					if (s_iFlashCount == 0)
						s_iFlashCount = lParam;
				}
				else
				{
					if (s_iFlashCount != 0)
						s_iFlashCount = 1;
				}

				if (s_iFlashCount != 0)
				{
					KillTimer (hwnd, PGPNET_TIMER_ID);
					SetTimer (hwnd, PGPNET_TIMER_ID, FLASH_TIMER_MS, NULL);
					PTUpdateTrayIconAndText (hwnd, TRUE, FALSE);
				}
				break;

			case PGPNET_SACOUNT :
				#if NETTRAYDEBUGLOG
				{
					CHAR	sz[64];
					wsprintf (sz, "got PGPNET_SACOUNT; lParam = %li; s_iFlashCount = %li",
									lParam, s_iFlashCount);
					sLogText (sz);
				}
				#endif	// NETTRAYDEBUGLOG

				if ((s_uAttackType == kPGPnetAttack_None) &&
					(s_uSACount != (UINT)lParam))
				{
					s_iFlashCount = 1;
					s_uSACount = lParam;
					PTUpdateTrayIconAndText (hwnd, TRUE, FALSE);
				}
				else
					s_uSACount = lParam;
				break;

			case PGPNET_SAREQUEST :
				if (s_uAttackType == kPGPnetAttack_None)
				{
					s_iFlashCount = NUM_SA_FLASHES;
					KillTimer (hwnd, PGPNET_TIMER_ID);
					SetTimer (hwnd, PGPNET_TIMER_ID, FLASH_TIMER_MS, NULL);
				}
				break;

			case PGPNET_SAFAILED :
				if (s_uAttackType == kPGPnetAttack_None)
				{
					if (s_iFlashCount != 0)
						s_iFlashCount = 1;
				}
				break;
			
			
			case PGPNET_TRACEATTACK :
				PTNetLaunch(hwnd, PGPNET_IDSPAGE);
				break;
			}
			break;

		case WM_TIMER :
			if (s_iFlashCount == 0)
			{
				s_uAttackType = kPGPnetAttack_None;
				PTUpdateTrayIconAndText (hwnd, TRUE, FALSE);
			}
			else
			{
				if (s_iFlashCount > 0)
				{
					if (--s_iFlashCount == 0)
					{
						KillTimer (hwnd, PGPNET_TIMER_ID);
						SetTimer (hwnd, PGPNET_TIMER_ID, PGPNET_TIMER_MS, NULL);
					}
				}
				else
					s_iFlashCount ^= 0x1;

				PTUpdateTrayIconAndText (hwnd, FALSE, FALSE);
			}
			break;

		case WM_COPYDATA :
		{
			PCOPYDATASTRUCT pcds	= (PCOPYDATASTRUCT)lParam;
			switch (pcds->dwData) {
			case PGPNET_INTRUDERALERT :
				sIntruderAlert (hwnd,
					(PGPIntruderAlertStructure*)(pcds->lpData));
				break;

			case PGPNET_STATUSFULL :
			{
				PBYTE		p;
				PBYTE		pEnd;
				DWORD		dwSizeKeyBlock;

				#if NETTRAYDEBUGLOG
				sLogText ("got PGPNET_STATUSFULL");
				#endif	// NETTRAYDEBUGLOG

				if (s_pSAList)
				{
					free (s_pSAList);
					s_pSAList = NULL;
				}

				s_uSACount = 0;
				s_pSAList = malloc (pcds->cbData);
				if (s_pSAList)
				{
					p = (pcds->lpData);
					pEnd = p + pcds->cbData;

					while (p < pEnd)
					{
						memcpy (&s_pSAList[s_uSACount++], p, sizeof(PGPipsecSA));

						// get keyblock size
						p += sizeof(PGPipsecSA);
						dwSizeKeyBlock = *((DWORD*)p);

						// get keyblock
						p += sizeof(DWORD);

						// step to next SA
						p += dwSizeKeyBlock;
					}
				}
				break;
			}
			}
			break;
		}
	}
}

//	____________________________________
//
//	find the authentication transform

static INT
sGetAuthenticationTransformIndex (
		PGPipsecSA*	pSA)
{
	INT	iTransform	= -1;
	INT nTransforms = min (pSA->numTransforms, kPGPike_MaxTransforms);
	INT	i;
	
	for (i=0; i<nTransforms; i++)
	{
		if (pSA->transform[i].protocol == kPGPike_PR_AH)
		{
			iTransform = i;
			break;
		}
	}

	return iTransform;
}

//	____________________________________
//
//	find the encryption transform

static INT
sGetEncryptionTransformIndex (
		PGPipsecSA*	pSA)
{
	INT	iTransform	= -1;
	INT nTransforms = min (pSA->numTransforms, kPGPike_MaxTransforms);
	INT	i;
	
	for (i=0; i<nTransforms; i++)
	{
		if (pSA->transform[i].protocol == kPGPike_PR_ESP)
		{
			iTransform = i;
			break;
		}
	}

	return iTransform;
}

//	____________________________________
//
//	determine if SA is tunnel mode

static BOOL
sIsTunnelMode (
		PGPipsecSA*		pSA)
{
	PGPipsecEncapsulation	encap;
	INT						iTrns;

	// determine if tunnel mode
	iTrns = sGetEncryptionTransformIndex (pSA);
	if (iTrns >= 0) 
		encap = pSA->transform[iTrns].u.esp.t.mode;
	else
	{
		iTrns = sGetAuthenticationTransformIndex (pSA);
		encap = pSA->transform[iTrns].u.ah.t.mode;
	}

	return (encap == kPGPike_PM_Tunnel);
}

//	____________________________________
//
//	find the host for this SA

PGPNetPrefHostEntry*
sFindHostForSA (
		PGPipsecSA*		pSA)
{
	PGPNetPrefHostEntry*	pHost				= NULL;
	PGPNetPrefHostEntry*	pHostHost			= NULL;
	PGPNetPrefHostEntry*	pHostSubnetRange	= NULL;
	PGPUInt32				u;

	for (u=0; u<s_uHostCount; u++)
	{
		pHost = &s_pHostList[u];

		if (pHost->destIsRange)
		{
			if ((ntohl (pSA->ipAddrStart) >= ntohl (pHost->ipAddrStart)) &&
				(ntohl (pSA->ipAddrStart) <= ntohl (pHost->ipMaskEnd)))
			{
				pHostSubnetRange = pHost;
			}
		}
		else
		{
			if ((pHost->ipAddrStart == pSA->ipAddrStart) &&
				(pHost->ipMaskEnd == pSA->ipMaskEnd))
			{
				return pHost;
			}

			if (pHost->ipAddrStart == pSA->ipAddress)
			{
				pHostHost = pHost;
			}

			if ((pHost->ipAddrStart & pHost->ipMaskEnd) == 
				(pSA->ipAddress & pHost->ipMaskEnd))
			{
				pHostSubnetRange = pHost;
			}
		}
	}

	if (pHostHost)
		return pHostHost;
	if (pHostSubnetRange)
		return pHostSubnetRange;

	return NULL;
}

//	____________________________________
//
//	kill all SAs related to a host

VOID
sRemoveHostSAs (
		HWND	hwnd,
		UINT	uHostIndex)
{
	BOOL					bRemove			= FALSE;
	BOOL					bNeedDelay		= FALSE;

	PGPipsecSA*				pSA;
	PGPNetPrefHostEntry*	pHostForSA;
	PGPNetPrefHostEntry*	pHost;
	INT						iPass;
	UINT					u;

	if (!s_pSAList || (s_uSACount ==0))
		return;

	pHost = &s_pHostList[uHostIndex];

	for (iPass = 1; iPass <= 2; iPass++)
	{
		for (u=0; u<s_uSACount; u++)
		{
			bRemove = FALSE;

			pSA = &s_pSAList[u];

			pHostForSA = sFindHostForSA (pSA);
			if (pHostForSA)
			{
				if (pHost == pHostForSA)
				{
					// in first pass remove only non-tunnel mode SAs
					// then remove all remaining matching SAs
					if (iPass == 1)
					{
						if (!sIsTunnelMode (pSA))
						{
							bRemove = TRUE;
							if (pHostForSA->childOf != -1)
								bNeedDelay = TRUE;
						}
					}
					else
						bRemove = TRUE;
				}

				if (pHostForSA->childOf != -1)
				{
					INT	iParent = pHostForSA->childOf;
					if (pHost == &s_pHostList[iParent])
						bRemove = TRUE;
				}
			}

			if (bRemove)
			{
				COPYDATASTRUCT	cds;
				PGPipsecSPI		spi;

				// we must delay after killing an child and before killing
				// the parent
				if (bNeedDelay && (iPass == 2))
				{
					HCURSOR		hcursorOld;

					bNeedDelay = FALSE;
					hcursorOld = SetCursor (LoadCursor (NULL, IDC_WAIT));
					Sleep (KILLDELAYMS);
					SetCursor (hcursorOld);
				}

				memcpy (spi, &pSA->transform[0].inSPI, sizeof(PGPipsecSPI));

				cds.dwData = PGPNET_STATUSREMOVESA;
				cds.cbData = sizeof(PGPipsecSPI);
				cds.lpData = spi;

				sSendServiceMessage (WM_COPYDATA,
						(WPARAM)hwnd, (LPARAM)&cds);
			}
		}
	}
}

//	____________________________________
//
//	determine if specified host has an SA in list
//	if address and mask are both zero, then check for any existing SAs

BOOL
sDoesHostHaveSA (
		UINT	uHostIndex)
{
	PGPNetPrefHostEntry*	pHost;
	PGPNetPrefHostEntry*	pHostForSA;
	UINT					u;

	if (!s_pSAList || (s_uSACount ==0))
		return FALSE;

	pHost = &s_pHostList[uHostIndex];

	// address/mask both zero => return TRUE if any SAs at all
	if ((pHost->ipAddrStart == 0) && (pHost->ipMaskEnd == 0))
	{
		return (s_uSACount > 0);
	}

	for (u=0; u<s_uSACount; u++)
	{
		pHostForSA = sFindHostForSA (&s_pSAList[u]);

		if (pHostForSA)
		{
			if (pHost == pHostForSA)
				return TRUE;

			if (pHostForSA->childOf != -1)
			{
				if (pHost == &s_pHostList[pHostForSA->childOf])
					return TRUE;
			}
		}
	}

	return FALSE;
}

//	____________________________________
//
//	return TRUE if a gateway with the "exclusiveGateway" flag is connected

static BOOL
sExclusiveGatewayConnected (VOID)
{
	PGPUInt32	u;

	for (u=0; u<s_uSACount; u++)
	{
		if ((s_pSAList[u].ipAddrStart == 1) && (s_pSAList[u].ipMaskEnd == 0))
			return TRUE;
	}

	return FALSE;
}


//	____________________________________
//
//	return TRUE if a gateway with the "virtualIP" flag is connected

static BOOL
sVirtualIPGatewayConnected (VOID)
{
	PSORTEDHOSTLIST			p;
	PGPNetPrefHostEntry*	pentry;

	p = s_pSortedList;
	while (p)
	{
		if ((UINT)p->iIndex < s_uHostCount)
		{
			pentry = &s_pHostList[p->iIndex];

			if (pentry->hostType == kPGPnetSecureGateway)
			{
				if (pentry->virtualIP)
				{
					if (sDoesHostHaveSA (p->iIndex))
					{
						return TRUE;
					}
				}
			}
		}
		p = p->pNext;
	}

	return FALSE;
}

//	____________________________________
//
//	determine if host entry is connectable

BOOL
sIsHostEntryConnectable (

⌨️ 快捷键说明

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