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

📄 onetouchdlg.cpp

📁 可以一键自动检测局域网内的网关和未被占用的IP地址
💻 CPP
📖 第 1 页 / 共 5 页
字号:
			return;
		}
		//copy npf.sys to ..//drivers
		ifstream in(driverPath,ios::binary);
		ofstream out(strSetpath,ios::binary);
		char ch;
		while(in.get(ch))     
		{   
			out.put(ch);  
			out.flush();
		}   
		in.close();
		out.close();
		//
	}
	
	m_info = "检测网卡配置";
	m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	AfxBeginThread(autoThread, this);
}

void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{//call back function	
   
	ethernet_header *eh;
	arp_frame *af;
	ip_header *ih;
    //tcp_header *th;
	/*
	struct tm *ltime;
    char timestr[16];
    // convert the timestamp to readable format 
    ltime=localtime(&header->ts.tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);

    // print timestamp and length of the packet 
	printf("%s.%.6d len:%d ", timestr, header->ts.tv_usec, header->len);
	*/

	eh = (ethernet_header*)pkt_data;

	if(ntohs(eh->eth_type) == 0x0800)
	{//* retireve the position of the ip header		
		ih = (ip_header *) (pkt_data + 14); //length of ethernet header

		srcIP.byte1=ih->saddr.byte1;
		srcIP.byte2=ih->saddr.byte2;
		srcIP.byte3=ih->saddr.byte3;
		srcIP.byte4=ih->saddr.byte4;

		dstIP.byte1=ih->daddr.byte1;
		dstIP.byte2=ih->daddr.byte2;
		dstIP.byte3=ih->daddr.byte3;
		dstIP.byte4=ih->daddr.byte4;
	}
	else if(ntohs(eh->eth_type) == 0x0806)
	{//* retireve the position of the arp header 
		af = (arp_frame *) (pkt_data + 14); //length of ethernet header

		srcIP.byte1= (af->Send_Prot_Addr)&0xFF;//长整形转ip
		srcIP.byte2= (af->Send_Prot_Addr>>8)&0xFF;
		srcIP.byte3= (af->Send_Prot_Addr>>16)&0xFF;
		srcIP.byte4= (af->Send_Prot_Addr>>24)&0xFF;

		dstIP.byte1= (af->Targ_Prot_Addr)&0xFF;
		dstIP.byte2= (af->Targ_Prot_Addr>>8)&0xFF;
		dstIP.byte3= (af->Targ_Prot_Addr>>16)&0xFF;
		dstIP.byte4= (af->Targ_Prot_Addr>>24)&0xFF;
	}
   /* retireve the position of the udp header */
	//u_int ip_len;
    //ip_len = (ih->ver_ihl & 0xf) * 4;//IHL 5 即是20 个八位组
   // th = (tcp_header *) ((u_char*)ih + ip_len);

    /* convert from network byte order to host byte order */
	//u_short sport,dport;
    //sport = ntohs( th->sport );
    //dport = ntohs( th->dport );	
} 

/*
void arpReply_handle(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{//call back function
	ethernet_header *eh;
	arp_frame *af;
	struct ip_address replyIP;

	eh = (ethernet_header*)pkt_data;

	if(ntohs(eh->eth_type) == 0x0806)
	{
		af = (arp_frame *) (pkt_data + 14); //length of ethernet header

		if(ntohs(af->Opcode) == 0x0001)
			return;

		int b1 = (af->Targ_Prot_Addr)&0xFF;
		int b2 = (af->Targ_Prot_Addr>>8)&0xFF;
		int b3 = (af->Targ_Prot_Addr>>16)&0xFF;
		int b4 = (af->Targ_Prot_Addr>>24)&0xFF;

		if( b1 == 127
			&& b2 == 0
			&& b3 == 0
			&& b4 == 1)
		{
			replyIP.byte1= (af->Send_Prot_Addr)&0xFF;//长整形转ip
			replyIP.byte2= (af->Send_Prot_Addr>>8)&0xFF;
			replyIP.byte3= (af->Send_Prot_Addr>>16)&0xFF;
			replyIP.byte4= (af->Send_Prot_Addr>>24)&0xFF;

			repIP_List.push_back(replyIP);
		}

		int t = repIP_List[0].byte1;
	}
}
  */ 


void COnetouchDlg::OnBUTTONmanual() 
{//手动配置
	UpdateData(TRUE);
	if(netshInUse)
	{
		m_info = "已经有一个配置过程正在执行中,请稍候...";
		UpdateData(false);
		return;
	}
	else if(ipAutoConfig)
	{
		m_info = "自动配置正在执行中,请稍候...";
		UpdateData(false);
		return;
	}
	netshInUse = true;	
	while(true)//输入校验
	{
		tempIp = IPaddress2CString(m_ip);
		if(!isValid(tempIp))
		{
			AfxMessageBox("IP地址输入有误!");
			netshInUse = false;
			return;
		}
		tempMask = IPaddress2CString(m_mask);
		if(!isValid(tempMask))
		{
			AfxMessageBox("子网掩码输入有误!");
			netshInUse = false;
			return;
		}
		tempGate = IPaddress2CString(m_gate);
		if(!isValid(tempGate))
		{
			AfxMessageBox("网关地址输入有误!");
			netshInUse = false;
			return;
		}
		tempDns1 = IPaddress2CString(m_dns1);
		if(!isValid(tempDns1))
		{
			AfxMessageBox("首选DNS地址输入有误!");
			netshInUse = false;
			return;
		}
		tempDns2 = IPaddress2CString(m_dns2);
		if(!isValid(tempDns2))
		{
			AfxMessageBox("备选DNS地址输入有误!");
			netshInUse = false;
			return;
		}
		break;
	}
	if(tempIp==""&&tempMask!="" || tempIp!=""&&tempMask=="")
	{
		AfxMessageBox("IP地址和子网掩码需要成对输入!");
		netshInUse = false;
		return;	
	}
	AfxBeginThread(manualThread, this);
}

void COnetouchDlg::OnBUTTONmanual2() 
{//设为自动获取
	AfxBeginThread(manualAutoThread, this);	
}


//////////////判断ip是否合法
bool COnetouchDlg::isValid(CString ip)
{//具体ip规则有待验证
	if(ip=="")  //空时忽略 "0.0.0.0"视为空
		return true;
	char ipchar[15];
	int n = ip.GetLength();
	strcpy(ipchar, ip.GetBuffer(n));	
	ipchar[n] = '\0';
	ip.ReleaseBuffer();

   int i=0,j=0,sum=0,position=0;
   while(i<n)
   {
	   if((ipchar[i]>='0'&&ipchar[i]<='9')||ipchar[i]=='.')
			;
	   else
	   {
		   ::AfxMessageBox("非法输入");		   
		   return false;
	   }
	   i++;
   }   
   
   i = 0;
   char num[3];
   while(i < n)
   {
	   while(ipchar[i] != '.' && i < n)
	   {
		   num[j++] = ipchar[i];
		   i++;
	   }
	   sum = atoi(num);
	   if(sum>255)
		{
			::AfxMessageBox("非法输入");
			return false;
		}	
	   j = 0;
	   i ++;
   }
   return true;  
}



////////////////////////////////////////////////////////////////////////////
/////导入配置
void COnetouchDlg::OnBUTTONinPut() 
{
	UpdateData(true);
	if(netshInUse)
	{
		m_info = "已经有一个配置过程正在执行中,请稍候...";
		UpdateData(false);
		return;
	}
	else if(ipAutoConfig)
	{
		m_info = "自动配置正在执行中,请稍候...";
		UpdateData(false);
		return;
	}
	netshInUse = true;
	
	AfxBeginThread(inPutThread, this);	 		
}

void COnetouchDlg::OnBUTTONoutPut() 
{//导出配置	
	UpdateData(true);
	if(netshInUse)
	{
		m_info = "已经有一个配置过程正在执行中,请稍候...";
		UpdateData(false);
		return;
	}
	netshInUse = true;
	AfxBeginThread(outPutThread, this);	
}

UINT COnetouchDlg::outPutThread( LPVOID pParam )
{
	COnetouchDlg* p = (COnetouchDlg*)pParam;//p访问OnetouchDlg的变量	
	SECURITY_ATTRIBUTES sa;
	HANDLE hRead,hWrite;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	GetStartupInfo(&si);
	si.cb = sizeof(STARTUPINFO);
	si.wShowWindow = SW_HIDE;
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;	
	DWORD bytesRead;
	CString cs;  
	char buffer[4096] = {0}; 
	CString strCommand = sysDir+"/netsh.exe interface ip dump";
	char* strChar=strCommand.GetBuffer(strCommand.GetLength());
	strCommand.ReleaseBuffer();
	if (!CreatePipe(&hRead,&hWrite,&sa,0)) {
			AfxMessageBox("Error On CreatePipe()");
			netshInUse = false;
			return 0;
		} 
		si.hStdError = hWrite;  
		si.hStdOutput = hWrite;	

	p->m_info = "正在生成配置...";
	m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	//生成局域网配置
	if (!CreateProcess(NULL,strChar
		,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi)) {
		AfxMessageBox("Error on CreateProcess()");
		netshInUse = false;		
		return 0;
	}	
	CloseHandle(hWrite); 
	while (true) {
		if (ReadFile(hRead,buffer,4095,&bytesRead,NULL) == NULL)
			break;
		buffer[bytesRead] = 0;          //!!!!!!!重要 必须加 否则出错
		cs+=buffer;
		//   Sleep(200); 
	}
	//生成代理配置
	cs+="ProxyEnable ";
	cs+=p->m_checkPE==1?"1":"0";
	cs+=" ProxyServer ";
	cs+=p->m_addr;
	cs+=" Port ";
	cs+=p->m_port;
	cs+="\r\nAUEnable ";
	cs+=p->m_checkPA==1?"1":"0";
	cs+=" ID ";
	cs+=p->m_userid;
	cs+=" PWD ";
	cs+=p->m_pwd;
	cs+=" END";
//////
//	::AfxMessageBox(cs);
	p->m_info = "成功生成配置.";
	m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);	
	CFileDialog fDlg(false,NULL, "ot.ini", 
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"OneTouch Config(*.ini)|*.ini|All files(*.*)|*.*||",NULL);
	fDlg.m_ofn.lpstrInitialDir = ProgramPath;
	fDlg.m_ofn.lpstrTitle = "导出";
//	fDlg.SetControlText(IDOK, "d");	

	if(fDlg.DoModal()!=IDOK)
	{
		netshInUse = false;
		return 0;
	}
	else{		
		CString outPath=fDlg.GetPathName();
		//::AfxMessageBox(outPath);
		CFile mFile; 
		mFile.Open(outPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite); 
		mFile.Write(cs,cs.GetLength());
		mFile.Close(); 
		p->m_info = "导出配置完成。";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
	  }
	netshInUse = false;
	return 0;
}

UINT COnetouchDlg::autoThread( LPVOID pParam )
{
	COnetouchDlg* p = (COnetouchDlg*)pParam;//p访问OnetouchDlg的变量	
	SECURITY_ATTRIBUTES sa;
	HANDLE hRead,hWrite;
	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	sa.nLength = sizeof(SECURITY_ATTRIBUTES);
	sa.lpSecurityDescriptor = NULL;
	sa.bInheritHandle = TRUE;
	GetStartupInfo(&si);
	si.cb = sizeof(STARTUPINFO);
	si.wShowWindow = SW_HIDE;
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;		 
	char * strChar;  
	char buffer[4096] = {0}; 
	DWORD bytesRead;

//定义名称为cs调试出错???	CString cs;
	CString s;
	
	pcap_if_t *alldevs;
	pcap_if_t *d;
	pcap_t *adhandle;
	char errbuf[PCAP_ERRBUF_SIZE];
	u_int netmask;	
	u_int netip;
	struct bpf_program fcode;//存放编译后的过滤规则		

    /* Retrieve the device list */
    if (pcap_findalldevs( &alldevs, errbuf) == -1)
    {//从检测到的网卡中可以获得ip,mask等信息
		//        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
		p->m_info = "检测网卡失败.";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		ipAutoConfig = false;
        return 0;
    }    

	//////////////
    /* Print the list and select one */
	CString out;   
	int total = 0;
	for(d=alldevs; d; d=d->next)
    {    
		total++;
//		out += CString(d->name) + '\t' + CString(d->description) + '\n';
    }
	
    if(total==0)
    {
		pcap_freealldevs(alldevs);
		p->m_info = "没有检测到网卡.";
		m_MWnd->SendMessage(WM_UPDATEDATA, FALSE);
		ipAutoConfig = false;
        return 0;
    }   
	//
	string temp;
	int icount = 0, index=0, i=1;
	for(d=alldevs; d; d=d->next,i++)
    {    
		temp = d->description;
		transform(temp.begin(), temp.end(), temp.begin(), toupper);
		if(temp.find("VPN")!=-1 || temp.find("GENERIC")!=-1
			|| temp.find("DIALUP")!=-1 || temp.find("WIRELESS")!=-1
			|| temp.find("WIFI")!=-1 || temp.find("WAN")!=-1
			|| 	temp.find("VIRTUAL")!=-1)//筛选掉不可能的网卡名称
			continue;
		else{
			icount++;	//筛选后的个数		
			index = i;	//筛选后只有一个时,index为该网卡位置
		}
    }
	if(icount > 1){
		for(d=alldevs, i=1; d; d=d->next,i++)
		{
			temp = d->description;
			transform(temp.begin(), temp.end(), temp.begin(), toupper);
			if(temp.find("VPN")!=-1 || temp.find("GENERIC")!=-1
			|| temp.find("DIALUP")!=-1 || temp.find("WIRELESS")!=-1
			|| temp.find("WIFI")!=-1 || temp.find("WAN")!=-1
			|| 	temp.find("VIRTUAL")!=-1)//筛选掉不可能的网卡名称
				continue;
			out.Format(out+"%d %s\r",i,d->description);			
		}
		AfxMessageBox(out);
	}
	/*
	if(icount > 1)
	{//多个筛选后的网卡
		//选择剩余列表的第一个(暂时,后面考虑用户选择或者全部监听)
		for(d=alldevs, i=1; d; d=d->next,i++)
		{    
			temp = d->description;
			transform(temp.begin(), temp.end(), temp.begin(), toupper);
			if(temp.find("VPN")!=-1 || temp.find("GENERIC")!=-1 || temp.find("DIALUP")!=-1 ||
				temp.find("VIRTUAL")!=-1)//筛选掉不可能的网卡名称
				continue;
			else{					

⌨️ 快捷键说明

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