阅读:1368回复:1
为什么没贴上(给HUYG)。
HUYG大侠。。你说要贴附件给我的。。就是有关PACKET的文篇,为什么我没收到附近,,是不是_TMP.TXT可无法下载啊。麻烦你再发给我一下我的EM:walker_zjs@netease.com 。
|
|
|
沙发#
发布于:2001-12-17 20:17
HUYG大侠。。你说要贴附件给我的。。就是有关PACKET的文篇,为什么我没收到附近,,是不是_TMP.TXT可无法下载啊。麻烦你再发给我一下我的EM:walker_zjs@netease.com 。 我考,我也没有办法,估计是我操作失误,可是那个tmp1.txt 已经被我顺手删除了,我还是贴在这里吧。 还好那段解释没有放在附件里面,否则就真的是太惨了。 typedef struct _PACKET { OVERLAPPED OverLapped; BYTE Buffer[1514]; DWORD Length; int iAdapterNum; // 该PACKET是哪个ADAPTER抓的 } PACKET, *LPPACKET; BOOLEAN RecvPacket( BYTE *pbuf, int *piAdapterNum, PULONG BytesReceived ) { static int iReadAdapterNum=0; BOOLEAN Result; static PACKET Packet[32]; static HANDLE hEvent[32]; DWORD dwByteReceive; HANDLE hEventTemp; int i,j,k; if(g_bFirstCall) // if first call ,let\'s call 32 times readfile first { for(i=0;i<32;i++) { Packet.OverLapped.Offset=0; Packet.OverLapped.OffsetHigh=0; Packet.OverLapped.hEvent=CreateEvent( 0, TRUE, FALSE, NULL ); // manual reset,initial=false hEvent=Packet.OverLapped.hEvent; Packet.Length =1514; // if someone shit send a packet>>1514,what\'s happen? Result=ReadFile( Adapter[iReadAdapterNum].hFile, Packet.Buffer, Packet.Length, &dwByteReceive, &Packet.OverLapped ); Packet.iAdapterNum =iReadAdapterNum; iReadAdapterNum++; iReadAdapterNum=iReadAdapterNum%g_iAdapterNum; // need use a global } g_bFirstCall=false; } i= WaitForMultipleObjects( // which read return? 32, hEvent, false, // wait untill one hevent signal INFINITE // wait ever ); if(i==WAIT_FAILED) return false; for(j=0;j<32;j++) if(Packet[j].OverLapped.hEvent ==hEvent) break; // which read return? k=j; dwByteReceive=0; Result=GetOverlappedResult( Adapter[Packet[k].iAdapterNum].hFile, &Packet[k].OverLapped, &dwByteReceive, false ); if(!Result) { AfxMessageBox(\"!!!\"); return false; } *piAdapterNum=Packet[k].iAdapterNum ; // return value memcpy((void *)pbuf,(void *)Packet[k].Buffer,dwByteReceive); *BytesReceived=dwByteReceive; CloseHandle(Packet[k].OverLapped.hEvent); for(j=i;j<32;i++) hEvent=hEvent[++j]; hEventTemp=CreateEvent(0, TRUE, 0, NULL); if(!hEventTemp) { MessageBox(NULL,\"Can not create event\",NULL,MB_OK); return false; } Packet[k].OverLapped.hEvent=hEventTemp; memset(Packet[k].Buffer,0,1514); Packet[k].Length =1514; hEvent[31]=hEventTemp; // k返回了,就再读K一次 Result=ReadFile( Adapter[Packet[k].iAdapterNum].hFile, Packet[k].Buffer, Packet[k].Length, &dwByteReceive, &Packet[k].OverLapped ); return Result; } //////////////////////////////////////////////////////// typedef struct _IMDEVN_ETMSG { UINT Type; union { UCHAR OriginalPacketHeader[IPHEADER_MAX_LEN+8]; // 因为icmp包需要ip头部和数据的前8个字节,参见TCP/IP V1 P113. struct { USHORT AdapterNameBuffer[MAX_ADAPTER_LEN]; UINT AdapterNameLength; }; struct { USHORT LogMsg[MAX_LOG_LEN]; UINT LogMsgLen; }; }; }IMDEVENTMSG,*PIMDEVENTMSG; typedef struct _ANSY_IMD_EVENT_MSG { OVERLAPPED OverLapped; UCHAR Buffer[sizeof(IMDEVENTMSG)]; } ANSY_IMDEVENTMSG, *PANSY_IMDEVENTMSG; // // hEventStop must be created by such code: // hEventStop = CreateEvent( // 0, // TRUE, // FALSE, // NULL // ); // when caller want the GetEventMsg exit the block, // just SetEvent(hEventStop); BOOL GetEventMsg( IN HANDLE hDev, IN HANDLE hEventStop, OUT PIMDEVENTMSG EventMsg ) { BOOL bRet; static ANSY_IMDEVENTMSG Packet[32]; static HANDLE hEvent[32+1]; static bool s_bFirstCall = TRUE; DWORD cb; HANDLE hEventNew; int i,j,k; if(s_bFirstCall) // if first call ,let\'s call 32 times readfile first { for(i=0;i<32;i++) { Packet.OverLapped.Offset=0; Packet.OverLapped.OffsetHigh=0; Packet.OverLapped.hEvent=CreateEvent( 0, TRUE, FALSE, NULL ); // manual reset,initial=false hEvent=Packet.OverLapped.hEvent; bRet = DeviceIoControl( hDev, GET_EVENT_MSG, NULL, 0, Packet.Buffer, sizeof(IMDEVENTMSG), &cb, &Packet.OverLapped ); } hEvent[32]=hEventStop; s_bFirstCall=false; } i= WaitForMultipleObjects( // which read return? 33, hEvent, false, // wait untill one hevent signal INFINITE // wait forever ); if(i==WAIT_FAILED) return false; if(i==32) { ASSERT(hEvent == hEventStop); return true; // hEventStop raise } for(j=0;j<32;j++) { if(Packet[j].OverLapped.hEvent ==hEvent) break; // which read return? } k=j; cb=0; bRet=GetOverlappedResult( hDev, &Packet[k].OverLapped, &cb, false ); if(!bRet) { printf(\"GetOverlappedResult failed!!!\\n\"); return false; } ASSERT(cb == sizeof(IMDEVENTMSG)); memcpy((void *)EventMsg,(void *)Packet[k].Buffer,cb); CloseHandle(Packet[k].OverLapped.hEvent); for(j=i;j<32;i++) hEvent=hEvent[++j]; hEventNew=CreateEvent(0, TRUE, 0, NULL); if(!hEventNew) { printf(\"Can not create event!\\n\"); return false; } Packet[k].OverLapped.hEvent=hEventNew; memset(Packet[k].Buffer,0,sizeof(IMDEVENTMSG)); hEvent[31]=hEventNew; // k返回了,就再读K一次 bRet = DeviceIoControl( hDev, GET_EVENT_MSG, NULL, 0, Packet[k].Buffer, sizeof(IMDEVENTMSG), &cb, &Packet[k].OverLapped ); return bRet; } |
|
|