TraceRoute(tracert)源码(基于原始套接字实现)

 TraceRoute(tracert)源码(基于原始套接字实现) 佟强 2008.11.4

    TraceRoute实现原理 http://blog.csdn.net/microtong/archive/2008/11/04/3220450.aspx

 

本程序实现Windows下tracert的功能,程序使用原始套接字发送ICMP回显请求报文,本接收ICMP超时差错报文,和ICMP回显应答报文,得到每一跳的路由器IP地址和往返时间。

  1. //TraceRoute.h
  2. #ifndef _ITRACERT_H_
  3. #define _ITRACERT_H_
  4. #pragma pack(1)
  5. //IP数据报头
  6. typedef struct
  7. {
  8.  unsigned char hdr_len :4;  // length of the header
  9.  unsigned char version :4;  // version of IP
  10.  unsigned char tos;   // type of service
  11.  unsigned short total_len;  // total length of the packet
  12.  unsigned short identifier;  // unique identifier
  13.  unsigned short frag_and_flags; // flags
  14.  unsigned char ttl;   // time to live
  15.  unsigned char protocol;  // protocol (TCP, UDP etc)
  16.  unsigned short checksum;  // IP checksum
  17.  unsigned long sourceIP;  // source IP address
  18.  unsigned long destIP;   // destination IP address
  19. } IP_HEADER;
  20. //ICMP数据报头
  21. typedef struct
  22. {
  23.  BYTE type;  //8位类型
  24.  BYTE code;  //8位代码
  25.  USHORT cksum;  //16位校验和
  26.  USHORT id;   //16位标识符
  27.  USHORT seq;  //16位序列号
  28. } ICMP_HEADER;
  29. //解码结果
  30. typedef struct
  31. {
  32.  USHORT usSeqNo;   //包序列号
  33.  DWORD dwRoundTripTime; //往返时间
  34.  in_addr dwIPaddr;  //对端IP地址
  35. } DECODE_RESULT;
  36. #pragma pack()
  37. //ICMP类型字段
  38. const BYTE ICMP_ECHO_REQUEST = 8; //请求回显
  39. const BYTE ICMP_ECHO_REPLY  = 0; //回显应答
  40. const BYTE ICMP_TIMEOUT   = 11; //传输超时
  41. const DWORD DEF_ICMP_TIMEOUT = 3000; //默认超时时间,单位ms
  42. const 
  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值