📄 sender.cpp
字号:
#include "stdafx.h"
#include "Sender.h"
#include "resource.h"
#include "XAviPlay.h"
#ifdef DEBUG
#define Trace MyTrace
#else
#define Trace __noop
#endif
pcap_t* g_fp=NULL;
HANDLE g_hEvent=NULL;
int g_nBlockCount=0;
void MyTrace(char* fmt, ...)
{
char szBuffer[2048];
va_list argptr;
va_start(argptr,fmt);
vsprintf(szBuffer,fmt,argptr);
va_end(argptr);
OutputDebugString(szBuffer);
}
//CheckSum:计算校验和的子函数
BOOL IsWindowsNT()
{
BOOL bRet = FALSE;
BOOL bOsVersionInfoEx;
OSVERSIONINFOEX osvi;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return bRet;
}
if (osvi.dwPlatformId & VER_PLATFORM_WIN32_NT )
{
bRet = TRUE;
}
return bRet;
}
BOOL IsWindows2k()
{
BOOL bRet = FALSE;
BOOL bOsVersionInfoEx;
OSVERSIONINFOEX osvi;
// Try calling GetVersionEx using the OSVERSIONINFOEX structure,
// If that fails, try using the OSVERSIONINFO structure.
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
// If OSVERSIONINFOEX doesn't work, try OSVERSIONINFO.
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
return bRet;
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT && osvi.dwMajorVersion >= 5)
{
bRet = TRUE;
}
return bRet;
}
void __declspec(naked) beepInAsm(void)
{
__asm
{
pushad
mov bx, 3000h
mov cx, ax
mov al, 0b6h
out 43h, al
mov dx, 0012h
mov ax, 34dch
div cx
out 42h, al
mov al, ah
out 42h, al
in al, 61h
mov ah, al
or al, 03h
out 61h, al
l1:
mov ecx, 4680
l2:
loop l2
dec bx
jnz l1
mov al, ah
out 61h, al
popad
iretd
}
}
DWORDLONG IDT,SaveGate;
#define ExceptionUsed 9
WORD OurGate[4]={0,0x28,0x0ee00,0};
BOOL beep(DWORD dwFreq)
{
if(IsWindowsNT()){
Beep(dwFreq,500);
return TRUE;
}
__asm{
mov eax,offset beepInAsm
mov [OurGate],ax
shr eax,16
mov [OurGate+6],ax
sidt fword ptr IDT ; fetch IDT register
mov ebx, dword ptr [IDT+2] ; ebx -> IDT
add ebx, 8*ExceptionUsed ; Ebx -> IDT entry of ExceptionUsed
mov edi,offset SaveGate
mov esi,ebx
movsd
movsd
mov edi,ebx
mov esi,offset OurGate
movsd
movsd
mov eax ,dwFreq
int ExceptionUsed ; cause exception
mov edi,ebx
mov esi,offset SaveGate
movsd
movsd
}
return TRUE;
}
DWORD checksum(BYTE *buffer, int size)
{
unsigned long cksum=0;
while(size >1)
{
cksum+=*buffer++;
size -=sizeof(BYTE);
}
if(size )
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (DWORD)(~cksum);
}
void GetNetcardName(pcap_if_t *d,char* netchardsource )
{
pcap_addr_t *a;
for(a=d->addresses;a;a=a->next) {
//printf("\tAddress Family: #%d\n",a->addr->sa_family);
switch(a->addr->sa_family)
{
case AF_INET:
/*printf("\tAddress Family Name: AF_INET\n");*/
if (a->addr){
_tcscpy(netchardsource,d->name);
}
break;
default:
break;
}
}
}
pcap_t* GetOpenLink(){
pcap_if_t *alldevs;
pcap_if_t *d;
pcap_t* fp;
char errbuf[PCAP_ERRBUF_SIZE+1];
char netchardsource [1024];
if (pcap_findalldevs_ex("rpcap://", NULL, &alldevs, errbuf) == -1)
{
MessageBox(NULL,"WinPCap程序加载失败,请重新运行安装程序安装WinPCap","提示",MB_OK|MB_ICONSTOP);
return NULL ;
}
for(d=alldevs;d;d=d->next)
{
GetNetcardName(d,netchardsource);
}
if ( (fp= pcap_open(netchardsource/*"\\Device\\NPF_{8065DF3F-B15B-4EE1-BFEF-0A8893E9DABE}"*/, // name of the device
256, // portion of the packet to capture (only the first 100 bytes)
PCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode
1, // read timeout
NULL, // authentication on the remote machine
errbuf // error buffer
) ) == NULL)
{
// ATLTRACE("\nUnable to open the adapter. %s is not supported by WinPcap\n",netchardsource);
return NULL;
}
return fp;
}
int send_packet(char *frame,int framelen)
{
int j=0;
while(pcap_sendpacket(g_fp, (u_char *)frame,framelen)!=0)
{
j++;
if(j>3)
{
return -1;
}
}
// printf("ack_therad\n");
return 1;
}
int write_memblock(char *buff,int len)
{
__try{
MemNode m;
//Trace("Write Memblock :WritePos%d\n",MemWrite_pos);
memcpy(m.data,buff,len);
m.len=len;
m.bUsed=1;
MemBlock.push_back(m);
ReleaseSemaphore(
g_hSemaphore, // handle to semaphore
1, // increase count by one
NULL);
return 1;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
Trace("%s文件,%s函数,%d行,错误号码:%d",__FILE__,__FUNCTION__,__LINE__,GetLastError());
}
return 0;
}//write_memblock()
void send_thread(void)
{
//char err_buf[LIBNET_ERRBUF_SIZE];
char frame[FRAME_LEN];
__try{
/*Trace("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);*/
DWORD dwWaitResult;
//// Try to enter the semaphore gate.
//dwWaitResult = WaitForSingleObject(
// hSemaphore, // handle to semaphore
// 0L); // zero-second time-out interval
while(1)
{
dwWaitResult = WaitForSingleObject(g_hSemaphore,2);
MemNode m;
//Trace("Write Memblock :WritePos%d\n",MemWrite_pos);
//Trace("%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__);
//switch (dwWaitResult)
//{
// // The semaphore object was signaled.
// case WAIT_OBJECT_0:{
if(MemBlock.empty()){
m=MemBlock.front();
MemBlock.pop_front();
Trace("%s:%s:%d len:%d\n",__FILE__,__FUNCTION__,__LINE__,m.len);
while(1){
if(send_packet(m.data,m.len)!=1)//send error
{
break;
}
DWORD dwResult=WaitForSingleObject(g_hEvent,2000);
if(dwResult==WAIT_OBJECT_0)
break;
if(dwResult=WAIT_TIMEOUT)continue;
}
Sleep(10);
}
}//while
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
Trace("%s文件,%s函数,%d行,错误号码:%d",__FILE__,__FUNCTION__,__LINE__,GetLastError());
}
return;
}//send_thread()
void DecodeEthPkt_ack(u_char *p, struct pcap_pkthdr * pkthdr, u_char * pkt)
{
PacketInfo* pi=(PacketInfo*)pkt;
//Trace("%s文件\t%s函数\t%d行\tpkthdr->len:%d\n",__FILE__,__FUNCTION__,__LINE__,pkthdr->len);
if(pkthdr->len==92 && pi->dwFlag==ANSWER_FLAG){
//Trace("%s文件%s函数%d行\tdwFlag]:%d\tdwMemPos:%d\n",__FILE__,__FUNCTION__,__LINE__,pi->dwFlag,pi->dwMemPos);
SetEvent(g_hEvent);
}
return;
}//DecodeEthPkt_ack
void recv_ack_thread(void)
{
char ebuf[PCAP_ERRBUF_SIZE];
__try{
pcap_t* fp=GetOpenLink();
if(pcap_loop(fp,-1,(pcap_handler)DecodeEthPkt_ack,NULL) < 0)
{
perror("read error");
return;
}
pcap_close(fp);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
Trace("%s文件,%s函数,%d行,错误号码:%d",__FILE__,__FUNCTION__,__LINE__,GetLastError());
}
return;
}//recv_ack_thread()
void init_params(void)
{
MemBlock.clear();
g_hSemaphore = CreateSemaphore(
NULL, // no security attributes
0, // initial count
MEMBLOCK_LEN, // maximum count
NULL); // unnamed semaphore
g_hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
g_fp=GetOpenLink();
}//init_params()
int InitSend(/*char* input_fifo,BOOL bFile*/)
{
char *progname;
int c, no;
DWORD dwThread;
if (send_id==0) {
init_params();
send_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)send_thread,NULL,0,&dwThread);
}
if (recv_ack_id==0) {
recv_ack_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)recv_ack_thread,NULL,0,&dwThread);
}
return 0;
}
void EndSend()
{
TerminateThread(recv_ack_id,0);
TerminateThread(send_id,0);
recv_ack_id=0;
send_id=0;
CloseHandle(g_hSemaphore);
CloseHandle(g_hEvent);
pcap_close(g_fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -