计算机网络课程设计编程实现简单的TCP协议分析器Word文件下载.docx
《计算机网络课程设计编程实现简单的TCP协议分析器Word文件下载.docx》由会员分享,可在线阅读,更多相关《计算机网络课程设计编程实现简单的TCP协议分析器Word文件下载.docx(19页珍藏版)》请在冰点文库上搜索。
{
u_int8_tether_dhost[6];
/*目的以太网地址*/
u_int8_tether_shost[6];
/*源以太网地址*/
u_int16_tether_type;
/*以太网类型*/
};
structarp_header
u_int16_tarp_hardware_type;
/*硬件类型*/
u_int16_tarp_protocol_type;
/*协议类型*/
u_int8_tarp_hardware_length;
/*硬件地址长度*/
u_int8_tarp_protocol_length;
/*协议地址长度*/
u_int16_tarp_operation_code;
/*操作码*/
u_int8_tarp_source_ethernet_address[6];
u_int8_tarp_source_ip_address[4];
/*源IP地址*/
u_int8_tarp_destination_ethernet_address[6];
u_int8_tarp_destination_ip_address[4];
/*目的IP地址*/
structip_header
#ifdefined(WORDS_BIGENDIAN)
u_int8_tip_version:
4,
/*版本*/
ip_header_length:
4;
/*首部长度*/
#else
u_int8_tip_header_length:
4,ip_version:
#endif
u_int8_tip_tos;
/*服务质量*/
u_int16_tip_length;
/*长度*/
u_int16_tip_id;
/*标识*/
u_int16_tip_off;
/*偏移*/
u_int8_tip_ttl;
/*生存时间*/
u_int8_tip_protocol;
u_int16_tip_checksum;
/*校验和*/
structin_addrip_souce_address;
structin_addrip_destination_address;
structudp_header
u_int16_tudp_source_port;
/*源端口号*/
u_int16_tudp_destination_port;
/*目的端口号*/
u_int16_tudp_length;
u_int16_tudp_checksum;
structtcp_header
u_int16_ttcp_source_port;
u_int16_ttcp_destination_port;
u_int32_ttcp_sequence_liuzhen;
/*序列号*/
u_int32_ttcp_acknowledgement;
/*确认序列号*/
#ifdefWORDS_BIGENDIAN
u_int8_ttcp_offset:
/*偏移*/
tcp_reserved:
/*未用*/
u_int8_ttcp_reserved:
tcp_offset:
u_int8_ttcp_flags;
/*标记*/
u_int16_ttcp_windows;
/*窗口大小*/
u_int16_ttcp_checksum;
u_int16_ttcp_urgent_pointer;
/*紧急指针*/
structicmp_header
u_int8_ticmp_type;
/*ICMP类型*/
u_int8_ticmp_code;
/*ICMP代码*/
u_int16_ticmp_checksum;
u_int16_ticmp_id;
/*标识符*/
u_int16_ticmp_sequence;
/*序列码*/
voidtcp_protocol_packet_callback(u_char*argument,conststructpcap_pkthdr*packet_header,constu_char*packet_content)
structtcp_header*tcp_protocol;
/*TCP协议变量*/
u_charflags;
intheader_length;
u_shortsource_port;
/*源端口*/
u_shortdestination_port;
/*目的端口*/
u_shortwindows;
u_shorturgent_pointer;
u_intsequence;
u_intacknowledgement;
/*确认号*/
u_int16_tchecksum;
tcp_protocol=(structtcp_header*)(packet_content+14+20);
/*获得TCP协议内容*/
source_port=ntohs(tcp_protocol->
tcp_source_port);
/*获得源端口*/
destination_port=ntohs(tcp_protocol->
tcp_destination_port);
/*获得目的端口*/
header_length=tcp_protocol->
tcp_offset*4;
sequence=ntohl(tcp_protocol->
tcp_sequence_liuzhen);
acknowledgement=ntohl(tcp_protocol->
tcp_acknowledgement);
/*确认序列码*/
windows=ntohs(tcp_protocol->
tcp_windows);
urgent_pointer=ntohs(tcp_protocol->
tcp_urgent_pointer);
flags=tcp_protocol->
tcp_flags;
checksum=ntohs(tcp_protocol->
tcp_checksum);
printf("
-------TCP协议-------\n"
);
源端口号:
%d\n"
source_port);
目的端口号:
destination_port);
switch(destination_port)
{
case80:
上层协议为HTTP协议\n"
break;
case21:
上层协议为FTP协议\n"
case23:
上层协议为TELNET协议\n"
case25:
上层协议为SMTP协议\n"
case110:
上层协议POP3协议\n"
default:
}
printf("
序列码:
%u\n"
sequence);
确认号:
acknowledgement);
首部长度:
header_length);
保留:
tcp_protocol->
tcp_reserved);
标记:
"
if(flags&
0x08)
PSH"
0x10)
ACK"
0x02)
SYN"
0x20)
URG"
0x01)
FIN"
0x04)
RST"
\n"
窗口大小:
windows);
校验和:
checksum);
紧急指针:
urgent_pointer);
}
voidip_protocol_packet_callback(u_char*argument,conststructpcap_pkthdr*packet_header,constu_char*packet_content)
structip_header*ip_protocol;
/*IP协议变量*/
u_intheader_length;
u_intoffset;
u_chartos;
ip_protocol=(structip_header*)(packet_content+14);
/*获得IP协议内容*/
checksum=ntohs(ip_protocol->
ip_checksum);
/*获得校验和*/
header_length=ip_protocol->
ip_header_length*4;
/*获得长度*/
tos=ip_protocol->
ip_tos;
/*获得服务质量*/
offset=ntohs(ip_protocol->
ip_off);
/*获得偏移*/
if(ip_protocol->
ip_protocol==6)
-----------IP协议-----------\n"
版本号:
ip_protocol->
ip_version);
服务质量:
tos);
总长度:
ntohs(ip_protocol->
ip_length));
标识:
ip_id));
偏移:
(offset&
0x1fff)*8);
生存时间:
ip_ttl);
协议类型:
ip_protocol);
上层协议为TCP协议\n"
printf("
源IP地址:
%s\n"
inet_ntoa(ip_protocol->
ip_souce_address));
/*获得源IP地址*/
目的IP地址:
ip_destination_address));
/*获得目的IP地址*/
voidethernet_protocol_packet_callback(u_char*argument,conststructpcap_pkthdr*packet_header,constu_char*packet_content)
staticintpacket_number=1;
/*数据包个数,静态变量*/
u_shortethernet_type;
structether_header*ethernet_protocol;
/*获得偏移*/
/*以太网协议变量*/
ethernet_protocol=(structether_header*)packet_content;
ethernet_type=ntohs(ethernet_protocol->
ether_type);
/*获得以太网类型*/
if(ethernet_type==0x0800&
&
ip_protocol->
u_char*mac_string;
/*以太网地址*/
**************************************************\n"
捕获第%d个TCP网络数据包\n"
packet_number);
捕获时间:
%s"
ctime((consttime_t*)&
packet_header->
ts.tv_sec));
/*获得捕获数据包的时间*/
数据包长度:
packet_header->
len);
--------以太网协议--------\n"
/*获得以太网协议内容*/
类型:
%04x\n"
ethernet_type);
源以太网地址:
\n"
mac_string=ethernet_protocol->
ether_shost;
%02x:
%02x\n"
*mac_string,*(mac_string+1),*(mac_string+2),*(mac_string+3),*(mac_string+4),*(mac_string+5));
/*获得源以太网地址*/
目的以太网地址:
ether_dhost;
/*获得目的以太网地址*/
ip_protocol_packet_callback(argument,packet_header,packet_content);
packet_number++;
printf("
voidmain()
pcap_t*pcap_handle;
/*Winpcap句柄*/
charerror_content[PCAP_ERRBUF_SIZE];
/*存储错误信息*/
char*net_interface;
/*网络接口*/
structbpf_programbpf_filter;
/*BPF过滤规则*/
charbpf_filter_string[]="
;
/*过滤规则字符串*/
bpf_u_int32net_mask;
/*掩码*/
bpf_u_int32net_ip;
/*网路地址*/
net_interface=pcap_lookupdev(error_content);
/*获得可用的网络接口*/
pcap_lookupnet(net_interface,&
net_ip,&
net_mask,error_content);
/*获得网络地址和掩码地址*/
pcap_handle=pcap_open_live(net_interface,BUFSIZ,1,1,error_content);
/*打开网路接口*/
pcap_compile(pcap_handle,&
bpf_filter,bpf_filter_string,0,net_ip);
/*编译BPF过滤规则*/
pcap_setfilter(pcap_handle,&
bpf_filter);
/*设置过滤规则*/
if(pcap_datalink(pcap_handle)!
=DLT_EN10MB)
return;
pcap_loop(pcap_handle,-1,ethernet_protocol_packet_callback,NULL);
/*注册回调函数,循环捕获网络数据包,利用回调函数来处理每个数据包*/
pcap_close(pcap_handle);
/*关闭Winpcap操作*/
七、测试数据
本地局域网IP数据包
八、测试情况
程序运行结果图:
结论
通过两周的课程设计,增强了我的实际动手能力,通过实际的编程整合串联了我所学到的知识。
另外我还学到了作为编程人员的一些基本素质,这为我毕业后找工作奠定了基础。
通过做TCP协议分析器学习的很多网络编程知识:
1.学会了winpcap网络数据报捕获开发包的使用;
2.绑定网卡函数bind();
3.数据接受函数recv();
4.Windows套接字编程;
5.学习了原始套接字编程的基本机制;
6.学习对数据包进行协议分析的基本方法。
通过做TCP协议分析器学习的很多VisualC++Windows编程知识:
1.学会了Windows常用数据结构的使用;
2.学到了一些用VisualC++Windows编程的技巧。
参考文献
[1]甘玲邱劲《面向对象技术与VisualC++》清华大学出版社
[2]任哲《MFCWindows应用程序设计》清华大学出版社
[3]《计算机网络》北京:
机械工业出版社
[4]《VisualC++网络通信编程实用案例精选》人民邮电出版社
[5]《windows程序设计》北京大学出版社