mysniffview.cpp
上传用户:xakehao
上传日期:2013-08-28
资源大小:100k
文件大小:12k
- #include "stdafx.h"
- #include "mycap.h"
- #include "MycapDoc.h"
- #include "mysniffview.h"
- #include "mycapview.h"
- #include <vector>
- #include <pcap.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern int m_flag;
- using namespace std;
- typedef vector<const u_char *> v;
- extern v myvector;
- using namespace std;
- typedef vector<u_short> u;
- extern u mycount;
- using namespace std;
- typedef vector<PACKET> c;
- extern c mypacket;
- IMPLEMENT_DYNCREATE(mysniffview, CScrollView)
- mysniffview::mysniffview()
- {
- }
- mysniffview::~mysniffview()
- {
- }
- BEGIN_MESSAGE_MAP(mysniffview, CScrollView)
-
- END_MESSAGE_MAP()
- void mysniffview::OnInitialUpdate()
- {
- CScrollView::OnInitialUpdate();
- CSize sizeTotal;
- sizeTotal.cx = sizeTotal.cy = 500;//指定滚动刷新的范围
- SetScrollSizes(MM_TEXT, sizeTotal);
- }
- void mysniffview::OnDraw(CDC* pDC)
- {
- CMyCapDoc* pDoc = GetDocument();
- TEXTMETRIC font;
- pDC->GetTextMetrics(&font);
- LONG height= font.tmHeight;
- LONG width = font.tmMaxCharWidth;
- CRect myRect;
- GetClientRect(myRect);
- CRect myrect(0, 0, 100, 500);
- char ip_len[8] = "";
- char szLen[8] = "";
- char identifier[16] = "";
- char offset[8] = "";
- char live[8] = "";
- char proto[8] = "";
- char checksum[16] = "";
- char srcIP[16] = "";
- char desIP[16] = "";
- char ope[16] = "";
- char srcport[16] = "";
- char desport[16] = "";
- char dramlen[8] = "";
- char crc[8] = "";
- char tcpsport[16] = "";
- char tcpdport[16] = "";
- char sernum[16] = "";
- char acknum[16] = "";
- char winlen[8] = "";
- char point[8] = "";
- char type[8] = "";
- char code[8] = "";
- char checksm[16] = "";
- char identif[16] = "";
- char sernm[16] = "";
- if(pDoc->GetCount() != -1)
- {//当标准位不等于-1时,即此时选择了CListView窗口中的某一项
- int mycon = pDoc->GetCount();
- //读取存放在向量中的包头的信息,解开包的IP头
- pDC->TextOut(0, 0, "IP头");
- pDC->TextOut(0, height, "版本号:");
- pDC->TextOut(7*width, height, "4");
- pDC->TextOut(0, 2*height, "IP头长:");
- sprintf(ip_len, "%d", (mypacket[mycon].IpHead.ver_ihl & 0xf) * 4);
- pDC->TextOut(7*width, 2*height, ip_len);
- pDC->TextOut(0, 3*height, "服务类型:");
- pDC->TextOut(7*width, 3*height, mypacket[mycon].IpHead.tos);
- pDC->TextOut(0, 4*height, "总长:");
- sprintf(szLen, "%d", ntohs(mypacket[mycon].IpHead.tlen)+18);
- pDC->TextOut(7*width, 4*height, szLen);
- pDC->TextOut(0, 5*height, "标识:");
- sprintf(identifier, "%d", ntohs(mypacket[mycon].IpHead.identification));
- pDC->TextOut(7*width, 5*height, identifier);
- pDC->TextOut(0, 6*height, "分段偏移:");
- sprintf(offset, "%d", (char)(ntohs(mypacket[mycon].IpHead.flags_fo)) & 0x1fff);
- pDC->TextOut(7*width, 6*height, offset);
- pDC->TextOut(0, 7*height, "生命期(S):");
- sprintf(live, "%d", int(mypacket[mycon].IpHead.ttl));
- pDC->TextOut(7*width, 7*height, live);
- pDC->TextOut(0, 8*height, "协议:");
- sprintf(proto, "%d", int(mypacket[mycon].IpHead.proto));
- pDC->TextOut(7*width, 8*height, proto);
- if(int(mypacket[mycon].IpHead.proto) == 17)
- pDC->TextOut(8*width, 8*height, "(UDP)");
- if(int(mypacket[mycon].IpHead.proto) == 6)
- pDC->TextOut(8*width, 8*height, "(TCP)");
- if(int(mypacket[mycon].IpHead.proto) == 1)
- pDC->TextOut(8*width, 8*height, "(ICMP)");
- pDC->TextOut(0, 9*height, "头校验和:");
- sprintf(checksum, "%d", ntohs(mypacket[mycon].IpHead.crc));
- pDC->TextOut(7*width, 9*height, checksum);
- pDC->TextOut(0, 10*height, "源地址:");
- sprintf(srcIP, "%d.%d.%d.%d",
- mypacket[mycon].IpHead.saddr.byte1,
- mypacket[mycon].IpHead.saddr.byte2,
- mypacket[mycon].IpHead.saddr.byte3,
- mypacket[mycon].IpHead.saddr.byte4);
- pDC->TextOut(7*width, 10*height, srcIP);
- pDC->TextOut(0, 11*height, "目的地址:");
- sprintf(desIP, "%d.%d.%d.%d",
- mypacket[mycon].IpHead.daddr.byte1,
- mypacket[mycon].IpHead.daddr.byte2,
- mypacket[mycon].IpHead.daddr.byte3,
- mypacket[mycon].IpHead.daddr.byte4);
- pDC->TextOut(7*width, 11*height, desIP);
- if(int(mypacket[mycon].IpHead.proto) == 17)
- {//判断,如果IP包头的协议字段是17,表示是UDP包
- //从存放在向量中的包头中读取并接口UDP头
- pDC->TextOut(0, 13*height, "UDP头:");
- pDC->TextOut(0, 14*height, "源端口:");
- sprintf(srcport, "%d", ntohs(mypacket[mycon].UdpHead.sport));
- pDC->TextOut(7*width, 14*height, srcport);
- pDC->TextOut(0, 15*height, "目的端口:");
- sprintf(desport, "%d", ntohs(mypacket[mycon].UdpHead.dport));
- pDC->TextOut(7*width, 15*height, desport);
- pDC->TextOut(0, 16*height, "UDP长度:");
- sprintf(dramlen, "%d", ntohs(mypacket[mycon].UdpHead.len));
- pDC->TextOut(7*width, 16*height, dramlen);
- pDC->TextOut(0, 17*height, "UDP校验和:");
- sprintf(crc, "%d", ntohs(mypacket[mycon].UdpHead.crc));
- pDC->TextOut(7*width, 17*height, crc);
- }
- if(int(mypacket[mycon].IpHead.proto) == 6)
- {
- //判断,如果IP包头的协议字段是6,表示是TCP包
- //从存放在向量中的包头中读取并接口TCP头
- pDC->TextOut(0, 13*height, "TCP头:");
- pDC->TextOut(0, 14*height, "源端口:");
- sprintf(tcpsport, "%d", ntohs(mypacket[mycon].TcpHead.tcpsport));
- pDC->TextOut(7*width, 14*height, tcpsport);
- pDC->TextOut(0, 15*height, "目的端口:");
- sprintf(tcpdport, "%d", ntohs(mypacket[mycon].TcpHead.tcpdport));
- pDC->TextOut(7*width, 15*height, tcpdport);
- pDC->TextOut(0, 16*height, "序列号:");
- sprintf(sernum, "%uld", ntohl(mypacket[mycon].TcpHead.sernum));
- pDC->TextOut(7*width, 16*height, sernum);
- pDC->TextOut(0, 17*height, "确认号:");
- sprintf(acknum, "%uld", ntohl(mypacket[mycon].TcpHead.acknum));
- pDC->TextOut(7*width, 17*height, acknum);
- pDC->TextOut(0, 18*height, "URG位:");
- if(int(mypacket[mycon].TcpHead.bit & 0x20) == 0)
- pDC->TextOut(7*width, 18*height, "0");
- if(int(mypacket[mycon].TcpHead.bit & 0x20) == 32)
- pDC->TextOut(7*width, 18*height, "1");
- pDC->TextOut(0, 19*height, "ACK位:");
- if(int(mypacket[mycon].TcpHead.bit & 0x10) == 0)
- {
- pDC->TextOut(7*width, 19*height, "0");
- //AfxMessageBox("确认序号无效!");
- pDC->TextOut(0, 27*height, "确认序号无效!");
- }
- if(int(mypacket[mycon].TcpHead.bit & 0x10) == 16)
- pDC->TextOut(7*width, 19*height, "1");
- pDC->TextOut(0, 20*height, "PSH位:");
- if(int(mypacket[mycon].TcpHead.bit & 0x8) == 0)
- pDC->TextOut(7*width, 20*height, "0");
- if(int(mypacket[mycon].TcpHead.bit & 0x20) == 8)
- pDC->TextOut(7*width, 20*height, "1");
- pDC->TextOut(0, 21*height, "RST位:");
- if(int(mypacket[mycon].TcpHead.bit & 0x4) == 0)
- pDC->TextOut(7*width, 21*height, "0");
- if(int(mypacket[mycon].TcpHead.bit & 0x4) == 4)
- pDC->TextOut(7*width, 21*height, "1");
- pDC->TextOut(0, 22*height, "SYN位:");
- if(int(mypacket[mycon].TcpHead.bit & 0x2) == 0)
- {
- pDC->TextOut(7*width, 22*height, "0");
- //AfxMessageBox("没有建立连接或建立连接失败!");
- }
- if(int(mypacket[mycon].TcpHead.bit & 0x2) == 2)
- pDC->TextOut(7*width, 22*height, "1");
- pDC->TextOut(0, 23*height, "FIN位:");
- if(int(mypacket[mycon].TcpHead.bit & 0x1) == 0)
- pDC->TextOut(7*width, 23*height, "0");
- if(int(mypacket[mycon].TcpHead.bit & 0x1) == 1)
- pDC->TextOut(7*width, 23*height, "1");
- pDC->TextOut(0, 24*height, "窗口大小:");
- sprintf(winlen, "%d", ntohs(mypacket[mycon].TcpHead.winlen));
- pDC->TextOut(7*width, 24*height, winlen);
- pDC->TextOut(0, 25*height, "紧急指针:");
- sprintf(point, "%d", mypacket[mycon].TcpHead.point);
- pDC->TextOut(7*width, 25*height, point);
- }
- if(int(mypacket[mycon].IpHead.proto) == 1)
- {
- //判断,如果IP包头的协议字段是1,表示是ICMP包
- //从存放在向量中的包头中读取并接口ICMP头
- pDC->TextOut(0, 13*height, "ICMP");
- pDC->TextOut(0, 14*height, "类型:");
- sprintf(type, "%d", mypacket[mycon].IcmpHead.type);
- pDC->TextOut(7*width, 14*height, type);
- pDC->TextOut(0, 15*height, "代码:");
- sprintf(code, "%d", mypacket[mycon].IcmpHead.code);
- pDC->TextOut(7*width, 15*height, code);
- pDC->TextOut(0, 16*height, "校验和:");
- sprintf(checksm, "%d", ntohs(mypacket[mycon].IcmpHead.checksum));
- pDC->TextOut(7*width, 16*height, checksm);
- pDC->TextOut(0, 17*height, "标识符:");
- sprintf(identif, "%d", ntohs(mypacket[mycon].IcmpHead.identifier));
- pDC->TextOut(7*width, 17*height, identif);
- pDC->TextOut(0, 18*height, "序列号:");
- sprintf(sernm, "%d", ntohs(mypacket[mycon].IcmpHead.sernum));
- pDC->TextOut(7*width, 18*height, sernm);
- if(mypacket[mycon].IcmpHead.type == 0)
- {
- if(mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "回显应答(ping应答)");
- }
- if(mypacket[mycon].IcmpHead.type == 3)
- {
- switch (mypacket[mycon].IcmpHead.code)
- {
- case 0:
- pDC->TextOut(0, 20*height, "目标不可达");
- break;
- case 1:
- pDC->TextOut(0, 20*height, "主机不可达");
- break;
- case 2:
- pDC->TextOut(0, 20*height, "协议不可达");
- break;
- case 3:
- pDC->TextOut(0, 20*height, "端口不可达");
- break;
- case 4:
- pDC->TextOut(0, 20*height, "需要进行分片但不能设置分片比特");
- break;
- case 5:
- pDC->TextOut(0, 20*height, "源站选路失败");
- break;
- case 6:
- pDC->TextOut(0, 20*height, "目的网络不认识");
- break;
- case 7:
- pDC->TextOut(0, 20*height, "目的主机不认识");
- break;
- case 8:
- pDC->TextOut(0, 20*height, "源主机被隔离");
- break;
- case 9:
- pDC->TextOut(0, 20*height, "目的网络被强制禁止");
- break;
- case 10:
- pDC->TextOut(0, 20*height, "目的主机被强制禁止");
- break;
- case 11:
- pDC->TextOut(0, 20*height, "由于服务类型TOS,网络不可达");
- break;
- case 12:
- pDC->TextOut(0, 20*height, "由于服务类型TOS,主机不可达");
- break;
- case 13:
- pDC->TextOut(0, 20*height, "由于过滤,通信被强制禁止");
- break;
- case 14:
- pDC->TextOut(0, 20*height, "主机越权");
- break;
- case 15:
- pDC->TextOut(0, 20*height, "优先权中止生效");
- break;
- }
- }
- if(mypacket[mycon].IcmpHead.type == 4)
- {
- if(mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "源端被关闭");
- }
- if(mypacket[mycon].IcmpHead.type == 5)
- {
- switch(mypacket[mycon].IcmpHead.code)
- {
- case 0:
- pDC->TextOut(0, 20*height, "对网络重定向");
- break;
- case 1:
- pDC->TextOut(0, 20*height, "对主机重定向");
- break;
- case 2:
- pDC->TextOut(0, 20*height, "对服务类型和网络重定向");
- break;
- case 3:
- pDC->TextOut(0, 20*height, "对服务类型和主机重定向");
- break;
- }
- }
- if(mypacket[mycon].IcmpHead.type == 8 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "请求回显(ping请求)");
- if(mypacket[mycon].IcmpHead.type == 9 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "路由器通告");
- if(mypacket[mycon].IcmpHead.type == 10 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "路由器请求");
- if(mypacket[mycon].IcmpHead.type == 11)
- {
- switch(mypacket[mycon].IcmpHead.code)
- {
- case 0:
- pDC->TextOut(0, 20*height, "传输期间生存时间为0");
- break;
- case 1:
- pDC->TextOut(0, 20*height, "在数据报组装期间生存时间为0");
- break;
- }
- }
- if(mypacket[mycon].IcmpHead.type == 12)
- {
- switch(mypacket[mycon].IcmpHead.code)
- {
- case 0:
- pDC->TextOut(0, 20*height, "坏的IP首部");
- break;
- case 1:
- pDC->TextOut(0, 20*height, "缺少必需的选项");
- break;
- }
- }
- if(mypacket[mycon].IcmpHead.type == 13 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "时间戳请求");
- if(mypacket[mycon].IcmpHead.type == 14 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "时间戳应答");
- if(mypacket[mycon].IcmpHead.type == 15 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "信息请求");
- if(mypacket[mycon].IcmpHead.type == 16 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "信息应答");
- if(mypacket[mycon].IcmpHead.type == 17 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "地址掩码请求");
- if(mypacket[mycon].IcmpHead.type == 18 && mypacket[mycon].IcmpHead.code == 0)
- pDC->TextOut(0, 20*height, "地址掩码应答");
- }
- }
- }
- #ifdef _DEBUG
- void mysniffview::AssertValid() const
- {
- CScrollView::AssertValid();
- }
- void mysniffview::Dump(CDumpContext& dc) const
- {
- CScrollView::Dump(dc);
- }
- #endif //_DEBUG
- CMyCapDoc* mysniffview::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyCapDoc)));
- return (CMyCapDoc*)m_pDocument;
- }