📄 sender.cpp
字号:
#include "stdafx.h"
#include "Sender.h"
#include "resource.h"
#include "XAviPlay.h"
//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(pcap_t* handle, char *frame,int framelen)
{
int j=0;
//Win32 Specific. Sends a raw packet.
// This function allows to send a raw packet
// to the network. p is the interface that will be
// used to send the packet, buf contains the data
// of the packet to send (including the various
// protocol headers), size is the dimension of the
// buffer pointed by buf, i.e. the size of the packet
// to send. The MAC CRC doesn't need to be included,
// because it is transparently calculated and added by
// the network interface driver. The return value is 0
// if the packet is succesfully sent, -1 otherwise.
while(pcap_sendpacket(handle, (u_char *)frame,framelen)!=0)
{
//printf("出错,重发...!,j=%d\n",j);
//MessageBox(NULL,"出错,重发.","Error",MB_OK);
j++;
if(j>3)
{
return -1;
}
}
// printf("ack_therad\n");
return 1;
}
int write_memblock(char *buff,int len)
{
int writepos;
writepos=(MemWrite_pos+1)%MEMBLOCK_LEN;
//printf("Memwritepos=%d Memreadpos=%d\n",MemWrite_pos,MemRead_pos);
if(((writepos+2)%MEMBLOCK_LEN)!=(MemRead_pos+1)%MEMBLOCK_LEN)
{
memcpy(MemBlock[writepos].data,buff,len);
MemBlock[writepos].len=len;
MemWrite_pos=writepos;
return 1;
}
else
return -1;
}//write_memblock()
void send_thread(void)
{
//char err_buf[LIBNET_ERRBUF_SIZE];
int peerwin=0;
int readpos;
char frame[FRAME_LEN];
pcap_t *Lib_Handle;
Lib_Handle = GetOpenLink();
if (Lib_Handle==NULL)
{
//libnet_error(LIBNET_ERR_FATAL, "libnet_open_link_interface: %s\n", err_buf);
return;
}
while(1)
{
//printf("MReadpos=%d mWritepos=%d\n",MemRead_pos,MemWrite_pos);
if(MemRead_pos==MemWrite_pos||PeerWin==0)//empty
{
Sleep(2);
continue;
}
peerwin=PeerWin;
InterlockedExchange((PLONG)&PeerWin,1);
while((MemRead_pos==-1)&&(Send_Status=='n'))// when reset,handshake
{
ZeroMemory(frame,sizeof(frame));
*frame='d';
*(int *)(frame+1)=htonl(-1);
*(int *)(frame+5)=htonl(0);
send_packet(Lib_Handle,frame,HEAD_LEN+50);
Sleep(20);
}
Send_Status='t';
readpos=(MemRead_pos+1)%MEMBLOCK_LEN;
DWORD dwPrevious;
//printf("peerwin=%d readpos=%d %d\n",peerwin,readpos,(MemWrite_pos+1)%MEMBLOCK_LEN);
while((peerwin!=0)&&(
readpos!=(MemWrite_pos+1)%MEMBLOCK_LEN
)&&(Send_Status=='t'))
{
//ReleaseSemaphore(
// g_hSemaphore, // handle to semaphore
// 1, // increase count by one
// NULL);
memset(frame,0,sizeof(frame));
*frame='d';
*(int *)(frame+1)=htonl(readpos);
*(int *)(frame+5)=htonl(MemBlock[readpos].len);
memcpy(frame+HEAD_LEN,MemBlock[readpos].data,MemBlock[readpos].len);
// printf("send packet,data=%d\n",ntohl(*(int *)MemBlock[readpos].data));
if(send_packet(Lib_Handle,frame,MemBlock[readpos].len+HEAD_LEN)!=1)//send error
{
break;
}
peerwin--;
readpos=(readpos+1)%MEMBLOCK_LEN;
}//while
Sleep(2);
}//while(1)
pcap_close(Lib_Handle);
return;
}//send_thread()
void DecodeEthPkt_ack(u_char *p, struct pcap_pkthdr * pkthdr, u_char * pkt)
{
char flag;
int ack;
int peerwin;
flag=*pkt;
if((flag!='t')&&(flag!='f'))
return;
ack=ntohl(*(int *)(pkt+1));
peerwin=ntohl(*(int *)(pkt+5));
// TCHAR szMsg[1024];
//wsprintf(szMsg,"DecodeEthPkt_ack:ack:%d,peerwin:%d\n",ack,peerwin);
//OutputDebugString(szMsg);
if(peerwin!=PeerWin)
{
//if (!peerwin) {
// //MessageBox(NULL,"peerwin=ntohl(*(int *)(pkt+5)); is 0","InterlockedExchange((PLONG)&PeerWin,peerwin);",MB_OK);
//}
InterlockedExchange((PLONG)&PeerWin,peerwin);
}
if (ack !=55555) {
/*if((MemRead_pos==-1)&&(ack!=0))
return;*/
MemRead_pos=ack;
}
if(flag=='f')
{
Send_Status='f';
}
else{
Send_Status='t';
}
return;
}//DecodeEthPkt_ack
void recv_ack_thread(void)
{
char ebuf[PCAP_ERRBUF_SIZE];
pcap_t *pd;
pd = GetOpenLink();
if(!pd)
{
//printf("pcap_open_live error, please config %s's IP!\n",RECV_FXP);
return;
}
if(pcap_loop(pd,-1,(pcap_handler)DecodeEthPkt_ack,NULL) < 0)
{
perror("read error");
return;
}
return;
}//recv_ack_thread()
void input_thread(void)
{
const DWORD dwMax = 100;
TCHAR szMsg[256];
CSHProgressWnd dlg;
dlg.SetTitle ( _T("Hang on a sec...") );
dlg.SetAnimation ( IDR_FILECOPY );
dlg.SetCancelMessage ( _T("Cancelling this operation...") );
dlg.SetLineText ( 1, _T("Send files...") );
dlg.ShowModal( GetDesktopWindow() );
dlg.UpdateProgress ( 0, dwInFileSize );
int writelen,i=0;
//char buff[DATA_LEN];
BOOL bInputEnd=FALSE;
PacketInfo pi;
ZeroMemory((void*)&pi,DATA_LEN);
pi.dwFlag=FILE_HEADER_FLAG;
pi.dwLen=sizeof(FILE_HEADER);
pi.fh.dwFileLen=dwInFileSize;
pi.fh.dwFileHash=dwCheckSum;
_tcscpy(pi.fh.szFileName,szFileName);
//发送文件头信息
dlg.SetLineText ( 2, "发送文件头信息" );
DWORD dwWaitResult;
//retry:
// dwWaitResult = WaitForSingleObject(
// g_hSemaphore, // handle to semaphore
// 1); // zero-second time-out interval
//
// switch (dwWaitResult)
// {
// //队列中还有空位置
// case WAIT_OBJECT_0:
writelen=write_memblock((char*)&pi,DATA_LEN);
while(writelen==-1)
{
Sleep(1);
writelen=write_memblock((char*)&pi,DATA_LEN);
}
// break;
// //队列中无空位置,超时退出
//case WAIT_TIMEOUT:
// goto retry;
// break;
//}
//发送文件数据
pi.dwFlag=FILE_BODY_FLAG;
pi.dwLen=sizeof(FILE_BODY);
pi.fb.dwFileHash=dwCheckSum;
DWORD dwCount=0;
while(!bInputEnd)
{
if (dlg.HasUserCanceled()) {
bInputEnd=TRUE;
goto exit;
}
DWORD dwReturn;
//pi.fb.dwFileOffset =dwCount;
//dwWaitResult = WaitForSingleObject(
// g_hSemaphore, // handle to semaphore
// 1); // zero-second time-out interval
//switch (dwWaitResult)
//{
// //队列中还有空位置,读文件
//case WAIT_OBJECT_0:
ReadFile(inputfd,pi.fb.byData,DATA_LEN-16,&pi.fb.dwBlockSize,NULL);
if(pi.fb.dwBlockSize==0){
bInputEnd=TRUE;
goto exit;
}
writelen=write_memblock((char*)&pi,DATA_LEN);
while(writelen==-1)
{
Sleep(1);
writelen=write_memblock((char*)&pi,DATA_LEN);
}
// break;
// //队列中无空位置,继续等待空位置
//case WAIT_TIMEOUT:
//
//
// continue;
//
// break;
//}
dwCount+=pi.fb.dwBlockSize;
wsprintf ( szMsg, _T("已经发送%d字节\n"), dwCount );
dlg.SetLineText ( 2, szMsg );
dlg.UpdateProgress ( dwCount );
}//while
//发送文件结束标记
exit:
CloseHandle(inputfd);
pi.dwFlag=FILE_TAILER_FLAG;
pi.dwLen=sizeof(FILE_TAILER);
pi.ft.dwFileHash=dwCheckSum;
pi.ft.dwEndFlag=01;
pi.ft.dwFileLen=dwInFileSize;
retry2:
//dwWaitResult = WaitForSingleObject(
// g_hSemaphore, // handle to semaphore
// 1); // zero-second time-out interval
// switch (dwWaitResult)
// {
// // The semaphore object was signaled.
// case WAIT_OBJECT_0:
writelen=write_memblock((char*)&pi,DATA_LEN);
while(writelen==-1)
{
Sleep(1);
writelen=write_memblock((char*)&pi,DATA_LEN);
}
// break;
// // Semaphore was nonsignaled, so a time-out occurred.
//case WAIT_TIMEOUT:
//
// goto retry2;
//
// break;
//}
dlg.EndDialog();
beep(400);
}//input_thread()
void init_params(void)
{
MemRead_pos=-1;
MemWrite_pos=-1;
/*InitializeCriticalSection(
&win_mutex
);*/
// Create a semaphore with initial and max. counts of 10.
//g_hSemaphore = CreateSemaphore(
// NULL, // no security attributes
// MEMBLOCK_LEN, // initial count
// MEMBLOCK_LEN, // maximum count
// NULL); // unnamed semaphore
Send_Status='n';
}//init_params()
int InitSend(char* input_fifo,BOOL bFile)
{
char *progname;
int c, no;
DWORD dwThread;
if (send_id==0) {
send_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)send_thread,NULL,0,&dwThread);
}
Sleep(1000);
if (recv_ack_id==0) {
init_params();
recv_ack_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)recv_ack_thread,NULL,0,&dwThread);
}
if (!bFile) {
int writelen,i=0;
//char buff[DATA_LEN];
BOOL bInputEnd=FALSE;
PacketInfo pi;
ZeroMemory(&pi,DATA_LEN);
pi.dwFlag=MESSAGE_FLAG;
DWORD dwLen=_tcslen(input_fifo);
dwLen=dwLen>DATA_LEN-8?DATA_LEN-8:dwLen;
_tcsncpy(pi.szMessage,input_fifo,dwLen);
pi.dwLen=dwLen;
//发送文件头信息
writelen=write_memblock((char*)&pi,DATA_LEN);
while(writelen==-1)
{
Sleep(1);
writelen=write_memblock((char*)&pi,DATA_LEN);
//printf("Memwritepos=%d MemReadpos=%d\n",MemWrite_pos,MemRead_pos);
}
beep(400);
return 1;
}
WritePrivateProfileString("FileName","Send",input_fifo,"RawPacket.ini");
inputfd=CreateFile(input_fifo,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(inputfd==INVALID_HANDLE_VALUE)return 0;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(input_fifo, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE)
{
dwInFileSize=FindFileData.nFileSizeLow;
_tcscpy(szFileName,FindFileData.cFileName);
dwCheckSum=checksum((BYTE*)szFileName,_tcslen(szFileName));
}
else
return 0;
FindClose(hFind);
input_id=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)input_thread,NULL,0,&dwThread);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -