984724405计算机网络课程设计编程实现简单的TCP协议分析器_第1页
984724405计算机网络课程设计编程实现简单的TCP协议分析器_第2页
984724405计算机网络课程设计编程实现简单的TCP协议分析器_第3页
984724405计算机网络课程设计编程实现简单的TCP协议分析器_第4页
984724405计算机网络课程设计编程实现简单的TCP协议分析器_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、编程实现简单的tcp协议分析器一、问题描述编程实现简单的tcp协议分析器,tcp协议分析器是一种用于监督和跟踪网络活动的诊断工具,它从局域网中抓取ip数据包,并对它进行分析得到相应的头部信息,过滤tcp包进行分析,得到tcp包的相应信息。二、基本要求1.利用原始套接字实现简单的tcp协议分析器。2.系统功能包括: 2.1 原始套接字与网卡绑定,并接收流经网卡的所有数据包; 2.2 对数据包进行分析以获得源ip地址和目的ip地址; 2.3 对tcp segment进行分析以获得其首部详细信息; 2.4 显示分析结果。3 建议使用vc+。三、设计思想tcp协议的数据传送程序是由二个子程序组成的。也

2、可以看成是服务器端程序和客户端程序,其中:服务器端程序的功能是侦听端口号,接收远 程主要的tcp连接申请,并接收远程主机传送来的文字数据。另外一个子程序,也就是所谓的客户端程序,主要实现向网络的远程主机提出tcp连接申请。程序利用原始套接字抓取局域网中的ip包。tcp协议分析器实现了sniffer的一部分功能。而sniffer的工作原理是:1. 把网卡置于混杂模式;2. 捕获数据包;3. 分析数据包。raw socket: 原始套接字可以用它来发送和接收 ip 层以上的原始数据包, 如 icmp, tcp, udp等。 四、系统结构(1)pcap_addr描述网络接口地址;(2)pcap_pk

3、thdr用来描述每个捕获到的数据包的基本信息;(3)int_pcaplookupnet获取网络地址和网络掩码;(4)int_pcaploop循环捕获网络数据包,直到遇到错误或满足退出条件;(5)pcap_t* pcap_open_dead构造一个libpcap句柄。五、程序流程(或模块划分)六、源程序#include pcap.hstruct ether_header u_int8_t ether_dhost6; /* 目的以太网地址 */ u_int8_t ether_shost6; /* 源以太网地址 */ u_int16_t ether_type; /* 以太网类型 */;struct

4、arp_header u_int16_t arp_hardware_type; /* 硬件类型 */ u_int16_t arp_protocol_type; /* 协议类型 */ u_int8_t arp_hardware_length; /* 硬件地址长度 */ u_int8_t arp_protocol_length; /* 协议地址长度 */ u_int16_t arp_operation_code; /* 操作码 */ u_int8_t arp_source_ethernet_address6; /* 源以太网地址 */ u_int8_t arp_source_ip_address4

5、; /* 源ip地址 */ u_int8_t arp_destination_ethernet_address6; /* 目的以太网地址 */ u_int8_t arp_destination_ip_address4; /* 目的ip地址 */;struct ip_header #if defined(words_bigendian) u_int8_t ip_version: 4, /* 版本 */ ip_header_length: 4; /* 首部长度 */ #else u_int8_t ip_header_length: 4, ip_version: 4; #endif u_int8_t

6、 ip_tos; /* 服务质量 */ u_int16_t ip_length; /* 长度 */ u_int16_t ip_id; /* 标识 */ u_int16_t ip_off; /* 偏移 */ u_int8_t ip_ttl; /* 生存时间 */ u_int8_t ip_protocol; /* 协议类型 */ u_int16_t ip_checksum; /* 校验和 */ struct in_addr ip_souce_address; /* 源ip地址 */ struct in_addr ip_destination_address; /* 目的ip地址 */;struct

7、 udp_header u_int16_t udp_source_port; /* 源端口号 */ u_int16_t udp_destination_port; /* 目的端口号 */ u_int16_t udp_length; /* 长度 */ u_int16_t udp_checksum; /* 校验和 */;struct tcp_header u_int16_t tcp_source_port; /* 源端口号 */ u_int16_t tcp_destination_port; /* 目的端口号 */ u_int32_t tcp_sequence_liuzhen; /* 序列号 */

8、 u_int32_t tcp_acknowledgement; /* 确认序列号 */ #ifdef words_bigendian u_int8_t tcp_offset: 4, /* 偏移 */ tcp_reserved: 4; /* 未用 */ #else u_int8_t tcp_reserved: 4, /* 未用 */ tcp_offset: 4; /* 偏移 */ #endif u_int8_t tcp_flags; /* 标记 */ u_int16_t tcp_windows; /* 窗口大小 */ u_int16_t tcp_checksum; /* 校验和 */ u_int

9、16_t tcp_urgent_pointer; /* 紧急指针 */;struct icmp_header u_int8_t icmp_type; /* icmp类型 */ u_int8_t icmp_code; /* icmp代码 */ u_int16_t icmp_checksum; /* 校验和 */ u_int16_t icmp_id; /* 标识符 */ u_int16_t icmp_sequence; /* 序列码 */;void tcp_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *pa

10、cket_header, const u_char *packet_content) struct tcp_header *tcp_protocol; /* tcp协议变量 */ u_char flags; /* 标记 */ int header_length; /* 长度 */ u_short source_port; /* 源端口 */ u_short destination_port; /* 目的端口 */ u_short windows; /* 窗口大小 */ u_short urgent_pointer; /* 紧急指针 */ u_int sequence; /* 序列号 */ u_

11、int acknowledgement; /* 确认号 */ u_int16_t checksum; /* 校验和 */ tcp_protocol = (struct tcp_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_proto

12、col-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_prot

13、ocol-tcp_flags; /* 标识 */ checksum = ntohs(tcp_protocol-tcp_checksum); /* 校验和 */ printf(- tcp协议 -n); printf(源端口号:%dn, source_port); printf(目的端口号:%dn, destination_port); switch (destination_port) case 80: printf(上层协议为http协议n); break; case 21: printf(上层协议为ftp协议n); break; case 23: printf(上层协议为telnet协议n)

14、; break; case 25: printf(上层协议为smtp协议n); break; case 110: printf(上层协议pop3协议n); break; default: break; printf(序列码:%un, sequence); printf(确认号:%un, acknowledgement); printf(首部长度:%dn, header_length); printf(保留:%dn, tcp_protocol-tcp_reserved); printf(标记:); if (flags &0x08) printf(psh ); if (flags &0x10) p

15、rintf(ack ); if (flags &0x02) printf(syn ); if (flags &0x20) printf(urg ); if (flags &0x01) printf(fin ); if (flags &0x04) printf(rst ); printf(n); printf(窗口大小:%dn, windows); printf(校验和:%dn, checksum); printf(紧急指针:%dn, urgent_pointer);void ip_protocol_packet_callback(u_char *argument, const struct p

16、cap_pkthdr *packet_header, const u_char *packet_content) struct ip_header *ip_protocol; /* ip协议变量 */ u_int header_length; /* 长度 */ u_int offset; /* 偏移 */ u_char tos; /* 服务质量 */ u_int16_t checksum; /* 校验和 */ ip_protocol = (struct ip_header*)(packet_content + 14); /* 获得ip协议内容 */ checksum = ntohs(ip_pr

17、otocol-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) printf(- ip协议 -n); printf(版本号:%dn, ip_protocol-ip_version); printf(首部长度:%dn, header_length)

18、; printf(服务质量:%dn, tos); printf(总长度:%dn, ntohs(ip_protocol-ip_length); printf(标识:%dn, ntohs(ip_protocol-ip_id); printf(偏移:%dn, (offset &0x1fff) *8); printf(生存时间:%dn, ip_protocol-ip_ttl); printf(协议类型:%dn, ip_protocol-ip_protocol); printf(上层协议为tcp协议n);printf(校验和:%dn, checksum); printf(源ip地址:%sn, inet_

19、ntoa(ip_protocol-ip_souce_address); /* 获得源ip地址 */ printf(目的ip地址:%sn, inet_ntoa(ip_protocol-ip_destination_address); /* 获得目的ip地址 */ void ethernet_protocol_packet_callback(u_char *argument, const struct pcap_pkthdr *packet_header, const u_char *packet_content)static int packet_number = 1; /* 数据包个数,静态变

20、量 */u_short ethernet_type; /* 以太网类型 */ struct ether_header *ethernet_protocol;struct ip_header *ip_protocol; /* ip协议变量 */ u_int header_length; /* 长度 */ u_int offset; /* 偏移 */ u_char tos; /* 服务质量 */ u_int16_t checksum; /* 校验和 */ ip_protocol = (struct ip_header*)(packet_content + 14); /* 获得ip协议内容 */ c

21、hecksum = ntohs(ip_protocol-ip_checksum); /* 获得校验和 */ header_length = ip_protocol-ip_header_length *4; /* 获得长度 */ tos = ip_protocol-ip_tos; /* 获得服务质量 */ offset = ntohs(ip_protocol-ip_off); /* 获得偏移 */ /* 以太网协议变量 */ethernet_protocol = (struct ether_header*)packet_content;ethernet_type = ntohs(ethernet

22、_protocol-ether_type);/* 获得以太网类型 */if(ethernet_type=0x0800 & ip_protocol-ip_protocol=6)u_char *mac_string; /* 以太网地址 */ printf(*n); printf(捕获第%d个tcp网络数据包n, packet_number); printf(捕获时间:n); printf(%s, ctime(const time_t*) &packet_header-ts.tv_sec); /* 获得捕获数据包的时间 */ printf(数据包长度:n); printf(%dn, packet_h

23、eader-len); printf(- 以太网协议 -n); /* 获得以太网协议内容 */ printf(类型:n); printf(%04xn, ethernet_type); printf(源以太网地址: n); mac_string = ethernet_protocol-ether_shost; printf(%02x:%02x:%02x:%02x:%02x:%02xn, *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5)

24、; /* 获得源以太网地址 */ printf(目的以太网地址: n); mac_string = ethernet_protocol-ether_dhost; printf(%02x:%02x:%02x:%02x:%02x:%02xn, *mac_string, *(mac_string + 1), *(mac_string + 2), *(mac_string + 3), *(mac_string + 4), *(mac_string + 5); /* 获得目的以太网地址 */ ip_protocol_packet_callback(argument, packet_header, pac

25、ket_content);packet_number+; printf(*n); void main() pcap_t *pcap_handle; /* winpcap句柄 */ char error_contentpcap_errbuf_size; /* 存储错误信息 */ char *net_interface; /* 网络接口 */ struct bpf_program bpf_filter; /* bpf过滤规则 */ char bpf_filter_string = ; /* 过滤规则字符串 */ bpf_u_int32 net_mask; /* 掩码 */ bpf_u_int32 net_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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论