// DevceAttribute.cpp : 实现文件
//

#include "stdafx.h"
#include "NetSDKDemo.h"
#include "NetSDKDLL.h"
#include "DeviceAttributeDlg.h"
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
#ifndef ISDIGIT
#define ISDIGIT(c)      ( ((c) >= '0') && ((c) <= '9') )
#endif
// CDeviceAttribute 对话框
BOOL   checkipAddr(LPCTSTR   lp) ;
BOOL IsValidatedDigit(CString strDigit, int iMin, int iMax);
IMPLEMENT_DYNAMIC(CDeviceAttribute, CDialog)

CDeviceAttribute::CDeviceAttribute(IPC* pIPCInfo,  CWnd* pParent /*=NULL*/)
	: CDialog(CDeviceAttribute::IDD, pParent)
{
	memcpy(&m_IPCInfo, pIPCInfo, sizeof(IPC));
	m_pParent=pParent;
	//m_lIndex = lIndex;
}

CDeviceAttribute::~CDeviceAttribute()
{
}

void CDeviceAttribute::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_IPADDRESS_IP, m_ctlIP);
	DDX_Control(pDX, IDC_IPADDRESS_MASK, m_ctlMask);
	DDX_Control(pDX, IDC_IPADDRESS_GATEWAY, m_ctlGateway);
	DDX_Control(pDX, IDC_IPADDRESS_DNS1, m_ctlDNS1);
	DDX_Control(pDX, IDC_IPADDRESS_DNS2, m_ctlDNS2);
}


BEGIN_MESSAGE_MAP(CDeviceAttribute, CDialog)
	ON_BN_CLICKED(IDC_BTN_AUTOGET, &CDeviceAttribute::OnBnClickedBtnAutoget)
	ON_BN_CLICKED(IDC_BTN_MODIFY, &CDeviceAttribute::OnBnClickedBtnModify)
END_MESSAGE_MAP()


// CDeviceAttribute 消息处理程序

BOOL CDeviceAttribute::OnInitDialog()
{
	CDialog::OnInitDialog();

	// TODO:  在此添加额外的初始化
	
	IPC_ENTRY DevInfo;
	long lRet = IP_NET_DVR_GetIPCInfo(m_IPCInfo.m_iIndex, &DevInfo);
	
	if (lRet != 0)
	{
		m_ctlIP.SetWindowText(_T("0"));
		m_ctlMask.SetWindowText(_T("0"));
		m_ctlGateway.SetWindowText(_T("0"));
		m_ctlDNS1.SetWindowText(_T("0"));
		m_ctlDNS2.SetWindowText(_T("0"));
	}
	else
	{
		m_ctlIP.SetWindowText(DevInfo.lanCfg.IPAddress);
		m_ctlMask.SetWindowText(DevInfo.lanCfg.netMask);
		m_ctlGateway.SetWindowText(DevInfo.lanCfg.gateWay);
		m_ctlDNS1.SetWindowText(DevInfo.lanCfg.DNS1);
		m_ctlDNS2.SetWindowText(DevInfo.lanCfg.DNS2);
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}

void CDeviceAttribute::OnBnClickedBtnAutoget()
{
	// TODO: 在此添加控件通知处理程序代码
	enum
	{
		NETWORK_PARAM_IPADDRESS=0,//取得IP地址
		NETWORK_PARAM_NETMASK,//取得子网掩码
		NETWORK_PARAM_GATEWAY,//取得网关地址
		NETWORK_PARAM_DNS1,//取得主DNS
		NETWORK_PARAM_DNS2,//取得配用DNS
		NETWORK_PARAM_MAX//结束标记，不使用
	};

	int iSize = 33;
	char Str[40] = {0};
	IP_NET_DVR_GetOneIPAddress(Str,iSize);
	m_ctlIP.SetWindowText(Str);

	IP_NET_DVR_GetNetworkParam(NETWORK_PARAM_NETMASK, Str, iSize);
	m_ctlMask.SetWindowText(Str);

	IP_NET_DVR_GetNetworkParam(NETWORK_PARAM_GATEWAY, Str, iSize);
	m_ctlGateway.SetWindowText(Str);

	IP_NET_DVR_GetNetworkParam(NETWORK_PARAM_DNS1, Str, iSize);
	m_ctlDNS1.SetWindowText(Str);

	IP_NET_DVR_GetNetworkParam(NETWORK_PARAM_DNS2, Str, iSize);
	m_ctlDNS2.SetWindowText(Str);
	UpdateData(FALSE);
}

void CDeviceAttribute::OnBnClickedBtnModify()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strIP, strMask, strGateWay, strDNS1, strDNS2;
	
	BYTE nIPField0, nIPField1, nIPField2, nIPField3;
	m_ctlIP.GetAddress(nIPField0, nIPField1, nIPField2, nIPField3);
	
	BYTE nMField0, nMField1, nMField2, nMField3;
	m_ctlMask.GetAddress(nMField0, nMField1, nMField2, nMField3);
	
	BYTE nGWField0, nGWField1, nGWField2, nGWField3;
	m_ctlGateway.GetAddress(nGWField0, nGWField1, nGWField2, nGWField3);
	
	
// 	if ( (nIPField0 & nMField0 != nGWField0)
// 		|| (nIPField1 & nMField1 != nGWField1)
// 		|| (nIPField2 & nMField2 != nGWField2))
// 	{
// 		AfxMessageBox("IP 地址与网关不匹配!!!\n");
// 		return ;
// 	}
	if (nIPField0 != nGWField0 ||
		nIPField1 != nGWField1 ||
		nIPField2 != nGWField2)
	{
		AfxMessageBox("IP 地址与网关不匹配!!!\n");
		return ;
	}

	m_ctlDNS1.GetWindowText(strDNS1);
	m_ctlDNS2.GetWindowText(strDNS2);
	
	IPC_ENTRY DevInfo;
	long lRet = IP_NET_DVR_GetIPCInfo(m_IPCInfo.m_iIndex, &DevInfo);
	
	if (lRet != 0)
	{
		AfxMessageBox("获取IPC信息失败!!!\n");
		return ;
	}

	sprintf_s(DevInfo.lanCfg.IPAddress, MAX_IP_NAME_LEN, "%d.%d.%d.%d", nIPField0, nIPField1, nIPField2, nIPField3);
	sprintf_s(DevInfo.lanCfg.netMask, MAX_IP_NAME_LEN, "%d.%d.%d.%d", nMField0, nMField1, nMField2, nMField3);
	sprintf_s(DevInfo.lanCfg.gateWay, MAX_IP_NAME_LEN, "%d.%d.%d.%d", nGWField0, nGWField1, nGWField2, nGWField3);
	sprintf_s(DevInfo.lanCfg.DNS1, MAX_IP_NAME_LEN, "%s", strDNS1.GetBuffer());
	sprintf_s(DevInfo.lanCfg.DNS2, MAX_IP_NAME_LEN, "%s", strDNS2.GetBuffer());

	if (nIPField3 < 1 || nIPField3 >254)
	{
		AfxMessageBox(_T("IP地址最后一段不合理"));
		return;
	}

	if (!checkipAddr(DevInfo.lanCfg.IPAddress))
	{
		AfxMessageBox(_T("非法的IP地址!"));
		return ;
	}
	if (!checkipAddr(DevInfo.lanCfg.netMask))
	{
		AfxMessageBox(_T("非法的子网掩码!"));
		return ;
	}

	if (!checkipAddr(DevInfo.lanCfg.gateWay))
	{
		AfxMessageBox(_T("非法的网关地址!"));
		return ;
	}

	if (!checkipAddr(DevInfo.lanCfg.DNS1))
	{
		AfxMessageBox(_T("非法的DNS1!"));
		return ;
	}

	if (!checkipAddr(DevInfo.lanCfg.DNS2))
	{
		AfxMessageBox(_T("非法的DNS2!"));
		return ;
	}
	in_addr iip, iGateWay, iMask/*, iDNS1, iDNS2*/;
	iip.S_un.S_addr = inet_addr(DevInfo.lanCfg.IPAddress);
	iGateWay.S_un.S_addr = inet_addr(DevInfo.lanCfg.gateWay);
	iMask.S_un.S_addr = inet_addr(DevInfo.lanCfg.netMask);
	if((iGateWay.S_un.S_un_b.s_b1 & iMask.S_un.S_un_b.s_b1) != (iip.S_un.S_un_b.s_b1 & iMask.S_un.S_un_b.s_b1) 
		|| (iGateWay.S_un.S_un_b.s_b2 & iMask.S_un.S_un_b.s_b2) != (iip.S_un.S_un_b.s_b2 & iMask.S_un.S_un_b.s_b2) 
		|| (iGateWay.S_un.S_un_b.s_b3 & iMask.S_un.S_un_b.s_b3) != (iip.S_un.S_un_b.s_b3 & iMask.S_un.S_un_b.s_b3) 
		|| (iGateWay.S_un.S_un_b.s_b4 & iMask.S_un.S_un_b.s_b4) != (iip.S_un.S_un_b.s_b4 & iMask.S_un.S_un_b.s_b4) 
		|| iip.S_un.S_un_b.s_b4==255)
	{
		AfxMessageBox(_T("非法的网络配置！"));
		return ;
	}

#if 0
	IP_NET_DVR_ModifyIPCXML();
#endif
	lRet = IP_NET_DVR_ModifyIPC(m_IPCInfo.m_iIndex, &DevInfo);
	TRACE("IP_NET_DVR_ModifyIPC %s\n", DevInfo.lanCfg.IPAddress);
	if (lRet != 0)
	{
		AfxMessageBox("修改IPC信息失败!!!\n");
		return ;
	}
	AfxMessageBox("修改IPC信息已提交\n");	
}


BOOL IsValidatedDigit(CString strDigit, int iMin, int iMax)
{
	BOOL bValidate = TRUE;
	const int SZT_SIZE = 64;
	TCHAR szT[SZT_SIZE] = {0};
	if (strDigit.GetLength() > SZT_SIZE) bValidate = FALSE;

	if (bValidate && (_stprintf_s(szT, SZT_SIZE, _T("%s"), strDigit.GetBuffer())<=0))
		bValidate = FALSE;

	for(int i=0; bValidate && (i<SZT_SIZE); i++)
	{
		if (szT[i] == '\0') break;

		if (!ISDIGIT(szT[i]))
		{
			bValidate = FALSE;
			break;
		}
	}

	int iDigit = 0;
	if (bValidate && (_sntscanf_s(szT, _countof(szT), _T("%d"), &iDigit) != 1))
		bValidate = FALSE;	

	if (bValidate && (iDigit>=iMin) && (iDigit<=iMax));
	else bValidate = FALSE;

	return bValidate;
}

BOOL   checkipAddr(LPCTSTR   lp) 
{ 
	TCHAR   lpszip[100]; 
	bool   bret   =   TRUE; 

	if(lp==NULL||_tcslen(lp)>=100) 
	{
		bret   =   FALSE; 
	}
	else
	{ 
		_tcscpy(lpszip,lp); 
		TCHAR   seps[]       =   _T("."); 
		TCHAR   *token; 
		int   nfields=0; 
		token   =   _tcstok(   lpszip,   seps   ); 
		while(   token   !=   NULL   ) 
		{ 
			nfields   ++; 
			if (!IsValidatedDigit(token, 0, 255))
			{
				bret   =   false; 
				break; 
			}

			int   ipf=_tstoi(token); 
			if(ipf==0&&_tcsicmp(token, _T("0"))!=0) 
			{ 
				bret   =   false; 
				break; 
			} 
			if(ipf<0 || ipf>255) 
			{ 
				bret   =   false; 
				break; 
			} 
			token   =   _tcstok(   NULL,   seps   ); 
		} 
		if(nfields!=4) 
			bret   =   false; 
	} 

	if (inet_addr(lp) == INADDR_NONE) bret = FALSE;

	return   bret;	
} 

BOOL CDeviceAttribute::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN)
	{
		if(pMsg->wParam == 13)
			return TRUE;
		if (pMsg->wParam == 27)
		{
			::PostMessage(GetParent()->m_hWnd, WM_KEYDOWN, 27, 0);
			return TRUE;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}
