TMainForm *MainForm;
//--------------------------------------------------------------------------- __fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner) {
WSADATA WSAData; BOOL flag = true; int nTimeout = 1000; char LocalName[16]; struct hostent *pHost; //检查 Winsock 版本号
if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0) throw Exception(\ //初始化 Raw Socket
if ((sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) == INVALID_SOCKET)
throw Exception(\ //设置IP头操作选项
if (setsockopt(sock, IPPROTO_IP, IP_HDRINCL, (char*)&flag, sizeof(flag)) == SOCKET_ERROR)
throw Exception(\ //获取本机名
if (gethostname((char*)LocalName, sizeof(LocalName)-1) == SOCKET_ERROR) throw Exception(\ //获取本地 IP 地址
if ((pHost = gethostbyname((char*)LocalName)) == NULL) throw Exception(\
addr_in.sin_addr = *(in_addr *)pHost->h_addr_list[0]; //IP addr_in.sin_family = AF_INET; addr_in.sin_port = htons(57274); //把 sock 绑定到本地地址上
if (bind(sock, (PSOCKADDR)&addr_in, sizeof(addr_in)) == SOCKET_ERROR) throw Exception(\ iSortDirection = 1; }
//--------------------------------------------------------------------------- __fastcall TMainForm::~TMainForm() {
WSACleanup(); }
//--------------------------------------------------------------------------- void __fastcall TMainForm::btnCtrlClick(TObject *Sender) {
TListItem *Item; DWORD dwValue; int nIndex = 0;
if (btnCtrl->Caption == \ {
dwValue = 1;
//设置 SOCK_RAW 为SIO_RCVALL,以便接收所有的IP包 if (ioctlsocket(sock, SIO_RCVALL, &dwValue) != 0) throw Exception(\ bStop = false;
btnCtrl->Caption = \ lsvPacket->Items->Clear(); } else {
dwValue = 0; bStop = true;
btnCtrl->Caption = \
//设置SOCK_RAW为SIO_RCVALL,停止接收 if (ioctlsocket(sock, SIO_RCVALL, &dwValue) != 0) throw Exception(\ }
while (!bStop) {
if (recv(sock, RecvBuf, BUFFER_SIZE, 0) > 0) {
nIndex++;
ip = *(IP*)RecvBuf;
tcp = *(TCP*)(RecvBuf + (ip.HdrLen & IP_HDRLEN_MASK)); Item = lsvPacket->Items->Add(); Item->Caption = nIndex;
Item->SubItems->Add(GetProtocolTxt(ip.Protocol)); Item->SubItems->Add(inet_ntoa(*(in_addr*)&ip.SrcAddr)); Item->SubItems->Add(inet_ntoa(*(in_addr*)&ip.DstAddr)); Item->SubItems->Add(tcp.SrcPort);
Item->SubItems->Add(tcp.DstPort); Item->SubItems->Add(ntohs(ip.TotalLen)); }
Application->ProcessMessages(); } }
//--------------------------------------------------------------------------- AnsiString __fastcall TMainForm::GetProtocolTxt(int Protocol) {
switch (Protocol) {
case IPPROTO_ICMP : //1 /* control message protocol */ return PROTOCOL_STRING_ICMP_TXT; case IPPROTO_TCP : //6 /* tcp */ return PROTOCOL_STRING_TCP_TXT;
case IPPROTO_UDP : //17 /* user datagram protocol */ return PROTOCOL_STRING_UDP_TXT; default :
return PROTOCOL_STRING_UNKNOWN_TXT; } }
//--------------------------------------------------------------------------- //*************************************************************************// //* Header File: WMain.h for WMain.cpp class TMainForm //*************************************************************************// //---------------------------------------------------------------------------
#ifndef WMainH #define WMainH
//--------------------------------------------------------------------------- #define BUFFER_SIZE 65535 #include
//--------------------------------------------------------------------------- class TMainForm : public TForm {
__published: // IDE-managed Components TPanel *Panel1; TButton *btnCtrl; TListView *lsvPacket; TLabel *Label1;
void __fastcall btnCtrlClick(TObject *Sender);
void __fastcall lsvPacketColumnClick(TObject *Sender, TListColumn *Column);
void __fastcall lsvPacketCompare(TObject *Sender, TListItem *Item1, TListItem *Item2, int Data, int &Compare); void __fastcall Label1Click(TObject *Sender);
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库sniffer技术原理及应用,包括编程方法和工具使用(2)在线全文阅读。
相关推荐: