路由追踪Tracert
- 格式:doc
- 大小:161.00 KB
- 文档页数:8
实验5 Tracert 程序
实验目的
1、掌握ICMP协议的基本工作原理,熟悉ICMP报头格式与各字段的含义。
2、掌握Tracert程序的基本功能、原理与实现方法。
3、掌握Tracert程序设计与软件编程方法。
Tracert的实现原理:
1、IP报头中TTL字段的含义和作用:生存时间字段,限制IP报文在
网络中的停留时间(跳站数)。
2、Tracert程序利用TTL字段,通过从1开始递增TTL字段值,并
接收与解析从各跳路由器发回的超时差错报文来收集路由信息。
实验环境(设备)
操作系统:Windows 7
开发语言:C++
编译环境:Mc Visual vc6.0
实验内容
1、初始化Winsock2网络环境
2、解析命令行参数
3、创建原始套接字raw socket
4、设置raw socket 接收超时属性
5、定义IP和ICMP报头数据结构
6、填充ICMP回显请求消息
7、设置IP报头的TTL字段
8、按TTL递增的顺序发送ICMP请求
9、接收ICMP差错报文或ICMP回显应答
10、对接收报文进行解析处理
11、使用ICMP.dll库发送ICMP报文
程序源代码
第一个:
#ifndef _ITRACERT_H_
#define _ITRACERT_H_
//IP数据报头
typedef struct
{
unsigned char hdr_len :4; // length of the header
unsigned char version :4; // version of IP
unsigned char tos; // type of service
unsigned short identifier; // unique identifier
unsigned short frag_and_flags; // flags
unsigned char ttl; // time to live
unsigned char protocol; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum
unsigned long sourceIP; // source IP address
unsigned long destIP; // destination IP address
} IP_HEADER;
//ICMP数据报头
typedef struct
{
BYTE type; //8位类型
BYTE code; //8位代码
USHORT cksum; //16位校验和
USHORT id; //16位标识符
USHORT seq; //16位序列号
} ICMP_HEADER;
//解码结果
typedef struct
{
USHORT usSeqNo; //包序列号
DWORD dwRoundTripTime; //往返时间
in_addr dwIPaddr; //对端IP地址
} DECODE_RESULT;
//ICMP类型字段
const BYTE ICMP_ECHO_REQUEST = 8; //请求回显
const BYTE ICMP_ECHO_REPLY = 0; //回显应答
const BYTE ICMP_TIMEOUT = 11; //传输超时
const DWORD DEF_ICMP_TIMEOUT = 3000; //默认超时时间,单位ms
const int DEF_ICMP_DATA_SIZE = 32; //默认ICMP数据部分长度
const int MAX_ICMP_PACKET_SIZE = 1024; //最大ICMP数据报的大小
const int DEF_MAX_HOP = 30; //最大跳站数
USHORT GenerateChecksum(USHORT* pBuf, int iSize);
BOOL DecodeIcmpResponse(char* pBuf, int iPacketSize, DECODE_RESULT& stDecodeResult);
#ifndef _ITRACERT_H_
#define _ITRACERT_H_
//IP数据报头
typedef struct
{
unsigned char hdr_len :4; // length of the header
unsigned char version :4; // version of IP
unsigned char tos; // type of service
unsigned short identifier; // unique identifier
unsigned short frag_and_flags; // flags
unsigned char ttl; // time to live
unsigned char protocol; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum
unsigned long sourceIP; // source IP address
unsigned long destIP; // destination IP address
} IP_HEADER;
//ICMP数据报头
typedef struct
{
BYTE type; //8位类型
BYTE code; //8位代码
USHORT cksum; //16位校验和
USHORT id; //16位标识符
USHORT seq; //16位序列号
} ICMP_HEADER;
//解码结果
typedef struct
{
USHORT usSeqNo; //包序列号
DWORD dwRoundTripTime; //往返时间
in_addr dwIPaddr; //对端IP地址
} DECODE_RESULT;
//ICMP类型字段
const BYTE ICMP_ECHO_REQUEST = 8; //请求回显
const BYTE ICMP_ECHO_REPLY = 0; //回显应答
const BYTE ICMP_TIMEOUT = 11; //传输超时
const DWORD DEF_ICMP_TIMEOUT = 3000; //默认超时时间,单位ms
const int DEF_ICMP_DATA_SIZE = 32; //默认ICMP数据部分长度
const int MAX_ICMP_PACKET_SIZE = 1024; //最大ICMP数据报的大小
const int DEF_MAX_HOP = 30; //最大跳站数
USHORT GenerateChecksum(USHORT* pBuf, int iSize);
BOOL DecodeIcmpResponse(char* pBuf, int iPacketSize, DECODE_RESULT& stDecodeResult);
第二个程序:
#include
#include
//These defines & structure definitions are taken from the
//"ipexport.h" header file as provided with the Platform SDK.
//Including them here allows you to compile the code without
//the need to have the full Platform SDK installed.