Traceroute(路由追踪) --- 的原理及实现

现实世界中的网络是由无数的计算机和路由器组成的一张的大网,应用的数据包在发送到服务器之前都要经过层层的路由转发。而Traceroute是一种常规的网络分析工具,用来定位到目标主机之间的所有路由器。

原理

在介绍Traceroute的原理之前,需要了解几个技术名词:

  • IP协议

    IP协议是TCP/IP协议族中最核心的部分,它的作用是在两台主机之间传输数据,所有上层协议的数据(HTTP、TCP、UDP等)都会被封装在一个个的IP数据包中被发送到网络上。

  • ICMP
    ICMP全称为互联网控制报文协议,它常用于传递错误信息,ICMP协议是IP层的一部分,它的报文也是通过IP数据包来传输的。

  • TTL
    TTL(time-to-live)是IP数据包中的一个字段,它指定了数据包最多能经过几次路由器。从我们源主机发出去的数据包在到达目的主机的路上要经过许多个路由器的转发,在发送数据包的时候源主机会设置一个TTL的值,每经过一个路由器TTL就会被减去一,当TTL为0的时候该数据包会被直接丢弃(不再继续转发),并发送一个超时ICMP报文给源主机。

具体到traceroute的实现细节上,有两种不同的方案:

基于UDP实现

在基于UDP的实现中,客户端发送的数据包是通过UDP协议来传输的,会使用一个大于30000的端口号,服务器在收到这个数据包的时候会返回一个端口不可达的ICMP错误信息,客户端通过判断收到的错误信息是TTL超时还是端口不可达来判断数据包是否到达目标主机,如果报TTL超时,说明数据包还未到达目标主机,如果收到的消息是端口不可达,说明数据包已经到达目标主机。具体的流程如图所示:

 对于上图,解读如下:

  • 客户端发送一个TTL为1,端口号大于30000的UDP数据包,到达第一站路由器之后TTL被减去1,返回了一个超时的ICMP数据包,客户端得到第一跳路由器的地址。
  • 客户端发送一个TTL为2的数据包,在第二跳的路由器节点处超时,得到第二跳路由器的地址。
  • 客户端发送一个TTL为3的数据包,数据包成功到达目标主机,返回一个端口不可达错误,traceroute结束。

基于ICMP实现

在基于ICMP协议实现的过程中,我们不使用UDP协议,而是直接发送一个ICMP回显请求(echo request)数据包,服务器在收到回显请求的时候会向客户端发送一个ICMP回显应答(echo reply)数据包,在这之后的流程还是跟第一种方案一样。这样就避免了我们的traceroute数据包被服务器的防火墙策略墙掉。采用这种方案的实现流程如下:

对于上图,解读如下:

  1. 客户端发送一个TTL为1的ICMP请求回显数据包,在第一跳的时候超时并返回一个ICMP超时数据包,得到第一跳的地址。
  2. 客户端发送一个TTL为2的ICMP请求回显数据包,得到第二跳的地址。
  3. 客户端发送一个TTL为3的ICMP请求回显数据包,到达目标主机,目标主机返回一个ICMP回显应答,traceroute结束。

可以看出与第一种实现相比,区别主要在发送的数据包类型以及对于结束的判断上,大体的流程还是一致的。

 

DDU ICMP controls consist of 3 controls : TDDUOSPING - A non blocking PING protocolTDDUOSTRACEROUTE - A non blocking TraceRoute ProtocolTDDUOSDNS - A non blocking ARP/RARP controlAbout TDDUOSDNS :Host names are resolved to IP addresses, and IP address can be resolved to host names. If you enter an IP into the ADDRESS property, the HOST property will be completed, and the OnHostLookup event will occur. If you enter an IP or a HOST name into the HOST property, the ADDRESS property will be completed, and the OnAddressLookup event will occure. The runtime property OK can be queried to see if the lookup was successful. To reduce lookup overhead, an IP/Hostname cache can be used by setting the UseIPCache property to true. The cache is only persistent for the current session, and maynot be saved between sessions. You can use FlushIPCache method to clear the cache.When combined with the other controls in this package, the delay normally associated with connecting a socket can be avoided. Each control must have a unique TDDUOSDNS control.About the Demo :The demo project shows how to use each control. The TDDUOSDNS control is shown both in combination with other controls, and used independantly. DNS not an ICMP protocol, but it is included to prevent the windows application from blocking.Blocking is when your windows program stops responding to messages, waiting for a call to return. Normally, all calls blocks for a very short time, and is not noticable - but when you have to wait for 5 or 10 seconds or even longer, the application can appear to be crashed. By using threads, callback messages, and events, I have illustrated how it is possible to avoid blocking in an application.These controls form a custom set of TCP/IP controls I have been developing over the past year. The ICMP controls were written over the period of a week, and I consulted several references for information. I would like to thank Quinn Shute for his excellent book "Windows Socket Network Programming&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值