#include "PalmType.h"
#include "p_util.h"

/* Private IP : 10.0.0.0    - 10.255.255.255
                172.16.0.0  - 172.32.255.255
                192.168.0.0 - 192.168.255.255
*/  
BOOLEAN	IsPrivateIP(PXCHAR pIP)
{
	DCHAR iFirst, iSecond;

	iFirst = pIP[0];
	iSecond = pIP[1];

	if (iFirst == 10)
	{
		return TRUE;
	}
	else if (iFirst == 172)
	{
		if (iSecond >= 16 && iSecond <= 32)
		{
			return TRUE;
		}
	}
	else if (iFirst == 192)
	{
		if (iSecond == 168)
		{
			return TRUE;
		}
	}
	return FALSE;
}

BOOLEAN IsValidIP(PXCHAR pIP)
{
	DCHAR iFirst, iSecond;

	iFirst = pIP[0];
	iSecond = pIP[1];

	if (iFirst == 127 || iFirst >= 224)
	{
		return FALSE;
	}
	else if (!iFirst)
	{
		if (!iSecond)
		{
			return FALSE;
		}
	}

	return TRUE;
}

BOOLEAN IsPublicIP(PXCHAR pIP)
{
	if (!IsValidIP(pIP))
	{
		return FALSE;
	}
	if (IsPrivateIP(pIP))
	{
		return FALSE;
	}
	return TRUE;
}

BOOLEAN IsBroadcastIP(PXCHAR pPeerIP)
{
	DCHAR i;

	for (i = 0; i < IP_ALEN; i ++)
	{
		if ((pPeerIP[i] | Local_pSubnetMask[i]) != 0xff)
		{
			return FALSE;
		}
	}
	return TRUE;
}

PXCHAR ProxyGetLocalIP(PXCHAR pCompareIP)
{
	if (Sys_iNatTraversal == NAT_TRAVERSAL_ENABLE && (Sys_iProtocol == PROTOCOL_H323 || Sys_iProtocol == PROTOCOL_SIP || Sys_iProtocol == PROTOCOL_MGCP))
	{
		if (Sys_bNATed && IsPublicIP(pCompareIP))
		{
			return Sys_pProxyIP;
		}
	}
	return Local_pIPAddress;
}

